Account
Overview
Key Management class. Enables the creation of a new Aleo Account, importation of an existing account from an existing private key or seed, and message signing and verification functionality.
An Aleo Account is generated from a randomly generated seed (number) from which an account private key, view key, and a public account address are derived. The private key lies at the root of an Aleo account. It is a highly sensitive secret and should be protected as it allows for creation of Aleo Program executions and arbitrary value transfers. The View Key allows for decryption of a user's activity on the blockchain. The Address is the public address to which other users of Aleo can send Aleo credits and other records to. This class should only be used in environments where the safety of the underlying key material can be assured.
Kind: global class
- Account
- constructor
- instance
- .privateKey() ⇒
PrivateKey - .viewKey() ⇒
ViewKey - .computeKey() ⇒
ComputeKey - .address() ⇒
Address - .clone() ⇒
Account - .toString() ⇒
string - .encryptAccount(password) ⇒
PrivateKeyCiphertext - .decryptRecord(ciphertext) ⇒
RecordPlaintext - .decryptRecords(ciphertexts) ⇒
Array.<RecordPlaintext> - .generateRecordViewKey(recordCiphertext) ⇒
Field - .generateTransitionViewKey(tpk) ⇒
Field - .ownsRecordCiphertext(ciphertext) ⇒
boolean - .sign(message) ⇒
Signature - .verify(message, signature) ⇒
boolean
- .privateKey() ⇒
- static
- .fromCiphertext(ciphertext, password) ⇒
Account|Error
- .fromCiphertext(ciphertext, password) ⇒
Example
import { Account } from "@provablehq/sdk/testnet.js";
// Create a new account
const myRandomAccount = new Account();
// Create an account from a randomly generated seed
const seed = new Uint8Array([94, 91, 52, 251, 240, 230, 226, 35, 117, 253, 224, 210, 175, 13, 205, 120, 155, 214, 7, 169, 66, 62, 206, 50, 188, 40, 29, 122, 40, 250, 54, 18]);
const mySeededAccount = new Account({seed: seed});
// Create an account from an existing private key
const myExistingAccount = new Account({privateKey: process.env.privateKey});
// Sign a message
const hello_world = Uint8Array.from([104, 101, 108, 108, 111, 119, 111, 114, 108, 100]);
const signature = myRandomAccount.sign(hello_world);
// Verify a signature
assert(myRandomAccount.verify(hello_world, signature));
Constructor
Account
Create a new Aleo Account from a random seed, an existing private key, or a provided seed array.
new Account(params)
| Param | Type | Description |
|---|---|---|
| params | AccountParam | Optional parameters for account creation |
Interface
interface AccountParam {
privateKey?: string;
seed?: Uint8Array;
}
| Property | Type | Description |
|---|---|---|
| privateKey | string | Optional private key string to create account from |
| seed | Uint8Array | Optional seed array to create account from |
Example
import { Account } from "@provablehq/sdk/testnet.js";
// Create a new account with random seed
const myRandomAccount = new Account();
// Create an account from a randomly generated seed
const seed = new Uint8Array([94, 91, 52, 251, 240, 230, 226, 35, 117, 253, 224, 210, 175, 13, 205, 120, 155, 214, 7, 169, 66, 62, 206, 50, 188, 40, 29, 122, 40, 250, 54, 18]);
const mySeededAccount = new Account({seed: seed});
// Create an account from an existing private key
const myExistingAccount = new Account({privateKey: 'APrivateKey1zkp...'});
Methods
privateKey
Returns the PrivateKey associated with the account.
account.privateKey() ⇒ PrivateKey
Kind: instance method of Account
| Param | Type | Description |
|---|---|---|
| return | PrivateKey | The account's private key object |
Example
import { Account } from "@provablehq/sdk/testnet.js";
const account = new Account();
const privateKey = account.privateKey();
viewKey
Returns the ViewKey associated with the account.
account.viewKey() ⇒ ViewKey
Kind: instance method of Account
| Param | Type | Description |
|---|---|---|
| return | ViewKey | The account's view key for decrypting records |
Example
import { Account } from "@provablehq/sdk/testnet.js";
const account = new Account();
const viewKey = account.viewKey();
computeKey
Returns the ComputeKey associated with the account.
account.computeKey() ⇒ ComputeKey
Kind: instance method of Account
| Param | Type | Description |
|---|---|---|
| return | ComputeKey | The account's compute key for program execution |
Example
import { Account } from "@provablehq/sdk/testnet.js";
const account = new Account();
const computeKey = account.computeKey();
address
Returns the Aleo address associated with the account.
account.address() ⇒ Address
Kind: instance method of Account
| Param | Type | Description |
|---|---|---|
| return | Address | The account's public address |
Example
import { Account } from "@provablehq/sdk/testnet.js";
const account = new Account();
const address = account.address();
clone
Deep clones the Account.
account.clone() ⇒ Account
Kind: instance method of Account
| Param | Type | Description |
|---|---|---|
| return | Account | A deep copy of the account |
Example
import { Account } from "@provablehq/sdk/testnet.js";
const account = new Account();
const clonedAccount = account.clone();
toString
Returns the address of the account in a string representation.
account.toString() ⇒ string
Kind: instance method of Account
| Param | Type | Description |
|---|---|---|
| return | string | The account's address as a string |
encryptAccount
Encrypts the account's private key with a password.
account.encryptAccount(password) ⇒ PrivateKeyCiphertext
Kind: instance method of Account
| Param | Type | Description |
|---|---|---|
| password | string | Password to encrypt the private key |
| return | PrivateKeyCiphertext | The encrypted private key ciphertext |
Example
import { Account } from "@provablehq/sdk/testnet.js";
const account = new Account();
const ciphertext = account.encryptAccount("password");
process.env.ciphertext = ciphertext.toString();
decryptRecord
Decrypts an encrypted record string into a plaintext record object.
account.decryptRecord(ciphertext) ⇒ RecordPlaintext
Kind: instance method of Account
| Param | Type | Description |
|---|---|---|
| ciphertext | string | A string representing the ciphertext of a record |
| return | RecordPlaintext | The decrypted record plaintext |
Example
// Import the AleoNetworkClient and Account classes
import { AleoNetworkClient, Account } from "@provablehq/sdk/testnet.js";
// Create a connection to the Aleo network and an account
const networkClient = new AleoNetworkClient("https://api.explorer.provable.com/v1");
const account = Account.fromCiphertext(process.env.ciphertext, process.env.password);
// Get the record ciphertexts from a transaction.
const transaction = await networkClient.getTransactionObject("at1fjy6s9md2v4rgcn3j3q4qndtfaa2zvg58a4uha0rujvrn4cumu9qfazxdd");
const records = transaction.records();
// Decrypt any records the account owns.
const decryptedRecords = [];
for (const record of records) {
if (account.decryptRecord(record)) {
decryptedRecords.push(record);
}
}
decryptRecords
Decrypts an array of Record ciphertext strings into an array of record plaintext objects.
account.decryptRecords(ciphertexts) ⇒ Array.<RecordPlaintext>
Kind: instance method of Account
| Param | Type | Description |
|---|---|---|
| ciphertexts | Array.<string> | An array of strings representing the ciphertexts of records |
| return | Array.<RecordPlaintext> | An array of decrypted record plaintexts |
Example
// Import the AleoNetworkClient and Account classes
import { AleoNetworkClient, Account } from "@provablehq/sdk/testnet.js";
// Create a connection to the Aleo network and an account
const networkClient = new AleoNetworkClient("https://api.explorer.provable.com/v1");
const account = Account.fromCiphertext(process.env.ciphertext, process.env.password);
// Get the record ciphertexts from a transaction.
const transaction = await networkClient.getTransactionObject("at1fjy6s9md2v4rgcn3j3q4qndtfaa2zvg58a4uha0rujvrn4cumu9qfazxdd");
const records = transaction.records();
// Decrypt any records the account owns. If the account owns no records, the array will be empty.
const decryptedRecords = account.decryptRecords(records);
generateRecordViewKey
Generates a record view key from the account owner's view key and the record ciphertext. This key can be used to decrypt the record without revealing the account's view key.
account.generateRecordViewKey(recordCiphertext) ⇒ Field
Kind: instance method of Account
| Param | Type | Description |
|---|---|---|
| recordCiphertext | RecordCiphertext | string | The record ciphertext to generate the view key for |
| return | Field | The record view key |
Example
// Import the Account class
import { Account, RecordCiphertext } from "@provablehq/sdk/testnet.js";
// Create an account object from a previously encrypted ciphertext and password.
const account = Account.fromCiphertext(process.env.ciphertext, process.env.password);
// Generate a record view key from the account's view key and a record ciphertext
const recordCiphertext = RecordCiphertext.fromString("your_record_ciphertext_here");
const recordViewKey = account.generateRecordViewKey(recordCiphertext);
generateTransitionViewKey
Generates a transition view key from the account owner's view key and the transition public key. This key can be used to decrypt the private inputs and outputs of a transition without revealing the account's view key.
account.generateTransitionViewKey(tpk) ⇒ Field
Kind: instance method of Account
| Param | Type | Description |
|---|---|---|
| tpk | string | Group | The transition public key |
| return | Field | The transition view key |
Example
// Import the Account class
import { Account, Group } from "@provablehq/sdk/testnet.js";
// Generate a transition view key from the account's view key and a transition public key
const tpk = Group.fromString("your_transition_public_key_here");
const transitionViewKey = account.generateTransitionViewKey(tpk);
ownsRecordCiphertext
Determines whether the account owns a ciphertext record.
account.ownsRecordCiphertext(ciphertext) ⇒ boolean
Kind: instance method of Account
| Param | Type | Description |
|---|---|---|
| ciphertext | RecordCiphertext | string | The record ciphertext to check ownership of |
| return | boolean | True if the account owns the record, false otherwise |
Example
// Import the AleoNetworkClient and Account classes
import { AleoNetworkClient, Account } from "@provablehq/sdk/testnet.js";
// Create a connection to the Aleo network and an account
const networkClient = new AleoNetworkClient("https://api.explorer.provable.com/v1");
const account = Account.fromCiphertext(process.env.ciphertext, process.env.password);
// Get the record ciphertexts from a transaction and check ownership of them.
const transaction = await networkClient.getTransactionObject("at1fjy6s9md2v4rgcn3j3q4qndtfaa2zvg58a4uha0rujvrn4cumu9qfazxdd");
const records = transaction.records();
// Check if the account owns any of the record ciphertexts present in the transaction.
const ownedRecords = [];
for (const record of records) {
if (account.ownsRecordCiphertext(record)) {
ownedRecords.push(record);
}
}
sign
Signs a message with the account's private key. Returns a Signature.
account.sign(message) ⇒ Signature
Kind: instance method of Account
| Param | Type | Description |
|---|---|---|
| message | Uint8Array | Message to be signed |
| return | Signature | Signature over the message in bytes |
Example
// Import the Account class
import { Account } from "@provablehq/sdk/testnet.js";
// Create an account and a message to sign.
const account = new Account();
const message = Uint8Array.from([104, 101, 108, 108, 111, 119, 111, 114, 108, 100]);
const signature = account.sign(message);
// Verify the signature.
assert(account.verify(message, signature));
verify
Verifies the Signature on a message.
account.verify(message, signature) ⇒ boolean
Kind: instance method of Account
| Param | Type | Description |
|---|---|---|
| message | Uint8Array | Message in bytes to be signed |
| signature | Signature | Signature to be verified |
| return | boolean | True if the signature is valid, false otherwise |
Example
// Import the Account class
import { Account } from "@provablehq/sdk/testnet.js";
// Sign a message.
const account = new Account();
const message = Uint8Array.from([104, 101, 108, 108, 111, 119, 111, 114, 108, 100]);
const signature = account.sign(message);
// Verify the signature.
assert(account.verify(message, signature));
fromCiphertext
Attempts to create an account from a private key ciphertext
Account.fromCiphertext(ciphertext, password) ⇒ Account | Error
Kind: static method of Account
| Param | Type | Description |
|---|---|---|
| ciphertext | PrivateKeyCiphertext | string | The encrypted private key ciphertext or its string representation |
| password | string | The password used to decrypt the private key ciphertext |
| return | Account | Error | A new Account instance created from the decrypted private key |
Example
import { Account } from "@provablehq/sdk/testnet.js";
// Create an account object from a previously encrypted ciphertext and password.
const account = Account.fromCiphertext(process.env.ciphertext, process.env.password);