@provablehq/sdk 0.8.2 → 0.8.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +44 -928
- package/dist/mainnet/account.d.ts +4 -1
- package/dist/mainnet/browser.d.ts +4 -2
- package/dist/mainnet/browser.js +69 -9
- package/dist/mainnet/browser.js.map +1 -1
- package/dist/mainnet/models/blockJSON.d.ts +18 -6
- package/dist/mainnet/models/confirmed_transaction.d.ts +1 -1
- package/dist/mainnet/models/ratification.d.ts +4 -0
- package/dist/mainnet/models/solution.d.ts +14 -0
- package/dist/mainnet/network-client.d.ts +49 -11
- package/dist/mainnet/node.js +2 -2
- package/dist/mainnet/{program-manager-pTXN5gjF.js → program-manager-DUAbZEGO.js} +108 -43
- package/dist/mainnet/program-manager-DUAbZEGO.js.map +1 -0
- package/dist/mainnet/record-provider.d.ts +1 -1
- package/dist/mainnet/wasm.d.ts +1 -1
- package/dist/mainnet/worker.js +1 -1
- package/dist/testnet/account.d.ts +4 -1
- package/dist/testnet/browser.d.ts +4 -2
- package/dist/testnet/browser.js +69 -9
- package/dist/testnet/browser.js.map +1 -1
- package/dist/testnet/models/blockJSON.d.ts +18 -6
- package/dist/testnet/models/confirmed_transaction.d.ts +1 -1
- package/dist/testnet/models/ratification.d.ts +4 -0
- package/dist/testnet/models/solution.d.ts +14 -0
- package/dist/testnet/network-client.d.ts +49 -11
- package/dist/testnet/node.js +2 -2
- package/dist/testnet/{program-manager-DQZd0G79.js → program-manager-B2GtLTAh.js} +108 -43
- package/dist/testnet/program-manager-B2GtLTAh.js.map +1 -0
- package/dist/testnet/record-provider.d.ts +1 -1
- package/dist/testnet/wasm.d.ts +1 -1
- package/dist/testnet/worker.js +1 -1
- package/package.json +2 -2
- package/dist/mainnet/program-manager-pTXN5gjF.js.map +0 -1
- package/dist/testnet/program-manager-DQZd0G79.js.map +0 -1
package/README.md
CHANGED
|
@@ -1,4 +1,9 @@
|
|
|
1
|
-
|
|
1
|
+
---
|
|
2
|
+
id: overview
|
|
3
|
+
title: SDK for Building Zero Knowledge Web Apps
|
|
4
|
+
sidebar_label: Overview
|
|
5
|
+
---
|
|
6
|
+
|
|
2
7
|
<p align="center">
|
|
3
8
|
<img alt="Aleo SDK" width="1412" src="../.resources/banner.png">
|
|
4
9
|
</p>
|
|
@@ -8,946 +13,57 @@
|
|
|
8
13
|
<a href="https://circleci.com/gh/ProvableHQ/sdk"><img src="https://circleci.com/gh/ProvableHQ/sdk.svg?style=svg"></a>
|
|
9
14
|
<a href="https://discord.com/invite/aleo"><img src="https://img.shields.io/discord/700454073459015690?logo=discord"/></a>
|
|
10
15
|
<a href="https://github.com/ProvableHQ/sdk#%EF%B8%8F-contributors"><img src="https://img.shields.io/badge/contributors-23-ee8449"/></a>
|
|
16
|
+
<a href="https://www.npmjs.com/package/@provablehq/sdk"> <img alt="Aleo SDK" src="https://img.shields.io/npm/l/%40provablehq%2Fsdk?label=NPM%20-%20Aleo%20SDK&labelColor=green&color=blue"></a>
|
|
11
17
|
</p>
|
|
12
18
|
|
|
13
|
-
## Tools for Building Zero-Knowledge Web Apps
|
|
14
|
-
|
|
15
|
-
The Aleo SDK is a JavaScript library for building zero-knowledge web applications in both web browsers and Node.js.
|
|
16
19
|
|
|
17
|
-
|
|
20
|
+
The [Provable SDK](https://github.com/ProvableHQ/sdk) is an open source TypeScript/JavaScript SDK for building private full stack web applications. The
|
|
21
|
+
SDK provides an easy API for using Aleo's core suite of cryptographic primitives, zk-SNARKs and private program execution
|
|
22
|
+
protocol directly within Javascript. It also provides APIs for interacting with nodes and applications on the Aleo Network.
|
|
18
23
|
|
|
19
|
-
|
|
20
|
-
|
|
24
|
+
Applications built with the Aleo SDK include:
|
|
25
|
+
* **Private DeFi**
|
|
26
|
+
* **Private Data Custody Systems**
|
|
27
|
+
* **Zk Games & Gaming Toolkits**
|
|
28
|
+
* **ZkML**
|
|
29
|
+
* **Zero Knowledge Identity Management**
|
|
30
|
+
* **Aleo Wallets**
|
|
21
31
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
The Aleo SDK provides the following functionality (Click to see examples):
|
|
32
|
+
## Features
|
|
33
|
+
The Aleo SDK provides the following features. Follow the links below for live demonstrations of these features on
|
|
34
|
+
[provable.tools](https://provable.tools)
|
|
26
35
|
1. [Aleo account management](https://provable.tools/account)
|
|
27
|
-
2. [
|
|
28
|
-
3. [
|
|
29
|
-
4. [
|
|
30
|
-
5. [
|
|
31
|
-
6. [
|
|
32
|
-
|
|
33
|
-
## Table of Contents
|
|
34
|
-
|
|
35
|
-
* [Installation](#Installation)
|
|
36
|
-
* [Usage](#Usage)
|
|
37
|
-
* [Zero-Knowledge Web App Examples](#Zero-Knowledge-Web-App-Examples)
|
|
38
|
-
* [Create Leo App](#create-leo-app)
|
|
39
|
-
* [Provable.tools](#provabletools)
|
|
40
|
-
* [Create An Aleo Account](#1-create-an-aleo-account)
|
|
41
|
-
* [Execute Aleo Programs](#2-execute-aleo-programs)
|
|
42
|
-
* [Aleo Programs](#21-aleo-programs)
|
|
43
|
-
* [Program Execution Model](#22-program-execution-model)
|
|
44
|
-
* [WebAssembly Initialization](#23-wasm-initialization)
|
|
45
|
-
* [Local Program Execution](#24-local-program-execution)
|
|
46
|
-
* [Online Program Execution](#25-program-execution-on-the-aleo-network)
|
|
47
|
-
* [Program Proving Keys and Records](#26-program-proving-keys--program-records)
|
|
48
|
-
* [Deploy Programs](#27-deploy-a-new-program-to-the-aleo-network)
|
|
49
|
-
* [React Example](#28-react-example)
|
|
50
|
-
* [Aleo Credit Transfers](#3-value-transfers)
|
|
51
|
-
* [Aleo Credits](#31-aleo-credits)
|
|
52
|
-
* [Transfer Aleo Credits](#32-transfer-aleo-credits)
|
|
53
|
-
* [Check Public Balances](#32-checking-public-balances)
|
|
54
|
-
* [Program Data and Private State](#4-managing-program-data-and-private-state)
|
|
55
|
-
* [Private State Data: Records](#41-private-state-data--records)
|
|
56
|
-
* [Record Usage Example](#42-record-usage-example--private-value-transfers)
|
|
57
|
-
* [Public State Data: Mappings](#43-public-state-data--mappings)
|
|
58
|
-
* [Reading Mappings](#44-reading-mappings)
|
|
59
|
-
* [Initializing and Updating Mappings](#45-initializing--updating-mappings)
|
|
60
|
-
* [Communicating with the Aleo Network](#5-communicating-with-the-aleo-network)
|
|
61
|
-
* [Further Documentation](#further-documentation)
|
|
62
|
-
## Installation
|
|
63
|
-
|
|
64
|
-
### Clone the repository
|
|
65
|
-
|
|
66
|
-
To clone the repository, run:
|
|
67
|
-
|
|
68
|
-
`git clone git@github.com:ProvableHQ/sdk.git`
|
|
69
|
-
|
|
70
|
-
### NPM & YARN
|
|
71
|
-
|
|
72
|
-
To install the Aleo SDK from NPM run:
|
|
73
|
-
|
|
74
|
-
`npm install @provablehq/sdk` in your own project's root,
|
|
75
|
-
|
|
76
|
-
To install the Aleo SDK from Yarn run:
|
|
77
|
-
|
|
78
|
-
`yarn add @provablehq/sdk`.
|
|
79
|
-
|
|
80
|
-
### Build from source
|
|
81
|
-
|
|
82
|
-
To build the project from source, go to the project's root and execute:
|
|
83
|
-
|
|
84
|
-
`yarn build:all`
|
|
85
|
-
|
|
86
|
-
### Ensure compatibility with ES modules
|
|
87
|
-
|
|
88
|
-
In your project's `package.json`, ensure that the following line is added above `scripts`:
|
|
89
|
-
|
|
90
|
-
```json
|
|
91
|
-
{
|
|
92
|
-
"type": "module",
|
|
93
|
-
}
|
|
94
|
-
```
|
|
95
|
-
|
|
96
|
-
## Getting Started: Zero-Knowledge Web App Examples
|
|
97
|
-
|
|
98
|
-
### Create Leo App
|
|
99
|
-
A set of fully functional examples of zero-knowledge web apps can be found in
|
|
100
|
-
[create-leo-app](https://github.com/ProvableHQ/sdk/tree/mainnet/create-leo-app). Create Leo App provides several web app
|
|
101
|
-
templates in common web frameworks such as React that can be used as a starting point for building zero-knowledge web apps.
|
|
102
|
-
|
|
103
|
-
Developers can get started immediately with create-leo-app by running:
|
|
104
|
-
`npm create leo-app@latest`
|
|
105
|
-
|
|
106
|
-
### Provable.tools
|
|
107
|
-
|
|
108
|
-
Additionally, the SDK powers [provable.tools](https://provable.tools) - a React app that provides a graphical interface for most
|
|
109
|
-
of the functionality provided by the SDK and can be used as a reference for usage of the SDK. Source code for provable.tools
|
|
110
|
-
can be found [in the website directory of the SDK repo](https://github.com/ProvableHQ/sdk/tree/mainnet/website).
|
|
111
|
-
|
|
112
|
-
## Usage
|
|
113
|
-
|
|
114
|
-
## 1. Create an Aleo Account
|
|
115
|
-
|
|
116
|
-
The first step in operating a zero-knowledge web application is creating a private key which serves as a cryptographic
|
|
117
|
-
identity for a user. From it, the user's address and several other useful cryptographic keys that comprise the user's
|
|
118
|
-
identity are derived.
|
|
119
|
-
|
|
120
|
-
The total list of keys which comprise an Aleo account are as follows:
|
|
121
|
-
|
|
122
|
-
#### Private Key
|
|
123
|
-
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.
|
|
124
|
-
It is used to sign and create new program executions, to encrypt & decrypt private data within a zero knowledge function
|
|
125
|
-
execution, and to generate signatures, commitments, and other key material used in zero-knowledge proofs.
|
|
126
|
-
|
|
127
|
-
#### View Key
|
|
128
|
-
The `View Key` is derived from the private key and can be used to both decrypt encrypted data owned by a user and prove
|
|
129
|
-
ownership of that data. Specific usages of this key include, determining ownership of records, decrypting records,
|
|
130
|
-
and decrypting private inputs or outputs of a zero-knowledge program generated by the owner of the private key.
|
|
131
|
-
|
|
132
|
-
#### Compute Key
|
|
133
|
-
The `Compute Key` can be used to trustlessly run applications and generate transactions on a user's behalf.
|
|
134
|
-
|
|
135
|
-
#### Address
|
|
136
|
-
The `Address` is a user's unique public identifier. It serves as an address for a user to receive both Aleo
|
|
137
|
-
credits and data from other zero-knowledge Aleo programs.
|
|
138
|
-
|
|
139
|
-
All of these keys can be created using the account object:
|
|
140
|
-
```typescript
|
|
141
|
-
import { Account } from '@provablehq/sdk';
|
|
142
|
-
|
|
143
|
-
const account = new Account();
|
|
144
|
-
|
|
145
|
-
// Individual keys can be then be accessed through the following methods
|
|
146
|
-
const privateKey = account.privateKey();
|
|
147
|
-
const viewKey = account.viewKey();
|
|
148
|
-
const address = account.address();
|
|
149
|
-
```
|
|
150
|
-
|
|
151
|
-
Please note that all keys are considered sensitive information and should be stored securely.
|
|
152
|
-
|
|
153
|
-
## 2. Execute Aleo Programs
|
|
154
|
-
|
|
155
|
-
### 2.1 Aleo Programs
|
|
156
|
-
|
|
157
|
-
Aleo programs provide the ability for users to make any input or output of a program function private and prove that the
|
|
158
|
-
function was run correctly without exposing the values of these inputs or outputs. This allows developers to build
|
|
159
|
-
applications with complete data privacy.
|
|
160
|
-
|
|
161
|
-
Zero-knowledge programs on Aleo are written in one of two languages:
|
|
162
|
-
1. [Leo](https://docs.leo-lang.org/leo/language): A high-level, developer-friendly language for developing zero-knowledge programs.
|
|
163
|
-
|
|
164
|
-
2. [Aleo Instructions](https://docs.leo-lang.org/aleo/language): A low-level language that provides developers with fine-grained control over the execution
|
|
165
|
-
flow of zero-knowledge programs. Leo code is compiled into Aleo Instructions under the hood.
|
|
166
|
-
|
|
167
|
-
Documentation for both languages can be found at [docs.leo-lang.org](https://docs.leo-lang.org/).
|
|
168
|
-
|
|
169
|
-
#### "Hello World" in Leo
|
|
170
|
-
```
|
|
171
|
-
// A simple program adding two numbers together
|
|
172
|
-
program helloworld.aleo {
|
|
173
|
-
transition hello(public a: u32, b: u32) -> u32 {
|
|
174
|
-
let c: u32 = a + b;
|
|
175
|
-
return c;
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
|
-
```
|
|
179
|
-
|
|
180
|
-
#### "Hello World" in Aleo Instructions
|
|
181
|
-
```
|
|
182
|
-
program helloworld.aleo;
|
|
183
|
-
|
|
184
|
-
// The Leo code above compiles to the following Aleo Instructions:
|
|
185
|
-
function hello:
|
|
186
|
-
input r0 as u32.public;
|
|
187
|
-
input r1 as u32.private;
|
|
188
|
-
add r0 r1 into r2;
|
|
189
|
-
output r2 as u32.private;
|
|
190
|
-
```
|
|
191
|
-
|
|
192
|
-
### 2.2 Program Execution Model
|
|
193
|
-
|
|
194
|
-
#### Lifecycle of a Program Execution
|
|
195
|
-
|
|
196
|
-
When an Aleo program is executed, the following steps occur:
|
|
197
|
-
|
|
198
|
-
1. **Compilation into an R1CS Circuit:** The function code is compiled into an R1CS circuit and checked for correctness
|
|
199
|
-
(or more formally, satisfiability).
|
|
200
|
-
2. **Proof of Satisfiability:** The valid R1CS circuit is encoded into a polynomial representation that is then turned
|
|
201
|
-
into a Varuna ZKSnark proof which proves both that the executor is executing the correct function and that the inputs
|
|
202
|
-
lead to the stated outputs.
|
|
203
|
-
3. **Transaction Generation:** The zero knowledge proof is stored within an `Execution Transaction` and sent to the Aleo
|
|
204
|
-
network. This transaction is then verified by the Aleo network's validator nodes.
|
|
205
|
-
4. **Optional Execution of On-Chain Logic:** Any Aleo function can optionally contain on-chain logic to be executed by
|
|
206
|
-
the Aleo Network's validator nodes. This logic can be used to do further computations and update on-chain key-value
|
|
207
|
-
stores called `mappings` which store a program's public on-chain state.
|
|
208
|
-
5. **Addition to the Ledger**: If the proof is valid, the transaction is added to the Aleo ledger within a
|
|
209
|
-
specific block and all changes the execution made to the program's state are recorded within the Aleo blockchain.
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
#### Program Execution with the Aleo SDK
|
|
213
|
-
|
|
214
|
-
The SDK provides the ability to execute Aleo programs 100% client-side within the browser or Node.js.
|
|
215
|
-
|
|
216
|
-
The `ProgramManager` object encapsulates the functionality for executing programs and making zero-knowledge proofs about
|
|
217
|
-
them. Under the hood it uses cryptographic code compiled from [SnarkVM](https://docs.leo-lang.org/aleo) into WebAssembly
|
|
218
|
-
with JavaScript bindings that allow for the execution of Aleo programs fully within the browser.
|
|
219
|
-
|
|
220
|
-
The basic execution flow of a program within the SDK is as follows:
|
|
221
|
-
1. A web app creates an instance of the `ProgramManager` object.
|
|
222
|
-
2. The SDK wasm modules are loaded into the `WebAssembly` runtime.
|
|
223
|
-
3. An Aleo program in `Aleo Instructions` format is loaded into `WebAssembly` via the `ProgramManager` object.
|
|
224
|
-
4. The web or node app takes user input and begins execution of a program.
|
|
225
|
-
5. The execution is performed entirely within Node.js or the browser via WebAssembly.
|
|
226
|
-
6. The result is returned to the javascript environment in the form of an `Execution Transaction`.
|
|
227
|
-
7. The execution is sent to the Aleo network.
|
|
228
|
-
|
|
229
|
-
### 2.3 WebAssembly Initialization
|
|
230
|
-
|
|
231
|
-
You can enable multithreading by calling the `initThreadPool` function. This will run the SDK on multiple workers,
|
|
232
|
-
which significantly speeds up performance:
|
|
233
|
-
|
|
234
|
-
```typescript
|
|
235
|
-
import { Account, initThreadPool } from '@provablehq/sdk';
|
|
236
|
-
|
|
237
|
-
// Enables multithreading
|
|
238
|
-
await initThreadPool();
|
|
239
|
-
|
|
240
|
-
// Create a new Aleo account
|
|
241
|
-
const account = new Account();
|
|
242
|
-
|
|
243
|
-
// Perform further program logic...
|
|
244
|
-
````
|
|
245
|
-
|
|
246
|
-
### 2.4 Local program execution
|
|
247
|
-
|
|
248
|
-
A simple example of running the "hello world" program locally using Node.js and capturing its outputs is shown below:
|
|
249
|
-
```typescript
|
|
250
|
-
import { Account, ProgramManager } from '@provablehq/sdk';
|
|
251
|
-
|
|
252
|
-
/// Create the source for the "hello world" program
|
|
253
|
-
const program = "program helloworld.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";
|
|
254
|
-
const programManager = new ProgramManager();
|
|
255
|
-
|
|
256
|
-
/// Create a temporary account for the execution of the program
|
|
257
|
-
const account = new Account();
|
|
258
|
-
programManager.setAccount(account);
|
|
259
|
-
|
|
260
|
-
/// Get the response and ensure that the program executed correctly
|
|
261
|
-
const executionResponse = await programManager.run(program, "hello", ["5u32", "5u32"]);
|
|
262
|
-
const result = executionResponse.getOutputs();
|
|
263
|
-
assert.deepStrictEqual(result, ['10u32']);
|
|
264
|
-
```
|
|
265
|
-
|
|
266
|
-
### 2.5 Program execution on the Aleo network
|
|
267
|
-
The SDK provides the ability to execute programs and store an encrypted transcript of the execution on the Aleo
|
|
268
|
-
network that anyone can trustlessly verify.
|
|
269
|
-
|
|
270
|
-
This process can be thought of as being executed in the following steps:
|
|
271
|
-
1. A program is run locally.
|
|
272
|
-
2. A proof that the program was executed correctly and that the outputs follow from the inputs is generated.
|
|
273
|
-
3. A transcript of the proof is generated client-side containing encrypted proof data (see [Section 2.6](#4-managing-records-and-private-state))
|
|
274
|
-
and any public outputs or state the user of the program wishes to reveal.
|
|
275
|
-
4. The proof transcript is posted to the Aleo network and verified by the Aleo validator nodes in a trustless manner.
|
|
276
|
-
5. If the proof is valid, it is stored and anyone can later verify the proof and read the outputs the author of the
|
|
277
|
-
program has chosen to make public. Private inputs will remain encrypted, but the author of the proof can also choose to
|
|
278
|
-
retrieve this encrypted state at any point and decrypt it locally for their own use.
|
|
279
|
-
|
|
280
|
-
Posting an execution to the Aleo network serves as a globally trustless and verifiable record of a program execution as well as
|
|
281
|
-
any resulting state changes in private or public data.
|
|
282
|
-
|
|
283
|
-
A simple example of running the "hello world" program on the Aleo network is shown below:
|
|
284
|
-
```typescript
|
|
285
|
-
import { Account, AleoNetworkClient, NetworkRecordProvider, ProgramManager, AleoKeyProvider } from '@provablehq/sdk';
|
|
286
|
-
|
|
287
|
-
// Create an account
|
|
288
|
-
const account = new Account();
|
|
289
|
-
|
|
290
|
-
// Create a network client to connect to the Aleo network
|
|
291
|
-
const networkClient = new AleoNetworkClient("https://api.explorer.provable.com/v1");
|
|
292
|
-
|
|
293
|
-
// Create a key provider that will be used to find public proving & verifying keys for Aleo programs
|
|
294
|
-
const keyProvider = new AleoKeyProvider();
|
|
295
|
-
keyProvider.useCache = true;
|
|
296
|
-
|
|
297
|
-
// Create a record provider that will be used to find records and transaction data for Aleo programs
|
|
298
|
-
const recordProvider = new NetworkRecordProvider(account, networkClient);
|
|
299
|
-
|
|
300
|
-
// Initialize a program manager to talk to the Aleo network with the configured key and record providers
|
|
301
|
-
const programManager = new ProgramManager("https://api.explorer.provable.com/v1", keyProvider, recordProvider);
|
|
302
|
-
|
|
303
|
-
// Set the account for the program manager
|
|
304
|
-
programManager.setAccount(account);
|
|
305
|
-
|
|
306
|
-
(async () => {
|
|
307
|
-
try {
|
|
308
|
-
// Provide a key search parameter to find the correct key for the program if they are stored in a memory cache
|
|
309
|
-
const keySearchParams = { cacheKey: "helloworld.aleo:main" };
|
|
310
|
-
console.log("Key search parameters set: ", keySearchParams);
|
|
311
|
-
|
|
312
|
-
// Execute the program using the options provided inline
|
|
313
|
-
const tx_id = await programManager.execute({
|
|
314
|
-
programName: "helloworld.aleo",
|
|
315
|
-
functionName: "main",
|
|
316
|
-
fee: 0.020,
|
|
317
|
-
privateFee: false, // Assuming a value for privateFee
|
|
318
|
-
inputs: ["5u32", "5u32"], // Example inputs matching the function definition
|
|
319
|
-
keySearchParams: keySearchParams,
|
|
320
|
-
privateKey: account.privateKey() // Set the private key
|
|
321
|
-
});
|
|
322
|
-
const transaction = await programManager.networkClient.getTransaction(tx_id);
|
|
323
|
-
console.log("Transaction details: ", transaction);
|
|
324
|
-
} catch (error) {
|
|
325
|
-
console.error("Error executing program:", error);
|
|
326
|
-
}
|
|
327
|
-
})();
|
|
328
|
-
```
|
|
329
|
-
|
|
330
|
-
A reader of the above example may notice the `RecordProvider` and `KeyProvider` classes that were not present in the local
|
|
331
|
-
execution example. The `KeyProvider` class helps users of the SDK find `Proving Keys` for programs. `Proving Keys`
|
|
332
|
-
allow zero-knowledge proofs that the programs were executed correctly to be created. The `RecordProvider` class helps
|
|
333
|
-
find `Records` which are private data associated with programs that can be changed and updated throughout time.
|
|
334
|
-
These two concepts are explained in more detail below.
|
|
335
|
-
|
|
336
|
-
### 2.6 Program proving keys & program records
|
|
337
|
-
|
|
338
|
-
Executing Aleo programs using zero-knowledge requires two additional pieces of information:
|
|
339
|
-
|
|
340
|
-
1. **Function Proving & Verifying Keys:** Proving and verifying keys are cryptographic keys that are generated when a
|
|
341
|
-
program function is executed. These keys are public and unique for each function in a program. The proving keys allows any party to
|
|
342
|
-
execute the program and generate a proof that the program was executed correctly. The verifying keys allow any party
|
|
343
|
-
to verify that the proof was generated correctly and the execution is correct. These keys are required to create the
|
|
344
|
-
zero-knowledge property of program execution.
|
|
345
|
-
2. **Program Records:** Records are private state generated by a program belonging to a unique private keyholder. Records
|
|
346
|
-
are generated by a program's functions and can be changed and updated when a user runs various functions of the
|
|
347
|
-
program. These records are private by default and are used to manage updatable private state. One of the most clear
|
|
348
|
-
usages of records is to the `credits` record in the `credits.aleo` program. Credits records are one of two official ways of
|
|
349
|
-
representing Aleo credits on the Aleo network and are used to pay all transaction fees on the network. More information
|
|
350
|
-
on records can be found in the [Records](#41-private-state-data--records) section below.
|
|
351
|
-
|
|
352
|
-
For this reason, all programs will need proving and verifying keys to operate and many functions in Aleo programs will
|
|
353
|
-
require records as inputs. To simplify the process of managing keys and records, the Aleo SDK provides two abstractions
|
|
354
|
-
for managing these concepts:
|
|
355
|
-
|
|
356
|
-
1. **KeyProvider:** When program functions execute, they will by default synthesize the proving and verifying keys needed to
|
|
357
|
-
make a zero-knowledge proof of the execution. However, these keys are large and expensive to generate. For this reason, applications may
|
|
358
|
-
want to store these keys and re-use them in future executions. The `KeyProvider` interface provides the ability for
|
|
359
|
-
users of the SDK to provide their own key storage and retrieval mechanism. The SDK provides a default implementation
|
|
360
|
-
of the `KeyProvider` interface via the `AleoKeyProvider` class.
|
|
361
|
-
2. **RecordProvider:** When programs execute, they will often need to find records that belong to a user. The
|
|
362
|
-
`RecordProvider` interface allows users of the SDK to implement their own record storage and retrieval mechanism. The
|
|
363
|
-
SDK provides a default implementation of the `RecordProvider` interface via the `NetworkRecordProvider` class which
|
|
364
|
-
searches the Aleo network for records uniquely belonging to a user.
|
|
365
|
-
|
|
366
|
-
The `ProgramManager` class is capable of taking a `KeyProvider` and `RecordProvider` as arguments and will use them to
|
|
367
|
-
find the correct keys and records for a program execution.
|
|
368
|
-
|
|
369
|
-
### 2.7 Deploy a new program to the Aleo Network
|
|
370
|
-
|
|
371
|
-
The Aleo network contains a public registry of programs that can be executed by anyone. Any user can add an Aleo program
|
|
372
|
-
to the network (as long as it doesn't already exist) by paying a deployment fee in Aleo credits. The SDK
|
|
373
|
-
provides a simple interface for deploying programs to the Aleo network using the program manager.
|
|
374
|
-
|
|
375
|
-
```typescript
|
|
376
|
-
import { Account, AleoNetworkClient, NetworkRecordProvider, ProgramManager, AleoKeyProvider} from '@provablehq/sdk';
|
|
377
|
-
|
|
378
|
-
// Create a key provider that will be used to find public proving & verifying keys for Aleo programs
|
|
379
|
-
const keyProvider = new AleoKeyProvider();
|
|
380
|
-
keyProvider.useCache(true);
|
|
381
|
-
|
|
382
|
-
// Create a record provider that will be used to find records and transaction data for Aleo programs
|
|
383
|
-
const networkClient = new AleoNetworkClient("https://api.explorer.provable.com/v1");
|
|
384
|
-
|
|
385
|
-
// Use existing account with funds
|
|
386
|
-
const account = new Account({
|
|
387
|
-
privateKey: "user1PrivateKey",
|
|
388
|
-
});
|
|
389
|
-
|
|
390
|
-
const recordProvider = new NetworkRecordProvider(account, networkClient);
|
|
391
|
-
|
|
392
|
-
// Initialize a program manager to talk to the Aleo network with the configured key and record providers
|
|
393
|
-
const programManager = new ProgramManager("https://api.explorer.provable.com/v1", keyProvider, recordProvider);
|
|
394
|
-
programManager.setAccount(account)
|
|
395
|
-
|
|
396
|
-
// Define an Aleo program to deploy
|
|
397
|
-
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";
|
|
398
|
-
|
|
399
|
-
// Define a fee to pay to deploy the program
|
|
400
|
-
const fee = 3.8; // 3.8 Aleo credits
|
|
401
|
-
|
|
402
|
-
// Deploy the program to the Aleo network
|
|
403
|
-
const tx_id = await programManager.deploy(program, fee);
|
|
404
|
-
|
|
405
|
-
// Verify the transaction was successful
|
|
406
|
-
const transaction = await programManager.networkClient.getTransaction(tx_id);
|
|
407
|
-
```
|
|
408
|
-
|
|
409
|
-
The `NetworkRecordProvider` will attempt to scan the network for a fee record for the account provided. Doing a recent
|
|
410
|
-
public transfer to the deploying account will ensure a record is found quickly, or you can provide a fee record manually
|
|
411
|
-
by [scanning](https://docs.leo-lang.org/testnet/getting_started/deploy_execute/#scan) for a record and passing it as a
|
|
412
|
-
string.
|
|
413
|
-
|
|
414
|
-
```typescript
|
|
415
|
-
// Set fee record manually
|
|
416
|
-
const feeRecord = "{ owner: aleo1xxx...xxx.private, microcredits: 2000000u64.private, _nonce: 123...789group.public}";
|
|
417
|
-
|
|
418
|
-
// Deploy the program to the Aleo network
|
|
419
|
-
const tx_id = await programManager.deploy(program, fee, undefined, feeRecord);
|
|
420
|
-
```
|
|
421
|
-
|
|
422
|
-
### 2.8 React example
|
|
423
|
-
|
|
424
|
-
The above concepts can be tied together in a concrete example of a single-page web app. This example can be installed in one
|
|
425
|
-
step by running:
|
|
426
|
-
|
|
427
|
-
`npm create leo-app@latest`
|
|
428
|
-
|
|
429
|
-
You will then be prompted to select either Vanilla, React, or Node.js as the template for the project. For this example, select Vanilla.
|
|
430
|
-
|
|
431
|
-
#### Program execution
|
|
432
|
-
|
|
433
|
-
Program execution is a computationally-expensive process. For this reason, it is recommended to execute programs in
|
|
434
|
-
web workers. Create-Leo-App will automatically create a web worker for you that performs the execution called `worker.js`.
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
A full example of this implementation can be found [here](https://github.com/ProvableHQ/sdk/tree/mainnet/create-leo-app/template-vanilla)
|
|
438
|
-
|
|
439
|
-
## 3. Aleo Credit Transfers
|
|
440
|
-
|
|
441
|
-
### 3.1 Aleo credits
|
|
442
|
-
|
|
443
|
-
Aleo Credits are used to access blockspace and computational resources on the network, with users paying Credits to submit transactions and have them processed.
|
|
444
|
-
|
|
445
|
-
Aleo credits are defined in the [credits.aleo](https://explorer.provable.com/program/credits.aleo) program. This program is
|
|
446
|
-
deployed to the Aleo network and defines data structures representing Aleo credits and the functions used to manage them.
|
|
447
|
-
|
|
448
|
-
There are two ways to hold Aleo credits:
|
|
449
|
-
|
|
450
|
-
#### 1 - Private balances via `credits.aleo` records
|
|
451
|
-
The first method is owning a `credits` record which enables a participant in the Aleo
|
|
452
|
-
network to hold a private balance of Aleo credits.
|
|
453
|
-
```
|
|
454
|
-
record credits:
|
|
455
|
-
owner as address.private;
|
|
456
|
-
microcredits as u64.private;
|
|
457
|
-
```
|
|
458
|
-
|
|
459
|
-
A user's total private credits balance will consist of all unspent `credits` records owned by the user with a non-zero
|
|
460
|
-
`microcredits` value.
|
|
461
|
-
|
|
462
|
-
#### 2 - Public balances via `credits.aleo` account mappings
|
|
463
|
-
The second method is by holding a `balance` in the `account` mapping in the `credits.aleo` program on the Aleo network.
|
|
464
|
-
|
|
465
|
-
```
|
|
466
|
-
mapping account:
|
|
467
|
-
key owner as address.public;
|
|
468
|
-
value microcredits as u64.public;
|
|
469
|
-
```
|
|
470
|
-
|
|
471
|
-
The total public credits balance of a user is the value of the account mapping at the user's address. Users can hold both private and public balances simultaneously.
|
|
472
|
-
|
|
473
|
-
More information about `records` and `mappings` and how they are related to private and public balances are explained in the
|
|
474
|
-
[Managing Program Data and Private State](#4-managing-program-data-and-private-state) section.
|
|
475
|
-
|
|
476
|
-
### 3.2 Transferring Aleo credits
|
|
477
|
-
The `ProgramManager` allows transfers of Aleo credits via the `transfer` method. This function executes the `credits.aleo`
|
|
478
|
-
program under the hood.
|
|
479
|
-
|
|
480
|
-
There are four transfer functions available.
|
|
481
|
-
|
|
482
|
-
#### 1. `transfer_private`
|
|
483
|
-
|
|
484
|
-
Takes a `credits` record owned by the sender, subtracts an amount from it, and adds that amount
|
|
485
|
-
to a new record owned by the receiver. This function is 100% private and does not affect the `account` mapping.
|
|
486
|
-
|
|
487
|
-
```mermaid
|
|
488
|
-
graph LR
|
|
489
|
-
user1--record1 \n owner: user1address \n balance: 10000u64-->t1[transfer_private]
|
|
490
|
-
user1--amount: 4000u64-->t1
|
|
491
|
-
t1-.record2 \n owner: user1address \n amount: 6000u64.->user1
|
|
492
|
-
t1--record3 \n owner: user2address \n balance: 4000u64-->user2
|
|
493
|
-
|
|
494
|
-
```
|
|
495
|
-
|
|
496
|
-
#### 2. `transfer_private_to_public`
|
|
497
|
-
|
|
498
|
-
Takes a `credits` record owned by the sender, subtracts an amount from it, and adds
|
|
499
|
-
that amount to the `account` mapping of the receiver. This function is 50% private and 50% public. It consumes a record
|
|
500
|
-
as a private input and generates a public balance in the `account` mapping entry belonging to the receiver.
|
|
501
|
-
|
|
502
|
-
```mermaid
|
|
503
|
-
graph LR
|
|
504
|
-
subgraph credits.aleo
|
|
505
|
-
m1[account mapping \n key: user3address \n value: 3000u64]
|
|
506
|
-
end
|
|
507
|
-
user1--record3 \n owner: user2address \n balance: 4000u64-->t1[transfer_private_to_public]
|
|
508
|
-
t1-.record4 \n owner: user2address \n amount: 1000u64.->user1
|
|
509
|
-
t1--amount 3000u64-->m1
|
|
510
|
-
```
|
|
511
|
-
|
|
512
|
-
#### 3. `transfer_public`
|
|
513
|
-
|
|
514
|
-
Subtracts an amount of `credits` stored in the `account` mapping of the `credits.aleo` program, and
|
|
515
|
-
adds that amount to the `account` mapping of the receiver. This function is 100% public and does not consume or generate
|
|
516
|
-
any records.
|
|
517
|
-
|
|
518
|
-
```mermaid
|
|
519
|
-
graph LR
|
|
520
|
-
subgraph credits.aleo account mappings - state 2
|
|
521
|
-
m3[account mapping \n key: user4address \n value: 3000u64]
|
|
522
|
-
m4[account mapping \n key: user3address \n value: 0u64]
|
|
523
|
-
end
|
|
524
|
-
|
|
525
|
-
subgraph credits.aleo account mappings - state 1
|
|
526
|
-
m2[account mapping \n key: user3address \n value: 3000u64]--transfer_public \n recipient: user4address \n amount: 3000u64-->m3
|
|
527
|
-
m1[account mapping \n key: user4address \n value: N/A]
|
|
528
|
-
end
|
|
529
|
-
```
|
|
530
|
-
|
|
531
|
-
#### 4. `transfer_public_to_private`
|
|
532
|
-
|
|
533
|
-
Subtracts an amount `credits` stored in the `account` mapping of the `credits.aleo program`
|
|
534
|
-
and adds that amount to a new private record owned by the receiver. This function is 50% private and 50% public.
|
|
535
|
-
It publicly consumes a balance in the `account` mapping entry belonging to the sender and generates a private record
|
|
536
|
-
as a private output.
|
|
537
|
-
|
|
538
|
-
```mermaid
|
|
539
|
-
graph LR
|
|
540
|
-
subgraph credits.aleo account mappings - state 2
|
|
541
|
-
m2[account mapping \n key: user5address \n value: 0u64]
|
|
542
|
-
end
|
|
543
|
-
|
|
544
|
-
subgraph credits.aleo account mappings - state 1
|
|
545
|
-
m1[account mapping \n key: user5address \n value: 3000u64]
|
|
546
|
-
end
|
|
547
|
-
|
|
548
|
-
m1--recipient: user6address \n amount: 3000u64-->transfer_public_to_private
|
|
549
|
-
transfer_public_to_private--record5 \n owner: user6address \n amount: 3000u64-->user6
|
|
550
|
-
```
|
|
551
|
-
|
|
552
|
-
All four of these functions can be used to transfer credits between users via the `transfer` function in the
|
|
553
|
-
`ProgramManager` by specifying the transfer type as the third argument.
|
|
554
|
-
|
|
555
|
-
```typescript
|
|
556
|
-
import { Account, ProgramManager, AleoKeyProvider, NetworkRecordProvider, AleoNetworkClient } from '@provablehq/sdk';
|
|
557
|
-
|
|
558
|
-
// Create a new NetworkClient, KeyProvider, and RecordProvider
|
|
559
|
-
const account = Account.from_string({privateKey: "user1PrivateKey"});
|
|
560
|
-
const networkClient = new AleoNetworkClient("https://api.explorer.provable.com/v1");
|
|
561
|
-
const keyProvider = new AleoKeyProvider();
|
|
562
|
-
const recordProvider = new NetworkRecordProvider(account, networkClient);
|
|
563
|
-
|
|
564
|
-
// Initialize a program manager with the key provider to automatically fetch keys for executions
|
|
565
|
-
const USER_1_ADDRESS = "user1Address";
|
|
566
|
-
const programManager = new ProgramManager("https://api.explorer.provable.com/v1", keyProvider, recordProvider);
|
|
567
|
-
programManager.setAccount(account);
|
|
568
|
-
|
|
569
|
-
// Send a private transfer to yourself
|
|
570
|
-
const tx_id = await programManager.transfer(1, USER_1_ADDRESS, "transfer_private", 0.2);
|
|
571
|
-
|
|
572
|
-
// Update or initialize a public balance in your own account mapping
|
|
573
|
-
const tx_id_2 = await programManager.transfer(1, USER_1_ADDRESS, "transfer_private_to_public", 0.2);
|
|
574
|
-
|
|
575
|
-
// Check the value of your public balance
|
|
576
|
-
let public_balance = programManager.networkClient.getMappingValue("credits.aleo", USER_1_ADDRESS);
|
|
577
|
-
assert(public_balance === 0.2*1_000_000);
|
|
578
|
-
|
|
579
|
-
/// Send public transfer to another user
|
|
580
|
-
const USER_2_ADDRESS = "user2Address";
|
|
581
|
-
const tx_id_3 = await programManager.transfer(1, USER_2_ADDRESS, "transfer_public", 0.1);
|
|
582
|
-
|
|
583
|
-
// Check the value of the public balance and assert that it has been updated
|
|
584
|
-
public_balance = programManager.networkClient.getMappingValue("credits.aleo", USER_1_ADDRESS);
|
|
585
|
-
const user2_public_balance = programManager.networkClient.getMappingValue("credits.aleo", USER_1_ADDRESS);
|
|
586
|
-
assert(public_balance === 0.1*1_000_000);
|
|
587
|
-
assert(user2_public_balance === 0.1*1_000_000);
|
|
588
|
-
|
|
589
|
-
/// Create a private record from a public balance
|
|
590
|
-
const tx_id_4 = await programManager.transfer(1, USER_1_ADDRESS, "transfer_public_to_private", 0.1);
|
|
591
|
-
|
|
592
|
-
// Check the value of the public balance and assert that it has been updated
|
|
593
|
-
public_balance = programManager.networkClient.getMappingValue("credits.aleo", USER_1_ADDRESS);
|
|
594
|
-
assert(public_balance === 0);
|
|
595
|
-
```
|
|
596
|
-
|
|
597
|
-
### 3.2 Checking public balances
|
|
598
|
-
As shown above, a public balance of any address can be checked with `getMappingValue` function of the `NetworkClient`.
|
|
599
|
-
|
|
600
|
-
```typescript
|
|
601
|
-
const networkClient = new AleoNetworkClient("https://api.explorer.provable.com/v1");
|
|
602
|
-
const USER_1_ADDRESS = "user1Address";
|
|
603
|
-
const public_balance = networkClient.getMappingValue("credits.aleo", USER_1_ADDRESS);
|
|
604
|
-
```
|
|
605
|
-
|
|
606
|
-
## 4. Managing Program Data and Private State
|
|
607
|
-
|
|
608
|
-
### 4.1 Private state data: records
|
|
609
|
-
Records are analogous to the concept of [UTXOs](https://en.wikipedia.org/wiki/Unspent_transaction_output). When a record is
|
|
610
|
-
created by a program, it can then be consumed later by the same program as an input to a function. Once a record is used
|
|
611
|
-
as an input, it is considered consumed and cannot be used again. In many cases a new record will be created from the output
|
|
612
|
-
of the function. Records are private by default and are associated with a single Aleo program and a single private key
|
|
613
|
-
representing a user.
|
|
614
|
-
|
|
615
|
-
### 4.2 Record usage example: private value transfers
|
|
616
|
-
|
|
617
|
-
A straightforward example of a usage of records in a program can be demonstrated by explaining the process of private
|
|
618
|
-
value transfers of Aleo credits on the Aleo network.
|
|
619
|
-
|
|
620
|
-
Aleo credits are used for all on-chain execution and deployment fees. Credits can be public
|
|
621
|
-
or private. Private credits are represented by the `credits` record in the [credits.aleo](https://www.aleo.network/programs/credits.aleo)
|
|
622
|
-
program.
|
|
623
|
-
|
|
624
|
-
```
|
|
625
|
-
record credits:
|
|
626
|
-
owner as address.private;
|
|
627
|
-
microcredits as u64.private;
|
|
628
|
-
```
|
|
629
|
-
|
|
630
|
-
Credits records contain an `owner` field representing the address which owns the record and a `microcredits` field
|
|
631
|
-
representing the amount of microcredits in the record. 1 credit is equal to 1,000,000 microcredits.
|
|
632
|
-
|
|
633
|
-
An example of an Aleo function that both takes a record as input and outputs a record is the `transfer_private` function
|
|
634
|
-
of the `credits.aleo` program. This function takes a private `credits` record as input and outputs two new private `credits`
|
|
635
|
-
records as output (one that sends the credits to the recipient and one that sends the remaining credits to the sender).
|
|
636
|
-
|
|
637
|
-
The source code for the `transfer_private` is:
|
|
638
|
-
```
|
|
639
|
-
function transfer_private:
|
|
640
|
-
input r0 as credits.record;
|
|
641
|
-
input r1 as address.private;
|
|
642
|
-
input r2 as u64.private;
|
|
643
|
-
sub r0.microcredits r2 into r3;
|
|
644
|
-
cast r1 r2 into r4 as credits.record;
|
|
645
|
-
cast r0.owner r3 into r5 as credits.record;
|
|
646
|
-
output r4 as credits.record;
|
|
647
|
-
output r5 as credits.record;
|
|
648
|
-
```
|
|
649
|
-
|
|
650
|
-
The `transfer_private` function can be graphically represented by the graph below. In the graph the first record, Record 1,
|
|
651
|
-
is consumed and can never be used again. From the data in Record 1, two more records are created. One containing
|
|
652
|
-
the intended amount for the recipient which is now owned by the recipient and another containing the remaining credits
|
|
653
|
-
which are sent back to the sender.
|
|
654
|
-
|
|
655
|
-
```mermaid
|
|
656
|
-
graph LR
|
|
657
|
-
User1[Sender Address]
|
|
658
|
-
i1[Input 1: Credits Record 1]-->p1
|
|
659
|
-
i2[Input 2: Recipient Address]-->p1
|
|
660
|
-
p1[Credits.aleo:transfer_private]
|
|
661
|
-
p1--Credits Record 2-->User1
|
|
662
|
-
p1--Credits Record 3-->R1[Recipient Address]
|
|
663
|
-
```
|
|
664
|
-
|
|
665
|
-
This chain of ownership is tracked by the Aleo blockchain when users choose to submit their transactions to the Aleo
|
|
666
|
-
network. This allows other users who receive records to receive the updated data and verify that this data was
|
|
667
|
-
provably generated by the intended program.
|
|
668
|
-
|
|
669
|
-
What this process allows is a private chain of state to be created between multiple users. In the context of value
|
|
670
|
-
transfers, a chain of state might look like the following:
|
|
671
|
-
|
|
672
|
-
```mermaid
|
|
673
|
-
graph LR
|
|
674
|
-
user1--record1-->t1[transfer_private]
|
|
675
|
-
t1-.record2.->user1
|
|
676
|
-
t1--record3-->user2
|
|
677
|
-
user2--record3-->t2[transfer_private]
|
|
678
|
-
t2--record5-->user3
|
|
679
|
-
t2-.record4.->user2
|
|
680
|
-
```
|
|
681
|
-
|
|
682
|
-
The above state chain would be executed in the following way using the SDK:
|
|
683
|
-
#### Step 1 - User 1 sends a private value transfer to User 2
|
|
684
|
-
```typescript
|
|
685
|
-
// USER 1
|
|
686
|
-
import { Account, ProgramManager, AleoKeyProvider, NetworkRecordProvider, AleoNetworkClient } from '@provablehq/sdk';
|
|
687
|
-
|
|
688
|
-
// Create a new NetworkClient, KeyProvider, and RecordProvider
|
|
689
|
-
const account = Account.from_string({privateKey: "user1PrivateKey"});
|
|
690
|
-
const networkClient = new AleoNetworkClient("https://api.explorer.provable.com/v1");
|
|
691
|
-
const keyProvider = new AleoKeyProvider();
|
|
692
|
-
const recordProvider = new NetworkRecordProvider(account, networkClient);
|
|
693
|
-
|
|
694
|
-
// Initialize a program manager with the key provider to automatically fetch keys for executions
|
|
695
|
-
const USER_2_ADDRESS = "user2Address";
|
|
696
|
-
const programManager = new ProgramManager("https://api.explorer.provable.com/v1", keyProvider, recordProvider);
|
|
697
|
-
programManager.setAccount(account);
|
|
698
|
-
|
|
699
|
-
/// Send private transfer to User 2
|
|
700
|
-
const tx_id = await programManager.transfer(1, USER_2_ADDRESS, "transfer_private", 0.2);
|
|
701
|
-
```
|
|
702
|
-
|
|
703
|
-
#### Step 2 - User 2 receives the transaction ID and fetches the credits record they received from User 1 from the network. They then send it to User 3
|
|
704
|
-
|
|
705
|
-
```typescript
|
|
706
|
-
// USER 2
|
|
707
|
-
import { Account, ProgramManager, AleoKeyProvider, NetworkRecordProvider, AleoNetworkClient } from '@provablehq/sdk';
|
|
708
|
-
|
|
709
|
-
// Create a new NetworkClient, KeyProvider, and RecordProvider
|
|
710
|
-
const account = Account.from_string({privateKey: "user2PrivateKey"});
|
|
711
|
-
const networkClient = new AleoNetworkClient("https://api.explorer.provable.com/v1");
|
|
712
|
-
const keyProvider = new AleoKeyProvider();
|
|
713
|
-
const recordProvider_User2 = new NetworkRecordProvider(account, networkClient);
|
|
714
|
-
|
|
715
|
-
// Initialize a program manager with the key provider to automatically fetch keys for executions
|
|
716
|
-
const programManager = new ProgramManager("https://api.explorer.provable.com/v1", keyProvider, recordProvider);
|
|
717
|
-
programManager.setAccount(account);
|
|
718
|
-
|
|
719
|
-
// Fetch the transaction from the network that user 1 sent
|
|
720
|
-
const transaction = await programManager.networkClient.getTransaction(tx_id);
|
|
721
|
-
const record = <string>transaction.execution.transitions[0].outputs[0].value;
|
|
722
|
-
|
|
723
|
-
// Decrypt the record with the user's view key
|
|
724
|
-
const recordCiphertext = <RecordCiphertext>RecordCiphertext.fromString(record);
|
|
725
|
-
const recordPlaintext = <RecordPlaintext>recordCiphertext.decrypt(account.viewKey());
|
|
726
|
-
|
|
727
|
-
// Send a transfer to user 3 using the record found above
|
|
728
|
-
const USER_3_ADDRESS = "user3Address";
|
|
729
|
-
const tx_id = await programManager.transfer(1, USER_3_ADDRESS, "transfer_private", 0.2, undefined, recordPlaintext);
|
|
730
|
-
```
|
|
731
|
-
|
|
732
|
-
When an execution such as `transfer_private` consumes or generates a record, an encrypted transcript of the execution containing an encrypted version of the record output and a transaction ID is posted on the network.
|
|
733
|
-
|
|
734
|
-
Because the records are encrypted when they're posted on the network, they do not reveal any information about the party
|
|
735
|
-
who executed the program, nor the contents of the record. The only information that is revealed is the program ID,
|
|
736
|
-
function name, encrypted function inputs, and the transaction ID of the program execution. No user except for the recipient
|
|
737
|
-
of the record can see the contents of the record.
|
|
738
|
-
|
|
739
|
-
Below, you can see the exact data which is posted to the Aleo network when `transfer_private` is run. Note that the
|
|
740
|
-
record, the amount transferred, and both the sender and recipient addresses are all encrypted.
|
|
741
|
-
|
|
742
|
-
<details>
|
|
743
|
-
<summary>`transfer_private` Execution Transcript</summary>
|
|
744
|
-
|
|
745
|
-
```json
|
|
746
|
-
"transactions": [
|
|
747
|
-
{
|
|
748
|
-
"status": "accepted",
|
|
749
|
-
"type": "execute",
|
|
750
|
-
"index": 0,
|
|
751
|
-
"transaction": {
|
|
752
|
-
"type": "execute",
|
|
753
|
-
"id": "at1s7dxunms8xhdzgaxrwf0yvq2dqgxtf4a3j8g878rhfr0zwhap5gqywsw8y",
|
|
754
|
-
"execution": {
|
|
755
|
-
"transitions": [
|
|
756
|
-
{
|
|
757
|
-
"id": "as1thy8fvkz0rkls5wnmfq5udrcvvzurq7mqk8pkhjf63htqjf9mugqp0mfhd",
|
|
758
|
-
"program": "credits.aleo",
|
|
759
|
-
"function": "transfer_private",
|
|
760
|
-
"inputs": [
|
|
761
|
-
{
|
|
762
|
-
"type": "record",
|
|
763
|
-
"id": "1406044754369042876058586523429806531093330762697573675195902502647806778955field",
|
|
764
|
-
"tag": "242626059121157295593694555515381893342956813170338731374395259242800138642field"
|
|
765
|
-
},
|
|
766
|
-
{
|
|
767
|
-
"type": "private",
|
|
768
|
-
"id": "1533599744296862879610225011439684001995294756698105572984689232395187168232field",
|
|
769
|
-
"value": "ciphertext1qgqgpu7m8p0rwjahwffyvm4g4n6903d6ufqty74z4504w4rn356hgp9jvpuvx8suu0pukr3sl7n8x65dz35nu4jdy4lgcguxldygufrfpyqd6xr5"
|
|
770
|
-
},
|
|
771
|
-
{
|
|
772
|
-
"type": "private",
|
|
773
|
-
"id": "4081557229261486898857101724786348855190759711760925564309233047223407640812field",
|
|
774
|
-
"value": "ciphertext1qyqxd9wue0qh8hs6dgevn7zleedfkzf7pft8ecked2xq3pw54pgqzyqr69sgx"
|
|
775
|
-
}
|
|
776
|
-
],
|
|
777
|
-
"outputs": [
|
|
778
|
-
{
|
|
779
|
-
"type": "record",
|
|
780
|
-
"id": "1388064668770056715587596299070268626507043043686185311840561493640415146425field",
|
|
781
|
-
"checksum": "5376939704883651492329501631722578074516322228314928758786996843926470523116field",
|
|
782
|
-
"value": "record1qyqsq4r7mcd3ystjvjqda0v2a6dxnyzg9mk2daqjh0wwh359h396k7c9qyxx66trwfhkxun9v35hguerqqpqzqzshsw8dphxlzn5frh8pknsm5zlvhhee79xnhfesu68nkw75dt2qgrye03xqm4zf5xg5n6nscmmzh7ztgptlrzxq95syrzeaqaqu3vpzqf03s6"
|
|
783
|
-
},
|
|
784
|
-
{
|
|
785
|
-
"type": "record",
|
|
786
|
-
"id": "4635504195534945632234501197115926012056789160185660629718795843347495373207field",
|
|
787
|
-
"checksum": "3428805926164481449334365355155755448945974546383155334133384781819684465685field",
|
|
788
|
-
"value": "record1qyqsp2vsvvfulmk0q0tmxq7p9pffhfhha9h9pxsftujh57kkjuahx9s0qyxx66trwfhkxun9v35hguerqqpqzq8etfmzt2elj37hkf9fen2m2qes8564sr8k970zyud5eqmq7ztzq5r3095mkfdzqzz7yp6qfavqsl3t22t6dvgauqqt2xqk98zwmtusq5ck7fm"
|
|
789
|
-
}
|
|
790
|
-
],
|
|
791
|
-
"tpk": "5283803395323806407328334221689294196419052177553228331323093330938016699852group",
|
|
792
|
-
"tcm": "4398026033398688325681745841147300822741685834906186660771751747897598751646field"
|
|
793
|
-
}
|
|
794
|
-
],
|
|
795
|
-
```
|
|
796
|
-
</details>
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
#### Record Decryption
|
|
800
|
-
|
|
801
|
-
If a user receives a private record from a program execution, they can use the SDK to decrypt encrypted records with
|
|
802
|
-
their view keys and view their contents. Only records that are owned by the user can be decrypted. Decryption of records
|
|
803
|
-
that are not owned by the user will fail.
|
|
804
|
-
|
|
805
|
-
Record decryption and ownership verification can be done in the SDK using the following code:
|
|
806
|
-
|
|
807
|
-
```typescript
|
|
808
|
-
import { Account, RecordCiphertext, RecordPlaintext } from '@provablehq/sdk';
|
|
809
|
-
|
|
810
|
-
// Create an account from an existing private key
|
|
811
|
-
const account = Account.from_string({privateKey: "existingPrivateKey"});
|
|
812
|
-
|
|
813
|
-
// Record value received as a string from program output or found on the Aleo network
|
|
814
|
-
const record = "record1qyqsq4r7mcd3ystjvjqda0v2a6dxnyzg9mk2daqjh0wwh359h396k7c9qyxx66trwfhkxun9v35hguerqqpqzqzshsw8dphxlzn5frh8pknsm5zlvhhee79xnhfesu68nkw75dt2qgrye03xqm4zf5xg5n6nscmmzh7ztgptlrzxq95syrzeaqaqu3vpzqf03s6";
|
|
815
|
-
|
|
816
|
-
const recordCiphertext = RecordCiphertext.fromString(record);
|
|
817
|
-
|
|
818
|
-
// Check ownership of the record. If the account is the owner, decrypt the record
|
|
819
|
-
if (RecordCiphertext.is_owner(account.viewKey())) {
|
|
820
|
-
// Decrypt the record with the account's view key
|
|
821
|
-
const recordPlaintext = recordCiphertext.decrypt(account.viewKey());
|
|
822
|
-
|
|
823
|
-
// View the record data
|
|
824
|
-
console.log(recordPlaintext.toString());
|
|
825
|
-
}
|
|
826
|
-
```
|
|
827
|
-
|
|
828
|
-
### 4.3 Public State Data: Mappings
|
|
829
|
-
Mappings are simple key value stores defined in a program. They are represented by a key and a value each of a specified
|
|
830
|
-
type. They are stored directly within the Aleo blockchain and can be publicly read by any participant in the Aleo network.
|
|
831
|
-
|
|
832
|
-
An example of a mapping usage is the `account` mapping in the `credits.aleo` program.
|
|
833
|
-
```
|
|
834
|
-
mapping account:
|
|
835
|
-
key owner as address.public;
|
|
836
|
-
value microcredits as u64.public;
|
|
837
|
-
```
|
|
838
|
-
|
|
839
|
-
The `account` mapping is used to store public credit balances on the Aleo network. It takes a public address as a key
|
|
840
|
-
and a public `u64` value representing the number of microcredits owned by the address.
|
|
841
|
-
|
|
842
|
-
Mappings within programs are identified by the `mapping` identifier. Any program where this keyword appears contains an
|
|
843
|
-
on-chain mapping. An example of a program that uses a mapping is shown below:
|
|
844
|
-
```
|
|
845
|
-
program player_mapping_example.aleo
|
|
846
|
-
|
|
847
|
-
// The mapping identifier representing a score
|
|
848
|
-
mapping score:
|
|
849
|
-
key player as address.public;
|
|
850
|
-
value score as u64.public;
|
|
851
|
-
|
|
852
|
-
// The update score function
|
|
853
|
-
function update_score:
|
|
854
|
-
input r0 as address.public;
|
|
855
|
-
input r1 as u64.public;
|
|
856
|
-
|
|
857
|
-
// The finalize code block will be executed by Aleo network nodes.
|
|
858
|
-
// When it runs it will update the value of the mapping.
|
|
859
|
-
finalize update_score:
|
|
860
|
-
input r0 as address.public;
|
|
861
|
-
input r1 as u64.public;
|
|
862
|
-
get.or_use score[r0] 0u64 into r2;
|
|
863
|
-
add r1 r2 into r3;
|
|
864
|
-
set r3 into account[r0];
|
|
865
|
-
```
|
|
866
|
-
|
|
867
|
-
Note that the above function has a `finalize` identifier. This identifier is used to identify a portion of a function's
|
|
868
|
-
code that should be executed by nodes on the Aleo network. Program mappings are updated exclusively by code run by nodes
|
|
869
|
-
on the Aleo network written in `finalize` blocks.
|
|
870
|
-
|
|
871
|
-
### 4.4 Reading mappings
|
|
872
|
-
Any state within a program mapping is public and can be read by any participant in the Aleo network. The `NetworkClient`
|
|
873
|
-
class provides the `getMapping` method to read the public mappings within an program and the `getMappingValue` method to
|
|
874
|
-
read the value of a specific key within a mapping.
|
|
875
|
-
|
|
876
|
-
```typescript
|
|
877
|
-
import { AleoNetworkClient } from '@provablehq/sdk';
|
|
878
|
-
|
|
879
|
-
const networkClient = new AleoNetworkClient("https://api.explorer.provable.com/v1");
|
|
880
|
-
const creditsMappings = networkClient.getMappings("credits.aleo");
|
|
881
|
-
assert(creditsMappings === ["account"]);
|
|
882
|
-
|
|
883
|
-
const publicCredits = networkClient.getMapping("credits.aleo", "[a valid aleo account with zero balance]");
|
|
884
|
-
assert(publicCredits === "0u64");
|
|
885
|
-
```
|
|
886
|
-
|
|
887
|
-
### 4.5 Initializing & updating mappings
|
|
888
|
-
Updating mappings is done by executing a program function on the Aleo network which has a finalize block that updates the
|
|
889
|
-
program's mapping. For instance, the `transfer_public` function in the `credits.aleo` program updates the `account`
|
|
890
|
-
mapping (and thus a user's balance) when called.
|
|
891
|
-
|
|
892
|
-
```
|
|
893
|
-
// The public interface called by users
|
|
894
|
-
function transfer_public:
|
|
895
|
-
input r0 as address.public;
|
|
896
|
-
input r1 as u64.public;
|
|
897
|
-
finalize self.caller r0 r1;
|
|
898
|
-
|
|
899
|
-
// The finalize block run by nodes on the Aleo network which update a user's public balance
|
|
900
|
-
finalize transfer_public:
|
|
901
|
-
input r0 as address.public;
|
|
902
|
-
input r1 as address.public;
|
|
903
|
-
input r2 as u64.public;
|
|
904
|
-
get.or_use account[r0] 0u64 into r3;
|
|
905
|
-
sub r3 r2 into r4;
|
|
906
|
-
set r4 into account[r0];
|
|
907
|
-
get.or_use account[r1] 0u64 into r5;
|
|
908
|
-
add r5 r2 into r6;
|
|
909
|
-
set r6 into account[r1];
|
|
910
|
-
```
|
|
911
|
-
|
|
912
|
-
From the perspective of the caller of the API, this is as simple as executing a normal Aleo function. Given the inputs
|
|
913
|
-
to a function with a finalize scope that updates a mapping are valid, the mapping will either be intialized or updated
|
|
914
|
-
by the Aleo network. All that the user of the SDK must do is ensure that the inputs to the function are valid.
|
|
915
|
-
|
|
916
|
-
If function inputs are invalid, the network will return an error, but the fee paid for the transaction will still be
|
|
917
|
-
consumed. Therefore, it is important to ensure that the inputs to a function are valid before executing it.
|
|
918
|
-
|
|
919
|
-
A simple example of a mapping update can be shown by simply executing `transfer_public` as shown below.
|
|
920
|
-
|
|
921
|
-
```typescript
|
|
922
|
-
import { Account, ProgramManager, AleoKeyProvider, NetworkRecordProvider, AleoNetworkClient } from '@provablehq/sdk';
|
|
36
|
+
2. [Aleo cryptographic primitives](https://provable.tools/algebra)
|
|
37
|
+
3. [Web based zero knowledge program execution and deployment](https://provable.tools/develop)
|
|
38
|
+
4. [Aleo credit transfers and staking](https://provable.tools/transfer)
|
|
39
|
+
5. [Management of program state](https://provable.tools/protocol)
|
|
40
|
+
6. [Communication with the Aleo network](https://provable.tools/rest)
|
|
923
41
|
|
|
924
|
-
// Create a new NetworkClient, KeyProvider, and RecordProvider
|
|
925
|
-
const account = Account.from_string({privateKey: "user1PrivateKey"});
|
|
926
|
-
const networkClient = new AleoNetworkClient("https://api.explorer.provable.com/v1");
|
|
927
|
-
const keyProvider = new AleoKeyProvider();
|
|
928
|
-
const recordProvider = new NetworkRecordProvider(account, networkClient);
|
|
929
42
|
|
|
930
|
-
|
|
931
|
-
const RECIPIENT_ADDRESS = "user1Address";
|
|
932
|
-
const programManager = new ProgramManager("https://api.explorer.provable.com/v1", keyProvider, recordProvider);
|
|
933
|
-
programManager.setAccount(account);
|
|
43
|
+
## Start Building
|
|
934
44
|
|
|
935
|
-
|
|
936
|
-
const tx_id = await programManager.transfer(1, RECIPIENT_ADDRESS, "transfer_private_to_public", 0.2);
|
|
937
|
-
```
|
|
45
|
+
Developers interested in using the SDK to build private web applications can get started with the following resources.
|
|
938
46
|
|
|
47
|
+
### [The SDK Guide](https://docs.explorer.provable.com/docs/sdk/6p7047svvq2ox-intro-to-aleo)
|
|
48
|
+
<a href="https://www.npmjs.com/package/@provablehq/sdk"> <img alt="Aleo SDK" src="https://img.shields.io/npm/l/%40provablehq%2Fsdk?label=NPM%20-%20Aleo%20SDK&labelColor=green&color=blue"></a>
|
|
939
49
|
|
|
940
|
-
|
|
50
|
+
A step-by-step guide to building private web applications with the Aleo SDK. The guide covers creating Aleo accounts,
|
|
51
|
+
transferring and receiving aleo credits, executing/deploying/interacting with Aleo programs, managing private and public
|
|
52
|
+
state, and best practices for building private web applications.
|
|
941
53
|
|
|
942
|
-
|
|
943
|
-
data from Aleo network nodes and submit transactions to the Aleo network.
|
|
54
|
+
### [Create-Leo-App](https://docs.leo-lang.org/sdk/create-leo-app/tutorial)
|
|
944
55
|
|
|
945
|
-
|
|
946
|
-
[Network Client API documentation](https://docs.leo-lang.org/sdk/typescript/aleo_network_client).
|
|
56
|
+
<a href="https://www.npmjs.com/package/create-leo-app"> <img alt="Create Leo App" src="https://img.shields.io/npm/l/create-leo-app?label=NPM%20-%20Create-Leo-App&labelColor=green&color=blue"></a>
|
|
947
57
|
|
|
948
|
-
|
|
58
|
+
A suite of examples and templates for building private web applications in a variety of Javascript frameworks. These
|
|
59
|
+
examples can be easily installed and run using the `create-leo-app` command line tool. The examples cover a wide range
|
|
60
|
+
of use cases such as Private NFTs, offline transaction signing, and usage of the SDK in React, Next, and VanillaJS.
|
|
949
61
|
|
|
950
|
-
|
|
62
|
+
### [SDK API Documentation](https://docs.explorer.provable.com/docs/sdk/0qgi3uyhotv62-account)
|
|
63
|
+
For developers who prefer to dive straight into the code, the SDK API documentation provides a comprehensive reference
|
|
64
|
+
for all the SDK's classes, methods, and interfaces. The documentation is generated from the SDK's TypeScript source code.
|
|
951
65
|
|
|
952
|
-
|
|
953
|
-
|
|
66
|
+
### [Leo Language Documentation](https://docs.leo-lang.org)
|
|
67
|
+
For developers who want to build their own zero knowledge programs, the Leo Language provides an easy-to-use imperative
|
|
68
|
+
programming language for writing zero knowledge programs on Aleo. In conjuction with the SDK, private programs built
|
|
69
|
+
using Leo can be turned into fully functional private web applications.
|