Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate a MsgWirePayForMessage via CLI #32

Merged
merged 11 commits into from
Feb 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
192 changes: 66 additions & 126 deletions app/abci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ import (
"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
cliTx "github.com/cosmos/cosmos-sdk/client/tx"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
"github.com/cosmos/cosmos-sdk/crypto/hd"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
"github.com/cosmos/cosmos-sdk/server"
"github.com/cosmos/cosmos-sdk/simapp"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand All @@ -30,19 +31,26 @@ import (
dbm "github.com/tendermint/tm-db"
)

const testingKeyAcc = "test"

// Get flags every time the simulator is run
func init() {
simapp.GetSimulatorFlags()
}

func TestProcessMsg(t *testing.T) {
testApp, key := setupApp(t)

kb := keyring.NewInMemory()
info, _, err := kb.NewMnemonic(testingKeyAcc, keyring.English, "", hd.Secp256k1)
if err != nil {
t.Error(err)
}
ns := []byte{1, 1, 1, 1, 1, 1, 1, 1}
message := bytes.Repeat([]byte{1}, 256)

// create a tx
msg := generateWirePayForMessage(t, testApp.SquareSize(), key, ns, message)
// create a signed MsgWirePayFroMessage
msg := generateSignedWirePayForMessage(t, types.SquareSize, ns, message, kb)

testApp := setupApp(t, info.GetPubKey())

tests := []struct {
name string
Expand All @@ -65,7 +73,13 @@ func TestProcessMsg(t *testing.T) {
}

func TestPreprocessTxs(t *testing.T) {
testApp, key := setupApp(t)
kb := keyring.NewInMemory()
info, _, err := kb.NewMnemonic(testingKeyAcc, keyring.English, "", hd.Secp256k1)
if err != nil {
t.Error(err)
}

testApp := setupApp(t, info.GetPubKey())

type test struct {
input abci.RequestPreprocessTxs
Expand All @@ -75,15 +89,15 @@ func TestPreprocessTxs(t *testing.T) {

firstNS := []byte{2, 2, 2, 2, 2, 2, 2, 2}
firstMessage := bytes.Repeat([]byte{2}, 512)
firstRawTx := generateRawTx(t, key, testApp.txConfig, firstNS, firstMessage)
firstRawTx := generateRawTx(t, testApp.txConfig, firstNS, firstMessage, kb)

secondNS := []byte{1, 1, 1, 1, 1, 1, 1, 1}
secondMessage := []byte{2}
secondRawTx := generateRawTx(t, key, testApp.txConfig, secondNS, secondMessage)
secondRawTx := generateRawTx(t, testApp.txConfig, secondNS, secondMessage, kb)

thirdNS := []byte{3, 3, 3, 3, 3, 3, 3, 3}
thirdMessage := []byte{}
thirdRawTx := generateRawTx(t, key, testApp.txConfig, thirdNS, thirdMessage)
thirdRawTx := generateRawTx(t, testApp.txConfig, thirdNS, thirdMessage, kb)

tests := []test{
{
Expand Down Expand Up @@ -115,93 +129,11 @@ func TestPreprocessTxs(t *testing.T) {
}
}

// this is more of a sanity check
func TestTxSignature(t *testing.T) {
key := secp256k1.GenPrivKey()

encConf := MakeEncodingConfig()
txConf := encConf.TxConfig

ns := []byte{1, 1, 1, 1, 1, 1, 1, 1}
message := bytes.Repeat([]byte{1}, 256)

// create a msg
msg := generateWirePayForMessage(t, 64, key, ns, message)

// this is returning a tx.wrapper
builder := txConf.NewTxBuilder()
err := builder.SetMsgs(msg)
if err != nil {
t.Error(err)
}

signingData := authsigning.SignerData{
ChainID: "test-chain",
AccountNumber: 0,
Sequence: 0,
}

sigData := signing.SingleSignatureData{
SignMode: signing.SignMode_SIGN_MODE_DIRECT,
Signature: nil,
}

sig := signing.SignatureV2{
PubKey: key.PubKey(),
Data: &sigData,
Sequence: 0,
}

// set the unsigned signature data (nil) first
// this is required for SignWithPriveKey to sign properly
err = builder.SetSignatures(sig)
if err != nil {
if err != nil {
t.Error(err)
}
}

sigV2, err := cliTx.SignWithPrivKey(signing.SignMode_SIGN_MODE_DIRECT, signingData, builder, key, txConf, 0)
if err != nil {
t.Error(err)
}

err = builder.SetSignatures(sigV2)
if err != nil {
if err != nil {
t.Error(err)
}
}

tx := builder.GetTx()

err = authsigning.VerifySignature(key.PubKey(), signingData, sigV2.Data, txConf.SignModeHandler(), tx)
if err != nil {
t.Error("failure to verify Signature")
}

rawTx, err := txConf.TxEncoder()(tx)
if err != nil {
t.Error(err)
}

stx, err := txConf.TxDecoder()(rawTx)
if err != nil {
t.Error(err)
}

// verify the signature after decoding
err = authsigning.VerifySignature(key.PubKey(), signingData, sigV2.Data, txConf.SignModeHandler(), stx)
if err != nil {
t.Error(err)
}
}

/////////////////////////////
// Setup App
/////////////////////////////

func setupApp(t *testing.T) (*App, *secp256k1.PrivKey) {
func setupApp(t *testing.T, pub cryptotypes.PubKey) *App {
// var cache sdk.MultiStorePersistentCache
// EmptyAppOptions is a stub implementing AppOptions
emptyOpts := emptyAppOptions{}
Expand All @@ -220,16 +152,14 @@ func setupApp(t *testing.T) (*App, *secp256k1.PrivKey) {
anteOpt,
)

key := secp256k1.GenPrivKey()

for acc := range maccPerms {
require.Equal(t, !allowedReceivingModAcc[acc], testApp.BankKeeper.BlockedAddr(testApp.AccountKeeper.GetModuleAddress(acc)),
"ensure that blocked addresses are properly set in bank keeper")
}

genesisState := NewDefaultGenesisState()

genesisState, err := addGenesisAccount(sdk.AccAddress(key.PubKey().Address().Bytes()), genesisState, testApp.appCodec)
genesisState, err := addGenesisAccount(sdk.AccAddress(pub.Address().Bytes()), genesisState, testApp.appCodec)
if err != nil {
t.Error(err)
}
Expand All @@ -245,7 +175,7 @@ func setupApp(t *testing.T) (*App, *secp256k1.PrivKey) {
},
)

return testApp, key
return testApp
}

type emptyAppOptions struct{}
Expand Down Expand Up @@ -322,13 +252,18 @@ func addGenesisAccount(addr sdk.AccAddress, appState map[string]json.RawMessage,
// Generate Txs
/////////////////////////////

func generateRawTx(t *testing.T, key *secp256k1.PrivKey, txConfig client.TxConfig, ns, message []byte) (rawTx []byte) {
func generateRawTx(t *testing.T, txConfig client.TxConfig, ns, message []byte, ring keyring.Keyring) (rawTx []byte) {
// create a msg
msg := generateWirePayForMessage(t, types.SquareSize, key, ns, message)
msg := generateSignedWirePayForMessage(t, types.SquareSize, ns, message, ring)

info, err := ring.Key(testingKeyAcc)
if err != nil {
t.Error(err)
}

// this is returning a tx.wrapper
builder := txConfig.NewTxBuilder()
err := builder.SetMsgs(msg)
err = builder.SetMsgs(msg)
if err != nil {
t.Error(err)
}
Expand All @@ -355,7 +290,7 @@ func generateRawTx(t *testing.T, key *secp256k1.PrivKey, txConfig client.TxConfi
}

sig := signing.SignatureV2{
PubKey: key.PubKey(),
PubKey: info.GetPubKey(),
Data: &sigData,
Sequence: 0,
}
Expand All @@ -368,12 +303,36 @@ func generateRawTx(t *testing.T, key *secp256k1.PrivKey, txConfig client.TxConfi
}
}

// create the actual signature
sigV2, err := cliTx.SignWithPrivKey(signing.SignMode_SIGN_MODE_DIRECT, signingData, builder, key, txConfig, 0)
// Generate the bytes to be signed.
bytesToSign, err := txConfig.
SignModeHandler().
GetSignBytes(
signing.SignMode_SIGN_MODE_DIRECT,
signingData,
builder.GetTx(),
)
if err != nil {
t.Error(err)
}

// Sign those bytes
sigBytes, _, err := ring.Sign(testingKeyAcc, bytesToSign)
if err != nil {
t.Error(err)
}

// Construct the SignatureV2 struct
sigData = signing.SingleSignatureData{
SignMode: signing.SignMode_SIGN_MODE_DIRECT,
Signature: sigBytes,
}

sigV2 := signing.SignatureV2{
PubKey: info.GetPubKey(),
Data: &sigData,
Sequence: 0,
}

// set the actual signature
err = builder.SetSignatures(sigV2)
if err != nil {
Expand All @@ -394,40 +353,21 @@ func generateRawTx(t *testing.T, key *secp256k1.PrivKey, txConfig client.TxConfi
return rawTx
}

func generateWirePayForMessage(t *testing.T, k uint64, key *secp256k1.PrivKey, ns, message []byte) *types.MsgWirePayForMessage {
pubKey := key.PubKey()

commit, err := types.CreateCommitment(k, ns, message)
func generateSignedWirePayForMessage(t *testing.T, k uint64, ns, message []byte, ring keyring.Keyring) *types.MsgWirePayForMessage {
info, err := ring.Key(testingKeyAcc)
if err != nil {
t.Error(err)
}

msg := &types.MsgWirePayForMessage{
Fee: &types.TransactionFee{},
Nonce: 0,
MessageNameSpaceId: ns,
MessageSize: 512,
Message: message,
PublicKey: pubKey.Bytes(),
MessageShareCommitment: []types.ShareCommitAndSignature{
{
K: k,
ShareCommitment: commit,
},
},
}

rawTxPFM, err := msg.GetCommitmentSignBytes(k)
msg, err := types.NewMsgWirePayForMessage(ns, message, info.GetPubKey().Bytes(), &types.TransactionFee{}, k)
if err != nil {
t.Error(err)
}

signedTxPFM, err := key.Sign(rawTxPFM)
err = msg.SignShareCommitments(testingKeyAcc, ring)
if err != nil {
t.Error(err)
}

msg.MessageShareCommitment[0].Signature = signedTxPFM

return msg
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ require (
github.com/tendermint/tm-db v0.6.3
golang.org/x/crypto v0.0.0-20210220033148-5ea612d1eb83 // indirect
golang.org/x/net v0.0.0-20201021035429-f5854403a974 // indirect
golang.org/x/sys v0.0.0-20210220050731-9a76102bfb43 // indirect
golang.org/x/sys v0.0.0-20210223095934-7937bea0104d // indirect
golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d // indirect
google.golang.org/genproto v0.0.0-20210207032614-bba0dbe2a9ea
google.golang.org/grpc v1.33.2
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -810,8 +810,8 @@ golang.org/x/sys v0.0.0-20200814200057-3d37ad5750ed/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201015000850-e3ed0017c211/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210220050731-9a76102bfb43 h1:SgQ6LNaYJU0JIuEHv9+s6EbhSCwYeAf5Yvj6lpYlqAE=
golang.org/x/sys v0.0.0-20210220050731-9a76102bfb43/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210223095934-7937bea0104d h1:u0GOGnBJ3EKE/tNqREhhGiCzE9jFXydDo2lf7hOwGuc=
golang.org/x/sys v0.0.0-20210223095934-7937bea0104d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221 h1:/ZHdbVpdR/jk3g30/d4yUL0JU9kksj8+F/bnQUVLGDM=
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d h1:SZxvLBoTP5yHO3Frd4z4vrF+DBX9vMVanchswa69toE=
Expand Down
Loading