Skip to content

Commit

Permalink
Merge branch 'docs/sdk-doc-reorganization' of github.com:ProvableHQ/s…
Browse files Browse the repository at this point in the history
…dk into docs/sdk-doc-reorganization
  • Loading branch information
iamalwaysuncomfortable committed Feb 20, 2025
2 parents b7fb099 + 605f098 commit 1378f44
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 5 deletions.
90 changes: 86 additions & 4 deletions docs/Guide/03_creating_accounts.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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.
```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);
```
7 changes: 7 additions & 0 deletions sdk/src/account.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
Address,
ComputeKey,
PrivateKey,
Signature,
ViewKey,
Expand Down Expand Up @@ -44,6 +45,7 @@ interface AccountParam {
export class Account {
_privateKey: PrivateKey;
_viewKey: ViewKey;
_computeKey: ComputeKey;
_address: Address;

constructor(params: AccountParam = {}) {
Expand All @@ -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);
}

Expand Down Expand Up @@ -95,6 +98,10 @@ export class Account {
return this._viewKey;
}

computeKey() {
return this._computeKey;
}

address() {
return this._address;
}
Expand Down
1 change: 1 addition & 0 deletions sdk/src/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export { logAndThrow } from "./utils";
export {
Address,
Ciphertext,
ComputeKey,
Execution as FunctionExecution,
ExecutionResponse,
Field,
Expand Down
1 change: 1 addition & 0 deletions sdk/src/wasm.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export {
Address,
Ciphertext,
ComputeKey,
Execution,
ExecutionResponse,
Field,
Expand Down
6 changes: 5 additions & 1 deletion sdk/tests/account.test.ts
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand All @@ -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', () => {
Expand All @@ -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);
Expand Down

0 comments on commit 1378f44

Please sign in to comment.