From 4c66c3154e319fc12b10b2b72daca20afe14ded1 Mon Sep 17 00:00:00 2001 From: KimlikDAO-bot Date: Sat, 25 May 2024 16:07:22 -0700 Subject: [PATCH] =?UTF-8?q?=F0=9F=8F=B7=EF=B8=8F=20Add=20Unique=20IDs=20to?= =?UTF-8?q?=20`PerHumanIDv1`=20events?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/test.yml | 4 +++- lib | 2 +- mina/HumanIDv1.test.ts | 14 ++++++------ mina/HumanIDv1.ts | 47 ++++++++++++++++++-------------------- mina/package.json | 5 ++-- 5 files changed, 36 insertions(+), 36 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 87899a5..069f54a 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -47,12 +47,14 @@ jobs: - uses: actions/checkout@v3 with: submodules: true + - name: Install bun + uses: oven-sh/setup-bun@v1 - uses: actions/setup-node@v4 with: node-version: "20.x" - name: run: | cd mina - npm i + bun i npm run test cd .. diff --git a/lib b/lib index 7c52fe1..284422c 160000 --- a/lib +++ b/lib @@ -1 +1 @@ -Subproject commit 7c52fe15bd66641499bd4c47259dce1e972b509c +Subproject commit 284422c9d0f52b6260eda165ee3182de78a4b731 diff --git a/mina/HumanIDv1.test.ts b/mina/HumanIDv1.test.ts index ceecdd8..a09c350 100644 --- a/mina/HumanIDv1.test.ts +++ b/mina/HumanIDv1.test.ts @@ -1,7 +1,7 @@ import { Field, Poseidon, PrivateKey, PublicKey, Signature } from "o1js"; import { HumanIDv1, authenticate, requireConsistent } from "./HumanIDv1"; -const Nodes = [ +const KPassSigners = [ PrivateKey.fromBigInt(1n), PrivateKey.fromBigInt(2n), PrivateKey.fromBigInt(3n), @@ -21,9 +21,9 @@ const signHumanIDv1 = (humanIDv1Id: bigint, sender: PublicKey): HumanIDv1 => { return new HumanIDv1({ id, commitmentR, - sig0: Signature.create(Nodes[0], [id, commitment]), - sig1: Signature.create(Nodes[1], [id, commitment]), - sig2: Signature.create(Nodes[2], [id, commitment]), + sig0: Signature.create(KPassSigners[0], [id, commitment]), + sig1: Signature.create(KPassSigners[1], [id, commitment]), + sig2: Signature.create(KPassSigners[2], [id, commitment]), }); }; @@ -33,9 +33,9 @@ const badSignHumanIDv1 = (humanIDv1: bigint, sender: PublicKey): HumanIDv1 => { return new HumanIDv1({ id, commitmentR, - sig0: Signature.create(Nodes[0], [id, commitment]), - sig1: Signature.create(Nodes[1], [id, commitment]), - sig2: Signature.create(Nodes[0], [id, commitment]), + sig0: Signature.create(KPassSigners[0], [id, commitment]), + sig1: Signature.create(KPassSigners[1], [id, commitment]), + sig2: Signature.create(KPassSigners[0], [id, commitment]), }); }; diff --git a/mina/HumanIDv1.ts b/mina/HumanIDv1.ts index 51a92f0..c098ba7 100644 --- a/mina/HumanIDv1.ts +++ b/mina/HumanIDv1.ts @@ -1,3 +1,4 @@ +import { uint8ArrayBEtoBigInt } from "@kimlikdao/lib/util/çevir"; import { Field, MerkleWitness, @@ -11,25 +12,6 @@ import { state, } from "o1js"; -const Uint8denHexe: string[] = Array(255); -for (let /** number */ i = 0; i < 256; ++i) - Uint8denHexe[i] = i.toString(16).padStart(2, "0"); - -const hex = (buff: Uint8Array) => { - /** @const {!Array} */ - const ikililer = new Array(buff.length); - for (let /** number */ i = 0; i < buff.length; ++i) - ikililer[i] = Uint8denHexe[buff[i]]; - return ikililer.join(""); -} - -const uint8ArrayBEtoBigInt = (bytes: Uint8Array) => BigInt("0x" + hex(bytes)); - -const readPublicKey = (bytes: Uint8Array) => PublicKey.from({ - x: uint8ArrayBEtoBigInt(bytes.subarray(0, 32)), - isOdd: !!bytes[32] -}); - const readField = (bytes: Uint8Array) => Field(uint8ArrayBEtoBigInt(bytes.subarray(0, 32))); const readSignature = (bytes: Uint8Array) => new Signature( @@ -60,6 +42,8 @@ class HumanIDv1 extends Struct({ class HumanIDv1Witness extends MerkleWitness(33) { } +const Event = Struct({ uid: Field, value: Field }); + const KPassSigners = [ PrivateKey.fromBigInt(1n).toPublicKey(), PrivateKey.fromBigInt(2n).toPublicKey(), @@ -79,9 +63,18 @@ const authenticate = (owner: PublicKey, hid: HumanIDv1) => { const EmptyRoot = Field(0x21afce36daa1a2d67391072035f4555a85aea7197e5830b128f121aa382770cdn); +// Field(1).div(Field(2 ** 32)) const Inverse2Exp32 = Field(0x3fffffffc00000000000000000000000224698fbe706601f8fe037d166d2cf14n); +// CircuitString.fromString("KimlikDAO-init").hash() +const InitEventUID = + Field(0x363b52a04cf908f3357575efb35b0bf635ebb4fc2e921c140e99426fb1ef89dcn); + +// CircuitString.fromString("KimlikDAO-add-HumanIDv1").hash() +const AddEventUID = + Field(0x13c6e18cd3ba5dab50481970932ded0f7513e22ada9b77949a83dd54fc7c4e6dn); + const requireConsistent = (humanIDv1: Field, truncatedHumanIDv1: Field) => humanIDv1 .sub(truncatedHumanIDv1) @@ -93,8 +86,8 @@ const requireConsistent = (humanIDv1: Field, truncatedHumanIDv1: Field) => class PerHumanIDv1Contract extends SmartContract { events = { - "KimlikDAO-init": Field, // Emits the tree height along with init event - "KimlikDAO-add-HumanIDv1": Field, // Emits the added HumanIDv1.id + "KimlikDAO-init": Event, // Emits the tree height along with init event + "KimlikDAO-add-HumanIDv1": Event, // Emits the added HumanIDv1.id }; @state(Field) treeRoot = State(); @@ -102,7 +95,10 @@ class PerHumanIDv1Contract extends SmartContract { init() { super.init(); this.treeRoot.set(EmptyRoot); - this.emitEvent("KimlikDAO-init", Field(32)); + this.emitEvent( + "KimlikDAO-init", + new Event({ uid: InitEventUID, value: Field(32) }) + ); } acceptHumanIDv1( @@ -113,7 +109,10 @@ class PerHumanIDv1Contract extends SmartContract { authenticate(owner, humanIDv1); requireConsistent(humanIDv1.id, witness.calculateIndex()); this.addToMerkleTree(witness); - this.emitEvent("KimlikDAO-add-HumanIDv1", humanIDv1.id); + this.emitEvent( + "KimlikDAO-add-HumanIDv1", + new Event({ uid: AddEventUID, value: humanIDv1.id }) + ); } addToMerkleTree(witness: HumanIDv1Witness) { @@ -134,8 +133,6 @@ export { PerHumanIDv1Contract, authenticate, readField, - readPublicKey, readSignature, requireConsistent }; - diff --git a/mina/package.json b/mina/package.json index 8cc356e..78b4c7d 100644 --- a/mina/package.json +++ b/mina/package.json @@ -4,10 +4,11 @@ "test": "node --experimental-vm-modules node_modules/jest/bin/jest.js" }, "dependencies": { + "@kimlikdao/lib": "file:../lib", "@types/jest": "^29.5.12", "jest": "^29.7.0", - "o1js": "^1.2.0", - "ts-jest": "^29.1.2", + "o1js": "^1.3.0", + "ts-jest": "^29.1.3", "typescript": "^5.4.5" }, "type": "module"