Skip to content

Commit 2bb1017

Browse files
authored
Merge pull request #128 from renproject/feat/update_buildtx
updated buildtx api
2 parents 0082378 + b6e7e36 commit 2bb1017

File tree

10 files changed

+96
-40
lines changed

10 files changed

+96
-40
lines changed

api/account/account.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package account
66
import (
77
"context"
88

9+
"github.com/renproject/id"
910
"github.com/renproject/multichain/api/address"
1011
"github.com/renproject/multichain/api/contract"
1112
"github.com/renproject/pack"
@@ -56,7 +57,7 @@ type Tx interface {
5657
// information, and this should be accepted during the construction of the
5758
// chain-specific transaction builder.
5859
type TxBuilder interface {
59-
BuildTx(ctx context.Context, from, to address.Address, value, nonce, gasLimit, gasPrice, gasCap pack.U256, payload pack.Bytes) (Tx, error)
60+
BuildTx(ctx context.Context, fromPubKey *id.PubKey, to address.Address, value, nonce, gasLimit, gasPrice, gasCap pack.U256, payload pack.Bytes) (Tx, error)
6061
}
6162

6263
// The Client interface defines the functionality required to interact with a

chain/cosmos/tx.go

+5-6
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package cosmos
33
import (
44
"context"
55
"crypto/sha256"
6-
"encoding/hex"
76
"fmt"
87
"math/big"
98

@@ -16,11 +15,13 @@ import (
1615
authsigning "github.com/cosmos/cosmos-sdk/x/auth/signing"
1716
"github.com/cosmos/cosmos-sdk/x/auth/tx"
1817
bankType "github.com/cosmos/cosmos-sdk/x/bank/types"
18+
"github.com/renproject/id"
1919
"github.com/renproject/multichain"
2020
"github.com/renproject/multichain/api/account"
2121
"github.com/renproject/multichain/api/address"
2222
"github.com/renproject/multichain/api/contract"
2323
"github.com/renproject/pack"
24+
"github.com/renproject/surge"
2425
"github.com/tendermint/tendermint/crypto/tmhash"
2526
)
2627

@@ -75,15 +76,13 @@ func (builder txBuilder) WithSignMode(signMode int32) txBuilder {
7576
// BuildTx consumes a list of MsgSend to build and return a cosmos transaction.
7677
// This transaction is unsigned, and must be signed before submitting to the
7778
// cosmos chain.
78-
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))
79+
func (builder txBuilder) BuildTx(ctx context.Context, fromPubKey *id.PubKey, to address.Address, value, nonce, gasLimit, gasPrice, gasCap pack.U256, payload pack.Bytes) (account.Tx, error) {
80+
pubKeyBytes, err := surge.ToBinary(fromPubKey)
8281
if err != nil {
8382
return nil, err
8483
}
8584
pubKey := secp256k1.PubKey{Key: pubKeyBytes}
86-
from = multichain.Address(types.AccAddress(pubKey.Address()).String())
85+
from := multichain.Address(types.AccAddress(pubKey.Address()).String())
8786

8887
fromAddr, err := types.AccAddressFromBech32(string(from))
8988
if err != nil {

chain/ethereum/tx.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77

88
"github.com/ethereum/go-ethereum/common"
99
"github.com/ethereum/go-ethereum/core/types"
10+
"github.com/renproject/id"
1011
"github.com/renproject/multichain/api/account"
1112
"github.com/renproject/multichain/api/address"
1213
"github.com/renproject/multichain/chain/evm"
@@ -26,7 +27,7 @@ func NewTxBuilder(chainID *big.Int) TxBuilder {
2627
}
2728

2829
// BuildTx receives transaction fields and constructs a new transaction.
29-
func (txBuilder TxBuilder) BuildTx(ctx context.Context, from, to address.Address, value, nonce, gas, gasTipCap, gasFeeCap pack.U256, payload pack.Bytes) (account.Tx, error) {
30+
func (txBuilder TxBuilder) BuildTx(ctx context.Context, fromPubKey *id.PubKey, to address.Address, value, nonce, gas, gasTipCap, gasFeeCap pack.U256, payload pack.Bytes) (account.Tx, error) {
3031
toAddr, err := NewAddressFromHex(string(pack.String(to)))
3132
if err != nil {
3233
return nil, fmt.Errorf("bad to address '%v': %v", to, err)

chain/evm/tx.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77

88
"github.com/ethereum/go-ethereum/common"
99
"github.com/ethereum/go-ethereum/core/types"
10+
"github.com/renproject/id"
1011
"github.com/renproject/multichain/api/account"
1112
"github.com/renproject/multichain/api/address"
1213
"github.com/renproject/multichain/api/contract"
@@ -26,7 +27,7 @@ func NewTxBuilder(chainID *big.Int) TxBuilder {
2627
}
2728

2829
// BuildTx receives transaction fields and constructs a new transaction.
29-
func (txBuilder TxBuilder) BuildTx(ctx context.Context, from, to address.Address, value, nonce, gasLimit, gasPrice, gasCap pack.U256, payload pack.Bytes) (account.Tx, error) {
30+
func (txBuilder TxBuilder) BuildTx(ctx context.Context, fromPubKey *id.PubKey, to address.Address, value, nonce, gasLimit, gasPrice, gasCap pack.U256, payload pack.Bytes) (account.Tx, error) {
3031
toAddr, err := NewAddressFromHex(string(pack.String(to)))
3132
if err != nil {
3233
return nil, fmt.Errorf("bad to address '%v': %v", to, err)

chain/filecoin/account.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,17 @@ package filecoin
33
import (
44
"bytes"
55
"context"
6+
"crypto/ecdsa"
67
"fmt"
78

9+
ethcrypto "github.com/ethereum/go-ethereum/crypto"
810
filaddress "github.com/filecoin-project/go-address"
911
"github.com/filecoin-project/go-state-types/abi"
1012
"github.com/filecoin-project/go-state-types/big"
1113
"github.com/filecoin-project/go-state-types/crypto"
1214
"github.com/filecoin-project/lotus/chain/types"
1315
"github.com/minio/blake2b-simd"
16+
"github.com/renproject/id"
1417
"github.com/renproject/multichain/api/account"
1518
"github.com/renproject/multichain/api/address"
1619
"github.com/renproject/multichain/api/contract"
@@ -29,10 +32,11 @@ func NewTxBuilder() TxBuilder {
2932
}
3033

3134
// BuildTx receives transaction fields and constructs a new transaction.
32-
func (txBuilder TxBuilder) BuildTx(ctx context.Context, from, to address.Address, value, nonce, gasLimit, gasPrice, gasCap pack.U256, payload pack.Bytes) (account.Tx, error) {
33-
filfrom, err := filaddress.NewFromString(string(from))
35+
func (txBuilder TxBuilder) BuildTx(ctx context.Context, fromPubKey *id.PubKey, to address.Address, value, nonce, gasLimit, gasPrice, gasCap pack.U256, payload pack.Bytes) (account.Tx, error) {
36+
pubKeyUncompressed := ethcrypto.FromECDSAPub((*ecdsa.PublicKey)(fromPubKey))
37+
filfrom, err := filaddress.NewSecp256k1Address(pubKeyUncompressed)
3438
if err != nil {
35-
return nil, fmt.Errorf("bad from address '%v': %v", from, err)
39+
return nil, fmt.Errorf("bad from pubkey: %v", err)
3640
}
3741
filto, err := filaddress.NewFromString(string(to))
3842
if err != nil {

chain/filecoin/filecoin_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ var _ = Describe("Filecoin", func() {
8282

8383
tx, err := filTxBuilder.BuildTx(
8484
ctx,
85-
sender,
85+
senderPrivKey.PubKey(),
8686
multichain.Address(pack.String(recipientFilAddr.String())),
8787
amount, // amount
8888
nonce, // nonce

chain/terra/terra_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ var _ = Describe("Terra", func() {
7575
amount := pack.NewU256FromU64(pack.U64(2000000))
7676
tx, err := txBuilder.BuildTx(
7777
ctx,
78-
multichain.Address(addr.String()), // from
78+
privKey.PubKey(), // fromPubKey
7979
recipient, // to
8080
amount, // amount
8181
nonce, // nonce

go.mod

-8
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,4 @@ replace github.com/filecoin-project/filecoin-ffi => ./chain/filecoin/filecoin-ff
3333

3434
replace github.com/renproject/solana-ffi => ./chain/solana/solana-ffi
3535

36-
replace github.com/cosmos/ledger-cosmos-go => github.com/terra-money/ledger-terra-go v0.11.2
37-
38-
replace google.golang.org/grpc => google.golang.org/grpc v1.33.2
39-
4036
replace github.com/gogo/protobuf => github.com/regen-network/protobuf v1.3.3-alpha.regen.1
41-
42-
replace github.com/tendermint/tendermint => github.com/tendermint/tendermint v0.34.12
43-
44-
replace github.com/99designs/keyring => github.com/cosmos/keyring v1.1.7-0.20210622111912-ef00f8ac3d76

0 commit comments

Comments
 (0)