@nuggetslife/vc-rn 0.1.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.
Files changed (137) hide show
  1. package/LICENSE +20 -0
  2. package/README.md +31 -0
  3. package/android/build.gradle +101 -0
  4. package/android/gradle.properties +5 -0
  5. package/android/src/main/AndroidManifest.xml +3 -0
  6. package/android/src/main/AndroidManifestNew.xml +2 -0
  7. package/android/src/main/java/com/nuggetslife/bindings/vc/vc.kt +673 -0
  8. package/android/src/main/java/com/nuggetslife/vc/VcModule.kt +30 -0
  9. package/android/src/main/java/com/nuggetslife/vc/VcPackage.kt +17 -0
  10. package/android/src/main/jniLibs/arm64-v8a/libvc.so +0 -0
  11. package/android/src/main/jniLibs/armeabi-v7a/libvc.so +0 -0
  12. package/android/src/main/jniLibs/x86/libvc.so +0 -0
  13. package/android/src/main/jniLibs/x86_64/libvc.so +0 -0
  14. package/ios/Vc-Bridging-Header.h +2 -0
  15. package/ios/VcModule.mm +14 -0
  16. package/ios/VcModule.swift +23 -0
  17. package/ios/vc.swift +446 -0
  18. package/ios/vc.xcframework/Info.plist +48 -0
  19. package/ios/vc.xcframework/ios-arm64/Headers/module.modulemap +6 -0
  20. package/ios/vc.xcframework/ios-arm64/Headers/vcFFI.h +174 -0
  21. package/ios/vc.xcframework/ios-arm64/libvc.a +0 -0
  22. package/ios/vc.xcframework/ios-arm64_x86_64-simulator/Headers/module.modulemap +6 -0
  23. package/ios/vc.xcframework/ios-arm64_x86_64-simulator/Headers/vcFFI.h +174 -0
  24. package/ios/vc.xcframework/ios-arm64_x86_64-simulator/libvc.a +0 -0
  25. package/ios/vcFFI.h +174 -0
  26. package/lib/commonjs/Bls12381G2KeyPair.js +405 -0
  27. package/lib/commonjs/Bls12381G2KeyPair.js.map +1 -0
  28. package/lib/commonjs/index.d.js +18 -0
  29. package/lib/commonjs/index.d.js.map +1 -0
  30. package/lib/commonjs/index.js +20 -0
  31. package/lib/commonjs/index.js.map +1 -0
  32. package/lib/commonjs/types/BlsCurveName.js +14 -0
  33. package/lib/commonjs/types/BlsCurveName.js.map +1 -0
  34. package/lib/commonjs/types/GenerateKeyPairOptions.js +2 -0
  35. package/lib/commonjs/types/GenerateKeyPairOptions.js.map +1 -0
  36. package/lib/commonjs/types/JsonWebKey.js +2 -0
  37. package/lib/commonjs/types/JsonWebKey.js.map +1 -0
  38. package/lib/commonjs/types/JwkKeyPairOptions.js +6 -0
  39. package/lib/commonjs/types/JwkKeyPairOptions.js.map +1 -0
  40. package/lib/commonjs/types/JwkKty.js +13 -0
  41. package/lib/commonjs/types/JwkKty.js.map +1 -0
  42. package/lib/commonjs/types/KeyPairOptions.js +2 -0
  43. package/lib/commonjs/types/KeyPairOptions.js.map +1 -0
  44. package/lib/commonjs/types/KeyPairSigner.js +2 -0
  45. package/lib/commonjs/types/KeyPairSigner.js.map +1 -0
  46. package/lib/commonjs/types/KeyPairVerifier.js +2 -0
  47. package/lib/commonjs/types/KeyPairVerifier.js.map +1 -0
  48. package/lib/commonjs/types/index.js +20 -0
  49. package/lib/commonjs/types/index.js.map +1 -0
  50. package/lib/commonjs/utils/codec.js +22 -0
  51. package/lib/commonjs/utils/codec.js.map +1 -0
  52. package/lib/commonjs/utils/index.js +19 -0
  53. package/lib/commonjs/utils/index.js.map +1 -0
  54. package/lib/commonjs/validators/Bls12381G2Jwk.js +19 -0
  55. package/lib/commonjs/validators/Bls12381G2Jwk.js.map +1 -0
  56. package/lib/commonjs/validators/JsonWebKey.js +27 -0
  57. package/lib/commonjs/validators/JsonWebKey.js.map +1 -0
  58. package/lib/module/Bls12381G2KeyPair.js +398 -0
  59. package/lib/module/Bls12381G2KeyPair.js.map +1 -0
  60. package/lib/module/index.d.js +9 -0
  61. package/lib/module/index.d.js.map +1 -0
  62. package/lib/module/index.js +14 -0
  63. package/lib/module/index.js.map +1 -0
  64. package/lib/module/types/BlsCurveName.js +8 -0
  65. package/lib/module/types/BlsCurveName.js.map +1 -0
  66. package/lib/module/types/GenerateKeyPairOptions.js +2 -0
  67. package/lib/module/types/GenerateKeyPairOptions.js.map +1 -0
  68. package/lib/module/types/JsonWebKey.js +2 -0
  69. package/lib/module/types/JsonWebKey.js.map +1 -0
  70. package/lib/module/types/JwkKeyPairOptions.js +2 -0
  71. package/lib/module/types/JwkKeyPairOptions.js.map +1 -0
  72. package/lib/module/types/JwkKty.js +7 -0
  73. package/lib/module/types/JwkKty.js.map +1 -0
  74. package/lib/module/types/KeyPairOptions.js +2 -0
  75. package/lib/module/types/KeyPairOptions.js.map +1 -0
  76. package/lib/module/types/KeyPairSigner.js +2 -0
  77. package/lib/module/types/KeyPairSigner.js.map +1 -0
  78. package/lib/module/types/KeyPairVerifier.js +2 -0
  79. package/lib/module/types/KeyPairVerifier.js.map +1 -0
  80. package/lib/module/types/index.js +3 -0
  81. package/lib/module/types/index.js.map +1 -0
  82. package/lib/module/utils/codec.js +13 -0
  83. package/lib/module/utils/codec.js.map +1 -0
  84. package/lib/module/utils/index.js +2 -0
  85. package/lib/module/utils/index.js.map +1 -0
  86. package/lib/module/validators/Bls12381G2Jwk.js +11 -0
  87. package/lib/module/validators/Bls12381G2Jwk.js.map +1 -0
  88. package/lib/module/validators/JsonWebKey.js +19 -0
  89. package/lib/module/validators/JsonWebKey.js.map +1 -0
  90. package/lib/typescript/src/Bls12381G2KeyPair.d.ts +104 -0
  91. package/lib/typescript/src/Bls12381G2KeyPair.d.ts.map +1 -0
  92. package/lib/typescript/src/index.d.ts +2 -0
  93. package/lib/typescript/src/index.d.ts.map +1 -0
  94. package/lib/typescript/src/types/BlsCurveName.d.ts +7 -0
  95. package/lib/typescript/src/types/BlsCurveName.d.ts.map +1 -0
  96. package/lib/typescript/src/types/GenerateKeyPairOptions.d.ts +18 -0
  97. package/lib/typescript/src/types/GenerateKeyPairOptions.d.ts.map +1 -0
  98. package/lib/typescript/src/types/JsonWebKey.d.ts +60 -0
  99. package/lib/typescript/src/types/JsonWebKey.d.ts.map +1 -0
  100. package/lib/typescript/src/types/JwkKeyPairOptions.d.ts +23 -0
  101. package/lib/typescript/src/types/JwkKeyPairOptions.d.ts.map +1 -0
  102. package/lib/typescript/src/types/JwkKty.d.ts +6 -0
  103. package/lib/typescript/src/types/JwkKty.d.ts.map +1 -0
  104. package/lib/typescript/src/types/KeyPairOptions.d.ts +22 -0
  105. package/lib/typescript/src/types/KeyPairOptions.d.ts.map +1 -0
  106. package/lib/typescript/src/types/KeyPairSigner.d.ts +7 -0
  107. package/lib/typescript/src/types/KeyPairSigner.d.ts.map +1 -0
  108. package/lib/typescript/src/types/KeyPairVerifier.d.ts +8 -0
  109. package/lib/typescript/src/types/KeyPairVerifier.d.ts.map +1 -0
  110. package/lib/typescript/src/types/index.d.ts +9 -0
  111. package/lib/typescript/src/types/index.d.ts.map +1 -0
  112. package/lib/typescript/src/utils/codec.d.ts +3 -0
  113. package/lib/typescript/src/utils/codec.d.ts.map +1 -0
  114. package/lib/typescript/src/utils/index.d.ts +2 -0
  115. package/lib/typescript/src/utils/index.d.ts.map +1 -0
  116. package/lib/typescript/src/validators/Bls12381G2Jwk.d.ts +4 -0
  117. package/lib/typescript/src/validators/Bls12381G2Jwk.d.ts.map +1 -0
  118. package/lib/typescript/src/validators/JsonWebKey.d.ts +4 -0
  119. package/lib/typescript/src/validators/JsonWebKey.d.ts.map +1 -0
  120. package/nuggetslife-vc.podspec +42 -0
  121. package/package.json +164 -0
  122. package/src/Bls12381G2KeyPair.ts +447 -0
  123. package/src/index.d.ts +126 -0
  124. package/src/index.tsx +22 -0
  125. package/src/types/BlsCurveName.ts +6 -0
  126. package/src/types/GenerateKeyPairOptions.ts +17 -0
  127. package/src/types/JsonWebKey.ts +68 -0
  128. package/src/types/JwkKeyPairOptions.ts +24 -0
  129. package/src/types/JwkKty.ts +5 -0
  130. package/src/types/KeyPairOptions.ts +21 -0
  131. package/src/types/KeyPairSigner.ts +6 -0
  132. package/src/types/KeyPairVerifier.ts +8 -0
  133. package/src/types/index.ts +8 -0
  134. package/src/utils/codec.ts +12 -0
  135. package/src/utils/index.ts +1 -0
  136. package/src/validators/Bls12381G2Jwk.ts +21 -0
  137. package/src/validators/JsonWebKey.ts +28 -0
