github.com/smartcontractkit/chainlink-testing-framework/libs@v0.0.0-20240227141906-ec710b4eb1a3/blockchain/wallet.go (about)

     1  package blockchain
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/ethereum/go-ethereum/common"
     7  	"github.com/ethereum/go-ethereum/crypto"
     8  )
     9  
    10  // EthereumWallet is the implementation to allow testing with ETH based wallets
    11  type EthereumWallet struct {
    12  	privateKey string
    13  	address    common.Address
    14  }
    15  
    16  // NewEthereumWallet returns the instantiated ETH wallet based on a given private key
    17  func NewEthereumWallet(pk string) (*EthereumWallet, error) {
    18  	privateKey, err := crypto.HexToECDSA(pk)
    19  	if err != nil {
    20  		return nil, fmt.Errorf("invalid private key: %w", err)
    21  	}
    22  	return &EthereumWallet{
    23  		privateKey: pk,
    24  		address:    crypto.PubkeyToAddress(privateKey.PublicKey),
    25  	}, nil
    26  }
    27  
    28  // RawPrivateKey returns raw private key if it has some encoding or in bytes
    29  func (e *EthereumWallet) RawPrivateKey() interface{} {
    30  	return e.privateKey
    31  }
    32  
    33  // PrivateKey returns the private key for a given Ethereum wallet
    34  func (e *EthereumWallet) PrivateKey() string {
    35  	return e.privateKey
    36  }
    37  
    38  // Address returns the ETH address for a given wallet
    39  func (e *EthereumWallet) Address() string {
    40  	return e.address.String()
    41  }