@vordium/sdk 0.2.2 → 0.4.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.
@@ -52,36 +52,51 @@ async function refreshRecord(net, opts = {}) {
52
52
  try {
53
53
  l.record = await (0, releaseRecord_js_1.readRecord)(l.chainId, l.urls, l.keys, l.read);
54
54
  l.problem = null;
55
+ l.code = null;
55
56
  }
56
57
  catch (e) {
57
58
  if (!(e instanceof releaseRecord_js_1.RecordRefused))
58
59
  throw e;
59
60
  l.record = null;
60
- l.problem = `${e.message} Nothing was signed.`;
61
+ l.problem = withNothing(e.message);
62
+ l.code = e.code;
61
63
  }
62
- if (!l.record && !l.problem && l.byChain)
64
+ if (!l.record && !l.problem && l.byChain) {
63
65
  l.problem = noRecordText(l.chainId);
66
+ l.code = 'not_listed';
67
+ }
64
68
  l.at = Date.now();
65
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
+ }
66
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.`;
67
78
  /** The pin from the network's release record as last read (throws when it cannot be used), or null when no record is
68
79
  * served and none was used for the network (then its pin applies). */
69
80
  function recordSigningPin(net, l) {
70
81
  const now = l.read.now ?? Date.now;
71
- let problem = l.problem;
72
- if (!problem && l.record && now() / 1000 > l.record.record.validUntil)
73
- problem = `The signed release record expired; it is read again before the next signature. Nothing was signed.`;
74
- if (!problem && !l.record && net.record)
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) {
75
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
+ }
76
91
  if (problem)
77
- throw l.byChain ? new pins_js_1.UnpinnedNetwork(problem) : new pins_js_1.PinMismatch(problem);
92
+ throw recordError(problem, code, l.byChain);
78
93
  let pin;
79
94
  if (!l.record) {
80
95
  if (!net.recordProblem)
81
96
  return null;
82
97
  const named = presetPinOrNull({ ...net, pin: undefined });
83
98
  if (!named)
84
- throw new pins_js_1.UnpinnedNetwork(net.recordProblem);
99
+ throw recordError(net.recordProblem, net.recordProblemCode, true);
85
100
  pin = named;
86
101
  }
87
102
  else {
@@ -129,7 +144,7 @@ function signingPin(net) {
129
144
  return live;
130
145
  }
131
146
  if (net.recordProblem)
132
- throw net.record || presetPinOrNull(net) ? new pins_js_1.PinMismatch(net.recordProblem) : new pins_js_1.UnpinnedNetwork(net.recordProblem);
147
+ throw recordError(net.recordProblem, net.recordProblemCode, !(net.record || presetPinOrNull(net)));
133
148
  const pin = net.record && net.pin ? net.pin : presetPin(net);
134
149
  if (net.pinProblem)
135
150
  throw pin ? new pins_js_1.PinMismatch(net.pinProblem) : new pins_js_1.UnpinnedNetwork(net.pinProblem);
@@ -218,12 +233,13 @@ async function connectNetwork(network = 'testnet', opts = {}) {
218
233
  const recordChain = p.recordChainId ?? (named ? named.chainId : null);
219
234
  let record = null;
220
235
  let recordProblem = null;
236
+ let recordProblemCode = null;
221
237
  let recordLive;
222
238
  if (recordChain !== null) {
223
239
  const urls = p.releaseRecord ? [p.releaseRecord]
224
240
  : p.recordChainId !== undefined ? Object.values(releaseRecord_js_1.RELEASE_RECORD_URLS).flat() // a chain only the record names: every address
225
241
  : releaseRecord_js_1.RELEASE_RECORD_URLS[p.name] ?? [];
226
- const read = { transport: t, timeoutMs: ms, store: opts.releaseRecordStore, now: opts.releaseRecordNow };
242
+ const read = { transport: t, timeoutMs: ms, store: opts.releaseRecordStore, now: opts.releaseRecordNow, minSequence: opts.releaseRecordMinSequence };
227
243
  try {
228
244
  const keys = (0, releaseRecord_js_1.pinnedKeys)(opts.releaseKeys);
229
245
  recordLive = { chainId: recordChain, urls, keys, byChain: p.recordChainId !== undefined, read, record: null, problem: null, at: Date.now() };
@@ -232,12 +248,15 @@ async function connectNetwork(network = 'testnet', opts = {}) {
232
248
  catch (e) {
233
249
  if (!(e instanceof releaseRecord_js_1.RecordRefused))
234
250
  throw e;
235
- recordProblem = `${e.message} Nothing was signed.`;
251
+ recordProblem = withNothing(e.message);
252
+ recordProblemCode = e.code;
236
253
  }
237
- if (!record && !recordProblem && p.recordChainId !== undefined)
254
+ if (!record && !recordProblem && p.recordChainId !== undefined) {
238
255
  recordProblem = noRecordText(p.recordChainId);
256
+ recordProblemCode = 'not_listed';
257
+ }
239
258
  if (recordLive)
240
- Object.assign(recordLive, { record, problem: recordProblem, at: Date.now() });
259
+ Object.assign(recordLive, { record, problem: recordProblem, code: recordProblemCode, at: Date.now() });
241
260
  }
242
261
  const pin = record ? Object.freeze({ ...(p.name ? { name: p.name } : {}), chainId: recordChain, genesis: record.entry.genesis }) : hardPin;
243
262
  const sr = await t.get(p.spec, ms);
@@ -277,8 +296,11 @@ async function connectNetwork(network = 'testnet', opts = {}) {
277
296
  }
278
297
  if (d.length)
279
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
+ }
280
302
  }
281
303
  if (pinProblem && opts.onPinProblem)
282
304
  opts.onPinProblem(pinProblem);
283
- return { ...p, ...(pin ? { pin } : {}), chainId: ident.chainId, genesis: ident.genesis, specRead: spec, pinProblem, record, recordProblem, ...(recordLive ? { recordLive } : {}) };
305
+ return { ...p, ...(pin ? { pin } : {}), chainId: ident.chainId, genesis: ident.genesis, specRead: spec, pinProblem, record, recordProblem, recordProblemCode, ...(recordLive ? { recordLive } : {}) };
284
306
  }