diff --git a/docs/Guide/03_creating_accounts.md b/docs/Guide/03_creating_accounts.md index 7af9843cf..9c022b49a 100644 --- a/docs/Guide/03_creating_accounts.md +++ b/docs/Guide/03_creating_accounts.md @@ -1,14 +1,16 @@ --- id: accounts -title: Creating Aleo Accounts -sidebar_label: Creating Aleo Accounts +title: Creating and Managing Aleo Accounts +sidebar_label: Creating and Managing Aleo Accounts --- +# Account Creation and Management The first step in operating a zero-knowledge web application is creating a private key which serves as a cryptographic identity for a user. From it, the user's address and several other useful cryptographic keys that comprise the user's identity are derived. -The total list of keys which comprise an Aleo account are as follows: + +## Account Keys #### Private Key The `Private Key` can be thought of as the identity of a user and is the most sensitive of the keys within an Aleo account. @@ -36,7 +38,87 @@ const account = new Account(); // Individual keys can be then be accessed through the following methods const privateKey = account.privateKey(); const viewKey = account.viewKey(); +const computeKey = account.computeKey(); const address = account.address(); ``` -Please note that all keys are considered sensitive information and should be stored securely. \ No newline at end of file +```mermaid +flowchart TD + subgraph Account + B( + Private Key: + An account's private and + unique identity. + ) + B --> C( + Address: + An account's unique + public identifier. + ) + B --> D( + View Key: + The key that allows + an account to decrypt + private data it owns + and prove ownership + of data. + + ) + B --> E( + Compute Key: + Key used to trustlessly + run applications and + generate transactions on + an account's behalf. + ) + end +``` + +Please note that all keys are considered sensitive information and should be stored securely. + +## Creating an Account + +A new account can be created as displayed below: +```typescript +import { Account } from '@provablehq/sdk'; + +const account = new Account(); +``` + +Alternatively, an account can be created with an existing private key: +```typescript +import { Account } from '@provablehq/sdk'; +import { PrivateKey } from './wasm'; + +// From a newly generated private key +const privateKey = new PrivateKey(); +const account = new Account({ privateKey }); + +// From a private key derived from its string representation +const privateKey2 = PrivateKey.fromString('APrivateKey1...'); +const account2 = new Account({ + privateKey: privateKey2, +}); + +// From a private key string +const account3 = new Account({ + privateKey: 'APrivateKey1...', +}); +``` + +Or an encrypted ciphertext of the private key: +```typescript +import { Account } from '@provablehq/sdk'; +import { PrivateKey } from './wasm'; + +// From a newly generated encrypted private key +const password = 'password'; +const ciphertext = PrivateKey.newEncrypted(password); +const account = Account.fromCiphertext(ciphertext, password); + +// From the encryption of an existing private key +const privateKey = PrivateKey.fromString('APrivateKey1...'); +const otherPassword = 'otherPassword'; +const otherCiphertext = privateKey.toCiphertext(otherPassword); +const otherAccount = Account.fromCiphertext(otherCiphertext, otherPassword); +``` \ No newline at end of file diff --git a/sdk/src/account.ts b/sdk/src/account.ts index 0fa64cab5..a57e828a1 100644 --- a/sdk/src/account.ts +++ b/sdk/src/account.ts @@ -1,5 +1,6 @@ import { Address, + ComputeKey, PrivateKey, Signature, ViewKey, @@ -44,6 +45,7 @@ interface AccountParam { export class Account { _privateKey: PrivateKey; _viewKey: ViewKey; + _computeKey: ComputeKey; _address: Address; constructor(params: AccountParam = {}) { @@ -54,6 +56,7 @@ export class Account { throw new Error("Wrong Parameter"); } this._viewKey = ViewKey.from_private_key(this._privateKey); + this._computeKey = ComputeKey.from_private_key(this._privateKey); this._address = Address.from_private_key(this._privateKey); } @@ -95,6 +98,10 @@ export class Account { return this._viewKey; } + computeKey() { + return this._computeKey; + } + address() { return this._address; } diff --git a/sdk/src/browser.ts b/sdk/src/browser.ts index 932dc1cae..c75fdc07f 100644 --- a/sdk/src/browser.ts +++ b/sdk/src/browser.ts @@ -57,6 +57,7 @@ export { logAndThrow } from "./utils"; export { Address, Ciphertext, + ComputeKey, Execution as FunctionExecution, ExecutionResponse, Field, diff --git a/sdk/src/wasm.ts b/sdk/src/wasm.ts index 1ba048720..d3a112f9c 100644 --- a/sdk/src/wasm.ts +++ b/sdk/src/wasm.ts @@ -1,6 +1,7 @@ export { Address, Ciphertext, + ComputeKey, Execution, ExecutionResponse, Field, diff --git a/sdk/tests/account.test.ts b/sdk/tests/account.test.ts index a0474758a..534706a6f 100644 --- a/sdk/tests/account.test.ts +++ b/sdk/tests/account.test.ts @@ -1,6 +1,6 @@ import sinon from "sinon"; import { expect } from "chai"; -import { Account, Address, PrivateKey, RecordCiphertext, ViewKey } from "../src/node"; +import { Account, Address, ComputeKey, PrivateKey, RecordCiphertext, ViewKey } from "../src/node"; import { seed, message, beaconPrivateKeyString, beaconViewKeyString, beaconAddressString, recordCiphertextString, foreignCiphertextString, recordPlaintextString } from "./data/account-data"; describe('Account', () => { @@ -17,10 +17,12 @@ describe('Account', () => { expect(account._privateKey).instanceof(PrivateKey); expect(account._viewKey).instanceof(ViewKey); expect(account._address).instanceof(Address); + expect(account._computeKey).instanceOf(ComputeKey); // Test convenience method type consistency expect(account.privateKey()).instanceof(PrivateKey); expect(account.viewKey()).instanceof(ViewKey); expect(account.address()).instanceof(Address); + expect(account.computeKey()).instanceOf(ComputeKey); }); it('creates a new from seed', () => { @@ -31,10 +33,12 @@ describe('Account', () => { expect(account._privateKey).instanceof(PrivateKey); expect(account._viewKey).instanceof(ViewKey); expect(account._address).instanceof(Address); + expect(account._computeKey).instanceOf(ComputeKey); // Test convenience method type consistency expect(account.privateKey()).instanceof(PrivateKey); expect(account.viewKey()).instanceof(ViewKey); expect(account.address()).instanceof(Address); + expect(account.computeKey()).instanceOf(ComputeKey); // Test that expected output is generated expect(account.privateKey().to_string()).equal(beaconPrivateKeyString); expect(account.viewKey().to_string()).equal(beaconViewKeyString);