Skip to content
🎉 Welcome to the new Aptos Docs! Click here to submit an issue.

创建和管理账户

使用 TypeScript SDK 有几种方式可以生成账户凭证。你可以使用:

  • Account.generate()
  • Account.fromPrivateKey()
  • Account.fromDerivationPath()

Account.generate() 是最常用的为新账户创建密钥的方法。 它默认使用 ED25519 密钥编码,但你也可以手动指定偏好的签名方案:

const account = Account.generate(); // 默认为 Legacy Ed25519
const account = Account.generate({ scheme: SigningSchemeInput.Secp256k1Ecdsa }); // Single Sender Secp256k1
const account = Account.generate({
  scheme: SigningSchemeInput.Ed25519,
  legacy: false,
}); // Single Sender Ed25519

根据 AIP-55,SDK 支持 LegacyUnified 认证方式。Legacy 包含 ED25519MultiED25519,而 Unified 包含 SingleSenderMultiSender 认证器。

生成凭证后,你必须为其注资,网络才能识别该账户的存在。

在本地网络/开发网络中,可以通过运行以下命令使用 faucet 完成:

fund.ts
const transaction = await aptos.fundAccount({
  accountAddress: account.accountAddress,
  amount: 100,
});

对于测试网络,你可以使用此处的铸币页面here

其他表示账户的方式

如果你拥有私钥或等效表示形式,可以使用它们创建 Account 对象,在使用 TypeScript SDK 时管理这些凭证。

以下是几个展示如何使用特定编码方案实现的示例。

从私钥派生账户

SDK 支持使用 fromPrivateKey() 静态方法从私钥派生账户。 此外,该方法还支持从私钥和账户地址派生账户。 该方法使用本地计算,因此用于派生尚未进行认证密钥轮换的 Account

// 使用 legacy Ed25519 密钥方案派生账户
const privateKey = new Ed25519PrivateKey(privateKeyBytes);
const account = Account.fromPrivateKey({ privateKey });
 
// 使用 Single Sender Ed25519 密钥方案派生账户
const privateKey = new Ed25519PrivateKey(privateKeyBytes);
const account = Account.fromPrivateKey({ privateKey, legacy: false });
 
// 使用 Single Sender Secp256k1 密钥方案派生账户
const privateKey = new Secp256k1PrivateKey(privateKeyBytes);
const account = Account.fromPrivateKey({ privateKey });
 
// 使用私钥和账户地址派生账户
const privateKey = new Ed25519PrivateKey(privateKeyBytes);
const address = AccountAddress.from(address);
const account = Account.fromPrivateKey({ privateKey, address });

从派生路径派生账户

SDK 支持使用 fromDerivationPath() 静态方法从派生路径派生账户。

// 使用传统的 Ed25519 密钥方案派生账户
const { mnemonic, address, path } = wallet;
const account = Account.fromDerivationPath({
  path,
  mnemonic,
  scheme: SigningSchemeInput.Ed25519,
});
 
// 使用 Single Sender Ed25519 密钥方案派生账户
const { mnemonic, address, path } = wallet;
const account = Account.fromDerivationPath({
  path,
  mnemonic,
  scheme: SigningSchemeInput.Ed25519,
  legacy: false,
});// 派生一个使用 Single Sender Secp256k1 密钥方案的账户
const { mnemonic, address, path } = wallet;
const account = Account.fromDerivationPath({
  path,
  mnemonic,
  scheme: SigningSchemeInput.Secp256k1Ecdsa,
});