github.com/status-im/status-go@v1.1.0/eth-node/bridge/geth/keystore.go (about)

     1  package gethbridge
     2  
     3  import (
     4  	"crypto/ecdsa"
     5  	"errors"
     6  	"strings"
     7  
     8  	"github.com/ethereum/go-ethereum/accounts"
     9  	"github.com/ethereum/go-ethereum/accounts/keystore"
    10  	"github.com/ethereum/go-ethereum/common"
    11  
    12  	"github.com/status-im/status-go/eth-node/types"
    13  	"github.com/status-im/status-go/extkeys"
    14  )
    15  
    16  type gethKeyStoreAdapter struct {
    17  	keystore *keystore.KeyStore
    18  }
    19  
    20  // WrapKeyStore creates a types.KeyStore wrapper over a keystore.KeyStore object
    21  func WrapKeyStore(keystore *keystore.KeyStore) types.KeyStore {
    22  	return &gethKeyStoreAdapter{keystore: keystore}
    23  }
    24  
    25  func (k *gethKeyStoreAdapter) ImportECDSA(priv *ecdsa.PrivateKey, passphrase string) (types.Account, error) {
    26  	gethAccount, err := k.keystore.ImportECDSA(priv, passphrase)
    27  	return accountFrom(gethAccount), err
    28  }
    29  
    30  func (k *gethKeyStoreAdapter) ImportSingleExtendedKey(extKey *extkeys.ExtendedKey, passphrase string) (types.Account, error) {
    31  	gethAccount, err := k.keystore.ImportSingleExtendedKey(extKey, passphrase)
    32  	return accountFrom(gethAccount), err
    33  }
    34  
    35  func (k *gethKeyStoreAdapter) ImportExtendedKeyForPurpose(keyPurpose extkeys.KeyPurpose, extKey *extkeys.ExtendedKey, passphrase string) (types.Account, error) {
    36  	gethAccount, err := k.keystore.ImportExtendedKeyForPurpose(keyPurpose, extKey, passphrase)
    37  	return accountFrom(gethAccount), err
    38  }
    39  
    40  func (k *gethKeyStoreAdapter) AccountDecryptedKey(a types.Account, auth string) (types.Account, *types.Key, error) {
    41  	gethAccount, err := gethAccountFrom(a)
    42  	if err != nil {
    43  		return types.Account{}, nil, err
    44  	}
    45  
    46  	var gethKey *keystore.Key
    47  	gethAccount, gethKey, err = k.keystore.AccountDecryptedKey(gethAccount, auth)
    48  	return accountFrom(gethAccount), keyFrom(gethKey), err
    49  }
    50  
    51  func (k *gethKeyStoreAdapter) Delete(a types.Account) error {
    52  	gethAccount, err := gethAccountFrom(a)
    53  	if err != nil {
    54  		return err
    55  	}
    56  
    57  	return k.keystore.Delete(gethAccount)
    58  }
    59  
    60  // parseGethURL converts a user supplied URL into the accounts specific structure.
    61  func parseGethURL(url string) (accounts.URL, error) {
    62  	parts := strings.Split(url, "://")
    63  	if len(parts) != 2 || parts[0] == "" {
    64  		return accounts.URL{}, errors.New("protocol scheme missing")
    65  	}
    66  	return accounts.URL{
    67  		Scheme: parts[0],
    68  		Path:   parts[1],
    69  	}, nil
    70  }
    71  
    72  func gethAccountFrom(account types.Account) (accounts.Account, error) {
    73  	var (
    74  		gethAccount accounts.Account
    75  		err         error
    76  	)
    77  	gethAccount.Address = common.Address(account.Address)
    78  	if account.URL != "" {
    79  		gethAccount.URL, err = parseGethURL(account.URL)
    80  	}
    81  	return gethAccount, err
    82  }
    83  
    84  func accountFrom(gethAccount accounts.Account) types.Account {
    85  	return types.Account{
    86  		Address: types.Address(gethAccount.Address),
    87  		URL:     gethAccount.URL.String(),
    88  	}
    89  }
    90  
    91  func keyFrom(k *keystore.Key) *types.Key {
    92  	if k == nil {
    93  		return nil
    94  	}
    95  
    96  	return &types.Key{
    97  		ID:              k.Id,
    98  		Address:         types.Address(k.Address),
    99  		PrivateKey:      k.PrivateKey,
   100  		ExtendedKey:     k.ExtendedKey,
   101  		SubAccountIndex: k.SubAccountIndex,
   102  	}
   103  }