@bsv/sdk 1.9.9 → 1.9.11

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.
@@ -218,6 +218,15 @@ export default class Certificate {
218
218
  protocolID: WalletProtocol;
219
219
  keyID: string;
220
220
  }
221
+ static fromObject(obj: {
222
+ type: Base64String;
223
+ serialNumber: Base64String;
224
+ subject: PubKeyHex;
225
+ certifier: PubKeyHex;
226
+ revocationOutpoint: OutpointString;
227
+ fields: Record<CertificateFieldNameUnder50Bytes, Base64String>;
228
+ signature?: HexString;
229
+ }): Certificate
221
230
  }
222
231
  ```
223
232
 
@@ -330,6 +339,32 @@ Argument Details
330
339
  + **bin**
331
340
  + The binary data representing the certificate.
332
341
 
342
+ #### Method fromObject
343
+
344
+ Creates a Certificate instance from a plain object representation.
345
+
346
+ ```ts
347
+ static fromObject(obj: {
348
+ type: Base64String;
349
+ serialNumber: Base64String;
350
+ subject: PubKeyHex;
351
+ certifier: PubKeyHex;
352
+ revocationOutpoint: OutpointString;
353
+ fields: Record<CertificateFieldNameUnder50Bytes, Base64String>;
354
+ signature?: HexString;
355
+ }): Certificate
356
+ ```
357
+ See also: [Base64String](./wallet.md#type-base64string), [Certificate](./auth.md#class-certificate), [CertificateFieldNameUnder50Bytes](./wallet.md#type-certificatefieldnameunder50bytes), [HexString](./wallet.md#type-hexstring), [OutpointString](./wallet.md#type-outpointstring), [PubKeyHex](./wallet.md#type-pubkeyhex)
358
+
359
+ Returns
360
+
361
+ A new Certificate instance.
362
+
363
+ Argument Details
364
+
365
+ + **obj**
366
+ + The object containing certificate data.
367
+
333
368
  #### Method getCertificateFieldEncryptionDetails
334
369
 
335
370
  Helper function which retrieves the protocol ID and key ID for certificate field encryption.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bsv/sdk",
3
- "version": "1.9.9",
3
+ "version": "1.9.11",
4
4
  "type": "module",
5
5
  "description": "BSV Blockchain Software Development Kit",
6
6
  "main": "dist/cjs/mod.js",
@@ -280,4 +280,32 @@ export default class Certificate {
280
280
  keyID: serialNumber ? `${serialNumber} ${fieldName}` : fieldName
281
281
  }
282
282
  }
283
+
284
+ /**
285
+ * Creates a Certificate instance from a plain object representation.
286
+ *
287
+ * @param obj - The object containing certificate data.
288
+ * @returns A new Certificate instance.
289
+ */
290
+ static fromObject(obj: {
291
+ type: Base64String,
292
+ serialNumber: Base64String,
293
+ subject: PubKeyHex,
294
+ certifier: PubKeyHex,
295
+ revocationOutpoint: OutpointString,
296
+ fields: Record<CertificateFieldNameUnder50Bytes, Base64String>,
297
+ signature?: HexString
298
+ }): Certificate {
299
+ const cert = new Certificate(
300
+ obj.type,
301
+ obj.serialNumber,
302
+ obj.subject,
303
+ obj.certifier,
304
+ obj.revocationOutpoint,
305
+ obj.fields,
306
+ obj.signature
307
+ );
308
+
309
+ return cert;
310
+ }
283
311
  }
@@ -332,4 +332,55 @@ describe('Certificate', () => {
332
332
  expect(certificateWithMismatch.certifier).toBe(certifierPubKey)
333
333
  expect(await certificateWithMismatch.verify()).toBe(true)
334
334
  })
335
+
336
+ it('should create a Certificate from an object using fromObject()', () => {
337
+ const certificateObject = {
338
+ type: 'Q29tbW9uU291cmNlIGlkZW50aXR5',
339
+ subject: '028e2e075e1e57ba4c62c90125468109f9b4e2a7741da3dd76ccd0c73b2a8a37ad',
340
+ serialNumber: 'UegX3uufsqHsbEKeBSxUd9AziLSyru86TnwfhPoZJYE=',
341
+ certifier: '03c644fe2fd97673a5d86555a58587e7936390be6582ece262bc387014bcff6fe4',
342
+ revocationOutpoint: '0245242bd144a85053b4c1e4a0ed5467c79a4d172680ca77a970ebabd682d564.0',
343
+ signature: '304402202c86ef816c469fe657289ddea12d2c444f006ef5ab5851f00107c7724eb67ea602202786244c077567c8f3ec5da78bd61ce0c35bf1eeac0488e026c03b21c403b0fd',
344
+ fields: {
345
+ displayName: 'eqsSpUgTKk891y1EkyCPPg+C4YoVZJvB0EQ4iore7VofkM5TB9Ctj7x2PgBaWI0A9tfATDO9',
346
+ email: 'n6HVUvyHkIDMvB4ERxVGxmX6lgRBM+e7kbbC5DiRCKe5a60BJeXr05g4POq6OHYO9Wl/b1Xxe+JKsejl',
347
+ phoneNumber: '5yWyN9kOGaZs5G6yvXUWnWj4rm7kDAug4YIsn4BQLKGYRzDx8s1dytb43ega6BnSp0gUTnskcjiM8ekqul2a',
348
+ lat: 'lc3u6SFKQ5Mpxp5vc+6s4aXe7lOyQQLfN+CbOu4XlBYsj7Jlc78gt4sGCwDSxbzvA41eElCjlc2Our5bpLcsg1I6',
349
+ lng: 'FmY3iM/2/LDfYNEeXpcj7Epn933tRHz50WoBkBrqYv6jmZ6dXE6RRYId9TcaxIvB0D7Y14aD5vjSV6Bx48hdic5g'
350
+ }
351
+ }
352
+
353
+ const certificate = Certificate.fromObject(certificateObject)
354
+
355
+ expect(certificate.type).toEqual(certificateObject.type)
356
+ expect(certificate.serialNumber).toEqual(certificateObject.serialNumber)
357
+ expect(certificate.subject).toEqual(certificateObject.subject)
358
+ expect(certificate.certifier).toEqual(certificateObject.certifier)
359
+ expect(certificate.revocationOutpoint).toEqual(certificateObject.revocationOutpoint)
360
+ expect(certificate.signature).toEqual(certificateObject.signature)
361
+ expect(certificate.fields).toEqual(certificateObject.fields)
362
+ })
363
+
364
+ it('should create a Certificate from an object without signature using fromObject()', () => {
365
+ const certificateObject = {
366
+ type: 'Q29tbW9uU291cmNlIGlkZW50aXR5',
367
+ subject: '028e2e075e1e57ba4c62c90125468109f9b4e2a7741da3dd76ccd0c73b2a8a37ad',
368
+ serialNumber: 'UegX3uufsqHsbEKeBSxUd9AziLSyru86TnwfhPoZJYE=',
369
+ certifier: '03c644fe2fd97673a5d86555a58587e7936390be6582ece262bc387014bcff6fe4',
370
+ revocationOutpoint: '0245242bd144a85053b4c1e4a0ed5467c79a4d172680ca77a970ebabd682d564.0',
371
+ fields: {
372
+ displayName: 'eqsSpUgTKk891y1EkyCPPg+C4YoVZJvB0EQ4iore7VofkM5TB9Ctj7x2PgBaWI0A9tfATDO9'
373
+ }
374
+ }
375
+
376
+ const certificate = Certificate.fromObject(certificateObject)
377
+
378
+ expect(certificate.type).toEqual(certificateObject.type)
379
+ expect(certificate.serialNumber).toEqual(certificateObject.serialNumber)
380
+ expect(certificate.subject).toEqual(certificateObject.subject)
381
+ expect(certificate.certifier).toEqual(certificateObject.certifier)
382
+ expect(certificate.revocationOutpoint).toEqual(certificateObject.revocationOutpoint)
383
+ expect(certificate.signature).toBeUndefined()
384
+ expect(certificate.fields).toEqual(certificateObject.fields)
385
+ })
335
386
  })
@@ -7,7 +7,6 @@
7
7
  * - Modern browsers via globalThis.crypto, self.crypto, or window.crypto
8
8
  * - Web Workers and Service Workers via self.crypto
9
9
  * - Deno and Bun via globalThis.crypto
10
- * - React Native (requires react-native-get-random-values polyfill)
11
10
  *
12
11
  * @throws {Error} If no secure random number generator is available
13
12
  */
@@ -72,34 +71,6 @@ class Rand {
72
71
  return
73
72
  }
74
73
 
75
- // React Native support - try to load polyfill
76
- if (typeof navigator !== 'undefined' && (navigator as any).product === 'ReactNative') {
77
- try {
78
- // Try to require the polyfill - this will populate globalThis.crypto
79
- // eslint-disable-next-line @typescript-eslint/no-var-requires
80
- require('react-native-get-random-values')
81
-
82
- if (typeof (globalThis as any).crypto?.getRandomValues === 'function') {
83
- this._rand = (n) => {
84
- /* eslint-disable-next-line */
85
- return this.getRandomValues(globalThis as any, n)
86
- }
87
- return
88
- }
89
- } catch (e) {
90
- // Polyfill not available - provide helpful error
91
- this._rand = (): never => {
92
- throw new Error(
93
- 'React Native detected but crypto is not available. ' +
94
- 'Please install and import "react-native-get-random-values" at the top of your entry file:\n' +
95
- 'npm install react-native-get-random-values\n' +
96
- 'Then add: import "react-native-get-random-values" to your index.js/App.js'
97
- )
98
- }
99
- return
100
- }
101
- }
102
-
103
74
  // No crypto available
104
75
  this._rand = noRand
105
76
  }