This repository has been archived by the owner on Oct 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 41
/
BbsBlsSignature2020.ts
413 lines (368 loc) · 11.1 KB
/
BbsBlsSignature2020.ts
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
/*
* Copyright 2020 - MATTR Limited
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* eslint-disable @typescript-eslint/no-explicit-any */
import jsonld from "jsonld";
import { suites, SECURITY_CONTEXT_URL } from "jsonld-signatures";
import {
SignatureSuiteOptions,
CreateProofOptions,
CanonizeOptions,
CreateVerifyDataOptions,
VerifyProofOptions,
VerifySignatureOptions,
SuiteSignOptions,
} from "./types";
import { w3cDate } from "./utilities";
import { Bls12381G2KeyPair } from "@mattrglobal/bls12381-key-pair";
/**
* A BBS+ signature suite for use with BLS12-381 key pairs
*/
export class BbsBlsSignature2020 extends suites.LinkedDataProof {
/**
* Default constructor
* @param options {SignatureSuiteOptions} options for constructing the signature suite
*/
constructor(options: SignatureSuiteOptions = {}) {
const {
verificationMethod,
signer,
key,
date,
useNativeCanonize,
LDKeyClass,
} = options;
// validate common options
if (
verificationMethod !== undefined &&
typeof verificationMethod !== "string"
) {
throw new TypeError('"verificationMethod" must be a URL string.');
}
super({
type: "sec:BbsBlsSignature2020",
});
this.proof = {
"@context": [
{
sec: "https://w3id.org/security#",
proof: {
"@id": "sec:proof",
"@type": "@id",
"@container": "@graph",
},
},
"https://w3id.org/security/bbs/v1",
],
type: "BbsBlsSignature2020",
};
this.LDKeyClass = LDKeyClass ?? Bls12381G2KeyPair;
this.signer = signer;
this.verificationMethod = verificationMethod;
this.proofSignatureKey = "proofValue";
if (key) {
if (verificationMethod === undefined) {
this.verificationMethod = key.id;
}
this.key = key;
if (typeof key.signer === "function") {
this.signer = key.signer();
}
if (typeof key.verifier === "function") {
this.verifier = key.verifier();
}
}
if (date) {
this.date = new Date(date);
if (isNaN(this.date)) {
throw TypeError(`"date" "${date}" is not a valid date.`);
}
}
this.useNativeCanonize = useNativeCanonize;
}
/**
* @param options {CreateProofOptions} options for creating the proof
*
* @returns {Promise<object>} Resolves with the created proof object.
*/
async createProof(options: CreateProofOptions): Promise<object> {
const { document, purpose, documentLoader, expansionMap, compactProof } =
options;
let proof;
if (this.proof) {
// use proof JSON-LD document passed to API
proof = await jsonld.compact(this.proof, SECURITY_CONTEXT_URL, {
documentLoader,
expansionMap,
compactToRelative: false,
});
} else {
// create proof JSON-LD document
proof = { "@context": SECURITY_CONTEXT_URL };
}
// ensure proof type is set
proof.type = this.type;
// set default `now` date if not given in `proof` or `options`
let date = this.date;
if (proof.created === undefined && date === undefined) {
date = new Date();
}
// ensure date is in string format
if (date !== undefined && typeof date !== "string") {
date = w3cDate(date);
}
// add API overrides
if (date !== undefined) {
proof.created = date;
}
if (this.verificationMethod !== undefined) {
proof.verificationMethod = this.verificationMethod;
}
// allow purpose to update the proof; the `proof` is in the
// SECURITY_CONTEXT_URL `@context` -- therefore the `purpose` must
// ensure any added fields are also represented in that same `@context`
proof = await purpose.update(proof, {
document,
suite: this,
documentLoader,
expansionMap,
});
// create data to sign
const verifyData = (
await this.createVerifyData({
document,
proof,
documentLoader,
expansionMap,
compactProof,
})
).map((item) => new Uint8Array(Buffer.from(item)));
// sign data
proof = await this.sign({
verifyData,
document,
proof,
documentLoader,
expansionMap,
});
return proof;
}
/**
* @param options {object} options for verifying the proof.
*
* @returns {Promise<{object}>} Resolves with the verification result.
*/
async verifyProof(options: VerifyProofOptions): Promise<object> {
const { proof, document, documentLoader, expansionMap, purpose } = options;
try {
// create data to verify
const verifyData = (
await this.createVerifyData({
document,
proof,
documentLoader,
expansionMap,
compactProof: false,
})
).map((item) => new Uint8Array(Buffer.from(item)));
// fetch verification method
const verificationMethod = await this.getVerificationMethod({
proof,
document,
documentLoader,
expansionMap,
});
// verify signature on data
const verified = await this.verifySignature({
verifyData,
verificationMethod,
document,
proof,
documentLoader,
expansionMap,
});
if (!verified) {
throw new Error("Invalid signature.");
}
// ensure proof was performed for a valid purpose
const { valid, error } = await purpose.validate(proof, {
document,
suite: this,
verificationMethod,
documentLoader,
expansionMap,
});
if (!valid) {
throw error;
}
return { verified: true };
} catch (error) {
return { verified: false, error };
}
}
async canonize(input: any, options: CanonizeOptions): Promise<string> {
const { documentLoader, expansionMap, skipExpansion } = options;
return jsonld.canonize(input, {
algorithm: "URDNA2015",
format: "application/n-quads",
documentLoader,
expansionMap,
skipExpansion,
useNative: this.useNativeCanonize,
});
}
async canonizeProof(proof: any, options: CanonizeOptions): Promise<string> {
const { documentLoader, expansionMap } = options;
proof = { ...proof };
delete proof[this.proofSignatureKey];
return this.canonize(proof, {
documentLoader,
expansionMap,
skipExpansion: false,
});
}
/**
* @param document {CreateVerifyDataOptions} options to create verify data
*
* @returns {Promise<{string[]>}.
*/
async createVerifyData(options: CreateVerifyDataOptions): Promise<string[]> {
const { proof, document, documentLoader, expansionMap } = options;
const proofStatements = await this.createVerifyProofData(proof, {
documentLoader,
expansionMap,
});
const documentStatements = await this.createVerifyDocumentData(document, {
documentLoader,
expansionMap,
});
// concatenate c14n proof options and c14n document
return proofStatements.concat(documentStatements);
}
/**
* @param proof to canonicalize
* @param options to create verify data
*
* @returns {Promise<{string[]>}.
*/
async createVerifyProofData(
proof: any,
{ documentLoader, expansionMap }: any
): Promise<string[]> {
const c14nProofOptions = await this.canonizeProof(proof, {
documentLoader,
expansionMap,
});
return c14nProofOptions.split("\n").filter((_) => _.length > 0);
}
/**
* @param document to canonicalize
* @param options to create verify data
*
* @returns {Promise<{string[]>}.
*/
async createVerifyDocumentData(
document: any,
{ documentLoader, expansionMap }: any
): Promise<string[]> {
const c14nDocument = await this.canonize(document, {
documentLoader,
expansionMap,
});
return c14nDocument.split("\n").filter((_) => _.length > 0);
}
/**
* @param document {object} to be signed.
* @param proof {object}
* @param documentLoader {function}
* @param expansionMap {function}
*/
async getVerificationMethod({ proof, documentLoader }: any): Promise<any> {
let { verificationMethod } = proof;
if (typeof verificationMethod === "object") {
verificationMethod = verificationMethod.id;
}
if (!verificationMethod) {
throw new Error('No "verificationMethod" found in proof.');
}
// Note: `expansionMap` is intentionally not passed; we can safely drop
// properties here and must allow for it
const result = await jsonld.frame(
verificationMethod,
{
"@context": [
"https://w3id.org/security/v2",
"https://w3id.org/security/suites/jws-2020/v1",
],
"@embed": "@always",
id: verificationMethod,
},
{
documentLoader,
compactToRelative: false,
expandContext: SECURITY_CONTEXT_URL,
}
);
if (!result) {
throw new Error(`Verification method ${verificationMethod} not found.`);
}
// ensure verification method has not been revoked
if (result.revoked !== undefined) {
throw new Error("The verification method has been revoked.");
}
return result;
}
/**
* @param options {SuiteSignOptions} Options for signing.
*
* @returns {Promise<{object}>} the proof containing the signature value.
*/
async sign(options: SuiteSignOptions): Promise<object> {
const { verifyData, proof } = options;
if (!(this.signer && typeof this.signer.sign === "function")) {
throw new Error(
"A signer API with sign function has not been specified."
);
}
const proofValue: Uint8Array = await this.signer.sign({
data: verifyData,
});
proof[this.proofSignatureKey] = Buffer.from(proofValue).toString("base64");
return proof;
}
/**
* @param verifyData {VerifySignatureOptions} Options to verify the signature.
*
* @returns {Promise<boolean>}
*/
async verifySignature(options: VerifySignatureOptions): Promise<boolean> {
const { verificationMethod, verifyData, proof } = options;
let { verifier } = this;
if (!verifier) {
const key = verificationMethod.publicKeyJwk
? await this.LDKeyClass.fromJwk(verificationMethod)
: await this.LDKeyClass.from(verificationMethod);
verifier = key.verifier(key, this.alg, this.type);
}
return await verifier.verify({
data: verifyData,
signature: new Uint8Array(
Buffer.from(proof[this.proofSignatureKey] as string, "base64")
),
});
}
static proofType = [
"BbsBlsSignature2020",
"sec:BbsBlsSignature2020",
"https://w3id.org/security#BbsBlsSignature2020",
];
}