aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAaron Buchwald <aaron.buchwald56@gmail.com>2020-11-03 20:11:32 -0500
committerAaron Buchwald <aaron.buchwald56@gmail.com>2020-11-04 12:28:05 -0500
commitd46b49cc7a0c624cb29eb40016d956d10e100ec9 (patch)
treecd0906c74478ee2203285b82cd3aa0bfce598d50
parent24cf772145b7d976fff613a51b7d22f70fc1ac34 (diff)
Add bech32 address parsing
-rw-r--r--plugin/evm/vm.go41
1 files changed, 38 insertions, 3 deletions
diff --git a/plugin/evm/vm.go b/plugin/evm/vm.go
index a8c4fc0..fd177a0 100644
--- a/plugin/evm/vm.go
+++ b/plugin/evm/vm.go
@@ -155,7 +155,8 @@ func init() {
type VM struct {
ctx *snow.Context
- CLIConfig CommandLineConfig
+ CLIConfig CommandLineConfig
+ encodingManager formatting.EncodingManager
chainID *big.Int
networkID uint64
@@ -236,6 +237,12 @@ func (vm *VM) Initialize(
return vm.CLIConfig.ParsingError
}
+ encodingManager, err := formatting.NewEncodingManager(formatting.CB58Encoding)
+ if err != nil {
+ return fmt.Errorf("problem creating encoding manager: %w", err)
+ }
+ vm.encodingManager = encodingManager
+
if len(fxs) > 0 {
return errUnsupportedFXs
}
@@ -243,8 +250,7 @@ func (vm *VM) Initialize(
vm.ctx = ctx
vm.chaindb = Database{db}
g := new(core.Genesis)
- err := json.Unmarshal(b, g)
- if err != nil {
+ if err := json.Unmarshal(b, g); err != nil {
return err
}
@@ -947,6 +953,35 @@ func (vm *VM) GetAcceptedNonce(address common.Address) (uint64, error) {
return state.GetNonce(address), nil
}
+// ParseLocalAddress takes in an address for this chain and produces the ID
+func (vm *VM) ParseLocalAddress(addrStr string) (ids.ShortID, error) {
+ chainID, addr, err := vm.ParseAddress(addrStr)
+ if err != nil {
+ return ids.ShortID{}, err
+ }
+ if !chainID.Equals(vm.ctx.ChainID) {
+ return ids.ShortID{}, fmt.Errorf("expected chainID to be %q but was %q",
+ vm.ctx.ChainID, chainID)
+ }
+ return addr, nil
+}
+
+// FormatLocalAddress takes in a raw address and produces the formatted address
+func (vm *VM) FormatLocalAddress(addr ids.ShortID) (string, error) {
+ return vm.FormatAddress(vm.ctx.ChainID, addr)
+}
+
+// FormatAddress takes in a chainID and a raw address and produces the formatted
+// address
+func (vm *VM) FormatAddress(chainID ids.ID, addr ids.ShortID) (string, error) {
+ chainIDAlias, err := vm.ctx.BCLookup.PrimaryAlias(chainID)
+ if err != nil {
+ return "", err
+ }
+ hrp := constants.GetHRP(vm.ctx.NetworkID)
+ return formatting.FormatAddress(chainIDAlias, hrp, addr.Bytes())
+}
+
// ParseEthAddress parses [addrStr] and returns an Ethereum address
func ParseEthAddress(addrStr string) (common.Address, error) {
if !common.IsHexAddress(addrStr) {