Generating accounts

Representing accounts

Representing accounts using the Namada SDK is straightforward. An account on Namada is defined by its public key(s) and private key(s) (plural for multi-signature accounts). The public key(s) is/are used to identify the account and the private key is used to sign transactions. In the below snippet, we create a struct to represent the account using the public key and private key.

use namada_sdk::key::common::{PublicKey, SecretKey};
struct SimpleAccount {
  public_key: PublicKey,
  private_key: SecretKey
}

For a multisignature account, we can represent this through a vector of keys.

use namada_sdk::key::common::{PublicKey, SecretKey};
struct MultisigAccount {
  public_keys: Vec<PublicKey>,
  private_keys: Vec<SecretKey>
}

Multisignature accounts, because they are initialized by an on-chain transaction, will always have their public key revealed to the ledger. However, when keypairs are generated in an offline fashion, the user must submit a transaction in order to reveal their public key. Because of this, it is helpful to add the field revealed to the account struct.

use namada_sdk::key::common::{PublicKey, SecretKey};
struct SimpleAccount {
  public_key: PublicKey,
  private_key: SecretKey,
  revealed: bool,
}

Revealing the public key of an implicit account

In order to reveal the public key of an implicit account, the user must submit a transaction to the ledger.

use namada_sdk::signing::default_sign;
 
// Build the reveal pk transaction using the sdk object
let reveal_tx_builder = sdk
  .new_reveal_pk(account.public_key.clone())
  .signing_keys(vec![account.public_key.clone()]);
let (mut reveal_tx, signing_data) = reveal_tx_builder
  .build(&sdk)
  .await
  .expect("unable to build reveal pk tx");
 
// Sign the transaction
sdk.sign(&mut reveal_tx, &reveal_tx_builder.tx, signing_data, default_sign, ())
  .await
  .expect("unable to sign reveal pk tx");
 
// Submit the signed tx to the ledger for execution
// Assumes account already has funds to cover gas costs
match sdk.submit(reveal_tx.clone(), &reveal_tx_builder.tx).await {
  Ok(res) => println!("Tx result: {:?}", res),
  Err(e) => println!("Tx error: {:?}", e) 
}

Once the public key is revealed, the account can be used to sign transactions.