decred.org/dcrdex@v1.0.3/dex/networks/eth/common.go (about)

     1  // This code is available on the terms of the project LICENSE.md file,
     2  // also available online at https://blueoakcouncil.org/license/1.0.0.
     3  
     4  package eth
     5  
     6  import (
     7  	"fmt"
     8  	"sort"
     9  	"strings"
    10  
    11  	"decred.org/dcrdex/dex"
    12  	"github.com/ethereum/go-ethereum/common"
    13  	"github.com/ethereum/go-ethereum/rpc"
    14  )
    15  
    16  // DecodeCoinID decodes the coin ID into a common.Hash. For eth, there are no
    17  // funding coin IDs, just an account address. Care should be taken not to use
    18  // DecodeCoinID or (Driver).DecodeCoinID for account addresses.
    19  func DecodeCoinID(coinID []byte) (common.Hash, error) {
    20  	if len(coinID) != common.HashLength {
    21  		return common.Hash{}, fmt.Errorf("wrong coin ID length. wanted %d, got %d",
    22  			common.HashLength, len(coinID))
    23  	}
    24  	var h common.Hash
    25  	h.SetBytes(coinID)
    26  	return h, nil
    27  }
    28  
    29  // SecretHashSize is the byte-length of the hash of the secret key used in
    30  // swaps.
    31  const SecretHashSize = 32
    32  
    33  // CheckAPIModules checks that the geth node supports the required modules.
    34  func CheckAPIModules(c *rpc.Client, endpoint string, log dex.Logger, reqModules []string) (err error) {
    35  	apis, err := c.SupportedModules()
    36  	if err != nil {
    37  		return fmt.Errorf("unable to check supported modules: %v", err)
    38  	}
    39  	reqModulesMap := make(map[string]struct{})
    40  	for _, mod := range reqModules {
    41  		reqModulesMap[mod] = struct{}{}
    42  	}
    43  	haveModules := make([]string, 0, len(apis))
    44  	for api, version := range apis {
    45  		_, has := reqModulesMap[api]
    46  		if has {
    47  			delete(reqModulesMap, api)
    48  		}
    49  		haveModules = append(haveModules, fmt.Sprintf("%s:%s", api, version))
    50  	}
    51  	if len(reqModulesMap) > 0 {
    52  		reqs := make([]string, 0, len(reqModulesMap))
    53  		for v := range reqModulesMap {
    54  			reqs = append(reqs, v)
    55  		}
    56  		return fmt.Errorf("needed apis not present: %v.", strings.Join(reqs, " "))
    57  	}
    58  	sort.Strings(haveModules)
    59  	log.Debugf("API endpoints supported by %s: %s", endpoint, strings.Join(haveModules, " "))
    60  	return nil
    61  }