From 61479754d935e36993f7144d70d7c518cdb26a36 Mon Sep 17 00:00:00 2001 From: Aaron Buchwald Date: Wed, 25 Nov 2020 11:20:36 -0500 Subject: Add avax API client and upgrade to avalanchego v1.0.5-client --- plugin/evm/service.go | 78 ++++++++++++--------------------------------------- 1 file changed, 18 insertions(+), 60 deletions(-) (limited to 'plugin/evm/service.go') diff --git a/plugin/evm/service.go b/plugin/evm/service.go index 49bb862..2bb06df 100644 --- a/plugin/evm/service.go +++ b/plugin/evm/service.go @@ -138,7 +138,11 @@ func (service *AvaxAPI) ExportKey(r *http.Request, args *ExportKeyArgs, reply *E if err != nil { return fmt.Errorf("problem retrieving private key: %w", err) } - reply.PrivateKey = constants.SecretKeyPrefix + formatting.CB58{Bytes: sk.Bytes()}.String() + encodedKey, err := formatting.Encode(formatting.CB58, sk.Bytes()) + if err != nil { + return fmt.Errorf("problem encoding bytes as cb58: %w", err) + } + reply.PrivateKey = constants.SecretKeyPrefix + encodedKey reply.PrivateKeyHex = hexutil.Encode(sk.Bytes()) return nil } @@ -158,13 +162,13 @@ func (service *AvaxAPI) ImportKey(r *http.Request, args *ImportKeyArgs, reply *a } trimmedPrivateKey := strings.TrimPrefix(args.PrivateKey, constants.SecretKeyPrefix) - formattedPrivateKey := formatting.CB58{} - if err := formattedPrivateKey.FromString(trimmedPrivateKey); err != nil { + pkBytes, err := formatting.Decode(formatting.CB58, trimmedPrivateKey) + if err != nil { return fmt.Errorf("problem parsing private key: %w", err) } factory := crypto.FactorySECP256K1R{} - skIntf, err := factory.ToPrivateKey(formattedPrivateKey.Bytes) + skIntf, err := factory.ToPrivateKey(pkBytes) if err != nil { return fmt.Errorf("problem parsing private key: %w", err) } @@ -315,49 +319,8 @@ func (service *AvaxAPI) Export(_ *http.Request, args *ExportArgs, response *api. return service.vm.issueTx(tx) } -// Index is an address and an associated UTXO. -// Marks a starting or stopping point when fetching UTXOs. Used for pagination. -type Index struct { - Address string `json:"address"` // The address as a string - UTXO string `json:"utxo"` // The UTXO ID as a string -} - -// GetUTXOsArgs are arguments for passing into GetUTXOs. -// Gets the UTXOs that reference at least one address in [Addresses]. -// Returns at most [limit] addresses. -// If specified, [SourceChain] is the chain where the atomic UTXOs were exported from. If not specified, -// then GetUTXOs returns an error since the C Chain only has atomic UTXOs. -// If [limit] == 0 or > [maxUTXOsToFetch], fetches up to [maxUTXOsToFetch]. -// [StartIndex] defines where to start fetching UTXOs (for pagination.) -// UTXOs fetched are from addresses equal to or greater than [StartIndex.Address] -// For address [StartIndex.Address], only UTXOs with IDs greater than [StartIndex.UTXO] will be returned. -// If [StartIndex] is omitted, gets all UTXOs. -// If GetUTXOs is called multiple times, with our without [StartIndex], it is not guaranteed -// that returned UTXOs are unique. That is, the same UTXO may appear in the response of multiple calls. -type GetUTXOsArgs struct { - Addresses []string `json:"addresses"` - SourceChain string `json:"sourceChain"` - Limit json.Uint32 `json:"limit"` - StartIndex Index `json:"startIndex"` - Encoding string `json:"encoding"` -} - -// GetUTXOsReply defines the GetUTXOs replies returned from the API -type GetUTXOsReply struct { - // Number of UTXOs returned - NumFetched json.Uint64 `json:"numFetched"` - // The UTXOs - UTXOs []string `json:"utxos"` - // The last UTXO that was returned, and the address it corresponds to. - // Used for pagination. To get the rest of the UTXOs, call GetUTXOs - // again and set [StartIndex] to this value. - EndIndex Index `json:"endIndex"` - // Encoding specifies the encoding format the UTXOs are returned in - Encoding string `json:"encoding"` -} - // GetUTXOs gets all utxos for passed in addresses -func (service *AvaxAPI) GetUTXOs(r *http.Request, args *GetUTXOsArgs, reply *GetUTXOsReply) error { +func (service *AvaxAPI) GetUTXOs(r *http.Request, args *api.GetUTXOsArgs, reply *api.GetUTXOsReply) error { service.vm.ctx.Log.Info("EVM: GetUTXOs called for with %s", args.Addresses) if len(args.Addresses) == 0 { @@ -367,11 +330,6 @@ func (service *AvaxAPI) GetUTXOs(r *http.Request, args *GetUTXOsArgs, reply *Get return fmt.Errorf("number of addresses given, %d, exceeds maximum, %d", len(args.Addresses), maxGetUTXOsAddrs) } - encoding, err := service.vm.encodingManager.GetEncoding(args.Encoding) - if err != nil { - return fmt.Errorf("problem getting encoding formatter for '%s': %w", args.Encoding, err) - } - sourceChain := ids.ID{} if args.SourceChain == "" { return errNoSourceChain @@ -418,11 +376,15 @@ func (service *AvaxAPI) GetUTXOs(r *http.Request, args *GetUTXOsArgs, reply *Get reply.UTXOs = make([]string, len(utxos)) for i, utxo := range utxos { - b, err := service.vm.codec.Marshal(utxo) + b, err := service.vm.codec.Marshal(codecVersion, utxo) if err != nil { return fmt.Errorf("problem marshalling UTXO: %w", err) } - reply.UTXOs[i] = encoding.ConvertBytes(b) + str, err := formatting.Encode(args.Encoding, b) + if err != nil { + return fmt.Errorf("problem encoding utxo: %w", err) + } + reply.UTXOs[i] = str } endAddress, err := service.vm.FormatLocalAddress(endAddr) @@ -433,7 +395,7 @@ func (service *AvaxAPI) GetUTXOs(r *http.Request, args *GetUTXOsArgs, reply *Get reply.EndIndex.Address = endAddress reply.EndIndex.UTXO = endUTXOID.String() reply.NumFetched = json.Uint64(len(utxos)) - reply.Encoding = encoding.Encoding() + reply.Encoding = args.Encoding return nil } @@ -441,17 +403,13 @@ func (service *AvaxAPI) GetUTXOs(r *http.Request, args *GetUTXOsArgs, reply *Get func (service *AvaxAPI) IssueTx(r *http.Request, args *api.FormattedTx, response *api.JSONTxID) error { log.Info("EVM: IssueTx called") - encoding, err := service.vm.encodingManager.GetEncoding(args.Encoding) - if err != nil { - return fmt.Errorf("problem getting encoding formatter for '%s': %w", args.Encoding, err) - } - txBytes, err := encoding.ConvertString(args.Tx) + txBytes, err := formatting.Decode(args.Encoding, args.Tx) if err != nil { return fmt.Errorf("problem decoding transaction: %w", err) } tx := &Tx{} - if err := service.vm.codec.Unmarshal(txBytes, tx); err != nil { + if _, err := service.vm.codec.Unmarshal(txBytes, tx); err != nil { return fmt.Errorf("problem parsing transaction: %w", err) } if err := tx.Sign(service.vm.codec, nil); err != nil { -- cgit v1.2.3-70-g09d2