@leofcoin/peernet 1.2.8 → 1.2.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,4 @@
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';
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, 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$1(ALPHABET$1);
130
+ base(ALPHABET$1);
131
131
 
132
132
  const ALPHABET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_';
133
- base$1(ALPHABET);
133
+ base(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$2 = (proto, input, compress) => {
7071
+ const encode = (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$2 = (proto, input, compress) => {
7086
7086
  }
7087
7087
  return compress ? pako.deflate(index$3(set)) : index$3(set);
7088
7088
  };
7089
- const decode$4 = (proto, uint8Array, compressed) => {
7089
+ const decode = (proto, uint8Array, compressed) => {
7090
7090
  if (compressed)
7091
7091
  uint8Array = pako.inflate(uint8Array);
7092
7092
  let deconcated = index$4(uint8Array);
@@ -7118,43 +7118,19 @@ const decode$4 = (proto, uint8Array, compressed) => {
7118
7118
  return output;
7119
7119
  };
7120
7120
  var index = {
7121
- encode: encode$2,
7122
- decode: decode$4
7121
+ encode,
7122
+ decode
7123
7123
  };
7124
7124
 
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
7125
  const jsonStringifyBigInt = (key, value) => {
7146
7126
  if (typeof value === 'bigint')
7147
7127
  return { $bigint: value.toString() };
7148
- if (value instanceof Uint8Array)
7149
- return { $uint8array: uint8ArrayToBase64(value) };
7150
7128
  return value;
7151
7129
  };
7152
7130
  const jsonParseBigInt = (key, value) => {
7153
7131
  if (typeof value === 'object' && value) {
7154
7132
  if (value.$bigint)
7155
7133
  return BigInt(value.$bigint);
7156
- if (value.$uint8array)
7157
- return base64ToUint8Array(value.$uint8array);
7158
7134
  }
7159
7135
  return value;
7160
7136
  };
@@ -7522,7 +7498,7 @@ var utils = {
7522
7498
  codecs: codecs
7523
7499
  };
7524
7500
 
7525
- let Codec$2 = class Codec extends BasicInterface {
7501
+ let Codec$1 = class Codec extends BasicInterface {
7526
7502
  codecBuffer;
7527
7503
  codec;
7528
7504
  hashAlg;
@@ -7564,9 +7540,10 @@ let Codec$2 = class Codec extends BasicInterface {
7564
7540
  else
7565
7541
  this.fromString(buffer);
7566
7542
  }
7567
- if (!isNaN(buffer))
7543
+ else if (typeof buffer === 'number') {
7568
7544
  if (utils.getCodec(buffer))
7569
7545
  this.fromCodec(buffer);
7546
+ }
7570
7547
  }
7571
7548
  }
7572
7549
  fromEncoded(encoded) {
@@ -7623,997 +7600,7 @@ let Codec$2 = class Codec extends BasicInterface {
7623
7600
  }
7624
7601
  };
7625
7602
 
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) => {
7603
+ const createHasher = (hashVariant) => {
8617
7604
  switch (hashVariant) {
8618
7605
  case 1:
8619
7606
  return createSHA1();
@@ -8626,44 +7613,23 @@ const getStreamingHasher = async (hashVariant) => {
8626
7613
  case 512:
8627
7614
  return createSHA512();
8628
7615
  default:
8629
- return null;
7616
+ throw new Error(`unsupported hash variant: ${hashVariant}`);
8630
7617
  }
8631
7618
  };
8632
7619
  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
- }
7620
+ const view = buffer instanceof Uint8Array ? buffer : new Uint8Array(buffer);
7621
+ const hasher = await createHasher(hashVariant);
7622
+ hasher.init();
7623
+ hasher.update(view);
7624
+ const digest = hasher.digest('binary');
7625
+ return digest instanceof Uint8Array ? digest : new Uint8Array(digest);
8661
7626
  };
8662
7627
  class CodecHash extends BasicInterface {
8663
7628
  codec;
8664
7629
  discoCodec;
8665
7630
  size;
8666
- constructor(buffer, options) {
7631
+ codecs;
7632
+ constructor(options = { name: 'disco-hash', codecs: {} }) {
8667
7633
  super();
8668
7634
  if (options?.name)
8669
7635
  this.name = options.name;
@@ -8671,42 +7637,24 @@ class CodecHash extends BasicInterface {
8671
7637
  this.name = 'disco-hash';
8672
7638
  if (options?.codecs)
8673
7639
  this.codecs = options.codecs;
8674
- return this.init(buffer);
8675
7640
  }
8676
7641
  async init(uint8Array) {
8677
7642
  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;
7643
+ if (!(uint8Array instanceof Uint8Array)) {
7644
+ throw new Error('CodecHash only supports Uint8Array input');
8686
7645
  }
8687
- if (uint8Array instanceof Uint8Array) {
8688
- this.discoCodec = new Codec$2(uint8Array);
8689
- const name = this.discoCodec.name;
8690
- if (name) {
8691
- this.name = name;
8692
- this.decode(uint8Array);
8693
- }
8694
- else {
8695
- await this.encode(uint8Array);
8696
- }
7646
+ // For large buffers, only check first bytes for codec prefix
7647
+ // Codec prefixes are small (varint encoded), so 100 bytes is more than enough
7648
+ const checkBuffer = uint8Array.length > 100 ? uint8Array.subarray(0, 100) : uint8Array;
7649
+ this.discoCodec = new Codec$1(checkBuffer);
7650
+ const name = this.discoCodec.name;
7651
+ if (name) {
7652
+ this.name = name;
7653
+ this.decode(uint8Array);
8697
7654
  }
8698
- if (typeof uint8Array === 'string') {
8699
- if (this.isHex(uint8Array))
8700
- await this.fromHex(uint8Array);
8701
- if (this.isBase32(uint8Array))
8702
- await this.fromBs32(uint8Array);
8703
- else if (this.isBase58(uint8Array))
8704
- await this.fromBs58(uint8Array);
8705
- else
8706
- throw new Error(`unsupported string ${uint8Array}`);
7655
+ else {
7656
+ await this.encode(uint8Array);
8707
7657
  }
8708
- else if (typeof uint8Array === 'object')
8709
- await this.fromJSON(uint8Array);
8710
7658
  }
8711
7659
  return this;
8712
7660
  }
@@ -8726,16 +7674,12 @@ class CodecHash extends BasicInterface {
8726
7674
  get hash() {
8727
7675
  return this.encoded;
8728
7676
  }
8729
- fromJSON(json) {
8730
- return this.encode(new TextEncoder().encode(JSON.stringify(json, jsonStringifyBigInt)));
8731
- }
8732
7677
  async encode(buffer, name) {
8733
7678
  if (!this.name && name)
8734
7679
  this.name = name;
8735
7680
  if (!buffer)
8736
- buffer = this.buffer;
8737
- this.discoCodec = new Codec$2(this.name);
8738
- this.discoCodec.fromName(this.name);
7681
+ throw new Error('buffer is required for encoding');
7682
+ this.discoCodec = new Codec$1(this.name);
8739
7683
  let hashAlg = this.discoCodec.hashAlg;
8740
7684
  const hashVariant = Number(hashAlg.split('-')[hashAlg.split('-').length - 1]);
8741
7685
  if (hashAlg.includes('dbl')) {
@@ -8752,57 +7696,28 @@ class CodecHash extends BasicInterface {
8752
7696
  this.encoded = uint8Array;
8753
7697
  return this.encoded;
8754
7698
  }
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;
8768
- this.size = this.digest.length;
8769
- this.codec = this.discoCodec.encode();
8770
- this.codec = this.discoCodec.codecBuffer;
8771
- const uint8Array = new Uint8Array(this.digest.length + this.prefix.length);
8772
- uint8Array.set(this.prefix);
8773
- uint8Array.set(this.digest, this.prefix.length);
8774
- this.encoded = uint8Array;
8775
- return this.encoded;
8776
- }
8777
7699
  async validate(buffer) {
8778
- if (Buffer.isBuffer(buffer)) {
8779
- const codec = index$5.decode(buffer);
8780
- if (this.codecs[codec]) {
8781
- this.decode(buffer);
8782
- }
8783
- else {
8784
- await this.encode(buffer);
8785
- }
7700
+ if (!(buffer instanceof Uint8Array)) {
7701
+ throw new Error('CodecHash only supports Uint8Array input');
8786
7702
  }
8787
- if (typeof buffer === 'string') {
8788
- if (this.isHex(buffer))
8789
- this.fromHex(buffer);
8790
- if (this.isBase32(buffer))
8791
- this.fromBs32(buffer);
7703
+ const codec = index$5.decode(buffer);
7704
+ if (this.codecs[codec]) {
7705
+ this.decode(buffer);
7706
+ }
7707
+ else {
7708
+ await this.encode(buffer);
8792
7709
  }
8793
- if (typeof buffer === 'object')
8794
- this.fromJSON(buffer);
8795
7710
  }
8796
7711
  decode(buffer) {
8797
7712
  this.encoded = buffer;
8798
7713
  const codec = index$5.decode(buffer);
8799
- this.discoCodec = new Codec$2(codec, this.codecs);
7714
+ this.discoCodec = new Codec$1(codec, this.codecs);
8800
7715
  // TODO: validate codec
8801
7716
  buffer = buffer.slice(index$5.decode.bytes);
8802
7717
  this.size = index$5.decode(buffer);
8803
7718
  this.digest = buffer.slice(index$5.decode.bytes);
8804
7719
  if (this.digest.length !== this.size) {
8805
- throw new Error(`hash length inconsistent: 0x${this.encoded.toString('hex')}`);
7720
+ throw new Error(`hash length inconsistent: 0x${this.encoded.toString()}`);
8806
7721
  }
8807
7722
  // const discoCodec = new Codec(codec, this.codecs)
8808
7723
  this.name = this.discoCodec.name;
@@ -8859,13 +7774,13 @@ let FormatInterface$1 = class FormatInterface extends BasicInterface {
8859
7774
  hasCodec() {
8860
7775
  if (!this.encoded)
8861
7776
  return false;
8862
- const codec = new Codec$2(this.encoded);
7777
+ const codec = new Codec$1(this.encoded);
8863
7778
  if (codec.name)
8864
7779
  return true;
8865
7780
  }
8866
7781
  decode(encoded) {
8867
7782
  encoded = encoded || this.encoded;
8868
- const codec = new Codec$2(encoded);
7783
+ const codec = new Codec$1(encoded);
8869
7784
  if (codec.codecBuffer) {
8870
7785
  encoded = encoded.slice(codec.codecBuffer.length);
8871
7786
  this.name = codec.name;
@@ -8883,7 +7798,7 @@ let FormatInterface$1 = class FormatInterface extends BasicInterface {
8883
7798
  encode(decoded) {
8884
7799
  let encoded;
8885
7800
  decoded = decoded || this.decoded;
8886
- const codec = new Codec$2(this.name);
7801
+ const codec = new Codec$1(this.name);
8887
7802
  if (decoded instanceof Uint8Array)
8888
7803
  encoded = decoded;
8889
7804
  else
@@ -8916,6 +7831,18 @@ let FormatInterface$1 = class FormatInterface extends BasicInterface {
8916
7831
  const upper = this.hashFormat.charAt(0).toUpperCase();
8917
7832
  return `${upper}${this.hashFormat.substring(1, this.hashFormat.length)}`;
8918
7833
  }
7834
+ /**
7835
+ * Extract content bytes without codec prefix
7836
+ */
7837
+ get contentBytes() {
7838
+ if (!this.encoded)
7839
+ return new Uint8Array(0);
7840
+ const codec = new Codec$1(this.encoded);
7841
+ if (codec.codecBuffer) {
7842
+ return this.encoded.slice(codec.codecBuffer.length);
7843
+ }
7844
+ return this.encoded;
7845
+ }
8919
7846
  beforeHashing(decoded) {
8920
7847
  // Avoid copying if not needed
8921
7848
  if (decoded && Object.prototype.hasOwnProperty.call(decoded, 'hash')) {
@@ -8930,9 +7857,20 @@ let FormatInterface$1 = class FormatInterface extends BasicInterface {
8930
7857
  * @return {PeernetHash}
8931
7858
  */
8932
7859
  get peernetHash() {
7860
+ // Optimize: if decoded has no hash property, contentBytes are already correct
7861
+ // Since protoEncode is deterministic (varint-based), we can use them directly
7862
+ if (this.encoded &&
7863
+ this.decoded &&
7864
+ !Object.prototype.hasOwnProperty.call(this.decoded, 'hash')) {
7865
+ // @ts-ignore
7866
+ const hash = new CodecHash({ name: this.name });
7867
+ return hash.init(this.contentBytes);
7868
+ }
7869
+ // Fallback: must re-encode without hash property
8933
7870
  const decoded = this.beforeHashing({ ...this.decoded });
8934
7871
  // @ts-ignore
8935
- return new CodecHash(decoded, { name: this.name });
7872
+ const hash = new CodecHash({ name: this.name });
7873
+ return hash.init(decoded);
8936
7874
  }
8937
7875
  /**
8938
7876
  * @return {peernetHash}
@@ -8982,7 +7920,7 @@ const FormatInterface = FormatInterface$1;
8982
7920
  /**
8983
7921
  * Codec utility class
8984
7922
  */
8985
- const Codec = Codec$2;
7923
+ const Codec = Codec$1;
8986
7924
 
8987
7925
  const BufferToUint8Array = (data) => {
8988
7926
  if (data.type === 'Buffer') {
@@ -9535,7 +8473,7 @@ class Peernet {
9535
8473
  await getAddress();
9536
8474
  this.storePrefix = options.storePrefix;
9537
8475
  this.root = options.root;
9538
- const { RequestMessage, ResponseMessage, PeerMessage, PeerMessageResponse, PeernetMessage, DHTMessage, DHTMessageResponse, DataMessage, DataMessageResponse, PsMessage, ChatMessage, PeernetFile } = await import(/* webpackChunkName: "messages" */ './messages-2i_0i581.js');
8476
+ const { RequestMessage, ResponseMessage, PeerMessage, PeerMessageResponse, PeernetMessage, DHTMessage, DHTMessageResponse, DataMessage, DataMessageResponse, PsMessage, ChatMessage, PeernetFile } = await import(/* webpackChunkName: "messages" */ './messages-odneBhMk.js');
9539
8477
  /**
9540
8478
  * proto Object containing protos
9541
8479
  * @type {Object}
@@ -9582,7 +8520,7 @@ class Peernet {
9582
8520
  if (this.#starting || this.#started)
9583
8521
  return;
9584
8522
  this.#starting = true;
9585
- const importee = await import('./client-KrGUTq9O.js');
8523
+ const importee = await import('./client-CwKuEJRO.js');
9586
8524
  /**
9587
8525
  * @access public
9588
8526
  * @type {PeernetClient}