Skip to main content

providers/classes/PrivateKeyProvider.md

A wallet provider over a private key. Always ready to sign: connect() / disconnect() only toggle isConnected().

Example​

const provider = new PrivateKeyProvider(process.env.PRIVATE_KEY!);
const signature = await provider.signMessage('Hello');
const signer = provider.getSigner(); // native GalaChain signing

Implements​

Constructors​

Constructor​

new PrivateKeyProvider(privateKey): PrivateKeyProvider

Parameters​

ParameterTypeDescription
privateKeystring0x plus 64 hex digits, or the 64 digits (any case).

Returns​

PrivateKeyProvider

Throws​

GalaSDKError INVALID_PRIVATE_KEY (bad_encoding, not_on_curve). The key is never part of the error.

Properties​

PropertyModifierTypeDescription
providerTypereadonlyWalletProviderTypeWhether the provider holds a raw key (privateKey) or signs in a wallet (external).

Methods​

connect()​

connect(): Promise<string>

Marks the provider connected (it always can sign) and returns its account.

Returns​

Promise<string>

Implementation of​

WalletProvider.connect


disconnect()​

disconnect(): Promise<void>

Marks the provider disconnected; it can still sign.

Returns​

Promise<void>

Implementation of​

WalletProvider.disconnect


exportPrivateKey()​

exportPrivateKey(): string

The key, synchronously: 0x plus 64 lower-case hex digits. For code that must build its own signer from it (an ethers Wallet) in a synchronous path.

Returns​

string


getAddress()​

getAddress(): Promise<string>

The key's account: 0x plus the EIP-55 digits.

Returns​

Promise<string>

Implementation of​

WalletProvider.getAddress


getGalaAddress()​

getGalaAddress(): Promise<string>

The key's account as GalaChain spells it: eth| plus the EIP-55 digits.

Returns​

Promise<string>

Implementation of​

WalletProvider.getGalaAddress


getPrivateKey()​

getPrivateKey(): Promise<string>

The key: 0x plus 64 lower-case hex digits.

Returns​

Promise<string>

Implementation of​

WalletProvider.getPrivateKey


getSigner()​

getSigner(): GalaChainSigner

The core PrivateKeySigner (native GalaChain signing) over this key.

Returns​

GalaChainSigner

Implementation of​

WalletProvider.getSigner


isConnected()​

isConnected(): boolean

True until disconnect() (again after connect()).

Returns​

boolean

Implementation of​

WalletProvider.isConnected


onAccountChanged()​

onAccountChanged(listener): () => void

Calls listener with the account on connect() and null on disconnect().

Parameters​

ParameterType
listenerAccountListener

Returns​

The function that stops the calls.

() => void

Implementation of​

WalletProvider.onAccountChanged


signMessage()​

signMessage(message): Promise<string>

Signs message as personal_sign does (EIP-191 over its UTF-8 bytes).

Parameters​

ParameterType
messagestring

Returns​

Promise<string>

Throws​

GalaSDKError SIGNING_FAILED for a message with a lone surrogate (it has no UTF-8 encoding; a wallet would sign U+FFFD instead of what was shown).

Implementation of​

WalletProvider.signMessage


signTypedData()​

signTypedData(domain, types, value): Promise<string>

Signs EIP-712 typed data (eth_signTypedData_v4) with core's encoder, which fails closed on a value ethers would have coerced (TYPED_DATA_*).

Parameters​

ParameterType
domainTypedDataDomain
typesRecord<string, TypedDataField[]>
valueRecord<string, unknown>

Returns​

Promise<string>

Throws​

GalaSDKError TYPED_DATA_* for typed data the encoder refuses, SIGNING_FAILED.

Implementation of​

WalletProvider.signTypedData


fromWallet()​

static fromWallet(wallet): PrivateKeyProvider

A provider over the key an ethers Wallet (or any { privateKey }) holds.

Parameters​

ParameterType
wallet{ privateKey: string; }
wallet.privateKeystring

Returns​

PrivateKeyProvider