Skip to content

Commit

Permalink
[Release] Bump SDK Release to v0.6.5
Browse files Browse the repository at this point in the history
  • Loading branch information
iamalwaysuncomfortable authored Oct 26, 2023
2 parents e024000 + 362472c commit 538a9e2
Show file tree
Hide file tree
Showing 25 changed files with 415 additions and 271 deletions.
24 changes: 11 additions & 13 deletions create-aleo-app/template-node/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ async function localProgramExecution(program, aleoFunction, inputs) {

// Create a key provider in order to re-use the same key for each execution
const keyProvider = new AleoKeyProvider();
keyProvider.useCache(true);
programManager.setKeyProvider(keyProvider);

// Pre-synthesize the program keys and then cache them in memory using key provider
Expand All @@ -36,24 +37,21 @@ async function localProgramExecution(program, aleoFunction, inputs) {
hello_hello_program,
"hello",
["5u32", "5u32"],
false,
true,
undefined,
keyProviderParams,
);
console.log(executionResponse.getOutputs())

executionResponse = await programManager.executeOffline(
hello_hello_program,
"hello",
["5u32", "5u32"],
false,
keyProviderParams,
);
return executionResponse.getOutputs();
console.log("hello_hello/hello executed - result:", executionResponse.getOutputs());

// Verify the execution using the verifying key that was generated earlier.
if (programManager.verifyExecution(executionResponse)) {
console.log("hello_hello/hello execution verified!");
} else {
throw("Execution failed verification!");
}
}

