@@ -11,13 +11,52 @@ import {
11
11
state ,
12
12
} from "o1js" ;
13
13
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
+
14
40
class HumanIDv1 extends Struct ( {
15
41
id : Field ,
16
42
commitmentR : Field ,
17
43
sig0 : Signature ,
18
44
sig1 : Signature ,
19
45
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
+ }
21
60
22
61
class HumanIDv1Witness extends MerkleWitness ( 33 ) { }
23
62
@@ -94,5 +133,9 @@ export {
94
133
KPassSigners ,
95
134
PerHumanIDv1Contract ,
96
135
authenticate ,
136
+ readField ,
137
+ readPublicKey ,
138
+ readSignature ,
97
139
requireConsistent
98
140
} ;
141
+
0 commit comments