生成账户 (Generating accounts)
表示账户 (Representing accounts)
使用 Namada SDK 表示账户非常简单。Namada 上的一个账户由其公钥和私钥定义(复数用于多重签名)。公钥用于标识账户,而私钥用于签署交易。在下面的代码片段中,我们使用公钥和私钥表示账户。
use namada_sdk::core::types::key::common::{PublicKey, SecretKey};
struct SimpleAccount {
public_key: PublicKey,
private_key: SecretKey
}
对于多重签名账户,我们可以通过密钥的向量来表示它。
use namada_sdk::core::types::key::common::{PublicKey, SecretKey};
struct MultisigAccount {
public_keys: Vec<PublicKey>,
private_keys: Vec<SecretKey>
}
多重签名账户,因为它们是通过链上交易初始化的,总是会向账本公开它们的公钥。但是,当密钥对以离线方式生成时,用户必须提交交易以公开其公钥。因此,在账户结构中添加字段 revealed
是很有帮助的。
use namada_sdk::core::types::key::common::{PublicKey, SecretKey};
struct Account {
public_key: PublicKey,
private_key: SecretKey,
revealed: bool
}
揭示隐式账户的公钥 (Revealing the public key of an implicit account)
为了揭示隐式账户的公钥,用户必须向账本提交交易。
use namada_sdk::io::NullIo;
use namada_sdk::NamadaImpl;
use namada_sdk::core::types::chain::ChainId;
// Define the namada implementation (assuming we have a wallet, http_client, and shielded_ctx)
let mut namada = NamadaImpl::new(&http_client, &mut wallet, &mut shielded_ctx, &NullIo)
.await
.expect("unable to construct Namada object")
.chain_id(ChainId::from_str("public-testnet-14.5d79b6958580").unwrap());
// Generate an account (assuming sk is a SecretKey)
let account = Account {
public_key: sk.to_public(),
private_key: sk,
revealed: false,
};
// Build the reveal pk transaction using the NamadaImpl object
let reveal_tx_builder = namada
.new_reveal_pk(account.public_key.clone())
.signing_keys(vec![account.private_key.clone()]);
let (mut reveal_tx, signing_data, _) = reveal_tx_builder
.build(namada)
.await
.expect("unable to build reveal pk tx");
// Sign the transaction
namada
.sign(&mut reveal_tx, &reveal_tx_builder.tx, signing_data)
.await
.expect("unable to sign reveal pk tx");
// Submit the signed tx to the ledger for execution
namada.submit(reveal_tx.clone(), reveal_tx_builder)
一旦公钥被揭示,该账户就可以用于签署交易。
Last updated