Skip to content

Commit 77a5bec

Browse files
committed
🥣 Add serialization methods to provable HumanIDv1
1 parent 88cfc98 commit 77a5bec

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

‎mina/HumanIDv1.ts

+44-1
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,52 @@ import {
1111
state,
1212
} from "o1js";
1313

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+
33+
const readField = (bytes: Uint8Array) => Field(uint8ArrayBEtoBigInt(bytes.subarray(0, 32)));
34+
35+
const readSignature = (bytes: Uint8Array) => new Signature(
36+
readField(bytes),
37+
readField(bytes.subarray(32))
38+
);
39+
1440
class HumanIDv1 extends Struct({
1541
id: Field,
1642
commitmentR: Field,
1743
sig0: Signature,
1844
sig1: Signature,
1945
sig2: Signature,
20-
}) { }
46+
}) {
47+
/**
48+
* @param bytes Uint8Array of length 256, where each field is written in BE encoding.
49+
*/
50+
static fromBytes(bytes: Uint8Array) {
51+
return new HumanIDv1({
52+
id: readField(bytes),
53+
commitmentR: readField(bytes.subarray(32)),
54+
sig0: readSignature(bytes.subarray(64)),
55+
sig1: readSignature(bytes.subarray(128)),
56+
sig2: readSignature(bytes.subarray(192))
57+
})
58+
}
59+
}
2160

2261
class HumanIDv1Witness extends MerkleWitness(33) { }
2362

@@ -94,5 +133,9 @@ export {
94133
KPassSigners,
95134
PerHumanIDv1Contract,
96135
authenticate,
136+
readField,
137+
readPublicKey,
138+
readSignature,
97139
requireConsistent
98140
};
141+

0 commit comments

Comments
 (0)