adjust test suite for ta validation

Signed-off-by: Lorenz Herzberger <lorenzherzberger@gmail.com>
This commit is contained in:
Lorenz Herzberger 2023-09-06 15:13:06 +02:00
parent b1f572ddee
commit e33452476f
No known key found for this signature in database
GPG Key ID: FA5EE906EB55316A
10 changed files with 99 additions and 52 deletions

View File

@ -120,6 +120,7 @@ import (
daomodule "planetmint-go/x/dao" daomodule "planetmint-go/x/dao"
daomodulekeeper "planetmint-go/x/dao/keeper" daomodulekeeper "planetmint-go/x/dao/keeper"
daomoduletypes "planetmint-go/x/dao/types" daomoduletypes "planetmint-go/x/dao/types"
// this line is used by starport scaffolding # stargate/app/moduleImport // this line is used by starport scaffolding # stargate/app/moduleImport
appparams "planetmint-go/app/params" appparams "planetmint-go/app/params"
@ -311,6 +312,7 @@ func New(
feegrant.StoreKey, evidencetypes.StoreKey, ibctransfertypes.StoreKey, icahosttypes.StoreKey, feegrant.StoreKey, evidencetypes.StoreKey, ibctransfertypes.StoreKey, icahosttypes.StoreKey,
capabilitytypes.StoreKey, group.StoreKey, icacontrollertypes.StoreKey, consensusparamtypes.StoreKey, capabilitytypes.StoreKey, group.StoreKey, icacontrollertypes.StoreKey, consensusparamtypes.StoreKey,
machinemoduletypes.StoreKey, machinemoduletypes.TAIndexKey, machinemoduletypes.IssuerPlanetmintIndexKey, machinemoduletypes.IssuerLiquidIndexKey, machinemoduletypes.StoreKey, machinemoduletypes.TAIndexKey, machinemoduletypes.IssuerPlanetmintIndexKey, machinemoduletypes.IssuerLiquidIndexKey,
machinemoduletypes.TrustAnchorKey,
assetmoduletypes.StoreKey, assetmoduletypes.StoreKey,
daomoduletypes.StoreKey, daomoduletypes.StoreKey,
// this line is used by starport scaffolding # stargate/app/storeKey // this line is used by starport scaffolding # stargate/app/storeKey

View File

@ -68,7 +68,8 @@ func (s *E2ETestSuite) SetupSuite() {
s.Require().NoError(s.network.WaitForNextBlock()) s.Require().NoError(s.network.WaitForNextBlock())
machine := sample.Machine(sample.Name, sample.PubKey) pubKey, prvKey := sample.KeyPair()
machine := sample.Machine(sample.Name, pubKey, prvKey)
machineJSON, err := json.Marshal(&machine) machineJSON, err := json.Marshal(&machine)
s.Require().NoError(err) s.Require().NoError(err)

View File

@ -21,7 +21,8 @@ func (s *E2ETestSuite) TestAttestMachineREST() {
s.Require().NoError(err) s.Require().NoError(err)
// Create Attest Machine TX // Create Attest Machine TX
machine := sample.Machine(sample.Name, sample.PubKey) pubKey, prvKey := sample.KeyPair()
machine := sample.Machine(sample.Name, pubKey, prvKey)
msg := machinetypes.MsgAttestMachine{ msg := machinetypes.MsgAttestMachine{
Creator: addr.String(), Creator: addr.String(),
Machine: &machine, Machine: &machine,
@ -42,7 +43,7 @@ func (s *E2ETestSuite) TestAttestMachineREST() {
s.Require().NoError(err) s.Require().NoError(err)
s.Require().Equal(uint32(0), txRes.TxResponse.Code) s.Require().Equal(uint32(0), txRes.TxResponse.Code)
queryMachineUrl := fmt.Sprintf("%s/planetmint-go/machine/get_machine_by_public_key/%s", baseURL, sample.PubKey) queryMachineUrl := fmt.Sprintf("%s/planetmint-go/machine/get_machine_by_public_key/%s", baseURL, pubKey)
queryMachineRes, err := testutil.GetRequest(queryMachineUrl) queryMachineRes, err := testutil.GetRequest(queryMachineUrl)
s.Require().NoError(err) s.Require().NoError(err)

View File

@ -73,20 +73,19 @@ func (s *E2ETestSuite) TestAttestMachine() {
val := s.network.Validators[0] val := s.network.Validators[0]
// register Ta // register Ta
prvKey, pubKey := sample.KeyPair()
machine := sample.Machine(sample.Name, sample.PubKey) ta := sample.TrustAnchor(pubKey)
machineJSON, err := json.Marshal(&machine) taJSON, err := json.Marshal(&ta)
s.Require().NoError(err) s.Require().NoError(err)
args := []string{ args := []string{
fmt.Sprintf("--%s=%s", flags.FlagChainID, s.network.Config.ChainID), fmt.Sprintf("--%s=%s", flags.FlagChainID, s.network.Config.ChainID),
fmt.Sprintf("--%s=%s", flags.FlagFrom, sample.Name), fmt.Sprintf("--%s=%s", flags.FlagFrom, sample.Name),
fmt.Sprintf("--%s=%s", flags.FlagFees, sample.Fees), fmt.Sprintf("--%s=%s", flags.FlagFees, sample.Fees),
"--yes", "--yes",
string(machineJSON), string(taJSON),
} }
out, err := clitestutil.ExecTestCLICmd(val.ClientCtx, machinecli.CmdRegisterTrustAnchor(), args)
out, err := clitestutil.ExecTestCLICmd(val.ClientCtx, machinecli.CmdAttestMachine(), args)
s.Require().NoError(err) s.Require().NoError(err)
txResponse, err := clitestutil.GetTxResponseFromOut(out) txResponse, err := clitestutil.GetTxResponseFromOut(out)
@ -96,10 +95,34 @@ func (s *E2ETestSuite) TestAttestMachine() {
rawLog, err := clitestutil.GetRawLogFromTxResponse(val, txResponse) rawLog, err := clitestutil.GetRawLogFromTxResponse(val, txResponse)
s.Require().NoError(err) s.Require().NoError(err)
assert.Contains(s.T(), rawLog, "planetmintgo.machine.MsgRegisterTrustAnchor")
machine := sample.Machine(sample.Name, pubKey, prvKey)
machineJSON, err := json.Marshal(&machine)
s.Require().NoError(err)
args = []string{
fmt.Sprintf("--%s=%s", flags.FlagChainID, s.network.Config.ChainID),
fmt.Sprintf("--%s=%s", flags.FlagFrom, sample.Name),
fmt.Sprintf("--%s=%s", flags.FlagFees, sample.Fees),
"--yes",
string(machineJSON),
}
out, err = clitestutil.ExecTestCLICmd(val.ClientCtx, machinecli.CmdAttestMachine(), args)
s.Require().NoError(err)
txResponse, err = clitestutil.GetTxResponseFromOut(out)
s.Require().NoError(err)
s.Require().NoError(s.network.WaitForNextBlock())
rawLog, err = clitestutil.GetRawLogFromTxResponse(val, txResponse)
s.Require().NoError(err)
assert.Contains(s.T(), rawLog, "planetmintgo.machine.MsgAttestMachine") assert.Contains(s.T(), rawLog, "planetmintgo.machine.MsgAttestMachine")
args = []string{ args = []string{
sample.PubKey, pubKey,
} }
_, err = clitestutil.ExecTestCLICmd(val.ClientCtx, machinecli.CmdGetMachineByPublicKey(), args) _, err = clitestutil.ExecTestCLICmd(val.ClientCtx, machinecli.CmdGetMachineByPublicKey(), args)

View File

@ -54,8 +54,8 @@ func AssetKeeper(t testing.TB) (*keeper.Keeper, sdk.Context) {
id := sample.MachineIndex(pk, ppk, lpk) id := sample.MachineIndex(pk, ppk, lpk)
mk.EXPECT().GetMachineIndex(ctx, pk).Return(id, true).AnyTimes() mk.EXPECT().GetMachineIndex(ctx, pk).Return(id, true).AnyTimes()
mk.EXPECT().GetMachineIndex(ctx, sk).Return(id, false).AnyTimes() mk.EXPECT().GetMachineIndex(ctx, sk).Return(id, false).AnyTimes()
mk.EXPECT().GetMachine(ctx, id).Return(sample.Machine(pk, pk), true).AnyTimes() mk.EXPECT().GetMachine(ctx, id).Return(sample.Machine(pk, pk, sk), true).AnyTimes()
mk.EXPECT().GetMachine(ctx, sk).Return(sample.Machine(pk, pk), false).AnyTimes() mk.EXPECT().GetMachine(ctx, sk).Return(sample.Machine(pk, pk, sk), false).AnyTimes()
k := keeper.NewKeeper( k := keeper.NewKeeper(
cdc, cdc,

View File

@ -54,22 +54,30 @@ func Secp256k1AccAddress() sdk.AccAddress {
return sdk.AccAddress(addr) return sdk.AccAddress(addr)
} }
func Machine(name, pubKey string) machinetypes.Machine { func Machine(name, pubKey string, prvKey string) machinetypes.Machine {
metadata := Metadata() metadata := Metadata()
_, liquidPubKey := ExtendedKeyPair(config.LiquidNetParams) _, liquidPubKey := ExtendedKeyPair(config.LiquidNetParams)
_, planetmintPubKey := ExtendedKeyPair(config.PlmntNetParams) _, planetmintPubKey := ExtendedKeyPair(config.PlmntNetParams)
prvKeyBytes, _ := hex.DecodeString(prvKey)
sk := &secp256k1.PrivKey{Key: prvKeyBytes}
pubKeyBytes := []byte(pubKey)
sign, _ := sk.Sign(pubKeyBytes)
signatureHex := hex.EncodeToString(sign)
m := machinetypes.Machine{ m := machinetypes.Machine{
Name: name, Name: name,
Ticker: name + "_ticker", Ticker: name + "_ticker",
Domain: "lab.r3c.network", Domain: "lab.r3c.network",
Reissue: true, Reissue: true,
Amount: 1000, Amount: 1000,
Precision: 8, Precision: 8,
IssuerPlanetmint: planetmintPubKey, IssuerPlanetmint: planetmintPubKey,
IssuerLiquid: liquidPubKey, IssuerLiquid: liquidPubKey,
MachineId: pubKey, MachineId: pubKey,
Metadata: &metadata, Metadata: &metadata,
Type: 1, Type: 1,
MachineIdSignature: signatureHex,
} }
return m return m
} }
@ -121,8 +129,8 @@ func ExtendedKeyPair(cfg chaincfg.Params) (string, string) {
return xprivKey.String(), xpubKey.String() return xprivKey.String(), xpubKey.String()
} }
func TrustAnchor() machinetypes.TrustAnchor { func TrustAnchor(pubkey string) machinetypes.TrustAnchor {
return machinetypes.TrustAnchor{ return machinetypes.TrustAnchor{
Pubkey: PubKey, Pubkey: pubkey,
} }
} }

View File

@ -0,0 +1,22 @@
package util
import (
"encoding/hex"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
)
func ValidateSignature(message string, signature string, publicKey string) bool {
// Convert the message, signature, and public key from hex to bytes
messageBytes := []byte(message)
signatureBytes, _ := hex.DecodeString(signature)
publicKeyBytes, _ := hex.DecodeString(publicKey)
// Create a secp256k1 public key object
pubKey := &secp256k1.PubKey{Key: publicKeyBytes}
// Verify the signature
isValid := pubKey.VerifySignature(messageBytes, signatureBytes)
return isValid
}

View File

@ -2,12 +2,11 @@ package keeper
import ( import (
"context" "context"
"encoding/hex"
"errors" "errors"
"planetmint-go/util"
"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"
) )
@ -20,7 +19,7 @@ func (k msgServer) NotarizeAsset(goCtx context.Context, msg *types.MsgNotarizeAs
return nil, errors.New("machine not found") return nil, errors.New("machine not found")
} }
valid := ValidateSignature(msg.Hash, msg.Signature, msg.PubKey) valid := util.ValidateSignature(msg.Hash, msg.Signature, msg.PubKey)
if !valid { if !valid {
return nil, errors.New("invalid signature") return nil, errors.New("invalid signature")
} }
@ -35,18 +34,3 @@ func (k msgServer) NotarizeAsset(goCtx context.Context, msg *types.MsgNotarizeAs
return &types.MsgNotarizeAssetResponse{}, nil return &types.MsgNotarizeAssetResponse{}, nil
} }
func ValidateSignature(message string, signature string, publicKey string) bool {
// Convert the message, signature, and public key from hex to bytes
messageBytes := []byte(message)
signatureBytes, _ := hex.DecodeString(signature)
publicKeyBytes, _ := hex.DecodeString(publicKey)
// Create a secp256k1 public key object
pubKey := &secp256k1.PubKey{Key: publicKeyBytes}
// Verify the signature
isValid := pubKey.VerifySignature(messageBytes, signatureBytes)
return isValid
}

View File

@ -6,6 +6,7 @@ import (
"strconv" "strconv"
config "planetmint-go/config" config "planetmint-go/config"
"planetmint-go/util"
"planetmint-go/x/machine/types" "planetmint-go/x/machine/types"
"github.com/btcsuite/btcd/btcutil/hdkeychain" "github.com/btcsuite/btcd/btcutil/hdkeychain"
@ -32,6 +33,11 @@ func (k msgServer) AttestMachine(goCtx context.Context, msg *types.MsgAttestMach
return nil, errors.New("trust anchor has already been used for attestation") return nil, errors.New("trust anchor has already been used for attestation")
} }
isValidMachineId := util.ValidateSignature(msg.Machine.MachineId, msg.Machine.MachineIdSignature, msg.Machine.MachineId)
if !isValidMachineId {
return nil, errors.New("invalid machine id")
}
isValidIssuerPlanetmint := validateExtendedPublicKey(msg.Machine.IssuerPlanetmint, config.PlmntNetParams) isValidIssuerPlanetmint := validateExtendedPublicKey(msg.Machine.IssuerPlanetmint, config.PlmntNetParams)
if !isValidIssuerPlanetmint { if !isValidIssuerPlanetmint {
return nil, errors.New("invalid planetmint key") return nil, errors.New("invalid planetmint key")

View File

@ -27,10 +27,10 @@ func TestMsgServer(t *testing.T) {
} }
func TestMsgServerAttestMachine(t *testing.T) { func TestMsgServerAttestMachine(t *testing.T) {
_, pk := sample.KeyPair() sk, pk := sample.KeyPair()
ta := sample.TrustAnchor() ta := sample.TrustAnchor(pk)
taMsg := types.NewMsgRegisterTrustAnchor(pk, &ta) taMsg := types.NewMsgRegisterTrustAnchor(pk, &ta)
machine := sample.Machine(pk, ta.Pubkey) machine := sample.Machine(pk, pk, sk)
msg := types.NewMsgAttestMachine(pk, &machine) msg := types.NewMsgAttestMachine(pk, &machine)
msgServer, ctx := setupMsgServer(t) msgServer, ctx := setupMsgServer(t)
_, err := msgServer.RegisterTrustAnchor(ctx, taMsg) _, err := msgServer.RegisterTrustAnchor(ctx, taMsg)
@ -42,10 +42,10 @@ func TestMsgServerAttestMachine(t *testing.T) {
} }
func TestMsgServerAttestMachineInvalidLiquidKey(t *testing.T) { func TestMsgServerAttestMachineInvalidLiquidKey(t *testing.T) {
_, pk := sample.KeyPair() sk, pk := sample.KeyPair()
ta := sample.TrustAnchor() ta := sample.TrustAnchor(pk)
taMsg := types.NewMsgRegisterTrustAnchor(pk, &ta) taMsg := types.NewMsgRegisterTrustAnchor(pk, &ta)
machine := sample.Machine(pk, ta.Pubkey) machine := sample.Machine(pk, pk, sk)
machine.IssuerLiquid = "invalidkey" machine.IssuerLiquid = "invalidkey"
msg := types.NewMsgAttestMachine(pk, &machine) msg := types.NewMsgAttestMachine(pk, &machine)
msgServer, ctx := setupMsgServer(t) msgServer, ctx := setupMsgServer(t)
@ -57,7 +57,7 @@ func TestMsgServerAttestMachineInvalidLiquidKey(t *testing.T) {
func TestMsgServerRegisterTrustAnchor(t *testing.T) { func TestMsgServerRegisterTrustAnchor(t *testing.T) {
_, pk := sample.KeyPair() _, pk := sample.KeyPair()
ta := sample.TrustAnchor() ta := sample.TrustAnchor(pk)
msg := types.NewMsgRegisterTrustAnchor(pk, &ta) msg := types.NewMsgRegisterTrustAnchor(pk, &ta)
msgServer, ctx := setupMsgServer(t) msgServer, ctx := setupMsgServer(t)
res, err := msgServer.RegisterTrustAnchor(ctx, msg) res, err := msgServer.RegisterTrustAnchor(ctx, msg)
@ -68,7 +68,7 @@ func TestMsgServerRegisterTrustAnchor(t *testing.T) {
func TestMsgServerRegisterTrustAnchorTwice(t *testing.T) { func TestMsgServerRegisterTrustAnchorTwice(t *testing.T) {
_, pk := sample.KeyPair() _, pk := sample.KeyPair()
ta := sample.TrustAnchor() ta := sample.TrustAnchor(pk)
msg := types.NewMsgRegisterTrustAnchor(pk, &ta) msg := types.NewMsgRegisterTrustAnchor(pk, &ta)
msgServer, ctx := setupMsgServer(t) msgServer, ctx := setupMsgServer(t)
res, err := msgServer.RegisterTrustAnchor(ctx, msg) res, err := msgServer.RegisterTrustAnchor(ctx, msg)