fixed keypair sample and test case

Signed-off-by: Lorenz Herzberger <lorenzherzberger@gmail.com>
This commit is contained in:
Lorenz Herzberger 2023-06-14 15:05:42 +02:00
parent ec07bb075e
commit dc0de3732d
No known key found for this signature in database
GPG Key ID: FA5EE906EB55316A
2 changed files with 28 additions and 6 deletions

View File

@ -1,19 +1,20 @@
package sample package sample
import ( import (
"encoding/hex"
machinetypes "planetmint-go/x/machine/types" machinetypes "planetmint-go/x/machine/types"
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519" "github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
sdk "github.com/cosmos/cosmos-sdk/types" sdk "github.com/cosmos/cosmos-sdk/types"
) )
// KeyPair returns a sample private / public keypair // KeyPair returns a sample private / public keypair
func KeyPair() (string, string) { func KeyPair() (string, string) {
secret := "Hello World!" secret := "Don't tell anybody"
sk := ed25519.GenPrivKeyFromSecret([]byte(secret)) sk := secp256k1.GenPrivKeyFromSecret([]byte(secret))
pk := sk.PubKey() pk := sk.PubKey()
return hex.EncodeToString(sk.Bytes()), hex.EncodeToString(pk.Bytes())
return sk.String(), pk.String()
} }
// AccAddress returns a sample account address // AccAddress returns a sample account address

View File

@ -2,6 +2,8 @@ package keeper_test
import ( import (
"context" "context"
"crypto/sha256"
"encoding/hex"
"testing" "testing"
keepertest "planetmint-go/testutil/keeper" keepertest "planetmint-go/testutil/keeper"
@ -9,6 +11,7 @@ import (
"planetmint-go/x/asset/keeper" "planetmint-go/x/asset/keeper"
"planetmint-go/x/asset/types" "planetmint-go/x/asset/types"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
sdk "github.com/cosmos/cosmos-sdk/types" sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
@ -19,8 +22,26 @@ func setupMsgServer(t testing.TB) (types.MsgServer, context.Context) {
} }
func TestMsgServerNotarizeAsset(t *testing.T) { func TestMsgServerNotarizeAsset(t *testing.T) {
_, pk := sample.KeyPair() sk, pk := sample.KeyPair()
msg := types.NewMsgNotarizeAsset(pk, "cid", "sign", "pubkey") cid := "cid"
skBytes, err := hex.DecodeString(sk)
if err != nil {
assert.Equal(t, true, false)
}
privKey := &secp256k1.PrivKey{Key: skBytes}
cidBytes, _ := hex.DecodeString(cid)
hash := sha256.Sum256(cidBytes)
sign, err := privKey.Sign(hash[:])
if err != nil {
assert.Equal(t, true, false)
}
signatureHex := hex.EncodeToString(sign)
msg := types.NewMsgNotarizeAsset(pk, cid, signatureHex, pk)
msgServer, ctx := setupMsgServer(t) msgServer, ctx := setupMsgServer(t)
res, err := msgServer.NotarizeAsset(ctx, msg) res, err := msgServer.NotarizeAsset(ctx, msg)
if assert.NoError(t, err) { if assert.NoError(t, err) {