@0xpolygonid/js-sdk 1.7.5 → 1.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/browser/esm/index.js +13041 -11748
- package/dist/browser/esm/index.js.map +1 -1
- package/dist/browser/umd/index.js +13070 -11755
- package/dist/browser/umd/index.js.map +1 -1
- package/dist/node/cjs/circuits/atomic-query-mtp-v2.js +3 -3
- package/dist/node/cjs/circuits/atomic-query-sig-v2.js +3 -3
- package/dist/node/cjs/circuits/auth-v2.js +1 -1
- package/dist/node/cjs/credentials/credential-wallet.js +1 -3
- package/dist/node/cjs/credentials/index.js +1 -0
- package/dist/node/cjs/credentials/models.js +2 -0
- package/dist/node/cjs/credentials/status/credential-status-publisher.js +23 -4
- package/dist/node/cjs/iden3comm/constants.js +3 -1
- package/dist/node/cjs/iden3comm/handlers/auth.js +119 -14
- package/dist/node/cjs/kms/key-providers/sec256k1-provider.js +5 -2
- package/dist/node/cjs/proof/common.js +3 -4
- package/dist/node/cjs/proof/index.js +2 -1
- package/dist/node/cjs/proof/proof-service.js +22 -2
- package/dist/node/cjs/proof/provers/index.js +19 -0
- package/dist/node/cjs/proof/{inputs-generator.js → provers/inputs-generator.js} +42 -23
- package/dist/node/cjs/proof/{prover.js → provers/prover.js} +1 -1
- package/dist/node/cjs/proof/verifiers/index.js +18 -0
- package/dist/node/cjs/proof/verifiers/pub-signals-verifier.js +344 -0
- package/dist/node/cjs/proof/verifiers/query.js +190 -0
- package/dist/node/cjs/schema-processor/json/parser.js +39 -198
- package/dist/node/cjs/schema-processor/utils.js +13 -26
- package/dist/node/cjs/storage/blockchain/state.js +53 -8
- package/dist/node/cjs/utils/compare-func.js +11 -0
- package/dist/node/cjs/utils/index.js +2 -0
- package/dist/node/cjs/utils/message-bus.js +73 -0
- package/dist/node/cjs/verifiable/core-utils.js +234 -0
- package/dist/node/cjs/verifiable/credential.js +139 -1
- package/dist/node/cjs/verifiable/index.js +1 -0
- package/dist/node/esm/circuits/atomic-query-mtp-v2.js +3 -3
- package/dist/node/esm/circuits/atomic-query-sig-v2.js +3 -3
- package/dist/node/esm/circuits/auth-v2.js +2 -2
- package/dist/node/esm/credentials/credential-wallet.js +1 -3
- package/dist/node/esm/credentials/index.js +1 -0
- package/dist/node/esm/credentials/models.js +1 -0
- package/dist/node/esm/credentials/status/credential-status-publisher.js +23 -4
- package/dist/node/esm/iden3comm/constants.js +3 -1
- package/dist/node/esm/iden3comm/handlers/auth.js +116 -13
- package/dist/node/esm/kms/key-providers/sec256k1-provider.js +5 -2
- package/dist/node/esm/proof/common.js +4 -5
- package/dist/node/esm/proof/index.js +2 -1
- package/dist/node/esm/proof/proof-service.js +22 -2
- package/dist/node/esm/proof/provers/index.js +3 -0
- package/dist/node/esm/proof/{inputs-generator.js → provers/inputs-generator.js} +39 -20
- package/dist/node/esm/proof/{prover.js → provers/prover.js} +1 -1
- package/dist/node/esm/proof/verifiers/index.js +2 -0
- package/dist/node/esm/proof/verifiers/pub-signals-verifier.js +340 -0
- package/dist/node/esm/proof/verifiers/query.js +186 -0
- package/dist/node/esm/schema-processor/json/parser.js +40 -199
- package/dist/node/esm/schema-processor/utils.js +13 -26
- package/dist/node/esm/storage/blockchain/state.js +53 -8
- package/dist/node/esm/utils/compare-func.js +7 -0
- package/dist/node/esm/utils/index.js +2 -0
- package/dist/node/esm/utils/message-bus.js +66 -0
- package/dist/node/esm/verifiable/core-utils.js +200 -0
- package/dist/node/esm/verifiable/credential.js +118 -3
- package/dist/node/esm/verifiable/index.js +1 -0
- package/dist/types/credentials/credential-wallet.d.ts +2 -60
- package/dist/types/credentials/index.d.ts +1 -0
- package/dist/types/credentials/models.d.ts +68 -0
- package/dist/types/credentials/status/credential-status-publisher.d.ts +2 -0
- package/dist/types/iden3comm/constants.d.ts +1 -0
- package/dist/types/iden3comm/handlers/auth.d.ts +53 -13
- package/dist/types/iden3comm/types/protocol/credentials.d.ts +16 -0
- package/dist/types/identity/identity-wallet.d.ts +3 -1
- package/dist/types/kms/key-providers/sec256k1-provider.d.ts +1 -1
- package/dist/types/proof/index.d.ts +2 -1
- package/dist/types/proof/proof-service.d.ts +28 -3
- package/dist/types/proof/provers/index.d.ts +3 -0
- package/dist/types/proof/{inputs-generator.d.ts → provers/inputs-generator.d.ts} +14 -7
- package/dist/types/proof/{prover.d.ts → provers/prover.d.ts} +2 -2
- package/dist/types/proof/verifiers/index.d.ts +2 -0
- package/dist/types/proof/verifiers/pub-signals-verifier.d.ts +58 -0
- package/dist/types/proof/verifiers/query.d.ts +25 -0
- package/dist/types/schema-processor/json/parser.d.ts +47 -28
- package/dist/types/schema-processor/utils.d.ts +9 -4
- package/dist/types/storage/blockchain/state.d.ts +5 -1
- package/dist/types/storage/interfaces/state.d.ts +8 -0
- package/dist/types/utils/compare-func.d.ts +1 -0
- package/dist/types/utils/index.d.ts +2 -0
- package/dist/types/utils/message-bus.d.ts +62 -0
- package/dist/types/verifiable/core-utils.d.ts +84 -0
- package/dist/types/verifiable/credential.d.ts +11 -1
- package/dist/types/verifiable/index.d.ts +1 -0
- package/dist/types/verifiable/proof.d.ts +4 -0
- package/package.json +10 -10
- /package/dist/node/cjs/proof/{witness_calculator.js → provers/witness_calculator.js} +0 -0
- /package/dist/node/esm/proof/{witness_calculator.js → provers/witness_calculator.js} +0 -0
- /package/dist/types/proof/{witness_calculator.d.ts → provers/witness_calculator.d.ts} +0 -0
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
import { BytesHelper, SchemaHash } from '@iden3/js-iden3-core';
|
|
2
|
+
import { Path } from '@iden3/js-jsonld-merklization';
|
|
3
|
+
import { byteDecoder } from '../utils';
|
|
4
|
+
import * as jsonld from 'jsonld/lib';
|
|
5
|
+
import * as ldcontext from 'jsonld/lib/context';
|
|
6
|
+
import { Hex } from '@iden3/js-crypto';
|
|
7
|
+
import { keccak256 } from 'js-sha3';
|
|
8
|
+
const credentialSubjectKey = 'credentialSubject';
|
|
9
|
+
const contextFullKey = '@context';
|
|
10
|
+
const serializationFullKey = 'iden3_serialization';
|
|
11
|
+
const fieldPrefix = 'iden3:v1:';
|
|
12
|
+
const credentialSubjectFullKey = 'https://www.w3.org/2018/credentials#credentialSubject';
|
|
13
|
+
const verifiableCredentialFullKey = 'https://www.w3.org/2018/credentials#VerifiableCredential';
|
|
14
|
+
const typeFullKey = '@type';
|
|
15
|
+
/**
|
|
16
|
+
* GetFieldSlotIndex return index of slot from 0 to 7 (each claim has by default 8 slots) for non-merklized claims
|
|
17
|
+
*
|
|
18
|
+
* @param {string} field - field name
|
|
19
|
+
* @param {Uint8Array} schemaBytes -json schema bytes
|
|
20
|
+
* @returns `number`
|
|
21
|
+
*/
|
|
22
|
+
export const getFieldSlotIndex = async (field, typeName, schemaBytes) => {
|
|
23
|
+
let ctxDoc = JSON.parse(byteDecoder.decode(schemaBytes));
|
|
24
|
+
ctxDoc = ctxDoc[contextFullKey];
|
|
25
|
+
if (ctxDoc === undefined) {
|
|
26
|
+
throw new Error('document has no @context');
|
|
27
|
+
}
|
|
28
|
+
const ldCtx = await jsonld.processContext(ldcontext.getInitialContext({}), ctxDoc, {});
|
|
29
|
+
const serAttr = await getSerializationAttrFromParsedContext(ldCtx, typeName);
|
|
30
|
+
if (!serAttr) {
|
|
31
|
+
throw new Error('serialization attribute is not set');
|
|
32
|
+
}
|
|
33
|
+
const sPaths = parseSerializationAttr(serAttr);
|
|
34
|
+
switch (field) {
|
|
35
|
+
case sPaths.indexAPath:
|
|
36
|
+
return 2;
|
|
37
|
+
case sPaths.indexBPath:
|
|
38
|
+
return 3;
|
|
39
|
+
case sPaths.valueAPath:
|
|
40
|
+
return 6;
|
|
41
|
+
case sPaths.valueBPath:
|
|
42
|
+
return 7;
|
|
43
|
+
default:
|
|
44
|
+
throw new Error(`field ${field} not specified in serialization info`);
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
/**
|
|
48
|
+
* checks if data can fill the slot
|
|
49
|
+
*
|
|
50
|
+
* @param {Uint8Array} slotData - slot data
|
|
51
|
+
* @param {Merklizer} mz - merklizer
|
|
52
|
+
* @param {string} path - path
|
|
53
|
+
* @returns {void}
|
|
54
|
+
*/
|
|
55
|
+
export const fillCoreClaimSlot = async (slotData, mz, path) => {
|
|
56
|
+
if (!path) {
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
path = credentialSubjectKey + '.' + path;
|
|
60
|
+
try {
|
|
61
|
+
const p = await mz.resolveDocPath(path, mz.options);
|
|
62
|
+
const entry = await mz.entry(p);
|
|
63
|
+
const intVal = await entry.getValueMtEntry();
|
|
64
|
+
const bytesVal = BytesHelper.intToBytes(intVal);
|
|
65
|
+
slotData.set(bytesVal, 0);
|
|
66
|
+
}
|
|
67
|
+
catch (err) {
|
|
68
|
+
if (err.toString().includes('entry not found')) {
|
|
69
|
+
throw new Error(`field not found in credential ${path}`);
|
|
70
|
+
}
|
|
71
|
+
throw err;
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
// Get `iden3_serialization` attr definition from context document either using
|
|
75
|
+
// type name like DeliverAddressMultiTestForked or by type id like
|
|
76
|
+
// urn:uuid:ac2ede19-b3b9-454d-b1a9-a7b3d5763100.
|
|
77
|
+
export const getSerializationAttrFromContext = async (context, opts, tp) => {
|
|
78
|
+
const ldCtx = await jsonld.processContext(ldcontext.getInitialContext({}), context, opts);
|
|
79
|
+
return getSerializationAttrFromParsedContext(ldCtx, tp);
|
|
80
|
+
};
|
|
81
|
+
export const getSerializationAttrFromParsedContext = async (ldCtx, tp) => {
|
|
82
|
+
const termDef = ldCtx.mappings;
|
|
83
|
+
if (!termDef) {
|
|
84
|
+
throw new Error('terms definitions is not of correct type');
|
|
85
|
+
}
|
|
86
|
+
const term = termDef.get(tp) ?? [...termDef.values()].find((value) => value['@id'] === tp);
|
|
87
|
+
if (!term) {
|
|
88
|
+
return '';
|
|
89
|
+
}
|
|
90
|
+
const termCtx = term[contextFullKey];
|
|
91
|
+
if (!termCtx) {
|
|
92
|
+
throw new Error('type @context is not of correct type');
|
|
93
|
+
}
|
|
94
|
+
const serStr = termCtx[serializationFullKey] ?? '';
|
|
95
|
+
return serStr;
|
|
96
|
+
};
|
|
97
|
+
export const parseSerializationAttr = (serAttr) => {
|
|
98
|
+
if (!serAttr.startsWith(fieldPrefix)) {
|
|
99
|
+
throw new Error('serialization attribute does not have correct prefix');
|
|
100
|
+
}
|
|
101
|
+
const parts = serAttr.slice(fieldPrefix.length).split('&');
|
|
102
|
+
if (parts.length > 4) {
|
|
103
|
+
throw new Error('serialization attribute has too many parts');
|
|
104
|
+
}
|
|
105
|
+
const paths = {};
|
|
106
|
+
for (const part of parts) {
|
|
107
|
+
const kv = part.split('=');
|
|
108
|
+
if (kv.length !== 2) {
|
|
109
|
+
throw new Error('serialization attribute part does not have correct format');
|
|
110
|
+
}
|
|
111
|
+
switch (kv[0]) {
|
|
112
|
+
case 'slotIndexA':
|
|
113
|
+
paths.indexAPath = kv[1];
|
|
114
|
+
break;
|
|
115
|
+
case 'slotIndexB':
|
|
116
|
+
paths.indexBPath = kv[1];
|
|
117
|
+
break;
|
|
118
|
+
case 'slotValueA':
|
|
119
|
+
paths.valueAPath = kv[1];
|
|
120
|
+
break;
|
|
121
|
+
case 'slotValueB':
|
|
122
|
+
paths.valueBPath = kv[1];
|
|
123
|
+
break;
|
|
124
|
+
default:
|
|
125
|
+
throw new Error('unknown serialization attribute slot');
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
return paths;
|
|
129
|
+
};
|
|
130
|
+
export const findCredentialType = (mz) => {
|
|
131
|
+
const opts = mz.options;
|
|
132
|
+
try {
|
|
133
|
+
// try to look into credentialSubject.@type to get type of credentials
|
|
134
|
+
const path1 = new Path([credentialSubjectFullKey, typeFullKey], opts.hasher);
|
|
135
|
+
const e = mz.rawValue(path1);
|
|
136
|
+
return e;
|
|
137
|
+
}
|
|
138
|
+
catch (err) {
|
|
139
|
+
// if type of credentials not found in credentialSubject.@type, loop at
|
|
140
|
+
// top level @types if it contains two elements: type we are looking for
|
|
141
|
+
// and "VerifiableCredential" type.
|
|
142
|
+
const path2 = new Path([typeFullKey], opts.hasher);
|
|
143
|
+
const topLevelTypes = mz.rawValue(path2);
|
|
144
|
+
if (!Array.isArray(topLevelTypes)) {
|
|
145
|
+
throw new Error('top level @type expected to be an array');
|
|
146
|
+
}
|
|
147
|
+
if (topLevelTypes.length !== 2) {
|
|
148
|
+
throw new Error('top level @type expected to be of length 2');
|
|
149
|
+
}
|
|
150
|
+
switch (verifiableCredentialFullKey) {
|
|
151
|
+
case topLevelTypes[0]:
|
|
152
|
+
return topLevelTypes[1];
|
|
153
|
+
case topLevelTypes[1]:
|
|
154
|
+
return topLevelTypes[0];
|
|
155
|
+
default:
|
|
156
|
+
throw new Error('@type(s) are expected to contain VerifiableCredential type');
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
};
|
|
160
|
+
/**
|
|
161
|
+
* parseCoreClaimSlots converts payload to claim slots using provided schema
|
|
162
|
+
*
|
|
163
|
+
* @param { { mappings: Map<string, Record<string, unknown>> } } ldCtx - ldCtx
|
|
164
|
+
* @param {Merklizer} mz - Merklizer
|
|
165
|
+
* @param {string} credentialType - credential type
|
|
166
|
+
* @returns `Promise<{ slots: ParsedSlots; nonMerklized: boolean }>`
|
|
167
|
+
*/
|
|
168
|
+
export const parseCoreClaimSlots = async (ldCtx, mz, credentialType) => {
|
|
169
|
+
// parseSlots converts payload to claim slots using provided schema
|
|
170
|
+
const slots = {
|
|
171
|
+
indexA: new Uint8Array(32),
|
|
172
|
+
indexB: new Uint8Array(32),
|
|
173
|
+
valueA: new Uint8Array(32),
|
|
174
|
+
valueB: new Uint8Array(32)
|
|
175
|
+
};
|
|
176
|
+
const serAttr = await getSerializationAttrFromParsedContext(ldCtx, credentialType);
|
|
177
|
+
if (!serAttr) {
|
|
178
|
+
return { slots, nonMerklized: false };
|
|
179
|
+
}
|
|
180
|
+
const sPaths = parseSerializationAttr(serAttr);
|
|
181
|
+
const isSPathEmpty = !Object.values(sPaths).some(Boolean);
|
|
182
|
+
if (isSPathEmpty) {
|
|
183
|
+
return { slots, nonMerklized: true };
|
|
184
|
+
}
|
|
185
|
+
await fillCoreClaimSlot(slots.indexA, mz, sPaths.indexAPath);
|
|
186
|
+
await fillCoreClaimSlot(slots.indexB, mz, sPaths.indexBPath);
|
|
187
|
+
await fillCoreClaimSlot(slots.valueA, mz, sPaths.valueAPath);
|
|
188
|
+
await fillCoreClaimSlot(slots.valueB, mz, sPaths.valueBPath);
|
|
189
|
+
return { slots, nonMerklized: true };
|
|
190
|
+
};
|
|
191
|
+
/**
|
|
192
|
+
* Calculates core schema hash
|
|
193
|
+
*
|
|
194
|
+
* @param {Uint8Array} schemaId
|
|
195
|
+
* @returns {*} {SchemaHash}
|
|
196
|
+
*/
|
|
197
|
+
export const caclulateCoreSchemaHash = (schemaId) => {
|
|
198
|
+
const sHash = Hex.decodeString(keccak256(schemaId));
|
|
199
|
+
return new SchemaHash(sHash.slice(sHash.length - 16, sHash.length));
|
|
200
|
+
};
|
|
@@ -1,12 +1,15 @@
|
|
|
1
1
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
2
|
import { BJJSignatureProof2021, Iden3SparseMerkleTreeProof } from './proof';
|
|
3
|
-
import { Claim } from '@iden3/js-iden3-core';
|
|
4
|
-
import { ProofType } from './constants';
|
|
3
|
+
import { Claim, DID, MerklizedRootPosition as MerklizedRootPositionCore, IdPosition, ClaimOptions } from '@iden3/js-iden3-core';
|
|
5
4
|
import { Hash, rootFromProof, verifyProof } from '@iden3/js-merkletree';
|
|
6
5
|
import { Merklizer } from '@iden3/js-jsonld-merklization';
|
|
7
6
|
import { PublicKey, poseidon } from '@iden3/js-crypto';
|
|
8
7
|
import { getUserDIDFromCredential } from '../credentials/utils';
|
|
9
|
-
import { validateDIDDocumentAuth } from '../utils';
|
|
8
|
+
import { byteEncoder, validateDIDDocumentAuth } from '../utils';
|
|
9
|
+
import { MerklizedRootPosition, ProofType, SubjectPosition } from './constants';
|
|
10
|
+
import { caclulateCoreSchemaHash, findCredentialType, parseCoreClaimSlots } from './core-utils';
|
|
11
|
+
import * as jsonld from 'jsonld/lib';
|
|
12
|
+
import * as ldcontext from 'jsonld/lib/context';
|
|
10
13
|
/**
|
|
11
14
|
* W3C Verifiable credential
|
|
12
15
|
*
|
|
@@ -86,6 +89,77 @@ export class W3CCredential {
|
|
|
86
89
|
}
|
|
87
90
|
return undefined;
|
|
88
91
|
}
|
|
92
|
+
/**
|
|
93
|
+
* gets core claim representation from W3CCredential
|
|
94
|
+
*
|
|
95
|
+
* @param {CoreClaimParsingOptions} [opts] - options to create core claim
|
|
96
|
+
* @returns {*} {(Promise<Claim>)}
|
|
97
|
+
*/
|
|
98
|
+
async toCoreClaim(opts) {
|
|
99
|
+
if (!opts) {
|
|
100
|
+
opts = {
|
|
101
|
+
revNonce: 0,
|
|
102
|
+
version: 0,
|
|
103
|
+
subjectPosition: SubjectPosition.Index,
|
|
104
|
+
merklizedRootPosition: MerklizedRootPosition.None,
|
|
105
|
+
updatable: false,
|
|
106
|
+
merklizeOpts: {}
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
const mz = await this.merklize(opts.merklizeOpts);
|
|
110
|
+
const credentialType = findCredentialType(mz);
|
|
111
|
+
const subjectId = this.credentialSubject['id'];
|
|
112
|
+
const ldCtx = await jsonld.processContext(ldcontext.getInitialContext({}), this['@context'], mz.options);
|
|
113
|
+
const { slots, nonMerklized } = await parseCoreClaimSlots(ldCtx, mz, credentialType);
|
|
114
|
+
// if schema is for non merklized credential, root position must be set to none ('')
|
|
115
|
+
// otherwise default position for merklized position is index.
|
|
116
|
+
if (nonMerklized && opts.merklizedRootPosition !== MerklizedRootPosition.None) {
|
|
117
|
+
throw new Error('merklized root position is not supported for non-merklized claims');
|
|
118
|
+
}
|
|
119
|
+
if (!nonMerklized && opts.merklizedRootPosition === MerklizedRootPosition.None) {
|
|
120
|
+
opts.merklizedRootPosition = MerklizedRootPosition.Index;
|
|
121
|
+
}
|
|
122
|
+
const schemaHash = caclulateCoreSchemaHash(byteEncoder.encode(credentialType));
|
|
123
|
+
const claim = Claim.newClaim(schemaHash, ClaimOptions.withIndexDataBytes(slots.indexA, slots.indexB), ClaimOptions.withValueDataBytes(slots.valueA, slots.valueB), ClaimOptions.withRevocationNonce(BigInt(opts.revNonce)), ClaimOptions.withVersion(opts.version));
|
|
124
|
+
if (opts.updatable) {
|
|
125
|
+
claim.setFlagUpdatable(opts.updatable);
|
|
126
|
+
}
|
|
127
|
+
if (this.expirationDate) {
|
|
128
|
+
claim.setExpirationDate(new Date(this.expirationDate));
|
|
129
|
+
}
|
|
130
|
+
if (subjectId) {
|
|
131
|
+
const did = DID.parse(subjectId.toString());
|
|
132
|
+
const id = DID.idFromDID(did);
|
|
133
|
+
switch (opts.subjectPosition) {
|
|
134
|
+
case '':
|
|
135
|
+
case SubjectPosition.Index:
|
|
136
|
+
claim.setIndexId(id);
|
|
137
|
+
break;
|
|
138
|
+
case SubjectPosition.Value:
|
|
139
|
+
claim.setValueId(id);
|
|
140
|
+
break;
|
|
141
|
+
default:
|
|
142
|
+
throw new Error('unknown subject position');
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
switch (opts.merklizedRootPosition) {
|
|
146
|
+
case MerklizedRootPosition.Index: {
|
|
147
|
+
const mk = await this.merklize(opts.merklizeOpts);
|
|
148
|
+
claim.setIndexMerklizedRoot((await mk.root()).bigInt());
|
|
149
|
+
break;
|
|
150
|
+
}
|
|
151
|
+
case MerklizedRootPosition.Value: {
|
|
152
|
+
const mk = await this.merklize(opts.merklizeOpts);
|
|
153
|
+
claim.setValueMerklizedRoot((await mk.root()).bigInt());
|
|
154
|
+
break;
|
|
155
|
+
}
|
|
156
|
+
case MerklizedRootPosition.None:
|
|
157
|
+
break;
|
|
158
|
+
default:
|
|
159
|
+
throw new Error('unknown merklized root position');
|
|
160
|
+
}
|
|
161
|
+
return claim;
|
|
162
|
+
}
|
|
89
163
|
/**
|
|
90
164
|
* checks BJJSignatureProof2021 in W3C VC
|
|
91
165
|
*
|
|
@@ -124,6 +198,7 @@ export class W3CCredential {
|
|
|
124
198
|
if (!coreClaim) {
|
|
125
199
|
throw new Error(`can't get core claim`);
|
|
126
200
|
}
|
|
201
|
+
await this.verifyCoreClaimMatch(coreClaim, opts?.merklizeOptions);
|
|
127
202
|
switch (proofType) {
|
|
128
203
|
case ProofType.BJJSignature: {
|
|
129
204
|
if (!opts?.credStatusResolverRegistry) {
|
|
@@ -141,6 +216,46 @@ export class W3CCredential {
|
|
|
141
216
|
}
|
|
142
217
|
}
|
|
143
218
|
}
|
|
219
|
+
async verifyCoreClaimMatch(coreClaim, merklizeOpts) {
|
|
220
|
+
let merklizedRootPosition = '';
|
|
221
|
+
const merklizedPosition = coreClaim.getMerklizedPosition();
|
|
222
|
+
switch (merklizedPosition) {
|
|
223
|
+
case MerklizedRootPositionCore.None:
|
|
224
|
+
merklizedRootPosition = MerklizedRootPosition.None;
|
|
225
|
+
break;
|
|
226
|
+
case MerklizedRootPositionCore.Index:
|
|
227
|
+
merklizedRootPosition = MerklizedRootPosition.Index;
|
|
228
|
+
break;
|
|
229
|
+
case MerklizedRootPositionCore.Value:
|
|
230
|
+
merklizedRootPosition = MerklizedRootPosition.Value;
|
|
231
|
+
break;
|
|
232
|
+
}
|
|
233
|
+
let subjectPosition = '';
|
|
234
|
+
const idPosition = coreClaim.getIdPosition();
|
|
235
|
+
switch (idPosition) {
|
|
236
|
+
case IdPosition.None:
|
|
237
|
+
subjectPosition = SubjectPosition.None;
|
|
238
|
+
break;
|
|
239
|
+
case IdPosition.Index:
|
|
240
|
+
subjectPosition = SubjectPosition.Index;
|
|
241
|
+
break;
|
|
242
|
+
case IdPosition.Value:
|
|
243
|
+
subjectPosition = SubjectPosition.Value;
|
|
244
|
+
break;
|
|
245
|
+
}
|
|
246
|
+
const coreClaimOpts = {
|
|
247
|
+
revNonce: Number(coreClaim.getRevocationNonce()),
|
|
248
|
+
version: coreClaim.getVersion(),
|
|
249
|
+
merklizedRootPosition,
|
|
250
|
+
subjectPosition,
|
|
251
|
+
updatable: coreClaim.getFlagUpdatable(),
|
|
252
|
+
merklizeOpts: merklizeOpts
|
|
253
|
+
};
|
|
254
|
+
const credentialCoreClaim = await this.toCoreClaim(coreClaimOpts);
|
|
255
|
+
if (coreClaim.hex() != credentialCoreClaim.hex()) {
|
|
256
|
+
throw new Error('proof generated for another credential');
|
|
257
|
+
}
|
|
258
|
+
}
|
|
144
259
|
async verifyBJJSignatureProof(proof, coreClaim, resolverURL, userDID, credStatusResolverRegistry) {
|
|
145
260
|
// issuer auth claim
|
|
146
261
|
const authClaim = proof.issuerData.authCoreClaim;
|
|
@@ -1,68 +1,10 @@
|
|
|
1
1
|
import { DID } from '@iden3/js-iden3-core';
|
|
2
2
|
import { IDataStorage } from '../storage/interfaces';
|
|
3
|
-
import { W3CCredential, ProofQuery,
|
|
3
|
+
import { W3CCredential, ProofQuery, CredentialStatus, RevocationStatus } from './../verifiable';
|
|
4
4
|
import { JSONSchema } from '../schema-processor';
|
|
5
5
|
import { CredentialStatusResolverRegistry } from './status/resolver';
|
|
6
6
|
import { CredentialStatusResolveOptions } from './status/resolver';
|
|
7
|
-
|
|
8
|
-
* Request to core library to create Core Claim from W3C Verifiable Credential
|
|
9
|
-
*
|
|
10
|
-
* @public
|
|
11
|
-
* @interface CredentialRequest
|
|
12
|
-
*/
|
|
13
|
-
export interface CredentialRequest {
|
|
14
|
-
/**
|
|
15
|
-
* JSON credential schema
|
|
16
|
-
*/
|
|
17
|
-
credentialSchema: string;
|
|
18
|
-
/**
|
|
19
|
-
* Credential type
|
|
20
|
-
*/
|
|
21
|
-
type: string;
|
|
22
|
-
/**
|
|
23
|
-
* Credential subject, usually contains claims and identifier
|
|
24
|
-
*/
|
|
25
|
-
credentialSubject: {
|
|
26
|
-
[key: string]: string | object | number | boolean;
|
|
27
|
-
};
|
|
28
|
-
/**
|
|
29
|
-
* expiration time
|
|
30
|
-
*/
|
|
31
|
-
expiration?: number;
|
|
32
|
-
/**
|
|
33
|
-
* refreshService
|
|
34
|
-
*/
|
|
35
|
-
refreshService?: RefreshService;
|
|
36
|
-
/**
|
|
37
|
-
* claim version
|
|
38
|
-
*/
|
|
39
|
-
version?: number;
|
|
40
|
-
/**
|
|
41
|
-
* subject position (index / value / none)
|
|
42
|
-
*/
|
|
43
|
-
subjectPosition?: SubjectPosition;
|
|
44
|
-
/**
|
|
45
|
-
* merklizedRootPosition (index / value / none)
|
|
46
|
-
*/
|
|
47
|
-
merklizedRootPosition?: MerklizedRootPosition;
|
|
48
|
-
/**
|
|
49
|
-
* Revocation options
|
|
50
|
-
*
|
|
51
|
-
* @type {{
|
|
52
|
-
* id: string;
|
|
53
|
-
* nonce?: number;
|
|
54
|
-
* type: CredentialStatusType;
|
|
55
|
-
* issuerState?: string;
|
|
56
|
-
* }}
|
|
57
|
-
* @memberof CredentialRequest
|
|
58
|
-
*/
|
|
59
|
-
revocationOpts: {
|
|
60
|
-
id: string;
|
|
61
|
-
nonce?: number;
|
|
62
|
-
type: CredentialStatusType;
|
|
63
|
-
issuerState?: string;
|
|
64
|
-
};
|
|
65
|
-
}
|
|
7
|
+
import { CredentialRequest } from './models';
|
|
66
8
|
/**
|
|
67
9
|
* Interface to work with credential wallets
|
|
68
10
|
*
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { CredentialStatusType, MerklizedRootPosition, RefreshService, SubjectPosition } from '../verifiable';
|
|
2
|
+
/**
|
|
3
|
+
* Represents the publish mode for identity wallet.
|
|
4
|
+
* It can be one of the following values: 'sync', 'async', or 'callback'.
|
|
5
|
+
* 'sync' - publish the status synchronously
|
|
6
|
+
* 'async' - publish the status asynchronously via message bus
|
|
7
|
+
* 'callback' - publish the status with a txCallback
|
|
8
|
+
*/
|
|
9
|
+
export type PublishMode = 'sync' | 'async' | 'callback';
|
|
10
|
+
/**
|
|
11
|
+
* Request to core library to create Core Claim from W3C Verifiable Credential
|
|
12
|
+
*
|
|
13
|
+
* @public
|
|
14
|
+
* @interface CredentialRequest
|
|
15
|
+
*/
|
|
16
|
+
export interface CredentialRequest {
|
|
17
|
+
/**
|
|
18
|
+
* JSON credential schema
|
|
19
|
+
*/
|
|
20
|
+
credentialSchema: string;
|
|
21
|
+
/**
|
|
22
|
+
* Credential type
|
|
23
|
+
*/
|
|
24
|
+
type: string;
|
|
25
|
+
/**
|
|
26
|
+
* Credential subject, usually contains claims and identifier
|
|
27
|
+
*/
|
|
28
|
+
credentialSubject: {
|
|
29
|
+
[key: string]: string | object | number | boolean;
|
|
30
|
+
};
|
|
31
|
+
/**
|
|
32
|
+
* expiration time
|
|
33
|
+
*/
|
|
34
|
+
expiration?: number;
|
|
35
|
+
/**
|
|
36
|
+
* refreshService
|
|
37
|
+
*/
|
|
38
|
+
refreshService?: RefreshService;
|
|
39
|
+
/**
|
|
40
|
+
* claim version
|
|
41
|
+
*/
|
|
42
|
+
version?: number;
|
|
43
|
+
/**
|
|
44
|
+
* subject position (index / value / none)
|
|
45
|
+
*/
|
|
46
|
+
subjectPosition?: SubjectPosition;
|
|
47
|
+
/**
|
|
48
|
+
* merklizedRootPosition (index / value / none)
|
|
49
|
+
*/
|
|
50
|
+
merklizedRootPosition?: MerklizedRootPosition;
|
|
51
|
+
/**
|
|
52
|
+
* Revocation options
|
|
53
|
+
*
|
|
54
|
+
* @type {{
|
|
55
|
+
* id: string;
|
|
56
|
+
* nonce?: number;
|
|
57
|
+
* type: CredentialStatusType;
|
|
58
|
+
* issuerState?: string;
|
|
59
|
+
* }}
|
|
60
|
+
* @memberof CredentialRequest
|
|
61
|
+
*/
|
|
62
|
+
revocationOpts: {
|
|
63
|
+
id: string;
|
|
64
|
+
nonce?: number;
|
|
65
|
+
type: CredentialStatusType;
|
|
66
|
+
issuerState?: string;
|
|
67
|
+
};
|
|
68
|
+
}
|
|
@@ -3,6 +3,7 @@ import { JSONObject } from '../../iden3comm';
|
|
|
3
3
|
import { OnChainRevocationStorage } from '../../storage';
|
|
4
4
|
import { CredentialStatusType } from '../../verifiable';
|
|
5
5
|
import { ProofNode } from './reverse-sparse-merkle-tree';
|
|
6
|
+
import { PublishMode } from '../models';
|
|
6
7
|
/**
|
|
7
8
|
* Represents a credential status publisher.
|
|
8
9
|
*/
|
|
@@ -47,6 +48,7 @@ export declare class Iden3OnchainSmtCredentialStatusPublisher implements ICreden
|
|
|
47
48
|
credentialStatusType: CredentialStatusType;
|
|
48
49
|
onChain?: {
|
|
49
50
|
txCallback?: (tx: TransactionReceipt) => Promise<void>;
|
|
51
|
+
publishMode?: PublishMode;
|
|
50
52
|
};
|
|
51
53
|
}): Promise<void>;
|
|
52
54
|
}
|
|
@@ -16,6 +16,7 @@ export declare const PROTOCOL_MESSAGE_TYPE: Readonly<{
|
|
|
16
16
|
REVOCATION_STATUS_REQUEST_MESSAGE_TYPE: string;
|
|
17
17
|
REVOCATION_STATUS_RESPONSE_MESSAGE_TYPE: string;
|
|
18
18
|
CONTRACT_INVOKE_REQUEST_MESSAGE_TYPE: string;
|
|
19
|
+
CREDENTIAL_ONCHAIN_OFFER_MESSAGE_TYPE: string;
|
|
19
20
|
}>;
|
|
20
21
|
/**
|
|
21
22
|
* Media types for iden3 comm communication protocol
|
|
@@ -2,6 +2,34 @@ import { MediaType } from '../constants';
|
|
|
2
2
|
import { IProofService } from '../../proof/proof-service';
|
|
3
3
|
import { AuthorizationRequestMessage, AuthorizationResponseMessage, IPackageManager, JWSPackerParams } from '../types';
|
|
4
4
|
import { DID } from '@iden3/js-iden3-core';
|
|
5
|
+
/**
|
|
6
|
+
* createAuthorizationRequest is a function to create protocol authorization request
|
|
7
|
+
* @param {string} reason - reason to request proof
|
|
8
|
+
* @param {string} sender - sender did
|
|
9
|
+
* @param {string} callbackUrl - callback that user should use to send response
|
|
10
|
+
* @returns `Promise<AuthorizationRequestMessage>`
|
|
11
|
+
*/
|
|
12
|
+
export declare function createAuthorizationRequest(reason: string, sender: string, callbackUrl: string): AuthorizationRequestMessage;
|
|
13
|
+
/**
|
|
14
|
+
* createAuthorizationRequestWithMessage is a function to create protocol authorization request with explicit message to sign
|
|
15
|
+
* @param {string} reason - reason to request proof
|
|
16
|
+
* @param {string} message - message to sign in the response
|
|
17
|
+
* @param {string} sender - sender did
|
|
18
|
+
* @param {string} callbackUrl - callback that user should use to send response
|
|
19
|
+
* @returns `Promise<AuthorizationRequestMessage>`
|
|
20
|
+
*/
|
|
21
|
+
export declare function createAuthorizationRequestWithMessage(reason: string, message: string, sender: string, callbackUrl: string): AuthorizationRequestMessage;
|
|
22
|
+
/**
|
|
23
|
+
*
|
|
24
|
+
* Options to pass to auth response handler
|
|
25
|
+
*
|
|
26
|
+
* @public
|
|
27
|
+
* @interface AuthResponseHandlerOptions
|
|
28
|
+
*/
|
|
29
|
+
export interface AuthResponseHandlerOptions {
|
|
30
|
+
acceptedStateTransitionDelay?: number;
|
|
31
|
+
acceptedProofGenerationDelay?: number;
|
|
32
|
+
}
|
|
5
33
|
/**
|
|
6
34
|
* Interface that allows the processing of the authorization request in the raw format for given identifier
|
|
7
35
|
*
|
|
@@ -32,6 +60,21 @@ export interface IAuthHandler {
|
|
|
32
60
|
authRequest: AuthorizationRequestMessage;
|
|
33
61
|
authResponse: AuthorizationResponseMessage;
|
|
34
62
|
}>;
|
|
63
|
+
/**
|
|
64
|
+
* handle authorization response
|
|
65
|
+
* @public
|
|
66
|
+
* @param {AuthorizationResponseMessage} response - auth response
|
|
67
|
+
* @param {AuthorizationRequestMessage} request - auth request
|
|
68
|
+
* @param {AuthResponseHandlerOptions} opts - options
|
|
69
|
+
* @returns `Promise<{
|
|
70
|
+
request: AuthorizationRequestMessage;
|
|
71
|
+
response: AuthorizationResponseMessage;
|
|
72
|
+
}>`
|
|
73
|
+
*/
|
|
74
|
+
handleAuthorizationResponse(response: AuthorizationResponseMessage, request: AuthorizationRequestMessage, opts?: AuthResponseHandlerOptions): Promise<{
|
|
75
|
+
request: AuthorizationRequestMessage;
|
|
76
|
+
response: AuthorizationResponseMessage;
|
|
77
|
+
}>;
|
|
35
78
|
}
|
|
36
79
|
/**
|
|
37
80
|
*
|
|
@@ -64,26 +107,23 @@ export declare class AuthHandler implements IAuthHandler {
|
|
|
64
107
|
*/
|
|
65
108
|
constructor(_packerMgr: IPackageManager, _proofService: IProofService);
|
|
66
109
|
/**
|
|
67
|
-
*
|
|
68
|
-
* @public
|
|
69
|
-
* @param {Uint8Array} request - raw byte message
|
|
70
|
-
* @returns `Promise<AuthorizationRequestMessage>`
|
|
110
|
+
* @inheritdoc IAuthHandler#parseAuthorizationRequest
|
|
71
111
|
*/
|
|
72
112
|
parseAuthorizationRequest(request: Uint8Array): Promise<AuthorizationRequestMessage>;
|
|
73
113
|
/**
|
|
74
|
-
*
|
|
75
|
-
* @public
|
|
76
|
-
* @param {did} did - sender DID
|
|
77
|
-
* @param {Uint8Array} request - raw byte message
|
|
78
|
-
* @returns `Promise<{
|
|
79
|
-
token: string;
|
|
80
|
-
authRequest: AuthorizationRequestMessage;
|
|
81
|
-
authResponse: AuthorizationResponseMessage;
|
|
82
|
-
}>`
|
|
114
|
+
* @inheritdoc IAuthHandler#handleAuthorizationRequest
|
|
83
115
|
*/
|
|
84
116
|
handleAuthorizationRequest(did: DID, request: Uint8Array, opts?: AuthHandlerOptions): Promise<{
|
|
85
117
|
token: string;
|
|
86
118
|
authRequest: AuthorizationRequestMessage;
|
|
87
119
|
authResponse: AuthorizationResponseMessage;
|
|
88
120
|
}>;
|
|
121
|
+
/**
|
|
122
|
+
* @inheritdoc IAuthHandler#handleAuthorizationResponse
|
|
123
|
+
*/
|
|
124
|
+
handleAuthorizationResponse(response: AuthorizationResponseMessage, request: AuthorizationRequestMessage, opts?: AuthResponseHandlerOptions | undefined): Promise<{
|
|
125
|
+
request: AuthorizationRequestMessage;
|
|
126
|
+
response: AuthorizationResponseMessage;
|
|
127
|
+
}>;
|
|
128
|
+
private verifyAuthRequest;
|
|
89
129
|
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { W3CCredential } from '../../../verifiable';
|
|
2
2
|
import { MediaType } from '../../constants';
|
|
3
3
|
import { JSONObject, ProtocolMessage } from '../packer';
|
|
4
|
+
import { ContractInvokeTransactionData } from './contract-request';
|
|
4
5
|
/** CredentialIssuanceRequestMessageBody represents data for credential issuance request */
|
|
5
6
|
export type CredentialIssuanceRequestMessageBody = {
|
|
6
7
|
schema: Schema;
|
|
@@ -32,6 +33,21 @@ export type CredentialsOfferMessageBody = {
|
|
|
32
33
|
url: string;
|
|
33
34
|
credentials: Array<CredentialOffer>;
|
|
34
35
|
};
|
|
36
|
+
/** CredentialsOnchainOfferMessage represent Iden3message for credential onhcain offer message */
|
|
37
|
+
export type CredentialsOnchainOfferMessage = {
|
|
38
|
+
id: string;
|
|
39
|
+
typ: MediaType;
|
|
40
|
+
type: ProtocolMessage;
|
|
41
|
+
thid?: string;
|
|
42
|
+
body: CredentialsOnchainOfferMessageBody;
|
|
43
|
+
from: string;
|
|
44
|
+
to: string;
|
|
45
|
+
};
|
|
46
|
+
/** CredentialsOnchainOfferMessageBody is struct the represents onchain offer message body */
|
|
47
|
+
export type CredentialsOnchainOfferMessageBody = {
|
|
48
|
+
credentials: Array<CredentialOffer>;
|
|
49
|
+
transaction_data: ContractInvokeTransactionData;
|
|
50
|
+
};
|
|
35
51
|
/** CredentialOffer is structure to fetch credential */
|
|
36
52
|
export type CredentialOffer = {
|
|
37
53
|
id: string;
|
|
@@ -3,7 +3,7 @@ import { Claim, DID } from '@iden3/js-iden3-core';
|
|
|
3
3
|
import { Signature } from '@iden3/js-crypto';
|
|
4
4
|
import { IDataStorage, Profile } from '../storage';
|
|
5
5
|
import { W3CCredential, MerkleTreeProofWithTreeState, CredentialStatusType, ProofQuery } from '../verifiable';
|
|
6
|
-
import { CredentialRequest, ICredentialWallet, TreesModel } from '../credentials';
|
|
6
|
+
import { CredentialRequest, ICredentialWallet, PublishMode, TreesModel } from '../credentials';
|
|
7
7
|
import { TreeState } from '../circuits';
|
|
8
8
|
import { Options } from '@iden3/js-jsonld-merklization';
|
|
9
9
|
import { TransactionReceipt } from 'ethers';
|
|
@@ -25,6 +25,7 @@ export type IdentityCreationOptions = {
|
|
|
25
25
|
nonce?: number;
|
|
26
26
|
onChain?: {
|
|
27
27
|
txCallback?: (tx: TransactionReceipt) => Promise<void>;
|
|
28
|
+
publishMode?: PublishMode;
|
|
28
29
|
};
|
|
29
30
|
};
|
|
30
31
|
seed?: Uint8Array;
|
|
@@ -38,6 +39,7 @@ export type RevocationInfoOptions = {
|
|
|
38
39
|
rhsUrl?: string;
|
|
39
40
|
onChain?: {
|
|
40
41
|
txCallback?: (tx: TransactionReceipt) => Promise<void>;
|
|
42
|
+
publishMode?: PublishMode;
|
|
41
43
|
};
|
|
42
44
|
};
|
|
43
45
|
/**
|
|
@@ -25,7 +25,7 @@ export declare class Sec256k1Provider implements IKeyProvider {
|
|
|
25
25
|
* @param {Uint8Array} seed - byte array seed
|
|
26
26
|
* @returns kms key identifier
|
|
27
27
|
*/
|
|
28
|
-
newPrivateKeyFromSeed(): Promise<KmsKeyId>;
|
|
28
|
+
newPrivateKeyFromSeed(seed: Uint8Array): Promise<KmsKeyId>;
|
|
29
29
|
/**
|
|
30
30
|
* Gets public key by kmsKeyId
|
|
31
31
|
*
|