-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix bug with address during compilation (#168)
- Loading branch information
1 parent
f99c836
commit 78c2acd
Showing
5 changed files
with
76 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package types | ||
|
||
import ( | ||
"encoding/hex" | ||
"fmt" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
) | ||
|
||
// Parse string address representation (both libra hex and bech32). | ||
func GetAccAddressFromHexOrBech32(strAddr string) (address sdk.AccAddress, err error) { | ||
address, err = hex.DecodeString(strAddr) | ||
if err != nil { | ||
address, err = sdk.AccAddressFromBech32(strAddr) | ||
if err != nil { | ||
err = fmt.Errorf("can't parse address %q (should be libra hex or bech32): %v", strAddr, err) | ||
} | ||
} | ||
|
||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// +build unit | ||
|
||
package types | ||
|
||
import ( | ||
"encoding/hex" | ||
"testing" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/stretchr/testify/require" | ||
"github.com/tendermint/tendermint/crypto/secp256k1" | ||
) | ||
|
||
func Test_GetAccAddressFromHexOrBech32(t *testing.T) { | ||
bech32 := sdk.AccAddress(secp256k1.GenPrivKey().PubKey().Address()) | ||
|
||
addr, err := GetAccAddressFromHexOrBech32(bech32.String()) | ||
require.NoError(t, err) | ||
|
||
require.Truef(t, bech32.Equals(addr), "bech32 addresses doesn't match", bech32, addr) | ||
|
||
libraAddr := hex.EncodeToString(bech32) | ||
|
||
addr, err = GetAccAddressFromHexOrBech32(libraAddr) | ||
require.NoError(t, err) | ||
|
||
require.Truef(t, addr.Equals(bech32), "libra hex addresses doesn't match", libraAddr, addr) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
// +build unit | ||
|
||
package types | ||
|
||
import ( | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters