Skip to content

Commit dd131c1

Browse files
authored
Merge pull request #3 from block-core/improve-nostr-scaling
Improve the resolve logic to only use provided relays for kind:10002
2 parents c4a3176 + 9ff1e0e commit dd131c1

File tree

5 files changed

+52
-20
lines changed

5 files changed

+52
-20
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,18 @@ Library that helps resolve DID Documents (decentralized identities) from the "di
44

55
Specification: https://nostrcg.github.io/did-nostr/
66

7+
## Description
8+
9+
This library is a JavaScript implementation of the Nostr DID Resolver. It allows you to resolve DIDs (Decentralized Identifiers) using the Nostr protocol. The resolver constructs the DID document from the events retrieived specified relays (and user's relay list) and returns it in a standard format.
10+
11+
The relays provided (or default) will only be used to retrieve the relay list (kind:10002),
12+
upon retrieving the relay list, the resolver will use the relays from the list to retrieve the profile metadata (kind:0).
13+
14+
If the profile metadata is not found on the user's relays, it will fall back to the provided (or default) relays for another attempt at finding the user profile.
15+
16+
This ensures that Nostr can scale better and the initial bootstrapping is only to discover
17+
the relay list (kind:10002).
18+
719
## Usage
820

921
```sh

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@blockcore/nostr-did-resolver",
3-
"version": "0.0.3",
3+
"version": "0.0.4",
44
"description": "",
55
"main": "lib/index.js",
66
"type": "module",

src/resolver.ts

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,49 @@ export class BlockcoreDidResolver {
3030
const pool = new SimplePool();
3131
const services = [] as any[];
3232
let profile = null;
33-
34-
const metadata = await pool.get(this.#relays, {
35-
kinds: [kinds.Metadata],
36-
authors: [parsed.id],
37-
});
33+
let metadata = null;
3834

3935
const relays = await pool.get(this.#relays, {
4036
kinds: [kinds.RelayList],
4137
authors: [parsed.id],
4238
});
4339

40+
let relayUrls: string[] = [];
41+
42+
if (relays) {
43+
relayUrls = relays.tags.filter(tag => tag.length >= 2 && tag[0] === 'r').map(tag => tag[1]);
44+
45+
for (let i = 0; i < relayUrls.length; i++) {
46+
services.push({
47+
id: `${parsed.did}#${i + 1}`,
48+
type: 'Relay',
49+
serviceEndpoint: relayUrls[i],
50+
});
51+
}
52+
}
53+
54+
// Attempt to connect to the user's defined relays, to help Nostr with
55+
// scaling, we don't use the default relays here.
56+
if (relayUrls.length > 0) {
57+
const userPool = new SimplePool();
58+
59+
metadata = await userPool.get(relayUrls, {
60+
kinds: [kinds.Metadata],
61+
authors: [parsed.id],
62+
});
63+
64+
userPool.close(relayUrls);
65+
}
66+
67+
// If we for some reason did not find the user profile on their defined relays,
68+
// or we didn't find relays, we will fall back to the default relays.
69+
if (!metadata) {
70+
metadata = await pool.get(this.#relays, {
71+
kinds: [kinds.Metadata],
72+
authors: [parsed.id],
73+
});
74+
}
75+
4476
pool.close(this.#relays);
4577

4678
if (metadata?.content) {
@@ -66,18 +98,6 @@ export class BlockcoreDidResolver {
6698
// }
6799
}
68100

69-
if (relays) {
70-
const relayUrls = relays.tags.filter(tag => tag.length >= 2 && tag[0] === 'r').map(tag => tag[1]);
71-
72-
for (let i = 0; i < relayUrls.length; i++) {
73-
services.push({
74-
id: `${parsed.did}#${i + 1}`,
75-
type: 'Relay',
76-
serviceEndpoint: relayUrls[i],
77-
});
78-
}
79-
}
80-
81101
const didDocument = this.getDocument(parsed.did, services);
82102

83103
const resolution: DIDResolutionResult = {

test/identity.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ test('Create the resolver', async t => {
2525
t.assert(didResolution.didResolutionMetadata.contentType == 'application/did+ld+json');
2626

2727
t.assert(didResolution.didDocument.verificationMethod[0].publicKeyMultibase == 'z6DtPPzVD8nXDKTHG3x8cx8UpoVP6VSBsXaDhSWcoysUnkEY');
28-
t.assert(didResolution.didDocument.service[0].serviceEndpoint == 'https://sondreb.com');
2928
t.assert(didResolution.didDocumentMetadata.profile !== null);
3029
t.assert(didResolution.didDocumentMetadata.profile.name == 'sondreb');
30+
t.assert(didResolution.didDocument.service.find(s => s.type === 'LinkedDomains')?.serviceEndpoint === 'https://sondreb.com');
3131

3232
// Used to generate README example:
3333
// console.log(JSON.stringify(didResolution, null, 2));

0 commit comments

Comments
 (0)