Skip to content

Commit a9df6cd

Browse files
committed
updated build tx api
Signed-off-by: Soumya Ghosh Dastidar <[email protected]>
1 parent 8f5bf72 commit a9df6cd

File tree

6 files changed

+27
-22
lines changed

6 files changed

+27
-22
lines changed

api/account/account.go

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

9+
"github.com/renproject/id"
10+
911
"github.com/renproject/multichain/api/address"
1012
"github.com/renproject/multichain/api/contract"
1113
"github.com/renproject/pack"
@@ -56,7 +58,7 @@ type Tx interface {
5658
// information, and this should be accepted during the construction of the
5759
// chain-specific transaction builder.
5860
type TxBuilder interface {
59-
BuildTx(ctx context.Context, from, to address.Address, value, nonce, gasLimit, gasPrice, gasCap pack.U256, payload pack.Bytes) (Tx, error)
61+
BuildTx(ctx context.Context, fromPubKey *id.PubKey, to address.Address, value, nonce, gasLimit, gasPrice, gasCap pack.U256, payload pack.Bytes) (Tx, error)
6062
}
6163

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

chain/cosmos/tx.go

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

9+
"github.com/renproject/id"
10+
"github.com/renproject/surge"
11+
1012
"github.com/btcsuite/btcd/btcec"
1113
"github.com/cosmos/cosmos-sdk/client"
1214
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
@@ -75,15 +77,15 @@ func (builder txBuilder) WithSignMode(signMode int32) txBuilder {
7577
// BuildTx consumes a list of MsgSend to build and return a cosmos transaction.
7678
// This transaction is unsigned, and must be signed before submitting to the
7779
// 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) {
80+
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) {
7981
// We assume the "from" address is a public key as it is required for
8082
// setting the signature.
81-
pubKeyBytes, err := hex.DecodeString(string(from))
83+
pubKeyBytes, err := surge.ToBinary(fromPubKey)
8284
if err != nil {
8385
return nil, err
8486
}
8587
pubKey := secp256k1.PubKey{Key: pubKeyBytes}
86-
from = multichain.Address(types.AccAddress(pubKey.Address()).String())
88+
from := multichain.Address(types.AccAddress(pubKey.Address()).String())
8789

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

chain/ethereum/tx.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"fmt"
66
"math/big"
77

8+
"github.com/renproject/id"
9+
810
"github.com/ethereum/go-ethereum/common"
911
"github.com/ethereum/go-ethereum/core/types"
1012
"github.com/renproject/multichain/api/account"
@@ -26,7 +28,10 @@ func NewTxBuilder(chainID *big.Int) TxBuilder {
2628
}
2729

2830
// 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) {
31+
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) {
32+
33+
//_ = multichain.Address(crypto.PubkeyToAddress(ecdsa.PublicKey(fromPubKey)).Hex())
34+
3035
toAddr, err := NewAddressFromHex(string(pack.String(to)))
3136
if err != nil {
3237
return nil, fmt.Errorf("bad to address '%v': %v", to, err)

chain/evm/tx.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"fmt"
66
"math/big"
77

8+
"github.com/renproject/id"
9+
810
"github.com/ethereum/go-ethereum/common"
911
"github.com/ethereum/go-ethereum/core/types"
1012
"github.com/renproject/multichain/api/account"
@@ -26,7 +28,7 @@ func NewTxBuilder(chainID *big.Int) TxBuilder {
2628
}
2729

2830
// 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) {
31+
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) {
3032
toAddr, err := NewAddressFromHex(string(pack.String(to)))
3133
if err != nil {
3234
return nil, fmt.Errorf("bad to address '%v': %v", to, err)

chain/filecoin/account.go

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

9+
"github.com/renproject/id"
10+
11+
eth_crypto "github.com/ethereum/go-ethereum/crypto"
812
filaddress "github.com/filecoin-project/go-address"
913
"github.com/filecoin-project/go-state-types/abi"
1014
"github.com/filecoin-project/go-state-types/big"
@@ -29,10 +33,11 @@ func NewTxBuilder() TxBuilder {
2933
}
3034

3135
// 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))
36+
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) {
37+
pubKeyUncompressed := eth_crypto.FromECDSAPub((*ecdsa.PublicKey)(fromPubKey))
38+
filfrom, err := filaddress.NewSecp256k1Address(pubKeyUncompressed)
3439
if err != nil {
35-
return nil, fmt.Errorf("bad from address '%v': %v", from, err)
40+
return nil, fmt.Errorf("bad from pubkey address '%v': %v", fromPubKey, err)
3641
}
3742
filto, err := filaddress.NewFromString(string(to))
3843
if err != nil {

multichain_test.go

+1-12
Original file line numberDiff line numberDiff line change
@@ -924,22 +924,11 @@ var _ = Describe("Multichain", func() {
924924

925925
accountTx, err := txBuilder.BuildTx(
926926
ctx,
927-
multichain.Address(senderAddr),
927+
senderPubKey,
928928
recipientAddr,
929929
amount, nonce, gasLimit, gasPrice, gasCap,
930930
payload,
931931
)
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-
}
943932
Expect(err).NotTo(HaveOccurred())
944933

945934
// Get the transaction bytes and sign them.

0 commit comments

Comments
 (0)