o1js-pack 0.5.4 → 0.7.0
Sign up to get free protection for your applications and to get access to all the features.
- package/.github/workflows/ci.yml +1 -1
- package/CHANGELOG.md +9 -0
- package/build/src/lib/PackingPlant.d.ts +70 -34
- package/build/src/lib/PackingPlant.js +1 -1
- package/build/src/lib/PackingPlant.js.map +1 -1
- package/build/src/lib/packed-types/PackedBool.d.ts +50 -32
- package/build/src/lib/packed-types/PackedBool.test.js +11 -7
- package/build/src/lib/packed-types/PackedBool.test.js.map +1 -1
- package/build/src/lib/packed-types/PackedString.d.ts +98 -66
- package/build/src/lib/packed-types/PackedString.js.map +1 -1
- package/build/src/lib/packed-types/PackedString.test.js +12 -8
- package/build/src/lib/packed-types/PackedString.test.js.map +1 -1
- package/build/src/lib/packed-types/PackedUInt32.d.ts +49 -31
- package/build/src/lib/packed-types/PackedUInt32.test.js +12 -8
- package/build/src/lib/packed-types/PackedUInt32.test.js.map +1 -1
- package/examples/smart_contract/election/contract.ts +8 -8
- package/examples/zk_program/age_gate/circuit.ts +6 -7
- package/package.json +11 -8
- package/tests/provable/end_to_end.test.ts +1 -1
- package/tests/provable/example_packed_string_circuit.ts +3 -10
- package/tests/provable/example_packed_uint_circuit.ts +4 -4
- package/tsconfig.json +2 -1
- package/examples/smart_contract/tokenState/README.md +0 -7
- package/examples/smart_contract/tokenState/contract.ts +0 -62
- package/examples/smart_contract/tokenState/run.ts +0 -71
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "o1js-pack",
|
3
|
-
"version": "0.
|
3
|
+
"version": "0.7.0",
|
4
4
|
"description": "",
|
5
5
|
"author": "45930",
|
6
6
|
"license": "Apache-2.0",
|
@@ -16,11 +16,11 @@
|
|
16
16
|
"scripts": {
|
17
17
|
"build": "tsc",
|
18
18
|
"buildw": "tsc --watch",
|
19
|
-
"coverage": "node --experimental-vm-modules
|
19
|
+
"coverage": "node --experimental-vm-modules node_modules/jest/bin/jest.js --coverage",
|
20
20
|
"format": "prettier --write --ignore-unknown **/*",
|
21
21
|
"prepare": "husky install",
|
22
|
-
"test": "node --experimental-vm-modules
|
23
|
-
"testw": "node --experimental-vm-modules
|
22
|
+
"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js",
|
23
|
+
"testw": "node --experimental-vm-modules node_modules/jest/bin/jest.js --watch",
|
24
24
|
"lint": "npx eslint src/* --fix"
|
25
25
|
},
|
26
26
|
"lint-staged": {
|
@@ -38,13 +38,16 @@
|
|
38
38
|
"eslint": "^8.7.0",
|
39
39
|
"eslint-plugin-o1js": "^0.4.0",
|
40
40
|
"husky": "^7.0.1",
|
41
|
-
"jest": "^
|
41
|
+
"jest": "^29.7.0",
|
42
42
|
"lint-staged": "^11.0.1",
|
43
43
|
"prettier": "^2.3.2",
|
44
|
-
"ts-jest": "^
|
45
|
-
"typescript": "^
|
44
|
+
"ts-jest": "^29.1.5",
|
45
|
+
"typescript": "^5.5.2"
|
46
46
|
},
|
47
47
|
"peerDependencies": {
|
48
|
-
"o1js": "^0.
|
48
|
+
"o1js": "^1.0.0"
|
49
|
+
},
|
50
|
+
"volta": {
|
51
|
+
"node": "20.15.0"
|
49
52
|
}
|
50
53
|
}
|
@@ -1,11 +1,4 @@
|
|
1
|
-
import {
|
2
|
-
ZkProgram,
|
3
|
-
SelfProof,
|
4
|
-
Character,
|
5
|
-
Poseidon,
|
6
|
-
Provable,
|
7
|
-
Field,
|
8
|
-
} from 'o1js';
|
1
|
+
import { ZkProgram, SelfProof, Character, Provable } from 'o1js';
|
9
2
|
import { PackedStringFactory } from '../../src/lib/packed-types/PackedString';
|
10
3
|
|
11
4
|
export class TextInput extends PackedStringFactory() {}
|
@@ -17,14 +10,14 @@ export const TextInputProgram = ZkProgram({
|
|
17
10
|
methods: {
|
18
11
|
init: {
|
19
12
|
privateInputs: [],
|
20
|
-
method(state: TextInput) {
|
13
|
+
async method(state: TextInput) {
|
21
14
|
const initState = TextInput.fromString('Mina Protocol');
|
22
15
|
state.assertEquals(initState);
|
23
16
|
},
|
24
17
|
},
|
25
18
|
changeFirstLetter: {
|
26
19
|
privateInputs: [SelfProof, Provable.Array(Character, 31), Character],
|
27
|
-
method(
|
20
|
+
async method(
|
28
21
|
newState: TextInput,
|
29
22
|
oldProof: SelfProof<TextInput, TextInput>,
|
30
23
|
oldCharacters: Array<Character>,
|
@@ -10,14 +10,14 @@ export const VotesProgram = ZkProgram({
|
|
10
10
|
methods: {
|
11
11
|
init: {
|
12
12
|
privateInputs: [],
|
13
|
-
method(
|
13
|
+
async method(publicInput: Votes) {
|
14
14
|
const initState = Votes.fromBigInts([0n, 0n]);
|
15
|
-
|
15
|
+
publicInput.assertEquals(initState);
|
16
16
|
},
|
17
17
|
},
|
18
18
|
incrementIndex0: {
|
19
19
|
privateInputs: [SelfProof],
|
20
|
-
method(newState: Votes, oldProof: SelfProof<Votes, Votes>) {
|
20
|
+
async method(newState: Votes, oldProof: SelfProof<Votes, Votes>) {
|
21
21
|
oldProof.verify();
|
22
22
|
const unpacked = Votes.unpack(oldProof.publicInput.packed);
|
23
23
|
unpacked[0] = unpacked[0].add(1);
|
@@ -26,7 +26,7 @@ export const VotesProgram = ZkProgram({
|
|
26
26
|
},
|
27
27
|
incrementIndex1: {
|
28
28
|
privateInputs: [SelfProof],
|
29
|
-
method(newState: Votes, oldProof: SelfProof<Votes, Votes>) {
|
29
|
+
async method(newState: Votes, oldProof: SelfProof<Votes, Votes>) {
|
30
30
|
oldProof.verify();
|
31
31
|
const unpacked = Votes.unpack(oldProof.publicInput.packed);
|
32
32
|
unpacked[1] = unpacked[1].add(1);
|
package/tsconfig.json
CHANGED
@@ -1,7 +0,0 @@
|
|
1
|
-
## Using Tokens as State Smart Contract
|
2
|
-
|
3
|
-
Using o1js-pack, a token can actually be a packed value. Token balances are natively a `UInt64`, so there is not as much room as a full `Field`, but we can still pack 2 `UInt32`, or we could pack 8 characters, or 64 booleans.
|
4
|
-
|
5
|
-
The `mint` and `burn` mechanisms are used like an unspent transaction output system. The state going in is always burnt, and the state coming out is unrelated numerically. In the example in `run.ts`, the token balance is set to 5 when the state is (5, 0). But when the state is (5, 10), the token balance becomes 42949672965.
|
6
|
-
|
7
|
-
This can be useful for per-user state in a zkapp. Booleans could be used to track feature flags or whether or not a user account is active, in good standing, etc... Characters could be used to store shore strings on a per-user basis, perhaps a country code? UInts can be used like in this example to track the first and last time a user has used an app. Or you could use half of the state to track an actual user balance, and the other half to track another number like number of token transactions.
|
@@ -1,62 +0,0 @@
|
|
1
|
-
import {
|
2
|
-
SmartContract,
|
3
|
-
state,
|
4
|
-
State,
|
5
|
-
method,
|
6
|
-
UInt32,
|
7
|
-
UInt64,
|
8
|
-
Account,
|
9
|
-
Provable,
|
10
|
-
} from 'o1js';
|
11
|
-
import { PackedUInt32Factory } from '../../../src/index.js';
|
12
|
-
|
13
|
-
/**
|
14
|
-
* 2 UInt32s representing the first-joined block and the last-updated block
|
15
|
-
* Could be used in conjunction with some other app logic to ensure only active users can access some resource, or similar
|
16
|
-
*/
|
17
|
-
export class UserBlockStamps extends PackedUInt32Factory(2) {}
|
18
|
-
|
19
|
-
export class UserBlockStampsToken extends SmartContract {
|
20
|
-
/**
|
21
|
-
* Initializes the user state by setting the first-joined blockstamp to the current blockheight
|
22
|
-
*/
|
23
|
-
@method
|
24
|
-
joinSystem() {
|
25
|
-
const account = Account(this.sender, this.token.id);
|
26
|
-
const currentTokenBalance = account.balance.getAndAssertEquals();
|
27
|
-
currentTokenBalance.assertEquals(UInt64.from(0));
|
28
|
-
|
29
|
-
const userBlockStamps = UserBlockStamps.fromUInt32s([
|
30
|
-
this.network.blockchainLength.getAndAssertEquals(),
|
31
|
-
UInt32.from(0),
|
32
|
-
]);
|
33
|
-
this.token.mint({
|
34
|
-
address: this.sender,
|
35
|
-
amount: UInt64.from(userBlockStamps.packed),
|
36
|
-
});
|
37
|
-
}
|
38
|
-
|
39
|
-
/**
|
40
|
-
* Updates the user state by setting the last-updated blockstamp to the current blockheight
|
41
|
-
*/
|
42
|
-
@method
|
43
|
-
interactWithSystem() {
|
44
|
-
const account = Account(this.sender, this.token.id);
|
45
|
-
const currentTokenBalance = account.balance.getAndAssertEquals();
|
46
|
-
const existingUserBlockstamps = UserBlockStamps.unpack(
|
47
|
-
currentTokenBalance.value
|
48
|
-
);
|
49
|
-
|
50
|
-
this.token.burn({
|
51
|
-
address: this.sender,
|
52
|
-
amount: currentTokenBalance,
|
53
|
-
});
|
54
|
-
|
55
|
-
existingUserBlockstamps[1] =
|
56
|
-
this.network.blockchainLength.getAndAssertEquals();
|
57
|
-
this.token.mint({
|
58
|
-
address: this.sender,
|
59
|
-
amount: UInt64.from(UserBlockStamps.pack(existingUserBlockstamps)),
|
60
|
-
});
|
61
|
-
}
|
62
|
-
}
|
@@ -1,71 +0,0 @@
|
|
1
|
-
import { AccountUpdate, Mina, PrivateKey, UInt32 } from 'o1js';
|
2
|
-
import { UserBlockStamps, UserBlockStampsToken } from './contract.js';
|
3
|
-
|
4
|
-
let Local = Mina.LocalBlockchain({ proofsEnabled: true });
|
5
|
-
Mina.setActiveInstance(Local);
|
6
|
-
|
7
|
-
// a test account that pays all the fees, and puts additional funds into the zkapp
|
8
|
-
let { privateKey: senderKey, publicKey: sender } = Local.testAccounts[0];
|
9
|
-
|
10
|
-
// the zkapp account
|
11
|
-
let zkappKey = PrivateKey.random();
|
12
|
-
let zkappAddress = zkappKey.toPublicKey();
|
13
|
-
|
14
|
-
// a special account that is allowed to pull out half of the zkapp balance, once
|
15
|
-
let privilegedKey = PrivateKey.random();
|
16
|
-
let privilegedAddress = privilegedKey.toPublicKey();
|
17
|
-
|
18
|
-
let initialBalance = 10_000_000_000;
|
19
|
-
let zkapp = new UserBlockStampsToken(zkappAddress);
|
20
|
-
|
21
|
-
console.time('compile');
|
22
|
-
await UserBlockStampsToken.compile();
|
23
|
-
console.timeEnd('compile');
|
24
|
-
|
25
|
-
console.time('deploy');
|
26
|
-
let tx = await Mina.transaction(sender, () => {
|
27
|
-
let senderUpdate = AccountUpdate.fundNewAccount(sender);
|
28
|
-
senderUpdate.send({ to: zkappAddress, amount: initialBalance });
|
29
|
-
zkapp.deploy({ zkappKey });
|
30
|
-
});
|
31
|
-
await tx.prove();
|
32
|
-
await tx.sign([senderKey]).send();
|
33
|
-
console.timeEnd('deploy');
|
34
|
-
|
35
|
-
Local.setBlockchainLength(UInt32.from(5));
|
36
|
-
|
37
|
-
console.time('join system');
|
38
|
-
tx = await Mina.transaction(sender, () => {
|
39
|
-
AccountUpdate.fundNewAccount(sender);
|
40
|
-
zkapp.joinSystem();
|
41
|
-
});
|
42
|
-
await tx.prove();
|
43
|
-
await tx.sign([senderKey]).send();
|
44
|
-
console.timeEnd('join system');
|
45
|
-
|
46
|
-
let tokenBalance = Mina.getBalance(sender, zkapp.token.id);
|
47
|
-
console.log(tokenBalance.toString());
|
48
|
-
console.log(UserBlockStamps.unpack(tokenBalance.value).toString());
|
49
|
-
|
50
|
-
Local.setBlockchainLength(UInt32.from(10));
|
51
|
-
|
52
|
-
console.time('interact with system');
|
53
|
-
tx = await Mina.transaction(sender, () => {
|
54
|
-
zkapp.interactWithSystem();
|
55
|
-
});
|
56
|
-
await tx.prove();
|
57
|
-
await tx.sign([senderKey]).send();
|
58
|
-
console.timeEnd('interact with system');
|
59
|
-
|
60
|
-
tokenBalance = Mina.getBalance(sender, zkapp.token.id);
|
61
|
-
console.log(tokenBalance.toString());
|
62
|
-
console.log(UserBlockStamps.unpack(tokenBalance.value).toString());
|
63
|
-
|
64
|
-
// compile: 1.818s
|
65
|
-
// deploy: 615.559ms
|
66
|
-
// join system: 23.085s
|
67
|
-
// 5
|
68
|
-
// 5,0
|
69
|
-
// interact with system: 15.263s
|
70
|
-
// 42949672965
|
71
|
-
// 5,10
|