@meshsdk/wallet
Version:
Wallets - https://meshjs.dev/apis/wallets
519 lines (506 loc) • 22.5 kB
TypeScript
import { DataSignature, Extension as Extension$1, UTxO, Asset, IFetcher, ISubmitter } from '@meshsdk/common';
interface ISigner {
getPublicKey(): Promise<string>;
getPublicKeyHash(): Promise<string>;
sign(data: string): Promise<string>;
verify(data: string, signature: string): Promise<boolean>;
}
type DerivationPath = number[] | string;
interface ISecretManager {
getPublicKey(derivationPath: DerivationPath): Promise<string>;
getSigner(derivationPath: DerivationPath): Promise<ISigner>;
}
declare class InMemoryBip32 implements ISecretManager {
private bip32PrivateKey;
private constructor();
static fromMnemonic(mnemonic: string[], password?: string): Promise<InMemoryBip32>;
static fromEntropy(entropy: string, password?: string): Promise<InMemoryBip32>;
static fromKeyHex(keyHex: string): InMemoryBip32;
static fromBech32(bech32: string): InMemoryBip32;
/**
* Get the Bip32 public key in hex format.
* @returns {Promise<string>} A promise that resolves to the public key in hex format.
*/
getPublicKey(): Promise<string>;
/**
* Get an ISigner instance initialized with the current Bip32 private key.
* @returns {Promise<ISigner>} A promise that resolves to an ISigner instance initialized with the current Bip32 private key.
*/
getSigner(derivationPath: DerivationPath): Promise<ISigner>;
}
/**
* BaseSigner provides functionalities to sign and verify data using Ed25519 keys.
* It supports construction from both extended and normal private key hex formats.
*/
declare class BaseSigner implements ISigner {
private ed25519PrivateKey;
private constructor();
/**
* Create a BaseSigner instance from an Ed25519 private key in extended hex format.
* @param keyHex Ed25519 private key in extended hex format
* @returns {BaseSigner} A BaseSigner instance
*/
static fromExtendedKeyHex(keyHex: string): BaseSigner;
/**
* Create a BaseSigner instance from an Ed25519 private key in normal hex format.
* @param keyHex Ed25519 private key in normal hex format
* @returns {BaseSigner} A BaseSigner instance
*/
static fromNormalKeyHex(keyHex: string): BaseSigner;
static fromKeyHex(keyHex: string): BaseSigner;
/**
* Get the Ed25519 public key in hex format.
* @returns {Promise<string>} A promise that resolves to the public key in hex format.
*/
getPublicKey(): Promise<string>;
/**
* Get the Ed25519 public key hash in hex format.
* @returns {Promise<string>} A promise that resolves to the public key hash in hex format.
*/
getPublicKeyHash(): Promise<string>;
/**
* Sign data using the Ed25519 private key.
* @param data data to be signed in hex format
* @returns {Promise<string>} A promise that resolves to the signature in hex format.
*/
sign(data: string): Promise<string>;
/**
* Verify a signature using the Ed25519 public key.
* @param data The original data in hex format.
* @param signature The signature to verify in hex format.
* @returns {Promise<boolean>} A promise that resolves to true if the signature is valid, false otherwise.
*/
verify(data: string, signature: string): Promise<boolean>;
}
declare enum CredentialType {
KeyHash = 0,
ScriptHash = 1
}
type Credential = {
type: CredentialType;
hash: string;
};
declare enum AddressType {
Enterprise = 0,
Base = 1,
Reward = 2
}
declare class CardanoAddress {
addressType: AddressType;
networkId: number;
paymentCredential: Credential;
stakeCredential?: Credential;
constructor(addressType: AddressType, networkId: number, paymentCredential: Credential, stakeCredential?: Credential);
getAddressBech32(): string;
getAddressHex(): string;
private getEnterpriseAddressBech32;
private getEnterpriseAddressHex;
private getBaseAddressBech32;
private getBaseAddressHex;
private getRewardAddressBech32;
private getRewardAddressHex;
}
interface ICardanoWallet {
getExtensions(): Promise<{
cip: number;
}[]>;
getNetworkId(): Promise<number>;
getUtxos(): Promise<string[]>;
getCollateral(): Promise<string[]>;
getBalance(): Promise<string>;
getUsedAddresses(): Promise<string[]>;
getUnusedAddresses(): Promise<string[]>;
getRewardAddresses(): Promise<string[]>;
getChangeAddress(): Promise<string>;
signTx(data: string, partialSign: boolean): Promise<string>;
signData(addressHex: string, data: string): Promise<DataSignature>;
submitTx(tx: string): Promise<string>;
}
type Wallet = {
id: string;
name: string;
icon: string;
version: string;
};
type Cardano = {
[key: string]: {
name: string;
icon: string;
apiVersion: string;
enable: (extensions?: {
extensions: {
cip: number;
}[];
}) => Promise<ICardanoWallet>;
supportedExtensions?: {
cip: number;
}[];
};
};
type Extension = {
cip: number;
};
declare class CardanoBrowserWallet implements ICardanoWallet {
walletInstance: ICardanoWallet;
constructor(walletInstance: ICardanoWallet);
getExtensions(): Promise<{
cip: number;
}[]>;
getNetworkId(): Promise<number>;
getUtxos(): Promise<string[]>;
getCollateral(): Promise<string[]>;
getBalance(): Promise<string>;
getUsedAddresses(): Promise<string[]>;
getUnusedAddresses(): Promise<string[]>;
getRewardAddresses(): Promise<string[]>;
getChangeAddress(): Promise<string>;
signTx(data: string, partialSign: boolean): Promise<string>;
signData(addressBech32: string, data: string): Promise<DataSignature>;
submitTx(tx: string): Promise<string>;
/**
* Returns a list of wallets installed on user's device. Each wallet is an object with the following properties:
* - A name is provided to display wallet's name on the user interface.
* - A version is provided to display wallet's version on the user interface.
* - An icon is provided to display wallet's icon on the user interface.
*
* @returns a list of wallet names
*/
static getInstalledWallets(): Wallet[];
/**
* This is the entrypoint to start communication with the user's wallet. The wallet should request the user's permission to connect the web page to the user's wallet, and if permission has been granted, the wallet will be returned and exposing the full API for the app to use.
*
* Query BrowserWallet.getInstalledWallets() to get a list of available wallets, then provide the wallet name for which wallet the user would like to connect with.
*
* @param walletName - the name of the wallet to enable (e.g. "eternl", "begin")
* @param extensions - optional, a list of CIPs that the wallet should support
* @returns WalletInstance
*/
static enable(walletName: string, extensions?: Extension[]): Promise<ICardanoWallet>;
}
declare class MeshCardanoBrowserWallet extends CardanoBrowserWallet {
constructor(walletInstance: ICardanoWallet);
static enable(walletName: string, extensions?: Extension$1[]): Promise<MeshCardanoBrowserWallet>;
getUtxosMesh(): Promise<UTxO[]>;
getCollateralMesh(): Promise<UTxO[]>;
getBalanceMesh(): Promise<Asset[]>;
getUsedAddressesBech32(): Promise<string[]>;
getUnusedAddressesBech32(): Promise<string[]>;
getChangeAddressBech32(): Promise<string>;
getRewardAddressesBech32(): Promise<string[]>;
signTxReturnFullTx(tx: string, partialSign?: boolean): Promise<string>;
}
type CredentialSource = {
type: "signer";
signer: ISigner;
} | {
type: "scriptHash";
scriptHash: string;
};
interface AddressManagerConfig {
addressSource: AddressSource;
networkId: number;
}
type AddressSource = {
type: "secretManager";
secretManager: ISecretManager;
} | {
type: "credentials";
paymentCredential: CredentialSource;
stakeCredential?: CredentialSource;
drepCredential?: CredentialSource;
};
declare class AddressManager {
private readonly paymentCredential;
private readonly stakeCredential;
private readonly drepCredential;
private readonly paymentSigner;
private readonly stakeSigner?;
private readonly drepSigner?;
private readonly networkId;
static create(config: AddressManagerConfig): Promise<AddressManager>;
private constructor();
getNextAddress(addressType: AddressType): Promise<CardanoAddress>;
getChangeAddress(addressType: AddressType): Promise<CardanoAddress>;
getRewardAccount(): Promise<CardanoAddress>;
asyncGetAllUsedAddresses(): Promise<CardanoAddress[]>;
getCredentialsSigners(pubkeyHashes: Set<string>): Promise<Map<string, ISigner>>;
}
type WalletAddressType = AddressType.Base | AddressType.Enterprise;
/**
* Configuration for creating a CardanoHeadlessWallet instance.
*/
interface CardanoHeadlessWalletConfig {
/**
* The source for wallet's signing keys and addresses. Could either be a secret manager or explicit credentials.
*/
addressSource: AddressSource;
/**
* The network ID (0 for testnet, 1 for mainnet).
*/
networkId: number;
/**
* The type of wallet address to use (Base or Enterprise).
* Base addresses include staking capabilities, while Enterprise addresses do not.
*/
walletAddressType: WalletAddressType;
/**
* Optional fetcher instance for querying blockchain data (UTxOs, protocol parameters, etc.).
*/
fetcher?: IFetcher;
/**
* Optional submitter instance for submitting transactions to the blockchain.
*/
submitter?: ISubmitter;
}
declare class CardanoHeadlessWallet implements ICardanoWallet {
protected networkId: number;
protected addressManager: AddressManager;
protected fetcher?: IFetcher;
protected submitter?: ISubmitter;
protected walletAddressType: WalletAddressType;
protected constructor(networkId: number, addressManager: AddressManager, walletAddressType: WalletAddressType, fetcher?: IFetcher, submitter?: ISubmitter);
static create(config: CardanoHeadlessWalletConfig): Promise<CardanoHeadlessWallet>;
/**
* Create a CardanoHeadlessWallet instance from a Bip32 root in Bech32 format.
* @param config The configuration object
* @returns {Promise<CardanoHeadlessWallet>} A promise that resolves to a CardanoHeadlessWallet instance
*/
static fromBip32Root(config: Omit<CardanoHeadlessWalletConfig, "addressSource"> & {
bech32: string;
}): Promise<CardanoHeadlessWallet>;
/**
* Create a CardanoHeadlessWallet instance from a Bip32 root in hex format.
* @param config The configuration object
* @returns {Promise<CardanoHeadlessWallet>} A promise that resolves to a CardanoHeadlessWallet instance
*/
static fromBip32RootHex(config: Omit<CardanoHeadlessWalletConfig, "addressSource"> & {
hex: string;
}): Promise<CardanoHeadlessWallet>;
/**
* Create a CardanoHeadlessWallet instance from a mnemonic phrase.
* @param config The configuration object
* @returns {Promise<CardanoHeadlessWallet>} A promise that resolves to a CardanoHeadlessWallet instance
*/
static fromMnemonic(config: Omit<CardanoHeadlessWalletConfig, "addressSource"> & {
mnemonic: string[];
password?: string;
}): Promise<CardanoHeadlessWallet>;
static fromCredentialSources(config: Omit<CardanoHeadlessWalletConfig, "addressSource"> & {
paymentCredentialSource: CredentialSource;
stakeCredentialSource?: CredentialSource;
drepCredentialSource?: CredentialSource;
}): Promise<CardanoHeadlessWallet>;
/**
* Submit a transaction to the network, using the submitter instance.
* @param tx The transaction in CBOR hex format
* @returns {Promise<string>} A promise that resolves to the transaction ID
*/
submitTx(tx: string): Promise<string>;
/**
* Get the list of extensions enabled for this wallet.
* @returns {Promise<{ cip: number }[]>} A promise that resolves to an array of enabled extensions
*/
getExtensions(): Promise<{
cip: number;
}[]>;
/**
* Get the network ID.
* @returns {number} The network ID
*/
getNetworkId(): Promise<number>;
/**
* Get the UTxOs for the wallet.
*
* NOTE: This method is only an approximation to CIP-30 getUtxos, as this wallet is completely
* stateless and does not track which UTxOs are specifically set as collateral. Which means that there
* will be overlap between getUtxos() and getCollateral() results. This can result in the collateral being
* spent between transactions.
*
* The method also does not perform pagination, nor is there a coin selection mechanism.
* @returns {Promise<string[]>} A promise that resolves to an array of UTxOs in CBOR hex format
*/
getUtxos(): Promise<string[]>;
/**
* Get the collateral UTxOs for the wallet.
*
* NOTE: This method is only an approximation to CIP-30 getCollateral, as this wallet is completely
* stateless and does not track which UTxOs are specifically set as collateral. Which means that there
* will be overlap between getUtxos() and getCollateral() results.
*
* The basic strategy is to return the smallest pure ADA UTxO that is at least 5 ADA belonging to the wallet.
* @returns {Promise<string[]>} A promise that resolves to an array of UTxOs in CBOR hex format
*/
getCollateral(): Promise<string[]>;
/**
* Get the balance of the wallet.
*
* NOTE: This method is only an approximation to CIP-30 getBalance, as this wallet is completely
* stateless and does not track which UTxOs are specifically set as collateral. Which means the balance
* returned includes all UTxOs, including those that may be used as collateral.
* @returns {Promise<string>} A promise that resolves to the balance in CBOR hex format
*/
getBalance(): Promise<string>;
/**
* Get the used addresses for the wallet.
*
* NOTE: This method completely deviates from CIP-30 getUsedAddresses, as this wallet is stateless
* it is impossible to track which addresses have been used. This method simply returns the wallet's main address.
*
* It will be effective to be used as a single address wallet.
*
* @returns {Promise<string[]>} A promise that resolves to an array of used addresses in hex format
*/
getUsedAddresses(): Promise<string[]>;
/**
* Get the unused addresses for the wallet.
*
* NOTE: This method completely deviates from CIP-30 getUnusedAddresses, as this wallet is stateless
* it is impossible to track which addresses have been used. This method simply returns the wallet's main address.
*
* It will be effective to be used as a single address wallet.
*
* @returns {Promise<string[]>} A promise that resolves to an array of unused addresses in hex format
*/
getUnusedAddresses(): Promise<string[]>;
/**
* Get the change address for the wallet.
* NOTE: This method deviates from CIP-30 getChangeAddress, as this wallet is stateless
* it does not track which addresses has been previously used as change address. This method simply
* returns the wallet's main address.
*
* It will be effective to be used as a single address wallet.
*
* @returns {Promise<string>} A promise that resolves to the change address in hex format
*/
getChangeAddress(): Promise<string>;
/**
* Get the reward address for the wallet.
*
* @returns {Promise<string[]>} A promise that resolves an array of reward addresses in hex format
*/
getRewardAddresses(): Promise<string[]>;
/**
* Sign a transaction with the wallet.
*
* NOTE: This method requires a fetcher to resolve input UTxOs for determining required signers.
*
* It is also only an approximation to CIP-30 signTx, as this wallet is stateless and does not repeatedly
* derive keys, it is unable to sign for multiple derived key indexes.
*
* It will be effective to be used as a single address wallet.
*
* @param tx The transaction in CBOR hex format
* @returns A promise that resolves to a witness set with the signatures in CBOR hex format
*/
signTx(tx: string, partialSign?: boolean): Promise<string>;
signData(addressBech32: string, data: string): Promise<DataSignature>;
fetchAccountUtxos(): Promise<UTxO[]>;
}
/**
* MeshCardanoHeadlessWallet provides additional convenience methods on top of CardanoHeadlessWallet,
* such as returning results in Mesh-compatible formats and Bech32 addresses.
*/
declare class MeshCardanoHeadlessWallet extends CardanoHeadlessWallet {
static create(config: CardanoHeadlessWalletConfig): Promise<MeshCardanoHeadlessWallet>;
static fromMnemonic(config: Omit<CardanoHeadlessWalletConfig, "addressSource"> & {
mnemonic: string[];
password?: string;
}): Promise<MeshCardanoHeadlessWallet>;
static fromBip32Root(config: Omit<CardanoHeadlessWalletConfig, "addressSource"> & {
bech32: string;
}): Promise<MeshCardanoHeadlessWallet>;
static fromBip32RootHex(config: Omit<CardanoHeadlessWalletConfig, "addressSource"> & {
hex: string;
}): Promise<MeshCardanoHeadlessWallet>;
static fromCredentialSources(config: Omit<CardanoHeadlessWalletConfig, "addressSource"> & {
paymentCredentialSource: CredentialSource;
stakeCredentialSource?: CredentialSource;
drepCredentialSource?: CredentialSource;
}): Promise<MeshCardanoHeadlessWallet>;
/**
* Get the UTxOs for the wallet.
*
* NOTE: This method is only an approximation to CIP-30 getUtxos, as this wallet is completely
* stateless and does not track which UTxOs are specifically set as collateral. Which means that there
* will be overlap between getUtxos() and getCollateral() results. This can result in the collateral being
* spent between transactions.
*
* The method also does not perform pagination, nor is there a coin selection mechanism.
* @returns {Promise<UTxO[]>} A promise that resolves to an array of UTxOs in the Mesh UTxO format
*/
getUtxosMesh(): Promise<UTxO[]>;
/**
* Get the collateral UTxOs for the wallet.
*
* NOTE: This method is only an approximation to CIP-30 getCollateral, as this wallet is completely
* stateless and does not track which UTxOs are specifically set as collateral. Which means that there
* will be overlap between getUtxos() and getCollateral() results.
*
* The basic strategy is to return the smallest pure ADA UTxO that is at least 5 ADA belonging to the wallet.
* @returns {Promise<UTxO[]>} A promise that resolves to an array of UTxOs in the Mesh UTxO format
*/
getCollateralMesh(): Promise<UTxO[]>;
/**
* Get the balance of the wallet.
*
* NOTE: This method is only an approximation to CIP-30 getBalance, as this wallet is completely
* stateless and does not track which UTxOs are specifically set as collateral. Which means the balance
* returned includes all UTxOs, including those that may be used as collateral.
* @returns {Promise<Asset[]>} A promise that resolves to the balance in the Mesh Asset format
*/
getBalanceMesh(): Promise<Asset[]>;
/**
* Get the used addresses for the wallet.
*
* NOTE: This method completely deviates from CIP-30 getUsedAddresses, as this wallet is stateless
* it is impossible to track which addresses have been used. This method simply returns the wallet's main address.
*
* It will be effective to be used as a single address wallet.
*
* @returns {Promise<string[]>} A promise that resolves to an array of used addresses in Bech32 format
*/
getUsedAddressesBech32(): Promise<string[]>;
/**
* Get the unused addresses for the wallet.
*
* NOTE: This method completely deviates from CIP-30 getUnusedAddresses, as this wallet is stateless
* it is impossible to track which addresses have been used. This method simply returns the wallet's main address.
*
* It will be effective to be used as a single address wallet.
*
* @returns {Promise<string[]>} A promise that resolves to an array of unused addresses in Bech32 format
*/
getUnusedAddressesBech32(): Promise<string[]>;
/**
* Get the change address for the wallet.
* NOTE: This method deviates from CIP-30 getChangeAddress, as this wallet is stateless
* it does not track which addresses has been previously used as change address. This method simply
* returns the wallet's main address.
*
* It will be effective to be used as a single address wallet.
*
* @returns {Promise<string>} A promise that resolves to the change address in Bech32 format
*/
getChangeAddressBech32(): Promise<string>;
/**
* Get the reward address for the wallet.
* @returns {Promise<string[]>} A promise that resolves an array of reward addresses in Bech32 format
*/
getRewardAddressesBech32(): Promise<string[]>;
/**
* Sign a transaction with the wallet.
*
* NOTE: This method requires a fetcher to resolve input UTxOs for determining required signers.
*
* It is also only an approximation to CIP-30 signTx, as this wallet is stateless and does not repeatedly
* derive keys, it is unable to sign for multiple derived key indexes.
*
* It will be effective to be used as a single address wallet.
*
* @param tx The transaction in CBOR hex format
* @returns A promise that resolves to a full transaction with extra vkey witnesses added from the wallet
* to the witness set in CBOR hex format
*/
signTxReturnFullTx(tx: string, partialSign?: boolean): Promise<string>;
}
export { AddressType, BaseSigner, type Cardano, CardanoAddress, CardanoBrowserWallet, CardanoHeadlessWallet, type CardanoHeadlessWalletConfig, type Credential, type CredentialSource, CredentialType, type ICardanoWallet, InMemoryBip32, MeshCardanoBrowserWallet, MeshCardanoHeadlessWallet, type WalletAddressType };