创建钱包
Stable 钱包是符合以太坊标准的密钥对。任何生成 EVM 账户的钱包库都可以在 Stable 上运行,无需修改。本指南介绍了两种方法:对于大多数应用程序,可以使用 ethers.js;对于需要为代理和支付提供一站式自托管层的集成,可以使用 Tether 的 WDK(钱包开发工具包)。
前提条件
- Node.js 20 或更高版本。
选项 1: ethers.js
安装库并生成密钥对。
npm install ethers// wallet.ts
import { ethers } from "ethers";
const provider = new ethers.JsonRpcProvider("https://rpc.testnet.stable.xyz");
/** 为新用户创建新钱包。 */
export function createWallet() {
const wallet = ethers.Wallet.createRandom(provider);
return {
wallet,
address: wallet.address,
seedPhrase: wallet.mnemonic!.phrase, // 一次性向用户显示以便备份
};
}
/** 从助记词恢复钱包(回访用户)。 */
export function restoreWallet(seedPhrase: string) {
const wallet = ethers.Wallet.fromPhrase(seedPhrase, provider);
return { wallet, address: wallet.address };
}
if (import.meta.url === `file://${process.argv[1]}`) {
const { address, seedPhrase } = createWallet();
console.log("Address: ", address);
console.log("Seed phrase:", seedPhrase);
}npx tsx wallet.tsAddress: 0xAlice...1234
Seed phrase: liberty shoot ... (12 words)选项 2: Tether WDK
WDK 将密钥派生、签名和交易提交包装在一个接口中。当你希望自托管而无需重新实现常见的账户流时,这是一个正确的选择,并且它直接与 x402 集成,用于代理支付。
npm install @tetherto/wdk @tetherto/wdk-wallet-evm// wallet-wdk.ts
import WDK from "@tetherto/wdk";
import WalletManagerEvm from "@tetherto/wdk-wallet-evm";
function initWdk(seedPhrase: string) {
return new WDK(seedPhrase)
.registerWallet("stable", WalletManagerEvm, {
provider: "https://rpc.testnet.stable.xyz",
});
}
/** 为新用户创建新钱包。 */
export async function createWallet() {
const seedPhrase = WDK.getRandomSeedPhrase();
const wdk = initWdk(seedPhrase);
const account = await wdk.getAccount("stable", 0);
return {
account,
address: await account.getAddress(),
seedPhrase, // 一次性向用户显示以便备份
};
}
/** 从助记词恢复钱包(回访用户)。 */
export async function restoreWallet(seedPhrase: string) {
const wdk = initWdk(seedPhrase);
const account = await wdk.getAccount("stable", 0);
return { account, address: await account.getAddress() };
}npx tsx wallet-wdk.tsAddress: 0xAlice...1234
Seed phrase: liberty shoot ... (12 words)充值钱包
在钱包可以进行交易之前,它需要 USDT0 作为燃料费。在测试网上,从水龙头请求:
open https://faucet.stable.xyz粘贴地址并选择按钮以接收 1 个测试网 USDT0(足以用于数千次原生转账)。对于主网,从任何受支持的交易所或网桥发送 USDT0;请参阅将 USDT0 桥接到 Stable。
检查余额
原生 USDT0 使用 18 位小数。原生余额是支付燃料费的余额。
// balance.ts
import { ethers } from "ethers";
const provider = new ethers.JsonRpcProvider("https://rpc.testnet.stable.xyz");
const balance = await provider.getBalance("0xYourAddress");
console.log("Balance:", ethers.formatEther(balance), "USDT0");npx tsx balance.tsBalance: 1.0 USDT0接下来推荐
- 使用 EIP-7702 进行委托:为这个钱包添加批量支付、支出限额和会话密钥。
- 发送您的第一个 USDT0:在同一余额上进行原生和 ERC-20 转账。
- 为测试网钱包充值:水龙头和 Sepolia 网桥选项,用于更大的测试余额。

