Skip to content

Commit 3c62763

Browse files
committed
🏷️ Add Unique IDs to PerHumanIDv1 events
1 parent 77a5bec commit 3c62763

File tree

5 files changed

+36
-36
lines changed

5 files changed

+36
-36
lines changed

.github/workflows/test.yml

+3-1
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,14 @@ jobs:
4747
- uses: actions/checkout@v3
4848
with:
4949
submodules: true
50+
- name: Install bun
51+
uses: oven-sh/setup-bun@v1
5052
- uses: actions/setup-node@v4
5153
with:
5254
node-version: "20.x"
5355
- name:
5456
run: |
5557
cd mina
56-
npm i
58+
bun i
5759
npm run test
5860
cd ..

mina/HumanIDv1.test.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Field, Poseidon, PrivateKey, PublicKey, Signature } from "o1js";
22
import { HumanIDv1, authenticate, requireConsistent } from "./HumanIDv1";
33

4-
const Nodes = [
4+
const KPassSigners = [
55
PrivateKey.fromBigInt(1n),
66
PrivateKey.fromBigInt(2n),
77
PrivateKey.fromBigInt(3n),
@@ -21,9 +21,9 @@ const signHumanIDv1 = (humanIDv1Id: bigint, sender: PublicKey): HumanIDv1 => {
2121
return new HumanIDv1({
2222
id,
2323
commitmentR,
24-
sig0: Signature.create(Nodes[0], [id, commitment]),
25-
sig1: Signature.create(Nodes[1], [id, commitment]),
26-
sig2: Signature.create(Nodes[2], [id, commitment]),
24+
sig0: Signature.create(KPassSigners[0], [id, commitment]),
25+
sig1: Signature.create(KPassSigners[1], [id, commitment]),
26+
sig2: Signature.create(KPassSigners[2], [id, commitment]),
2727
});
2828
};
2929

@@ -33,9 +33,9 @@ const badSignHumanIDv1 = (humanIDv1: bigint, sender: PublicKey): HumanIDv1 => {
3333
return new HumanIDv1({
3434
id,
3535
commitmentR,
36-
sig0: Signature.create(Nodes[0], [id, commitment]),
37-
sig1: Signature.create(Nodes[1], [id, commitment]),
38-
sig2: Signature.create(Nodes[0], [id, commitment]),
36+
sig0: Signature.create(KPassSigners[0], [id, commitment]),
37+
sig1: Signature.create(KPassSigners[1], [id, commitment]),
38+
sig2: Signature.create(KPassSigners[0], [id, commitment]),
3939
});
4040
};
4141

mina/HumanIDv1.ts

+22-25
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { uint8ArrayBEtoBigInt } from "@kimlikdao/lib/util/çevir";
12
import {
23
Field,
34
MerkleWitness,
@@ -11,25 +12,6 @@ import {
1112
state,
1213
} from "o1js";
1314

14-
const Uint8denHexe: string[] = Array(255);
15-
for (let /** number */ i = 0; i < 256; ++i)
16-
Uint8denHexe[i] = i.toString(16).padStart(2, "0");
17-
18-
const hex = (buff: Uint8Array) => {
19-
/** @const {!Array<string>} */
20-
const ikililer = new Array(buff.length);
21-
for (let /** number */ i = 0; i < buff.length; ++i)
22-
ikililer[i] = Uint8denHexe[buff[i]];
23-
return ikililer.join("");
24-
}
25-
26-
const uint8ArrayBEtoBigInt = (bytes: Uint8Array) => BigInt("0x" + hex(bytes));
27-
28-
const readPublicKey = (bytes: Uint8Array) => PublicKey.from({
29-
x: uint8ArrayBEtoBigInt(bytes.subarray(0, 32)),
30-
isOdd: !!bytes[32]
31-
});
32-
3315
const readField = (bytes: Uint8Array) => Field(uint8ArrayBEtoBigInt(bytes.subarray(0, 32)));
3416

3517
const readSignature = (bytes: Uint8Array) => new Signature(
@@ -60,6 +42,8 @@ class HumanIDv1 extends Struct({
6042

6143
class HumanIDv1Witness extends MerkleWitness(33) { }
6244

45+
const Event = Struct({ uid: Field, value: Field });
46+
6347
const KPassSigners = [
6448
PrivateKey.fromBigInt(1n).toPublicKey(),
6549
PrivateKey.fromBigInt(2n).toPublicKey(),
@@ -79,9 +63,18 @@ const authenticate = (owner: PublicKey, hid: HumanIDv1) => {
7963
const EmptyRoot =
8064
Field(0x21afce36daa1a2d67391072035f4555a85aea7197e5830b128f121aa382770cdn);
8165

66+
// Field(1).div(Field(2 ** 32))
8267
const Inverse2Exp32 =
8368
Field(0x3fffffffc00000000000000000000000224698fbe706601f8fe037d166d2cf14n);
8469

70+
// CircuitString.fromString("KimlikDAO-init").hash()
71+
const InitEventUID =
72+
Field(0x363b52a04cf908f3357575efb35b0bf635ebb4fc2e921c140e99426fb1ef89dcn);
73+
74+
// CircuitString.fromString("KimlikDAO-add-HumanIDv1").hash()
75+
const AddEventUID =
76+
Field(0x13c6e18cd3ba5dab50481970932ded0f7513e22ada9b77949a83dd54fc7c4e6dn);
77+
8578
const requireConsistent = (humanIDv1: Field, truncatedHumanIDv1: Field) =>
8679
humanIDv1
8780
.sub(truncatedHumanIDv1)
@@ -93,16 +86,19 @@ const requireConsistent = (humanIDv1: Field, truncatedHumanIDv1: Field) =>
9386

9487
class PerHumanIDv1Contract extends SmartContract {
9588
events = {
96-
"KimlikDAO-init": Field, // Emits the tree height along with init event
97-
"KimlikDAO-add-HumanIDv1": Field, // Emits the added HumanIDv1.id
89+
"KimlikDAO-init": Event, // Emits the tree height along with init event
90+
"KimlikDAO-add-HumanIDv1": Event, // Emits the added HumanIDv1.id
9891
};
9992

10093
@state(Field) treeRoot = State<Field>();
10194

10295
init() {
10396
super.init();
10497
this.treeRoot.set(EmptyRoot);
105-
this.emitEvent("KimlikDAO-init", Field(32));
98+
this.emitEvent(
99+
"KimlikDAO-init",
100+
new Event({ uid: InitEventUID, value: Field(32) })
101+
);
106102
}
107103

108104
acceptHumanIDv1(
@@ -113,7 +109,10 @@ class PerHumanIDv1Contract extends SmartContract {
113109
authenticate(owner, humanIDv1);
114110
requireConsistent(humanIDv1.id, witness.calculateIndex());
115111
this.addToMerkleTree(witness);
116-
this.emitEvent("KimlikDAO-add-HumanIDv1", humanIDv1.id);
112+
this.emitEvent(
113+
"KimlikDAO-add-HumanIDv1",
114+
new Event({ uid: AddEventUID, value: humanIDv1.id })
115+
);
117116
}
118117

119118
addToMerkleTree(witness: HumanIDv1Witness) {
@@ -134,8 +133,6 @@ export {
134133
PerHumanIDv1Contract,
135134
authenticate,
136135
readField,
137-
readPublicKey,
138136
readSignature,
139137
requireConsistent
140138
};
141-

mina/package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44
"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js"
55
},
66
"dependencies": {
7+
"@kimlikdao/lib": "github:KimlikDAO/kimlikdao-js",
78
"@types/jest": "^29.5.12",
89
"jest": "^29.7.0",
9-
"o1js": "^1.2.0",
10-
"ts-jest": "^29.1.2",
10+
"o1js": "^1.3.0",
11+
"ts-jest": "^29.1.3",
1112
"typescript": "^5.4.5"
1213
},
1314
"type": "module"

0 commit comments

Comments
 (0)