Skip to content

Commit

Permalink
[accounts] Expose internal Address value, parsing (#1734)
Browse files Browse the repository at this point in the history
  • Loading branch information
fxfactorial authored Oct 16, 2019
1 parent 62268ed commit 9ef6c8a
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions accounts/address.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package accounts

import (
"github.com/harmony-one/harmony/internal/bech32"
"github.com/harmony-one/harmony/internal/common"
"github.com/pkg/errors"
)

// ParseAddrH is a wrapper to cast ethCommon.Address to harmony's common.Address
func ParseAddrH(s string) common.Address {
return common.Address(common.ParseAddr(s))
}

// MustBech32ToAddressH is a wrapper for casting ethCommon.Address to harmony's common.Address
func MustBech32ToAddressH(b32 string) common.Address {
return common.Address(common.MustBech32ToAddress(b32))
}

// Bech32ToAddressH decodes the given bech32 address.
func Bech32ToAddressH(b32 string) (addr common.Address, err error) {
var hrp string
err = ParseBech32AddrH(b32, &hrp, &addr)
if err == nil && hrp != common.Bech32AddressHRP {
err = errors.Errorf("%#v is not a %#v address", b32, common.Bech32AddressHRP)
}
return
}

// ParseBech32AddrH is another wrapper
func ParseBech32AddrH(b32 string, hrp *string, addr *common.Address) error {
h, b, err := bech32.DecodeAndConvert(b32)
if err != nil {
return errors.Wrapf(err, "cannot decode %#v as bech32 address", b32)
}
if len(b) != common.AddressLength {
return errors.Errorf("decoded bech32 %#v has invalid length %d",
b32, len(b))
}
*hrp = h
addr.SetBytes(b)
return nil
}

0 comments on commit 9ef6c8a

Please sign in to comment.