@provablehq/sdk 0.6.9 → 0.6.10
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 +112 -107
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -10,18 +10,18 @@
|
|
|
10
10
|
<a href="https://github.com/ProvableHQ/sdk#%EF%B8%8F-contributors"><img src="https://img.shields.io/badge/contributors-23-ee8449"/></a>
|
|
11
11
|
</p>
|
|
12
12
|
|
|
13
|
-
## Tools for Building Zero
|
|
13
|
+
## Tools for Building Zero-Knowledge Web Apps
|
|
14
14
|
|
|
15
|
-
The Aleo SDK is a collection of JavaScript libraries for building zero
|
|
16
|
-
and
|
|
15
|
+
The Aleo SDK is a collection of JavaScript libraries for building zero-knowledge web applications in both the browser
|
|
16
|
+
and Node.js.
|
|
17
17
|
|
|
18
18
|
## Overview
|
|
19
19
|
|
|
20
|
-
Aleo provides the ability to run programs
|
|
21
|
-
within the browser and all other levels of the web stack to build privacy
|
|
20
|
+
Aleo provides the ability to run programs with the power of zero-knowledge. The Aleo SDK provides the tools to use these programs
|
|
21
|
+
within the browser and all other levels of the web stack to build privacy-preserving applications.
|
|
22
22
|
|
|
23
23
|
The Aleo SDK provides the following functionality (Click to see examples):
|
|
24
|
-
1. [Aleo account
|
|
24
|
+
1. [Aleo account management](https://provable.tools/account)
|
|
25
25
|
2. [Web-based program execution and deployment](https://provable.tools/develop)
|
|
26
26
|
3. [Aleo credit transfers](https://provable.tools/transfer)
|
|
27
27
|
4. [Management of program state and data](https://provable.tools/record)
|
|
@@ -31,7 +31,7 @@ The Aleo SDK provides the following functionality (Click to see examples):
|
|
|
31
31
|
|
|
32
32
|
* [Installation](#Installation)
|
|
33
33
|
* [Usage](#Usage)
|
|
34
|
-
* [Zero
|
|
34
|
+
* [Zero-Knowledge Web App Examples](#Zero-Knowledge-Web-App-Examples)
|
|
35
35
|
* [Create Aleo App](#create-aleo-app)
|
|
36
36
|
* [Provable.tools](#provabletools)
|
|
37
37
|
* [Create An Aleo Account](#1-create-an-aleo-account)
|
|
@@ -58,54 +58,60 @@ The Aleo SDK provides the following functionality (Click to see examples):
|
|
|
58
58
|
* [Further Documentation](#further-documentation)
|
|
59
59
|
## Installation
|
|
60
60
|
|
|
61
|
+
### Clone the repository
|
|
62
|
+
|
|
63
|
+
To clone the repository, run:
|
|
64
|
+
|
|
65
|
+
`git clone git@github.com:ProvableHQ/sdk.git`
|
|
66
|
+
|
|
61
67
|
### NPM
|
|
62
68
|
|
|
63
69
|
To install the Aleo SDK from NPM run:
|
|
64
70
|
|
|
65
|
-
`npm install @provablehq/sdk` or `yarn add @provablehq/sdk`.
|
|
71
|
+
`npm install @provablehq/sdk` or `cd sdk && yarn add @provablehq/sdk`.
|
|
66
72
|
|
|
67
73
|
### Build from source
|
|
68
74
|
|
|
69
75
|
To build the project from source, go to this project's root and execute:
|
|
70
76
|
|
|
71
|
-
`
|
|
77
|
+
`yarn build:all`
|
|
72
78
|
|
|
73
|
-
## Getting Started: Zero
|
|
79
|
+
## Getting Started: Zero-Knowledge Web App Examples
|
|
74
80
|
|
|
75
81
|
### Create Aleo App
|
|
76
|
-
A set of fully functional examples of zero
|
|
77
|
-
[create-aleo-app](https://github.com/ProvableHQ/sdk/tree/testnet3/create-aleo-app). Create
|
|
78
|
-
templates in common web frameworks such as React that can be used as a starting point for building zero
|
|
82
|
+
A set of fully functional examples of zero-knowledge web apps can be found in
|
|
83
|
+
[create-aleo-app](https://github.com/ProvableHQ/sdk/tree/testnet3/create-aleo-app). Create Aleo App provides several web app
|
|
84
|
+
templates in common web frameworks such as React that can be used as a starting point for building zero-knowledge web apps.
|
|
79
85
|
|
|
80
|
-
Developers can get started immediately with create-
|
|
86
|
+
Developers can get started immediately with create-aleo-app by running:
|
|
81
87
|
`npm create aleo-app@latest`
|
|
82
88
|
|
|
83
|
-
###
|
|
89
|
+
### provable.tools
|
|
84
90
|
|
|
85
91
|
Additionally, the SDK powers [provable.tools](https://provable.tools) - a React app that provides a graphical interface for most
|
|
86
92
|
of the functionality provided by the SDK and can be used as a reference for usage of the SDK. Source code for provable.tools
|
|
87
|
-
can be found [in the SDK repo here](https://github.com/ProvableHQ/sdk/tree/testnet3/website)
|
|
93
|
+
can be found [in the SDK repo here](https://github.com/ProvableHQ/sdk/tree/testnet3/website).
|
|
88
94
|
|
|
89
95
|
## Usage
|
|
90
96
|
|
|
91
97
|
## 1. Create an Aleo Account
|
|
92
98
|
|
|
93
|
-
The first step in operating a zero
|
|
99
|
+
The first step in operating a zero-knowledge web application is creating a private key which serves as a cryptographic
|
|
94
100
|
identity for a user. After a private key is generated, several keys that enable specialized methods of interacting with
|
|
95
101
|
Aleo programs can be derived.
|
|
96
102
|
|
|
97
103
|
These keys include:
|
|
98
104
|
#### Private Key
|
|
99
|
-
The `Private Key` can be treated as the identity of a user. It is used for critical functions like authorizing zero
|
|
100
|
-
program execution, decrypting private data, and proving ownership of data.
|
|
101
|
-
|
|
105
|
+
The `Private Key` can be treated as the identity of a user. It is used for critical functions like authorizing zero-knowledge
|
|
106
|
+
program execution, decrypting private data, and proving ownership of user data.
|
|
107
|
+
|
|
102
108
|
#### View Key
|
|
103
109
|
The `View Key` is derived from the private key and can be used to both decrypt encrypted data owned by a user and prove
|
|
104
110
|
ownership of that data.
|
|
105
111
|
#### Compute Key
|
|
106
112
|
The `Compute Key` can be used to trustlessly run applications and generate transactions on a user's behalf.
|
|
107
113
|
#### Address
|
|
108
|
-
The `Address` is a user's unique public identifier. It serves as an address for a user to receive both
|
|
114
|
+
The `Address` is a user's unique public identifier. It serves as an address for a user to receive both Aleo
|
|
109
115
|
credits and data from other zero-knowledge Aleo programs.
|
|
110
116
|
|
|
111
117
|
All of these keys can be created using the account object:
|
|
@@ -129,16 +135,15 @@ Please note that all keys are considered sensitive information and should be sto
|
|
|
129
135
|
Aleo programs provide the ability for users to make any input or output of a program private and prove that the program
|
|
130
136
|
was run correctly. Keeping program inputs and outputs private allows developers to build privacy into their applications.
|
|
131
137
|
|
|
132
|
-
Zero-
|
|
133
|
-
1. [Leo](https://
|
|
134
|
-
zero
|
|
135
|
-
2. [Aleo Instructions](https://
|
|
136
|
-
grained control over the execution flow of zero knowledge programs. Leo programs are compiled into Aleo Instructions
|
|
138
|
+
Zero-knowledge programs are written in one of two languages:
|
|
139
|
+
1. [Leo](https://docs.leo-lang.org/leo/language): A high-level, developer friendly language for developing
|
|
140
|
+
zero-knowledge programs.
|
|
141
|
+
2. [Aleo Instructions](https://docs.leo-lang.org/aleo): A low-level language that provides developers with fine-grained control over the execution flow of zero-knowledge programs. Leo programs are compiled into Aleo Instructions
|
|
137
142
|
under the hood.
|
|
138
143
|
|
|
139
144
|
Documentation for both languages can be found at [developer.aleo.org](https://developer.aleo.org).
|
|
140
145
|
|
|
141
|
-
#### Hello
|
|
146
|
+
#### "Hello World" in Leo
|
|
142
147
|
```
|
|
143
148
|
// A simple program adding two numbers together
|
|
144
149
|
program helloworld.aleo {
|
|
@@ -149,11 +154,11 @@ program helloworld.aleo {
|
|
|
149
154
|
}
|
|
150
155
|
```
|
|
151
156
|
|
|
152
|
-
#### Hello
|
|
157
|
+
#### "Hello World" in Aleo Instructions
|
|
153
158
|
```
|
|
154
159
|
program helloworld.aleo;
|
|
155
160
|
|
|
156
|
-
// The leo code above compiles to the following Aleo
|
|
161
|
+
// The leo code above compiles to the following Aleo Instructions
|
|
157
162
|
function hello:
|
|
158
163
|
input r0 as u32.public;
|
|
159
164
|
input r1 as u32.private;
|
|
@@ -163,21 +168,21 @@ function hello:
|
|
|
163
168
|
|
|
164
169
|
### 2.2 Program Execution Model
|
|
165
170
|
|
|
166
|
-
The SDK provides the ability execute
|
|
171
|
+
The SDK provides the ability to execute Aleo programs 100% client-side within the browser.
|
|
167
172
|
|
|
168
|
-
The `ProgramManager` object encapsulates the functionality for executing programs and making zero
|
|
173
|
+
The `ProgramManager` object encapsulates the functionality for executing programs and making zero-knowledge proofs about
|
|
169
174
|
them. Under the hood it uses cryptographic code compiled from [SnarkVM](https://developer.aleo.org/aleo) into WebAssembly
|
|
170
|
-
with JavaScript bindings that allow execution of Aleo programs fully within the browser. Users interested in lower
|
|
171
|
-
details how this is achieved can visit the [aleo-wasm](https://
|
|
175
|
+
with JavaScript bindings that allow for the execution of Aleo programs fully within the browser. Users interested in lower-level
|
|
176
|
+
details on how this is achieved can visit the [aleo-wasm](https://www.npmjs.com/package/@provablehq/wasm) crate.
|
|
172
177
|
|
|
173
178
|
The basic execution flow of a program is as follows:
|
|
174
179
|
1. A web app is loaded with an instance of the `ProgramManager` object
|
|
175
180
|
2. The SDK wasm modules are loaded into the browser's WebAssembly runtime
|
|
176
181
|
2. An Aleo program in `Aleo Instructions` format is loaded into the `ProgramManager` as a wasm object
|
|
177
182
|
3. The web app provides a user input form for the program
|
|
178
|
-
4. The user submits the inputs and the zero
|
|
183
|
+
4. The user submits the inputs and the zero-knowledge execution is performed entirely within the browser via WebAssembly
|
|
179
184
|
5. The result is returned to the user
|
|
180
|
-
6.
|
|
185
|
+
6. A fully encrypted zero-knowledge transcript of the execution is optionally sent to the Aleo network
|
|
181
186
|
|
|
182
187
|
A diagramatic representation of the program execution flow is shown below.
|
|
183
188
|
```mermaid
|
|
@@ -198,14 +203,14 @@ graph LR
|
|
|
198
203
|
in-memory-program-."ZK result (Optional)".->Aleo-Network
|
|
199
204
|
```
|
|
200
205
|
|
|
201
|
-
### 2.3
|
|
206
|
+
### 2.3 Multithreading
|
|
202
207
|
|
|
203
|
-
You can enable
|
|
208
|
+
You can enable multithreading by calling the `initThreadPool` function. This will run the SDK on multiple workers, which significantly speeds up performance:
|
|
204
209
|
|
|
205
210
|
```typescript
|
|
206
211
|
import { Account, initThreadPool } from '@provablehq/sdk';
|
|
207
212
|
|
|
208
|
-
// Enables
|
|
213
|
+
// Enables multithreading
|
|
209
214
|
await initThreadPool();
|
|
210
215
|
|
|
211
216
|
// Create a new Aleo account
|
|
@@ -214,8 +219,9 @@ const account = new Account();
|
|
|
214
219
|
// Perform further program logic...
|
|
215
220
|
````
|
|
216
221
|
|
|
217
|
-
### 2.4 Local
|
|
218
|
-
|
|
222
|
+
### 2.4 Local program execution
|
|
223
|
+
|
|
224
|
+
A simple example of running the "hello world" program within the web browser and capturing its outputs is shown below:
|
|
219
225
|
```typescript
|
|
220
226
|
import { Account, Program } from '@provablehq/sdk';
|
|
221
227
|
|
|
@@ -233,11 +239,11 @@ const result = executionResponse.getOutputs();
|
|
|
233
239
|
assert(result === ["10u32"]);
|
|
234
240
|
```
|
|
235
241
|
|
|
236
|
-
### 2.5 Program execution on the Aleo
|
|
242
|
+
### 2.5 Program execution on the Aleo network
|
|
237
243
|
The SDK provides the ability to execute programs and store an encrypted transcript of the execution on the Aleo
|
|
238
244
|
network that anyone can trustlessly verify.
|
|
239
245
|
|
|
240
|
-
This process can be thought of being executed in the following steps:
|
|
246
|
+
This process can be thought of as being executed in the following steps:
|
|
241
247
|
1. A program is run locally
|
|
242
248
|
2. A proof that the program was executed correctly and that the outputs follow from the inputs is generated
|
|
243
249
|
3. A transcript of the proof is generated client-side containing encrypted proof data (see [Section 2.6](#4-managing-records-and-private-state))
|
|
@@ -245,12 +251,12 @@ and any public outputs or state the user of the program wishes to reveal
|
|
|
245
251
|
4. The proof transcript is posted to the Aleo network and verified by the Aleo validator nodes in a trustless manner
|
|
246
252
|
5. If the proof is valid, it is stored and anyone can later verify the proof and read the outputs the author of the
|
|
247
253
|
program has chosen to make public. Private inputs will remain encrypted, but the author of the proof can also choose to
|
|
248
|
-
retrieve this encrypted state at any point and decrypt it locally for their own use
|
|
254
|
+
retrieve this encrypted state at any point and decrypt it locally for their own use
|
|
249
255
|
|
|
250
|
-
Posting an execution to the Aleo
|
|
256
|
+
Posting an execution to the Aleo network serves as a globally trustless and verifiable record of a program execution as well as
|
|
251
257
|
any resulting state changes in private or public data.
|
|
252
258
|
|
|
253
|
-
A simple example of running the hello world program on the Aleo network is shown below:
|
|
259
|
+
A simple example of running the "hello world" program on the Aleo network is shown below:
|
|
254
260
|
```typescript
|
|
255
261
|
import { Account, AleoNetworkClient, NetworkRecordProvider, ProgramManager, KeySearchParams } from '@provablehq/sdk';
|
|
256
262
|
|
|
@@ -282,32 +288,32 @@ const transaction = await programManager.networkClient.getTransaction(tx_id);
|
|
|
282
288
|
|
|
283
289
|
A reader of the above example may notice the `RecordProvider` and `KeyProvider` classes that were not present in the local
|
|
284
290
|
execution example. The `KeyProvider` class helps users of the SDK find `Proving Keys` for programs. `Proving Keys`
|
|
285
|
-
allow zero
|
|
291
|
+
allow zero-knowledge proofs that the programs were executed correctly to be created. The `RecordProvider` class helps
|
|
286
292
|
find `Records` which are private data associated with programs that can be changed and updated throughout time.
|
|
287
293
|
These two concepts are explained in more detail below.
|
|
288
294
|
|
|
289
|
-
### 2.6 Program
|
|
295
|
+
### 2.6 Program proving keys & program records
|
|
290
296
|
|
|
291
|
-
Executing Aleo programs in zero
|
|
297
|
+
Executing Aleo programs in zero-knowledge requires two additional pieces of information:
|
|
292
298
|
|
|
293
299
|
1. **Function Proving & Verifying Keys:** Proving and Verifying keys are cryptographic keys that are generated when a
|
|
294
|
-
program function is executed. These keys are public and unique for each function in a program. The proving
|
|
300
|
+
program function is executed. These keys are public and unique for each function in a program. The proving keys allows any party to
|
|
295
301
|
execute the program and generate a proof that the program was executed correctly. The verifying keys allow any party
|
|
296
302
|
to verify that the proof was generated correctly and the execution is correct. These keys are required to create the
|
|
297
|
-
zero
|
|
303
|
+
zero-knowledge property of program execution.
|
|
298
304
|
2. **Program Records:** Records are private state generated by a program belonging to a unique private keyholder. Records
|
|
299
|
-
are generated by a program's functions and can be changed and updated
|
|
305
|
+
are generated by a program's functions and can be changed and updated when a user runs various functions of the
|
|
300
306
|
program. These records are private by default and are used to manage updatable private state. One of the most clear
|
|
301
|
-
usages of records is to the `credits` record in the `credits.aleo
|
|
302
|
-
representing Aleo credits on the Aleo
|
|
303
|
-
on
|
|
307
|
+
usages of records is to the `credits` record in the `credits.aleo` program. Credits records are one of two official ways of
|
|
308
|
+
representing Aleo credits on the Aleo network and are used to pay all transaction fees on the network. More information
|
|
309
|
+
on records can be found in the [Records](#41-private-state-data--records) section below.
|
|
304
310
|
|
|
305
311
|
For this reason, all programs will need proving and verifying keys to operate and many functions in Aleo programs will
|
|
306
312
|
require records as inputs. To simplify the process of managing keys and records, the Aleo SDK provides two abstractions
|
|
307
313
|
for managing these concepts:
|
|
308
314
|
|
|
309
|
-
1. **KeyProvider:** When program functions execute, by default
|
|
310
|
-
make a zero
|
|
315
|
+
1. **KeyProvider:** When program functions execute, they will by default synthesize the proving and verifying keys needed to
|
|
316
|
+
make a zero-knowledge proof of the execution. However, these keys are large and expensive to generate. For this reason, applications may
|
|
311
317
|
want to store these keys and re-use them in future executions. The `KeyProvider` interface provides the ability for
|
|
312
318
|
users of the SDK to provide their own key storage and retrieval mechanism. The SDK provides a default implementation
|
|
313
319
|
of the `KeyProvider` interface via the `AleoKeyProvider` class.
|
|
@@ -319,10 +325,10 @@ searches the Aleo network for records uniquely belonging to a user.
|
|
|
319
325
|
The `ProgramManager` class is capable of taking a `KeyProvider` and `RecordProvider` as arguments and will use them to
|
|
320
326
|
find the correct keys and records for a program execution.
|
|
321
327
|
|
|
322
|
-
### 2.7 Deploy a new
|
|
328
|
+
### 2.7 Deploy a new program to the Aleo Network
|
|
323
329
|
|
|
324
|
-
The Aleo
|
|
325
|
-
to the network (as long as it doesn't already
|
|
330
|
+
The Aleo network contains a public registry of programs that can be executed by anyone. Any user can add an Aleo program
|
|
331
|
+
to the network (as long as it doesn't already exist) by paying a deployment fee in Aleo credits. The SDK
|
|
326
332
|
provides a simple interface for deploying programs to the Aleo network using the program manager.
|
|
327
333
|
|
|
328
334
|
```typescript
|
|
@@ -359,7 +365,7 @@ const tx_id = await programManager.deploy(program, fee);
|
|
|
359
365
|
const transaction = await programManager.networkClient.getTransaction(tx_id);
|
|
360
366
|
```
|
|
361
367
|
|
|
362
|
-
The NetworkRecordProvider will attempt to scan the network for a fee record for the account provided. Doing a recent
|
|
368
|
+
The `NetworkRecordProvider` will attempt to scan the network for a fee record for the account provided. Doing a recent
|
|
363
369
|
public transfer to the deploying account will ensure a record is found quickly, or you can provide a fee record manually
|
|
364
370
|
by [scanning](https://developer.aleo.org/testnet/getting_started/deploy_execute/#scan) for a record and passing it as a
|
|
365
371
|
string.
|
|
@@ -372,17 +378,17 @@ const feeRecord = "{ owner: aleo1xxx...xxx.private, microcredits: 2000000u64.p
|
|
|
372
378
|
const tx_id = await programManager.deploy(program, fee, undefined, feeRecord);
|
|
373
379
|
```
|
|
374
380
|
|
|
375
|
-
### 2.8 React
|
|
381
|
+
### 2.8 React example
|
|
376
382
|
|
|
377
383
|
The above concepts can be tied together in a concrete example of a React web app. This example can be installed in one
|
|
378
384
|
step by running:
|
|
379
385
|
|
|
380
386
|
`npm create aleo-app@latest`
|
|
381
387
|
|
|
382
|
-
#### Program
|
|
388
|
+
#### Program execution
|
|
383
389
|
|
|
384
390
|
Program execution is a computationally expensive process. For this reason, it is recommended to execute programs in
|
|
385
|
-
|
|
391
|
+
Web Workers.
|
|
386
392
|
|
|
387
393
|
<details>
|
|
388
394
|
<summary>Example Web Worker Usage</summary>
|
|
@@ -483,7 +489,7 @@ self.addEventListener("message", (ev) => {
|
|
|
483
489
|
});
|
|
484
490
|
```
|
|
485
491
|
|
|
486
|
-
The
|
|
492
|
+
The Web Worker can then be initialized in a worker provider component which uses React effects
|
|
487
493
|
|
|
488
494
|
```jsx
|
|
489
495
|
import { useEffect, useState } from "react";
|
|
@@ -532,11 +538,11 @@ export default WorkerProvider;
|
|
|
532
538
|
|
|
533
539
|
</details>
|
|
534
540
|
|
|
535
|
-
Using both
|
|
536
|
-
Aleo zero
|
|
541
|
+
Using both Web Workers and a Wasm initialization in a React hook, a single-page app can be created that executes
|
|
542
|
+
Aleo zero-knowledge programs.
|
|
537
543
|
|
|
538
544
|
<details>
|
|
539
|
-
<summary>Example App.jsx Implementing Zero
|
|
545
|
+
<summary>Example App.jsx Implementing Zero-Knowledge Program Execution</summary>
|
|
540
546
|
|
|
541
547
|
```jsx
|
|
542
548
|
import { useEffect, useState } from "react";
|
|
@@ -653,17 +659,17 @@ export default App;
|
|
|
653
659
|
</details>
|
|
654
660
|
|
|
655
661
|
|
|
656
|
-
A full example of this implementation can be found [here](https://github.com/ProvableHQ/sdk/blob/testnet3/create-aleo-app/template-react/src/App.jsx)
|
|
662
|
+
A full example of this implementation can be found [here](https://github.com/ProvableHQ/sdk/blob/testnet3/create-aleo-app/template-react-managed-worker/src/App.jsx)
|
|
657
663
|
|
|
658
664
|
## 3. Aleo Credit Transfers
|
|
659
665
|
|
|
660
|
-
### 3.1 Aleo
|
|
666
|
+
### 3.1 Aleo credits
|
|
661
667
|
|
|
662
|
-
The official token of operation
|
|
668
|
+
The official token of operation on the Aleo Network are Aleo credits. Aleo credits are used to pay all fees for program
|
|
663
669
|
execution on the Aleo network.
|
|
664
670
|
|
|
665
|
-
Aleo credits are defined in the [credits.aleo](https://
|
|
666
|
-
deployed to the Aleo
|
|
671
|
+
Aleo credits are defined in the [credits.aleo](https://explorer.aleo.org/program/credits.aleo) program. This program is
|
|
672
|
+
deployed to the Aleo network and defines data structures representing Aleo credits and the functions used to manage them.
|
|
667
673
|
|
|
668
674
|
There are two ways to hold Aleo credits.
|
|
669
675
|
|
|
@@ -676,11 +682,11 @@ record credits:
|
|
|
676
682
|
microcredits as u64.private;
|
|
677
683
|
```
|
|
678
684
|
|
|
679
|
-
A user's total private credits balance will consist of all unspent `credits` records owned by the user with a
|
|
685
|
+
A user's total private credits balance will consist of all unspent `credits` records owned by the user with a non-zero
|
|
680
686
|
`microcredits` value.
|
|
681
687
|
|
|
682
688
|
#### 2 - Public balances via credits.aleo account mapping
|
|
683
|
-
The second is by holding a `balance` in the `account` mapping in the `credits.aleo` program on the Aleo
|
|
689
|
+
The second is by holding a `balance` in the `account` mapping in the `credits.aleo` program on the Aleo network.
|
|
684
690
|
|
|
685
691
|
```
|
|
686
692
|
mapping account:
|
|
@@ -688,21 +694,21 @@ mapping account:
|
|
|
688
694
|
value microcredits as u64.public;
|
|
689
695
|
```
|
|
690
696
|
|
|
691
|
-
The total public credits balance of a user is the value of account mapping at the user's address. Users can hold both private and public balances simultaneously.
|
|
697
|
+
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.
|
|
692
698
|
|
|
693
|
-
More information about `records` and `mappings` and how they related to private and public balances are explained in the
|
|
699
|
+
More information about `records` and `mappings` and how they are related to private and public balances are explained in the
|
|
694
700
|
[Managing Program Data and Private State](#4-managing-program-data-and-private-state) section.
|
|
695
701
|
|
|
696
|
-
### 3.2
|
|
697
|
-
The `ProgramManager` allows transfers of
|
|
702
|
+
### 3.2 Transferring Aleo credits
|
|
703
|
+
The `ProgramManager` allows transfers of Aleo credits via the `transfer` method. This function executes the `credits.aleo`
|
|
698
704
|
program under the hood.
|
|
699
705
|
|
|
700
706
|
There are four transfer functions available.
|
|
701
707
|
|
|
702
708
|
#### 1. transfer_private
|
|
703
709
|
|
|
704
|
-
Takes a `credits` record
|
|
705
|
-
to a new record owned by the receiver. This function is %
|
|
710
|
+
Takes a `credits` record owned by the sender, subtracts an amount from it, and adds that amount
|
|
711
|
+
to a new record owned by the receiver. This function is 100% private and does not affect the `account` mapping.
|
|
706
712
|
|
|
707
713
|
```mermaid
|
|
708
714
|
graph LR
|
|
@@ -714,8 +720,8 @@ graph LR
|
|
|
714
720
|
```
|
|
715
721
|
|
|
716
722
|
#### 2. transfer_private_to_public
|
|
717
|
-
Takes a `credits` record
|
|
718
|
-
that amount to the `account` mapping of the receiver. This function is %
|
|
723
|
+
Takes a `credits` record owned by the sender, subtracts an amount from it, and adds
|
|
724
|
+
that amount to the `account` mapping of the receiver. This function is 50% private and 50% public. It consumes a record
|
|
719
725
|
as a private input and generates a public balance in the `account` mapping entry belonging to the receiver.
|
|
720
726
|
|
|
721
727
|
```mermaid
|
|
@@ -730,7 +736,7 @@ graph LR
|
|
|
730
736
|
|
|
731
737
|
#### 3. transfer_public
|
|
732
738
|
|
|
733
|
-
Subtracts an amount of `credits` stored in the `account` mapping of the `credits.aleo program
|
|
739
|
+
Subtracts an amount of `credits` stored in the `account` mapping of the `credits.aleo` program, and
|
|
734
740
|
adds that amount to the `account` mapping of the receiver. This function is 100% public and does not consume or generate
|
|
735
741
|
any records.
|
|
736
742
|
|
|
@@ -749,7 +755,7 @@ graph LR
|
|
|
749
755
|
|
|
750
756
|
#### 4. transfer_public_to_private
|
|
751
757
|
Subtracts an amount `credits` stored in the `account` mapping of the `credits.aleo program`
|
|
752
|
-
and adds that amount to a new private record owned by the receiver. This function is %
|
|
758
|
+
and adds that amount to a new private record owned by the receiver. This function is 50% private and 50% public.
|
|
753
759
|
It publicly consumes a balance in the `account` mapping entry belonging to the sender and generates a private record
|
|
754
760
|
as a private output.
|
|
755
761
|
|
|
@@ -812,7 +818,7 @@ public_balance = programManager.networkClient.getMappingValue("credits.aleo", US
|
|
|
812
818
|
assert(public_balance === 0);
|
|
813
819
|
```
|
|
814
820
|
|
|
815
|
-
### 3.2 Checking
|
|
821
|
+
### 3.2 Checking public balances
|
|
816
822
|
As shown above, a public balance of any address can be checked with `getMappingValue` function of the `NetworkClient`.
|
|
817
823
|
|
|
818
824
|
```typescript
|
|
@@ -823,17 +829,17 @@ const public_balance = networkClient.getMappingValue("credits.aleo", USER_1_ADDR
|
|
|
823
829
|
|
|
824
830
|
## 4. Managing Program Data and Private State
|
|
825
831
|
|
|
826
|
-
### 4.1 Private
|
|
827
|
-
Records
|
|
832
|
+
### 4.1 Private state data: records
|
|
833
|
+
Records are analogous to concept of [UTXOs](https://en.wikipedia.org/wiki/Unspent_transaction_output). When a record is
|
|
828
834
|
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
|
|
829
|
-
as input, it is considered consumed and cannot be used again. In many cases a new record will be created from the output
|
|
835
|
+
as an input, it is considered consumed and cannot be used again. In many cases a new record will be created from the output
|
|
830
836
|
of the function. Records are private by default and are associated with a single Aleo program and a single private key
|
|
831
837
|
representing a user.
|
|
832
838
|
|
|
833
|
-
### 4.2 Record
|
|
839
|
+
### 4.2 Record usage example: private value transfers
|
|
834
840
|
|
|
835
841
|
A straightforward example of a usage of records in a program can be demonstrated by explaining the process of private
|
|
836
|
-
value transfers of
|
|
842
|
+
value transfers of Aleo credits on the Aleo network.
|
|
837
843
|
|
|
838
844
|
Aleo credits are the official token in which all on-chain execution and deployment fees are paid. Credits can be public
|
|
839
845
|
or private. Private credits are represented by the `credits` record in the [credits.aleo](https://www.aleo.network/programs/credits.aleo)
|
|
@@ -866,7 +872,7 @@ function transfer_private:
|
|
|
866
872
|
```
|
|
867
873
|
|
|
868
874
|
The `transfer_private` function can be graphically represented by the graph below. In the graph the first record "Record 1"
|
|
869
|
-
is
|
|
875
|
+
is consumed and can never be used again. From the data in Record 1, two more records are created. One containing
|
|
870
876
|
the intended amount for the recipient which is now owned by the recipient and another containing the remaining credits
|
|
871
877
|
which is sent back to the sender.
|
|
872
878
|
|
|
@@ -880,8 +886,8 @@ graph LR
|
|
|
880
886
|
p1--Credits Record 3-->R1[Recipient Address]
|
|
881
887
|
```
|
|
882
888
|
|
|
883
|
-
This chain of ownership is tracked by the Aleo
|
|
884
|
-
|
|
889
|
+
This chain of ownership is tracked by the Aleo blockchain when users choose to submit their transactions to the Aleo
|
|
890
|
+
network. This allows other users who receive records to receive the updated data and verify that this data was
|
|
885
891
|
provably generated by the intended program.
|
|
886
892
|
|
|
887
893
|
What this process allows is a private chain of state to be created between multiple users. In the context of value
|
|
@@ -947,16 +953,15 @@ const USER_3_ADDRESS = "user3Address";
|
|
|
947
953
|
const tx_id = await programManager.transfer(1, USER_3_ADDRESS, "transfer_private", 0.2, undefined, recordPlaintext);
|
|
948
954
|
```
|
|
949
955
|
|
|
950
|
-
When an execution such as
|
|
951
|
-
of the execution is posted, containing an encrypted version of the record output and a transaction ID.
|
|
956
|
+
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.
|
|
952
957
|
|
|
953
958
|
Because the records are encrypted when they're posted on the network, they do not reveal any information about the party
|
|
954
959
|
who executed the program, nor the contents of the record. The only information that is revealed is the program ID,
|
|
955
960
|
function name, encrypted function inputs, and the transaction ID of the program execution. No user except for the recipient
|
|
956
961
|
of the record can see the contents of the record.
|
|
957
962
|
|
|
958
|
-
Below you can see what the exact data which is posted to the Aleo
|
|
959
|
-
record, the amount transferred, and both the sender
|
|
963
|
+
Below, you can see what the exact data which is posted to the Aleo network when `transfer_private` is run. Note that the
|
|
964
|
+
record, the amount transferred, and both the sender and recipient addresses are all encrypted.
|
|
960
965
|
|
|
961
966
|
<details>
|
|
962
967
|
<summary>transfer_private Execution Transcript</summary>
|
|
@@ -1055,8 +1060,8 @@ mapping account:
|
|
|
1055
1060
|
value microcredits as u64.public;
|
|
1056
1061
|
```
|
|
1057
1062
|
|
|
1058
|
-
The `account` mapping is used to store public
|
|
1059
|
-
and a public u64 value representing the number of microcredits owned by the address.
|
|
1063
|
+
The `account` mapping is used to store public credit balances on the Aleo network. It takes a public address as a key
|
|
1064
|
+
and a public `u64` value representing the number of microcredits owned by the address.
|
|
1060
1065
|
|
|
1061
1066
|
Mappings within programs are identified by the `mapping` identifier. Any program where this keyword appears contains an
|
|
1062
1067
|
on-chain mapping. An example of a program that uses a mapping is shown below:
|
|
@@ -1085,9 +1090,9 @@ finalize update_score:
|
|
|
1085
1090
|
|
|
1086
1091
|
Note that the above function has a `finalize` identifier. This identifier is used to identify a portion of a function's
|
|
1087
1092
|
code that should be executed by nodes on the Aleo network. Program mappings are updated exclusively by code run by nodes
|
|
1088
|
-
on the Aleo
|
|
1093
|
+
on the Aleo network written in `finalize` blocks.
|
|
1089
1094
|
|
|
1090
|
-
### 4.4 Reading
|
|
1095
|
+
### 4.4 Reading mappings
|
|
1091
1096
|
Any state within a program mapping is public and can be read by any participant in the Aleo network. The `NetworkClient`
|
|
1092
1097
|
class provides the `getMapping` method to read the public mappings within an program and the `getMappingValue` method to
|
|
1093
1098
|
read the value of a specific key within a mapping.
|
|
@@ -1103,7 +1108,7 @@ const publicCredits = networkClient.getMapping("credits.aleo", "[a valid aleo ac
|
|
|
1103
1108
|
assert(publicCredits === "0u64");
|
|
1104
1109
|
```
|
|
1105
1110
|
|
|
1106
|
-
### 4.5 Initializing &
|
|
1111
|
+
### 4.5 Initializing & updating mappings
|
|
1107
1112
|
Updating mappings is done by executing a program function on the Aleo network which has a finalize block that updates the
|
|
1108
1113
|
program's mapping. For instance the `transfer_public` function in the `credits.aleo` program updates the `account`
|
|
1109
1114
|
mapping (and thus a user's balance) when called.
|
|
@@ -1130,12 +1135,12 @@ finalize transfer_public:
|
|
|
1130
1135
|
|
|
1131
1136
|
From the perspective of the caller of the API, this is as simple as executing a normal Aleo function. Given the inputs
|
|
1132
1137
|
to a function with a finalize scope that updates a mapping are valid, the mapping will either be intialized or updated
|
|
1133
|
-
by the Aleo network. All the user of the SDK must do is ensure that the inputs to the function are valid.
|
|
1138
|
+
by the Aleo network. All that the user of the SDK must do is ensure that the inputs to the function are valid.
|
|
1134
1139
|
|
|
1135
|
-
If function inputs are
|
|
1136
|
-
consumed.
|
|
1140
|
+
If function inputs are invalid, the network will return an error, but the fee paid for the transaction will still be
|
|
1141
|
+
consumed. Therefore, it is important to ensure that the inputs to a function are valid before executing it.
|
|
1137
1142
|
|
|
1138
|
-
A simple example of a mapping update can be shown by simply executing
|
|
1143
|
+
A simple example of a mapping update can be shown by simply executing 'transfer_public` as shown below.
|
|
1139
1144
|
|
|
1140
1145
|
```typescript
|
|
1141
1146
|
import { Account, ProgramManager, AleoKeyProvider, NetworkRecordProvider, AleoNetworkClient } from '@aleo/sdk';
|
|
@@ -1156,7 +1161,7 @@ const tx_id = await programManager.transfer(1, RECIPIENT_ADDRESS, "transfer_priv
|
|
|
1156
1161
|
```
|
|
1157
1162
|
|
|
1158
1163
|
|
|
1159
|
-
## 5. Communicating with the Aleo
|
|
1164
|
+
## 5. Communicating with the Aleo network
|
|
1160
1165
|
|
|
1161
1166
|
Communication with the Aleo network is done through the `AleoNetworkClient` class. This class provides methods to query
|
|
1162
1167
|
data from Aleo network nodes and submit transactions to the Aleo network.
|