Go SDK:创建和管理账户
Go SDK 提供多种生成账户凭据的方式,可使用:
aptos.NewEd25519Account()aptos.NewSecp256k1Account()aptos.NewEd25519SingleSenderAccount()aptos.NewAccountFromSigner()aptos.NewEd25519AccountFromMnemonic()aptos.NewEd25519AccountFromDerivationPath()
NewEd25519Account() 是最常用于为新账户创建密钥的方法。它默认使用 ED25519 密钥类型,但也可按以下方式指定所需的签名方案:
// To derive an ed25519 accountaccount1, err := aptos.NewEd25519Account()if err != nil { panic("Failed to create account:" + err.Error())}
// To derive a secp256k1 accountaccount2, err := aptos.NewSecp256k1Account()if err != nil { panic("Failed to create account:" + err.Error())}生成凭据后,必须为账户注资,网络才能识别其存在。
在 Devnet 环境中,可通过 faucet 使用以下命令完成:
client, err := aptos.NewClient(aptos.DevnetConfig)if err != nil { panic("Failed to create client:" + err.Error())}
// Fund an account with 1 Devnet APTerr = client.Fund(account1.Address, 100_000_000)if err != nil { panic("Failed to fund account:" + err.Error())}在 Testnet 上,可在铸造页面领取代币。
表示账户的其他方式
Section titled “表示账户的其他方式”如果拥有私钥或等效表示,可以用它创建 Account 结构体,并在使用 Go SDK 时管理这些凭据。
以下示例展示如何通过特定编码方案实现此目的。
从私钥派生账户
Section titled “从私钥派生账户”SDK 支持使用 NewAccountFromSigner() 方法从私钥派生账户。
此外,该方法支持根据私钥和账户地址派生账户。
此方法使用本地计算,因此用于派生尚未轮换认证密钥的 Account。
// to derive an account with a Ed25519 key schemeprivateKey := &aptos.Ed25519PrivateKey{}err := privateKey.FromHex(privateKeyHex)if err != nil { panic("Failed to parse private key:" + err.Error())}account, err := aptos.NewAccountFromSigner(privateKey)if err != nil { panic("Failed to derive account:" + err.Error())}
// to derive an account with a Single Sender Ed25519 key schemeprivateKey := &aptos.Ed25519PrivateKey{}err := privateKey.FromHex(privateKeyHex)if err != nil { panic("Failed to parse private key:" + err.Error())}singleSigner := &crypto.SingleSigner{Signer: privateKey}account, err = aptos.NewAccountFromSigner(singleSigner)if err != nil { panic("Failed to derive account:" + err.Error())}
// to derive an account with a Single Sender Secp256k1 key schemeprivateKey := &aptos.Secp256k1PrivateKey{}err := privateKey.FromHex(privateKeyHex)if err != nil { panic("Failed to parse private key:" + err.Error())}singleSigner := &crypto.SingleSigner{Signer: privateKey}account, err = aptos.NewAccountFromSigner(singleSigner)if err != nil { panic("Failed to derive account:" + err.Error())}
// to derive an account with a private key and account addressvar address aptos.AccountAddresserr = address.ParseStringRelaxed(addressHex)if err != nil { panic("Failed to parse address:" + err.Error())}privateKey := &aptos.Ed25519PrivateKey{}err = privateKey.FromHex(privateKeyHex)if err != nil { panic("Failed to parse private key:" + err.Error())}account, err = aptos.NewAccountFromSigner(privateKey, address)if err != nil { panic("Failed to derive account:" + err.Error())}从助记词派生账户
Section titled “从助记词派生账户”account, err := aptos.NewEd25519AccountFromMnemonic(mnemonic)if err != nil { panic("Failed to derive account:" + err.Error())}
account, err = aptos.NewEd25519AccountFromDerivationPath(mnemonic, "m/44'/637'/0'/0'/0'")if err != nil { panic("Failed to derive account:" + err.Error())}