@bsv/sdk 1.7.0 → 1.7.1

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.
@@ -130,8 +130,8 @@ IdentityClient lets you discover who others are, and let the world know who you
130
130
  export class IdentityClient {
131
131
  constructor(wallet?: WalletInterface, private readonly options = DEFAULT_IDENTITY_CLIENT_OPTIONS, private readonly originator?: OriginatorDomainNameStringUnder250Bytes)
132
132
  async publiclyRevealAttributes(certificate: WalletCertificate, fieldsToReveal: CertificateFieldNameUnder50Bytes[]): Promise<BroadcastResponse | BroadcastFailure>
133
- async resolveByIdentityKey(args: DiscoverByIdentityKeyArgs): Promise<DisplayableIdentity[]>
134
- async resolveByAttributes(args: DiscoverByAttributesArgs): Promise<DisplayableIdentity[]>
133
+ async resolveByIdentityKey(args: DiscoverByIdentityKeyArgs, overrideWithContacts = true): Promise<DisplayableIdentity[]>
134
+ async resolveByAttributes(args: DiscoverByAttributesArgs, overrideWithContacts = true): Promise<DisplayableIdentity[]>
135
135
  public async getContacts(identityKey?: PubKeyHex, forceRefresh = false): Promise<Contact[]>
136
136
  public async saveContact(contact: DisplayableIdentity, metadata?: Record<string, any>): Promise<void>
137
137
  public async removeContact(identityKey: PubKeyHex): Promise<void>
@@ -228,7 +228,7 @@ Argument Details
228
228
  Resolves displayable identity certificates by specific identity attributes, issued by a trusted entity.
229
229
 
230
230
  ```ts
231
- async resolveByAttributes(args: DiscoverByAttributesArgs): Promise<DisplayableIdentity[]>
231
+ async resolveByAttributes(args: DiscoverByAttributesArgs, overrideWithContacts = true): Promise<DisplayableIdentity[]>
232
232
  ```
233
233
  See also: [DiscoverByAttributesArgs](./wallet.md#interface-discoverbyattributesargs), [DisplayableIdentity](./identity.md#interface-displayableidentity)
234
234
 
@@ -240,13 +240,15 @@ Argument Details
240
240
 
241
241
  + **args**
242
242
  + Attributes and optional parameters used to discover certificates.
243
+ + **overrideWithContacts**
244
+ + Whether to override the results with personal contacts if available.
243
245
 
244
246
  #### Method resolveByIdentityKey
245
247
 
246
248
  Resolves displayable identity certificates, issued to a given identity key by a trusted certifier.
247
249
 
248
250
  ```ts
249
- async resolveByIdentityKey(args: DiscoverByIdentityKeyArgs): Promise<DisplayableIdentity[]>
251
+ async resolveByIdentityKey(args: DiscoverByIdentityKeyArgs, overrideWithContacts = true): Promise<DisplayableIdentity[]>
250
252
  ```
251
253
  See also: [DiscoverByIdentityKeyArgs](./wallet.md#interface-discoverbyidentitykeyargs), [DisplayableIdentity](./identity.md#interface-displayableidentity)
252
254
 
@@ -258,6 +260,8 @@ Argument Details
258
260
 
259
261
  + **args**
260
262
  + Arguments for requesting the discovery based on the identity key.
263
+ + **overrideWithContacts**
264
+ + Whether to override the results with personal contacts if available.
261
265
 
262
266
  #### Method saveContact
263
267
 
@@ -354,7 +354,7 @@ getURLForFile = (file: Uint8Array | number[]): string => {
354
354
  }
355
355
  ```
356
356
 
357
- See also: [getURLForHash](./storage.md#variable-geturlforhash)
357
+ See also: [SHA256](./primitives.md#class-sha256), [getURLForHash](./storage.md#variable-geturlforhash)
358
358
 
359
359
  Links: [API](#api), [Interfaces](#interfaces), [Classes](#classes), [Functions](#functions), [Types](#types), [Enums](#enums), [Variables](#variables)
360
360
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bsv/sdk",
3
- "version": "1.7.0",
3
+ "version": "1.7.1",
4
4
  "type": "module",
5
5
  "description": "BSV Blockchain Software Development Kit",
6
6
  "main": "dist/cjs/mod.js",
@@ -116,11 +116,21 @@ export class IdentityClient {
116
116
  * Resolves displayable identity certificates, issued to a given identity key by a trusted certifier.
117
117
  *
118
118
  * @param {DiscoverByIdentityKeyArgs} args - Arguments for requesting the discovery based on the identity key.
119
+ * @param {boolean} [overrideWithContacts=true] - Whether to override the results with personal contacts if available.
119
120
  * @returns {Promise<DisplayableIdentity[]>} The promise resolves to displayable identities.
120
121
  */
121
122
  async resolveByIdentityKey (
122
- args: DiscoverByIdentityKeyArgs
123
+ args: DiscoverByIdentityKeyArgs,
124
+ overrideWithContacts = true
123
125
  ): Promise<DisplayableIdentity[]> {
126
+ if (overrideWithContacts) {
127
+ // Override results with personal contacts if available
128
+ const contacts = await this.contactsManager.getContacts(args.identityKey)
129
+ if (contacts.length > 0) {
130
+ return contacts
131
+ }
132
+ }
133
+
124
134
  const { certificates } = await this.wallet.discoverByIdentityKey(args, this.originator)
125
135
  return certificates.map(cert => {
126
136
  return IdentityClient.parseIdentity(cert)
@@ -131,15 +141,31 @@ export class IdentityClient {
131
141
  * Resolves displayable identity certificates by specific identity attributes, issued by a trusted entity.
132
142
  *
133
143
  * @param {DiscoverByAttributesArgs} args - Attributes and optional parameters used to discover certificates.
144
+ * @param {boolean} [overrideWithContacts=true] - Whether to override the results with personal contacts if available.
134
145
  * @returns {Promise<DisplayableIdentity[]>} The promise resolves to displayable identities.
135
146
  */
136
147
  async resolveByAttributes (
137
- args: DiscoverByAttributesArgs
148
+ args: DiscoverByAttributesArgs,
149
+ overrideWithContacts = true
138
150
  ): Promise<DisplayableIdentity[]> {
139
- const { certificates } = await this.wallet.discoverByAttributes(args, this.originator)
140
- return certificates.map(cert => {
141
- return IdentityClient.parseIdentity(cert)
142
- })
151
+ // Run both queries in parallel for better performance
152
+ const [contacts, certificatesResult] = await Promise.all([
153
+ overrideWithContacts ? this.contactsManager.getContacts() : Promise.resolve([]),
154
+ this.wallet.discoverByAttributes(args, this.originator)
155
+ ])
156
+
157
+ // Fast lookup by identityKey
158
+ const contactByKey = new Map<PubKeyHex, Contact>(
159
+ contacts.map(contact => [contact.identityKey, contact] as const)
160
+ )
161
+
162
+ // Guard if certificates might be absent
163
+ const certs = certificatesResult?.certificates ?? []
164
+
165
+ // Parse certificates and substitute with contacts where available
166
+ return certs.map(cert =>
167
+ contactByKey.get(cert.subject) ?? IdentityClient.parseIdentity(cert)
168
+ )
143
169
  }
144
170
 
145
171
  /**
@@ -226,6 +226,43 @@ describe('IdentityClient', () => {
226
226
  badgeClickURL: 'https://socialcert.net'
227
227
  })
228
228
  })
229
+
230
+ it('should prioritize contacts over discovered identities for same identity key', async () => {
231
+ const contact = {
232
+ name: 'Alice Smith (Personal Contact)',
233
+ identityKey: 'alice-identity-key',
234
+ avatarURL: 'alice-avatar.png',
235
+ abbreviatedKey: 'alice-i...',
236
+ badgeIconURL: '',
237
+ badgeLabel: '',
238
+ badgeClickURL: ''
239
+ }
240
+
241
+ const discoveredCertificate = {
242
+ type: KNOWN_IDENTITY_TYPES.xCert,
243
+ subject: 'alice-identity-key',
244
+ decryptedFields: {
245
+ userName: 'Alice Public',
246
+ profilePhoto: 'public-photo.png'
247
+ },
248
+ certifierInfo: {
249
+ name: 'CertifierX',
250
+ iconUrl: 'certifier-icon.png'
251
+ }
252
+ }
253
+
254
+ // Mock ContactsManager to return contact for the specific identity key
255
+ const mockContactsManager = identityClient['contactsManager']
256
+ mockContactsManager.getContacts = jest.fn().mockResolvedValue([contact])
257
+ walletMock.discoverByIdentityKey = jest.fn().mockResolvedValue({ certificates: [discoveredCertificate] })
258
+
259
+ const identities = await identityClient.resolveByIdentityKey({ identityKey: 'alice-identity-key' })
260
+
261
+ expect(identities).toHaveLength(1)
262
+ expect(identities[0].name).toBe('Alice Smith (Personal Contact)') // Contact should be returned, not discovered identity
263
+ // Wallet method should not be called when contact is found
264
+ expect(walletMock.discoverByIdentityKey).not.toHaveBeenCalled()
265
+ })
229
266
  })
230
267
 
231
268
  it('should throw if createAction returns no tx', async () => {
@@ -256,34 +293,124 @@ describe('IdentityClient', () => {
256
293
  })
257
294
 
258
295
  describe('resolveByAttributes', () => {
259
- it('should return parsed identities from discovered certificates', async () => {
296
+ beforeEach(() => {
297
+ // Mock both getContacts and discoverByAttributes
298
+ walletMock.discoverByAttributes = jest.fn().mockResolvedValue({ certificates: [] })
299
+ identityClient.getContacts = jest.fn().mockResolvedValue([])
300
+ })
301
+
302
+ it('should return parsed identities from discovered certificates only', async () => {
260
303
  const dummyCertificate = {
261
304
  type: KNOWN_IDENTITY_TYPES.emailCert,
262
- subject: 'emailSubject1234',
305
+ subject: 'alice-identity-key',
263
306
  decryptedFields: {
264
- email: 'alice@example.com',
265
- profilePhoto: 'ignored' // not used for email type
307
+ identityKey: 'alice-identity-key',
308
+ email: 'alice@example.com'
266
309
  },
267
310
  certifierInfo: {
268
- name: 'EmailCertifier',
269
- iconUrl: 'emailIconUrl'
311
+ name: 'Email Certifier',
312
+ iconUrl: 'certifier-icon.png',
313
+ publicKey: 'certifier-public-key',
314
+ website: 'https://certifier.example.com'
270
315
  }
271
316
  }
272
- // Mock discoverByAttributes to return a certificate list.
317
+
273
318
  walletMock.discoverByAttributes = jest.fn().mockResolvedValue({ certificates: [dummyCertificate] })
274
319
 
275
320
  const identities = await identityClient.resolveByAttributes({ attributes: { email: 'alice@example.com' } })
276
- expect(walletMock.discoverByAttributes).toHaveBeenCalledWith({ attributes: { email: 'alice@example.com' } }, undefined)
277
321
  expect(identities).toHaveLength(1)
278
- expect(identities[0]).toEqual({
279
- name: 'alice@example.com',
280
- avatarURL: 'XUTZxep7BBghAJbSBwTjNfmcsDdRFs5EaGEgkESGSgjJVYgMEizu',
281
- abbreviatedKey: 'emailSubje...',
282
- identityKey: 'emailSubject1234',
283
- badgeLabel: 'Email certified by EmailCertifier',
284
- badgeIconURL: 'emailIconUrl',
285
- badgeClickURL: 'https://socialcert.net'
286
- })
322
+ expect(identities[0].name).toBe('alice@example.com')
323
+ })
324
+
325
+ it('should prioritize contacts over discovered identities for same identity key', async () => {
326
+ const contact = {
327
+ name: 'Alice Smith (Personal)',
328
+ identityKey: 'alice-identity-key',
329
+ avatarURL: 'alice-avatar.png',
330
+ abbreviatedKey: 'alice-i...',
331
+ badgeIconURL: '',
332
+ badgeLabel: '',
333
+ badgeClickURL: ''
334
+ }
335
+
336
+ const discoveredCertificate = {
337
+ type: KNOWN_IDENTITY_TYPES.emailCert,
338
+ subject: 'alice-identity-key',
339
+ decryptedFields: {
340
+ email: 'alice@example.com'
341
+ },
342
+ certifierInfo: {
343
+ name: 'Email Certifier',
344
+ iconUrl: 'certifier-icon.png',
345
+ publicKey: 'certifier-public-key',
346
+ website: 'https://certifier.example.com'
347
+ }
348
+ }
349
+
350
+ // Mock the ContactsManager's getContacts method instead of the IdentityClient method
351
+ const mockContactsManager = identityClient['contactsManager']
352
+ mockContactsManager.getContacts = jest.fn().mockResolvedValue([contact])
353
+ walletMock.discoverByAttributes = jest.fn().mockResolvedValue({ certificates: [discoveredCertificate] })
354
+
355
+ const identities = await identityClient.resolveByAttributes({ attributes: { name: 'Alice' } })
356
+
357
+ expect(identities).toHaveLength(1)
358
+ expect(identities[0].name).toBe('Alice Smith (Personal)') // Contact should be returned, not discovered identity
359
+ })
360
+
361
+ it('should return empty array for empty search terms', async () => {
362
+ const contacts = [
363
+ {
364
+ name: 'Alice Smith',
365
+ identityKey: 'alice-key',
366
+ avatarURL: '', abbreviatedKey: 'alice-i...', badgeIconURL: '', badgeLabel: '', badgeClickURL: ''
367
+ }
368
+ ]
369
+
370
+ const mockContactsManager = identityClient['contactsManager']
371
+ mockContactsManager.getContacts = jest.fn().mockResolvedValue(contacts)
372
+
373
+ const identities = await identityClient.resolveByAttributes({ attributes: { name: '', email: ' ' } })
374
+
375
+ expect(identities).toHaveLength(0)
376
+ })
377
+
378
+ it('should return only discovered identities when overrideWithContacts is false', async () => {
379
+ const contacts = [
380
+ {
381
+ name: 'Alice Smith (Personal)',
382
+ identityKey: 'alice-key',
383
+ avatarURL: '', abbreviatedKey: 'alice-i...', badgeIconURL: '', badgeLabel: '', badgeClickURL: ''
384
+ }
385
+ ]
386
+
387
+ const discoveredCertificate = {
388
+ type: KNOWN_IDENTITY_TYPES.emailCert,
389
+ subject: 'alice-key', // Same key as contact but should not be filtered out
390
+ decryptedFields: {
391
+ email: 'alice@example.com'
392
+ },
393
+ certifierInfo: {
394
+ name: 'Email Certifier',
395
+ iconUrl: 'certifier-icon.png',
396
+ publicKey: 'certifier-public-key',
397
+ website: 'https://certifier.example.com'
398
+ }
399
+ }
400
+
401
+ const mockContactsManager = identityClient['contactsManager']
402
+ mockContactsManager.getContacts = jest.fn().mockResolvedValue(contacts)
403
+ walletMock.discoverByAttributes = jest.fn().mockResolvedValue({ certificates: [discoveredCertificate] })
404
+
405
+ // With overrideWithContacts = false, should ignore contacts entirely
406
+ const identities = await identityClient.resolveByAttributes(
407
+ { attributes: { name: 'Alice' } },
408
+ false
409
+ )
410
+
411
+ expect(identities).toHaveLength(1)
412
+ expect(identities[0].name).toBe('alice@example.com') // Should be discovered identity, not contact
413
+ expect(mockContactsManager.getContacts).not.toHaveBeenCalled() // Should not fetch contacts
287
414
  })
288
415
  })
289
416
 
@@ -434,14 +561,14 @@ describe('IdentityClient', () => {
434
561
  customInstructions: JSON.stringify({ keyID: 'existingKeyID' })
435
562
  }
436
563
 
437
- ; (walletMock.listOutputs as jest.Mock).mockResolvedValueOnce({
438
- outputs: [existingOutput],
439
- BEEF: [1, 2, 3]
440
- })
564
+ ; (walletMock.listOutputs as jest.Mock).mockResolvedValueOnce({
565
+ outputs: [existingOutput],
566
+ BEEF: [1, 2, 3]
567
+ })
441
568
 
442
- ; (walletMock.decrypt as jest.Mock).mockResolvedValue({
443
- plaintext: new TextEncoder().encode(JSON.stringify(mockContact))
444
- })
569
+ ; (walletMock.decrypt as jest.Mock).mockResolvedValue({
570
+ plaintext: new TextEncoder().encode(JSON.stringify(mockContact))
571
+ })
445
572
 
446
573
  const updatedContact = { ...mockContact, name: 'Alice Updated' }
447
574
  await identityClient.saveContact(updatedContact)
@@ -489,14 +616,14 @@ describe('IdentityClient', () => {
489
616
  customInstructions: JSON.stringify({ keyID: 'mockKeyID' })
490
617
  }
491
618
 
492
- ; (walletMock.listOutputs as jest.Mock).mockResolvedValue({
493
- outputs: [mockOutput],
494
- BEEF: [1, 2, 3]
495
- })
619
+ ; (walletMock.listOutputs as jest.Mock).mockResolvedValue({
620
+ outputs: [mockOutput],
621
+ BEEF: [1, 2, 3]
622
+ })
496
623
 
497
- ; (walletMock.decrypt as jest.Mock).mockResolvedValue({
498
- plaintext: new TextEncoder().encode(JSON.stringify(mockContact))
499
- })
624
+ ; (walletMock.decrypt as jest.Mock).mockResolvedValue({
625
+ plaintext: new TextEncoder().encode(JSON.stringify(mockContact))
626
+ })
500
627
 
501
628
  const result = await identityClient.getContacts()
502
629
 
@@ -523,11 +650,11 @@ describe('IdentityClient', () => {
523
650
  })
524
651
  await identityClient.saveContact(mockContact)
525
652
 
526
- // Mock empty result for force refresh
527
- ; (walletMock.listOutputs as jest.Mock).mockResolvedValue({
528
- outputs: [],
529
- BEEF: []
530
- })
653
+ // Mock empty result for force refresh
654
+ ; (walletMock.listOutputs as jest.Mock).mockResolvedValue({
655
+ outputs: [],
656
+ BEEF: []
657
+ })
531
658
 
532
659
  const result = await identityClient.getContacts(undefined, true)
533
660
 
@@ -542,7 +669,7 @@ describe('IdentityClient', () => {
542
669
  BEEF: []
543
670
  })
544
671
  await identityClient.saveContact(mockContact)
545
-
672
+
546
673
  const otherContact = { ...mockContact, identityKey: 'different-key', name: 'Bob' }
547
674
  await identityClient.saveContact(otherContact)
548
675
 
@@ -575,7 +702,7 @@ describe('IdentityClient', () => {
575
702
  BEEF: []
576
703
  })
577
704
  await identityClient.saveContact(mockContact)
578
-
705
+
579
706
  const otherContact = { ...mockContact, identityKey: 'other-key', name: 'Bob' }
580
707
  await identityClient.saveContact(otherContact)
581
708
 
@@ -585,14 +712,14 @@ describe('IdentityClient', () => {
585
712
  customInstructions: JSON.stringify({ keyID: 'mockKeyID' })
586
713
  }
587
714
 
588
- ; (walletMock.listOutputs as jest.Mock).mockResolvedValue({
589
- outputs: [mockOutput],
590
- BEEF: [1, 2, 3]
591
- })
715
+ ; (walletMock.listOutputs as jest.Mock).mockResolvedValue({
716
+ outputs: [mockOutput],
717
+ BEEF: [1, 2, 3]
718
+ })
592
719
 
593
- ; (walletMock.decrypt as jest.Mock).mockResolvedValue({
594
- plaintext: new TextEncoder().encode(JSON.stringify(mockContact))
595
- })
720
+ ; (walletMock.decrypt as jest.Mock).mockResolvedValue({
721
+ plaintext: new TextEncoder().encode(JSON.stringify(mockContact))
722
+ })
596
723
 
597
724
  await identityClient.removeContact(mockContact.identityKey)
598
725