Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Cosmos config initialisation #64

Merged
merged 2 commits into from
Nov 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 7 additions & 11 deletions chain/cosmos/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
6 changes: 0 additions & 6 deletions chain/cosmos/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down
2 changes: 0 additions & 2 deletions chain/cosmos/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion chain/terra/address_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Expand Down
14 changes: 14 additions & 0 deletions chain/terra/terra.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion chain/terra/terra_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())

Expand Down
9 changes: 4 additions & 5 deletions multichain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,10 @@ var _ = Describe("Multichain", func() {
{
multichain.Terra,
func() multichain.AddressEncodeDecoder {
return terra.NewAddressEncodeDecoder("terra")
return terra.NewAddressEncodeDecoder()
},
func() multichain.Address {
pk := secp256k1.GenPrivKey()
cosmossdk.GetConfig().SetBech32PrefixForAccount("terra", "terrapub")
addr := cosmossdk.AccAddress(pk.PubKey().Address())
return multichain.Address(addr.String())
},
Expand Down Expand Up @@ -317,7 +316,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{}
Expand All @@ -330,15 +329,15 @@ 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
},
"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
Expand Down