1
+ import { uint8ArrayBEtoBigInt } from "@kimlikdao/lib/util/çevir" ;
1
2
import {
2
3
Field ,
3
4
MerkleWitness ,
@@ -11,25 +12,6 @@ import {
11
12
state ,
12
13
} from "o1js" ;
13
14
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
15
const readField = ( bytes : Uint8Array ) => Field ( uint8ArrayBEtoBigInt ( bytes . subarray ( 0 , 32 ) ) ) ;
34
16
35
17
const readSignature = ( bytes : Uint8Array ) => new Signature (
@@ -60,6 +42,8 @@ class HumanIDv1 extends Struct({
60
42
61
43
class HumanIDv1Witness extends MerkleWitness ( 33 ) { }
62
44
45
+ const Event = Struct ( { uid : Field , value : Field } ) ;
46
+
63
47
const KPassSigners = [
64
48
PrivateKey . fromBigInt ( 1n ) . toPublicKey ( ) ,
65
49
PrivateKey . fromBigInt ( 2n ) . toPublicKey ( ) ,
@@ -79,9 +63,18 @@ const authenticate = (owner: PublicKey, hid: HumanIDv1) => {
79
63
const EmptyRoot =
80
64
Field ( 0x21afce36daa1a2d67391072035f4555a85aea7197e5830b128f121aa382770cdn ) ;
81
65
66
+ // Field(1).div(Field(2 ** 32))
82
67
const Inverse2Exp32 =
83
68
Field ( 0x3fffffffc00000000000000000000000224698fbe706601f8fe037d166d2cf14n ) ;
84
69
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
+
85
78
const requireConsistent = ( humanIDv1 : Field , truncatedHumanIDv1 : Field ) =>
86
79
humanIDv1
87
80
. sub ( truncatedHumanIDv1 )
@@ -93,16 +86,19 @@ const requireConsistent = (humanIDv1: Field, truncatedHumanIDv1: Field) =>
93
86
94
87
class PerHumanIDv1Contract extends SmartContract {
95
88
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
98
91
} ;
99
92
100
93
@state ( Field ) treeRoot = State < Field > ( ) ;
101
94
102
95
init ( ) {
103
96
super . init ( ) ;
104
97
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
+ ) ;
106
102
}
107
103
108
104
acceptHumanIDv1 (
@@ -113,7 +109,10 @@ class PerHumanIDv1Contract extends SmartContract {
113
109
authenticate ( owner , humanIDv1 ) ;
114
110
requireConsistent ( humanIDv1 . id , witness . calculateIndex ( ) ) ;
115
111
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
+ ) ;
117
116
}
118
117
119
118
addToMerkleTree ( witness : HumanIDv1Witness ) {
@@ -134,8 +133,6 @@ export {
134
133
PerHumanIDv1Contract ,
135
134
authenticate ,
136
135
readField ,
137
- readPublicKey ,
138
136
readSignature ,
139
137
requireConsistent
140
138
} ;
141
-
0 commit comments