创建钱包
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 作为 gas。在测试网上,可以从水龙头请求:
open https://faucet.stable.xyz粘贴地址并选择按钮接收 1 个测试网 USDT0(足以进行数千次原生转账)。对于主网,从任何支持的交易所或桥接发送 USDT0;请参阅将 USDT0 桥接到 Stable。
检查余额
原生 USDT0 使用 18 位小数。原生余额是支付 gas 的余额。
// 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 桥接选项。

