cafe-utility 36.0.0 → 36.1.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.
- package/index.d.ts +3 -1
- package/index.js +230 -236
- package/package.json +1 -1
package/index.d.ts
CHANGED
|
@@ -725,7 +725,9 @@ export declare class ChunkSplitter {
|
|
|
725
725
|
private counters
|
|
726
726
|
private pending
|
|
727
727
|
private onBatch
|
|
728
|
-
|
|
728
|
+
private onIntermediateChunk?
|
|
729
|
+
private hasParity
|
|
730
|
+
constructor(onBatch: (batch: ChunkEntry[]) => Promise<ChunkEntry[]>, maxShards?: number, encrypted?: boolean, onIntermediateChunk?: (chunk: Chunk, hasParity: boolean) => void)
|
|
729
731
|
static root(data: Uint8Array): Promise<Chunk>
|
|
730
732
|
static encryptedRoot(data: Uint8Array): Promise<{
|
|
731
733
|
address: Uint8Array
|
package/index.js
CHANGED
|
@@ -11,7 +11,7 @@ async function runInParallelBatches(n, t = 1) {
|
|
|
11
11
|
const e = splitByCount(n, t),
|
|
12
12
|
r = [],
|
|
13
13
|
i = e.map(async o => {
|
|
14
|
-
for (const
|
|
14
|
+
for (const s of o) r.push(await s())
|
|
15
15
|
})
|
|
16
16
|
return await Promise.all(i), r
|
|
17
17
|
}
|
|
@@ -122,18 +122,18 @@ function pickManyUnique(n, t, e, r = Math.random) {
|
|
|
122
122
|
const i = []
|
|
123
123
|
for (; i.length < t; ) {
|
|
124
124
|
const o = pick(n, r)
|
|
125
|
-
i.some(
|
|
125
|
+
i.some(s => e(s, o)) || i.push(o)
|
|
126
126
|
}
|
|
127
127
|
return i
|
|
128
128
|
}
|
|
129
129
|
function pickGuaranteed(n, t, e, r, i, o = Math.random) {
|
|
130
|
-
const
|
|
131
|
-
|
|
132
|
-
for (t !== null &&
|
|
133
|
-
const u = exports.Random.intBetween(0,
|
|
134
|
-
i(
|
|
130
|
+
const s = n.filter(u => u !== t && u !== e),
|
|
131
|
+
c = []
|
|
132
|
+
for (t !== null && c.push(t); s.length && c.length < r; ) {
|
|
133
|
+
const u = exports.Random.intBetween(0, s.length - 1, o)
|
|
134
|
+
i(s[u], c) && c.push(s[u]), s.splice(u, 1)
|
|
135
135
|
}
|
|
136
|
-
return shuffle(
|
|
136
|
+
return shuffle(c, o), { values: c, indexOfGuaranteed: t !== null ? c.indexOf(t) : -1 }
|
|
137
137
|
}
|
|
138
138
|
function last(n) {
|
|
139
139
|
if (!n.length) throw Error('Received empty array')
|
|
@@ -147,7 +147,7 @@ function makePipe(n, t) {
|
|
|
147
147
|
}
|
|
148
148
|
function pickWeighted(n, t, e) {
|
|
149
149
|
if ((e === void 0 && (e = Math.random()), n.length !== t.length)) throw new Error('Array length mismatch')
|
|
150
|
-
let r = t.reduce((o,
|
|
150
|
+
let r = t.reduce((o, s) => o + s, 0)
|
|
151
151
|
const i = e * r
|
|
152
152
|
for (let o = 0; o < n.length - 1; o++) if (((r -= t[o]), i >= r)) return n[o]
|
|
153
153
|
return last(n)
|
|
@@ -156,7 +156,7 @@ function sortWeighted(n, t, e = Math.random) {
|
|
|
156
156
|
const r = t.map(o => e() * o),
|
|
157
157
|
i = []
|
|
158
158
|
for (let o = 0; o < n.length; o++) i.push([n[o], r[o]])
|
|
159
|
-
return i.sort((o,
|
|
159
|
+
return i.sort((o, s) => s[1] - o[1]).map(o => o[0])
|
|
160
160
|
}
|
|
161
161
|
function getDeep(n, t) {
|
|
162
162
|
if (n == null) return null
|
|
@@ -172,11 +172,11 @@ function setDeep(n, t, e) {
|
|
|
172
172
|
const r = t.split(/\.|\[/)
|
|
173
173
|
let i = n
|
|
174
174
|
for (let o = 0; o < r.length; o++) {
|
|
175
|
-
const
|
|
176
|
-
|
|
177
|
-
f =
|
|
175
|
+
const s = r[o],
|
|
176
|
+
c = o < r.length - 1 && r[o + 1].includes(']'),
|
|
177
|
+
f = s.includes(']') ? s.replace(/\[|\]/g, '') : s
|
|
178
178
|
if (o === r.length - 1) return (i[f] = e), e
|
|
179
|
-
isObject(i[f]) || (
|
|
179
|
+
isObject(i[f]) || (c ? (i[f] = []) : (i[f] = {})), (i = i[f])
|
|
180
180
|
}
|
|
181
181
|
return e
|
|
182
182
|
}
|
|
@@ -232,16 +232,16 @@ function whereAmI() {
|
|
|
232
232
|
return n ? (n.browser === !0 ? 'browser' : 'node') : 'browser'
|
|
233
233
|
}
|
|
234
234
|
async function withRetries(n, t, e, r, i, o) {
|
|
235
|
-
let
|
|
236
|
-
for (let
|
|
235
|
+
let s = null
|
|
236
|
+
for (let c = 0; c <= t; c++)
|
|
237
237
|
try {
|
|
238
238
|
return await n()
|
|
239
239
|
} catch (u) {
|
|
240
|
-
if (((
|
|
241
|
-
const f = e + (r - e) * (
|
|
242
|
-
i && i('Error in withRetries, retrying', { attempt:
|
|
240
|
+
if (((s = u), c === t)) break
|
|
241
|
+
const f = e + (r - e) * (c / (t - 1))
|
|
242
|
+
i && i('Error in withRetries, retrying', { attempt: c + 1, allowedFailures: t, delayMillis: f, error: u }), o && o(), await sleepMillis(f)
|
|
243
243
|
}
|
|
244
|
-
throw
|
|
244
|
+
throw s
|
|
245
245
|
}
|
|
246
246
|
function asMegabytes(n) {
|
|
247
247
|
return n / 1024 / 1024
|
|
@@ -259,10 +259,10 @@ function rgbToHex(n) {
|
|
|
259
259
|
}
|
|
260
260
|
function haversineDistanceToMeters(n, t, e, r) {
|
|
261
261
|
const o = (n * Math.PI) / 180,
|
|
262
|
-
|
|
263
|
-
|
|
262
|
+
s = (e * Math.PI) / 180,
|
|
263
|
+
c = ((e - n) * Math.PI) / 180,
|
|
264
264
|
u = ((r - t) * Math.PI) / 180,
|
|
265
|
-
f = Math.sin(
|
|
265
|
+
f = Math.sin(c / 2) * Math.sin(c / 2) + Math.cos(o) * Math.cos(s) * Math.sin(u / 2) * Math.sin(u / 2)
|
|
266
266
|
return 6371e3 * (2 * Math.atan2(Math.sqrt(f), Math.sqrt(1 - f)))
|
|
267
267
|
}
|
|
268
268
|
function roundToNearest(n, t) {
|
|
@@ -282,9 +282,9 @@ function searchFloat(n) {
|
|
|
282
282
|
function binomialSample(n, t, e = Math.random) {
|
|
283
283
|
const r = n * t,
|
|
284
284
|
i = Math.sqrt(n * t * (1 - t)),
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
return Math.max(0, Math.min(n,
|
|
285
|
+
s = (e() + e() + e() + e() + e() + e() - 3) * Math.SQRT2,
|
|
286
|
+
c = Math.round(r + i * s)
|
|
287
|
+
return Math.max(0, Math.min(n, c))
|
|
288
288
|
}
|
|
289
289
|
function toSignificantDigits(n, t) {
|
|
290
290
|
if (!n.includes('.')) return n
|
|
@@ -298,8 +298,8 @@ function toSignificantDigits(n, t) {
|
|
|
298
298
|
l = r.slice(0, f)
|
|
299
299
|
return /[1-9]/.test(l) ? `${e}.${l.replace(/0+$/, '')}` : e
|
|
300
300
|
}
|
|
301
|
-
const
|
|
302
|
-
u = t +
|
|
301
|
+
const c = r.match(/^0*/)?.[0].length ?? 0,
|
|
302
|
+
u = t + c
|
|
303
303
|
return `${e}.${r.slice(0, u)}`
|
|
304
304
|
}
|
|
305
305
|
function isObject(n, t = !0) {
|
|
@@ -783,12 +783,12 @@ function expand(n) {
|
|
|
783
783
|
const r = e[1].split(','),
|
|
784
784
|
i = n.slice(0, e.index),
|
|
785
785
|
o = n.slice(e.index + e[0].length)
|
|
786
|
-
let
|
|
787
|
-
for (const
|
|
788
|
-
const u = expand(i +
|
|
789
|
-
|
|
786
|
+
let s = []
|
|
787
|
+
for (const c of r) {
|
|
788
|
+
const u = expand(i + c + o)
|
|
789
|
+
s = s.concat(u)
|
|
790
790
|
}
|
|
791
|
-
return
|
|
791
|
+
return s
|
|
792
792
|
}
|
|
793
793
|
function shrinkTrim(n) {
|
|
794
794
|
return n
|
|
@@ -1031,22 +1031,22 @@ function baseToUint8Array(n, t) {
|
|
|
1031
1031
|
r = t.length
|
|
1032
1032
|
let i = 0,
|
|
1033
1033
|
o = 0
|
|
1034
|
-
const
|
|
1035
|
-
for (let
|
|
1036
|
-
const u = n[
|
|
1034
|
+
const s = []
|
|
1035
|
+
for (let c = 0; c < n.length; c++) {
|
|
1036
|
+
const u = n[c]
|
|
1037
1037
|
if (u === e) break
|
|
1038
1038
|
const f = t.indexOf(u)
|
|
1039
1039
|
if (f === -1) throw new Error(`Invalid character: ${u}`)
|
|
1040
|
-
;(o = (o << Math.log2(r)) | f), (i += Math.log2(r)), i >= 8 && ((i -= 8),
|
|
1040
|
+
;(o = (o << Math.log2(r)) | f), (i += Math.log2(r)), i >= 8 && ((i -= 8), s.push((o >> i) & 255))
|
|
1041
1041
|
}
|
|
1042
|
-
return new Uint8Array(
|
|
1042
|
+
return new Uint8Array(s)
|
|
1043
1043
|
}
|
|
1044
1044
|
function uint8ArrayToBase(n, t) {
|
|
1045
1045
|
const e = t.length
|
|
1046
1046
|
let r = 0,
|
|
1047
1047
|
i = 0,
|
|
1048
1048
|
o = ''
|
|
1049
|
-
for (let
|
|
1049
|
+
for (let s = 0; s < n.length; s++) for (i = (i << 8) | n[s], r += 8; r >= Math.log2(e); ) (r -= Math.log2(e)), (o += t[(i >> r) & (e - 1)])
|
|
1050
1050
|
return r > 0 && (o += t[(i << (Math.log2(e) - r)) & (e - 1)]), o.length % 4 !== 0 && (o += '='.repeat(4 - (o.length % 4))), o
|
|
1051
1051
|
}
|
|
1052
1052
|
function hexToUint8Array(n) {
|
|
@@ -1084,9 +1084,9 @@ function route(n, t) {
|
|
|
1084
1084
|
if (e.length !== r.length) return null
|
|
1085
1085
|
const i = {}
|
|
1086
1086
|
for (let o = 0; o < e.length; o++) {
|
|
1087
|
-
const
|
|
1088
|
-
if (
|
|
1089
|
-
else if (
|
|
1087
|
+
const s = e[o]
|
|
1088
|
+
if (s.startsWith(':')) i[s.slice(1)] = r[o]
|
|
1089
|
+
else if (s !== r[o]) return null
|
|
1090
1090
|
}
|
|
1091
1091
|
return i
|
|
1092
1092
|
}
|
|
@@ -1097,25 +1097,25 @@ function explodeReplace(n, t, e) {
|
|
|
1097
1097
|
}
|
|
1098
1098
|
function generateVariants(n, t, e, r = Math.random) {
|
|
1099
1099
|
const i = exports.Arrays.shuffle(
|
|
1100
|
-
t.map(
|
|
1100
|
+
t.map(s => ({
|
|
1101
1101
|
variants: exports.Arrays.shuffle(
|
|
1102
|
-
|
|
1102
|
+
s.variants.map(c => c),
|
|
1103
1103
|
r
|
|
1104
1104
|
),
|
|
1105
|
-
avoid:
|
|
1105
|
+
avoid: s.avoid
|
|
1106
1106
|
})),
|
|
1107
1107
|
r
|
|
1108
1108
|
),
|
|
1109
1109
|
o = []
|
|
1110
|
-
for (const
|
|
1111
|
-
const
|
|
1112
|
-
u =
|
|
1113
|
-
if (u && (pushAll(o, explodeReplace(n, u,
|
|
1110
|
+
for (const s of i) {
|
|
1111
|
+
const c = s.variants.filter(f => f !== s.avoid),
|
|
1112
|
+
u = c.find(f => n.includes(f))
|
|
1113
|
+
if (u && (pushAll(o, explodeReplace(n, u, c)), o.length >= e)) break
|
|
1114
1114
|
}
|
|
1115
1115
|
if (o.length < e)
|
|
1116
|
-
for (const
|
|
1117
|
-
const
|
|
1118
|
-
if (
|
|
1116
|
+
for (const s of i) {
|
|
1117
|
+
const c = s.variants.find(u => n.includes(u))
|
|
1118
|
+
if (c && (pushAll(o, explodeReplace(n, c, s.variants)), o.length >= e)) break
|
|
1119
1119
|
}
|
|
1120
1120
|
return o.slice(0, e)
|
|
1121
1121
|
}
|
|
@@ -1144,10 +1144,10 @@ function toLines(n, t, e = {}) {
|
|
|
1144
1144
|
const r = []
|
|
1145
1145
|
let i = '',
|
|
1146
1146
|
o = 0
|
|
1147
|
-
for (let
|
|
1148
|
-
const
|
|
1149
|
-
u = e[
|
|
1150
|
-
if (((i +=
|
|
1147
|
+
for (let s = 0; s < n.length; s++) {
|
|
1148
|
+
const c = n[s],
|
|
1149
|
+
u = e[c] || 1
|
|
1150
|
+
if (((i += c), (o += u), o > t)) {
|
|
1151
1151
|
const { line: f, rest: l } = breakLine(i)
|
|
1152
1152
|
r.push(f),
|
|
1153
1153
|
(i = l),
|
|
@@ -1223,8 +1223,8 @@ function resolveVariableWithDefaultSyntax(n, t, e, r = '$', i = ':') {
|
|
|
1223
1223
|
if (n[o + t.length + 1] === i)
|
|
1224
1224
|
if (n[o + t.length + 2] === i) n = n.replace(`${r}${t}${i}${i}`, e)
|
|
1225
1225
|
else {
|
|
1226
|
-
const
|
|
1227
|
-
n = n.replace(`${r}${t}${i}${
|
|
1226
|
+
const c = readNextWord(n, o + t.length + 2, ['_'])
|
|
1227
|
+
n = n.replace(`${r}${t}${i}${c}`, e)
|
|
1228
1228
|
}
|
|
1229
1229
|
else n = n.replace(`${r}${t}`, e)
|
|
1230
1230
|
o = n.indexOf(`${r}${t}`, o + e.length)
|
|
@@ -1238,8 +1238,8 @@ function resolveRemainingVariablesWithDefaults(n, t = '$', e = ':') {
|
|
|
1238
1238
|
if (n[r + i.length + 1] === e)
|
|
1239
1239
|
if (n[r + i.length + 2] === e) n = n.replace(`${t}${i}${e}${e}`, '')
|
|
1240
1240
|
else {
|
|
1241
|
-
const
|
|
1242
|
-
n = n.replace(`${t}${i}${e}${
|
|
1241
|
+
const s = readNextWord(n, r + i.length + 2)
|
|
1242
|
+
n = n.replace(`${t}${i}${e}${s}`, s)
|
|
1243
1243
|
}
|
|
1244
1244
|
r = n.indexOf(t, r + 1)
|
|
1245
1245
|
}
|
|
@@ -1251,9 +1251,9 @@ function resolveMarkdownLinks(n, t) {
|
|
|
1251
1251
|
const r = lastIndexOfBefore(n, '[', e),
|
|
1252
1252
|
i = n.indexOf(')', e)
|
|
1253
1253
|
if (r !== -1 && i !== -1) {
|
|
1254
|
-
const [o,
|
|
1255
|
-
|
|
1256
|
-
n = n.slice(0, r) +
|
|
1254
|
+
const [o, s] = n.slice(r + 1, i).split(']('),
|
|
1255
|
+
c = t(o, s)
|
|
1256
|
+
n = n.slice(0, r) + c + n.slice(i + 1)
|
|
1257
1257
|
}
|
|
1258
1258
|
e = n.indexOf('](', e + 1)
|
|
1259
1259
|
}
|
|
@@ -1282,15 +1282,15 @@ function selectMax(n, t) {
|
|
|
1282
1282
|
let e = null,
|
|
1283
1283
|
r = -1 / 0
|
|
1284
1284
|
for (const [i, o] of Object.entries(n)) {
|
|
1285
|
-
const
|
|
1286
|
-
|
|
1285
|
+
const s = t(o)
|
|
1286
|
+
s > r && ((r = s), (e = i))
|
|
1287
1287
|
}
|
|
1288
1288
|
return e ? [e, n[e]] : null
|
|
1289
1289
|
}
|
|
1290
1290
|
function reposition(n, t, e, r) {
|
|
1291
|
-
const i = n.find(
|
|
1292
|
-
o = n.find(
|
|
1293
|
-
i && o ? ((i[t] = e + r), (o[t] = e)) : i && (i[t] = e + r), n.sort((
|
|
1291
|
+
const i = n.find(s => s[t] === e),
|
|
1292
|
+
o = n.find(s => s[t] === e + r)
|
|
1293
|
+
i && o ? ((i[t] = e + r), (o[t] = e)) : i && (i[t] = e + r), n.sort((s, c) => asNumber(s[t]) - asNumber(c[t])), n.forEach((s, c) => (s[t] = c + 1))
|
|
1294
1294
|
}
|
|
1295
1295
|
function unwrapSingleKey(n) {
|
|
1296
1296
|
const t = Object.keys(n)
|
|
@@ -1317,8 +1317,8 @@ function parseCsv(n, t = ',', e = '"') {
|
|
|
1317
1317
|
const r = []
|
|
1318
1318
|
let i = '',
|
|
1319
1319
|
o = !1
|
|
1320
|
-
const
|
|
1321
|
-
for (const
|
|
1320
|
+
const s = n.split('')
|
|
1321
|
+
for (const c of s) c === t && !o ? (r.push(i), (i = '')) : c === e && ((!i && !o) || o) ? (o = !o) : (i += c)
|
|
1322
1322
|
return r.push(i), r
|
|
1323
1323
|
}
|
|
1324
1324
|
function humanizeProgress(n) {
|
|
@@ -1408,8 +1408,8 @@ function secondsToHumanTime(n, t = DefaultTimeDeltaLabels) {
|
|
|
1408
1408
|
function countCycles(n, t, e) {
|
|
1409
1409
|
const i = (e?.now ?? Date.now()) - n,
|
|
1410
1410
|
o = Math.floor(i / t),
|
|
1411
|
-
|
|
1412
|
-
return { cycles: o, remaining:
|
|
1411
|
+
s = t / (e?.precision ?? 1) - Math.ceil((i % t) / (e?.precision ?? 1))
|
|
1412
|
+
return { cycles: o, remaining: s }
|
|
1413
1413
|
}
|
|
1414
1414
|
const throttleTimers = {}
|
|
1415
1415
|
function throttle(n, t) {
|
|
@@ -1423,10 +1423,10 @@ function getProgress(n, t, e, r) {
|
|
|
1423
1423
|
r || (r = Date.now())
|
|
1424
1424
|
const i = t / e,
|
|
1425
1425
|
o = r - n,
|
|
1426
|
-
|
|
1427
|
-
|
|
1428
|
-
u =
|
|
1429
|
-
return { deltaMs: o, progress: i, baseTimeMs:
|
|
1426
|
+
s = o / t,
|
|
1427
|
+
c = s * e,
|
|
1428
|
+
u = c - o
|
|
1429
|
+
return { deltaMs: o, progress: i, baseTimeMs: s, totalTimeMs: c, remainingTimeMs: u }
|
|
1430
1430
|
}
|
|
1431
1431
|
const dayNumberIndex = { 0: 'sunday', 1: 'monday', 2: 'tuesday', 3: 'wednesday', 4: 'thursday', 5: 'friday', 6: 'saturday' }
|
|
1432
1432
|
function mapDayNumber(n) {
|
|
@@ -1484,10 +1484,10 @@ async function getCached(n, t, e, r) {
|
|
|
1484
1484
|
if (o && o.validUntil > i) return o.value
|
|
1485
1485
|
r?.onMiss?.()
|
|
1486
1486
|
try {
|
|
1487
|
-
const
|
|
1488
|
-
return tinyCache.set(n, { value:
|
|
1489
|
-
} catch (
|
|
1490
|
-
throw (r?.onFailure?.(
|
|
1487
|
+
const s = await e()
|
|
1488
|
+
return tinyCache.set(n, { value: s, validUntil: i + t }), s
|
|
1489
|
+
} catch (s) {
|
|
1490
|
+
throw (r?.onFailure?.(s), s)
|
|
1491
1491
|
}
|
|
1492
1492
|
}
|
|
1493
1493
|
function getCachedDeferred(n, t, e, r) {
|
|
@@ -1495,13 +1495,13 @@ function getCachedDeferred(n, t, e, r) {
|
|
|
1495
1495
|
o = tinyCache.get(n)
|
|
1496
1496
|
if (o && o.validUntil > i) return o.value
|
|
1497
1497
|
r?.onMiss?.()
|
|
1498
|
-
const
|
|
1498
|
+
const s = e()
|
|
1499
1499
|
return (
|
|
1500
|
-
tinyCache.set(n, { value:
|
|
1501
|
-
|
|
1502
|
-
tinyCache.delete(n), r?.onFailure?.(
|
|
1500
|
+
tinyCache.set(n, { value: s, validUntil: i + t }),
|
|
1501
|
+
s.catch(c => {
|
|
1502
|
+
tinyCache.delete(n), r?.onFailure?.(c)
|
|
1503
1503
|
}),
|
|
1504
|
-
|
|
1504
|
+
s
|
|
1505
1505
|
)
|
|
1506
1506
|
}
|
|
1507
1507
|
function deleteFromCache(n) {
|
|
@@ -1528,9 +1528,9 @@ function joinUrl(n, t = !1) {
|
|
|
1528
1528
|
}
|
|
1529
1529
|
function replaceBetweenStrings(n, t, e, r, i = !0) {
|
|
1530
1530
|
const o = n.indexOf(t),
|
|
1531
|
-
|
|
1532
|
-
if (o === -1 ||
|
|
1533
|
-
return i ? n.substring(0, o + t.length) + r + n.substring(
|
|
1531
|
+
s = n.indexOf(e, o + t.length)
|
|
1532
|
+
if (o === -1 || s === -1) throw Error('Start or end not found')
|
|
1533
|
+
return i ? n.substring(0, o + t.length) + r + n.substring(s) : n.substring(0, o) + r + n.substring(s + e.length)
|
|
1534
1534
|
}
|
|
1535
1535
|
function describeMarkdown(n) {
|
|
1536
1536
|
let t = 'p'
|
|
@@ -1596,12 +1596,12 @@ function createStatefulToggle(n) {
|
|
|
1596
1596
|
}
|
|
1597
1597
|
function organiseWithLimits(n, t, e, r, i) {
|
|
1598
1598
|
const o = {}
|
|
1599
|
-
for (const
|
|
1599
|
+
for (const s of Object.keys(t)) o[s] = []
|
|
1600
1600
|
;(o[r] = []), i && (n = n.sort(i))
|
|
1601
|
-
for (const
|
|
1602
|
-
const
|
|
1603
|
-
u = t[
|
|
1604
|
-
o[u].length >= t[u] ? o[r].push(
|
|
1601
|
+
for (const s of n) {
|
|
1602
|
+
const c = s[e],
|
|
1603
|
+
u = t[c] ? c : r
|
|
1604
|
+
o[u].length >= t[u] ? o[r].push(s) : o[u].push(s)
|
|
1605
1605
|
}
|
|
1606
1606
|
return o
|
|
1607
1607
|
}
|
|
@@ -1654,7 +1654,7 @@ function formatNumber(n, t) {
|
|
|
1654
1654
|
i = e ? longNumberUnits : shortNumberUnits,
|
|
1655
1655
|
o = t?.precision ?? 1
|
|
1656
1656
|
if (n < thresholds[0]) return `${n}${r}`
|
|
1657
|
-
for (let
|
|
1657
|
+
for (let s = 0; s < thresholds.length - 1; s++) if (n < thresholds[s + 1]) return `${(n / thresholds[s]).toFixed(o)}${e ? ' ' : ''}${i[s]}${r}`
|
|
1658
1658
|
return `${(n / thresholds[thresholds.length - 1]).toFixed(o)}${e ? ' ' : ''}${i[thresholds.length - 1]}${r}`
|
|
1659
1659
|
}
|
|
1660
1660
|
function makeNumber(n) {
|
|
@@ -1738,22 +1738,22 @@ function flip(n) {
|
|
|
1738
1738
|
}
|
|
1739
1739
|
function getAllPermutations(n) {
|
|
1740
1740
|
const t = Object.keys(n),
|
|
1741
|
-
e = t.map(
|
|
1742
|
-
r = e.reduce((
|
|
1741
|
+
e = t.map(c => n[c].length),
|
|
1742
|
+
r = e.reduce((c, u) => (c *= u))
|
|
1743
1743
|
let i = 1
|
|
1744
1744
|
const o = [1]
|
|
1745
|
-
for (let
|
|
1746
|
-
const
|
|
1747
|
-
for (let
|
|
1745
|
+
for (let c = 0; c < e.length - 1; c++) (i *= e[c]), o.push(i)
|
|
1746
|
+
const s = []
|
|
1747
|
+
for (let c = 0; c < r; c++) {
|
|
1748
1748
|
const u = {}
|
|
1749
1749
|
for (let f = 0; f < t.length; f++) {
|
|
1750
1750
|
const l = n[t[f]],
|
|
1751
|
-
a = Math.floor(
|
|
1751
|
+
a = Math.floor(c / o[f]) % l.length
|
|
1752
1752
|
u[t[f]] = l[a]
|
|
1753
1753
|
}
|
|
1754
|
-
|
|
1754
|
+
s.push(u)
|
|
1755
1755
|
}
|
|
1756
|
-
return
|
|
1756
|
+
return s
|
|
1757
1757
|
}
|
|
1758
1758
|
function countTruthyValues(n) {
|
|
1759
1759
|
return Object.values(n).filter(t => t).length
|
|
@@ -1763,9 +1763,9 @@ function getFlatNotation(n, t, e) {
|
|
|
1763
1763
|
}
|
|
1764
1764
|
function flattenInner(n, t, e, r, i) {
|
|
1765
1765
|
if (!isObject(t)) return t
|
|
1766
|
-
for (const [o,
|
|
1767
|
-
const
|
|
1768
|
-
Array.isArray(
|
|
1766
|
+
for (const [o, s] of Object.entries(t)) {
|
|
1767
|
+
const c = getFlatNotation(e, o, r)
|
|
1768
|
+
Array.isArray(s) ? (i ? flattenInner(n, s, c, !0, i) : (n[c] = s.map(u => flattenInner(Array.isArray(u) ? [] : {}, u, '', !1, i)))) : isObject(s) ? flattenInner(n, s, c, !1, i) : (n[c] = s)
|
|
1769
1769
|
}
|
|
1770
1770
|
return n
|
|
1771
1771
|
}
|
|
@@ -1830,10 +1830,10 @@ function makeUnique(n, t) {
|
|
|
1830
1830
|
}
|
|
1831
1831
|
function countUnique(n, t, e, r, i) {
|
|
1832
1832
|
const o = t ? n.map(t) : n,
|
|
1833
|
-
|
|
1834
|
-
for (const u of o)
|
|
1835
|
-
const
|
|
1836
|
-
return e ? Object.keys(
|
|
1833
|
+
s = {}
|
|
1834
|
+
for (const u of o) s[u] = (s[u] || 0) + 1
|
|
1835
|
+
const c = r ? sortObjectValues(s, i ? (u, f) => u[1] - f[1] : (u, f) => f[1] - u[1]) : s
|
|
1836
|
+
return e ? Object.keys(c) : c
|
|
1837
1837
|
}
|
|
1838
1838
|
function sortObjectValues(n, t) {
|
|
1839
1839
|
return Object.fromEntries(Object.entries(n).sort(t))
|
|
@@ -1844,7 +1844,7 @@ function transformToArray(n) {
|
|
|
1844
1844
|
r = n[e[0]].length
|
|
1845
1845
|
for (let i = 0; i < r; i++) {
|
|
1846
1846
|
const o = {}
|
|
1847
|
-
for (const
|
|
1847
|
+
for (const s of e) o[s] = n[s][i]
|
|
1848
1848
|
t.push(o)
|
|
1849
1849
|
}
|
|
1850
1850
|
return t
|
|
@@ -1997,7 +1997,7 @@ class Node {
|
|
|
1997
1997
|
}
|
|
1998
1998
|
function createHierarchy(n, t, e, r, i = !1) {
|
|
1999
1999
|
const o = new Map(),
|
|
2000
|
-
|
|
2000
|
+
s = []
|
|
2001
2001
|
n.forEach(u => {
|
|
2002
2002
|
const f = new Node(u)
|
|
2003
2003
|
o.set(u[t], f)
|
|
@@ -2009,17 +2009,17 @@ function createHierarchy(n, t, e, r, i = !1) {
|
|
|
2009
2009
|
if (l) {
|
|
2010
2010
|
const a = o.get(l)
|
|
2011
2011
|
a && a.children.push(f)
|
|
2012
|
-
} else
|
|
2012
|
+
} else s.push(f)
|
|
2013
2013
|
})
|
|
2014
|
-
const
|
|
2014
|
+
const c = u => {
|
|
2015
2015
|
u.children.sort((f, l) => {
|
|
2016
2016
|
const a = f.value[r],
|
|
2017
2017
|
h = l.value[r]
|
|
2018
2018
|
return i ? h - a : a - h
|
|
2019
2019
|
}),
|
|
2020
|
-
u.children.forEach(
|
|
2020
|
+
u.children.forEach(c)
|
|
2021
2021
|
}
|
|
2022
|
-
return
|
|
2022
|
+
return s.forEach(c), s
|
|
2023
2023
|
}
|
|
2024
2024
|
function log2Reduce(n, t) {
|
|
2025
2025
|
if (Math.log2(n.length) % 1 !== 0) throw new Error('Array length must be a power of 2')
|
|
@@ -2121,8 +2121,8 @@ function keccakPermutate(n) {
|
|
|
2121
2121
|
r = n[1] ^ n[11] ^ n[21] ^ n[31] ^ n[41],
|
|
2122
2122
|
i = n[2] ^ n[12] ^ n[22] ^ n[32] ^ n[42],
|
|
2123
2123
|
o = n[3] ^ n[13] ^ n[23] ^ n[33] ^ n[43],
|
|
2124
|
-
|
|
2125
|
-
|
|
2124
|
+
s = n[4] ^ n[14] ^ n[24] ^ n[34] ^ n[44],
|
|
2125
|
+
c = n[5] ^ n[15] ^ n[25] ^ n[35] ^ n[45],
|
|
2126
2126
|
u = n[6] ^ n[16] ^ n[26] ^ n[36] ^ n[46],
|
|
2127
2127
|
f = n[7] ^ n[17] ^ n[27] ^ n[37] ^ n[47],
|
|
2128
2128
|
l = n[8] ^ n[18] ^ n[28] ^ n[38] ^ n[48],
|
|
@@ -2131,8 +2131,8 @@ function keccakPermutate(n) {
|
|
|
2131
2131
|
p = (o << 1) | (i >>> 31),
|
|
2132
2132
|
d = l ^ h,
|
|
2133
2133
|
m = a ^ p,
|
|
2134
|
-
w = (
|
|
2135
|
-
x = (
|
|
2134
|
+
w = (s << 1) | (c >>> 31),
|
|
2135
|
+
x = (c << 1) | (s >>> 31),
|
|
2136
2136
|
g = e ^ w,
|
|
2137
2137
|
y = r ^ x,
|
|
2138
2138
|
En = (u << 1) | (f >>> 31),
|
|
@@ -2141,8 +2141,8 @@ function keccakPermutate(n) {
|
|
|
2141
2141
|
A = o ^ Mn,
|
|
2142
2142
|
Sn = (l << 1) | (a >>> 31),
|
|
2143
2143
|
kn = (a << 1) | (l >>> 31),
|
|
2144
|
-
$ =
|
|
2145
|
-
E =
|
|
2144
|
+
$ = s ^ Sn,
|
|
2145
|
+
E = c ^ kn,
|
|
2146
2146
|
On = (e << 1) | (r >>> 31),
|
|
2147
2147
|
Tn = (r << 1) | (e >>> 31),
|
|
2148
2148
|
M = u ^ On,
|
|
@@ -2155,13 +2155,13 @@ function keccakPermutate(n) {
|
|
|
2155
2155
|
R = (n[5] << 30) | (n[4] >>> 2),
|
|
2156
2156
|
I = (n[4] << 30) | (n[5] >>> 2),
|
|
2157
2157
|
D = (n[6] << 28) | (n[7] >>> 4),
|
|
2158
|
-
|
|
2159
|
-
|
|
2158
|
+
P = (n[7] << 28) | (n[6] >>> 4),
|
|
2159
|
+
B = (n[8] << 27) | (n[9] >>> 5),
|
|
2160
2160
|
U = (n[9] << 27) | (n[8] >>> 5),
|
|
2161
|
-
|
|
2162
|
-
|
|
2163
|
-
|
|
2164
|
-
|
|
2161
|
+
j = (n[11] << 4) | (n[10] >>> 28),
|
|
2162
|
+
L = (n[10] << 4) | (n[11] >>> 28),
|
|
2163
|
+
N = (n[13] << 12) | (n[12] >>> 20),
|
|
2164
|
+
v = (n[12] << 12) | (n[13] >>> 20),
|
|
2165
2165
|
F = (n[14] << 6) | (n[15] >>> 26),
|
|
2166
2166
|
z = (n[15] << 6) | (n[14] >>> 26),
|
|
2167
2167
|
W = (n[17] << 23) | (n[16] >>> 9),
|
|
@@ -2181,8 +2181,8 @@ function keccakPermutate(n) {
|
|
|
2181
2181
|
en = (n[31] << 9) | (n[30] >>> 23),
|
|
2182
2182
|
rn = (n[30] << 9) | (n[31] >>> 23),
|
|
2183
2183
|
on = (n[33] << 13) | (n[32] >>> 19),
|
|
2184
|
-
|
|
2185
|
-
|
|
2184
|
+
sn = (n[32] << 13) | (n[33] >>> 19),
|
|
2185
|
+
cn = (n[34] << 15) | (n[35] >>> 17),
|
|
2186
2186
|
un = (n[35] << 15) | (n[34] >>> 17),
|
|
2187
2187
|
fn = (n[36] << 21) | (n[37] >>> 11),
|
|
2188
2188
|
ln = (n[37] << 21) | (n[36] >>> 11),
|
|
@@ -2198,7 +2198,7 @@ function keccakPermutate(n) {
|
|
|
2198
2198
|
bn = (n[46] << 24) | (n[47] >>> 8),
|
|
2199
2199
|
An = (n[48] << 14) | (n[49] >>> 18),
|
|
2200
2200
|
$n = (n[49] << 14) | (n[48] >>> 18)
|
|
2201
|
-
;(n[0] = k ^ (~
|
|
2201
|
+
;(n[0] = k ^ (~N & G)), (n[1] = O ^ (~v & _)), (n[2] = N ^ (~G & fn)), (n[3] = v ^ (~_ & ln)), (n[4] = G ^ (~fn & An)), (n[5] = _ ^ (~ln & $n)), (n[6] = fn ^ (~An & k)), (n[7] = ln ^ (~$n & O)), (n[8] = An ^ (~k & N)), (n[9] = $n ^ (~O & v)), (n[10] = D ^ (~q & J)), (n[11] = P ^ (~V & K)), (n[12] = q ^ (~J & on)), (n[13] = V ^ (~K & sn)), (n[14] = J ^ (~on & wn)), (n[15] = K ^ (~sn & xn)), (n[16] = on ^ (~wn & D)), (n[17] = sn ^ (~xn & P)), (n[18] = wn ^ (~D & q)), (n[19] = xn ^ (~P & V)), (n[20] = T ^ (~F & Y)), (n[21] = C ^ (~z & X)), (n[22] = F ^ (~Y & an)), (n[23] = z ^ (~X & hn)), (n[24] = Y ^ (~an & dn)), (n[25] = X ^ (~hn & pn)), (n[26] = an ^ (~dn & T)), (n[27] = hn ^ (~pn & C)), (n[28] = dn ^ (~T & F)), (n[29] = pn ^ (~C & z)), (n[30] = B ^ (~j & Z)), (n[31] = U ^ (~L & Q)), (n[32] = j ^ (~Z & cn)), (n[33] = L ^ (~Q & un)), (n[34] = Z ^ (~cn & yn)), (n[35] = Q ^ (~un & bn)), (n[36] = cn ^ (~yn & B)), (n[37] = un ^ (~bn & U)), (n[38] = yn ^ (~B & j)), (n[39] = bn ^ (~U & L)), (n[40] = R ^ (~W & nn)), (n[41] = I ^ (~H & tn)), (n[42] = W ^ (~nn & en)), (n[43] = H ^ (~tn & rn)), (n[44] = nn ^ (~en & mn)), (n[45] = tn ^ (~rn & gn)), (n[46] = en ^ (~mn & R)), (n[47] = rn ^ (~gn & I)), (n[48] = mn ^ (~R & W)), (n[49] = gn ^ (~I & H)), (n[0] ^= IOTA_CONSTANTS[t * 2]), (n[1] ^= IOTA_CONSTANTS[t * 2 + 1])
|
|
2202
2202
|
}
|
|
2203
2203
|
}
|
|
2204
2204
|
function bytesToNumbers(n) {
|
|
@@ -2247,10 +2247,10 @@ function bmtRoot(n) {
|
|
|
2247
2247
|
let i = t.length >>> 5
|
|
2248
2248
|
for (; i > 1; ) {
|
|
2249
2249
|
const o = i >>> 1
|
|
2250
|
-
for (let
|
|
2251
|
-
r.set(t.subarray(
|
|
2252
|
-
for (let
|
|
2253
|
-
keccakPermutate(e), squeezeInto(e, t,
|
|
2250
|
+
for (let s = 0; s < o; s++) {
|
|
2251
|
+
r.set(t.subarray(s << 6, (s + 1) << 6)), e.fill(0)
|
|
2252
|
+
for (let c = 0, u = 0; c < 34; c += 2, u += 8) (e[c] ^= r[u + 4] | (r[u + 5] << 8) | (r[u + 6] << 16) | (r[u + 7] << 24)), (e[c + 1] ^= r[u] | (r[u + 1] << 8) | (r[u + 2] << 16) | (r[u + 3] << 24))
|
|
2253
|
+
keccakPermutate(e), squeezeInto(e, t, s << 5)
|
|
2254
2254
|
}
|
|
2255
2255
|
i = o
|
|
2256
2256
|
}
|
|
@@ -2261,12 +2261,12 @@ function encryptSegments(n, t, e) {
|
|
|
2261
2261
|
const r = new Uint8Array(e.length),
|
|
2262
2262
|
i = new Uint8Array(36)
|
|
2263
2263
|
i.set(n)
|
|
2264
|
-
for (let o = 0,
|
|
2265
|
-
const
|
|
2266
|
-
;(i[32] =
|
|
2264
|
+
for (let o = 0, s = 0; s < e.length; o++, s += 32) {
|
|
2265
|
+
const c = (t + o) >>> 0
|
|
2266
|
+
;(i[32] = c & 255), (i[33] = (c >>> 8) & 255), (i[34] = (c >>> 16) & 255), (i[35] = (c >>> 24) & 255)
|
|
2267
2267
|
const u = keccak256(keccak256(i)),
|
|
2268
|
-
f = Math.min(
|
|
2269
|
-
for (let l =
|
|
2268
|
+
f = Math.min(s + 32, e.length)
|
|
2269
|
+
for (let l = s; l < f; l++) r[l] = e[l] ^ u[l - s]
|
|
2270
2270
|
}
|
|
2271
2271
|
return r
|
|
2272
2272
|
}
|
|
@@ -2362,8 +2362,8 @@ function modInverse(n, t) {
|
|
|
2362
2362
|
let [e, r] = [0n, 1n],
|
|
2363
2363
|
[i, o] = [t, n]
|
|
2364
2364
|
for (; o !== 0n; ) {
|
|
2365
|
-
const
|
|
2366
|
-
;([e, r] = [r, e -
|
|
2365
|
+
const s = i / o
|
|
2366
|
+
;([e, r] = [r, e - s * r]), ([i, o] = [o, i - s * o])
|
|
2367
2367
|
}
|
|
2368
2368
|
if (i > 1n) throw new Error('a is not invertible')
|
|
2369
2369
|
return e < 0n && (e += t), e
|
|
@@ -2394,9 +2394,9 @@ function ellipticAdd(n, t, e, r, i) {
|
|
|
2394
2394
|
if (n === e && t === mod(-r, i)) return [0n, 0n]
|
|
2395
2395
|
if (n === e && t === r) return ellipticDouble(n, t, i)
|
|
2396
2396
|
const o = mod((r - t) * modInverse(e - n, i), i),
|
|
2397
|
-
|
|
2398
|
-
|
|
2399
|
-
return [
|
|
2397
|
+
s = mod(o * o - n - e, i),
|
|
2398
|
+
c = mod(o * (n - s) - t, i)
|
|
2399
|
+
return [s, c]
|
|
2400
2400
|
}
|
|
2401
2401
|
function privateKeyToPublicKey(n) {
|
|
2402
2402
|
if (n <= 0n || n >= SECP256K1_N) throw new Error('Invalid private key')
|
|
@@ -2432,19 +2432,19 @@ function jacobianDouble(n, t, e) {
|
|
|
2432
2432
|
const r = SECP256K1_P,
|
|
2433
2433
|
i = (t * t) % r,
|
|
2434
2434
|
o = (4n * n * i) % r,
|
|
2435
|
-
|
|
2436
|
-
|
|
2437
|
-
u = mod(
|
|
2435
|
+
s = (3n * n * n) % r,
|
|
2436
|
+
c = mod(s * s - 2n * o, r),
|
|
2437
|
+
u = mod(s * (o - c) - 8n * i * i, r),
|
|
2438
2438
|
f = (2n * t * e) % r
|
|
2439
|
-
return [
|
|
2439
|
+
return [c, u, f]
|
|
2440
2440
|
}
|
|
2441
2441
|
function jacobianAddMixed(n, t, e, r, i) {
|
|
2442
2442
|
if (e === 0n) return [r, i, 1n]
|
|
2443
2443
|
const o = SECP256K1_P,
|
|
2444
|
-
|
|
2445
|
-
|
|
2446
|
-
u = (((i *
|
|
2447
|
-
f = mod(
|
|
2444
|
+
s = (e * e) % o,
|
|
2445
|
+
c = (r * s) % o,
|
|
2446
|
+
u = (((i * s) % o) * e) % o,
|
|
2447
|
+
f = mod(c - n, o),
|
|
2448
2448
|
l = mod(u - t, o)
|
|
2449
2449
|
if (f === 0n) return l === 0n ? jacobianDouble(n, t, e) : [0n, 0n, 0n]
|
|
2450
2450
|
const a = (f * f) % o,
|
|
@@ -2468,10 +2468,10 @@ function glvDecompose(n) {
|
|
|
2468
2468
|
r = 0xe4437ed6010e88286f547fa90abfe4c3n,
|
|
2469
2469
|
i = 0x114ca50f7a8e2f3f657c1108d9d44cfd8n,
|
|
2470
2470
|
o = 0x3086d221a7d46bcde86c90e49284eb15n,
|
|
2471
|
-
|
|
2472
|
-
|
|
2473
|
-
let u = mod(n -
|
|
2474
|
-
f = mod(
|
|
2471
|
+
s = (o * n + t / 2n) / t,
|
|
2472
|
+
c = (r * n + t / 2n) / t
|
|
2473
|
+
let u = mod(n - s * e - c * i, t),
|
|
2474
|
+
f = mod(s * r - c * o, t)
|
|
2475
2475
|
const l = u > 1n << 128n,
|
|
2476
2476
|
a = f > 1n << 128n
|
|
2477
2477
|
return l && (u = t - u), a && (f = t - f), [u, l, f, a]
|
|
@@ -2497,44 +2497,44 @@ function batchToAffine(n) {
|
|
|
2497
2497
|
if (e === 1) return [jacobianToAffine(n[0][0], n[0][1], n[0][2])]
|
|
2498
2498
|
const r = new Array(e)
|
|
2499
2499
|
r[0] = n[0][2]
|
|
2500
|
-
for (let
|
|
2500
|
+
for (let c = 1; c < e; c++) r[c] = (r[c - 1] * n[c][2]) % t
|
|
2501
2501
|
let i = modInverse(r[e - 1], t)
|
|
2502
2502
|
const o = new Array(e)
|
|
2503
|
-
for (let
|
|
2504
|
-
const u = (i * r[
|
|
2503
|
+
for (let c = e - 1; c > 0; c--) {
|
|
2504
|
+
const u = (i * r[c - 1]) % t,
|
|
2505
2505
|
f = (u * u) % t
|
|
2506
|
-
;(o[
|
|
2506
|
+
;(o[c] = [(n[c][0] * f) % t, (((n[c][1] * f) % t) * u) % t]), (i = (i * n[c][2]) % t)
|
|
2507
2507
|
}
|
|
2508
|
-
const
|
|
2509
|
-
return (o[0] = [(n[0][0] *
|
|
2508
|
+
const s = (i * i) % t
|
|
2509
|
+
return (o[0] = [(n[0][0] * s) % t, (((n[0][1] * s) % t) * i) % t]), o
|
|
2510
2510
|
}
|
|
2511
2511
|
function precomputeOddMultiples(n, t) {
|
|
2512
2512
|
const e = 1 << (WNAF_W - 2),
|
|
2513
2513
|
[r, i] = jacobianToAffine(...jacobianDouble(n, t, 1n)),
|
|
2514
2514
|
o = [[n, t, 1n]]
|
|
2515
|
-
for (let
|
|
2516
|
-
const [
|
|
2517
|
-
o.push(jacobianAddMixed(
|
|
2515
|
+
for (let s = 1; s < e; s++) {
|
|
2516
|
+
const [c, u, f] = o[s - 1]
|
|
2517
|
+
o.push(jacobianAddMixed(c, u, f, r, i))
|
|
2518
2518
|
}
|
|
2519
2519
|
return batchToAffine(o)
|
|
2520
2520
|
}
|
|
2521
2521
|
function shamirWNAF(n, t, e, r, i, o) {
|
|
2522
|
-
const
|
|
2523
|
-
|
|
2522
|
+
const s = toWNAF(n),
|
|
2523
|
+
c = toWNAF(r),
|
|
2524
2524
|
u = precomputeOddMultiples(t, e),
|
|
2525
2525
|
f = precomputeOddMultiples(i, o),
|
|
2526
|
-
l = Math.max(
|
|
2526
|
+
l = Math.max(s.length, c.length)
|
|
2527
2527
|
let a = 0n,
|
|
2528
2528
|
h = 0n,
|
|
2529
2529
|
p = 0n
|
|
2530
2530
|
for (let d = l - 1; d >= 0; d--) {
|
|
2531
2531
|
;[a, h, p] = jacobianDouble(a, h, p)
|
|
2532
|
-
const m = d <
|
|
2532
|
+
const m = d < s.length ? s[d] : 0
|
|
2533
2533
|
if (m !== 0) {
|
|
2534
2534
|
const [x, g] = u[(Math.abs(m) - 1) >> 1]
|
|
2535
2535
|
;[a, h, p] = jacobianAddMixed(a, h, p, x, m > 0 ? g : SECP256K1_P - g)
|
|
2536
2536
|
}
|
|
2537
|
-
const w = d <
|
|
2537
|
+
const w = d < c.length ? c[d] : 0
|
|
2538
2538
|
if (w !== 0) {
|
|
2539
2539
|
const [x, g] = f[(Math.abs(w) - 1) >> 1]
|
|
2540
2540
|
;[a, h, p] = jacobianAddMixed(a, h, p, x, w > 0 ? g : SECP256K1_P - g)
|
|
@@ -2543,11 +2543,11 @@ function shamirWNAF(n, t, e, r, i, o) {
|
|
|
2543
2543
|
return jacobianToAffine(a, h, p)
|
|
2544
2544
|
}
|
|
2545
2545
|
function doubleAndAdd(n, t, e) {
|
|
2546
|
-
const [r, i, o,
|
|
2547
|
-
|
|
2546
|
+
const [r, i, o, s] = glvDecompose(e),
|
|
2547
|
+
c = i ? SECP256K1_P - t : t,
|
|
2548
2548
|
u = (SECP256K1_BETA * n) % SECP256K1_P,
|
|
2549
|
-
f =
|
|
2550
|
-
return shamirWNAF(r, n,
|
|
2549
|
+
f = s ? SECP256K1_P - t : t
|
|
2550
|
+
return shamirWNAF(r, n, c, o, u, f)
|
|
2551
2551
|
}
|
|
2552
2552
|
function signMessage(n, t, e) {
|
|
2553
2553
|
return signHash(uint256ToNumber(keccak256(n), 'BE'), t, e)
|
|
@@ -2558,29 +2558,29 @@ function signHash(n, t, e) {
|
|
|
2558
2558
|
const r = mod(n, SECP256K1_N),
|
|
2559
2559
|
i = doubleAndAdd(SECP256K1_X, SECP256K1_Y, e),
|
|
2560
2560
|
o = mod(i[0], SECP256K1_N)
|
|
2561
|
-
let
|
|
2562
|
-
if (o === 0n ||
|
|
2563
|
-
let
|
|
2564
|
-
return
|
|
2561
|
+
let s = mod((r + mod(o, SECP256K1_N) * t) * modInverse(e, SECP256K1_N), SECP256K1_N)
|
|
2562
|
+
if (o === 0n || s === 0n) throw new Error('Invalid r or s value')
|
|
2563
|
+
let c = i[1] % 2n === 0n ? 27n : 28n
|
|
2564
|
+
return s > SECP256K1_N / 2n && ((s = SECP256K1_N - s), (c = c === 27n ? 28n : 27n)), [o, s, c]
|
|
2565
2565
|
}
|
|
2566
2566
|
function recoverPublicKey(n, t, e, r) {
|
|
2567
2567
|
const i = modSqrt(mod(t ** 3n + 7n, SECP256K1_P), SECP256K1_P)
|
|
2568
2568
|
if (!i) throw new Error('Invalid r: does not correspond to a valid curve point')
|
|
2569
2569
|
const o = r === 27n ? 0n : 1n,
|
|
2570
|
-
|
|
2571
|
-
|
|
2572
|
-
u = doubleAndAdd(t,
|
|
2573
|
-
f = doubleAndAdd(SECP256K1_X, SECP256K1_Y,
|
|
2570
|
+
s = i % 2n === o ? i : SECP256K1_P - i,
|
|
2571
|
+
c = mod(uint256ToNumber(keccak256(n), 'BE'), SECP256K1_N),
|
|
2572
|
+
u = doubleAndAdd(t, s, e),
|
|
2573
|
+
f = doubleAndAdd(SECP256K1_X, SECP256K1_Y, c),
|
|
2574
2574
|
l = ellipticAdd(u[0], u[1], f[0], mod(-f[1], SECP256K1_P), SECP256K1_P)
|
|
2575
2575
|
return doubleAndAdd(l[0], l[1], modInverse(t, SECP256K1_N))
|
|
2576
2576
|
}
|
|
2577
2577
|
function verifySignature(n, t, e, r) {
|
|
2578
2578
|
const i = mod(uint256ToNumber(keccak256(n), 'BE'), SECP256K1_N),
|
|
2579
2579
|
o = modInverse(r, SECP256K1_N),
|
|
2580
|
-
|
|
2581
|
-
|
|
2582
|
-
u = doubleAndAdd(SECP256K1_X, SECP256K1_Y,
|
|
2583
|
-
f = doubleAndAdd(t[0], t[1],
|
|
2580
|
+
s = mod(i * o, SECP256K1_N),
|
|
2581
|
+
c = mod(e * o, SECP256K1_N),
|
|
2582
|
+
u = doubleAndAdd(SECP256K1_X, SECP256K1_Y, s),
|
|
2583
|
+
f = doubleAndAdd(t[0], t[1], c),
|
|
2584
2584
|
l = ellipticAdd(u[0], u[1], f[0], f[1], SECP256K1_P)
|
|
2585
2585
|
return e === mod(l[0], SECP256K1_N)
|
|
2586
2586
|
}
|
|
@@ -2639,8 +2639,8 @@ class Chunk {
|
|
|
2639
2639
|
}
|
|
2640
2640
|
;(exports.Chunk = Chunk), (Chunk.hashFunction = keccak256)
|
|
2641
2641
|
class ChunkSplitter {
|
|
2642
|
-
constructor(t, e, r = !1) {
|
|
2643
|
-
;(this.counters = [1]), (this.pending = [[]]), (this.encrypted = r), (this.refSize = r ? 64 : 32), (this.maxShards = e ?? 4096 / this.refSize), (this.chunks = [new Chunk()]), (this.onBatch = t)
|
|
2642
|
+
constructor(t, e, r = !1, i) {
|
|
2643
|
+
;(this.counters = [1]), (this.pending = [[]]), (this.hasParity = [!1]), (this.encrypted = r), (this.refSize = r ? 64 : 32), (this.maxShards = e ?? 4096 / this.refSize), (this.chunks = [new Chunk()]), (this.onBatch = t), (this.onIntermediateChunk = i)
|
|
2644
2644
|
}
|
|
2645
2645
|
static async root(t) {
|
|
2646
2646
|
const e = new _a(_a.NOOP)
|
|
@@ -2659,7 +2659,7 @@ class ChunkSplitter {
|
|
|
2659
2659
|
}
|
|
2660
2660
|
}
|
|
2661
2661
|
async elevate(t) {
|
|
2662
|
-
if (((this.counters[t] = ++this.counters[t] % (4096 / this.refSize)), this.pending[t] || (this.pending[t] = []), this.encrypted)) {
|
|
2662
|
+
if (((this.counters[t] = ++this.counters[t] % (4096 / this.refSize)), this.pending[t] || (this.pending[t] = []), t >= 1 && this.onIntermediateChunk && (this.onIntermediateChunk(this.chunks[t], this.hasParity[t] ?? !1), (this.hasParity[t] = !1)), this.encrypted)) {
|
|
2663
2663
|
const { address: e, key: r } = this.chunks[t].encryptedHash(),
|
|
2664
2664
|
i = new Uint8Array(64)
|
|
2665
2665
|
i.set(e), i.set(r, 32), this.pending[t].push({ entry: { chunk: this.chunks[t], key: r }, ref: i, span: this.chunks[t].span })
|
|
@@ -2667,27 +2667,21 @@ class ChunkSplitter {
|
|
|
2667
2667
|
;(this.chunks[t] = new Chunk()), this.pending[t].length >= this.maxShards && (await this.flushBatch(t))
|
|
2668
2668
|
}
|
|
2669
2669
|
async flushBatch(t) {
|
|
2670
|
-
this.chunks[t + 1] || (this.chunks.push(new Chunk()), this.counters.push(1), this.pending.push([]))
|
|
2670
|
+
this.chunks[t + 1] || (this.chunks.push(new Chunk()), this.counters.push(1), this.pending.push([]), this.hasParity.push(!1))
|
|
2671
2671
|
const e = this.pending[t]
|
|
2672
2672
|
this.pending[t] = []
|
|
2673
2673
|
const r = await this.onBatch(e.map(i => i.entry))
|
|
2674
2674
|
for (const { ref: i, span: o } of e) await this.append(i, t + 1, o)
|
|
2675
|
+
r.length > 0 && (this.hasParity[t + 1] = !0)
|
|
2675
2676
|
for (const { chunk: i, key: o } of r)
|
|
2676
2677
|
if (this.encrypted) {
|
|
2677
|
-
const { address:
|
|
2678
|
+
const { address: s, key: c } = i.encryptedHash(o),
|
|
2678
2679
|
u = new Uint8Array(64)
|
|
2679
|
-
u.set(
|
|
2680
|
+
u.set(s), u.set(c, 32), await this.append(u, t + 1, i.span)
|
|
2680
2681
|
} else await this.append(i.hash(), t + 1, i.span)
|
|
2681
2682
|
}
|
|
2682
2683
|
async finalize(t = 0) {
|
|
2683
|
-
|
|
2684
|
-
if (this.encrypted) {
|
|
2685
|
-
const { key: e } = this.chunks[t].encryptedHash()
|
|
2686
|
-
await this.onBatch([{ chunk: this.chunks[t], key: e }])
|
|
2687
|
-
} else await this.onBatch([{ chunk: this.chunks[t] }])
|
|
2688
|
-
return this.chunks[t]
|
|
2689
|
-
}
|
|
2690
|
-
return this.counters[t] === 1 ? (await this.elevate(t + 1), await this.flushBatch(t + 1), (this.chunks[t + 1] = this.chunks[t]), this.finalize(t + 1)) : (await this.elevate(t), await this.flushBatch(t), this.finalize(t + 1))
|
|
2684
|
+
return this.pending[t]?.length && (await this.flushBatch(t)), this.chunks[t + 1] ? (this.counters[t] === 1 ? (await this.elevate(t + 1), await this.flushBatch(t + 1), (this.chunks[t + 1] = this.chunks[t]), this.finalize(t + 1)) : (await this.elevate(t), await this.flushBatch(t), this.finalize(t + 1))) : (t >= 1 && this.onIntermediateChunk && this.onIntermediateChunk(this.chunks[t], this.hasParity[t] ?? !1), this.chunks[t])
|
|
2691
2685
|
}
|
|
2692
2686
|
}
|
|
2693
2687
|
;(exports.ChunkSplitter = ChunkSplitter), (_a = ChunkSplitter), (ChunkSplitter.NOOP = async n => [])
|
|
@@ -2726,11 +2720,11 @@ class ChunkJoiner {
|
|
|
2726
2720
|
let i, o
|
|
2727
2721
|
if ((this.encrypted && e ? ({ span: i, data: o } = decryptChunk(r, e)) : ((i = uint64ToNumber(r.subarray(0, 8), 'LE')), (o = r.subarray(8, 4104))), i <= 4096n)) await this.onData(o.subarray(0, Number(i)))
|
|
2728
2722
|
else
|
|
2729
|
-
for (let
|
|
2730
|
-
const
|
|
2731
|
-
u =
|
|
2723
|
+
for (let s = 0; s < 4096 / this.refSize; s++) {
|
|
2724
|
+
const c = o.subarray(s * this.refSize, (s + 1) * this.refSize),
|
|
2725
|
+
u = c.subarray(0, 32)
|
|
2732
2726
|
if (isAllZero(u)) break
|
|
2733
|
-
await this.join(u, this.encrypted ?
|
|
2727
|
+
await this.join(u, this.encrypted ? c.subarray(32, 64) : void 0)
|
|
2734
2728
|
}
|
|
2735
2729
|
}
|
|
2736
2730
|
}
|
|
@@ -2809,12 +2803,12 @@ function tickPlaybook(n) {
|
|
|
2809
2803
|
return t.ttlMax ? --t.ttl <= 0 && n.shift() : (t.ttlMax = t.ttl), { progress: (t.ttlMax - t.ttl) / t.ttlMax, data: t.data }
|
|
2810
2804
|
}
|
|
2811
2805
|
function getArgument(n, t, e, r) {
|
|
2812
|
-
const i = n.findIndex(
|
|
2806
|
+
const i = n.findIndex(c => c === `--${t}` || c.startsWith(`--${t}=`)),
|
|
2813
2807
|
o = n[i]
|
|
2814
2808
|
if (!o) return (e || {})[r || t || ''] || null
|
|
2815
2809
|
if (o.includes('=')) return o.split('=')[1]
|
|
2816
|
-
const
|
|
2817
|
-
return
|
|
2810
|
+
const s = n[i + 1]
|
|
2811
|
+
return s && !s.startsWith('-') ? s : (e || {})[r || t || ''] || null
|
|
2818
2812
|
}
|
|
2819
2813
|
function getNumberArgument(n, t, e, r) {
|
|
2820
2814
|
const i = getArgument(n, t, e, r)
|
|
@@ -2830,10 +2824,10 @@ function getBooleanArgument(n, t, e, r) {
|
|
|
2830
2824
|
o = getArgument(n, t, e, r)
|
|
2831
2825
|
if (!o && i) return !0
|
|
2832
2826
|
if (!o && !i) return null
|
|
2833
|
-
const
|
|
2834
|
-
|
|
2835
|
-
if (
|
|
2836
|
-
if (
|
|
2827
|
+
const s = ['true', '1', 'yes', 'y', 'on'],
|
|
2828
|
+
c = ['false', '0', 'no', 'n', 'off']
|
|
2829
|
+
if (s.includes(o.toLowerCase())) return !0
|
|
2830
|
+
if (c.includes(o.toLowerCase())) return !1
|
|
2837
2831
|
throw Error(`Invalid boolean argument ${t}: ${o}`)
|
|
2838
2832
|
}
|
|
2839
2833
|
function requireStringArgument(n, t, e, r) {
|
|
@@ -2912,9 +2906,9 @@ function findCorners(n, t, e, r) {
|
|
|
2912
2906
|
{ x: e, y: r }
|
|
2913
2907
|
]
|
|
2914
2908
|
for (let o = 0; o < n.length; o++)
|
|
2915
|
-
for (let
|
|
2916
|
-
const
|
|
2917
|
-
for (const u of
|
|
2909
|
+
for (let s = 0; s < n[0].length; s++) {
|
|
2910
|
+
const c = getCorners(n, o, s)
|
|
2911
|
+
for (const u of c) i.some(f => f.x === u.x && f.y === u.y) || i.push(u)
|
|
2918
2912
|
}
|
|
2919
2913
|
return i.map(o => ({ x: o.x * t, y: o.y * t }))
|
|
2920
2914
|
}
|
|
@@ -2924,9 +2918,9 @@ function findLines(n, t) {
|
|
|
2924
2918
|
i = filterCoordinates(n, (u, f) => n[u][f] === 0 && n[u - 1]?.[f] !== 0, 'column-first').map(u => ({ ...u, dx: 0, dy: 1 })),
|
|
2925
2919
|
o = filterCoordinates(n, (u, f) => n[u][f] === 0 && n[u + 1]?.[f] !== 0, 'column-first').map(u => ({ ...u, dx: 0, dy: 1 }))
|
|
2926
2920
|
e.forEach(u => u.y++), o.forEach(u => u.x++)
|
|
2927
|
-
const
|
|
2928
|
-
|
|
2929
|
-
return [...
|
|
2921
|
+
const s = group([...i, ...o], (u, f) => u.x === f.x && u.y - 1 === f.y),
|
|
2922
|
+
c = group([...r, ...e], (u, f) => u.y === f.y && u.x - 1 === f.x)
|
|
2923
|
+
return [...s, ...c].map(u => ({ start: u[0], end: last(u) })).map(u => ({ start: multiplyPoint(u.start, t), end: multiplyPoint(addPoint(u.end, { x: u.start.dx, y: u.start.dy }), t) }))
|
|
2930
2924
|
}
|
|
2931
2925
|
function getAngleInRadians(n, t) {
|
|
2932
2926
|
return Math.atan2(t.y - n.y, t.x - n.x)
|
|
@@ -2938,33 +2932,33 @@ function getLineIntersectionPoint(n, t, e, r) {
|
|
|
2938
2932
|
const i = (r.y - e.y) * (t.x - n.x) - (r.x - e.x) * (t.y - n.y)
|
|
2939
2933
|
if (i === 0) return null
|
|
2940
2934
|
let o = n.y - e.y,
|
|
2941
|
-
|
|
2942
|
-
const
|
|
2943
|
-
u = (t.x - n.x) * o - (t.y - n.y) *
|
|
2944
|
-
return (o =
|
|
2935
|
+
s = n.x - e.x
|
|
2936
|
+
const c = (r.x - e.x) * o - (r.y - e.y) * s,
|
|
2937
|
+
u = (t.x - n.x) * o - (t.y - n.y) * s
|
|
2938
|
+
return (o = c / i), (s = u / i), o > 0 && o < 1 && s > 0 && s < 1 ? { x: n.x + o * (t.x - n.x), y: n.y + o * (t.y - n.y) } : null
|
|
2945
2939
|
}
|
|
2946
2940
|
function raycast(n, t, e) {
|
|
2947
2941
|
const r = [],
|
|
2948
2942
|
i = pushPoint(n, e, 1e4)
|
|
2949
2943
|
for (const o of t) {
|
|
2950
|
-
const
|
|
2951
|
-
|
|
2944
|
+
const s = getLineIntersectionPoint(n, i, o.start, o.end)
|
|
2945
|
+
s && r.push(s)
|
|
2952
2946
|
}
|
|
2953
2947
|
return r.length
|
|
2954
|
-
? r.reduce((o,
|
|
2955
|
-
const
|
|
2948
|
+
? r.reduce((o, s) => {
|
|
2949
|
+
const c = getDistanceBetweenPoints(n, s),
|
|
2956
2950
|
u = getDistanceBetweenPoints(n, o)
|
|
2957
|
-
return
|
|
2951
|
+
return c < u ? s : o
|
|
2958
2952
|
})
|
|
2959
2953
|
: null
|
|
2960
2954
|
}
|
|
2961
2955
|
function raycastCircle(n, t, e) {
|
|
2962
2956
|
const i = getSortedRayAngles(n, e),
|
|
2963
2957
|
o = []
|
|
2964
|
-
for (const
|
|
2965
|
-
const
|
|
2966
|
-
u = raycast(n, t,
|
|
2967
|
-
|
|
2958
|
+
for (const s of i) {
|
|
2959
|
+
const c = raycast(n, t, s - 0.001),
|
|
2960
|
+
u = raycast(n, t, s + 0.001)
|
|
2961
|
+
c && o.push(c), u && o.push(u)
|
|
2968
2962
|
}
|
|
2969
2963
|
return o
|
|
2970
2964
|
}
|
|
@@ -3037,18 +3031,18 @@ class TrieRouter {
|
|
|
3037
3031
|
let i = r,
|
|
3038
3032
|
o
|
|
3039
3033
|
if ((r.startsWith(':') && ((i = ':'), (o = r.slice(1))), !this.forks.has(i))) {
|
|
3040
|
-
const
|
|
3041
|
-
o && (
|
|
3034
|
+
const s = new TrieRouter()
|
|
3035
|
+
o && (s.variableName = o), this.forks.set(i, s)
|
|
3042
3036
|
}
|
|
3043
3037
|
this.forks.get(i).insert(t.slice(1), e)
|
|
3044
3038
|
}
|
|
3045
3039
|
async handle(t, e, r, i) {
|
|
3046
3040
|
if (t.length === 0) return this.handler ? (await this.handler(e, r, i), !0) : !1
|
|
3047
3041
|
const o = t[0],
|
|
3048
|
-
|
|
3049
|
-
if (
|
|
3050
|
-
const
|
|
3051
|
-
if (
|
|
3042
|
+
s = this.forks.get(o)
|
|
3043
|
+
if (s) return s.handle(t.slice(1), e, r, i)
|
|
3044
|
+
const c = this.forks.get(':')
|
|
3045
|
+
if (c) return c.variableName && i.set(c.variableName, decodeURIComponent(o)), c.handle(t.slice(1), e, r, i)
|
|
3052
3046
|
const u = this.forks.get('*')
|
|
3053
3047
|
return u ? (i.set('wildcard', t.join('/')), u.handler ? (await u.handler(e, r, i), !0) : !1) : !1
|
|
3054
3048
|
}
|