From 47e1aac048712959399d962c12ea4afc5844e8f0 Mon Sep 17 00:00:00 2001 From: Jaz Gulati Date: Fri, 13 Nov 2020 12:54:18 +1100 Subject: [PATCH 1/2] chain/cosmos, chain/terra: set bech32 prefix on boot --- chain/cosmos/address.go | 18 +++++++----------- chain/cosmos/client.go | 6 ------ chain/cosmos/tx.go | 2 -- chain/terra/address_test.go | 2 +- chain/terra/terra.go | 14 ++++++++++++++ chain/terra/terra_test.go | 2 +- multichain_test.go | 8 ++++---- 7 files changed, 27 insertions(+), 25 deletions(-) diff --git a/chain/cosmos/address.go b/chain/cosmos/address.go index 63f9c172..33f63274 100644 --- a/chain/cosmos/address.go +++ b/chain/cosmos/address.go @@ -28,37 +28,34 @@ type AddressEncodeDecoder struct { } // NewAddressEncodeDecoder creates a new address encoder-decoder -func NewAddressEncodeDecoder(hrp string) AddressEncodeDecoder { +func NewAddressEncodeDecoder() AddressEncodeDecoder { return AddressEncodeDecoder{ - AddressEncoder: NewAddressEncoder(hrp), - AddressDecoder: NewAddressDecoder(hrp), + AddressEncoder: NewAddressEncoder(), + AddressDecoder: NewAddressDecoder(), } } // AddressEncoder implements the address.Encoder interface type AddressEncoder struct { - hrp string } // AddressDecoder implements the address.Decoder interface type AddressDecoder struct { - hrp string } // NewAddressDecoder creates a new address decoder -func NewAddressDecoder(hrp string) AddressDecoder { - return AddressDecoder{hrp: hrp} +func NewAddressDecoder() AddressDecoder { + return AddressDecoder{} } // NewAddressEncoder creates a new address encoder -func NewAddressEncoder(hrp string) AddressEncoder { - return AddressEncoder{hrp: hrp} +func NewAddressEncoder() AddressEncoder { + return AddressEncoder{} } // DecodeAddress consumes a human-readable representation of a cosmos // compatible address and decodes it to its raw bytes representation. func (decoder AddressDecoder) DecodeAddress(addr address.Address) (address.RawAddress, error) { - sdk.GetConfig().SetBech32PrefixForAccount(decoder.hrp, decoder.hrp+"pub") rawAddr, err := sdk.AccAddressFromBech32(string(addr)) if err != nil { return nil, err @@ -69,7 +66,6 @@ func (decoder AddressDecoder) DecodeAddress(addr address.Address) (address.RawAd // EncodeAddress consumes raw bytes and encodes them to a human-readable // address format. func (encoder AddressEncoder) EncodeAddress(rawAddr address.RawAddress) (address.Address, error) { - sdk.GetConfig().SetBech32PrefixForAccount(encoder.hrp, encoder.hrp+"pub") bech32Addr := sdk.AccAddress(rawAddr) return address.Address(bech32Addr.String()), nil } diff --git a/chain/cosmos/client.go b/chain/cosmos/client.go index d231a68c..5ca24533 100644 --- a/chain/cosmos/client.go +++ b/chain/cosmos/client.go @@ -169,8 +169,6 @@ func (client *Client) SubmitTx(ctx context.Context, tx account.Tx) error { // AccountNonce returns the current nonce of the account. This is the nonce to // be used while building a new transaction. func (client *Client) AccountNonce(_ context.Context, addr address.Address) (pack.U256, error) { - types.GetConfig().SetBech32PrefixForAccount(client.hrp, client.hrp+"pub") - cosmosAddr, err := types.AccAddressFromBech32(string(addr)) if err != nil { return pack.U256{}, fmt.Errorf("bad address: '%v': %v", addr, err) @@ -187,8 +185,6 @@ func (client *Client) AccountNonce(_ context.Context, addr address.Address) (pac // AccountNumber returns the account number for a given address. func (client *Client) AccountNumber(_ context.Context, addr address.Address) (pack.U64, error) { - types.GetConfig().SetBech32PrefixForAccount(client.hrp, client.hrp+"pub") - cosmosAddr, err := types.AccAddressFromBech32(string(addr)) if err != nil { return 0, fmt.Errorf("bad address: '%v': %v", addr, err) @@ -205,8 +201,6 @@ func (client *Client) AccountNumber(_ context.Context, addr address.Address) (pa // AccountBalance returns the account balancee for a given address. func (client *Client) AccountBalance(_ context.Context, addr address.Address) (pack.U256, error) { - types.GetConfig().SetBech32PrefixForAccount(client.hrp, client.hrp+"pub") - cosmosAddr, err := types.AccAddressFromBech32(string(addr)) if err != nil { return pack.U256{}, fmt.Errorf("bad address: '%v': %v", addr, err) diff --git a/chain/cosmos/tx.go b/chain/cosmos/tx.go index 5a91d39d..b96f229a 100644 --- a/chain/cosmos/tx.go +++ b/chain/cosmos/tx.go @@ -65,8 +65,6 @@ func NewTxBuilder(options TxBuilderOptions, client *Client) account.TxBuilder { // This transaction is unsigned, and must be signed before submitting to the // cosmos chain. func (builder txBuilder) BuildTx(ctx context.Context, from, to address.Address, value, nonce, gasLimit, gasPrice, gasCap pack.U256, payload pack.Bytes) (account.Tx, error) { - types.GetConfig().SetBech32PrefixForAccount(builder.client.hrp, builder.client.hrp+"pub") - fromAddr, err := types.AccAddressFromBech32(string(from)) if err != nil { return nil, err diff --git a/chain/terra/address_test.go b/chain/terra/address_test.go index cb31d74b..9aeb8729 100644 --- a/chain/terra/address_test.go +++ b/chain/terra/address_test.go @@ -13,7 +13,7 @@ var _ = Describe("Terra", func() { Context("when decoding address", func() { Context("when decoding Terra address", func() { It("should work", func() { - decoder := terra.NewAddressDecoder("terra") + decoder := terra.NewAddressDecoder() addrStr := "terra1ztez03dp94y2x55fkhmrvj37ck204geq33msma" _, err := decoder.DecodeAddress(multichain.Address(pack.NewString(addrStr))) diff --git a/chain/terra/terra.go b/chain/terra/terra.go index 31a70dfc..4d6137e0 100644 --- a/chain/terra/terra.go +++ b/chain/terra/terra.go @@ -1,6 +1,7 @@ package terra import ( + "github.com/cosmos/cosmos-sdk/types" "github.com/renproject/multichain/api/account" "github.com/renproject/multichain/chain/cosmos" "github.com/terra-project/core/app" @@ -28,6 +29,19 @@ var ( NewGasEstimator = cosmos.NewGasEstimator ) +// Set the Bech32 address prefix for the globally-defined config variable inside +// Cosmos SDK. This is required as there are a number of functions inside the +// SDK that make use of this global config directly, instead of allowing us to +// provide a custom config. +func init() { + // TODO: This will prevent us from being able to support multiple + // Cosmos-compatible chains in the Multichain. This is expected to be + // resolved before v1.0 of the Cosmos SDK (issue being tracked here: + // https://github.com/cosmos/cosmos-sdk/issues/7448). + types.GetConfig().SetBech32PrefixForAccount("terra", "terrapub") + types.GetConfig().Seal() +} + // NewClient returns returns a new Client with Terra codec. func NewClient(opts ClientOptions) *Client { return cosmos.NewClient(opts, app.MakeCodec(), "terra") diff --git a/chain/terra/terra_test.go b/chain/terra/terra_test.go index 8c5b6d5f..c462c68c 100644 --- a/chain/terra/terra_test.go +++ b/chain/terra/terra_test.go @@ -48,7 +48,7 @@ var _ = Describe("Terra", func() { // random recipient pkRecipient := secp256k1.GenPrivKey() - addrEncoder := terra.NewAddressEncoder("terra") + addrEncoder := terra.NewAddressEncoder() recipient, err := addrEncoder.EncodeAddress(address.RawAddress(pack.Bytes(pkRecipient.PubKey().Address()))) Expect(err).NotTo(HaveOccurred()) diff --git a/multichain_test.go b/multichain_test.go index 98d578ab..4fc32f5b 100644 --- a/multichain_test.go +++ b/multichain_test.go @@ -146,7 +146,7 @@ var _ = Describe("Multichain", func() { { multichain.Terra, func() multichain.AddressEncodeDecoder { - return terra.NewAddressEncodeDecoder("terra") + return terra.NewAddressEncodeDecoder() }, func() multichain.Address { pk := secp256k1.GenPrivKey() @@ -317,7 +317,7 @@ var _ = Describe("Multichain", func() { Expect(err).NotTo(HaveOccurred()) var pk secp256k1.PrivKeySecp256k1 copy(pk[:], pkBytes) - addrEncoder := terra.NewAddressEncoder("terra") + addrEncoder := terra.NewAddressEncoder() senderAddr, err := addrEncoder.EncodeAddress(multichain.RawAddress(pack.Bytes(pk.PubKey().Address()))) Expect(err).NotTo(HaveOccurred()) senderPrivKey := id.PrivKey{} @@ -330,7 +330,7 @@ var _ = Describe("Multichain", func() { Expect(err).NotTo(HaveOccurred()) var pk secp256k1.PrivKeySecp256k1 copy(pk[:], pkBytes) - addrEncoder := terra.NewAddressEncoder("terra") + addrEncoder := terra.NewAddressEncoder() addr, err := addrEncoder.EncodeAddress(multichain.RawAddress(pack.Bytes(pk.PubKey().Address()))) Expect(err).NotTo(HaveOccurred()) return addr @@ -338,7 +338,7 @@ var _ = Describe("Multichain", func() { "http://127.0.0.1:26657", func() multichain.Address { recipientKey := secp256k1.GenPrivKey() - addrEncoder := terra.NewAddressEncoder("terra") + addrEncoder := terra.NewAddressEncoder() recipient, err := addrEncoder.EncodeAddress(multichain.RawAddress(pack.Bytes(recipientKey.PubKey().Address()))) Expect(err).NotTo(HaveOccurred()) return recipient From 4e5e107c6dd44aa3e2f398e71cf1fe7aa0432a8c Mon Sep 17 00:00:00 2001 From: Jaz Gulati Date: Fri, 13 Nov 2020 14:24:21 +1100 Subject: [PATCH 2/2] multichain_test: fix modifying sealed config --- multichain_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/multichain_test.go b/multichain_test.go index 4fc32f5b..83181ef5 100644 --- a/multichain_test.go +++ b/multichain_test.go @@ -150,7 +150,6 @@ var _ = Describe("Multichain", func() { }, func() multichain.Address { pk := secp256k1.GenPrivKey() - cosmossdk.GetConfig().SetBech32PrefixForAccount("terra", "terrapub") addr := cosmossdk.AccAddress(pk.PubKey().Address()) return multichain.Address(addr.String()) },