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
- instance
- .encryptAccount(ciphertext) ⇒
PrivateKeyCiphertext
- .decryptRecord(ciphertext) ⇒
Record
- .decryptRecords(ciphertexts) ⇒
Array.<Record>
- .ownsRecordCiphertext(ciphertext) ⇒
boolean
- .sign(message) ⇒
Signature
- .verify(message, signature) ⇒
boolean
- .encryptAccount(ciphertext) ⇒
- static
- .fromCiphertext(ciphertext, password) ⇒
PrivateKey
|Error
- .fromCiphertext(ciphertext, password) ⇒
- instance
Example
// 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: 'myExistingPrivateKey'})
// 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
myRandomAccount.verify(hello_world, signature)
Constructor
Account
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
// 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
encryptAccount
Encrypt the account's private key with a password
account.encryptAccount(ciphertext) ⇒ PrivateKeyCiphertext
Kind: instance method of Account
Param | Type |
---|---|
ciphertext | string |
return | PrivateKeyCiphertext |
Example
let account = new Account();
let ciphertext = account.encryptAccount("password");
decryptRecord
Decrypts a Record in ciphertext form into plaintext
account.decryptRecord(ciphertext) ⇒ Record
Kind: instance method of Account
Param | Type |
---|---|
ciphertext | string |
return | Record |
Example
let account = new Account();
let record = account.decryptRecord("record1ciphertext");
decryptRecords
Decrypts an array of Records in ciphertext form into plaintext
account.decryptRecords(ciphertexts) ⇒ Array.<Record>
Kind: instance method of Account
Param | Type |
---|---|
ciphertexts | Array.<string> |
return | Array.<Record> |
Example
let account = new Account();
let record = account.decryptRecords(["record1ciphertext", "record2ciphertext"]);
ownsRecordCiphertext
Determines whether the account owns a ciphertext record
account.ownsRecordCiphertext(ciphertext) ⇒ boolean
Kind: instance method of Account
Param | Type |
---|---|
ciphertext | RecordCipherText | string |
return | boolean |
Example
// Create a connection to the Aleo network and an account
let connection = new NodeConnection("vm.aleo.org/api");
let account = Account.fromCiphertext("ciphertext", "password");
// Get a record from the network
let record = connection.getBlock(1234);
let recordCipherText = record.transactions[0].execution.transitions[0].id;
// Check if the account owns the record
if account.ownsRecord(recordCipherText) {
// Then one can do something like:
// Decrypt the record and check if it's spent
// Store the record in a local database
// Etc.
}
sign
Signs a message with the account's private key. Returns a Signature.
account.sign(message) ⇒ Signature
Kind: instance method of Account
Param | Type |
---|---|
message | Uint8Array |
return | Signature |
Example
let account = new Account();
let message = Uint8Array.from([104, 101, 108, 108, 111 119, 111, 114, 108, 100])
account.sign(message);
verify
Verifies the Signature on a message.
account.verify(message, signature) ⇒ boolean
Kind: instance method of Account
Param | Type |
---|---|
message | Uint8Array |
signature | Signature |
return | boolean |
Example
let account = new Account();
let message = Uint8Array.from([104, 101, 108, 108, 111 119, 111, 114, 108, 100])
let signature = account.sign(message);
account.verify(message, signature);
fromCiphertext
Attempts to create an account from a private key ciphertext
Account.fromCiphertext(ciphertext, password) ⇒ PrivateKey | Error
Kind: static method of Account
Param | Type |
---|---|
ciphertext | PrivateKeyCiphertext | string |
password | string |
return | PrivateKey | Error |
Example
let ciphertext = PrivateKey.newEncrypted("password");
let account = Account.fromCiphertext(ciphertext, "password");