github.com/status-im/status-go@v1.1.0/services/utils/utils.go (about)

     1  package utils
     2  
     3  import (
     4  	"math/big"
     5  	"strings"
     6  
     7  	"github.com/ethereum/go-ethereum/accounts/abi/bind"
     8  	"github.com/ethereum/go-ethereum/common"
     9  	ethTypes "github.com/ethereum/go-ethereum/core/types"
    10  	"github.com/status-im/status-go/account"
    11  	"github.com/status-im/status-go/api/multiformat"
    12  	"github.com/status-im/status-go/eth-node/crypto"
    13  	"github.com/status-im/status-go/eth-node/types"
    14  	prot_common "github.com/status-im/status-go/protocol/common"
    15  )
    16  
    17  func GetSigner(chainID uint64, accountsManager *account.GethManager, keyStoreDir string, from types.Address, password string) bind.SignerFn {
    18  	return func(addr common.Address, tx *ethTypes.Transaction) (*ethTypes.Transaction, error) {
    19  		selectedAccount, err := accountsManager.VerifyAccountPassword(keyStoreDir, from.Hex(), password)
    20  		if err != nil {
    21  			return nil, err
    22  		}
    23  		s := ethTypes.NewLondonSigner(new(big.Int).SetUint64(chainID))
    24  		return ethTypes.SignTx(tx, s, selectedAccount.PrivateKey)
    25  	}
    26  }
    27  
    28  func DeserializePublicKey(compressedKey string) (types.HexBytes, error) {
    29  	rawKey, err := multiformat.DeserializePublicKey(compressedKey, "f")
    30  	if err != nil {
    31  		return nil, err
    32  	}
    33  
    34  	secp256k1Code := "fe701"
    35  	pubKeyBytes := "0x" + strings.TrimPrefix(rawKey, secp256k1Code)
    36  
    37  	pubKey, err := prot_common.HexToPubkey(pubKeyBytes)
    38  	if err != nil {
    39  		return nil, err
    40  	}
    41  
    42  	return crypto.CompressPubkey(pubKey), nil
    43  }
    44  
    45  func SerializePublicKey(compressedKey types.HexBytes) (string, error) {
    46  	rawKey, err := crypto.DecompressPubkey(compressedKey)
    47  	if err != nil {
    48  		return "", err
    49  	}
    50  	pubKey := types.EncodeHex(crypto.FromECDSAPub(rawKey))
    51  
    52  	secp256k1Code := "0xe701"
    53  	base58btc := "z"
    54  	multiCodecKey := secp256k1Code + strings.TrimPrefix(pubKey, "0x")
    55  	cpk, err := multiformat.SerializePublicKey(multiCodecKey, base58btc)
    56  	if err != nil {
    57  		return "", err
    58  	}
    59  	return cpk, nil
    60  }