const start = Date.now();
console.log("Starting execute!");
const result = await localProgramExecution();
console.log(result);
await localProgramExecution();
console.log("Execute finished!", Date.now() - start);
4 changes: 2 additions & 2 deletions create-aleo-app/template-react-leo/src/workers/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ async function deployProgram(program) {
keyProvider.useCache(true);

// Create a record provider that will be used to find records and transaction data for Aleo programs
const networkClient = new AleoNetworkClient("https://vm.aleo.org/api");
const networkClient = new AleoNetworkClient("https://api.explorer.aleo.org/v1");

// Use existing account with funds
const account = new Account({
Expand All @@ -48,7 +48,7 @@ async function deployProgram(program) {

// Initialize a program manager to talk to the Aleo network with the configured key and record providers
const programManager = new ProgramManager(
"https://vm.aleo.org/api",
"https://api.explorer.aleo.org/v1",
keyProvider,
recordProvider,
);
Expand Down
26 changes: 13 additions & 13 deletions sdk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -259,12 +259,12 @@ const keyProvider = new AleoKeyProvider();
keyProvider.useCache = true;
// Create a record provider that will be used to find records and transaction data for Aleo programs
const networkClient = new AleoNetworkClient("https://vm.aleo.org/api");
const networkClient = new AleoNetworkClient("https://api.explorer.aleo.org/v1");
const recordProvider = new NetworkRecordProvider(account, networkClient);
// Initialize a program manager to talk to the Aleo network with the configured key and record providers
const programName = "hello_hello.aleo";
const programManager = new ProgramManager("https://vm.aleo.org/api", keyProvider, recordProvider);
const programManager = new ProgramManager("https://api.explorer.aleo.org/v1", keyProvider, recordProvider);
// Provide a key search parameter to find the correct key for the program if they are stored in a memory cache
const keySearchParams = { "cacheKey": "hello_hello:hello" };
Expand Down Expand Up @@ -325,7 +325,7 @@ const keyProvider = new AleoKeyProvider();
keyProvider.useCache(true);

// Create a record provider that will be used to find records and transaction data for Aleo programs
const networkClient = new AleoNetworkClient("https://vm.aleo.org/api");
const networkClient = new AleoNetworkClient("https://api.explorer.aleo.org/v1");

// Use existing account with funds
const account = new Account({
Expand All @@ -335,7 +335,7 @@ const account = new Account({
const recordProvider = new NetworkRecordProvider(account, networkClient);

// Initialize a program manager to talk to the Aleo network with the configured key and record providers
const programManager = new ProgramManager("https://vm.aleo.org/api", keyProvider, recordProvider);
const programManager = new ProgramManager("https://api.explorer.aleo.org/v1", keyProvider, recordProvider);
programManager.setAccount(account)

// Define an Aleo program to deploy
Expand Down Expand Up @@ -388,9 +388,9 @@ import * as aleo from "@aleohq/sdk";
await aleo.initThreadPool();

/// The program manager is initialized with a key provider and a record provider
const defaultHost = "https://vm.aleo.org/api";
const defaultHost = "https://api.explorer.aleo.org/v1";
const keyProvider = new aleo.AleoKeyProvider();
const recordProvider = new aleo.NetworkRecordProvider(new Account(), "https://vm.aleo.org/api");
const recordProvider = new aleo.NetworkRecordProvider(new Account(), "https://api.explorer.aleo.org/v1");
const programManager = new aleo.ProgramManager(
defaultHost,
keyProvider,
Expand Down Expand Up @@ -767,13 +767,13 @@ import { Account, ProgramManager, AleoKeyProvider, NetworkRecordProvider, AleoNe

// Create a new NetworkClient, KeyProvider, and RecordProvider
const account = Account.from_string({privateKey: "user1PrivateKey"});
const networkClient = new AleoNetworkClient("https://vm.aleo.org/api");
const networkClient = new AleoNetworkClient("https://api.explorer.aleo.org/v1");
const keyProvider = new AleoKeyProvider();
const recordProvider = new NetworkRecordProvider(account, networkClient);

// Initialize a program manager with the key provider to automatically fetch keys for executions
const USER_1_ADDRESS = "user1Address";
const programManager = new ProgramManager("https://vm.aleo.org/api", keyProvider, recordProvider);
const programManager = new ProgramManager("https://api.explorer.aleo.org/v1", keyProvider, recordProvider);
programManager.setAccount(account);

// Send a private transfer to yourself
Expand Down Expand Up @@ -808,7 +808,7 @@ assert(public_balance === 0);
As shown above, a public balance of any address can be checked with `getMappingValue` function of the `NetworkClient`.
```typescript
const networkClient = new AleoNetworkClient("https://vm.aleo.org/api");
const networkClient = new AleoNetworkClient("https://api.explorer.aleo.org/v1");
const USER_1_ADDRESS = "user1Address";
const public_balance = networkClient.getMappingValue("credits.aleo", USER_1_ADDRESS);
```
Expand Down Expand Up @@ -897,13 +897,13 @@ import { Account, ProgramManager, AleoKeyProvider, NetworkRecordProvider, AleoNe

// Create a new NetworkClient, KeyProvider, and RecordProvider
const account = Account.from_string({privateKey: "user1PrivateKey"});
const networkClient = new AleoNetworkClient("https://vm.aleo.org/api");
const networkClient = new AleoNetworkClient("https://api.explorer.aleo.org/v1");
const keyProvider = new AleoKeyProvider();
const recordProvider = new NetworkRecordProvider(account, networkClient);

// Initialize a program manager with the key provider to automatically fetch keys for executions
const USER_2_ADDRESS = "user2Address";
const programManager = new ProgramManager("https://vm.aleo.org/api", keyProvider, recordProvider);
const programManager = new ProgramManager("https://api.explorer.aleo.org/v1", keyProvider, recordProvider);
programManager.setAccount(account);

/// Send private transfer to user 2
Expand All @@ -918,12 +918,12 @@ import { Account, ProgramManager, AleoKeyProvider, NetworkRecordProvider, AleoNe

// Create a new NetworkClient, KeyProvider, and RecordProvider
const account = Account.from_string({privateKey: "user2PrivateKey"});
const networkClient = new AleoNetworkClient("https://vm.aleo.org/api");
const networkClient = new AleoNetworkClient("https://api.explorer.aleo.org/v1");
const keyProvider = new AleoKeyProvider();
const recordProvider_User2 = new NetworkRecordProvider(account, networkClient);

// Initialize a program manager with the key provider to automatically fetch keys for executions
const programManager = new ProgramManager("https://vm.aleo.org/api", keyProvider, recordProvider);
const programManager = new ProgramManager("https://api.explorer.aleo.org/v1", keyProvider, recordProvider);
programManager.setAccount(account);

// Fetch the transaction from the network that user 1 sent
Expand Down
2 changes: 1 addition & 1 deletion sdk/docs/AleoNetworkClient.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
let local_connection = new AleoNetworkClient("http://localhost:3030");

// Connection to a public beacon node
let public_connection = new AleoNetworkClient("https://vm.aleo.org/api");</code></pre></div></div></div><h2 id="classes" class="subsection-title has-anchor">Classes</h2><dl><dt><a href="AleoNetworkClient.html">AleoNetworkClient</a></dt><dd></dd></dl><h2 id="methods" class="subsection-title has-anchor">Methods</h2><h3 class="name has-anchor" id="findUnspentRecords"><span class="type-signature">(async) </span>findUnspentRecords<span class="signature">()</span></h3><div class="description">Attempts to find unspent records in the Aleo blockchain for a specified private key</div><dl class="details"><div class="details-item-container"><dt class="tag-source bold">Source</dt><dd class="tag-source"><ul><li><a href="aleo_network_client.ts.html">network-client.ts</a>, <a href="aleo_network_client.ts.html#line230">line 230</a></li></ul></dd></div></dl><div class="method-member-container flex flex-col w-100 overflow-auto mt-20"><strong>Example</strong><div class="rel"><pre class="prettyprint"><code>// Find all unspent records
let public_connection = new AleoNetworkClient("https://api.explorer.aleo.org/v1");</code></pre></div></div></div><h2 id="classes" class="subsection-title has-anchor">Classes</h2><dl><dt><a href="AleoNetworkClient.html">AleoNetworkClient</a></dt><dd></dd></dl><h2 id="methods" class="subsection-title has-anchor">Methods</h2><h3 class="name has-anchor" id="findUnspentRecords"><span class="type-signature">(async) </span>findUnspentRecords<span class="signature">()</span></h3><div class="description">Attempts to find unspent records in the Aleo blockchain for a specified private key</div><dl class="details"><div class="details-item-container"><dt class="tag-source bold">Source</dt><dd class="tag-source"><ul><li><a href="aleo_network_client.ts.html">network-client.ts</a>, <a href="aleo_network_client.ts.html#line230">line 230</a></li></ul></dd></div></dl><div class="method-member-container flex flex-col w-100 overflow-auto mt-20"><strong>Example</strong><div class="rel"><pre class="prettyprint"><code>// Find all unspent records
const privateKey = "[PRIVATE_KEY]";
let records = connection.findUnspentRecords(0, undefined, privateKey);

Expand Down
2 changes: 1 addition & 1 deletion sdk/docs/aleo_network_client.ts.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* let local_connection = new AleoNetworkClient("http://localhost:3030");
*
* // Connection to a public beacon node
* let public_connection = new AleoNetworkClient("https://vm.aleo.org/api");
* let public_connection = new AleoNetworkClient("https://api.explorer.aleo.org/v1");
*/
export class AleoNetworkClient {
host: string;
Expand Down
2 changes: 1 addition & 1 deletion sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@aleohq/sdk",
"version": "0.6.3",
"version": "0.6.5",
"description": "A Software Development Kit (SDK) for Zero-Knowledge Transactions",
"collaborators": [
"The Aleo Team <[email protected]>"
Expand Down
14 changes: 7 additions & 7 deletions sdk/src/function-key-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,11 @@ interface FunctionKeyProvider {
*
*
* const keyProvider = new AleoKeyProvider();
* const networkClient = new AleoNetworkClient("https://vm.aleo.org/api");
* const networkClient = new AleoNetworkClient("https://api.explorer.aleo.org/v1");
* const recordProvider = new NetworkRecordProvider(account, networkClient);
*
* // Initialize a program manager with the key provider to automatically fetch keys for value transfers
* const programManager = new ProgramManager("https://vm.aleo.org/api", keyProvider, recordProvider);
* const programManager = new ProgramManager("https://api.explorer.aleo.org/v1", keyProvider, recordProvider);
* programManager.transfer(1, "aleo166q6ww6688cug7qxwe7nhctjpymydwzy2h7rscfmatqmfwnjvggqcad0at", "public", 0.5);
*
* // Keys can also be fetched manually
Expand All @@ -120,12 +120,12 @@ interface FunctionKeyProvider {
*
* @example
* // Create a new object which implements the KeyProvider interface
* const networkClient = new AleoNetworkClient("https://vm.aleo.org/api");
* const networkClient = new AleoNetworkClient("https://api.explorer.aleo.org/v1");
* const keyProvider = new AleoKeyProvider();
* const recordProvider = new NetworkRecordProvider(account, networkClient);
*
* // Initialize a program manager with the key provider to automatically fetch keys for value transfers
* const programManager = new ProgramManager("https://vm.aleo.org/api", keyProvider, recordProvider);
* const programManager = new ProgramManager("https://api.explorer.aleo.org/v1", keyProvider, recordProvider);
* programManager.transfer(1, "aleo166q6ww6688cug7qxwe7nhctjpymydwzy2h7rscfmatqmfwnjvggqcad0at", "public", 0.5);
*
* // Keys can also be fetched manually
Expand Down Expand Up @@ -271,13 +271,13 @@ class AleoKeyProvider implements FunctionKeyProvider {
*
* @example
* // Create a new object which implements the KeyProvider interface
* const networkClient = new AleoNetworkClient("https://vm.aleo.org/api");
* const networkClient = new AleoNetworkClient("https://api.explorer.aleo.org/v1");
* const keyProvider = new AleoKeyProvider();
* const recordProvider = new NetworkRecordProvider(account, networkClient);
* const AleoProviderParams = new AleoProviderParams("https://testnet3.parameters.aleo.org/transfer_private.");
*
* // Initialize a program manager with the key provider to automatically fetch keys for value transfers
* const programManager = new ProgramManager("https://vm.aleo.org/api", keyProvider, recordProvider);
* const programManager = new ProgramManager("https://api.explorer.aleo.org/v1", keyProvider, recordProvider);
* programManager.transfer(1, "aleo166q6ww6688cug7qxwe7nhctjpymydwzy2h7rscfmatqmfwnjvggqcad0at", "public", 0.5);
*
* // Keys can also be fetched manually using the key provider
Expand Down Expand Up @@ -323,7 +323,7 @@ class AleoKeyProvider implements FunctionKeyProvider {
*
* @example
* // Create a new AleoKeyProvider object
* const networkClient = new AleoNetworkClient("https://vm.aleo.org/api");
* const networkClient = new AleoNetworkClient("https://api.explorer.aleo.org/v1");
* const keyProvider = new AleoKeyProvider();
* const recordProvider = new NetworkRecordProvider(account, networkClient);
*
Expand Down
47 changes: 40 additions & 7 deletions sdk/src/network-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type ProgramImports = { [key: string]: string | Program };
* const localNetworkClient = new AleoNetworkClient("http://localhost:3030");
*
* // Connection to a public beacon node
* const publicnetworkClient = new AleoNetworkClient("https://vm.aleo.org/api");
* const publicnetworkClient = new AleoNetworkClient("https://api.explorer.aleo.org/v1");
*/
class AleoNetworkClient {
host: string;
Expand Down Expand Up @@ -307,6 +307,39 @@ class AleoNetworkClient {
}
}

/**
* Returns the deployment transaction id associated with the specified program
*
* @param {Program | string} program
* @returns {Transaction | Error}
*/
async getDeploymentTransactionIDForProgram(program: Program | string): Promise<string | Error> {
if (program instanceof Program) {
program = program.toString();
}
try {
const id = await this.fetchData<string>("/find/transactionID/deployment/" + program);
return id.replace("\"", "")
} catch (error) {
throw new Error("Error fetching deployment transaction for program.");
}
}

/**
* Returns the deployment transaction associated with a specified program
*
* @param {Program | string} program
* @returns {Transaction | Error}
*/
async getDeploymentTransactionForProgram(program: Program | string): Promise<Transaction | Error> {
try {
const transaction_id = <string>await this.getDeploymentTransactionIDForProgram(program);
return <Transaction>await this.getTransaction(transaction_id);
} catch (error) {
throw new Error("Error fetching deployment transaction for program.");
}
}

/**
* Returns the contents of the latest block
*
Expand All @@ -322,16 +355,15 @@ class AleoNetworkClient {
}

/**
* Returns the hash of the last published block
* Returns the latest committee
*
* @example
* const latestHash = networkClient.getLatestHash();
* @returns {Promise<object>} A javascript object containing the latest committee
*/
async getLatestHash(): Promise<string | Error> {
async getLatestCommittee(): Promise<object | Error> {
try {
return await this.fetchData<string>("/latest/hash");
return await this.fetchData<object>("/committee/latest");
} catch (error) {
throw new Error("Error fetching latest hash.");
throw new Error("Error fetching latest block.");
}
}

Expand Down Expand Up @@ -538,6 +570,7 @@ class AleoNetworkClient {
} catch (error) {
throw new Error("Error fetching transaction.");
}

}

/**
Expand Down
30 changes: 25 additions & 5 deletions sdk/src/program-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ import {
PRIVATE_TRANSFER_TYPES,
VALID_TRANSFER_TYPES,
logAndThrow,
ProgramManagerBase as WasmProgramManager,
ProgramManagerBase as WasmProgramManager, verifyFunctionExecution,
} from "./index";
import {Execution} from "@aleohq/wasm/dist/crates/aleo_wasm";



Expand All @@ -40,7 +41,7 @@ class ProgramManager {
*/
constructor(host: string | undefined, keyProvider: FunctionKeyProvider | undefined, recordProvider: RecordProvider | undefined) {
if (!host) {
this.host = "https://vm.aleo.org/api";
this.host = "https://api.explorer.aleo.org/v1";
this.networkClient = new AleoNetworkClient(this.host);
} else {
this.host = host;
Expand Down Expand Up @@ -107,13 +108,13 @@ class ProgramManager {
*
* @example
* // Create a new NetworkClient, KeyProvider, and RecordProvider
* const networkClient = new AleoNetworkClient("https://vm.aleo.org/api");
* const networkClient = new AleoNetworkClient("https://api.explorer.aleo.org/v1");
* const keyProvider = new AleoKeyProvider();
* const recordProvider = new NetworkRecordProvider(account, networkClient);
*
* // Initialize a program manager with the key provider to automatically fetch keys for deployments
* const program = "program hello_hello.aleo;\n\nfunction hello:\n input r0 as u32.public;\n input r1 as u32.private;\n add r0 r1 into r2;\n output r2 as u32.private;\n";
* const programManager = new ProgramManager("https://vm.aleo.org/api", keyProvider, recordProvider);
* const programManager = new ProgramManager("https://api.explorer.aleo.org/v1", keyProvider, recordProvider);
*
* // Define a fee in credits
* const fee = 1.2;
Expand Down Expand Up @@ -355,7 +356,7 @@ class ProgramManager {
console.log("Running program offline")
console.log("Proving key: ", provingKey);
console.log("Verifying key: ", verifyingKey);
return WasmProgramManager.executeFunctionOffline(executionPrivateKey, program, function_name, inputs, proveExecution, false, imports, provingKey, verifyingKey);
return WasmProgramManager.executeFunctionOffline(executionPrivateKey, program, function_name, inputs, proveExecution, false, imports, provingKey, verifyingKey, this.host);
}

/**
Expand Down Expand Up @@ -595,6 +596,25 @@ class ProgramManager {
return await this.networkClient.submitTransaction(tx);
}

/**
* Verify a proof of execution from an offline execution
*
* @param {executionResponse} executionResponse
* @returns {boolean} True if the proof is valid, false otherwise
*/
verifyExecution(executionResponse: ExecutionResponse): boolean {
try {
const execution = <Execution>executionResponse.getExecution();
const function_id = executionResponse.getFunctionId();
const program = executionResponse.getProgram();
const verifyingKey = executionResponse.getVerifyingKey();
return verifyFunctionExecution(execution, verifyingKey, program, function_id);
} catch(e) {
console.warn("The execution was not found in the response, cannot verify the execution");
return false;
}
}

/**
* Create a program object from a program's source code
*
Expand Down
Loading

0 comments on commit 538a9e2

Please sign in to comment.