Skip to content

Commit

Permalink
Merge pull request #3 from xpladev/my/fix-x
Browse files Browse the repository at this point in the history
fix: logic of generating multisig transaction
  • Loading branch information
Moonyongjung committed Sep 6, 2023
2 parents 687ea0f + 10d30bf commit f06ae38
Show file tree
Hide file tree
Showing 6 changed files with 304 additions and 57 deletions.
146 changes: 146 additions & 0 deletions client/msg_tx_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package client_test

import (
"path/filepath"

"github.com/evmos/ethermint/crypto/hd"
"github.com/xpladev/xpla.go/client"
"github.com/xpladev/xpla.go/key"
"github.com/xpladev/xpla.go/types"
"github.com/xpladev/xpla.go/util/testutil"

Expand All @@ -18,6 +23,9 @@ import (
mupgrade "github.com/xpladev/xpla.go/core/upgrade"
mwasm "github.com/xpladev/xpla.go/core/wasm"

"github.com/cosmos/cosmos-sdk/crypto/keyring"
"github.com/cosmos/cosmos-sdk/crypto/keys/multisig"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)

Expand Down Expand Up @@ -808,3 +816,141 @@ func (s *ClientTestSuite) TestWasmTx() {
s.Require().NoError(err)
s.Require().Equal(testutil.WasmMigrateTxTemplates, string(wasmMigrateJsonTxbytes))
}

func (s *ClientTestSuite) TestMultiSignature() {
s.xplac.WithURL(s.apis[0])
rootDir := s.network.Validators[0].Dir
key1 := s.accounts[0]
key2 := s.accounts[1]

key1Name := "key1"
key2Name := "key2"
multiKeyName := "multiKey"

// gen multisig account
kb, err := keyring.New(types.XplaToolDefaultName, keyring.BackendTest, rootDir, nil, hd.EthSecp256k1Option())
s.Require().NoError(err)

err = kb.ImportPrivKey(
key1Name,
key.EncryptArmorPrivKey(key1.PrivKey, key.DefaultEncryptPassphrase),
key.DefaultEncryptPassphrase,
)
s.Require().NoError(err)

err = kb.ImportPrivKey(
key2Name,
key.EncryptArmorPrivKey(key2.PrivKey, key.DefaultEncryptPassphrase),
key.DefaultEncryptPassphrase,
)
s.Require().NoError(err)

var pks []cryptotypes.PubKey
multisigThreshold := 2

k1, err := kb.Key(key1Name)
s.Require().NoError(err)
pks = append(pks, k1.GetPubKey())

k2, err := kb.Key(key2Name)
s.Require().NoError(err)
pks = append(pks, k2.GetPubKey())

multiKeyInfo, err := kb.SaveMultisig(multiKeyName, multisig.NewLegacyAminoPubKey(multisigThreshold, pks))
s.Require().NoError(err)

// send coin to multisig account
s.xplac.WithPrivateKey(key2.PrivKey)

bankSendMsg := types.BankSendMsg{
FromAddress: key2.Address.String(),
ToAddress: multiKeyInfo.GetAddress().String(),
Amount: "10000000000axpla",
}
bankSendTxbytes, err := s.xplac.BankSend(bankSendMsg).CreateAndSignTx()
s.Require().NoError(err)

_, err = s.xplac.Broadcast(bankSendTxbytes)
s.Require().NoError(err)
s.Require().NoError(s.network.WaitForNextBlock())

// create unsigned tx
unsignedTxPath := filepath.Join(rootDir, "unsignedTx.json")
s.xplac.WithOutputDocument(unsignedTxPath)

bankSendMsg2 := types.BankSendMsg{
FromAddress: multiKeyInfo.GetAddress().String(),
ToAddress: key1.Address.String(),
Amount: "10",
}

_, err = s.xplac.BankSend(bankSendMsg2).CreateUnsignedTx()
s.Require().NoError(err)

// create signature of key1
s.xplac.WithPrivateKey(key1.PrivKey)
signature1Path := filepath.Join(rootDir, "signature1.json")
s.xplac.WithOutputDocument(signature1Path)

signTxMsg1 := types.SignTxMsg{
UnsignedFileName: unsignedTxPath,
SignatureOnly: true,
MultisigAddress: multiKeyInfo.GetAddress().String(),
Overwrite: false,
Amino: false,
Offline: false,
}
_, err = s.xplac.SignTx(signTxMsg1)
s.Require().NoError(err)

// create signature of key2
s.xplac.WithPrivateKey(key2.PrivKey)
signature2Path := filepath.Join(rootDir, "signature2.json")
s.xplac.WithOutputDocument(signature2Path)

signTxMsg2 := types.SignTxMsg{
UnsignedFileName: unsignedTxPath,
SignatureOnly: true,
MultisigAddress: multiKeyInfo.GetAddress().String(),
Overwrite: false,
Amino: false,
Offline: false,
}
_, err = s.xplac.SignTx(signTxMsg2)
s.Require().NoError(err)

// create multisigned transaction
s.xplac.WithOutputDocument("")
txMultiSignMsg := types.TxMultiSignMsg{
FileName: unsignedTxPath,
GenerateOnly: true,
FromName: multiKeyInfo.GetName(),
Offline: false,
SignatureFiles: []string{
signature1Path,
signature2Path,
},
KeyringBackend: "test",
KeyringPath: rootDir,
}
multiSignTx, err := s.xplac.MultiSign(txMultiSignMsg)
s.Require().NoError(err)

// broadcast test
sdkTx, err := s.xplac.GetEncoding().TxConfig.TxJSONDecoder()(multiSignTx)
s.Require().NoError(err)
txBytes, err := s.xplac.GetEncoding().TxConfig.TxEncoder()(sdkTx)
s.Require().NoError(err)

_, err = s.xplac.Broadcast(txBytes)

// generate error insufficient funds
// multisig tx is normal
s.Require().Error(err)
s.Require().Equal(
`code 8 : tx failed - [with code 5 : 10000000000axpla is smaller than 133715200000000000axpla: insufficient funds: insufficient funds]`,
err.Error(),
)

s.xplac = client.ResetXplac(s.xplac)
}
Loading

0 comments on commit f06ae38

Please sign in to comment.