Skip to content

Commit 8f5bf72

Browse files
authored
Merge pull request #125 from renproject/fix/cosmos_hash
fixed cosmos hashing
2 parents 24a4914 + ab288a4 commit 8f5bf72

File tree

4 files changed

+62
-42
lines changed

4 files changed

+62
-42
lines changed

chain/cosmos/tx.go

+16-8
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,20 @@ package cosmos
33
import (
44
"context"
55
"crypto/sha256"
6+
"encoding/hex"
67
"fmt"
78
"math/big"
89

910
"github.com/btcsuite/btcd/btcec"
10-
1111
"github.com/cosmos/cosmos-sdk/client"
1212
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
1313
"github.com/cosmos/cosmos-sdk/types"
1414
txTypes "github.com/cosmos/cosmos-sdk/types/tx"
1515
"github.com/cosmos/cosmos-sdk/types/tx/signing"
1616
authsigning "github.com/cosmos/cosmos-sdk/x/auth/signing"
17+
"github.com/cosmos/cosmos-sdk/x/auth/tx"
1718
bankType "github.com/cosmos/cosmos-sdk/x/bank/types"
19+
"github.com/renproject/multichain"
1820
"github.com/renproject/multichain/api/account"
1921
"github.com/renproject/multichain/api/address"
2022
"github.com/renproject/multichain/api/contract"
@@ -48,7 +50,6 @@ func (opts TxBuilderOptions) WithChainID(chainID pack.String) TxBuilderOptions {
4850
}
4951

5052
type txBuilder struct {
51-
pubKey []byte
5253
client *Client
5354
chainID pack.String
5455
signMode int32
@@ -57,10 +58,9 @@ type txBuilder struct {
5758
// NewTxBuilder returns an implementation of the transaction builder interface
5859
// from the Cosmos Compat API, and exposes the functionality to build simple
5960
// Cosmos based transactions.
60-
func NewTxBuilder(options TxBuilderOptions, client *Client, key []byte) account.TxBuilder {
61+
func NewTxBuilder(options TxBuilderOptions, client *Client) account.TxBuilder {
6162
return txBuilder{
6263
signMode: DefaultSignMode,
63-
pubKey: key,
6464
client: client,
6565
chainID: options.ChainID,
6666
}
@@ -76,6 +76,15 @@ func (builder txBuilder) WithSignMode(signMode int32) txBuilder {
7676
// This transaction is unsigned, and must be signed before submitting to the
7777
// cosmos chain.
7878
func (builder txBuilder) BuildTx(ctx context.Context, from, to address.Address, value, nonce, gasLimit, gasPrice, gasCap pack.U256, payload pack.Bytes) (account.Tx, error) {
79+
// We assume the "from" address is a public key as it is required for
80+
// setting the signature.
81+
pubKeyBytes, err := hex.DecodeString(string(from))
82+
if err != nil {
83+
return nil, err
84+
}
85+
pubKey := secp256k1.PubKey{Key: pubKeyBytes}
86+
from = multichain.Address(types.AccAddress(pubKey.Address()).String())
87+
7988
fromAddr, err := types.AccAddressFromBech32(string(from))
8089
if err != nil {
8190
return nil, err
@@ -86,7 +95,6 @@ func (builder txBuilder) BuildTx(ctx context.Context, from, to address.Address,
8695
return nil, err
8796
}
8897

89-
var pubKey = secp256k1.PubKey{Key: builder.pubKey}
9098
sendMsg := MsgSend{
9199
FromAddress: Address(fromAddr),
92100
ToAddress: Address(toAddr),
@@ -211,7 +219,7 @@ func (t Tx) From() address.Address {
211219
}
212220

213221
if t.sendMsg != nil {
214-
return address.Address(t.sendMsg.FromAddress)
222+
return address.Address(t.sendMsg.FromAddress.String())
215223
}
216224
return address.Address("")
217225
}
@@ -224,7 +232,7 @@ func (t Tx) To() address.Address {
224232
}
225233

226234
if t.sendMsg != nil {
227-
return address.Address(t.sendMsg.ToAddress)
235+
return address.Address(t.sendMsg.ToAddress.String())
228236
}
229237
return address.Address("")
230238
}
@@ -307,7 +315,7 @@ func (t Tx) Serialize() (pack.Bytes, error) {
307315
var txBytes []byte
308316
var err error = nil
309317
if t.originalTx != nil {
310-
txBytes, err = t.encoder(t.originalTx)
318+
txBytes, err = t.encoder(tx.WrapTx(t.originalTx).GetTx())
311319
} else if t.sendMsg != nil {
312320
txBytes, err = t.encoder(t.txBuilder.GetTx())
313321
}

chain/terra/terra.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,6 @@ func NewClient(opts ClientOptions) *Client {
5151
// NewTxBuilder returns an implementation of the transaction builder interface
5252
// from the Cosmos Compat API, and exposes the functionality to build simple
5353
// Terra transactions.
54-
func NewTxBuilder(opts TxBuilderOptions, client *Client, key []byte) account.TxBuilder {
55-
return cosmos.NewTxBuilder(opts, client, key)
54+
func NewTxBuilder(opts TxBuilderOptions, client *Client) account.TxBuilder {
55+
return cosmos.NewTxBuilder(opts, client)
5656
}

chain/terra/terra_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ var _ = Describe("Terra", func() {
6767
txBuilder := terra.NewTxBuilder(
6868
terra.DefaultTxBuilderOptions().
6969
WithChainID("testnet"),
70-
client, pubKey[:],
70+
client,
7171
)
7272

7373
// build the transaction

multichain_test.go

+43-31
Original file line numberDiff line numberDiff line change
@@ -18,37 +18,35 @@ import (
1818
"testing/quick"
1919
"time"
2020

21-
"github.com/btcsuite/btcutil/hdkeychain"
22-
"github.com/ethereum/go-ethereum/accounts/keystore"
23-
"github.com/ethereum/go-ethereum/crypto"
24-
"github.com/renproject/multichain/chain/avalanche"
25-
"github.com/renproject/multichain/chain/bsc"
26-
"github.com/renproject/multichain/chain/ethereum"
27-
"github.com/renproject/multichain/chain/fantom"
28-
"github.com/renproject/multichain/chain/polygon"
29-
"github.com/tyler-smith/go-bip39"
30-
3121
"github.com/btcsuite/btcd/btcec"
3222
"github.com/btcsuite/btcd/chaincfg"
3323
"github.com/btcsuite/btcd/txscript"
3424
"github.com/btcsuite/btcutil"
3525
"github.com/btcsuite/btcutil/base58"
26+
"github.com/btcsuite/btcutil/hdkeychain"
27+
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
3628
cosmossdk "github.com/cosmos/cosmos-sdk/types"
29+
"github.com/ethereum/go-ethereum/accounts/keystore"
30+
"github.com/ethereum/go-ethereum/crypto"
3731
filaddress "github.com/filecoin-project/go-address"
3832
filtypes "github.com/filecoin-project/lotus/chain/types"
3933
"github.com/renproject/id"
4034
"github.com/renproject/multichain"
35+
"github.com/renproject/multichain/api/account"
36+
"github.com/renproject/multichain/chain/avalanche"
4137
"github.com/renproject/multichain/chain/bitcoin"
4238
"github.com/renproject/multichain/chain/bitcoincash"
43-
44-
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
45-
// "github.com/renproject/multichain/chain/digibyte"
39+
"github.com/renproject/multichain/chain/bsc"
4640
"github.com/renproject/multichain/chain/dogecoin"
41+
"github.com/renproject/multichain/chain/ethereum"
42+
"github.com/renproject/multichain/chain/fantom"
4743
"github.com/renproject/multichain/chain/filecoin"
44+
"github.com/renproject/multichain/chain/polygon"
4845
"github.com/renproject/multichain/chain/terra"
4946
"github.com/renproject/multichain/chain/zcash"
5047
"github.com/renproject/pack"
5148
"github.com/renproject/surge"
49+
"github.com/tyler-smith/go-bip39"
5250
"go.uber.org/zap"
5351
"go.uber.org/zap/zapcore"
5452

@@ -541,7 +539,7 @@ var _ = Describe("Multichain", func() {
541539
privKeyToAddr func(pk id.PrivKey) multichain.Address
542540
rpcURL pack.String
543541
randomRecipientAddr func() multichain.Address
544-
initialise func(pack.String, []byte) (multichain.AccountClient, multichain.AccountTxBuilder)
542+
initialise func(pack.String) (multichain.AccountClient, multichain.AccountTxBuilder)
545543
txParams func(multichain.AccountClient) (pack.U256, pack.U256, pack.U256, pack.U256, pack.Bytes)
546544
chain multichain.Chain
547545
}{
@@ -582,7 +580,7 @@ var _ = Describe("Multichain", func() {
582580
recipientKey := id.NewPrivKey()
583581
return multichain.Address(crypto.PubkeyToAddress(recipientKey.PublicKey).Hex())
584582
},
585-
func(rpcURL pack.String, _ []byte) (multichain.AccountClient, multichain.AccountTxBuilder) {
583+
func(rpcURL pack.String) (multichain.AccountClient, multichain.AccountTxBuilder) {
586584
client, err := ethereum.NewClient(string(rpcURL))
587585
Expect(err).NotTo(HaveOccurred())
588586
txBuilder := ethereum.NewTxBuilder(big.NewInt(1337))
@@ -620,7 +618,7 @@ var _ = Describe("Multichain", func() {
620618
recipientKey := id.NewPrivKey()
621619
return multichain.Address(crypto.PubkeyToAddress(recipientKey.PublicKey).Hex())
622620
},
623-
func(rpcURL pack.String, _ []byte) (multichain.AccountClient, multichain.AccountTxBuilder) {
621+
func(rpcURL pack.String) (multichain.AccountClient, multichain.AccountTxBuilder) {
624622
client, err := polygon.NewClient(string(rpcURL))
625623
Expect(err).NotTo(HaveOccurred())
626624
txBuilder := polygon.NewTxBuilder(big.NewInt(15001))
@@ -674,7 +672,7 @@ var _ = Describe("Multichain", func() {
674672
recipientKey := id.NewPrivKey()
675673
return multichain.Address(crypto.PubkeyToAddress(recipientKey.PublicKey).Hex())
676674
},
677-
func(rpcURL pack.String, _ []byte) (multichain.AccountClient, multichain.AccountTxBuilder) {
675+
func(rpcURL pack.String) (multichain.AccountClient, multichain.AccountTxBuilder) {
678676
client, err := bsc.NewClient(string(rpcURL))
679677
Expect(err).NotTo(HaveOccurred())
680678
txBuilder := bsc.NewTxBuilder(big.NewInt(420))
@@ -712,7 +710,7 @@ var _ = Describe("Multichain", func() {
712710
recipientKey := id.NewPrivKey()
713711
return multichain.Address(crypto.PubkeyToAddress(recipientKey.PublicKey).Hex())
714712
},
715-
func(rpcURL pack.String, _ []byte) (multichain.AccountClient, multichain.AccountTxBuilder) {
713+
func(rpcURL pack.String) (multichain.AccountClient, multichain.AccountTxBuilder) {
716714
client, err := avalanche.NewClient(string(rpcURL))
717715
Expect(err).NotTo(HaveOccurred())
718716
txBuilder := avalanche.NewTxBuilder(big.NewInt(43112))
@@ -748,7 +746,7 @@ var _ = Describe("Multichain", func() {
748746
recipientKey := id.NewPrivKey()
749747
return multichain.Address(crypto.PubkeyToAddress(recipientKey.PublicKey).Hex())
750748
},
751-
func(rpcURL pack.String, _ []byte) (multichain.AccountClient, multichain.AccountTxBuilder) {
749+
func(rpcURL pack.String) (multichain.AccountClient, multichain.AccountTxBuilder) {
752750
client, err := fantom.NewClient(string(rpcURL))
753751
Expect(err).NotTo(HaveOccurred())
754752
txBuilder := fantom.NewTxBuilder(big.NewInt(4003))
@@ -797,7 +795,7 @@ var _ = Describe("Multichain", func() {
797795
recipient := multichain.Address(cosmossdk.AccAddress(recipientKey.PubKey().Address()).String())
798796
return recipient
799797
},
800-
initialise: func(rpcURL pack.String, key []byte) (multichain.AccountClient, multichain.AccountTxBuilder) {
798+
initialise: func(rpcURL pack.String) (multichain.AccountClient, multichain.AccountTxBuilder) {
801799
client := terra.NewClient(
802800
terra.DefaultClientOptions().
803801
WithHost(rpcURL).
@@ -806,7 +804,7 @@ var _ = Describe("Multichain", func() {
806804
txBuilder := terra.NewTxBuilder(
807805
terra.DefaultTxBuilderOptions().
808806
WithChainID("testnet"),
809-
client, key,
807+
client,
810808
)
811809

812810
return client, txBuilder
@@ -867,7 +865,7 @@ var _ = Describe("Multichain", func() {
867865
Expect(err).NotTo(HaveOccurred())
868866
return multichain.Address(pack.String(addr.String()))
869867
},
870-
func(rpcURL pack.String, _ []byte) (multichain.AccountClient, multichain.AccountTxBuilder) {
868+
func(rpcURL pack.String) (multichain.AccountClient, multichain.AccountTxBuilder) {
871869
// dirty hack to fetch auth token
872870
client, err := filecoin.NewClient(
873871
filecoin.DefaultClientOptions().
@@ -915,8 +913,8 @@ var _ = Describe("Multichain", func() {
915913

916914
// Initialise the account chain's client, and possibly get a nonce for
917915
// the sender.
918-
accountClient, txBuilder := accountChain.initialise(accountChain.rpcURL, pack.NewBytes(senderPubKeyBytes))
919-
sendTx := func() (pack.Bytes, pack.U256) {
916+
accountClient, txBuilder := accountChain.initialise(accountChain.rpcURL)
917+
sendTx := func() (pack.Bytes, account.Tx) {
920918
// Get the appropriate nonce for sender.
921919
nonce, err := accountClient.AccountNonce(ctx, senderAddr)
922920
Expect(err).NotTo(HaveOccurred())
@@ -931,6 +929,17 @@ var _ = Describe("Multichain", func() {
931929
amount, nonce, gasLimit, gasPrice, gasCap,
932930
payload,
933931
)
932+
// TODO: Temporarily use the public key in place of the
933+
// from address for Terra.
934+
if accountChain.chain == multichain.Terra {
935+
accountTx, err = txBuilder.BuildTx(
936+
ctx,
937+
multichain.Address(hex.EncodeToString(senderPubKeyBytes)),
938+
recipientAddr,
939+
amount, nonce, gasLimit, gasPrice, gasCap,
940+
payload,
941+
)
942+
}
934943
Expect(err).NotTo(HaveOccurred())
935944

936945
// Get the transaction bytes and sign them.
@@ -954,9 +963,9 @@ var _ = Describe("Multichain", func() {
954963
err = accountClient.SubmitTx(ctx, accountTx)
955964
Expect(err).NotTo(HaveOccurred())
956965
logger.Debug("submit tx", zap.String("from", string(senderAddr)), zap.String("to", string(recipientAddr)), zap.Any("txHash", txHash))
957-
return txHash, amount
966+
return txHash, accountTx
958967
}
959-
txHash, amount := sendTx()
968+
txHash, accountTx := sendTx()
960969
if accountChain.chain == multichain.Avalanche {
961970
time.Sleep(5 * time.Second)
962971
sendTx()
@@ -969,10 +978,13 @@ var _ = Describe("Multichain", func() {
969978
tx, confs, err := accountClient.Tx(ctx, txHash)
970979
if err == nil && confs > 0 {
971980
Expect(confs.Uint64()).To(BeNumerically(">", 0))
972-
Expect(tx.Value()).To(Equal(amount))
973-
Expect(tx.From()).To(Equal(senderAddr))
974-
Expect(tx.To()).To(Equal(recipientAddr))
975-
Expect(tx.Value()).To(Equal(amount))
981+
Expect(tx.Value()).To(Equal(accountTx.Value()))
982+
Expect(tx.From()).To(Equal(accountTx.From()))
983+
Expect(tx.To()).To(Equal(accountTx.To()))
984+
// FIXME: Filecoin signed message hash is different, so we ignore this check for filecoin. Appropriate check should be added for Filecoin.
985+
if accountChain.chain != multichain.Filecoin {
986+
Expect(tx.Hash()).To(Equal(accountTx.Hash()))
987+
}
976988
break
977989
}
978990
// wait and retry querying for the transaction
@@ -982,7 +994,7 @@ var _ = Describe("Multichain", func() {
982994

983995
It("should be able to fetch the latest block", func() {
984996
// Initialise client
985-
accountClient, _ := accountChain.initialise(accountChain.rpcURL, nil)
997+
accountClient, _ := accountChain.initialise(accountChain.rpcURL)
986998

987999
latestBlock, err := accountClient.LatestBlock(ctx)
9881000
Expect(err).NotTo(HaveOccurred())

0 commit comments

Comments
 (0)