@docknetwork/wallet-sdk-wasm 1.4.0 → 1.5.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/lib/{account-3c88155a.js → account-bf00e5c0.js} +5 -5
- package/lib/{account-8f5cbc4d.js → account-e48dc9b6.js} +5 -5
- package/lib/core/polkadot-utils.js +18 -4
- package/lib/core/polkadot-utils.mjs +15 -2
- package/lib/index.js +14 -17
- package/lib/index.mjs +14 -17
- package/lib/modules/account.js +9 -5
- package/lib/modules/account.mjs +9 -5
- package/lib/modules/accounts.js +9 -5
- package/lib/modules/accounts.mjs +9 -5
- package/lib/modules/network-manager.js +4 -2
- package/lib/modules/network-manager.mjs +4 -2
- package/lib/modules/wallet.js +12 -8
- package/lib/modules/wallet.mjs +12 -8
- package/lib/rpc-server.js +14 -17
- package/lib/rpc-server.mjs +14 -17
- package/lib/services/blockchain/configs.js +7 -0
- package/lib/services/blockchain/configs.mjs +3 -0
- package/lib/services/{dock → blockchain}/index.android.js +3 -4
- package/lib/services/{dock → blockchain}/index.android.mjs +3 -4
- package/lib/services/{dock → blockchain}/index.ios.js +3 -4
- package/lib/services/{dock → blockchain}/index.ios.mjs +3 -4
- package/lib/services/blockchain/index.js +30 -0
- package/lib/services/blockchain/index.mjs +22 -0
- package/lib/services/{dock → blockchain}/service-rpc.js +5 -7
- package/lib/services/{dock → blockchain}/service-rpc.mjs +5 -7
- package/lib/services/blockchain/service.js +212 -0
- package/lib/services/blockchain/service.mjs +206 -0
- package/lib/services/credential/bbs-revocation.js +33 -61
- package/lib/services/credential/bbs-revocation.mjs +34 -62
- package/lib/services/credential/bound-check.js +0 -8
- package/lib/services/credential/bound-check.mjs +1 -5
- package/lib/services/credential/index.js +20 -23
- package/lib/services/credential/index.mjs +20 -23
- package/lib/services/credential/pex-helpers.js +134 -34
- package/lib/services/credential/pex-helpers.mjs +133 -35
- package/lib/services/credential/service.js +54 -55
- package/lib/services/credential/service.mjs +43 -41
- package/lib/services/credential/utils.js +12 -18
- package/lib/services/credential/utils.mjs +11 -14
- package/lib/services/dids/config.js +8 -0
- package/lib/services/dids/config.mjs +8 -0
- package/lib/services/dids/index.js +8 -7
- package/lib/services/dids/index.mjs +8 -7
- package/lib/services/dids/service-rpc.js +2 -2
- package/lib/services/dids/service-rpc.mjs +2 -2
- package/lib/services/dids/service.js +22 -32
- package/lib/services/dids/service.mjs +22 -32
- package/lib/services/edv/hmac.js +5 -0
- package/lib/services/edv/hmac.mjs +5 -0
- package/lib/services/edv/index.js +10 -8
- package/lib/services/edv/index.mjs +10 -8
- package/lib/services/edv/service-rpc.js +4 -0
- package/lib/services/edv/service-rpc.mjs +4 -0
- package/lib/services/edv/service.js +26 -10
- package/lib/services/edv/service.mjs +25 -10
- package/lib/services/example/service.js +3 -1
- package/lib/services/example/service.mjs +3 -1
- package/lib/services/index.js +12 -15
- package/lib/services/index.mjs +12 -15
- package/lib/services/keyring/configs.js +12 -0
- package/lib/services/keyring/configs.mjs +12 -0
- package/lib/services/keyring/service.js +5 -0
- package/lib/services/keyring/service.mjs +5 -0
- package/lib/services/pex/service.js +17 -15
- package/lib/services/pex/service.mjs +17 -15
- package/lib/services/substrate/api-utils.js +18 -6
- package/lib/services/substrate/api-utils.mjs +18 -6
- package/lib/services/substrate/index.js +13 -6
- package/lib/services/substrate/index.mjs +13 -6
- package/lib/services/substrate/service.js +16 -9
- package/lib/services/substrate/service.mjs +16 -9
- package/lib/services/test-utils.js +27 -18
- package/lib/services/test-utils.mjs +27 -18
- package/lib/services/trust-registry/index.js +16 -5
- package/lib/services/trust-registry/index.mjs +16 -5
- package/lib/services/trust-registry/service.js +26 -10
- package/lib/services/trust-registry/service.mjs +26 -10
- package/lib/services/util-crypto/configs.js +3 -0
- package/lib/services/util-crypto/configs.mjs +3 -0
- package/lib/services/util-crypto/service-rpc.js +6 -0
- package/lib/services/util-crypto/service-rpc.mjs +6 -0
- package/lib/services/util-crypto/service.js +5 -0
- package/lib/services/util-crypto/service.mjs +6 -1
- package/lib/setup-nodejs.js +14 -17
- package/lib/setup-nodejs.mjs +14 -17
- package/lib/setup-tests.js +14 -17
- package/lib/setup-tests.mjs +14 -17
- package/lib/src/core/polkadot-utils.d.ts +1 -0
- package/lib/src/core/polkadot-utils.d.ts.map +1 -1
- package/lib/src/modules/network-manager.d.ts +1 -0
- package/lib/src/modules/network-manager.d.ts.map +1 -1
- package/lib/src/services/blockchain/configs.d.ts +8 -0
- package/lib/src/services/blockchain/configs.d.ts.map +1 -0
- package/lib/src/services/blockchain/index.d.ts +2 -0
- package/lib/src/services/blockchain/index.d.ts.map +1 -0
- package/lib/src/services/{dock → blockchain}/service.d.ts +12 -11
- package/lib/src/services/blockchain/service.d.ts.map +1 -0
- package/lib/src/services/credential/bbs-revocation.d.ts.map +1 -1
- package/lib/src/services/credential/bound-check.d.ts +0 -4
- package/lib/src/services/credential/bound-check.d.ts.map +1 -1
- package/lib/src/services/credential/index.d.ts +2 -7
- package/lib/src/services/credential/index.d.ts.map +1 -1
- package/lib/src/services/credential/pex-helpers.d.ts +5 -3
- package/lib/src/services/credential/pex-helpers.d.ts.map +1 -1
- package/lib/src/services/credential/service.d.ts +2 -7
- package/lib/src/services/credential/service.d.ts.map +1 -1
- package/lib/src/services/credential/utils.d.ts.map +1 -1
- package/lib/src/services/dids/config.d.ts +5 -0
- package/lib/src/services/dids/config.d.ts.map +1 -1
- package/lib/src/services/dids/service-rpc.d.ts +1 -1
- package/lib/src/services/dids/service-rpc.d.ts.map +1 -1
- package/lib/src/services/dids/service.d.ts +9 -7
- package/lib/src/services/dids/service.d.ts.map +1 -1
- package/lib/src/services/edv/hmac.d.ts +1 -0
- package/lib/src/services/edv/hmac.d.ts.map +1 -1
- package/lib/src/services/edv/service.d.ts +5 -0
- package/lib/src/services/edv/service.d.ts.map +1 -1
- package/lib/src/services/example/service.d.ts.map +1 -1
- package/lib/src/services/keyring/configs.d.ts +6 -0
- package/lib/src/services/keyring/configs.d.ts.map +1 -1
- package/lib/src/services/pex/service.d.ts.map +1 -1
- package/lib/src/services/trust-registry/service.d.ts.map +1 -1
- package/lib/src/services/util-crypto/configs.d.ts +1 -0
- package/lib/src/services/util-crypto/configs.d.ts.map +1 -1
- package/lib/src/services/util-crypto/service.d.ts +1 -0
- package/lib/src/services/util-crypto/service.d.ts.map +1 -1
- package/lib/test/setup-test-state.js +10 -6
- package/lib/test/setup-test-state.mjs +10 -6
- package/lib/tsconfig.tsbuildinfo +1 -1
- package/package.json +5 -7
- package/src/core/polkadot-utils.js +14 -3
- package/src/modules/network-manager.ts +5 -2
- package/src/modules/wallet.ts +5 -5
- package/src/services/blockchain/configs.ts +11 -0
- package/src/services/blockchain/index.android.js +3 -0
- package/src/services/blockchain/index.ios.js +3 -0
- package/src/services/blockchain/index.js +1 -0
- package/src/services/blockchain/index.test.js +34 -0
- package/src/services/{dock → blockchain}/service-rpc.js +5 -6
- package/src/services/blockchain/service.ts +261 -0
- package/src/services/credential/bbs-revocation.ts +42 -90
- package/src/services/credential/bound-check.test.ts +2 -5
- package/src/services/credential/bound-check.ts +0 -5
- package/src/services/credential/index.test.js +148 -7
- package/src/services/credential/pex-helpers.js +132 -34
- package/src/services/credential/pex-helpers.test.js +284 -27
- package/src/services/credential/service.ts +44 -27
- package/src/services/credential/utils.js +5 -14
- package/src/services/dids/config.ts +15 -0
- package/src/services/dids/index.test.js +5 -46
- package/src/services/dids/service-rpc.ts +2 -2
- package/src/services/dids/service.ts +17 -34
- package/src/services/edv/hmac.ts +13 -0
- package/src/services/edv/index.test.js +22 -0
- package/src/services/edv/service-rpc.js +4 -0
- package/src/services/edv/service.ts +22 -0
- package/src/services/example/service.ts +3 -1
- package/src/services/index.js +2 -2
- package/src/services/keyring/configs.ts +21 -0
- package/src/services/keyring/service.ts +13 -0
- package/src/services/pex/service.ts +29 -18
- package/src/services/pex/tests/pex-service.test.js +163 -6
- package/src/services/substrate/api-utils.test.js +7 -7
- package/src/services/substrate/api-utils.ts +3 -3
- package/src/services/substrate/service.ts +6 -6
- package/src/services/test-utils.js +13 -11
- package/src/services/trust-registry/index.test.js +6 -4
- package/src/services/trust-registry/service.ts +13 -7
- package/src/services/util-crypto/configs.ts +4 -0
- package/src/services/util-crypto/service-rpc.js +6 -0
- package/src/services/util-crypto/service.ts +8 -0
- package/src/test/setup-test-state.js +2 -2
- package/lib/services/dock/configs.js +0 -18
- package/lib/services/dock/configs.mjs +0 -10
- package/lib/services/dock/index.js +0 -18
- package/lib/services/dock/index.mjs +0 -10
- package/lib/services/dock/service.js +0 -136
- package/lib/services/dock/service.mjs +0 -128
- package/lib/src/services/dock/configs.d.ts +0 -7
- package/lib/src/services/dock/configs.d.ts.map +0 -1
- package/lib/src/services/dock/index.d.ts +0 -2
- package/lib/src/services/dock/index.d.ts.map +0 -1
- package/lib/src/services/dock/service.d.ts.map +0 -1
- package/src/services/dock/configs.ts +0 -12
- package/src/services/dock/index.android.js +0 -3
- package/src/services/dock/index.ios.js +0 -3
- package/src/services/dock/index.js +0 -1
- package/src/services/dock/index.test.js +0 -48
- package/src/services/dock/service.ts +0 -159
|
@@ -4,13 +4,15 @@ Object.defineProperty(exports, '__esModule', { value: true });
|
|
|
4
4
|
|
|
5
5
|
var jsonpath = require('@astronautlabs/jsonpath');
|
|
6
6
|
|
|
7
|
-
const EPSILON_NUMBER = 0.
|
|
7
|
+
const EPSILON_NUMBER = 0.001;
|
|
8
8
|
const EPSILON_INT = 1;
|
|
9
9
|
|
|
10
10
|
const MAX_DATE_PLACEHOLDER = 884541351600000;
|
|
11
11
|
const MIN_DATE_PLACEHOLDER = -17592186044415;
|
|
12
|
-
const
|
|
13
|
-
const
|
|
12
|
+
const MAX_INTEGER = 100 ** 9;
|
|
13
|
+
const MIN_INTEGER = -4294967295;
|
|
14
|
+
const MAX_NUMBER = 100 ** 5;
|
|
15
|
+
const MIN_NUMBER = -4294967294;
|
|
14
16
|
|
|
15
17
|
/*
|
|
16
18
|
PEX Filter rules:
|
|
@@ -25,6 +27,15 @@ function correctFieldPath(path) {
|
|
|
25
27
|
return path.replace('$.', '');
|
|
26
28
|
}
|
|
27
29
|
|
|
30
|
+
function getNumDecimalPlaces(n) {
|
|
31
|
+
const parts = n.toString().split('.');
|
|
32
|
+
return parts.length > 1 && parts[1].length;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function toMaxDecimalPlaces(n, maxDecimalPlaces) {
|
|
36
|
+
return +n.toFixed(maxDecimalPlaces);
|
|
37
|
+
}
|
|
38
|
+
|
|
28
39
|
function getAttributeName({field, selectedCredentials, index}) {
|
|
29
40
|
let attributeName;
|
|
30
41
|
if (Array.isArray(field.path) && field.path.length > 1) {
|
|
@@ -56,18 +67,40 @@ function getAttributeName({field, selectedCredentials, index}) {
|
|
|
56
67
|
function pexToBounds(
|
|
57
68
|
pexRequest,
|
|
58
69
|
selectedCredentials = [],
|
|
59
|
-
|
|
70
|
+
removeFromRequest = false,
|
|
60
71
|
) {
|
|
61
72
|
const descriptorBounds = [];
|
|
62
|
-
const
|
|
63
|
-
MIN_DATE_PLACEHOLDER,
|
|
64
|
-
MAX_DATE_PLACEHOLDER,
|
|
65
|
-
MIN_NUMBER,
|
|
66
|
-
MAX_NUMBER,
|
|
67
|
-
].map(value => (isRawBoundies ? undefined : value));
|
|
73
|
+
const fieldsToRemove = [];
|
|
68
74
|
|
|
69
75
|
// One list of bounds per descriptor/credential
|
|
70
76
|
pexRequest.input_descriptors.forEach((inputDescriptor, index) => {
|
|
77
|
+
const selectedCredential = selectedCredentials[index];
|
|
78
|
+
if (!selectedCredential) {
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// Get embedded schema if existing
|
|
83
|
+
let decodedSchema = {};
|
|
84
|
+
if (selectedCredential.credentialSchema) {
|
|
85
|
+
const schemaStartStr = 'data:application/json;charset=utf-8,';
|
|
86
|
+
|
|
87
|
+
if (selectedCredential.credentialSchema.details) {
|
|
88
|
+
decodedSchema =
|
|
89
|
+
JSON.parse(selectedCredential.credentialSchema.details).jsonSchema ||
|
|
90
|
+
{};
|
|
91
|
+
} else if (
|
|
92
|
+
selectedCredential.credentialSchema.id &&
|
|
93
|
+
selectedCredential.credentialSchema.id.startsWith(schemaStartStr)
|
|
94
|
+
) {
|
|
95
|
+
// LEGACY embedded schema handling
|
|
96
|
+
decodedSchema = JSON.parse(
|
|
97
|
+
decodeURIComponent(
|
|
98
|
+
selectedCredential.credentialSchema.id.split(schemaStartStr)[1],
|
|
99
|
+
),
|
|
100
|
+
);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
71
104
|
const bounds = [];
|
|
72
105
|
inputDescriptor.constraints.fields.forEach(field => {
|
|
73
106
|
const {
|
|
@@ -81,6 +114,44 @@ function pexToBounds(
|
|
|
81
114
|
type,
|
|
82
115
|
} = field.filter || {};
|
|
83
116
|
|
|
117
|
+
if (!field.path || field.path.length === 0) {
|
|
118
|
+
throw new Error(
|
|
119
|
+
'Missing or empty field "path" property, expected array or string',
|
|
120
|
+
);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
let attributeName;
|
|
124
|
+
if (Array.isArray(field.path) && field.path.length > 1) {
|
|
125
|
+
// Must find a path that matches in selectedcredential, and choose that for bounds
|
|
126
|
+
const pathCount = field.path.length;
|
|
127
|
+
for (let i = 0; i < pathCount; i++) {
|
|
128
|
+
const path = field.path[i];
|
|
129
|
+
const paths = jsonpath.JSONPath.paths(selectedCredential, path);
|
|
130
|
+
if (paths.length) {
|
|
131
|
+
// First come first served
|
|
132
|
+
attributeName = correctFieldPath(jsonpath.JSONPath.stringify(paths[0]));
|
|
133
|
+
break;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
} else {
|
|
137
|
+
const path = Array.isArray(field.path) ? field.path[0] : field.path;
|
|
138
|
+
attributeName = correctFieldPath(path);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
if (!attributeName) {
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
const schemaPath = `$.properties.${attributeName.replaceAll(
|
|
146
|
+
'.',
|
|
147
|
+
'.properties.',
|
|
148
|
+
)}`;
|
|
149
|
+
const attributeSchema = jsonpath.JSONPath.query(
|
|
150
|
+
decodedSchema,
|
|
151
|
+
schemaPath,
|
|
152
|
+
1,
|
|
153
|
+
)[0] || {type};
|
|
154
|
+
|
|
84
155
|
let max =
|
|
85
156
|
maximum === undefined
|
|
86
157
|
? formatMaximum === undefined
|
|
@@ -99,62 +170,89 @@ function pexToBounds(
|
|
|
99
170
|
return;
|
|
100
171
|
}
|
|
101
172
|
|
|
173
|
+
const attributeType = attributeSchema.type || type;
|
|
174
|
+
|
|
102
175
|
// Get min/max bounds values, if using exclusive we must apply an epsilon so equality isnt true
|
|
103
176
|
if (format === 'date-time' || format === 'date') {
|
|
104
|
-
max = max === undefined ?
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
177
|
+
max = new Date(max === undefined ? MAX_DATE_PLACEHOLDER : max);
|
|
178
|
+
min = new Date(min === undefined ? MIN_DATE_PLACEHOLDER : min);
|
|
179
|
+
} else if (attributeType === 'number') {
|
|
180
|
+
const epsilon = attributeSchema.multipleOf || EPSILON_NUMBER;
|
|
181
|
+
const attribDecimalPlaces = getNumDecimalPlaces(epsilon);
|
|
182
|
+
|
|
109
183
|
max =
|
|
110
184
|
max === undefined
|
|
111
|
-
?
|
|
112
|
-
|
|
185
|
+
? attributeSchema.maximum === undefined
|
|
186
|
+
? MAX_NUMBER
|
|
187
|
+
: attributeSchema.maximum
|
|
188
|
+
: exclusiveMaximum === undefined
|
|
113
189
|
? max
|
|
114
|
-
: max -
|
|
190
|
+
: max - epsilon;
|
|
115
191
|
min =
|
|
116
192
|
min === undefined
|
|
117
|
-
?
|
|
118
|
-
|
|
193
|
+
? attributeSchema.minimum === undefined
|
|
194
|
+
? MIN_NUMBER
|
|
195
|
+
: attributeSchema.minimum
|
|
196
|
+
: exclusiveMinimum === undefined
|
|
119
197
|
? min
|
|
120
|
-
: min +
|
|
121
|
-
|
|
198
|
+
: min + epsilon;
|
|
199
|
+
|
|
200
|
+
// Because of floating point math sucks, sometimes we can get extra decimal points
|
|
201
|
+
// the bounds must also match the same decimal points as the input
|
|
202
|
+
// for this we read from the embedded schema
|
|
203
|
+
min = toMaxDecimalPlaces(min, attribDecimalPlaces);
|
|
204
|
+
max = toMaxDecimalPlaces(max, attribDecimalPlaces);
|
|
205
|
+
} else if (attributeType === 'integer') {
|
|
122
206
|
max =
|
|
123
207
|
max === undefined
|
|
124
|
-
?
|
|
125
|
-
|
|
208
|
+
? attributeSchema.maximum === undefined
|
|
209
|
+
? MAX_INTEGER
|
|
210
|
+
: attributeSchema.maximum
|
|
211
|
+
: exclusiveMaximum === undefined
|
|
126
212
|
? max
|
|
127
213
|
: max - EPSILON_INT;
|
|
128
214
|
min =
|
|
129
215
|
min === undefined
|
|
130
|
-
?
|
|
131
|
-
|
|
216
|
+
? attributeSchema.minimum === undefined
|
|
217
|
+
? MIN_INTEGER
|
|
218
|
+
: attributeSchema.minimum
|
|
219
|
+
: exclusiveMinimum === undefined
|
|
132
220
|
? min
|
|
133
221
|
: min + EPSILON_INT;
|
|
222
|
+
|
|
223
|
+
// Ensure that input values are not decimals otherwise crypto-wasm-ts will complain
|
|
224
|
+
min = Math.floor(min);
|
|
225
|
+
max = Math.floor(max);
|
|
134
226
|
} else {
|
|
135
227
|
throw new Error(
|
|
136
228
|
`Unsupported format ${format} and type ${type} for enforce bounds`,
|
|
137
229
|
);
|
|
138
230
|
}
|
|
139
231
|
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
232
|
+
if (removeFromRequest) {
|
|
233
|
+
fieldsToRemove.push({
|
|
234
|
+
fields: inputDescriptor.constraints.fields,
|
|
235
|
+
field,
|
|
236
|
+
});
|
|
237
|
+
}
|
|
145
238
|
|
|
146
239
|
bounds.push({
|
|
147
240
|
attributeName,
|
|
148
241
|
min,
|
|
149
242
|
max,
|
|
150
|
-
type,
|
|
151
|
-
format,
|
|
152
243
|
});
|
|
153
244
|
});
|
|
154
245
|
|
|
155
246
|
descriptorBounds.push(bounds);
|
|
156
247
|
});
|
|
157
248
|
|
|
249
|
+
fieldsToRemove.forEach(({fields, field}) => {
|
|
250
|
+
const idx = fields.indexOf(field);
|
|
251
|
+
if (idx !== -1) {
|
|
252
|
+
fields.splice(idx, 1);
|
|
253
|
+
}
|
|
254
|
+
});
|
|
255
|
+
|
|
158
256
|
return descriptorBounds;
|
|
159
257
|
}
|
|
160
258
|
|
|
@@ -174,7 +272,7 @@ function getPexRequiredAttributes(pexRequest, selectedCredentials = []) {
|
|
|
174
272
|
.map((inputDescriptor, index) => {
|
|
175
273
|
return inputDescriptor.constraints.fields
|
|
176
274
|
.filter(field => {
|
|
177
|
-
if (field.filter) {
|
|
275
|
+
if (field.filter || field.optional) {
|
|
178
276
|
return false;
|
|
179
277
|
}
|
|
180
278
|
|
|
@@ -206,8 +304,10 @@ function getPexRequiredAttributes(pexRequest, selectedCredentials = []) {
|
|
|
206
304
|
exports.EPSILON_INT = EPSILON_INT;
|
|
207
305
|
exports.EPSILON_NUMBER = EPSILON_NUMBER;
|
|
208
306
|
exports.MAX_DATE_PLACEHOLDER = MAX_DATE_PLACEHOLDER;
|
|
307
|
+
exports.MAX_INTEGER = MAX_INTEGER;
|
|
209
308
|
exports.MAX_NUMBER = MAX_NUMBER;
|
|
210
309
|
exports.MIN_DATE_PLACEHOLDER = MIN_DATE_PLACEHOLDER;
|
|
310
|
+
exports.MIN_INTEGER = MIN_INTEGER;
|
|
211
311
|
exports.MIN_NUMBER = MIN_NUMBER;
|
|
212
312
|
exports.getPexRequiredAttributes = getPexRequiredAttributes;
|
|
213
313
|
exports.pexToBounds = pexToBounds;
|
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
import { JSONPath } from '@astronautlabs/jsonpath';
|
|
2
2
|
|
|
3
|
-
const EPSILON_NUMBER = 0.
|
|
3
|
+
const EPSILON_NUMBER = 0.001;
|
|
4
4
|
const EPSILON_INT = 1;
|
|
5
5
|
|
|
6
6
|
const MAX_DATE_PLACEHOLDER = 884541351600000;
|
|
7
7
|
const MIN_DATE_PLACEHOLDER = -17592186044415;
|
|
8
|
-
const
|
|
9
|
-
const
|
|
8
|
+
const MAX_INTEGER = 100 ** 9;
|
|
9
|
+
const MIN_INTEGER = -4294967295;
|
|
10
|
+
const MAX_NUMBER = 100 ** 5;
|
|
11
|
+
const MIN_NUMBER = -4294967294;
|
|
10
12
|
|
|
11
13
|
/*
|
|
12
14
|
PEX Filter rules:
|
|
@@ -21,6 +23,15 @@ function correctFieldPath(path) {
|
|
|
21
23
|
return path.replace('$.', '');
|
|
22
24
|
}
|
|
23
25
|
|
|
26
|
+
function getNumDecimalPlaces(n) {
|
|
27
|
+
const parts = n.toString().split('.');
|
|
28
|
+
return parts.length > 1 && parts[1].length;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function toMaxDecimalPlaces(n, maxDecimalPlaces) {
|
|
32
|
+
return +n.toFixed(maxDecimalPlaces);
|
|
33
|
+
}
|
|
34
|
+
|
|
24
35
|
function getAttributeName({field, selectedCredentials, index}) {
|
|
25
36
|
let attributeName;
|
|
26
37
|
if (Array.isArray(field.path) && field.path.length > 1) {
|
|
@@ -52,18 +63,40 @@ function getAttributeName({field, selectedCredentials, index}) {
|
|
|
52
63
|
function pexToBounds(
|
|
53
64
|
pexRequest,
|
|
54
65
|
selectedCredentials = [],
|
|
55
|
-
|
|
66
|
+
removeFromRequest = false,
|
|
56
67
|
) {
|
|
57
68
|
const descriptorBounds = [];
|
|
58
|
-
const
|
|
59
|
-
MIN_DATE_PLACEHOLDER,
|
|
60
|
-
MAX_DATE_PLACEHOLDER,
|
|
61
|
-
MIN_NUMBER,
|
|
62
|
-
MAX_NUMBER,
|
|
63
|
-
].map(value => (isRawBoundies ? undefined : value));
|
|
69
|
+
const fieldsToRemove = [];
|
|
64
70
|
|
|
65
71
|
// One list of bounds per descriptor/credential
|
|
66
72
|
pexRequest.input_descriptors.forEach((inputDescriptor, index) => {
|
|
73
|
+
const selectedCredential = selectedCredentials[index];
|
|
74
|
+
if (!selectedCredential) {
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// Get embedded schema if existing
|
|
79
|
+
let decodedSchema = {};
|
|
80
|
+
if (selectedCredential.credentialSchema) {
|
|
81
|
+
const schemaStartStr = 'data:application/json;charset=utf-8,';
|
|
82
|
+
|
|
83
|
+
if (selectedCredential.credentialSchema.details) {
|
|
84
|
+
decodedSchema =
|
|
85
|
+
JSON.parse(selectedCredential.credentialSchema.details).jsonSchema ||
|
|
86
|
+
{};
|
|
87
|
+
} else if (
|
|
88
|
+
selectedCredential.credentialSchema.id &&
|
|
89
|
+
selectedCredential.credentialSchema.id.startsWith(schemaStartStr)
|
|
90
|
+
) {
|
|
91
|
+
// LEGACY embedded schema handling
|
|
92
|
+
decodedSchema = JSON.parse(
|
|
93
|
+
decodeURIComponent(
|
|
94
|
+
selectedCredential.credentialSchema.id.split(schemaStartStr)[1],
|
|
95
|
+
),
|
|
96
|
+
);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
67
100
|
const bounds = [];
|
|
68
101
|
inputDescriptor.constraints.fields.forEach(field => {
|
|
69
102
|
const {
|
|
@@ -77,6 +110,44 @@ function pexToBounds(
|
|
|
77
110
|
type,
|
|
78
111
|
} = field.filter || {};
|
|
79
112
|
|
|
113
|
+
if (!field.path || field.path.length === 0) {
|
|
114
|
+
throw new Error(
|
|
115
|
+
'Missing or empty field "path" property, expected array or string',
|
|
116
|
+
);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
let attributeName;
|
|
120
|
+
if (Array.isArray(field.path) && field.path.length > 1) {
|
|
121
|
+
// Must find a path that matches in selectedcredential, and choose that for bounds
|
|
122
|
+
const pathCount = field.path.length;
|
|
123
|
+
for (let i = 0; i < pathCount; i++) {
|
|
124
|
+
const path = field.path[i];
|
|
125
|
+
const paths = JSONPath.paths(selectedCredential, path);
|
|
126
|
+
if (paths.length) {
|
|
127
|
+
// First come first served
|
|
128
|
+
attributeName = correctFieldPath(JSONPath.stringify(paths[0]));
|
|
129
|
+
break;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
} else {
|
|
133
|
+
const path = Array.isArray(field.path) ? field.path[0] : field.path;
|
|
134
|
+
attributeName = correctFieldPath(path);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
if (!attributeName) {
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
const schemaPath = `$.properties.${attributeName.replaceAll(
|
|
142
|
+
'.',
|
|
143
|
+
'.properties.',
|
|
144
|
+
)}`;
|
|
145
|
+
const attributeSchema = JSONPath.query(
|
|
146
|
+
decodedSchema,
|
|
147
|
+
schemaPath,
|
|
148
|
+
1,
|
|
149
|
+
)[0] || {type};
|
|
150
|
+
|
|
80
151
|
let max =
|
|
81
152
|
maximum === undefined
|
|
82
153
|
? formatMaximum === undefined
|
|
@@ -95,62 +166,89 @@ function pexToBounds(
|
|
|
95
166
|
return;
|
|
96
167
|
}
|
|
97
168
|
|
|
169
|
+
const attributeType = attributeSchema.type || type;
|
|
170
|
+
|
|
98
171
|
// Get min/max bounds values, if using exclusive we must apply an epsilon so equality isnt true
|
|
99
172
|
if (format === 'date-time' || format === 'date') {
|
|
100
|
-
max = max === undefined ?
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
173
|
+
max = new Date(max === undefined ? MAX_DATE_PLACEHOLDER : max);
|
|
174
|
+
min = new Date(min === undefined ? MIN_DATE_PLACEHOLDER : min);
|
|
175
|
+
} else if (attributeType === 'number') {
|
|
176
|
+
const epsilon = attributeSchema.multipleOf || EPSILON_NUMBER;
|
|
177
|
+
const attribDecimalPlaces = getNumDecimalPlaces(epsilon);
|
|
178
|
+
|
|
105
179
|
max =
|
|
106
180
|
max === undefined
|
|
107
|
-
?
|
|
108
|
-
|
|
181
|
+
? attributeSchema.maximum === undefined
|
|
182
|
+
? MAX_NUMBER
|
|
183
|
+
: attributeSchema.maximum
|
|
184
|
+
: exclusiveMaximum === undefined
|
|
109
185
|
? max
|
|
110
|
-
: max -
|
|
186
|
+
: max - epsilon;
|
|
111
187
|
min =
|
|
112
188
|
min === undefined
|
|
113
|
-
?
|
|
114
|
-
|
|
189
|
+
? attributeSchema.minimum === undefined
|
|
190
|
+
? MIN_NUMBER
|
|
191
|
+
: attributeSchema.minimum
|
|
192
|
+
: exclusiveMinimum === undefined
|
|
115
193
|
? min
|
|
116
|
-
: min +
|
|
117
|
-
|
|
194
|
+
: min + epsilon;
|
|
195
|
+
|
|
196
|
+
// Because of floating point math sucks, sometimes we can get extra decimal points
|
|
197
|
+
// the bounds must also match the same decimal points as the input
|
|
198
|
+
// for this we read from the embedded schema
|
|
199
|
+
min = toMaxDecimalPlaces(min, attribDecimalPlaces);
|
|
200
|
+
max = toMaxDecimalPlaces(max, attribDecimalPlaces);
|
|
201
|
+
} else if (attributeType === 'integer') {
|
|
118
202
|
max =
|
|
119
203
|
max === undefined
|
|
120
|
-
?
|
|
121
|
-
|
|
204
|
+
? attributeSchema.maximum === undefined
|
|
205
|
+
? MAX_INTEGER
|
|
206
|
+
: attributeSchema.maximum
|
|
207
|
+
: exclusiveMaximum === undefined
|
|
122
208
|
? max
|
|
123
209
|
: max - EPSILON_INT;
|
|
124
210
|
min =
|
|
125
211
|
min === undefined
|
|
126
|
-
?
|
|
127
|
-
|
|
212
|
+
? attributeSchema.minimum === undefined
|
|
213
|
+
? MIN_INTEGER
|
|
214
|
+
: attributeSchema.minimum
|
|
215
|
+
: exclusiveMinimum === undefined
|
|
128
216
|
? min
|
|
129
217
|
: min + EPSILON_INT;
|
|
218
|
+
|
|
219
|
+
// Ensure that input values are not decimals otherwise crypto-wasm-ts will complain
|
|
220
|
+
min = Math.floor(min);
|
|
221
|
+
max = Math.floor(max);
|
|
130
222
|
} else {
|
|
131
223
|
throw new Error(
|
|
132
224
|
`Unsupported format ${format} and type ${type} for enforce bounds`,
|
|
133
225
|
);
|
|
134
226
|
}
|
|
135
227
|
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
228
|
+
if (removeFromRequest) {
|
|
229
|
+
fieldsToRemove.push({
|
|
230
|
+
fields: inputDescriptor.constraints.fields,
|
|
231
|
+
field,
|
|
232
|
+
});
|
|
233
|
+
}
|
|
141
234
|
|
|
142
235
|
bounds.push({
|
|
143
236
|
attributeName,
|
|
144
237
|
min,
|
|
145
238
|
max,
|
|
146
|
-
type,
|
|
147
|
-
format,
|
|
148
239
|
});
|
|
149
240
|
});
|
|
150
241
|
|
|
151
242
|
descriptorBounds.push(bounds);
|
|
152
243
|
});
|
|
153
244
|
|
|
245
|
+
fieldsToRemove.forEach(({fields, field}) => {
|
|
246
|
+
const idx = fields.indexOf(field);
|
|
247
|
+
if (idx !== -1) {
|
|
248
|
+
fields.splice(idx, 1);
|
|
249
|
+
}
|
|
250
|
+
});
|
|
251
|
+
|
|
154
252
|
return descriptorBounds;
|
|
155
253
|
}
|
|
156
254
|
|
|
@@ -170,7 +268,7 @@ function getPexRequiredAttributes(pexRequest, selectedCredentials = []) {
|
|
|
170
268
|
.map((inputDescriptor, index) => {
|
|
171
269
|
return inputDescriptor.constraints.fields
|
|
172
270
|
.filter(field => {
|
|
173
|
-
if (field.filter) {
|
|
271
|
+
if (field.filter || field.optional) {
|
|
174
272
|
return false;
|
|
175
273
|
}
|
|
176
274
|
|
|
@@ -199,4 +297,4 @@ function getPexRequiredAttributes(pexRequest, selectedCredentials = []) {
|
|
|
199
297
|
.filter(requiredAttributes => requiredAttributes.length > 0);
|
|
200
298
|
}
|
|
201
299
|
|
|
202
|
-
export { EPSILON_INT, EPSILON_NUMBER, MAX_DATE_PLACEHOLDER, MAX_NUMBER, MIN_DATE_PLACEHOLDER, MIN_NUMBER, getPexRequiredAttributes, pexToBounds };
|
|
300
|
+
export { EPSILON_INT, EPSILON_NUMBER, MAX_DATE_PLACEHOLDER, MAX_INTEGER, MAX_NUMBER, MIN_DATE_PLACEHOLDER, MIN_INTEGER, MIN_NUMBER, getPexRequiredAttributes, pexToBounds };
|