@@ -0,0 +1,447 @@
1
+ import { vc_facade } from '.';
2
+ import bs58 from 'bs58';
3
+ import {
4
+ assertBls12381G2PrivateJwk,
5
+ assertBls12381G2PublicJwk,
6
+ } from './validators/Bls12381G2Jwk';
7
+ import { convertBase64urlToBase58, uint8ArrayToHexString } from './utils';
8
+ import {
9
+ BlsCurveName,
10
+ type GenerateKeyPairOptions,
11
+ type JsonWebKey,
12
+ type JwkKeyPairOptions,
13
+ type KeyPairOptions,
14
+ type KeyPairSigner,
15
+ type KeyPairVerifier,
16
+ } from './types';
17
+ import { base64url } from 'rfc4648';
18
+
19
+ /**
20
+ * z represents the multibase encoding scheme of base58 encoding
21
+ * @see https://github.com/multiformats/multibase/blob/master/multibase.csv#L18
22
+ * @ignore
23
+ */
24
+ const MULTIBASE_ENCODED_BASE58_IDENTIFIER = 'z';
25
+
26
+ /**
27
+ * 0x01 indicates the end of the leading bytes according to variable integer spec
28
+ * @see https://github.com/multiformats/multicodec
29
+ * @ignore
30
+ */
31
+ const VARIABLE_INTEGER_TRAILING_BYTE = 0x01;
32
+
33
+ /**
34
+ * 0xeb indicates a BLS 12-381 G2 public key
35
+ *
36
+ */
37
+ const BLS12381G2_MULTICODEC_IDENTIFIER = 0xeb;
38
+
39
+ const DEFAULT_BLS12381_G2_PUBLIC_KEY_LENGTH = 96;
40
+
41
+ /**
42
+ * Returns an object with an async sign function for producing BBS+ signatures.
43
+ * The sign function is bound to the KeyPair
44
+ * and then returned by the KeyPair's signer method.
45
+ * @param key - A Bls12381G2KeyPair.
46
+ *
47
+ * @returns An object with an async function sign
48
+ * using the private key passed in.
49
+ */
50
+
51
+ const signerFactory = (key: Bls12381G2KeyPair): KeyPairSigner => {
52
+ if (!key.privateKeyInner) {
53
+ return {
54
+ async sign(): Promise<Uint8Array> {
55
+ throw new Error('No private key to sign with.');
56
+ },
57
+ };
58
+ }
59
+ return {
60
+ async sign({ data }): Promise<Uint8Array> {
61
+ const messages = data.map((m) => bs58.encode(m));
62
+ let sig_bs58 = await vc_facade(
63
+ 'bls_sign',
64
+ JSON.stringify({
65
+ messages,
66
+ keyPair: {
67
+ secretKey: key.privateKey,
68
+ publicKey: key.publicKey,
69
+ },
70
+ })
71
+ );
72
+ return bs58.decode(sig_bs58);
73
+ },
74
+ };
75
+ };
76
+
77
+ /**
78
+ * @ignore
79
+ * Returns an object with an async verify function for verifying BBS+ signatures.
80
+ * The verify function is bound to the KeyPair
81
+ * and then returned by the KeyPair's verifier method.
82
+ * @param key - A Bls12381G2KeyPair.
83
+ *
84
+ * @returns An async verifier specific
85
+ * to the key passed in.
86
+ */
87
+ const verifierFactory = (key: Bls12381G2KeyPair): KeyPairVerifier => {
88
+ if (!key.publicKeyInner) {
89
+ return {
90
+ async verify(): Promise<boolean> {
91
+ throw new Error('No public key to verify with.');
92
+ },
93
+ };
94
+ }
95
+
96
+ return {
97
+ async verify({ data, signature }): Promise<boolean> {
98
+ const messages = data.map((m) => bs58.encode(m));
99
+ const sig_bs58 = bs58.encode(signature);
100
+ let res = await vc_facade(
101
+ 'bls_verify',
102
+ JSON.stringify({
103
+ messages,
104
+ publicKey: key.publicKey,
105
+ signature: sig_bs58,
106
+ })
107
+ );
108
+ return JSON.parse(res);
109
+ },
110
+ };
111
+ };
112
+
113
+ export class Bls12381G2KeyPair {
114
+ id?: string;
115
+ controller?: string;
116
+ privateKeyInner?: Array<number>;
117
+ publicKeyInner?: Array<number>;
118
+ type = 'Bls12381G2Key2020';
119
+
120
+ constructor(options?: KeyPairOptions | undefined | null) {
121
+ /**
122
+ * The provided publicKey needs to be 384 bits / 5.85 = 65.6
123
+ * which means the base58 encoded publicKey can be either 65 or 66 chars
124
+ * 5.85 = log base 2 (58) which is equivalent to the number of bits
125
+ * encoded per character of a base58 encoded string.
126
+ *
127
+ */
128
+ if (
129
+ options?.publicKeyBase58?.length !== 131 &&
130
+ options?.publicKeyBase58?.length !== 132
131
+ ) {
132
+ throw new Error(
133
+ `The size of the public key is incorrect. Expected 131 or 132 chars got: ${options?.publicKeyBase58?.length}`
134
+ );
135
+ }
136
+
137
+ /**
138
+ * Validates the size of the private key if one is included
139
+ * This is done by 256 bits / 5.85 = 43.7 which means
140
+ * the base58 encoded privateKey can be either 43 or 44 chars
141
+ */
142
+ if (
143
+ typeof options.privateKeyBase58 !== 'undefined' &&
144
+ options.privateKeyBase58.length !== 43 &&
145
+ options.privateKeyBase58.length !== 44
146
+ ) {
147
+ throw new Error(
148
+ `The size of the private key is incorrect. Expected 65 or 66 chars got: ${options.privateKeyBase58.length}`
149
+ );
150
+ }
151
+ this.id = options.id;
152
+ this.controller = options.controller;
153
+ this.privateKeyInner = options.privateKeyBase58
154
+ ? Array.from(bs58.decode(options.privateKeyBase58))
155
+ : undefined;
156
+ this.publicKeyInner = Array.from(bs58.decode(options.publicKeyBase58));
157
+ //TODO assert if key pair is the wrong length?
158
+ }
159
+ /**
160
+ * Generates a BLS 12-381 key pair
161
+ * @param options [Optional] options for the key pair generation
162
+ *
163
+ * @returns A BLS 12-381 key pair
164
+ */
165
+ static async generate(
166
+ options?: GenerateKeyPairOptions
167
+ ): Promise<Bls12381G2KeyPair> {
168
+ const res = options?.seed
169
+ ? await vc_facade(
170
+ 'bls_generate_g2_key',
171
+ JSON.stringify({
172
+ seed: bs58.encode(options.seed),
173
+ })
174
+ )
175
+ : await vc_facade('bls_generate_g2_key', JSON.stringify({}));
176
+
177
+ const keyPair: { publicKey: string; secretKey: string } = JSON.parse(res);
178
+ return new Bls12381G2KeyPair({
179
+ ...options,
180
+ privateKeyBase58: keyPair.secretKey,
181
+ publicKeyBase58: keyPair.publicKey,
182
+ });
183
+ }
184
+ /**
185
+ * Constructs a BLS 12-381 key pair from options
186
+ * @param options [Optional] options for key pair
187
+ *
188
+ * @returns A BLS 12-381 key pair
189
+ */
190
+ static async from(options: KeyPairOptions): Promise<Bls12381G2KeyPair> {
191
+ return new Bls12381G2KeyPair(options);
192
+ }
193
+ /**
194
+ * Constructs a BLS 12-381 key pair from options
195
+ * @param options [Optional] options for key pair
196
+ *
197
+ * @returns A BLS 12-381 G2 key pair
198
+ */
199
+ static async fromJwk(options: JwkKeyPairOptions): Promise<Bls12381G2KeyPair> {
200
+ const { id, controller, publicKeyJwk, privateKeyJwk } = options;
201
+ if (
202
+ typeof privateKeyJwk !== 'undefined' &&
203
+ /**
204
+ * The type casting is verified through the use of this assert function
205
+ * However because the returned interface leaves the properties as optional
206
+ * they need to be cast to pass to the convert function.
207
+ **/
208
+ assertBls12381G2PrivateJwk(privateKeyJwk)
209
+ ) {
210
+ return new Bls12381G2KeyPair({
211
+ id,
212
+ controller,
213
+ publicKeyBase58: convertBase64urlToBase58(privateKeyJwk.x as string),
214
+ privateKeyBase58: convertBase64urlToBase58(privateKeyJwk.d as string),
215
+ });
216
+ }
217
+
218
+ if (assertBls12381G2PublicJwk(publicKeyJwk)) {
219
+ return new Bls12381G2KeyPair({
220
+ id,
221
+ controller,
222
+ publicKeyBase58: convertBase64urlToBase58(publicKeyJwk.x as string),
223
+ });
224
+ }
225
+
226
+ throw Error('The JWK provided is not a valid');
227
+ }
228
+ /**
229
+ * Constructs a BLS 12-381 key pair from a public key fingerprint
230
+ * @param fingerprint [Optional] public key fingerprint
231
+ *
232
+ * TODO this interface needs to be refactored, there should be no
233
+ * hard coded notion of DIDs at this layer
234
+ *
235
+ * @returns A BLS 12-381 key pair
236
+ */
237
+ static fromFingerprint({
238
+ id,
239
+ controller,
240
+ fingerprint,
241
+ }: any): Bls12381G2KeyPair {
242
+ if (fingerprint.substr(0, 1) != MULTIBASE_ENCODED_BASE58_IDENTIFIER) {
243
+ throw new Error(
244
+ `Unsupported fingerprint type: expected first character to be \`z\` indicating base58 encoding, received \`${fingerprint.substr(
245
+ 0,
246
+ 1
247
+ )}\``
248
+ );
249
+ }
250
+
251
+ // parse of the multi-format public key removing the `z` that indicates base58 encoding
252
+ const buffer = bs58.decode(fingerprint.substr(1));
253
+
254
+ if (buffer.length !== DEFAULT_BLS12381_G2_PUBLIC_KEY_LENGTH + 2) {
255
+ throw new Error(
256
+ `Unsupported public key length: expected \`${DEFAULT_BLS12381_G2_PUBLIC_KEY_LENGTH}\` received \`${
257
+ buffer.length - 2
258
+ }\``
259
+ );
260
+ }
261
+
262
+ if (buffer[0] !== BLS12381G2_MULTICODEC_IDENTIFIER) {
263
+ throw new Error(
264
+ `Unsupported public key identifier: expected second character to be \`${BLS12381G2_MULTICODEC_IDENTIFIER}\` indicating BLS12381G2 key pair, received \`${buffer[0]}\``
265
+ );
266
+ }
267
+
268
+ if (buffer[1] !== VARIABLE_INTEGER_TRAILING_BYTE) {
269
+ throw new Error(
270
+ `Missing variable integer trailing byte: expected third character to be \`${VARIABLE_INTEGER_TRAILING_BYTE}\` indicating trailing integer, received \`${buffer[1]}\``
271
+ );
272
+ }
273
+
274
+ const publicKeyBase58 = bs58.encode(buffer.slice(2));
275
+
276
+ //Defaults the controller to a DID key based controller
277
+ if (!controller) {
278
+ controller = `did:key:${Bls12381G2KeyPair.fingerprintFromPublicKey({
279
+ publicKeyBase58,
280
+ })}`;
281
+ }
282
+
283
+ //Defaults the id to the did key based fragment
284
+ if (!id) {
285
+ id = `#${Bls12381G2KeyPair.fingerprintFromPublicKey({
286
+ publicKeyBase58,
287
+ })}`;
288
+ }
289
+
290
+ return new Bls12381G2KeyPair({
291
+ id,
292
+ controller,
293
+ publicKeyBase58,
294
+ });
295
+ }
296
+
297
+ /**
298
+ * Returns the base58 encoded public key.
299
+ *
300
+ * @returns The base58 encoded public key.
301
+ */
302
+ get publicKey(): string {
303
+ return bs58.encode(this.publicKeyBuffer);
304
+ }
305
+
306
+ /**
307
+ * Returns the JWK structured public key.
308
+ *
309
+ * @returns The JWK public key.
310
+ */
311
+ get publicKeyJwk(): JsonWebKey {
312
+ return {
313
+ kid: this.id,
314
+ kty: 'EC',
315
+ crv: BlsCurveName.G2,
316
+ x: base64url.stringify(this.publicKeyBuffer, { pad: false }),
317
+ };
318
+ }
319
+
320
+ /**
321
+ * Returns the base58 encoded private key.
322
+ *
323
+ * @returns The base58 encoded private key.
324
+ */
325
+ get privateKey(): string | undefined {
326
+ if (this.privateKeyInner) {
327
+ return bs58.encode(this.privateKeyBuffer);
328
+ }
329
+ return undefined;
330
+ }
331
+
332
+ /**
333
+ * Returns the JWK formatted private key.
334
+ *
335
+ * @returns The JWK formatted private key.
336
+ */
337
+ get privateKeyJwk(): JsonWebKey | undefined {
338
+ if (this.privateKeyInner) {
339
+ return {
340
+ kid: this.id,
341
+ kty: 'EC',
342
+ crv: BlsCurveName.G2,
343
+ x: base64url.stringify(this.publicKeyBuffer, { pad: false }),
344
+ d: base64url.stringify(this.privateKeyBuffer, { pad: false }),
345
+ };
346
+ }
347
+ return undefined;
348
+ }
349
+
350
+ /**
351
+ * Adds a public key base to a public key node.
352
+ *
353
+ * @param publicKeyNode - The public key node.
354
+ * @param publicKeyNode.publicKeyBase58 - Base58 public key.
355
+ *
356
+ * @returns A PublicKeyNode in a block.
357
+ */
358
+ addEncodedPublicKey(publicKeyNode: any): any {
359
+ publicKeyNode.publicKeyBase58 = this.publicKey;
360
+ return publicKeyNode;
361
+ }
362
+ get publicKeyBuffer(): Buffer {
363
+ return Buffer.from(this.publicKeyInner ?? []);
364
+ }
365
+ get privateKeyBuffer(): Buffer {
366
+ return Buffer.from(this.privateKeyInner ?? []);
367
+ }
368
+
369
+ /**
370
+ * Returns a signer object for use with jsonld-signatures.
371
+ *
372
+ * @returns {{sign: Function}} A signer for the json-ld block.
373
+ */
374
+ signer(): KeyPairSigner {
375
+ return signerFactory(this);
376
+ }
377
+
378
+ /**
379
+ * Returns a verifier object for use with jsonld-signatures.
380
+ *
381
+ * @returns {{verify: Function}} Used to verify jsonld-signatures.
382
+ */
383
+ verifier(): KeyPairVerifier {
384
+ return verifierFactory(this);
385
+ }
386
+
387
+ /**
388
+ * Generates and returns a public key fingerprint.
389
+ *
390
+ * @returns The fingerprint.
391
+ */
392
+ fingerprint(): string {
393
+ const publicKeyBase58 = this.publicKey;
394
+ return Bls12381G2KeyPair.fingerprintFromPublicKey({ publicKeyBase58 });
395
+ }
396
+ static fingerprintFromPublicKey({ publicKeyBase58 }: any): string {
397
+ const keyBytes = bs58.decode(publicKeyBase58);
398
+ const buffer = new Uint8Array(2 + keyBytes.length);
399
+
400
+ buffer[0] = BLS12381G2_MULTICODEC_IDENTIFIER;
401
+ buffer[1] = VARIABLE_INTEGER_TRAILING_BYTE;
402
+ buffer.set(keyBytes, 2);
403
+
404
+ return `${MULTIBASE_ENCODED_BASE58_IDENTIFIER}${bs58.encode(buffer)}`;
405
+ }
406
+ /**
407
+ * Verifies whether the fingerprint was generated from a given key pair.
408
+ *
409
+ * @param fingerprint - A Base58 public key.
410
+ *
411
+ * @returns An object indicating valid is true or false.
412
+ */
413
+ verifyFingerprint(fingerprint: string): any {
414
+ // fingerprint should have `z` prefix indicating
415
+ // that it's multi-base encoded
416
+ if (
417
+ !(
418
+ typeof fingerprint === 'string' &&
419
+ fingerprint[0] === MULTIBASE_ENCODED_BASE58_IDENTIFIER
420
+ )
421
+ ) {
422
+ return {
423
+ error: new Error('`fingerprint` must be a multibase encoded string.'),
424
+ valid: false,
425
+ };
426
+ }
427
+ let fingerprintBuffer;
428
+ try {
429
+ fingerprintBuffer = bs58.decode(fingerprint.slice(1));
430
+ } catch (e) {
431
+ return { error: e, valid: false };
432
+ }
433
+ const publicKeyBuffer = this.publicKeyBuffer;
434
+
435
+ // validate the first two multicodec bytes 0xeb01
436
+ const valid =
437
+ uint8ArrayToHexString(fingerprintBuffer.slice(0, 2)) === 'eb01' &&
438
+ publicKeyBuffer.equals(fingerprintBuffer.slice(2));
439
+ if (!valid) {
440
+ return {
441
+ error: new Error('The fingerprint does not match the public key.'),
442
+ valid: false,
443
+ };
444
+ }
445
+ return { valid };
446
+ }
447
+ }
package/src/index.d.ts ADDED
@@ -0,0 +1,126 @@
1
+ /* tslint:disable */
2
+ /* eslint-disable */
3
+
4
+ /* auto-generated by NAPI-RS */
5
+
6
+ export interface KeyPairOptions {
7
+ id?: string
8
+ controller?: string
9
+ privateKeyBase58?: string
10
+ publicKeyBase58?: string
11
+ }
12
+ export interface JwkKeyPairOptions {
13
+ id?: string
14
+ controller?: string
15
+ privateKeyJwk?: JsonWebKey
16
+ publicKeyJwk?: JsonWebKey
17
+ }
18
+ export interface FingerPrintFromPublicKeyOptions {
19
+ publicKeyBase58: string
20
+ }
21
+ export interface KeyPairFromFingerPrintOptions {
22
+ id?: string
23
+ controller?: string
24
+ fingerprint: string
25
+ }
26
+ export interface JsonWebKey {
27
+ /**
28
+ * Indicates the key type used
29
+ * For BLS12381_G1 and BLS12381_G2 the string "EC" MUST be used
30
+ * @see https://tools.ietf.org/html/rfc7517#section-4.1
31
+ *
32
+ */
33
+ kty: string
34
+ /**
35
+ * Indicates the curve this key is associated with
36
+ * In the case of BLS12-381, the curve will also indicate if it's a G1 or G2 point
37
+ * For a G1 point, use the string "BLS12381_G1"
38
+ * For a G2 point, use the string "BLS12381_G2"
39
+ */
40
+ crv: string
41
+ /**
42
+ * This is a compression of the public key point
43
+ * For a G1 public key, X is a 384bit base64url encoding of the octet string representation of the coordinate
44
+ * For a G2 public key, X is a 768bit made up of the concatenation of two 384 bit x coordinates known as
45
+ * x_a and x_b in the following form (x_a || x_b) as a base64url encoding of the octet string representation of the two coordinates
46
+ *
47
+ */
48
+ x: string
49
+ /** @see https://tools.ietf.org/html/rfc7517#section-4.2 */
50
+ use?: string
51
+ /**
52
+ * @see https://tools.ietf.org/html/rfc7517#section-4.3
53
+ *
54
+ */
55
+ keyOps?: Array<string>
56
+ /**
57
+ * @see https://tools.ietf.org/html/rfc7517#section-4.4
58
+ *
59
+ */
60
+ alg?: string
61
+ /**
62
+ * @see https://tools.ietf.org/html/rfc7517#section-4.5
63
+ * TODO: Add note about referencing did-jose-extensions when ready
64
+ *
65
+ */
66
+ kid?: string
67
+ /**
68
+ * IMPORTANT NOTE: d represents the private key value and should not be shared
69
+ * IT IS HIGHLY SENSITIVE DATA AND IF NOT SECURED PROPERLY CONSIDER THE KEY COMPROMISED
70
+ * @see https://tools.ietf.org/html/rfc7517#section-9.2
71
+ *
72
+ */
73
+ d?: string
74
+ /**
75
+ * This coordinate is not used for BLS Keys, but is kept here to make the interface more standard
76
+ *
77
+ */
78
+ y?: string
79
+ /**
80
+ * @see https://www.w3.org/TR/WebCryptoAPI/#cryptokey-interface-members
81
+ *
82
+ */
83
+ ext?: boolean
84
+ }
85
+ export interface GenerateKeyPairOptions {
86
+ id?: string
87
+ controller?: string
88
+ seed?: Buffer
89
+ }
90
+ export interface KeyPairSignerOptions {
91
+ data: Array<Uint8Array>
92
+ }
93
+ export interface KeyPairVerifierOptions {
94
+ data: Array<Uint8Array>
95
+ signature: Uint8Array
96
+ }
97
+ export class Bls12381G2KeyPair {
98
+ id?: string
99
+ controller?: string
100
+ privateKeyInner?: Array<number>
101
+ publicKeyInner?: Array<number>
102
+ type: string
103
+ constructor(options?: KeyPairOptions | undefined | null)
104
+ static generate(options?: GenerateKeyPairOptions | undefined | null): Promise<Bls12381G2KeyPair>
105
+ static from(options: KeyPairOptions): Promise<Bls12381G2KeyPair>
106
+ static fromJwk(options: JwkKeyPairOptions): Promise<Bls12381G2KeyPair>
107
+ static fromFingerprint(options: KeyPairFromFingerPrintOptions): Promise<Bls12381G2KeyPair>
108
+ get publicKey(): string | null
109
+ get publicKeyBuffer(): Buffer
110
+ get privateKeyBuffer(): Buffer
111
+ publicKeyJwk(): JsonWebKey
112
+ get privateKey(): string | null
113
+ privateKeyJwk(): JsonWebKey
114
+ signer(): KeyPairSigner
115
+ verifier(): KeyPairVerifier
116
+ fingerprint(): string
117
+ static fingerprintFromPublicKey(options: FingerPrintFromPublicKeyOptions): string
118
+ verifyFingerprint(fingerprint: string): void
119
+ }
120
+ export class KeyPairSigner {
121
+ sign(options: KeyPairSignerOptions): Promise<Uint8Array>
122
+ }
123
+ export class KeyPairVerifier {
124
+ verify(options: KeyPairVerifierOptions): Promise<boolean>
125
+ }
126
+
package/src/index.tsx ADDED
@@ -0,0 +1,22 @@
1
+ import { NativeModules, Platform } from 'react-native';
2
+
3
+ const LINKING_ERROR =
4
+ `The package '@nuggetslife/vc' doesn't seem to be linked. Make sure: \n\n` +
5
+ Platform.select({ ios: "- You have run 'pod install'\n", default: '' }) +
6
+ '- You rebuilt the app after installing the package\n' +
7
+ '- You are not using Expo Go\n';
8
+
9
+ const Vc = NativeModules.Vc
10
+ ? NativeModules.Vc
11
+ : new Proxy(
12
+ {},
13
+ {
14
+ get() {
15
+ throw new Error(LINKING_ERROR);
16
+ },
17
+ }
18
+ );
19
+
20
+ export function vc_facade(action: string, options: string): Promise<string> {
21
+ return Vc.vc_facade(action, options);
22
+ }
@@ -0,0 +1,6 @@
1
+ export enum BlsCurveName {
2
+ DEPRECATED_G1 = "BLS12381_G1",
3
+ DEPRECATED_G2 = "BLS12381_G2",
4
+ G1 = "Bls12381G1",
5
+ G2 = "Bls12381G2"
6
+ }
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Options for generating a new key pair
3
+ */
4
+ export interface GenerateKeyPairOptions {
5
+ /**
6
+ * The key id
7
+ */
8
+ readonly id?: string;
9
+ /**
10
+ * The key controller
11
+ */
12
+ readonly controller?: string;
13
+ /**
14
+ * An optional seed to derive the key pair from
15
+ */
16
+ readonly seed?: Uint8Array;
17
+ }
@@ -0,0 +1,68 @@
1
+ export interface JsonWebKey {
2
+ /**
3
+ * Indicates the key type used
4
+ * For BLS12381_G1 and BLS12381_G2 the string "EC" MUST be used
5
+ *
6
+ * @see https://tools.ietf.org/html/rfc7517#section-4.1
7
+ */
8
+ kty: string;
9
+
10
+ /**
11
+ * Indicates the curve this key is associated with
12
+ * In the case of BLS12-381, the curve will also indicate if it's a G1 or G2 point
13
+ *
14
+ * For a G1 point, use the string "BLS12381_G1"
15
+ * For a G2 point, use the string "BLS12381_G2"
16
+ */
17
+ crv: string;
18
+
19
+ /**
20
+ * This is a compression of the public key point
21
+ *
22
+ * For a G1 public key, X is a 384bit base64url encoding of the octet string representation of the coordinate
23
+ * For a G2 public key, X is a 768bit made up of the concatenation of two 384 bit x coordinates known as
24
+ * x_a and x_b in the following form (x_a || x_b) as a base64url encoding of the octet string representation of the two coordinates
25
+ */
26
+ x: string;
27
+
28
+ /**
29
+ * @see https://tools.ietf.org/html/rfc7517#section-4.2
30
+ */
31
+ use?: string;
32
+
33
+ /**
34
+ * @see https://tools.ietf.org/html/rfc7517#section-4.3
35
+ */
36
+ key_ops?: string[];
37
+
38
+ /**
39
+ * @see https://tools.ietf.org/html/rfc7517#section-4.4
40
+ */
41
+ alg?: string;
42
+
43
+ /**
44
+ * @see https://tools.ietf.org/html/rfc7517#section-4.5
45
+ *
46
+ * TODO: Add note about referencing did-jose-extensions when ready
47
+ */
48
+ kid?: string;
49
+
50
+ /**
51
+ *
52
+ * IMPORTANT NOTE: d represents the private key value and should not be shared
53
+ * IT IS HIGHLY SENSITIVE DATA AND IF NOT SECURED PROPERLY CONSIDER THE KEY COMPROMISED
54
+ *
55
+ * @see https://tools.ietf.org/html/rfc7517#section-9.2
56
+ */
57
+ d?: string;
58
+
59
+ /**
60
+ * This coordinate is not used for BLS Keys, but is kept here to make the interface more standard
61
+ */
62
+ y?: string;
63
+
64
+ /**
65
+ * @see https://www.w3.org/TR/WebCryptoAPI/#cryptokey-interface-members
66
+ */
67
+ ext?: boolean;
68
+ }
@@ -0,0 +1,24 @@
1
+ import type { JsonWebKey } from "./JsonWebKey"
2
+
3
+ /**
4
+ * Options for constructing a key pair
5
+ */
6
+ export interface JwkKeyPairOptions {
7
+ /**
8
+ * The key id
9
+ */
10
+ readonly id?: string;
11
+ /**
12
+ * The key controller
13
+ */
14
+ readonly controller?: string;
15
+ /**
16
+ * Base58 encoding of the private key
17
+ */
18
+ readonly privateKeyJwk?: JsonWebKey;
19
+ /**
20
+ * Base58 encoding of the public key
21
+ */
22
+ readonly publicKeyJwk: JsonWebKey;
23
+ }
24
+