@leofcoin/peernet 1.2.7 → 1.2.8
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/exports/browser/{browser-DMVbMScf.js → browser-DapPqCOs.js} +1 -1
- package/exports/browser/{browser-CWWW0kSj.js → browser-Yh0HnjMJ.js} +1 -1
- package/exports/browser/{client-AodRTAgI.js → client-KrGUTq9O.js} +5 -5
- package/exports/browser/{identity-nIyW_Xm8.js → identity-CqSnKXWH.js} +10360 -10230
- package/exports/browser/identity.js +1 -1
- package/exports/browser/{index-ChRjMyiM.js → index-D_MPeSmw.js} +1 -1
- package/exports/browser/{index-BoQ4uWm2.js → index-UBgl-8YH.js} +1 -1
- package/exports/browser/{messages-Dy2W05lC.js → messages-2i_0i581.js} +2 -2
- package/exports/browser/{peernet-Cmg8trCq.js → peernet-BYkqPZ2L.js} +1203 -51
- package/exports/browser/peernet.js +2 -2
- package/package.json +3 -3
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { b as base, a as base58$1, i as index$2, c as index$3, d as index$4, e as index$5, I as Identity } from './identity-
|
|
1
|
+
import { b as base$1, a as base58$1, i as index$2, c as index$3, d as index$4, e as index$5, f as createSHA512, g as createSHA384, h as createSHA256, j as createSHA224, k as createSHA1, I as Identity } from './identity-CqSnKXWH.js';
|
|
2
2
|
import { K as KeyPath, a as KeyValue } from './value-C3vAp-wb.js';
|
|
3
3
|
|
|
4
4
|
const getTargets = () => Array.from([]);
|
|
@@ -127,10 +127,10 @@ var isHex = string => /^[A-F0-9]+$/i.test(
|
|
|
127
127
|
);
|
|
128
128
|
|
|
129
129
|
const ALPHABET$1 = '0123456789ABCDEF';
|
|
130
|
-
base(ALPHABET$1);
|
|
130
|
+
base$1(ALPHABET$1);
|
|
131
131
|
|
|
132
132
|
const ALPHABET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_';
|
|
133
|
-
base(ALPHABET);
|
|
133
|
+
base$1(ALPHABET);
|
|
134
134
|
|
|
135
135
|
/**
|
|
136
136
|
* Returns a Uint8Array as String
|
|
@@ -7068,7 +7068,7 @@ const toType = (data) => {
|
|
|
7068
7068
|
return new TextEncoder().encode(data.toString());
|
|
7069
7069
|
throw new Error(`unsuported type ${typeof data || data}`);
|
|
7070
7070
|
};
|
|
7071
|
-
const encode = (proto, input, compress) => {
|
|
7071
|
+
const encode$2 = (proto, input, compress) => {
|
|
7072
7072
|
const keys = Object.keys(proto);
|
|
7073
7073
|
const values = Object.values(proto);
|
|
7074
7074
|
const set = [];
|
|
@@ -7086,7 +7086,7 @@ const encode = (proto, input, compress) => {
|
|
|
7086
7086
|
}
|
|
7087
7087
|
return compress ? pako.deflate(index$3(set)) : index$3(set);
|
|
7088
7088
|
};
|
|
7089
|
-
const decode = (proto, uint8Array, compressed) => {
|
|
7089
|
+
const decode$4 = (proto, uint8Array, compressed) => {
|
|
7090
7090
|
if (compressed)
|
|
7091
7091
|
uint8Array = pako.inflate(uint8Array);
|
|
7092
7092
|
let deconcated = index$4(uint8Array);
|
|
@@ -7118,12 +7118,48 @@ const decode = (proto, uint8Array, compressed) => {
|
|
|
7118
7118
|
return output;
|
|
7119
7119
|
};
|
|
7120
7120
|
var index = {
|
|
7121
|
-
encode,
|
|
7122
|
-
decode
|
|
7121
|
+
encode: encode$2,
|
|
7122
|
+
decode: decode$4
|
|
7123
7123
|
};
|
|
7124
7124
|
|
|
7125
|
-
const
|
|
7126
|
-
const
|
|
7125
|
+
const BASE64_CHUNK_SIZE = 0x8000;
|
|
7126
|
+
const uint8ArrayToBase64 = (value) => {
|
|
7127
|
+
if (typeof Buffer !== 'undefined')
|
|
7128
|
+
return Buffer.from(value).toString('base64');
|
|
7129
|
+
let binary = '';
|
|
7130
|
+
for (let offset = 0; offset < value.length; offset += BASE64_CHUNK_SIZE) {
|
|
7131
|
+
const slice = value.subarray(offset, offset + BASE64_CHUNK_SIZE);
|
|
7132
|
+
binary += String.fromCharCode(...slice);
|
|
7133
|
+
}
|
|
7134
|
+
return btoa(binary);
|
|
7135
|
+
};
|
|
7136
|
+
const base64ToUint8Array = (value) => {
|
|
7137
|
+
if (typeof Buffer !== 'undefined')
|
|
7138
|
+
return new Uint8Array(Buffer.from(value, 'base64'));
|
|
7139
|
+
const binary = atob(value);
|
|
7140
|
+
const output = new Uint8Array(binary.length);
|
|
7141
|
+
for (let i = 0; i < binary.length; i++)
|
|
7142
|
+
output[i] = binary.charCodeAt(i);
|
|
7143
|
+
return output;
|
|
7144
|
+
};
|
|
7145
|
+
const jsonStringifyBigInt = (key, value) => {
|
|
7146
|
+
if (typeof value === 'bigint')
|
|
7147
|
+
return { $bigint: value.toString() };
|
|
7148
|
+
if (value instanceof Uint8Array)
|
|
7149
|
+
return { $uint8array: uint8ArrayToBase64(value) };
|
|
7150
|
+
return value;
|
|
7151
|
+
};
|
|
7152
|
+
const jsonParseBigInt = (key, value) => {
|
|
7153
|
+
if (typeof value === 'object' && value) {
|
|
7154
|
+
if (value.$bigint)
|
|
7155
|
+
return BigInt(value.$bigint);
|
|
7156
|
+
if (value.$uint8array)
|
|
7157
|
+
return base64ToUint8Array(value.$uint8array);
|
|
7158
|
+
}
|
|
7159
|
+
return value;
|
|
7160
|
+
};
|
|
7161
|
+
const _textEncoder = new TextEncoder();
|
|
7162
|
+
const _textDecoder = new TextDecoder();
|
|
7127
7163
|
class BasicInterface {
|
|
7128
7164
|
#encoded;
|
|
7129
7165
|
#decoded;
|
|
@@ -7158,20 +7194,49 @@ class BasicInterface {
|
|
|
7158
7194
|
}
|
|
7159
7195
|
decode(encoded) {
|
|
7160
7196
|
encoded = encoded || this.encoded;
|
|
7161
|
-
|
|
7197
|
+
// Example: decode as JSON if possible (override in subclass)
|
|
7198
|
+
try {
|
|
7199
|
+
return JSON.parse(_textDecoder.decode(encoded), jsonParseBigInt);
|
|
7200
|
+
}
|
|
7201
|
+
catch {
|
|
7202
|
+
return new Object();
|
|
7203
|
+
}
|
|
7162
7204
|
}
|
|
7163
7205
|
encode(decoded) {
|
|
7164
7206
|
decoded = decoded || this.decoded;
|
|
7165
|
-
|
|
7207
|
+
// Example: encode as JSON (override in subclass)
|
|
7208
|
+
return _textEncoder.encode(JSON.stringify(decoded, jsonStringifyBigInt));
|
|
7166
7209
|
}
|
|
7167
7210
|
// get Codec(): Codec {}
|
|
7211
|
+
// Cache proto keys/values for reuse
|
|
7212
|
+
static _protoCache = new WeakMap();
|
|
7168
7213
|
protoEncode(data) {
|
|
7169
|
-
|
|
7214
|
+
let cache = BasicInterface._protoCache.get(this.proto);
|
|
7215
|
+
if (!cache) {
|
|
7216
|
+
cache = {
|
|
7217
|
+
keys: Object.keys(this.proto),
|
|
7218
|
+
values: Object.values(this.proto)
|
|
7219
|
+
};
|
|
7220
|
+
BasicInterface._protoCache.set(this.proto, cache);
|
|
7221
|
+
}
|
|
7222
|
+
// Use proto.encode directly, but avoid new array allocations inside encode if possible
|
|
7170
7223
|
return index.encode(this.proto, data, false);
|
|
7171
7224
|
}
|
|
7172
7225
|
protoDecode(data) {
|
|
7173
|
-
//
|
|
7174
|
-
|
|
7226
|
+
// Use a static output object if possible (not thread-safe, but safe for single-threaded use)
|
|
7227
|
+
if (!this._decodeOutput)
|
|
7228
|
+
this._decodeOutput = {};
|
|
7229
|
+
const result = index.decode(this.proto, data, false);
|
|
7230
|
+
// Copy properties to static object to avoid new allocations
|
|
7231
|
+
Object.keys(result).forEach((k) => {
|
|
7232
|
+
this._decodeOutput[k] = result[k];
|
|
7233
|
+
});
|
|
7234
|
+
// Remove any keys not in result
|
|
7235
|
+
Object.keys(this._decodeOutput).forEach((k) => {
|
|
7236
|
+
if (!(k in result))
|
|
7237
|
+
delete this._decodeOutput[k];
|
|
7238
|
+
});
|
|
7239
|
+
return this._decodeOutput;
|
|
7175
7240
|
}
|
|
7176
7241
|
isHex(string) {
|
|
7177
7242
|
return isHex(string);
|
|
@@ -7204,7 +7269,10 @@ class BasicInterface {
|
|
|
7204
7269
|
return this.decode(fromHex(string));
|
|
7205
7270
|
}
|
|
7206
7271
|
fromArray(array) {
|
|
7207
|
-
|
|
7272
|
+
// Avoid unnecessary copy if already Uint8Array
|
|
7273
|
+
if (array instanceof Uint8Array)
|
|
7274
|
+
return this.decode(array);
|
|
7275
|
+
return this.decode(Uint8Array.from(array));
|
|
7208
7276
|
}
|
|
7209
7277
|
fromEncoded(encoded) {
|
|
7210
7278
|
return this.decode(encoded);
|
|
@@ -7212,15 +7280,21 @@ class BasicInterface {
|
|
|
7212
7280
|
toString() {
|
|
7213
7281
|
if (!this.encoded)
|
|
7214
7282
|
this.encode();
|
|
7215
|
-
|
|
7283
|
+
// Use cached string if available
|
|
7284
|
+
if (!this._string)
|
|
7285
|
+
this._string = Array.prototype.join.call(this.encoded, ',');
|
|
7286
|
+
return this._string;
|
|
7216
7287
|
}
|
|
7217
7288
|
toHex() {
|
|
7218
7289
|
if (!this.encoded)
|
|
7219
7290
|
this.encode();
|
|
7220
|
-
|
|
7221
|
-
|
|
7222
|
-
.
|
|
7223
|
-
|
|
7291
|
+
// Use cached hex if available
|
|
7292
|
+
if (!this._hex) {
|
|
7293
|
+
if (!this._string)
|
|
7294
|
+
this._string = Array.prototype.join.call(this.encoded, ',');
|
|
7295
|
+
this._hex = toHex(this._string.split(',').map(Number));
|
|
7296
|
+
}
|
|
7297
|
+
return this._hex;
|
|
7224
7298
|
}
|
|
7225
7299
|
/**
|
|
7226
7300
|
* @return {String} encoded
|
|
@@ -7228,7 +7302,10 @@ class BasicInterface {
|
|
|
7228
7302
|
toBs32() {
|
|
7229
7303
|
if (!this.encoded)
|
|
7230
7304
|
this.encode();
|
|
7231
|
-
|
|
7305
|
+
// Use cached bs32 if available
|
|
7306
|
+
if (!this._bs32)
|
|
7307
|
+
this._bs32 = toBase32(this.encoded);
|
|
7308
|
+
return this._bs32;
|
|
7232
7309
|
}
|
|
7233
7310
|
/**
|
|
7234
7311
|
* @return {String} encoded
|
|
@@ -7445,7 +7522,7 @@ var utils = {
|
|
|
7445
7522
|
codecs: codecs
|
|
7446
7523
|
};
|
|
7447
7524
|
|
|
7448
|
-
let Codec$
|
|
7525
|
+
let Codec$2 = class Codec extends BasicInterface {
|
|
7449
7526
|
codecBuffer;
|
|
7450
7527
|
codec;
|
|
7451
7528
|
hashAlg;
|
|
@@ -7546,6 +7623,1042 @@ let Codec$1 = class Codec extends BasicInterface {
|
|
|
7546
7623
|
}
|
|
7547
7624
|
};
|
|
7548
7625
|
|
|
7626
|
+
function equals$1(aa, bb) {
|
|
7627
|
+
if (aa === bb) {
|
|
7628
|
+
return true;
|
|
7629
|
+
}
|
|
7630
|
+
if (aa.byteLength !== bb.byteLength) {
|
|
7631
|
+
return false;
|
|
7632
|
+
}
|
|
7633
|
+
for (let ii = 0; ii < aa.byteLength; ii++) {
|
|
7634
|
+
if (aa[ii] !== bb[ii]) {
|
|
7635
|
+
return false;
|
|
7636
|
+
}
|
|
7637
|
+
}
|
|
7638
|
+
return true;
|
|
7639
|
+
}
|
|
7640
|
+
function coerce(o) {
|
|
7641
|
+
if (o instanceof Uint8Array && o.constructor.name === 'Uint8Array') {
|
|
7642
|
+
return o;
|
|
7643
|
+
}
|
|
7644
|
+
if (o instanceof ArrayBuffer) {
|
|
7645
|
+
return new Uint8Array(o);
|
|
7646
|
+
}
|
|
7647
|
+
if (ArrayBuffer.isView(o)) {
|
|
7648
|
+
return new Uint8Array(o.buffer, o.byteOffset, o.byteLength);
|
|
7649
|
+
}
|
|
7650
|
+
throw new Error('Unknown type, must be binary type');
|
|
7651
|
+
}
|
|
7652
|
+
|
|
7653
|
+
/* eslint-disable */
|
|
7654
|
+
// base-x encoding / decoding
|
|
7655
|
+
// Copyright (c) 2018 base-x contributors
|
|
7656
|
+
// Copyright (c) 2014-2018 The Bitcoin Core developers (base58.cpp)
|
|
7657
|
+
// Distributed under the MIT software license, see the accompanying
|
|
7658
|
+
// file LICENSE or http://www.opensource.org/licenses/mit-license.php.
|
|
7659
|
+
/**
|
|
7660
|
+
* @param {string} ALPHABET
|
|
7661
|
+
* @param {any} name
|
|
7662
|
+
*/
|
|
7663
|
+
function base(ALPHABET, name) {
|
|
7664
|
+
if (ALPHABET.length >= 255) {
|
|
7665
|
+
throw new TypeError('Alphabet too long');
|
|
7666
|
+
}
|
|
7667
|
+
var BASE_MAP = new Uint8Array(256);
|
|
7668
|
+
for (var j = 0; j < BASE_MAP.length; j++) {
|
|
7669
|
+
BASE_MAP[j] = 255;
|
|
7670
|
+
}
|
|
7671
|
+
for (var i = 0; i < ALPHABET.length; i++) {
|
|
7672
|
+
var x = ALPHABET.charAt(i);
|
|
7673
|
+
var xc = x.charCodeAt(0);
|
|
7674
|
+
if (BASE_MAP[xc] !== 255) {
|
|
7675
|
+
throw new TypeError(x + ' is ambiguous');
|
|
7676
|
+
}
|
|
7677
|
+
BASE_MAP[xc] = i;
|
|
7678
|
+
}
|
|
7679
|
+
var BASE = ALPHABET.length;
|
|
7680
|
+
var LEADER = ALPHABET.charAt(0);
|
|
7681
|
+
var FACTOR = Math.log(BASE) / Math.log(256); // log(BASE) / log(256), rounded up
|
|
7682
|
+
var iFACTOR = Math.log(256) / Math.log(BASE); // log(256) / log(BASE), rounded up
|
|
7683
|
+
/**
|
|
7684
|
+
* @param {any[] | Iterable<number>} source
|
|
7685
|
+
*/
|
|
7686
|
+
function encode(source) {
|
|
7687
|
+
// @ts-ignore
|
|
7688
|
+
if (source instanceof Uint8Array)
|
|
7689
|
+
;
|
|
7690
|
+
else if (ArrayBuffer.isView(source)) {
|
|
7691
|
+
source = new Uint8Array(source.buffer, source.byteOffset, source.byteLength);
|
|
7692
|
+
}
|
|
7693
|
+
else if (Array.isArray(source)) {
|
|
7694
|
+
source = Uint8Array.from(source);
|
|
7695
|
+
}
|
|
7696
|
+
if (!(source instanceof Uint8Array)) {
|
|
7697
|
+
throw new TypeError('Expected Uint8Array');
|
|
7698
|
+
}
|
|
7699
|
+
if (source.length === 0) {
|
|
7700
|
+
return '';
|
|
7701
|
+
}
|
|
7702
|
+
// Skip & count leading zeroes.
|
|
7703
|
+
var zeroes = 0;
|
|
7704
|
+
var length = 0;
|
|
7705
|
+
var pbegin = 0;
|
|
7706
|
+
var pend = source.length;
|
|
7707
|
+
while (pbegin !== pend && source[pbegin] === 0) {
|
|
7708
|
+
pbegin++;
|
|
7709
|
+
zeroes++;
|
|
7710
|
+
}
|
|
7711
|
+
// Allocate enough space in big-endian base58 representation.
|
|
7712
|
+
var size = ((pend - pbegin) * iFACTOR + 1) >>> 0;
|
|
7713
|
+
var b58 = new Uint8Array(size);
|
|
7714
|
+
// Process the bytes.
|
|
7715
|
+
while (pbegin !== pend) {
|
|
7716
|
+
var carry = source[pbegin];
|
|
7717
|
+
// Apply "b58 = b58 * 256 + ch".
|
|
7718
|
+
var i = 0;
|
|
7719
|
+
for (var it1 = size - 1; (carry !== 0 || i < length) && (it1 !== -1); it1--, i++) {
|
|
7720
|
+
carry += (256 * b58[it1]) >>> 0;
|
|
7721
|
+
b58[it1] = (carry % BASE) >>> 0;
|
|
7722
|
+
carry = (carry / BASE) >>> 0;
|
|
7723
|
+
}
|
|
7724
|
+
if (carry !== 0) {
|
|
7725
|
+
throw new Error('Non-zero carry');
|
|
7726
|
+
}
|
|
7727
|
+
length = i;
|
|
7728
|
+
pbegin++;
|
|
7729
|
+
}
|
|
7730
|
+
// Skip leading zeroes in base58 result.
|
|
7731
|
+
var it2 = size - length;
|
|
7732
|
+
while (it2 !== size && b58[it2] === 0) {
|
|
7733
|
+
it2++;
|
|
7734
|
+
}
|
|
7735
|
+
// Translate the result into a string.
|
|
7736
|
+
var str = LEADER.repeat(zeroes);
|
|
7737
|
+
for (; it2 < size; ++it2) {
|
|
7738
|
+
str += ALPHABET.charAt(b58[it2]);
|
|
7739
|
+
}
|
|
7740
|
+
return str;
|
|
7741
|
+
}
|
|
7742
|
+
/**
|
|
7743
|
+
* @param {string | string[]} source
|
|
7744
|
+
*/
|
|
7745
|
+
function decodeUnsafe(source) {
|
|
7746
|
+
if (typeof source !== 'string') {
|
|
7747
|
+
throw new TypeError('Expected String');
|
|
7748
|
+
}
|
|
7749
|
+
if (source.length === 0) {
|
|
7750
|
+
return new Uint8Array();
|
|
7751
|
+
}
|
|
7752
|
+
var psz = 0;
|
|
7753
|
+
// Skip leading spaces.
|
|
7754
|
+
if (source[psz] === ' ') {
|
|
7755
|
+
return;
|
|
7756
|
+
}
|
|
7757
|
+
// Skip and count leading '1's.
|
|
7758
|
+
var zeroes = 0;
|
|
7759
|
+
var length = 0;
|
|
7760
|
+
while (source[psz] === LEADER) {
|
|
7761
|
+
zeroes++;
|
|
7762
|
+
psz++;
|
|
7763
|
+
}
|
|
7764
|
+
// Allocate enough space in big-endian base256 representation.
|
|
7765
|
+
var size = (((source.length - psz) * FACTOR) + 1) >>> 0; // log(58) / log(256), rounded up.
|
|
7766
|
+
var b256 = new Uint8Array(size);
|
|
7767
|
+
// Process the characters.
|
|
7768
|
+
while (source[psz]) {
|
|
7769
|
+
// Decode character
|
|
7770
|
+
var carry = BASE_MAP[source.charCodeAt(psz)];
|
|
7771
|
+
// Invalid character
|
|
7772
|
+
if (carry === 255) {
|
|
7773
|
+
return;
|
|
7774
|
+
}
|
|
7775
|
+
var i = 0;
|
|
7776
|
+
for (var it3 = size - 1; (carry !== 0 || i < length) && (it3 !== -1); it3--, i++) {
|
|
7777
|
+
carry += (BASE * b256[it3]) >>> 0;
|
|
7778
|
+
b256[it3] = (carry % 256) >>> 0;
|
|
7779
|
+
carry = (carry / 256) >>> 0;
|
|
7780
|
+
}
|
|
7781
|
+
if (carry !== 0) {
|
|
7782
|
+
throw new Error('Non-zero carry');
|
|
7783
|
+
}
|
|
7784
|
+
length = i;
|
|
7785
|
+
psz++;
|
|
7786
|
+
}
|
|
7787
|
+
// Skip trailing spaces.
|
|
7788
|
+
if (source[psz] === ' ') {
|
|
7789
|
+
return;
|
|
7790
|
+
}
|
|
7791
|
+
// Skip leading zeroes in b256.
|
|
7792
|
+
var it4 = size - length;
|
|
7793
|
+
while (it4 !== size && b256[it4] === 0) {
|
|
7794
|
+
it4++;
|
|
7795
|
+
}
|
|
7796
|
+
var vch = new Uint8Array(zeroes + (size - it4));
|
|
7797
|
+
var j = zeroes;
|
|
7798
|
+
while (it4 !== size) {
|
|
7799
|
+
vch[j++] = b256[it4++];
|
|
7800
|
+
}
|
|
7801
|
+
return vch;
|
|
7802
|
+
}
|
|
7803
|
+
/**
|
|
7804
|
+
* @param {string | string[]} string
|
|
7805
|
+
*/
|
|
7806
|
+
function decode(string) {
|
|
7807
|
+
var buffer = decodeUnsafe(string);
|
|
7808
|
+
if (buffer) {
|
|
7809
|
+
return buffer;
|
|
7810
|
+
}
|
|
7811
|
+
throw new Error(`Non-${name} character`);
|
|
7812
|
+
}
|
|
7813
|
+
return {
|
|
7814
|
+
encode: encode,
|
|
7815
|
+
decodeUnsafe: decodeUnsafe,
|
|
7816
|
+
decode: decode
|
|
7817
|
+
};
|
|
7818
|
+
}
|
|
7819
|
+
var src = base;
|
|
7820
|
+
var _brrp__multiformats_scope_baseX = src;
|
|
7821
|
+
|
|
7822
|
+
/**
|
|
7823
|
+
* Class represents both BaseEncoder and MultibaseEncoder meaning it
|
|
7824
|
+
* can be used to encode to multibase or base encode without multibase
|
|
7825
|
+
* prefix.
|
|
7826
|
+
*/
|
|
7827
|
+
class Encoder {
|
|
7828
|
+
name;
|
|
7829
|
+
prefix;
|
|
7830
|
+
baseEncode;
|
|
7831
|
+
constructor(name, prefix, baseEncode) {
|
|
7832
|
+
this.name = name;
|
|
7833
|
+
this.prefix = prefix;
|
|
7834
|
+
this.baseEncode = baseEncode;
|
|
7835
|
+
}
|
|
7836
|
+
encode(bytes) {
|
|
7837
|
+
if (bytes instanceof Uint8Array) {
|
|
7838
|
+
return `${this.prefix}${this.baseEncode(bytes)}`;
|
|
7839
|
+
}
|
|
7840
|
+
else {
|
|
7841
|
+
throw Error('Unknown type, must be binary type');
|
|
7842
|
+
}
|
|
7843
|
+
}
|
|
7844
|
+
}
|
|
7845
|
+
/**
|
|
7846
|
+
* Class represents both BaseDecoder and MultibaseDecoder so it could be used
|
|
7847
|
+
* to decode multibases (with matching prefix) or just base decode strings
|
|
7848
|
+
* with corresponding base encoding.
|
|
7849
|
+
*/
|
|
7850
|
+
class Decoder {
|
|
7851
|
+
name;
|
|
7852
|
+
prefix;
|
|
7853
|
+
baseDecode;
|
|
7854
|
+
prefixCodePoint;
|
|
7855
|
+
constructor(name, prefix, baseDecode) {
|
|
7856
|
+
this.name = name;
|
|
7857
|
+
this.prefix = prefix;
|
|
7858
|
+
const prefixCodePoint = prefix.codePointAt(0);
|
|
7859
|
+
/* c8 ignore next 3 */
|
|
7860
|
+
if (prefixCodePoint === undefined) {
|
|
7861
|
+
throw new Error('Invalid prefix character');
|
|
7862
|
+
}
|
|
7863
|
+
this.prefixCodePoint = prefixCodePoint;
|
|
7864
|
+
this.baseDecode = baseDecode;
|
|
7865
|
+
}
|
|
7866
|
+
decode(text) {
|
|
7867
|
+
if (typeof text === 'string') {
|
|
7868
|
+
if (text.codePointAt(0) !== this.prefixCodePoint) {
|
|
7869
|
+
throw Error(`Unable to decode multibase string ${JSON.stringify(text)}, ${this.name} decoder only supports inputs prefixed with ${this.prefix}`);
|
|
7870
|
+
}
|
|
7871
|
+
return this.baseDecode(text.slice(this.prefix.length));
|
|
7872
|
+
}
|
|
7873
|
+
else {
|
|
7874
|
+
throw Error('Can only multibase decode strings');
|
|
7875
|
+
}
|
|
7876
|
+
}
|
|
7877
|
+
or(decoder) {
|
|
7878
|
+
return or(this, decoder);
|
|
7879
|
+
}
|
|
7880
|
+
}
|
|
7881
|
+
class ComposedDecoder {
|
|
7882
|
+
decoders;
|
|
7883
|
+
constructor(decoders) {
|
|
7884
|
+
this.decoders = decoders;
|
|
7885
|
+
}
|
|
7886
|
+
or(decoder) {
|
|
7887
|
+
return or(this, decoder);
|
|
7888
|
+
}
|
|
7889
|
+
decode(input) {
|
|
7890
|
+
const prefix = input[0];
|
|
7891
|
+
const decoder = this.decoders[prefix];
|
|
7892
|
+
if (decoder != null) {
|
|
7893
|
+
return decoder.decode(input);
|
|
7894
|
+
}
|
|
7895
|
+
else {
|
|
7896
|
+
throw RangeError(`Unable to decode multibase string ${JSON.stringify(input)}, only inputs prefixed with ${Object.keys(this.decoders)} are supported`);
|
|
7897
|
+
}
|
|
7898
|
+
}
|
|
7899
|
+
}
|
|
7900
|
+
function or(left, right) {
|
|
7901
|
+
return new ComposedDecoder({
|
|
7902
|
+
...(left.decoders ?? { [left.prefix]: left }),
|
|
7903
|
+
...(right.decoders ?? { [right.prefix]: right })
|
|
7904
|
+
});
|
|
7905
|
+
}
|
|
7906
|
+
let Codec$1 = class Codec {
|
|
7907
|
+
name;
|
|
7908
|
+
prefix;
|
|
7909
|
+
baseEncode;
|
|
7910
|
+
baseDecode;
|
|
7911
|
+
encoder;
|
|
7912
|
+
decoder;
|
|
7913
|
+
constructor(name, prefix, baseEncode, baseDecode) {
|
|
7914
|
+
this.name = name;
|
|
7915
|
+
this.prefix = prefix;
|
|
7916
|
+
this.baseEncode = baseEncode;
|
|
7917
|
+
this.baseDecode = baseDecode;
|
|
7918
|
+
this.encoder = new Encoder(name, prefix, baseEncode);
|
|
7919
|
+
this.decoder = new Decoder(name, prefix, baseDecode);
|
|
7920
|
+
}
|
|
7921
|
+
encode(input) {
|
|
7922
|
+
return this.encoder.encode(input);
|
|
7923
|
+
}
|
|
7924
|
+
decode(input) {
|
|
7925
|
+
return this.decoder.decode(input);
|
|
7926
|
+
}
|
|
7927
|
+
};
|
|
7928
|
+
function from({ name, prefix, encode, decode }) {
|
|
7929
|
+
return new Codec$1(name, prefix, encode, decode);
|
|
7930
|
+
}
|
|
7931
|
+
function baseX({ name, prefix, alphabet }) {
|
|
7932
|
+
const { encode, decode } = _brrp__multiformats_scope_baseX(alphabet, name);
|
|
7933
|
+
return from({
|
|
7934
|
+
prefix,
|
|
7935
|
+
name,
|
|
7936
|
+
encode,
|
|
7937
|
+
decode: (text) => coerce(decode(text))
|
|
7938
|
+
});
|
|
7939
|
+
}
|
|
7940
|
+
function decode$3(string, alphabetIdx, bitsPerChar, name) {
|
|
7941
|
+
// Count the padding bytes:
|
|
7942
|
+
let end = string.length;
|
|
7943
|
+
while (string[end - 1] === '=') {
|
|
7944
|
+
--end;
|
|
7945
|
+
}
|
|
7946
|
+
// Allocate the output:
|
|
7947
|
+
const out = new Uint8Array((end * bitsPerChar / 8) | 0);
|
|
7948
|
+
// Parse the data:
|
|
7949
|
+
let bits = 0; // Number of bits currently in the buffer
|
|
7950
|
+
let buffer = 0; // Bits waiting to be written out, MSB first
|
|
7951
|
+
let written = 0; // Next byte to write
|
|
7952
|
+
for (let i = 0; i < end; ++i) {
|
|
7953
|
+
// Read one character from the string:
|
|
7954
|
+
const value = alphabetIdx[string[i]];
|
|
7955
|
+
if (value === undefined) {
|
|
7956
|
+
throw new SyntaxError(`Non-${name} character`);
|
|
7957
|
+
}
|
|
7958
|
+
// Append the bits to the buffer:
|
|
7959
|
+
buffer = (buffer << bitsPerChar) | value;
|
|
7960
|
+
bits += bitsPerChar;
|
|
7961
|
+
// Write out some bits if the buffer has a byte's worth:
|
|
7962
|
+
if (bits >= 8) {
|
|
7963
|
+
bits -= 8;
|
|
7964
|
+
out[written++] = 0xff & (buffer >> bits);
|
|
7965
|
+
}
|
|
7966
|
+
}
|
|
7967
|
+
// Verify that we have received just enough bits:
|
|
7968
|
+
if (bits >= bitsPerChar || (0xff & (buffer << (8 - bits))) !== 0) {
|
|
7969
|
+
throw new SyntaxError('Unexpected end of data');
|
|
7970
|
+
}
|
|
7971
|
+
return out;
|
|
7972
|
+
}
|
|
7973
|
+
function encode$1(data, alphabet, bitsPerChar) {
|
|
7974
|
+
const pad = alphabet[alphabet.length - 1] === '=';
|
|
7975
|
+
const mask = (1 << bitsPerChar) - 1;
|
|
7976
|
+
let out = '';
|
|
7977
|
+
let bits = 0; // Number of bits currently in the buffer
|
|
7978
|
+
let buffer = 0; // Bits waiting to be written out, MSB first
|
|
7979
|
+
for (let i = 0; i < data.length; ++i) {
|
|
7980
|
+
// Slurp data into the buffer:
|
|
7981
|
+
buffer = (buffer << 8) | data[i];
|
|
7982
|
+
bits += 8;
|
|
7983
|
+
// Write out as much as we can:
|
|
7984
|
+
while (bits > bitsPerChar) {
|
|
7985
|
+
bits -= bitsPerChar;
|
|
7986
|
+
out += alphabet[mask & (buffer >> bits)];
|
|
7987
|
+
}
|
|
7988
|
+
}
|
|
7989
|
+
// Partial character:
|
|
7990
|
+
if (bits !== 0) {
|
|
7991
|
+
out += alphabet[mask & (buffer << (bitsPerChar - bits))];
|
|
7992
|
+
}
|
|
7993
|
+
// Add padding characters until we hit a byte boundary:
|
|
7994
|
+
if (pad) {
|
|
7995
|
+
while (((out.length * bitsPerChar) & 7) !== 0) {
|
|
7996
|
+
out += '=';
|
|
7997
|
+
}
|
|
7998
|
+
}
|
|
7999
|
+
return out;
|
|
8000
|
+
}
|
|
8001
|
+
function createAlphabetIdx(alphabet) {
|
|
8002
|
+
// Build the character lookup table:
|
|
8003
|
+
const alphabetIdx = {};
|
|
8004
|
+
for (let i = 0; i < alphabet.length; ++i) {
|
|
8005
|
+
alphabetIdx[alphabet[i]] = i;
|
|
8006
|
+
}
|
|
8007
|
+
return alphabetIdx;
|
|
8008
|
+
}
|
|
8009
|
+
/**
|
|
8010
|
+
* RFC4648 Factory
|
|
8011
|
+
*/
|
|
8012
|
+
function rfc4648({ name, prefix, bitsPerChar, alphabet }) {
|
|
8013
|
+
const alphabetIdx = createAlphabetIdx(alphabet);
|
|
8014
|
+
return from({
|
|
8015
|
+
prefix,
|
|
8016
|
+
name,
|
|
8017
|
+
encode(input) {
|
|
8018
|
+
return encode$1(input, alphabet, bitsPerChar);
|
|
8019
|
+
},
|
|
8020
|
+
decode(input) {
|
|
8021
|
+
return decode$3(input, alphabetIdx, bitsPerChar, name);
|
|
8022
|
+
}
|
|
8023
|
+
});
|
|
8024
|
+
}
|
|
8025
|
+
|
|
8026
|
+
const base32 = rfc4648({
|
|
8027
|
+
prefix: 'b',
|
|
8028
|
+
name: 'base32',
|
|
8029
|
+
alphabet: 'abcdefghijklmnopqrstuvwxyz234567',
|
|
8030
|
+
bitsPerChar: 5
|
|
8031
|
+
});
|
|
8032
|
+
rfc4648({
|
|
8033
|
+
prefix: 'B',
|
|
8034
|
+
name: 'base32upper',
|
|
8035
|
+
alphabet: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567',
|
|
8036
|
+
bitsPerChar: 5
|
|
8037
|
+
});
|
|
8038
|
+
rfc4648({
|
|
8039
|
+
prefix: 'c',
|
|
8040
|
+
name: 'base32pad',
|
|
8041
|
+
alphabet: 'abcdefghijklmnopqrstuvwxyz234567=',
|
|
8042
|
+
bitsPerChar: 5
|
|
8043
|
+
});
|
|
8044
|
+
rfc4648({
|
|
8045
|
+
prefix: 'C',
|
|
8046
|
+
name: 'base32padupper',
|
|
8047
|
+
alphabet: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567=',
|
|
8048
|
+
bitsPerChar: 5
|
|
8049
|
+
});
|
|
8050
|
+
rfc4648({
|
|
8051
|
+
prefix: 'v',
|
|
8052
|
+
name: 'base32hex',
|
|
8053
|
+
alphabet: '0123456789abcdefghijklmnopqrstuv',
|
|
8054
|
+
bitsPerChar: 5
|
|
8055
|
+
});
|
|
8056
|
+
rfc4648({
|
|
8057
|
+
prefix: 'V',
|
|
8058
|
+
name: 'base32hexupper',
|
|
8059
|
+
alphabet: '0123456789ABCDEFGHIJKLMNOPQRSTUV',
|
|
8060
|
+
bitsPerChar: 5
|
|
8061
|
+
});
|
|
8062
|
+
rfc4648({
|
|
8063
|
+
prefix: 't',
|
|
8064
|
+
name: 'base32hexpad',
|
|
8065
|
+
alphabet: '0123456789abcdefghijklmnopqrstuv=',
|
|
8066
|
+
bitsPerChar: 5
|
|
8067
|
+
});
|
|
8068
|
+
rfc4648({
|
|
8069
|
+
prefix: 'T',
|
|
8070
|
+
name: 'base32hexpadupper',
|
|
8071
|
+
alphabet: '0123456789ABCDEFGHIJKLMNOPQRSTUV=',
|
|
8072
|
+
bitsPerChar: 5
|
|
8073
|
+
});
|
|
8074
|
+
rfc4648({
|
|
8075
|
+
prefix: 'h',
|
|
8076
|
+
name: 'base32z',
|
|
8077
|
+
alphabet: 'ybndrfg8ejkmcpqxot1uwisza345h769',
|
|
8078
|
+
bitsPerChar: 5
|
|
8079
|
+
});
|
|
8080
|
+
|
|
8081
|
+
const base36 = baseX({
|
|
8082
|
+
prefix: 'k',
|
|
8083
|
+
name: 'base36',
|
|
8084
|
+
alphabet: '0123456789abcdefghijklmnopqrstuvwxyz'
|
|
8085
|
+
});
|
|
8086
|
+
baseX({
|
|
8087
|
+
prefix: 'K',
|
|
8088
|
+
name: 'base36upper',
|
|
8089
|
+
alphabet: '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'
|
|
8090
|
+
});
|
|
8091
|
+
|
|
8092
|
+
const base58btc = baseX({
|
|
8093
|
+
name: 'base58btc',
|
|
8094
|
+
prefix: 'z',
|
|
8095
|
+
alphabet: '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
|
|
8096
|
+
});
|
|
8097
|
+
baseX({
|
|
8098
|
+
name: 'base58flickr',
|
|
8099
|
+
prefix: 'Z',
|
|
8100
|
+
alphabet: '123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ'
|
|
8101
|
+
});
|
|
8102
|
+
|
|
8103
|
+
/* eslint-disable */
|
|
8104
|
+
var encode_1 = encode;
|
|
8105
|
+
var MSB = 0x80, MSBALL = -128, INT = Math.pow(2, 31);
|
|
8106
|
+
/**
|
|
8107
|
+
* @param {number} num
|
|
8108
|
+
* @param {number[]} out
|
|
8109
|
+
* @param {number} offset
|
|
8110
|
+
*/
|
|
8111
|
+
function encode(num, out, offset) {
|
|
8112
|
+
out = out || [];
|
|
8113
|
+
offset = offset || 0;
|
|
8114
|
+
var oldOffset = offset;
|
|
8115
|
+
while (num >= INT) {
|
|
8116
|
+
out[offset++] = (num & 0xFF) | MSB;
|
|
8117
|
+
num /= 128;
|
|
8118
|
+
}
|
|
8119
|
+
while (num & MSBALL) {
|
|
8120
|
+
out[offset++] = (num & 0xFF) | MSB;
|
|
8121
|
+
num >>>= 7;
|
|
8122
|
+
}
|
|
8123
|
+
out[offset] = num | 0;
|
|
8124
|
+
// @ts-ignore
|
|
8125
|
+
encode.bytes = offset - oldOffset + 1;
|
|
8126
|
+
return out;
|
|
8127
|
+
}
|
|
8128
|
+
var decode$2 = read;
|
|
8129
|
+
var MSB$1 = 0x80, REST$1 = 0x7F;
|
|
8130
|
+
/**
|
|
8131
|
+
* @param {string | any[]} buf
|
|
8132
|
+
* @param {number} offset
|
|
8133
|
+
*/
|
|
8134
|
+
function read(buf, offset) {
|
|
8135
|
+
var res = 0, offset = offset || 0, shift = 0, counter = offset, b, l = buf.length;
|
|
8136
|
+
do {
|
|
8137
|
+
if (counter >= l) {
|
|
8138
|
+
// @ts-ignore
|
|
8139
|
+
read.bytes = 0;
|
|
8140
|
+
throw new RangeError('Could not decode varint');
|
|
8141
|
+
}
|
|
8142
|
+
b = buf[counter++];
|
|
8143
|
+
res += shift < 28
|
|
8144
|
+
? (b & REST$1) << shift
|
|
8145
|
+
: (b & REST$1) * Math.pow(2, shift);
|
|
8146
|
+
shift += 7;
|
|
8147
|
+
} while (b >= MSB$1);
|
|
8148
|
+
// @ts-ignore
|
|
8149
|
+
read.bytes = counter - offset;
|
|
8150
|
+
return res;
|
|
8151
|
+
}
|
|
8152
|
+
var N1 = Math.pow(2, 7);
|
|
8153
|
+
var N2 = Math.pow(2, 14);
|
|
8154
|
+
var N3 = Math.pow(2, 21);
|
|
8155
|
+
var N4 = Math.pow(2, 28);
|
|
8156
|
+
var N5 = Math.pow(2, 35);
|
|
8157
|
+
var N6 = Math.pow(2, 42);
|
|
8158
|
+
var N7 = Math.pow(2, 49);
|
|
8159
|
+
var N8 = Math.pow(2, 56);
|
|
8160
|
+
var N9 = Math.pow(2, 63);
|
|
8161
|
+
var length = function (/** @type {number} */ value) {
|
|
8162
|
+
return (value < N1 ? 1
|
|
8163
|
+
: value < N2 ? 2
|
|
8164
|
+
: value < N3 ? 3
|
|
8165
|
+
: value < N4 ? 4
|
|
8166
|
+
: value < N5 ? 5
|
|
8167
|
+
: value < N6 ? 6
|
|
8168
|
+
: value < N7 ? 7
|
|
8169
|
+
: value < N8 ? 8
|
|
8170
|
+
: value < N9 ? 9
|
|
8171
|
+
: 10);
|
|
8172
|
+
};
|
|
8173
|
+
var varint = {
|
|
8174
|
+
encode: encode_1,
|
|
8175
|
+
decode: decode$2,
|
|
8176
|
+
encodingLength: length
|
|
8177
|
+
};
|
|
8178
|
+
var _brrp_varint = varint;
|
|
8179
|
+
|
|
8180
|
+
function decode$1(data, offset = 0) {
|
|
8181
|
+
const code = _brrp_varint.decode(data, offset);
|
|
8182
|
+
return [code, _brrp_varint.decode.bytes];
|
|
8183
|
+
}
|
|
8184
|
+
function encodeTo(int, target, offset = 0) {
|
|
8185
|
+
_brrp_varint.encode(int, target, offset);
|
|
8186
|
+
return target;
|
|
8187
|
+
}
|
|
8188
|
+
function encodingLength(int) {
|
|
8189
|
+
return _brrp_varint.encodingLength(int);
|
|
8190
|
+
}
|
|
8191
|
+
|
|
8192
|
+
/**
|
|
8193
|
+
* Creates a multihash digest.
|
|
8194
|
+
*/
|
|
8195
|
+
function create(code, digest) {
|
|
8196
|
+
const size = digest.byteLength;
|
|
8197
|
+
const sizeOffset = encodingLength(code);
|
|
8198
|
+
const digestOffset = sizeOffset + encodingLength(size);
|
|
8199
|
+
const bytes = new Uint8Array(digestOffset + size);
|
|
8200
|
+
encodeTo(code, bytes, 0);
|
|
8201
|
+
encodeTo(size, bytes, sizeOffset);
|
|
8202
|
+
bytes.set(digest, digestOffset);
|
|
8203
|
+
return new Digest(code, size, digest, bytes);
|
|
8204
|
+
}
|
|
8205
|
+
/**
|
|
8206
|
+
* Turns bytes representation of multihash digest into an instance.
|
|
8207
|
+
*/
|
|
8208
|
+
function decode(multihash) {
|
|
8209
|
+
const bytes = coerce(multihash);
|
|
8210
|
+
const [code, sizeOffset] = decode$1(bytes);
|
|
8211
|
+
const [size, digestOffset] = decode$1(bytes.subarray(sizeOffset));
|
|
8212
|
+
const digest = bytes.subarray(sizeOffset + digestOffset);
|
|
8213
|
+
if (digest.byteLength !== size) {
|
|
8214
|
+
throw new Error('Incorrect length');
|
|
8215
|
+
}
|
|
8216
|
+
return new Digest(code, size, digest, bytes);
|
|
8217
|
+
}
|
|
8218
|
+
function equals(a, b) {
|
|
8219
|
+
if (a === b) {
|
|
8220
|
+
return true;
|
|
8221
|
+
}
|
|
8222
|
+
else {
|
|
8223
|
+
const data = b;
|
|
8224
|
+
return (a.code === data.code &&
|
|
8225
|
+
a.size === data.size &&
|
|
8226
|
+
data.bytes instanceof Uint8Array &&
|
|
8227
|
+
equals$1(a.bytes, data.bytes));
|
|
8228
|
+
}
|
|
8229
|
+
}
|
|
8230
|
+
/**
|
|
8231
|
+
* Represents a multihash digest which carries information about the
|
|
8232
|
+
* hashing algorithm and an actual hash digest.
|
|
8233
|
+
*/
|
|
8234
|
+
class Digest {
|
|
8235
|
+
code;
|
|
8236
|
+
size;
|
|
8237
|
+
digest;
|
|
8238
|
+
bytes;
|
|
8239
|
+
/**
|
|
8240
|
+
* Creates a multihash digest.
|
|
8241
|
+
*/
|
|
8242
|
+
constructor(code, size, digest, bytes) {
|
|
8243
|
+
this.code = code;
|
|
8244
|
+
this.size = size;
|
|
8245
|
+
this.digest = digest;
|
|
8246
|
+
this.bytes = bytes;
|
|
8247
|
+
}
|
|
8248
|
+
}
|
|
8249
|
+
|
|
8250
|
+
function format(link, base) {
|
|
8251
|
+
const { bytes, version } = link;
|
|
8252
|
+
switch (version) {
|
|
8253
|
+
case 0:
|
|
8254
|
+
return toStringV0(bytes, baseCache(link), base ?? base58btc.encoder);
|
|
8255
|
+
default:
|
|
8256
|
+
return toStringV1(bytes, baseCache(link), (base ?? base32.encoder));
|
|
8257
|
+
}
|
|
8258
|
+
}
|
|
8259
|
+
const cache = new WeakMap();
|
|
8260
|
+
function baseCache(cid) {
|
|
8261
|
+
const baseCache = cache.get(cid);
|
|
8262
|
+
if (baseCache == null) {
|
|
8263
|
+
const baseCache = new Map();
|
|
8264
|
+
cache.set(cid, baseCache);
|
|
8265
|
+
return baseCache;
|
|
8266
|
+
}
|
|
8267
|
+
return baseCache;
|
|
8268
|
+
}
|
|
8269
|
+
class CID {
|
|
8270
|
+
code;
|
|
8271
|
+
version;
|
|
8272
|
+
multihash;
|
|
8273
|
+
bytes;
|
|
8274
|
+
'/';
|
|
8275
|
+
/**
|
|
8276
|
+
* @param version - Version of the CID
|
|
8277
|
+
* @param code - Code of the codec content is encoded in, see https://github.com/multiformats/multicodec/blob/master/table.csv
|
|
8278
|
+
* @param multihash - (Multi)hash of the of the content.
|
|
8279
|
+
*/
|
|
8280
|
+
constructor(version, code, multihash, bytes) {
|
|
8281
|
+
this.code = code;
|
|
8282
|
+
this.version = version;
|
|
8283
|
+
this.multihash = multihash;
|
|
8284
|
+
this.bytes = bytes;
|
|
8285
|
+
// flag to serializers that this is a CID and
|
|
8286
|
+
// should be treated specially
|
|
8287
|
+
this['/'] = bytes;
|
|
8288
|
+
}
|
|
8289
|
+
/**
|
|
8290
|
+
* Signalling `cid.asCID === cid` has been replaced with `cid['/'] === cid.bytes`
|
|
8291
|
+
* please either use `CID.asCID(cid)` or switch to new signalling mechanism
|
|
8292
|
+
*
|
|
8293
|
+
* @deprecated
|
|
8294
|
+
*/
|
|
8295
|
+
get asCID() {
|
|
8296
|
+
return this;
|
|
8297
|
+
}
|
|
8298
|
+
// ArrayBufferView
|
|
8299
|
+
get byteOffset() {
|
|
8300
|
+
return this.bytes.byteOffset;
|
|
8301
|
+
}
|
|
8302
|
+
// ArrayBufferView
|
|
8303
|
+
get byteLength() {
|
|
8304
|
+
return this.bytes.byteLength;
|
|
8305
|
+
}
|
|
8306
|
+
toV0() {
|
|
8307
|
+
switch (this.version) {
|
|
8308
|
+
case 0: {
|
|
8309
|
+
return this;
|
|
8310
|
+
}
|
|
8311
|
+
case 1: {
|
|
8312
|
+
const { code, multihash } = this;
|
|
8313
|
+
if (code !== DAG_PB_CODE) {
|
|
8314
|
+
throw new Error('Cannot convert a non dag-pb CID to CIDv0');
|
|
8315
|
+
}
|
|
8316
|
+
// sha2-256
|
|
8317
|
+
if (multihash.code !== SHA_256_CODE) {
|
|
8318
|
+
throw new Error('Cannot convert non sha2-256 multihash CID to CIDv0');
|
|
8319
|
+
}
|
|
8320
|
+
return (CID.createV0(multihash));
|
|
8321
|
+
}
|
|
8322
|
+
default: {
|
|
8323
|
+
throw Error(`Can not convert CID version ${this.version} to version 0. This is a bug please report`);
|
|
8324
|
+
}
|
|
8325
|
+
}
|
|
8326
|
+
}
|
|
8327
|
+
toV1() {
|
|
8328
|
+
switch (this.version) {
|
|
8329
|
+
case 0: {
|
|
8330
|
+
const { code, digest } = this.multihash;
|
|
8331
|
+
const multihash = create(code, digest);
|
|
8332
|
+
return (CID.createV1(this.code, multihash));
|
|
8333
|
+
}
|
|
8334
|
+
case 1: {
|
|
8335
|
+
return this;
|
|
8336
|
+
}
|
|
8337
|
+
default: {
|
|
8338
|
+
throw Error(`Can not convert CID version ${this.version} to version 1. This is a bug please report`);
|
|
8339
|
+
}
|
|
8340
|
+
}
|
|
8341
|
+
}
|
|
8342
|
+
equals(other) {
|
|
8343
|
+
return CID.equals(this, other);
|
|
8344
|
+
}
|
|
8345
|
+
static equals(self, other) {
|
|
8346
|
+
const unknown = other;
|
|
8347
|
+
return (unknown != null &&
|
|
8348
|
+
self.code === unknown.code &&
|
|
8349
|
+
self.version === unknown.version &&
|
|
8350
|
+
equals(self.multihash, unknown.multihash));
|
|
8351
|
+
}
|
|
8352
|
+
toString(base) {
|
|
8353
|
+
return format(this, base);
|
|
8354
|
+
}
|
|
8355
|
+
toJSON() {
|
|
8356
|
+
return { '/': format(this) };
|
|
8357
|
+
}
|
|
8358
|
+
link() {
|
|
8359
|
+
return this;
|
|
8360
|
+
}
|
|
8361
|
+
[Symbol.toStringTag] = 'CID';
|
|
8362
|
+
// Legacy
|
|
8363
|
+
[Symbol.for('nodejs.util.inspect.custom')]() {
|
|
8364
|
+
return `CID(${this.toString()})`;
|
|
8365
|
+
}
|
|
8366
|
+
/**
|
|
8367
|
+
* Takes any input `value` and returns a `CID` instance if it was
|
|
8368
|
+
* a `CID` otherwise returns `null`. If `value` is instanceof `CID`
|
|
8369
|
+
* it will return value back. If `value` is not instance of this CID
|
|
8370
|
+
* class, but is compatible CID it will return new instance of this
|
|
8371
|
+
* `CID` class. Otherwise returns null.
|
|
8372
|
+
*
|
|
8373
|
+
* This allows two different incompatible versions of CID library to
|
|
8374
|
+
* co-exist and interop as long as binary interface is compatible.
|
|
8375
|
+
*/
|
|
8376
|
+
static asCID(input) {
|
|
8377
|
+
if (input == null) {
|
|
8378
|
+
return null;
|
|
8379
|
+
}
|
|
8380
|
+
const value = input;
|
|
8381
|
+
if (value instanceof CID) {
|
|
8382
|
+
// If value is instance of CID then we're all set.
|
|
8383
|
+
return value;
|
|
8384
|
+
}
|
|
8385
|
+
else if ((value['/'] != null && value['/'] === value.bytes) || value.asCID === value) {
|
|
8386
|
+
// If value isn't instance of this CID class but `this.asCID === this` or
|
|
8387
|
+
// `value['/'] === value.bytes` is true it is CID instance coming from a
|
|
8388
|
+
// different implementation (diff version or duplicate). In that case we
|
|
8389
|
+
// rebase it to this `CID` implementation so caller is guaranteed to get
|
|
8390
|
+
// instance with expected API.
|
|
8391
|
+
const { version, code, multihash, bytes } = value;
|
|
8392
|
+
return new CID(version, code, multihash, bytes ?? encodeCID(version, code, multihash.bytes));
|
|
8393
|
+
}
|
|
8394
|
+
else if (value[cidSymbol] === true) {
|
|
8395
|
+
// If value is a CID from older implementation that used to be tagged via
|
|
8396
|
+
// symbol we still rebase it to the this `CID` implementation by
|
|
8397
|
+
// delegating that to a constructor.
|
|
8398
|
+
const { version, multihash, code } = value;
|
|
8399
|
+
const digest = decode(multihash);
|
|
8400
|
+
return CID.create(version, code, digest);
|
|
8401
|
+
}
|
|
8402
|
+
else {
|
|
8403
|
+
// Otherwise value is not a CID (or an incompatible version of it) in
|
|
8404
|
+
// which case we return `null`.
|
|
8405
|
+
return null;
|
|
8406
|
+
}
|
|
8407
|
+
}
|
|
8408
|
+
/**
|
|
8409
|
+
* @param version - Version of the CID
|
|
8410
|
+
* @param code - Code of the codec content is encoded in, see https://github.com/multiformats/multicodec/blob/master/table.csv
|
|
8411
|
+
* @param digest - (Multi)hash of the of the content.
|
|
8412
|
+
*/
|
|
8413
|
+
static create(version, code, digest) {
|
|
8414
|
+
if (typeof code !== 'number') {
|
|
8415
|
+
throw new Error('String codecs are no longer supported');
|
|
8416
|
+
}
|
|
8417
|
+
if (!(digest.bytes instanceof Uint8Array)) {
|
|
8418
|
+
throw new Error('Invalid digest');
|
|
8419
|
+
}
|
|
8420
|
+
switch (version) {
|
|
8421
|
+
case 0: {
|
|
8422
|
+
if (code !== DAG_PB_CODE) {
|
|
8423
|
+
throw new Error(`Version 0 CID must use dag-pb (code: ${DAG_PB_CODE}) block encoding`);
|
|
8424
|
+
}
|
|
8425
|
+
else {
|
|
8426
|
+
return new CID(version, code, digest, digest.bytes);
|
|
8427
|
+
}
|
|
8428
|
+
}
|
|
8429
|
+
case 1: {
|
|
8430
|
+
const bytes = encodeCID(version, code, digest.bytes);
|
|
8431
|
+
return new CID(version, code, digest, bytes);
|
|
8432
|
+
}
|
|
8433
|
+
default: {
|
|
8434
|
+
throw new Error('Invalid version');
|
|
8435
|
+
}
|
|
8436
|
+
}
|
|
8437
|
+
}
|
|
8438
|
+
/**
|
|
8439
|
+
* Simplified version of `create` for CIDv0.
|
|
8440
|
+
*/
|
|
8441
|
+
static createV0(digest) {
|
|
8442
|
+
return CID.create(0, DAG_PB_CODE, digest);
|
|
8443
|
+
}
|
|
8444
|
+
/**
|
|
8445
|
+
* Simplified version of `create` for CIDv1.
|
|
8446
|
+
*
|
|
8447
|
+
* @param code - Content encoding format code.
|
|
8448
|
+
* @param digest - Multihash of the content.
|
|
8449
|
+
*/
|
|
8450
|
+
static createV1(code, digest) {
|
|
8451
|
+
return CID.create(1, code, digest);
|
|
8452
|
+
}
|
|
8453
|
+
/**
|
|
8454
|
+
* Decoded a CID from its binary representation. The byte array must contain
|
|
8455
|
+
* only the CID with no additional bytes.
|
|
8456
|
+
*
|
|
8457
|
+
* An error will be thrown if the bytes provided do not contain a valid
|
|
8458
|
+
* binary representation of a CID.
|
|
8459
|
+
*/
|
|
8460
|
+
static decode(bytes) {
|
|
8461
|
+
const [cid, remainder] = CID.decodeFirst(bytes);
|
|
8462
|
+
if (remainder.length !== 0) {
|
|
8463
|
+
throw new Error('Incorrect length');
|
|
8464
|
+
}
|
|
8465
|
+
return cid;
|
|
8466
|
+
}
|
|
8467
|
+
/**
|
|
8468
|
+
* Decoded a CID from its binary representation at the beginning of a byte
|
|
8469
|
+
* array.
|
|
8470
|
+
*
|
|
8471
|
+
* Returns an array with the first element containing the CID and the second
|
|
8472
|
+
* element containing the remainder of the original byte array. The remainder
|
|
8473
|
+
* will be a zero-length byte array if the provided bytes only contained a
|
|
8474
|
+
* binary CID representation.
|
|
8475
|
+
*/
|
|
8476
|
+
static decodeFirst(bytes) {
|
|
8477
|
+
const specs = CID.inspectBytes(bytes);
|
|
8478
|
+
const prefixSize = specs.size - specs.multihashSize;
|
|
8479
|
+
const multihashBytes = coerce(bytes.subarray(prefixSize, prefixSize + specs.multihashSize));
|
|
8480
|
+
if (multihashBytes.byteLength !== specs.multihashSize) {
|
|
8481
|
+
throw new Error('Incorrect length');
|
|
8482
|
+
}
|
|
8483
|
+
const digestBytes = multihashBytes.subarray(specs.multihashSize - specs.digestSize);
|
|
8484
|
+
const digest = new Digest(specs.multihashCode, specs.digestSize, digestBytes, multihashBytes);
|
|
8485
|
+
const cid = specs.version === 0
|
|
8486
|
+
? CID.createV0(digest)
|
|
8487
|
+
: CID.createV1(specs.codec, digest);
|
|
8488
|
+
return [cid, bytes.subarray(specs.size)];
|
|
8489
|
+
}
|
|
8490
|
+
/**
|
|
8491
|
+
* Inspect the initial bytes of a CID to determine its properties.
|
|
8492
|
+
*
|
|
8493
|
+
* Involves decoding up to 4 varints. Typically this will require only 4 to 6
|
|
8494
|
+
* bytes but for larger multicodec code values and larger multihash digest
|
|
8495
|
+
* lengths these varints can be quite large. It is recommended that at least
|
|
8496
|
+
* 10 bytes be made available in the `initialBytes` argument for a complete
|
|
8497
|
+
* inspection.
|
|
8498
|
+
*/
|
|
8499
|
+
static inspectBytes(initialBytes) {
|
|
8500
|
+
let offset = 0;
|
|
8501
|
+
const next = () => {
|
|
8502
|
+
const [i, length] = decode$1(initialBytes.subarray(offset));
|
|
8503
|
+
offset += length;
|
|
8504
|
+
return i;
|
|
8505
|
+
};
|
|
8506
|
+
let version = next();
|
|
8507
|
+
let codec = DAG_PB_CODE;
|
|
8508
|
+
if (version === 18) {
|
|
8509
|
+
// CIDv0
|
|
8510
|
+
version = 0;
|
|
8511
|
+
offset = 0;
|
|
8512
|
+
}
|
|
8513
|
+
else {
|
|
8514
|
+
codec = next();
|
|
8515
|
+
}
|
|
8516
|
+
if (version !== 0 && version !== 1) {
|
|
8517
|
+
throw new RangeError(`Invalid CID version ${version}`);
|
|
8518
|
+
}
|
|
8519
|
+
const prefixSize = offset;
|
|
8520
|
+
const multihashCode = next(); // multihash code
|
|
8521
|
+
const digestSize = next(); // multihash length
|
|
8522
|
+
const size = offset + digestSize;
|
|
8523
|
+
const multihashSize = size - prefixSize;
|
|
8524
|
+
return { version, codec, multihashCode, digestSize, multihashSize, size };
|
|
8525
|
+
}
|
|
8526
|
+
/**
|
|
8527
|
+
* Takes cid in a string representation and creates an instance. If `base`
|
|
8528
|
+
* decoder is not provided will use a default from the configuration. It will
|
|
8529
|
+
* throw an error if encoding of the CID is not compatible with supplied (or
|
|
8530
|
+
* a default decoder).
|
|
8531
|
+
*/
|
|
8532
|
+
static parse(source, base) {
|
|
8533
|
+
const [prefix, bytes] = parseCIDtoBytes(source, base);
|
|
8534
|
+
const cid = CID.decode(bytes);
|
|
8535
|
+
if (cid.version === 0 && source[0] !== 'Q') {
|
|
8536
|
+
throw Error('Version 0 CID string must not include multibase prefix');
|
|
8537
|
+
}
|
|
8538
|
+
// Cache string representation to avoid computing it on `this.toString()`
|
|
8539
|
+
baseCache(cid).set(prefix, source);
|
|
8540
|
+
return cid;
|
|
8541
|
+
}
|
|
8542
|
+
}
|
|
8543
|
+
function parseCIDtoBytes(source, base) {
|
|
8544
|
+
switch (source[0]) {
|
|
8545
|
+
// CIDv0 is parsed differently
|
|
8546
|
+
case 'Q': {
|
|
8547
|
+
const decoder = base ?? base58btc;
|
|
8548
|
+
return [
|
|
8549
|
+
base58btc.prefix,
|
|
8550
|
+
decoder.decode(`${base58btc.prefix}${source}`)
|
|
8551
|
+
];
|
|
8552
|
+
}
|
|
8553
|
+
case base58btc.prefix: {
|
|
8554
|
+
const decoder = base ?? base58btc;
|
|
8555
|
+
return [base58btc.prefix, decoder.decode(source)];
|
|
8556
|
+
}
|
|
8557
|
+
case base32.prefix: {
|
|
8558
|
+
const decoder = base ?? base32;
|
|
8559
|
+
return [base32.prefix, decoder.decode(source)];
|
|
8560
|
+
}
|
|
8561
|
+
case base36.prefix: {
|
|
8562
|
+
const decoder = base ?? base36;
|
|
8563
|
+
return [base36.prefix, decoder.decode(source)];
|
|
8564
|
+
}
|
|
8565
|
+
default: {
|
|
8566
|
+
if (base == null) {
|
|
8567
|
+
throw Error('To parse non base32, base36 or base58btc encoded CID multibase decoder must be provided');
|
|
8568
|
+
}
|
|
8569
|
+
return [source[0], base.decode(source)];
|
|
8570
|
+
}
|
|
8571
|
+
}
|
|
8572
|
+
}
|
|
8573
|
+
function toStringV0(bytes, cache, base) {
|
|
8574
|
+
const { prefix } = base;
|
|
8575
|
+
if (prefix !== base58btc.prefix) {
|
|
8576
|
+
throw Error(`Cannot string encode V0 in ${base.name} encoding`);
|
|
8577
|
+
}
|
|
8578
|
+
const cid = cache.get(prefix);
|
|
8579
|
+
if (cid == null) {
|
|
8580
|
+
const cid = base.encode(bytes).slice(1);
|
|
8581
|
+
cache.set(prefix, cid);
|
|
8582
|
+
return cid;
|
|
8583
|
+
}
|
|
8584
|
+
else {
|
|
8585
|
+
return cid;
|
|
8586
|
+
}
|
|
8587
|
+
}
|
|
8588
|
+
function toStringV1(bytes, cache, base) {
|
|
8589
|
+
const { prefix } = base;
|
|
8590
|
+
const cid = cache.get(prefix);
|
|
8591
|
+
if (cid == null) {
|
|
8592
|
+
const cid = base.encode(bytes);
|
|
8593
|
+
cache.set(prefix, cid);
|
|
8594
|
+
return cid;
|
|
8595
|
+
}
|
|
8596
|
+
else {
|
|
8597
|
+
return cid;
|
|
8598
|
+
}
|
|
8599
|
+
}
|
|
8600
|
+
const DAG_PB_CODE = 0x70;
|
|
8601
|
+
const SHA_256_CODE = 0x12;
|
|
8602
|
+
function encodeCID(version, code, multihash) {
|
|
8603
|
+
const codeOffset = encodingLength(version);
|
|
8604
|
+
const hashOffset = codeOffset + encodingLength(code);
|
|
8605
|
+
const bytes = new Uint8Array(hashOffset + multihash.byteLength);
|
|
8606
|
+
encodeTo(version, bytes, 0);
|
|
8607
|
+
encodeTo(code, bytes, codeOffset);
|
|
8608
|
+
bytes.set(multihash, hashOffset);
|
|
8609
|
+
return bytes;
|
|
8610
|
+
}
|
|
8611
|
+
const cidSymbol = Symbol.for('@ipld/js-cid/CID');
|
|
8612
|
+
|
|
8613
|
+
const MAX_WEBCRYPTO_INPUT = 0x7fffffff;
|
|
8614
|
+
const STREAMING_CHUNK_SIZE = 64 * 1024 * 1024;
|
|
8615
|
+
const toUint8Array = (buffer) => buffer instanceof Uint8Array ? buffer : new Uint8Array(buffer);
|
|
8616
|
+
const getStreamingHasher = async (hashVariant) => {
|
|
8617
|
+
switch (hashVariant) {
|
|
8618
|
+
case 1:
|
|
8619
|
+
return createSHA1();
|
|
8620
|
+
case 224:
|
|
8621
|
+
return createSHA224();
|
|
8622
|
+
case 256:
|
|
8623
|
+
return createSHA256();
|
|
8624
|
+
case 384:
|
|
8625
|
+
return createSHA384();
|
|
8626
|
+
case 512:
|
|
8627
|
+
return createSHA512();
|
|
8628
|
+
default:
|
|
8629
|
+
return null;
|
|
8630
|
+
}
|
|
8631
|
+
};
|
|
8632
|
+
const digestBuffer = async (hashVariant, buffer) => {
|
|
8633
|
+
const view = toUint8Array(buffer);
|
|
8634
|
+
if (view.byteLength >= MAX_WEBCRYPTO_INPUT) {
|
|
8635
|
+
const hasher = await getStreamingHasher(hashVariant);
|
|
8636
|
+
if (!hasher) {
|
|
8637
|
+
throw new Error(`unsupported streaming hash variant: ${hashVariant}`);
|
|
8638
|
+
}
|
|
8639
|
+
hasher.init();
|
|
8640
|
+
for (let offset = 0; offset < view.byteLength; offset += STREAMING_CHUNK_SIZE) {
|
|
8641
|
+
const end = Math.min(offset + STREAMING_CHUNK_SIZE, view.byteLength);
|
|
8642
|
+
hasher.update(view.subarray(offset, end));
|
|
8643
|
+
}
|
|
8644
|
+
const digest = hasher.digest('binary');
|
|
8645
|
+
return digest instanceof Uint8Array ? digest : new Uint8Array(digest);
|
|
8646
|
+
}
|
|
8647
|
+
const sourceView = view.byteOffset === 0 && view.byteLength === view.buffer.byteLength ? view : view.slice();
|
|
8648
|
+
const digest = await crypto.subtle.digest(`SHA-${hashVariant}`, sourceView.buffer);
|
|
8649
|
+
return new Uint8Array(digest);
|
|
8650
|
+
};
|
|
8651
|
+
const parseCid = (input) => {
|
|
8652
|
+
try {
|
|
8653
|
+
if (typeof input === 'string')
|
|
8654
|
+
return CID.parse(input);
|
|
8655
|
+
const bytes = input instanceof Uint8Array ? input : new Uint8Array(input);
|
|
8656
|
+
return CID.decode(bytes);
|
|
8657
|
+
}
|
|
8658
|
+
catch {
|
|
8659
|
+
return null;
|
|
8660
|
+
}
|
|
8661
|
+
};
|
|
7549
8662
|
class CodecHash extends BasicInterface {
|
|
7550
8663
|
codec;
|
|
7551
8664
|
discoCodec;
|
|
@@ -7562,8 +8675,17 @@ class CodecHash extends BasicInterface {
|
|
|
7562
8675
|
}
|
|
7563
8676
|
async init(uint8Array) {
|
|
7564
8677
|
if (uint8Array) {
|
|
8678
|
+
const cid = typeof uint8Array === 'string'
|
|
8679
|
+
? parseCid(uint8Array)
|
|
8680
|
+
: uint8Array instanceof Uint8Array || uint8Array instanceof ArrayBuffer
|
|
8681
|
+
? parseCid(uint8Array)
|
|
8682
|
+
: null;
|
|
8683
|
+
if (cid) {
|
|
8684
|
+
await this.fromCID(cid);
|
|
8685
|
+
return this;
|
|
8686
|
+
}
|
|
7565
8687
|
if (uint8Array instanceof Uint8Array) {
|
|
7566
|
-
this.discoCodec = new Codec$
|
|
8688
|
+
this.discoCodec = new Codec$2(uint8Array);
|
|
7567
8689
|
const name = this.discoCodec.name;
|
|
7568
8690
|
if (name) {
|
|
7569
8691
|
this.name = name;
|
|
@@ -7612,26 +8734,37 @@ class CodecHash extends BasicInterface {
|
|
|
7612
8734
|
this.name = name;
|
|
7613
8735
|
if (!buffer)
|
|
7614
8736
|
buffer = this.buffer;
|
|
7615
|
-
this.discoCodec = new Codec$
|
|
8737
|
+
this.discoCodec = new Codec$2(this.name);
|
|
7616
8738
|
this.discoCodec.fromName(this.name);
|
|
7617
8739
|
let hashAlg = this.discoCodec.hashAlg;
|
|
7618
8740
|
const hashVariant = Number(hashAlg.split('-')[hashAlg.split('-').length - 1]);
|
|
7619
8741
|
if (hashAlg.includes('dbl')) {
|
|
7620
8742
|
hashAlg = hashAlg.replace('dbl-', '');
|
|
7621
|
-
|
|
7622
|
-
// await hasher.init()
|
|
7623
|
-
// hasher.update(buffer)
|
|
7624
|
-
// buffer = hasher.digest('binary')
|
|
7625
|
-
buffer = await crypto.subtle.digest(`SHA-${hashVariant}`, buffer);
|
|
7626
|
-
}
|
|
7627
|
-
// const hasher = await createKeccak(hashVariant)
|
|
7628
|
-
// await hasher.init()
|
|
7629
|
-
// hasher.update(buffer)
|
|
7630
|
-
// this.digest = hasher.digest('binary')
|
|
7631
|
-
this.digest = await crypto.subtle.digest(`SHA-${hashVariant}`, buffer);
|
|
7632
|
-
if (this.digest instanceof ArrayBuffer) {
|
|
7633
|
-
this.digest = new Uint8Array(this.digest);
|
|
8743
|
+
buffer = await digestBuffer(hashVariant, buffer);
|
|
7634
8744
|
}
|
|
8745
|
+
this.digest = await digestBuffer(hashVariant, buffer);
|
|
8746
|
+
this.size = this.digest.length;
|
|
8747
|
+
this.codec = this.discoCodec.encode();
|
|
8748
|
+
this.codec = this.discoCodec.codecBuffer;
|
|
8749
|
+
const uint8Array = new Uint8Array(this.digest.length + this.prefix.length);
|
|
8750
|
+
uint8Array.set(this.prefix);
|
|
8751
|
+
uint8Array.set(this.digest, this.prefix.length);
|
|
8752
|
+
this.encoded = uint8Array;
|
|
8753
|
+
return this.encoded;
|
|
8754
|
+
}
|
|
8755
|
+
async fromCID(cid) {
|
|
8756
|
+
const parsed = cid instanceof CID
|
|
8757
|
+
? cid
|
|
8758
|
+
: typeof cid === 'string' || cid instanceof Uint8Array
|
|
8759
|
+
? parseCid(cid)
|
|
8760
|
+
: parseCid(cid);
|
|
8761
|
+
if (!parsed)
|
|
8762
|
+
throw new Error('invalid cid input');
|
|
8763
|
+
if (!this.name)
|
|
8764
|
+
this.name = 'disco-hash';
|
|
8765
|
+
this.discoCodec = new Codec$2(this.name);
|
|
8766
|
+
this.discoCodec.fromName(this.name);
|
|
8767
|
+
this.digest = parsed.multihash.digest;
|
|
7635
8768
|
this.size = this.digest.length;
|
|
7636
8769
|
this.codec = this.discoCodec.encode();
|
|
7637
8770
|
this.codec = this.discoCodec.codecBuffer;
|
|
@@ -7663,7 +8796,7 @@ class CodecHash extends BasicInterface {
|
|
|
7663
8796
|
decode(buffer) {
|
|
7664
8797
|
this.encoded = buffer;
|
|
7665
8798
|
const codec = index$5.decode(buffer);
|
|
7666
|
-
this.discoCodec = new Codec$
|
|
8799
|
+
this.discoCodec = new Codec$2(codec, this.codecs);
|
|
7667
8800
|
// TODO: validate codec
|
|
7668
8801
|
buffer = buffer.slice(index$5.decode.bytes);
|
|
7669
8802
|
this.size = index$5.decode(buffer);
|
|
@@ -7695,12 +8828,15 @@ let FormatInterface$1 = class FormatInterface extends BasicInterface {
|
|
|
7695
8828
|
this.#encoded = value;
|
|
7696
8829
|
}
|
|
7697
8830
|
init(buffer) {
|
|
7698
|
-
if (buffer instanceof FormatInterface && buffer?.name === this.name)
|
|
8831
|
+
if (buffer instanceof FormatInterface && buffer?.name === this.name) {
|
|
7699
8832
|
return buffer;
|
|
7700
|
-
|
|
8833
|
+
}
|
|
8834
|
+
else if (buffer instanceof Uint8Array) {
|
|
7701
8835
|
this.fromUint8Array(buffer);
|
|
7702
|
-
|
|
8836
|
+
}
|
|
8837
|
+
else if (buffer instanceof ArrayBuffer) {
|
|
7703
8838
|
this.fromArrayBuffer(buffer);
|
|
8839
|
+
}
|
|
7704
8840
|
else if (typeof buffer === 'string') {
|
|
7705
8841
|
if (this.isHex(buffer))
|
|
7706
8842
|
this.fromHex(buffer);
|
|
@@ -7711,21 +8847,25 @@ let FormatInterface$1 = class FormatInterface extends BasicInterface {
|
|
|
7711
8847
|
else
|
|
7712
8848
|
this.fromString(buffer);
|
|
7713
8849
|
}
|
|
7714
|
-
else {
|
|
8850
|
+
else if (typeof buffer === 'object' && buffer !== null) {
|
|
7715
8851
|
this.create(buffer);
|
|
7716
8852
|
}
|
|
8853
|
+
else {
|
|
8854
|
+
// Explicitly reject all other types (number, boolean, undefined, symbol, function, null)
|
|
8855
|
+
throw new TypeError(`Invalid input type for FormatInterface: ${typeof buffer}`);
|
|
8856
|
+
}
|
|
7717
8857
|
return this;
|
|
7718
8858
|
}
|
|
7719
8859
|
hasCodec() {
|
|
7720
8860
|
if (!this.encoded)
|
|
7721
8861
|
return false;
|
|
7722
|
-
const codec = new Codec$
|
|
8862
|
+
const codec = new Codec$2(this.encoded);
|
|
7723
8863
|
if (codec.name)
|
|
7724
8864
|
return true;
|
|
7725
8865
|
}
|
|
7726
8866
|
decode(encoded) {
|
|
7727
8867
|
encoded = encoded || this.encoded;
|
|
7728
|
-
const codec = new Codec$
|
|
8868
|
+
const codec = new Codec$2(encoded);
|
|
7729
8869
|
if (codec.codecBuffer) {
|
|
7730
8870
|
encoded = encoded.slice(codec.codecBuffer.length);
|
|
7731
8871
|
this.name = codec.name;
|
|
@@ -7743,7 +8883,7 @@ let FormatInterface$1 = class FormatInterface extends BasicInterface {
|
|
|
7743
8883
|
encode(decoded) {
|
|
7744
8884
|
let encoded;
|
|
7745
8885
|
decoded = decoded || this.decoded;
|
|
7746
|
-
const codec = new Codec$
|
|
8886
|
+
const codec = new Codec$2(this.name);
|
|
7747
8887
|
if (decoded instanceof Uint8Array)
|
|
7748
8888
|
encoded = decoded;
|
|
7749
8889
|
else
|
|
@@ -7777,7 +8917,13 @@ let FormatInterface$1 = class FormatInterface extends BasicInterface {
|
|
|
7777
8917
|
return `${upper}${this.hashFormat.substring(1, this.hashFormat.length)}`;
|
|
7778
8918
|
}
|
|
7779
8919
|
beforeHashing(decoded) {
|
|
7780
|
-
|
|
8920
|
+
// Avoid copying if not needed
|
|
8921
|
+
if (decoded && Object.prototype.hasOwnProperty.call(decoded, 'hash')) {
|
|
8922
|
+
// Only copy if hash is present
|
|
8923
|
+
const rest = { ...decoded };
|
|
8924
|
+
delete rest.hash;
|
|
8925
|
+
return rest;
|
|
8926
|
+
}
|
|
7781
8927
|
return decoded;
|
|
7782
8928
|
}
|
|
7783
8929
|
/**
|
|
@@ -7803,13 +8949,13 @@ let FormatInterface$1 = class FormatInterface extends BasicInterface {
|
|
|
7803
8949
|
this.encoded = buffer;
|
|
7804
8950
|
return this.hasCodec()
|
|
7805
8951
|
? this.decode()
|
|
7806
|
-
: this.create(JSON.parse(
|
|
8952
|
+
: this.create(JSON.parse(BasicInterface._textDecoder.decode(this.encoded), jsonParseBigInt));
|
|
7807
8953
|
}
|
|
7808
8954
|
fromArrayBuffer(buffer) {
|
|
7809
8955
|
this.encoded = new Uint8Array(buffer, buffer.byteOffset, buffer.byteLength);
|
|
7810
8956
|
return this.hasCodec()
|
|
7811
8957
|
? this.decode()
|
|
7812
|
-
: this.create(JSON.parse(
|
|
8958
|
+
: this.create(JSON.parse(BasicInterface._textDecoder.decode(this.encoded), jsonParseBigInt));
|
|
7813
8959
|
}
|
|
7814
8960
|
/**
|
|
7815
8961
|
* @param {Object} data
|
|
@@ -7829,8 +8975,14 @@ let FormatInterface$1 = class FormatInterface extends BasicInterface {
|
|
|
7829
8975
|
}
|
|
7830
8976
|
};
|
|
7831
8977
|
|
|
8978
|
+
/**
|
|
8979
|
+
* Interface for defining codec formats
|
|
8980
|
+
*/
|
|
7832
8981
|
const FormatInterface = FormatInterface$1;
|
|
7833
|
-
|
|
8982
|
+
/**
|
|
8983
|
+
* Codec utility class
|
|
8984
|
+
*/
|
|
8985
|
+
const Codec = Codec$2;
|
|
7834
8986
|
|
|
7835
8987
|
const BufferToUint8Array = (data) => {
|
|
7836
8988
|
if (data.type === 'Buffer') {
|
|
@@ -8383,7 +9535,7 @@ class Peernet {
|
|
|
8383
9535
|
await getAddress();
|
|
8384
9536
|
this.storePrefix = options.storePrefix;
|
|
8385
9537
|
this.root = options.root;
|
|
8386
|
-
const { RequestMessage, ResponseMessage, PeerMessage, PeerMessageResponse, PeernetMessage, DHTMessage, DHTMessageResponse, DataMessage, DataMessageResponse, PsMessage, ChatMessage, PeernetFile } = await import(/* webpackChunkName: "messages" */ './messages-
|
|
9538
|
+
const { RequestMessage, ResponseMessage, PeerMessage, PeerMessageResponse, PeernetMessage, DHTMessage, DHTMessageResponse, DataMessage, DataMessageResponse, PsMessage, ChatMessage, PeernetFile } = await import(/* webpackChunkName: "messages" */ './messages-2i_0i581.js');
|
|
8387
9539
|
/**
|
|
8388
9540
|
* proto Object containing protos
|
|
8389
9541
|
* @type {Object}
|
|
@@ -8430,7 +9582,7 @@ class Peernet {
|
|
|
8430
9582
|
if (this.#starting || this.#started)
|
|
8431
9583
|
return;
|
|
8432
9584
|
this.#starting = true;
|
|
8433
|
-
const importee = await import('./client-
|
|
9585
|
+
const importee = await import('./client-KrGUTq9O.js');
|
|
8434
9586
|
/**
|
|
8435
9587
|
* @access public
|
|
8436
9588
|
* @type {PeernetClient}
|