Skip to content

Commit

Permalink
🏷️ Add Unique IDs to PerHumanIDv1 events
Browse files Browse the repository at this point in the history
  • Loading branch information
KimlikDAO-bot committed May 25, 2024
1 parent 77a5bec commit 380efcd
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 39 deletions.
14 changes: 7 additions & 7 deletions mina/HumanIDv1.test.ts
Original file line number Diff line number Diff line change
@@ -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),
Expand All @@ -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]),
});
};

Expand All @@ -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]),
});
};

Expand Down
47 changes: 22 additions & 25 deletions mina/HumanIDv1.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { uint8ArrayBEtoBigInt } from "@kimlikdao/lib/util/çevir";
import {
Field,
MerkleWitness,
Expand All @@ -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<string>} */
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(
Expand Down Expand Up @@ -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(),
Expand All @@ -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)
Expand All @@ -93,16 +86,19 @@ 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<Field>();

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(
Expand All @@ -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) {
Expand All @@ -134,8 +133,6 @@ export {
PerHumanIDv1Contract,
authenticate,
readField,
readPublicKey,
readSignature,
requireConsistent
};

7 changes: 4 additions & 3 deletions mina/package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
{
"scripts": {
"build": "tsc",
"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js"
"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js",
"postinstall": "mkdir -p node_modules/@kimlikdao && ln -sfn ../../../lib node_modules/@kimlikdao/lib"
},
"dependencies": {
"@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"
Expand Down
7 changes: 4 additions & 3 deletions mina/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"esnext"
],
"outDir": "./build",
"rootDir": ".",
"rootDir": "../",
"strict": true,
"strictPropertyInitialization": false, // to enable generic constructors, e.g. on CircuitValue
"skipLibCheck": true,
Expand All @@ -24,7 +24,8 @@
"useDefineForClassFields": false,
},
"include": [
"./examples",
"./"
"../mina/examples",
"../mina",
"../lib/util"
],
}

0 comments on commit 380efcd

Please sign in to comment.