From d46b49cc7a0c624cb29eb40016d956d10e100ec9 Mon Sep 17 00:00:00 2001 From: Aaron Buchwald Date: Tue, 3 Nov 2020 20:11:32 -0500 Subject: Add bech32 address parsing --- plugin/evm/vm.go | 41 ++++++++++++++++++++++++++++++++++++++--- 1 file 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) { -- cgit v1.2.3-70-g09d2