@vordium/sdk 0.2.1 → 0.3.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.
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.NETWORKS = void 0;
3
+ exports.RECORD_RETRY_MS = exports.RECORD_TTL_MS = exports.NETWORKS = void 0;
4
+ exports.refreshRecord = refreshRecord;
4
5
  exports.presetPin = presetPin;
5
6
  exports.signingPin = signingPin;
6
7
  exports.readIdentity = readIdentity;
@@ -11,6 +12,7 @@ const errors_js_1 = require("./errors.js");
11
12
  const http_js_1 = require("./http.js");
12
13
  const domains_js_1 = require("./domains.js");
13
14
  const pins_js_1 = require("./pins.js");
15
+ const releaseRecord_js_1 = require("./releaseRecord.js");
14
16
  exports.NETWORKS = {
15
17
  mainnet: {
16
18
  name: 'mainnet',
@@ -29,6 +31,96 @@ exports.NETWORKS = {
29
31
  feeds: 'wss://testnet.vordex.xyz/chain/feeds',
30
32
  },
31
33
  };
34
+ /** How long a network's release record is used before it is read again (ms), and how soon a refused one is retried. */
35
+ exports.RECORD_TTL_MS = 300_000;
36
+ exports.RECORD_RETRY_MS = 30_000;
37
+ /**
38
+ * Read the network's release record again when it has expired, when it is older than {@link RECORD_TTL_MS}, or when it
39
+ * was refused more than {@link RECORD_RETRY_MS} ago. A client calls it before every signature; {@link signingPin} then
40
+ * judges the record as it is now.
41
+ */
42
+ async function refreshRecord(net, opts = {}) {
43
+ const l = net.recordLive;
44
+ if (!l)
45
+ return;
46
+ const now = l.read.now ?? Date.now;
47
+ const age = Date.now() - l.at;
48
+ const due = (l.record && (now() / 1000 > l.record.record.validUntil || age >= (opts.ttlMs ?? exports.RECORD_TTL_MS)))
49
+ || (l.problem !== null && age >= (opts.retryMs ?? exports.RECORD_RETRY_MS));
50
+ if (!due)
51
+ return;
52
+ try {
53
+ l.record = await (0, releaseRecord_js_1.readRecord)(l.chainId, l.urls, l.keys, l.read);
54
+ l.problem = null;
55
+ l.code = null;
56
+ }
57
+ catch (e) {
58
+ if (!(e instanceof releaseRecord_js_1.RecordRefused))
59
+ throw e;
60
+ l.record = null;
61
+ l.problem = withNothing(e.message);
62
+ l.code = e.code;
63
+ }
64
+ if (!l.record && !l.problem && l.byChain) {
65
+ l.problem = noRecordText(l.chainId);
66
+ l.code = 'not_listed';
67
+ }
68
+ l.at = Date.now();
69
+ }
70
+ /** A record refusal's sentence, saying once that nothing was signed. */
71
+ const withNothing = (m) => (/Nothing (is|was) signed/.test(m) ? m : `${m} Nothing was signed.`);
72
+ /** The error for a record problem: a `PinMismatch` (or `UnpinnedNetwork` for a network named only by its chain id) whose
73
+ * `recordCode` names why. */
74
+ function recordError(message, code, unpinned) {
75
+ return Object.assign(unpinned ? new pins_js_1.UnpinnedNetwork(message) : new pins_js_1.PinMismatch(message), { recordCode: code ?? null });
76
+ }
77
+ const noRecordText = (chainId) => `Refusing to sign: no signed release record lists chain ${chainId}, and a network named only by its chain id needs one. Nothing was signed.`;
78
+ /** The pin from the network's release record as last read (throws when it cannot be used), or null when no record is
79
+ * served and none was used for the network (then its pin applies). */
80
+ function recordSigningPin(net, l) {
81
+ const now = l.read.now ?? Date.now;
82
+ let problem = l.problem, code = l.code ?? null;
83
+ if (!problem && l.record && now() / 1000 > l.record.record.validUntil) {
84
+ problem = "The signed release record expired; it is read again before the next signature. Nothing was signed. Try again later; if this persists, check this computer's clock.";
85
+ code = 'expired';
86
+ }
87
+ if (!problem && !l.record && net.record) {
88
+ problem = 'Refusing to sign: the signed release record this network was checked against is not served now. Nothing was signed.';
89
+ code = 'not_served';
90
+ }
91
+ if (problem)
92
+ throw recordError(problem, code, l.byChain);
93
+ let pin;
94
+ if (!l.record) {
95
+ if (!net.recordProblem)
96
+ return null;
97
+ const named = presetPinOrNull({ ...net, pin: undefined });
98
+ if (!named)
99
+ throw recordError(net.recordProblem, net.recordProblemCode, true);
100
+ pin = named;
101
+ }
102
+ else {
103
+ pin = Object.freeze({ ...(net.name ? { name: net.name } : {}), chainId: l.record.entry.chainId, genesis: l.record.entry.genesis });
104
+ }
105
+ const same = !!(net.record && l.record && net.record.entry.genesis === l.record.entry.genesis);
106
+ if (same && net.pinProblem)
107
+ throw new pins_js_1.PinMismatch(net.pinProblem);
108
+ const d = (0, pins_js_1.pinDifferences)(pin, { 'the network': net.chainId }, { 'the network': net.genesis });
109
+ if (l.record && l.record.entry.retiredGenesis.includes(net.genesis)) {
110
+ d.unshift(`the node serves genesis ${net.genesis}, which the signed release record lists in retired_genesis for chain ${l.record.entry.chainId}`);
111
+ }
112
+ if (d.length)
113
+ throw new pins_js_1.PinMismatch((0, pins_js_1.pinRefusal)(pin, d));
114
+ return pin;
115
+ }
116
+ function presetPinOrNull(p) {
117
+ try {
118
+ return presetPin(p);
119
+ }
120
+ catch {
121
+ return null;
122
+ }
123
+ }
32
124
  /** The pin a preset is held to: its own `pin`, else the pin of the named network it names; null when it has none. */
33
125
  function presetPin(p) {
34
126
  const named = Object.prototype.hasOwnProperty.call(pins_js_1.PINS, p.name) ? pins_js_1.PINS[p.name] : undefined;
@@ -46,7 +138,14 @@ function presetPin(p) {
46
138
  * `PinMismatch` when what it reported differs from the pin; nothing may be signed then.
47
139
  */
48
140
  function signingPin(net) {
49
- const pin = presetPin(net);
141
+ if (net.recordLive) {
142
+ const live = recordSigningPin(net, net.recordLive);
143
+ if (live)
144
+ return live;
145
+ }
146
+ if (net.recordProblem)
147
+ throw recordError(net.recordProblem, net.recordProblemCode, !(net.record || presetPinOrNull(net)));
148
+ const pin = net.record && net.pin ? net.pin : presetPin(net);
50
149
  if (net.pinProblem)
51
150
  throw pin ? new pins_js_1.PinMismatch(net.pinProblem) : new pins_js_1.UnpinnedNetwork(net.pinProblem);
52
151
  if (!pin)
@@ -114,17 +213,52 @@ async function rpcChainId(rpc, transport, timeoutMs) {
114
213
  * `eth_chainId` name the same chain id, the spec and the node the same genesis, and the spec's `testnet` flag matches
115
214
  * the preset (a test network never stands in for the main network, nor the reverse).
116
215
  *
117
- * The reports are then held to the network's pin. When they differ (or the network has no pin) the connection still
118
- * reads, `pinProblem` says why, `onPinProblem` (when given) is called with it, and every signature is refused
119
- * (`PinMismatch` / `UnpinnedNetwork`). The SDK prints nothing.
216
+ * The network's identity comes from the signed release record when one is served (its genesis for the network's chain
217
+ * id; see `readRecord`), else from its pin. When the reports differ (or the network has no identity, or the served
218
+ * record cannot be used) the connection still reads, `pinProblem` says why, `onPinProblem` (when given) is called with
219
+ * it, and every signature is refused (`PinMismatch` / `UnpinnedNetwork`). The SDK prints nothing.
120
220
  */
121
221
  async function connectNetwork(network = 'testnet', opts = {}) {
122
222
  const p = typeof network === 'string' ? exports.NETWORKS[network] : network;
123
223
  if (!p)
124
224
  throw new errors_js_1.VordiumError('unknown_network', `Unknown network ${String(network)}; known: mainnet, testnet.`);
125
- const pin = presetPin(p);
225
+ const named = Object.prototype.hasOwnProperty.call(pins_js_1.PINS, p.name) ? pins_js_1.PINS[p.name] : undefined;
226
+ if (p.recordChainId !== undefined && (!Number.isSafeInteger(p.recordChainId) || p.recordChainId <= 0 || p.pin || named)) {
227
+ throw new errors_js_1.VordiumError('unknown_network', 'recordChainId names a network only the signed release record lists: give a positive chain id, with no pin and a name that is not mainnet or testnet.');
228
+ }
229
+ const hardPin = presetPin(p);
126
230
  const t = opts.transport ?? http_js_1.fetchTransport;
127
231
  const ms = opts.timeoutMs ?? 10000;
232
+ // The identity: the signed record's for a named network or a record-only chain id; an explicit pin of your own skips it.
233
+ const recordChain = p.recordChainId ?? (named ? named.chainId : null);
234
+ let record = null;
235
+ let recordProblem = null;
236
+ let recordProblemCode = null;
237
+ let recordLive;
238
+ if (recordChain !== null) {
239
+ const urls = p.releaseRecord ? [p.releaseRecord]
240
+ : p.recordChainId !== undefined ? Object.values(releaseRecord_js_1.RELEASE_RECORD_URLS).flat() // a chain only the record names: every address
241
+ : releaseRecord_js_1.RELEASE_RECORD_URLS[p.name] ?? [];
242
+ const read = { transport: t, timeoutMs: ms, store: opts.releaseRecordStore, now: opts.releaseRecordNow, minSequence: opts.releaseRecordMinSequence };
243
+ try {
244
+ const keys = (0, releaseRecord_js_1.pinnedKeys)(opts.releaseKeys);
245
+ recordLive = { chainId: recordChain, urls, keys, byChain: p.recordChainId !== undefined, read, record: null, problem: null, at: Date.now() };
246
+ record = await (0, releaseRecord_js_1.readRecord)(recordChain, urls, keys, read);
247
+ }
248
+ catch (e) {
249
+ if (!(e instanceof releaseRecord_js_1.RecordRefused))
250
+ throw e;
251
+ recordProblem = withNothing(e.message);
252
+ recordProblemCode = e.code;
253
+ }
254
+ if (!record && !recordProblem && p.recordChainId !== undefined) {
255
+ recordProblem = noRecordText(p.recordChainId);
256
+ recordProblemCode = 'not_listed';
257
+ }
258
+ if (recordLive)
259
+ Object.assign(recordLive, { record, problem: recordProblem, code: recordProblemCode, at: Date.now() });
260
+ }
261
+ const pin = record ? Object.freeze({ ...(p.name ? { name: p.name } : {}), chainId: recordChain, genesis: record.entry.genesis }) : hardPin;
128
262
  const sr = await t.get(p.spec, ms);
129
263
  if (sr.status !== 200 || !sr.body || typeof sr.body !== 'object')
130
264
  throw new errors_js_1.VordiumError('no_spec', `The network spec at ${p.spec} could not be read.`);
@@ -151,16 +285,22 @@ async function connectNetwork(network = 'testnet', opts = {}) {
151
285
  if (specGenesis !== null && specGenesis !== ident.genesis) {
152
286
  throw new errors_js_1.NetworkMismatch(`The spec names genesis ${specGenesis.slice(0, 8)}…, the node ${ident.genesis.slice(0, 8)}…; refusing to sign.`);
153
287
  }
154
- let pinProblem = null;
155
- if (!pin) {
288
+ let pinProblem = recordProblem;
289
+ if (!pinProblem && !pin) {
156
290
  pinProblem = unpinnedText(p);
157
291
  }
158
- else {
292
+ else if (!pinProblem && pin) {
159
293
  const d = (0, pins_js_1.pinDifferences)(pin, { 'the network spec': specChain, "the node's /status": ident.chainId, eth_chainId: rpcChain }, { 'the network spec': specGenesis, "the node's /status": ident.genesis });
294
+ if (record && record.entry.retiredGenesis.includes(ident.genesis)) {
295
+ d.unshift(`the node serves genesis ${ident.genesis}, which the signed release record lists in retired_genesis for chain ${record.entry.chainId}`);
296
+ }
160
297
  if (d.length)
161
298
  pinProblem = (0, pins_js_1.pinRefusal)(pin, d);
299
+ if (d.length && !record && recordChain !== null && d.some((x) => x.includes('genesis'))) {
300
+ pinProblem += " The signed release record could not be read, so the SDK's own pin was used: if this network has moved to a new genesis, try again once the record can be read.";
301
+ }
162
302
  }
163
303
  if (pinProblem && opts.onPinProblem)
164
304
  opts.onPinProblem(pinProblem);
165
- return { ...p, ...(pin ? { pin } : {}), chainId: ident.chainId, genesis: ident.genesis, specRead: spec, pinProblem };
305
+ return { ...p, ...(pin ? { pin } : {}), chainId: ident.chainId, genesis: ident.genesis, specRead: spec, pinProblem, record, recordProblem, recordProblemCode, ...(recordLive ? { recordLive } : {}) };
166
306
  }
package/dist/cjs/nonce.js CHANGED
@@ -280,12 +280,16 @@ function fileNonceStore(dir, opts = {}) {
280
280
  let done = null;
281
281
  try {
282
282
  let last = 0;
283
+ let text = null;
283
284
  try {
284
- const t = (await fs.readFile(file, 'utf8')).trim();
285
- if (/^\d+$/.test(t))
286
- last = Number(t);
285
+ text = (await fs.readFile(file, 'utf8')).trim();
287
286
  }
288
287
  catch { /* no mark yet */ }
288
+ if (text !== null && /^\d+$/.test(text))
289
+ last = Number(text);
290
+ else if (text !== null && opts?.strict) {
291
+ throw new errors_js_1.VordiumError('state_unreadable', `The mark in ${file} cannot be read. Check the file, or remove it to start again.`);
292
+ }
289
293
  let n = last, claimed = false, seen = [];
290
294
  if (!unique)
291
295
  n = fn(last); // a record, not a nonce