Skip to main content

流程

iframe​

新增设备​

创建嵌入钱包​

生成私钥​

签名交易​

发交易​

其他 api 同 viem​

example​

alt text

Embedded Wallet Provider

This package provides an EmbeddedWalletProvider class to manage blockchain interactions through an embedded wallet within an iframe. It supports general transactions, blob transactions, message signing, and chain management.

Table of Contents​

Installation​

To use this package, you'll need to install the required dependencies:

npm install viem @next-wallet/provider

Usage​

To use the EmbeddedWalletProvider, you need to import it and initialize it with the required parameters.

import { defineChain, http } from 'viem';
import { sepolia } from 'viem/chains';
import { EmbeddedWalletProvider } from '@next-wallet/provider';

const defaultChain = defineChain({
id: 1,
name: 'Ethereum Mainnet',
rpcUrls: ['https://mainnet.infura.io/v3/YOUR_INFURA_PROJECT_ID'],
});

const params: EmbeddedWalletProviderParams = {
defaultChain: sepolia,
supportedChains: [sepolia],
address: '0xYourWalletAddress', // @next-wallet/provider 创建的钱包地址
iframeSrc: 'https://your-iframe-source.com', // your iframe
onReady: (res) => {
console.log('Iframe is ready', res);
},
};

const walletProvider = new EmbeddedWalletProvider(params);

API​

Constructor​

EmbeddedWalletProvider(parameters: EmbeddedWalletProviderParams)​

  • parameters: An object containing the following properties:
    • defaultChain: The default blockchain chain.
    • supportedChains: An array of supported blockchain chains.
    • address: The address of the embedded wallet.
    • iframeSrc: The source URL of the embedded wallet iframe.
    • transport: (Optional) The transport method for blockchain communication.
    • onReady: A callback function that is called when the iframe is ready.

Methods​

createWallet(): Promise<ResponsePayload>​

Creates a new wallet.

exportDeviceShare(address: Hex): Promise<ResponsePayload>​

Exports the device share for the given address.

exportPrivateKey(address: Hex): Promise<ResponsePayload>​

Exports the private key for the given address.

switchAddress(address: Hex): void​

Switches the current address to the provided address.

getAllAddress(): Promise<ResponsePayload>​

Gets all addresses associated with the wallet.

generateClient(): Promise<void>​

Generates a public and wallet client.

signTransaction(transaction: GeneralTransaction | BlobTransaction): Promise<Hex>​

Signs a transaction.

sendTransaction(transaction: GeneralTransaction | BlobTransaction): Promise<Hex>​

Sends a signed transaction.

sendRawTransaction(args: SendRawTransactionParameters): Promise<Hex>​

Sends a raw transaction.

waitForTransactionReceipt(args: WaitForTransactionReceiptParameters): Promise<TransactionReceipt>​

Waits for the transaction receipt of the given transaction hash.

signMessage(message: string): Promise<Hex>​

Signs a message.

signTypedData(typedData: TypedDataParameter): Promise<Hex>​

Signs typed data (EIP-712).

sendBlobDataTransaction(): Promise<void>​

(Not implemented) Sends a blob data transaction.

addChain(chain: Chain): Promise<void>​

Adds a new chain to the supported chains.

switchChain(id: Chain['id']): Promise<void>​

Switches to the chain with the given ID.

getChains(): Promise<Chain[]>​

Gets all supported chains.

Example​

Here is an example of how to use the EmbeddedWalletProvider to sign and send a transaction:

import { defineChain, http } from 'viem';
import { EmbeddedWalletProvider, EmbeddedWalletProviderParams } from './path-to-your-provider-file';

const defaultChain = defineChain({
id: 1,
name: 'Ethereum Mainnet',
rpcUrls: ['https://mainnet.infura.io/v3/YOUR_INFURA_PROJECT_ID'],
});

const params: EmbeddedWalletProviderParams = {
defaultChain,
supportedChains: [defaultChain],
address: '0xYourWalletAddress',
iframeSrc: 'https://your-iframe-source.com',
onReady: (res) => {
console.log('Iframe is ready', res);
},
};

const walletProvider = new EmbeddedWalletProvider(params);

async function sendTransaction() {
const transaction = {
maxFeePerGas: BigInt(20000000000),
maxPriorityFeePerGas: BigInt(2000000000),
gas: 21000,
to: '0xRecipientAddress',
value: BigInt(1000000000000000000), // 1 ETH
};

try {
const signedTransaction = await walletProvider.signTransaction(transaction);
const txHash = await walletProvider.sendTransaction(signedTransaction);
console.log('Transaction sent with hash:', txHash);
} catch (error) {
console.error('Error sending transaction:', error);
}
}

sendTransaction();

This example demonstrates how to initialize the EmbeddedWalletProvider, sign a transaction, and send it to the blockchain.