Repository navigation
Expand file tree
/
Copy pathutils.ts
More file actions
349 lines (297 loc) · 10.4 KB
/
Copy pathutils.ts
File metadata and controls
349 lines (297 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
import {
Claim,
Id,
SchemaHash,
ClaimOptions,
ElemBytes,
NetworkId,
DidMethod,
Blockchain,
buildDIDType,
idenState,
getDateFromUnixTimestamp
} from '@iden3/js-iden3-core';
import {
Hash,
hashElems,
str2Bytes,
ZERO_HASH,
Merkletree,
InMemoryDB,
Proof
} from '@iden3/js-merkletree';
import { Hex, poseidon, PrivateKey, Signature } from '@iden3/js-crypto';
import { TreeState } from '../../src/circuits';
import { Merklizer } from '@iden3/js-jsonld-merklization';
const TestClaimDocument = `{
"@context": [
"https://www.w3.org/2018/credentials/v1",
"https://w3id.org/citizenship/v1",
"https://w3id.org/security/bbs/v1"
],
"id": "https://issuer.oidp.uscis.gov/credentials/83627465",
"type": ["VerifiableCredential", "PermanentResidentCard"],
"issuer": "did:example:489398593",
"identifier": 83627465,
"name": "Permanent Resident Card",
"description": "Government of Example Permanent Resident Card.",
"issuanceDate": "2019-12-03T12:19:52Z",
"expirationDate": "2029-12-03T12:19:52Z",
"credentialSubject": {
"id": "did:example:b34ca6cd37bbf23",
"type": ["PermanentResident", "Person"],
"givenName": "JOHN",
"familyName": "SMITH",
"gender": "Male",
"image": "data:image/png;base64,iVBORw0KGgokJggg==",
"residentSince": "2015-01-01",
"lprCategory": "C09",
"lprNumber": "999-999-999",
"commuterClassification": "C1",
"birthCountry": "Bahamas",
"birthDate": "1958-07-17"
}
}`;
export function authClaimFromPubKey(x: bigint, y: bigint): Claim {
const schemaHash = new SchemaHash(Hex.decodeString('cca3371a6cb1b715004407e325bd993c'));
// NOTE: We take nonce as hash of public key to make it random
// We don't use random number here because this test vectors will be used for tests
// and have randomization inside tests is usually a bad idea
const revNonce = poseidon.hash([x]);
return Claim.newClaim(
schemaHash,
ClaimOptions.withIndexDataInts(x, y),
ClaimOptions.withRevocationNonce(revNonce)
);
}
export function authV2ClaimFromPubKey(x: bigint, y: bigint): Claim {
const schemaHash = new SchemaHash(SchemaHash.authSchemaHash.bytes);
// NOTE: We take nonce as hash of public key to make it random
// We don't use random number here because this test vectors will be used for tests
// and have randomization inside tests is usually a bad idea
const revNonce = poseidon.hash([x]);
return Claim.newClaim(
schemaHash,
ClaimOptions.withIndexDataInts(x, y),
ClaimOptions.withRevocationNonce(revNonce)
);
}
export function claimsIndexValueHashes(c: Claim): { indexHash: bigint; valueHash: bigint } {
const { index, value } = c.rawSlots();
const indexHash = poseidon.hash(ElemBytes.elemBytesToInts(index));
const valueHash = poseidon.hash(ElemBytes.elemBytesToInts(value));
return { indexHash, valueHash };
}
/*
This method is to generate auth claim, identity, all its trees, state
and sign a challenge with the claim private key.
*/
export async function authClaimFullInfo(
privKeyHex: string,
challenge: bigint
): Promise<{
id: Id;
claim: Claim;
state: Hash;
claimsTree: Merkletree;
revTree: Merkletree;
rootsTree: Merkletree;
claimEntryMTP: Proof;
claimNonRevMTP: Proof;
challengeSignature: Signature;
}> {
const data = await generate(privKeyHex);
//Proof claim exists
const { indexHash } = await claimsIndexValueHashes(data.authClaim);
const claimEntryMTP = await data.claimsTree.generateProof(
indexHash,
await data.claimsTree.root()
);
//Proof claim not revoked
const revNonce = data.authClaim.getRevocationNonce();
const revNonceInt = BigInt(revNonce);
const root = await data.revTree.root();
const claimNonRevMTP = await data.revTree.generateProof(revNonceInt, root);
//Calculate state
const state = await calcStateFromRoots(data.claimsTree, data.revTree, data.rootsTree);
//Calculate signature
const challengeSignature = await data.privateKey.signPoseidon(challenge);
return {
id: data.identity,
claim: data.authClaim,
state,
claimsTree: data.claimsTree,
revTree: data.revTree,
rootsTree: data.rootsTree,
claimEntryMTP: claimEntryMTP.proof,
claimNonRevMTP: claimNonRevMTP.proof,
challengeSignature
};
}
export async function generate(privKeyHex: string): Promise<{
identity: Id;
claimsTree: Merkletree;
revTree: Merkletree;
rootsTree: Merkletree;
authClaim: Claim;
privateKey: PrivateKey;
}> {
// extract pubKey
const privateKey: PrivateKey = new PrivateKey(Hex.decodeString(privKeyHex));
const pubKey = privateKey.public();
const x = pubKey.p[0];
const y = pubKey.p[1];
// init claims tree
const claimsTree = new Merkletree(new InMemoryDB(str2Bytes('')), true, 40);
// create auth claim
const authClaim = authClaimFromPubKey(x, y);
// add auth claim to claimsMT
const { indexHash, valueHash } = claimsIndexValueHashes(authClaim);
await claimsTree.add(indexHash, valueHash);
const ctr = await claimsTree.root();
const state = poseidon.hash([ctr.bigInt(), BigInt(0), BigInt(0)]);
// create new identity
const identity = Id.idGenesisFromIdenState(
buildDIDType(DidMethod.Iden3, Blockchain.Polygon, NetworkId.Amoy),
state
);
const revTree = new Merkletree(new InMemoryDB(str2Bytes('')), true, 40);
const rootsTree = new Merkletree(new InMemoryDB(str2Bytes('')), true, 40);
return { identity, claimsTree, revTree, rootsTree, authClaim, privateKey };
}
async function calcStateFromRoots(claimsTree: Merkletree, ...args: Merkletree[]): Promise<Hash> {
let revTreeRoot = ZERO_HASH.bigInt();
let rootsTreeRoot = ZERO_HASH.bigInt();
if (args.length > 0) {
const root = await args[0].root();
revTreeRoot = root.bigInt();
}
if (args.length > 1) {
const root = await args[1].root();
rootsTreeRoot = root.bigInt();
}
const root = await claimsTree.root();
const state = await hashElems([root.bigInt(), revTreeRoot, rootsTreeRoot]);
return state;
}
export class IdentityTest {
id!: Id;
clt!: Merkletree;
ret!: Merkletree;
rot!: Merkletree;
authClaim!: Claim;
pk!: PrivateKey;
/**
*
*/
constructor(privKHex: string) {
this.pk = new PrivateKey(Hex.decodeString(privKHex));
}
async addClaim(claim: Claim) {
// add claim to claimsMT
const { hi, hv } = claim.hiHv();
await this.clt.add(hi, hv);
}
async claimMTPRaw(claim: Claim): Promise<{ proof: Proof; value: bigint }> {
const { hi } = claim.hiHv();
return await this.clt.generateProof(hi);
}
async claimRevMTPRaw(claim: Claim): Promise<{ proof: Proof; value: bigint }> {
const revNonce = claim.getRevocationNonce();
return await this.ret.generateProof(revNonce);
}
signClaim(claim: Claim): Signature {
const { hi, hv } = claim.hiHv();
const commonHash = poseidon.hash([hi, hv]);
return this.pk.signPoseidon(commonHash);
}
signBJJ(int: bigint): Signature {
return this.pk.signPoseidon(int);
}
static async newIdentity(privKHex: string): Promise<IdentityTest> {
const it = new IdentityTest(privKHex);
// init claims tree
it.clt = new Merkletree(new InMemoryDB(str2Bytes('')), true, 40);
it.ret = new Merkletree(new InMemoryDB(str2Bytes('')), true, 40);
it.rot = new Merkletree(new InMemoryDB(str2Bytes('')), true, 40);
// extract pubKey
const { x, y } = extractPubXY(privKHex);
// create auth claim
const authClaim = authV2ClaimFromPubKey(x, y);
it.authClaim = authClaim;
// add auth claim to claimsMT
const { hi, hv } = authClaim.hiHv();
await it.clt.add(hi, hv);
const state = await it.state();
it.id = idFromState(state.bigInt());
return it;
}
async state(): Promise<Hash> {
const clt = await this.clt.root();
const ret = await this.ret.root();
const rot = await this.rot.root();
const state = idenState(clt.bigInt(), ret.bigInt(), rot.bigInt());
const hash = Hash.fromBigInt(state);
return hash;
}
}
export function extractPubXY(privKHex: string): { key: PrivateKey; x: bigint; y: bigint } {
const k = new PrivateKey(Hex.decodeString(privKHex));
const pk = k.public();
return { key: k, x: pk.p[0], y: pk.p[1] };
}
export function idFromState(state: bigint): Id {
const typ = buildDIDType(DidMethod.Iden3, Blockchain.ReadOnly, NetworkId.NoNetwork);
// create new identity
return Id.idGenesisFromIdenState(typ, state);
}
export async function defaultJSONUserClaim(subject: Id): Promise<{ mz: Merklizer; claim: Claim }> {
const mz = await Merklizer.merklizeJSONLD(TestClaimDocument);
const schemaHash = new SchemaHash(Hex.decodeString('ce6bb12c96bfd1544c02c289c6b4b987'));
const nonce = BigInt(10);
const claim = Claim.newClaim(
schemaHash,
ClaimOptions.withIndexId(subject),
ClaimOptions.withExpirationDate(getDateFromUnixTimestamp(1669884010)), //Thu Dec 01 2022 08:40:10 GMT+0000
ClaimOptions.withRevocationNonce(nonce),
ClaimOptions.withIndexMerklizedRoot((await mz.root()).bigInt())
);
return { mz, claim };
}
export const getTreeState = async (it: IdentityTest): Promise<TreeState> => ({
state: await it.state(),
claimsRoot: await it.clt.root(),
revocationRoot: await it.ret.root(),
rootOfRoots: await it.rot.root()
});
export const userPK = '28156abe7fe2fd433dc9df969286b96666489bac508612d0e16593e944c4f69f';
export const issuerPK = '21a5e7321d0e2f3ca1cc6504396e6594a2211544b08c206847cdee96f832421a';
export const timestamp = 1642074362;
export function defaultUserClaim(subject: Id): Claim {
const dataSlotA = ElemBytes.fromInt(BigInt(10));
const nonce = BigInt(1);
const schemaHash = new SchemaHash(Hex.decodeString('ce6bb12c96bfd1544c02c289c6b4b987'));
const claim = Claim.newClaim(
schemaHash,
ClaimOptions.withIndexId(subject),
ClaimOptions.withIndexData(dataSlotA, new ElemBytes()),
ClaimOptions.withExpirationDate(getDateFromUnixTimestamp(1669884010)), //Thu Dec 01 2022 08:40:10 GMT+0000
ClaimOptions.withRevocationNonce(nonce)
);
return claim;
}
export function prepareIntArray(arr: bigint[], length: number): bigint[] {
// Add the rest of empty levels to the array
for (let i = arr.length; i < length; i++) {
arr.push(BigInt(0));
}
return arr;
}
export function mtHashFromStr(hashStr: string): Hash {
return Hash.fromString(hashStr);
}
export const globalTree = () => new Merkletree(new InMemoryDB(str2Bytes('')), true, 64);
export const coreSchemaFromStr = (schemaIntString: string) => {
const schemaInt = BigInt(schemaIntString);
return SchemaHash.newSchemaHashFromInt(schemaInt);
};