@uuaid/pillar 2.6.1 → 2.7.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.
- package/README.md +201 -5
- package/bin/carrier.mjs +21 -0
- package/bin/pillar-ask.mjs +4 -1
- package/bin/pillar-blob.mjs +6 -0
- package/bin/pillar-directory.mjs +280 -16
- package/bin/pillar-escrow.mjs +4 -1
- package/bin/pillar-group-v2.mjs +6 -0
- package/bin/pillar-notify.mjs +13 -0
- package/bin/pillar-responder.mjs +266 -23
- package/bin/pillar.mjs +504 -255
- package/package.json +17 -25
- package/src/cli-help.mjs +23 -0
- package/src/group-v2-cli.mjs +1 -1
- package/src/identity/trust.mjs +72 -6
- package/src/mailbox/mailbox.mjs +308 -12
- package/src/net/carrier-server.mjs +34 -2
- package/src/net/refusals.mjs +2 -0
- package/src/net/seed-registry.mjs +2 -1
- package/src/net/transport.mjs +308 -92
- package/src/node-check.mjs +29 -8
- package/src/notify-events.mjs +148 -0
- package/src/notify-log.mjs +124 -0
- package/src/notify-runner.mjs +372 -0
- package/src/notify-store.mjs +725 -0
- package/src/notify.mjs +350 -0
- package/src/pillar.mjs +382 -27
- package/src/readers.mjs +2 -2
- package/src/registry/federation.mjs +3056 -0
- package/src/registry/gossip.mjs +294 -0
- package/src/registry/heartbeats.mjs +195 -0
- package/src/registry/lineage-verifier.mjs +972 -0
- package/src/registry/lineage.mjs +984 -0
- package/src/registry/listings.mjs +549 -0
- package/src/registry/resolve.mjs +376 -0
- package/src/registry/stats.mjs +508 -0
- package/src/sqlite-loader.mjs +40 -0
- package/src/vendor/pillar-core.mjs +22 -0
- package/src/vendor/uuaid-soul.mjs +2246 -0
- package/src/vnext/a6b-common.mjs +270 -0
- package/src/vnext/b1-cli.mjs +970 -0
- package/src/vnext/card-cli.mjs +370 -0
- package/src/vnext/greeter-operator.mjs +160 -0
- package/src/vnext/greeter-suggest.mjs +318 -0
- package/src/vnext/greeter.mjs +571 -0
- package/src/vnext/homes.mjs +48 -0
- package/src/vnext/init-cli.mjs +489 -0
- package/src/vnext/intro-cli.mjs +81 -0
- package/src/vnext/json-dir-mailbox.mjs +296 -0
- package/src/vnext/jsonl-mailbox.mjs +1066 -0
- package/src/vnext/knock-answer.mjs +162 -0
- package/src/vnext/knock-cli.mjs +520 -0
- package/src/vnext/knock-lane.mjs +312 -0
- package/src/vnext/knock-responder.mjs +344 -0
- package/src/vnext/lineage-cli.mjs +653 -0
- package/src/vnext/listing-cli.mjs +323 -0
- package/src/vnext/resolve-cli.mjs +86 -0
- package/src/vnext/soul-cli.mjs +666 -0
- package/src/vnext/verify-cli.mjs +251 -0
- package/src/vnext/w4-redirect.mjs +178 -0
- package/src/vnext/wave-a.mjs +193 -0
- package/src/vnext/witness-cli.mjs +547 -0
|
@@ -65,7 +65,7 @@
|
|
|
65
65
|
import { createServer } from "node:http";
|
|
66
66
|
import { clientIpOf, DEFAULT_TRUSTED_PROXIES } from "./client-ip.mjs";
|
|
67
67
|
import { createHash } from "node:crypto";
|
|
68
|
-
import
|
|
68
|
+
import { requireBetterSqlite3 } from "../sqlite-loader.mjs";
|
|
69
69
|
import { open as openEnvelope, envelopeSha as canonicalEnvelopeSha } from "./envelope.mjs";
|
|
70
70
|
import { Keychain, keyDerivesUuaid, isStrictEd25519PublicKey } from "../identity/keychain.mjs";
|
|
71
71
|
import { jcs } from "../identity/jcs.mjs";
|
|
@@ -179,6 +179,7 @@ export class CarrierServer {
|
|
|
179
179
|
}
|
|
180
180
|
if (!isStrictEd25519PublicKey(id.publicKeyHex)) throw new Error("receiptKeychain key is not a strict Ed25519 key");
|
|
181
181
|
}
|
|
182
|
+
const Database = requireBetterSqlite3("CarrierServer");
|
|
182
183
|
this.db = new Database(dbPath);
|
|
183
184
|
this.db.pragma("journal_mode = WAL");
|
|
184
185
|
this.secureDelete = secureDelete !== false;
|
|
@@ -244,6 +245,9 @@ export class CarrierServer {
|
|
|
244
245
|
this.server = null;
|
|
245
246
|
this.rateMap = new Map(); // F2: "<kind>:<key>" -> [ms timestamps]
|
|
246
247
|
this.activeWaits = 0; // F2: long-polls currently sleeping
|
|
248
|
+
// N4: recipient uuaid -> wakers of its sleeping long-polls. A store wakes
|
|
249
|
+
// them at once; the 400 ms tick in _handleInbox stays as the fallback.
|
|
250
|
+
this._inboxWakers = new Map();
|
|
247
251
|
this.communityWaitCap = parseCommunityWaits(communityInboxWaits);
|
|
248
252
|
this.priorityWaitCap = this.communityWaitCap + PRIORITY_WAIT_RESERVE;
|
|
249
253
|
this._closed = false;
|
|
@@ -563,6 +567,7 @@ export class CarrierServer {
|
|
|
563
567
|
).run(envelope.id, envelope.recipient, envelope.sender, sha, JSON.stringify(envelope), acceptedAt, retainUntil, canonicalSha);
|
|
564
568
|
this._bump("relayed");
|
|
565
569
|
const seq = Number(r.lastInsertRowid);
|
|
570
|
+
this._wakeInbox(envelope.recipient);
|
|
566
571
|
const body = { accepted: true, seq, sha };
|
|
567
572
|
if (this.receiptKeychain) {
|
|
568
573
|
// Re-read the committed row: the receipt is built from what is stored.
|
|
@@ -592,6 +597,32 @@ export class CarrierServer {
|
|
|
592
597
|
}
|
|
593
598
|
}
|
|
594
599
|
|
|
600
|
+
/** N4: wake every long-poll sleeping on `uuaid`'s inbox (called after a store). */
|
|
601
|
+
_wakeInbox(uuaid) {
|
|
602
|
+
const set = this._inboxWakers.get(uuaid);
|
|
603
|
+
if (set) for (const wake of [...set]) wake();
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
/**
|
|
607
|
+
* Sleep up to `ms`, or less when an envelope for `uuaid` is stored. The
|
|
608
|
+
* caller re-reads the DB after either. Registration is synchronous with the
|
|
609
|
+
* caller's empty read, so no store can fall between the two unseen.
|
|
610
|
+
*/
|
|
611
|
+
_sleepOrWake(uuaid, ms) {
|
|
612
|
+
return new Promise((resolve) => {
|
|
613
|
+
let set = this._inboxWakers.get(uuaid);
|
|
614
|
+
if (!set) { set = new Set(); this._inboxWakers.set(uuaid, set); }
|
|
615
|
+
const wake = () => {
|
|
616
|
+
clearTimeout(timer);
|
|
617
|
+
set.delete(wake);
|
|
618
|
+
if (set.size === 0 && this._inboxWakers.get(uuaid) === set) this._inboxWakers.delete(uuaid);
|
|
619
|
+
resolve();
|
|
620
|
+
};
|
|
621
|
+
const timer = setTimeout(wake, ms);
|
|
622
|
+
set.add(wake);
|
|
623
|
+
});
|
|
624
|
+
}
|
|
625
|
+
|
|
595
626
|
_verifyInboxAuth(req, uuaid, sincePath) {
|
|
596
627
|
const pubkey = req.headers["x-pillar-pubkey"];
|
|
597
628
|
const ts = req.headers["x-pillar-ts"];
|
|
@@ -686,7 +717,7 @@ export class CarrierServer {
|
|
|
686
717
|
let rows = fetchRows();
|
|
687
718
|
const deadline = Date.now() + wait * 1000;
|
|
688
719
|
while (rows.length === 0 && Date.now() < deadline && !this._closed) {
|
|
689
|
-
await
|
|
720
|
+
await this._sleepOrWake(uuaid, 400);
|
|
690
721
|
rows = fetchRows();
|
|
691
722
|
}
|
|
692
723
|
if (rows.length) this._bump("served", rows.length);
|
|
@@ -750,6 +781,7 @@ export class CarrierServer {
|
|
|
750
781
|
|
|
751
782
|
async close() {
|
|
752
783
|
this._closed = true;
|
|
784
|
+
for (const set of this._inboxWakers.values()) for (const wake of [...set]) wake();
|
|
753
785
|
if (this.server) {
|
|
754
786
|
// close() must not wait for in-flight connections: a client long-polling
|
|
755
787
|
// this carrier re-issues requests on its keep-alive socket, so
|
package/src/net/refusals.mjs
CHANGED
|
@@ -73,6 +73,8 @@ export const REFUSAL_FIXES = Object.freeze({
|
|
|
73
73
|
"blob-key-unproven": "use a self-certifying agent UUAID derived from this key (pillar init), or get a registry badge that names this key",
|
|
74
74
|
"sender-key-not-active": "ask the sender to send from a self-certifying agent UUAID derived from its own key, or to get a registry badge naming its key",
|
|
75
75
|
"group-v2-use-v2-send": "send to this pillar-group/2 group with pillar group send <group>; v1 group sends (send-group, sendGroup) do not reach it",
|
|
76
|
+
"p2p-not-installed": "install the optional libp2p transport with npm i @uuaid/pillar-p2p, or omit --libp2p to run in carrier-only mode",
|
|
77
|
+
"sqlite-not-installed": "install or rebuild optional dependency better-sqlite3 with npm i better-sqlite3",
|
|
76
78
|
});
|
|
77
79
|
|
|
78
80
|
/**
|
|
@@ -37,7 +37,7 @@ import dns from "node:dns";
|
|
|
37
37
|
import { request as httpRequest } from "node:http";
|
|
38
38
|
import { request as httpsRequest } from "node:https";
|
|
39
39
|
import { isIP } from "node:net";
|
|
40
|
-
import
|
|
40
|
+
import { requireBetterSqlite3 } from "../sqlite-loader.mjs";
|
|
41
41
|
import { jcs } from "../identity/jcs.mjs";
|
|
42
42
|
import { Keychain } from "../identity/keychain.mjs";
|
|
43
43
|
import { makeTierGrant } from "../identity/tier.mjs";
|
|
@@ -415,6 +415,7 @@ export class SeedRegistry {
|
|
|
415
415
|
this.lookup = lookupImpl ?? lookup;
|
|
416
416
|
this.allowPrivateUrls = allowPrivateUrls;
|
|
417
417
|
this.probeTimeoutMs = probeTimeoutMs;
|
|
418
|
+
const Database = requireBetterSqlite3("SeedRegistry");
|
|
418
419
|
this.db = new Database(dbPath);
|
|
419
420
|
this.db.pragma("journal_mode = WAL");
|
|
420
421
|
this.db.exec(`
|
package/src/net/transport.mjs
CHANGED
|
@@ -1,124 +1,340 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Transport — libp2p
|
|
2
|
+
* Transport — lazy libp2p loader (`@uuaid/pillar-p2p`) + pure-Node PeerId & CID helpers.
|
|
3
3
|
*
|
|
4
4
|
* Design:
|
|
5
|
-
* -
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
* envelopes. Noise encryption is handled by libp2p; the envelope
|
|
14
|
-
* signature is our IAASO-1201 attestation.
|
|
15
|
-
*
|
|
16
|
-
* Two roles:
|
|
17
|
-
* - Peer : ordinary agent. Dials others; listens for direct dials.
|
|
18
|
-
* - Relay : a Peer that ALSO runs circuitRelayServer, letting NATed
|
|
19
|
-
* peers use it as a Circuit Relay v2 rendezvous. Optional
|
|
20
|
-
* opt-in — every Peer can be a Relay; not every Peer needs to be.
|
|
5
|
+
* - Pure-Node helpers (`uuaidToDhtKey`, `peerIdFromSeedHex`, `peerIdFromEd25519Hex`,
|
|
6
|
+
* `computeUuaidCidV1`) use ONLY `node:crypto` with zero `@libp2p/*` or
|
|
7
|
+
* `multiformats` imports, so `pillar id` and `@uuaid/pillar` load in
|
|
8
|
+
* carrier-only installs without pulling the libp2p dependency tree.
|
|
9
|
+
* - `createTransport()` and `loadP2pRuntime()` dynamically import `@uuaid/pillar-p2p`,
|
|
10
|
+
* falling back to direct `import("libp2p")` when legacy `node_modules` has
|
|
11
|
+
* `libp2p` installed directly, and throwing a clear error with fix
|
|
12
|
+
* `npm i @uuaid/pillar-p2p` when neither is installed.
|
|
21
13
|
*/
|
|
22
|
-
import {
|
|
23
|
-
import {
|
|
24
|
-
import {
|
|
25
|
-
import { noise } from "@chainsafe/libp2p-noise";
|
|
26
|
-
import { yamux } from "@chainsafe/libp2p-yamux";
|
|
27
|
-
import { identify, identifyPush } from "@libp2p/identify";
|
|
28
|
-
import { kadDHT, removePrivateAddressesMapper, passthroughMapper } from "@libp2p/kad-dht";
|
|
29
|
-
import { bootstrap } from "@libp2p/bootstrap";
|
|
30
|
-
import { circuitRelayTransport, circuitRelayServer } from "@libp2p/circuit-relay-v2";
|
|
31
|
-
import { dcutr } from "@libp2p/dcutr";
|
|
32
|
-
import { autoNAT } from "@libp2p/autonat";
|
|
33
|
-
import { ping } from "@libp2p/ping";
|
|
34
|
-
import { privateKeyFromRaw, publicKeyFromRaw } from "@libp2p/crypto/keys";
|
|
35
|
-
import { peerIdFromPublicKey, peerIdFromPrivateKey } from "@libp2p/peer-id";
|
|
36
|
-
import { createHash } from "node:crypto";
|
|
14
|
+
import { createECDH, createHash } from "node:crypto";
|
|
15
|
+
import { createRequire } from "node:module";
|
|
16
|
+
import { REFUSAL_FIXES } from "./refusals.mjs";
|
|
37
17
|
|
|
38
18
|
export const PILLAR_PROTOCOL = "/uuaid/pillar/1.0.0";
|
|
39
19
|
export const DHT_PROTOCOL_PREFIX = "/uuaid";
|
|
40
20
|
|
|
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
|
-
|
|
21
|
+
const req = createRequire(import.meta.url);
|
|
22
|
+
const BASE58BTC_ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
|
|
23
|
+
const BASE32LOWER_ALPHABET = "abcdefghijklmnopqrstuvwxyz234567";
|
|
24
|
+
|
|
25
|
+
function ensurePromiseWithResolvers() {
|
|
26
|
+
if (typeof Promise.withResolvers === "function") return;
|
|
27
|
+
Object.defineProperty(Promise, "withResolvers", {
|
|
28
|
+
value: function withResolvers() {
|
|
29
|
+
if (!this) throw new TypeError("Promise.withResolvers called on non-object");
|
|
30
|
+
let resolve;
|
|
31
|
+
let reject;
|
|
32
|
+
const promise = new this((res, rej) => {
|
|
33
|
+
resolve = res;
|
|
34
|
+
reject = rej;
|
|
35
|
+
});
|
|
36
|
+
return { promise, resolve, reject };
|
|
37
|
+
},
|
|
38
|
+
writable: true,
|
|
39
|
+
configurable: true,
|
|
40
|
+
enumerable: false,
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function encodeBase58btc(bytes) {
|
|
45
|
+
let zeros = 0;
|
|
46
|
+
while (zeros < bytes.length && bytes[zeros] === 0) zeros++;
|
|
47
|
+
let num = 0n;
|
|
48
|
+
for (let i = zeros; i < bytes.length; i++) {
|
|
49
|
+
num = (num << 8n) | BigInt(bytes[i]);
|
|
50
|
+
}
|
|
51
|
+
let out = "";
|
|
52
|
+
while (num > 0n) {
|
|
53
|
+
const rem = Number(num % 58n);
|
|
54
|
+
num /= 58n;
|
|
55
|
+
out = BASE58BTC_ALPHABET[rem] + out;
|
|
56
|
+
}
|
|
57
|
+
return "1".repeat(zeros) + out;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function encodeBase32Lower(bytes) {
|
|
61
|
+
let out = "";
|
|
62
|
+
let bits = 0;
|
|
63
|
+
let value = 0;
|
|
64
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
65
|
+
value = (value << 8) | bytes[i];
|
|
66
|
+
bits += 8;
|
|
67
|
+
while (bits >= 5) {
|
|
68
|
+
bits -= 5;
|
|
69
|
+
out += BASE32LOWER_ALPHABET[(value >>> bits) & 0x1f];
|
|
70
|
+
}
|
|
75
71
|
}
|
|
72
|
+
if (bits > 0) {
|
|
73
|
+
out += BASE32LOWER_ALPHABET[(value << (5 - bits)) & 0x1f];
|
|
74
|
+
}
|
|
75
|
+
return out;
|
|
76
|
+
}
|
|
76
77
|
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
78
|
+
function makePurePeerId(keyType, rawPubBuf, protoBuf) {
|
|
79
|
+
const mhBuf = Buffer.concat([Buffer.from([0x00, protoBuf.length]), protoBuf]);
|
|
80
|
+
const str = encodeBase58btc(mhBuf);
|
|
81
|
+
const multihash = {
|
|
82
|
+
code: 0x00,
|
|
83
|
+
size: protoBuf.length,
|
|
84
|
+
digest: new Uint8Array(protoBuf),
|
|
85
|
+
bytes: new Uint8Array(mhBuf),
|
|
84
86
|
};
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
87
|
+
const publicKey = {
|
|
88
|
+
type: keyType,
|
|
89
|
+
raw: new Uint8Array(rawPubBuf),
|
|
90
|
+
};
|
|
91
|
+
return {
|
|
92
|
+
type: keyType,
|
|
93
|
+
multihash,
|
|
94
|
+
publicKey,
|
|
95
|
+
toMultihash() { return multihash; },
|
|
96
|
+
toBytes() { return multihash.bytes; },
|
|
97
|
+
toString() { return str; },
|
|
98
|
+
toJSON() { return str; },
|
|
99
|
+
equals(other) { return Boolean(other) && String(other) === str; },
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function missingP2pError(cause) {
|
|
104
|
+
const err = new Error(
|
|
105
|
+
"p2p-not-installed: libp2p transport requires the optional @uuaid/pillar-p2p package — run: npm i @uuaid/pillar-p2p",
|
|
106
|
+
cause ? { cause } : undefined,
|
|
107
|
+
);
|
|
108
|
+
err.code = "p2p-not-installed";
|
|
109
|
+
err.fix = REFUSAL_FIXES["p2p-not-installed"];
|
|
110
|
+
return err;
|
|
111
|
+
}
|
|
88
112
|
|
|
89
|
-
|
|
90
|
-
|
|
113
|
+
function isModuleNotFound(err) {
|
|
114
|
+
const code = err?.code;
|
|
115
|
+
if (code === "ERR_MODULE_NOT_FOUND" || code === "MODULE_NOT_FOUND") return true;
|
|
116
|
+
return /Cannot find package|Cannot find module/.test(String(err?.message ?? ""));
|
|
91
117
|
}
|
|
92
118
|
|
|
93
119
|
/**
|
|
94
|
-
*
|
|
95
|
-
|
|
96
|
-
|
|
120
|
+
* Synchronous check for whether `@uuaid/pillar-p2p` or legacy `libp2p` is installed.
|
|
121
|
+
*/
|
|
122
|
+
export function isP2pInstalled() {
|
|
123
|
+
if (process.env.PILLAR_DISABLE_P2P === "1") return false;
|
|
124
|
+
try {
|
|
125
|
+
const p2pEntry = req.resolve("@uuaid/pillar-p2p");
|
|
126
|
+
createRequire(p2pEntry).resolve("libp2p");
|
|
127
|
+
return true;
|
|
128
|
+
} catch {}
|
|
129
|
+
try {
|
|
130
|
+
req.resolve("libp2p");
|
|
131
|
+
return true;
|
|
132
|
+
} catch {}
|
|
133
|
+
return false;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
let cachedP2pPromise = null;
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Dynamically load `@uuaid/pillar-p2p`, falling back to direct `libp2p` imports
|
|
140
|
+
* when legacy `node_modules` has `libp2p` installed directly.
|
|
141
|
+
*/
|
|
142
|
+
export async function loadP2pRuntime() {
|
|
143
|
+
if (process.env.PILLAR_DISABLE_P2P === "1") throw missingP2pError();
|
|
144
|
+
ensurePromiseWithResolvers();
|
|
145
|
+
if (cachedP2pPromise) return cachedP2pPromise;
|
|
146
|
+
|
|
147
|
+
cachedP2pPromise = (async () => {
|
|
148
|
+
try {
|
|
149
|
+
return await import("@uuaid/pillar-p2p");
|
|
150
|
+
} catch (firstErr) {
|
|
151
|
+
if (!isModuleNotFound(firstErr)) {
|
|
152
|
+
cachedP2pPromise = null;
|
|
153
|
+
throw firstErr;
|
|
154
|
+
}
|
|
155
|
+
try {
|
|
156
|
+
const [
|
|
157
|
+
{ createLibp2p },
|
|
158
|
+
{ tcp },
|
|
159
|
+
{ webSockets },
|
|
160
|
+
{ noise },
|
|
161
|
+
{ yamux },
|
|
162
|
+
{ identify, identifyPush },
|
|
163
|
+
{ kadDHT, removePrivateAddressesMapper, passthroughMapper },
|
|
164
|
+
{ bootstrap },
|
|
165
|
+
{ circuitRelayTransport, circuitRelayServer },
|
|
166
|
+
{ dcutr },
|
|
167
|
+
{ autoNAT },
|
|
168
|
+
{ ping },
|
|
169
|
+
{ privateKeyFromRaw, publicKeyFromRaw },
|
|
170
|
+
{ peerIdFromPublicKey, peerIdFromPrivateKey, peerIdFromString, peerIdFromCID },
|
|
171
|
+
{ multiaddr },
|
|
172
|
+
{ CID },
|
|
173
|
+
raw,
|
|
174
|
+
{ sha256 },
|
|
175
|
+
] = await Promise.all([
|
|
176
|
+
import("libp2p"),
|
|
177
|
+
import("@libp2p/tcp"),
|
|
178
|
+
import("@libp2p/websockets"),
|
|
179
|
+
import("@chainsafe/libp2p-noise"),
|
|
180
|
+
import("@chainsafe/libp2p-yamux"),
|
|
181
|
+
import("@libp2p/identify"),
|
|
182
|
+
import("@libp2p/kad-dht"),
|
|
183
|
+
import("@libp2p/bootstrap"),
|
|
184
|
+
import("@libp2p/circuit-relay-v2"),
|
|
185
|
+
import("@libp2p/dcutr"),
|
|
186
|
+
import("@libp2p/autonat"),
|
|
187
|
+
import("@libp2p/ping"),
|
|
188
|
+
import("@libp2p/crypto/keys"),
|
|
189
|
+
import("@libp2p/peer-id"),
|
|
190
|
+
import("@multiformats/multiaddr"),
|
|
191
|
+
import("multiformats/cid"),
|
|
192
|
+
import("multiformats/codecs/raw"),
|
|
193
|
+
import("multiformats/hashes/sha2"),
|
|
194
|
+
]);
|
|
195
|
+
|
|
196
|
+
return {
|
|
197
|
+
PILLAR_PROTOCOL,
|
|
198
|
+
DHT_PROTOCOL_PREFIX,
|
|
199
|
+
multiaddr,
|
|
200
|
+
peerIdFromString,
|
|
201
|
+
peerIdFromCID,
|
|
202
|
+
peerIdFromPublicKey,
|
|
203
|
+
peerIdFromPrivateKey,
|
|
204
|
+
privateKeyFromRaw,
|
|
205
|
+
publicKeyFromRaw,
|
|
206
|
+
removePrivateAddressesMapper,
|
|
207
|
+
passthroughMapper,
|
|
208
|
+
CID,
|
|
209
|
+
async uuaidCid(uuaid) {
|
|
210
|
+
const digest = await sha256.digest(new Uint8Array(Buffer.from(uuaid, "utf8")));
|
|
211
|
+
return CID.createV1(raw.code, digest);
|
|
212
|
+
},
|
|
213
|
+
async createTransport({
|
|
214
|
+
seedHex,
|
|
215
|
+
listenAddrs = ["/ip4/0.0.0.0/tcp/0", "/ip4/0.0.0.0/tcp/0/ws"],
|
|
216
|
+
bootstrapPeers = [],
|
|
217
|
+
enableRelay = false,
|
|
218
|
+
dhtClientMode = true,
|
|
219
|
+
dhtPeerInfoMapper,
|
|
220
|
+
}) {
|
|
221
|
+
const seed = Buffer.from(seedHex, "hex");
|
|
222
|
+
if (seed.length !== 32) throw new Error("seedHex must be 32 raw bytes hex");
|
|
223
|
+
const privateKey = privateKeyFromRaw(seed);
|
|
224
|
+
|
|
225
|
+
const services = {
|
|
226
|
+
identify: identify(),
|
|
227
|
+
identifyPush: identifyPush(),
|
|
228
|
+
ping: ping(),
|
|
229
|
+
dht: kadDHT({
|
|
230
|
+
protocol: `${DHT_PROTOCOL_PREFIX}/kad/1.0.0`,
|
|
231
|
+
clientMode: dhtClientMode,
|
|
232
|
+
peerInfoMapper: dhtPeerInfoMapper ?? removePrivateAddressesMapper,
|
|
233
|
+
}),
|
|
234
|
+
autoNAT: autoNAT(),
|
|
235
|
+
dcutr: dcutr(),
|
|
236
|
+
};
|
|
237
|
+
if (enableRelay) {
|
|
238
|
+
services.relay = circuitRelayServer({
|
|
239
|
+
reservations: { maxReservations: 128, defaultDurationLimit: 5 * 60 * 1000 },
|
|
240
|
+
});
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
const cfg = {
|
|
244
|
+
privateKey,
|
|
245
|
+
addresses: { listen: listenAddrs },
|
|
246
|
+
transports: [tcp(), webSockets(), circuitRelayTransport()],
|
|
247
|
+
connectionEncrypters: [noise()],
|
|
248
|
+
streamMuxers: [yamux()],
|
|
249
|
+
services,
|
|
250
|
+
};
|
|
251
|
+
if (bootstrapPeers.length > 0) {
|
|
252
|
+
cfg.peerDiscovery = [bootstrap({ list: bootstrapPeers })];
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
return createLibp2p(cfg);
|
|
256
|
+
},
|
|
257
|
+
};
|
|
258
|
+
} catch (secondErr) {
|
|
259
|
+
cachedP2pPromise = null;
|
|
260
|
+
if (isModuleNotFound(secondErr)) throw missingP2pError(secondErr);
|
|
261
|
+
throw secondErr;
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
})();
|
|
265
|
+
|
|
266
|
+
return cachedP2pPromise;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
/**
|
|
270
|
+
* Build a libp2p node with the seed-derived secp256k1 transport identity.
|
|
271
|
+
*
|
|
272
|
+
* @param {{seedHex: string, listenAddrs?: string[], bootstrapPeers?: string[], enableRelay?: boolean, dhtClientMode?: boolean, dhtPeerInfoMapper?: Function}} opts
|
|
273
|
+
*/
|
|
274
|
+
export async function createTransport(opts) {
|
|
275
|
+
const p2p = await loadP2pRuntime();
|
|
276
|
+
return p2p.createTransport(opts);
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
/**
|
|
280
|
+
* Map a UUAID string to the DHT key we advertise under (raw sha256 Buffer).
|
|
97
281
|
*/
|
|
98
282
|
export function uuaidToDhtKey(uuaid) {
|
|
99
283
|
return createHash("sha256").update(uuaid, "utf-8").digest();
|
|
100
284
|
}
|
|
101
285
|
|
|
102
286
|
/**
|
|
103
|
-
*
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
287
|
+
* Pure-Node CIDv1 (`raw` 0x55 + `sha2-256` 0x12 0x20) for a UUAID string.
|
|
288
|
+
*/
|
|
289
|
+
export function computeUuaidCidV1(uuaid) {
|
|
290
|
+
const digest = createHash("sha256").update(uuaid, "utf8").digest();
|
|
291
|
+
const mhBytes = Buffer.concat([Buffer.from([0x12, 0x20]), digest]);
|
|
292
|
+
const cidBytes = Buffer.concat([Buffer.from([0x01, 0x55]), mhBytes]);
|
|
293
|
+
const str = `b${encodeBase32Lower(cidBytes)}`;
|
|
294
|
+
return {
|
|
295
|
+
version: 1,
|
|
296
|
+
code: 0x55,
|
|
297
|
+
multihash: {
|
|
298
|
+
code: 0x12,
|
|
299
|
+
size: 32,
|
|
300
|
+
digest: new Uint8Array(digest),
|
|
301
|
+
bytes: new Uint8Array(mhBytes),
|
|
302
|
+
},
|
|
303
|
+
bytes: new Uint8Array(cidBytes),
|
|
304
|
+
toV1() { return this; },
|
|
305
|
+
toString() { return str; },
|
|
306
|
+
toJSON() { return { "/": str }; },
|
|
307
|
+
};
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
/**
|
|
311
|
+
* Pure-Node libp2p PeerId of a node that `createTransport` starts with this
|
|
312
|
+
* 32-byte seed (secp256k1 compressed 33-byte public key -> protobuf
|
|
313
|
+
* `0x08 0x02 0x12 0x21 <33B>` -> identity multihash `0x00 0x25 ...` base58btc).
|
|
107
314
|
*/
|
|
108
315
|
export function peerIdFromSeedHex(seedHex) {
|
|
316
|
+
if (typeof seedHex !== "string" || !/^[0-9a-f]{64}$/i.test(seedHex)) {
|
|
317
|
+
throw new Error("seedHex must be 32 raw bytes hex");
|
|
318
|
+
}
|
|
109
319
|
const seed = Buffer.from(seedHex, "hex");
|
|
110
320
|
if (seed.length !== 32) throw new Error("seedHex must be 32 raw bytes hex");
|
|
111
|
-
|
|
321
|
+
const ecdh = createECDH("secp256k1");
|
|
322
|
+
ecdh.setPrivateKey(seed);
|
|
323
|
+
const pubCompressed = ecdh.getPublicKey(null, "compressed");
|
|
324
|
+
const proto = Buffer.concat([Buffer.from([0x08, 0x02, 0x12, 0x21]), pubCompressed]);
|
|
325
|
+
return makePurePeerId("secp256k1", pubCompressed, proto);
|
|
112
326
|
}
|
|
113
327
|
|
|
114
328
|
/**
|
|
115
|
-
*
|
|
116
|
-
*
|
|
117
|
-
* which uses a seed-derived secp256k1 key. Use peerIdFromSeedHex for that
|
|
118
|
-
* node; a UUAID's Ed25519 public key alone cannot derive its transport PeerId.
|
|
329
|
+
* Pure-Node Ed25519 libp2p PeerId from a raw 32-byte Ed25519 public key hex
|
|
330
|
+
* (protobuf `0x08 0x01 0x12 0x20 <32B>` -> identity multihash `0x00 0x24 ...` base58btc).
|
|
119
331
|
*/
|
|
120
332
|
export function peerIdFromEd25519Hex(publicKeyHex) {
|
|
333
|
+
if (typeof publicKeyHex !== "string" || !/^[0-9a-f]{64}$/i.test(publicKeyHex)) {
|
|
334
|
+
throw new Error("publicKeyHex must be 32 raw bytes hex");
|
|
335
|
+
}
|
|
121
336
|
const raw = Buffer.from(publicKeyHex, "hex");
|
|
122
|
-
|
|
123
|
-
|
|
337
|
+
if (raw.length !== 32) throw new Error("publicKeyHex must be 32 raw bytes hex");
|
|
338
|
+
const proto = Buffer.concat([Buffer.from([0x08, 0x01, 0x12, 0x20]), raw]);
|
|
339
|
+
return makePurePeerId("Ed25519", raw, proto);
|
|
124
340
|
}
|
package/src/node-check.mjs
CHANGED
|
@@ -1,17 +1,38 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Node.js version gate for
|
|
2
|
+
* Node.js version gate + Node 20 `Promise.withResolvers` polyfill for pillar CLI tools.
|
|
3
3
|
*
|
|
4
4
|
* Every bin imports this module FIRST, so it runs before any dependency does.
|
|
5
|
-
*
|
|
6
|
-
* libp2p
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
* package.json `engines` states the same floor, but npm only warns about
|
|
10
|
-
* engines; this turns the warning into a clear refusal.
|
|
5
|
+
* Node 20 LTS lacks unflagged `Promise.withResolvers` (added in Node 22.0.0),
|
|
6
|
+
* which `libp2p` and downstream packages call inside peer stores and streams;
|
|
7
|
+
* polyfilling it here lets `@uuaid/pillar` (and optional `@uuaid/pillar-p2p`)
|
|
8
|
+
* run cleanly on Node 20+.
|
|
11
9
|
*/
|
|
12
10
|
import { basename } from "node:path";
|
|
13
11
|
|
|
14
|
-
export const MIN_NODE_MAJOR =
|
|
12
|
+
export const MIN_NODE_MAJOR = 20;
|
|
13
|
+
|
|
14
|
+
export function ensurePromiseWithResolvers(target = Promise) {
|
|
15
|
+
if (typeof target.withResolvers === "function") return target.withResolvers;
|
|
16
|
+
const withResolvers = function withResolvers() {
|
|
17
|
+
if (!this) throw new TypeError("Promise.withResolvers called on non-object");
|
|
18
|
+
let resolve;
|
|
19
|
+
let reject;
|
|
20
|
+
const promise = new this((res, rej) => {
|
|
21
|
+
resolve = res;
|
|
22
|
+
reject = rej;
|
|
23
|
+
});
|
|
24
|
+
return { promise, resolve, reject };
|
|
25
|
+
};
|
|
26
|
+
Object.defineProperty(target, "withResolvers", {
|
|
27
|
+
value: withResolvers,
|
|
28
|
+
writable: true,
|
|
29
|
+
configurable: true,
|
|
30
|
+
enumerable: false,
|
|
31
|
+
});
|
|
32
|
+
return withResolvers;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
ensurePromiseWithResolvers();
|
|
15
36
|
|
|
16
37
|
/**
|
|
17
38
|
* Returns a human-readable reason this Node.js version cannot run pillar, or
|