@fangorn-network/sdk 2026.6.16 → 2026.7.1-2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (105) hide show
  1. package/README.md +210 -266
  2. package/lib/cli/cli.d.ts +0 -0
  3. package/lib/cli/cli.js +261 -167
  4. package/lib/cli/index.d.ts +13 -651
  5. package/lib/config.d.ts +137 -792
  6. package/lib/config.js +6 -22
  7. package/lib/contracts/data-registry/abi.d.ts +38 -0
  8. package/lib/contracts/data-registry/abi.js +237 -0
  9. package/lib/contracts/data-registry/index.d.ts +81 -0
  10. package/lib/contracts/data-registry/index.js +184 -0
  11. package/lib/contracts/index.d.ts +3 -0
  12. package/lib/contracts/index.js +4 -0
  13. package/lib/crypto/aes.d.ts +26 -0
  14. package/lib/crypto/aes.js +58 -0
  15. package/lib/crypto/encryption.d.ts +53 -0
  16. package/lib/crypto/encryption.js +82 -0
  17. package/lib/{modules/encryption → crypto}/rand.d.ts +1 -1
  18. package/lib/{modules/encryption → crypto}/rand.js +1 -1
  19. package/lib/{modules/encryption → crypto}/types.d.ts +1 -1
  20. package/lib/engine/blockstore.d.ts +24 -0
  21. package/lib/engine/blockstore.js +85 -0
  22. package/lib/engine/index.d.ts +180 -0
  23. package/lib/engine/index.js +367 -0
  24. package/lib/fangorn.d.ts +149 -16
  25. package/lib/fangorn.js +256 -37
  26. package/lib/harness/examples/hackmd/build-markdown-graph.js +17 -0
  27. package/lib/harness/index.d.ts +57 -0
  28. package/lib/harness/index.js +67 -0
  29. package/lib/index.d.ts +2 -2
  30. package/lib/node_modules/.pnpm/@noble_ciphers@1.3.0/node_modules/@noble/ciphers/esm/_polyval.js +218 -0
  31. package/lib/node_modules/.pnpm/@noble_ciphers@1.3.0/node_modules/@noble/ciphers/esm/aes.js +261 -0
  32. package/lib/node_modules/.pnpm/@noble_ciphers@1.3.0/node_modules/@noble/ciphers/esm/utils.js +156 -0
  33. package/lib/node_modules/.pnpm/@noble_curves@1.9.7/node_modules/@noble/curves/esm/abstract/curve.js +299 -0
  34. package/lib/node_modules/.pnpm/@noble_curves@1.9.7/node_modules/@noble/curves/esm/abstract/edwards.js +532 -0
  35. package/lib/node_modules/.pnpm/@noble_curves@1.9.7/node_modules/@noble/curves/esm/abstract/modular.js +344 -0
  36. package/lib/node_modules/.pnpm/@noble_curves@1.9.7/node_modules/@noble/curves/esm/abstract/montgomery.js +137 -0
  37. package/lib/node_modules/.pnpm/@noble_curves@1.9.7/node_modules/@noble/curves/esm/ed25519.js +262 -0
  38. package/lib/node_modules/.pnpm/@noble_curves@1.9.7/node_modules/@noble/curves/esm/utils.js +150 -0
  39. package/lib/providers/storage/car.d.ts +36 -0
  40. package/lib/providers/storage/car.js +80 -0
  41. package/lib/providers/storage/pinata.d.ts +29 -1
  42. package/lib/providers/storage/pinata.js +135 -20
  43. package/lib/providers/storage/types.d.ts +25 -18
  44. package/lib/roles/repo/index.d.ts +56 -0
  45. package/lib/roles/repo/index.js +73 -0
  46. package/lib/test/car_smoke.d.ts +1 -0
  47. package/lib/test/car_smoke.js +81 -0
  48. package/lib/test/cleanup-embeddings-testdata.js +9 -38
  49. package/lib/test/setup-embeddings-testdata.d.ts +4 -6
  50. package/lib/test/setup-embeddings-testdata.js +40 -94
  51. package/lib/test/testbed.d.ts +35 -32
  52. package/lib/test/testbed.js +48 -138
  53. package/lib/types/index.d.ts +3 -19
  54. package/lib/utils/manifest.d.ts +23 -0
  55. package/lib/utils/manifest.js +77 -0
  56. package/package.json +99 -81
  57. package/lib/modules/encryption/actions.d.ts +0 -4
  58. package/lib/modules/encryption/actions.js +0 -18
  59. package/lib/modules/encryption/aes.d.ts +0 -10
  60. package/lib/modules/encryption/aes.js +0 -51
  61. package/lib/registries/datasource-registry/abi.d.ts +0 -186
  62. package/lib/registries/datasource-registry/abi.js +0 -229
  63. package/lib/registries/datasource-registry/index.d.ts +0 -39
  64. package/lib/registries/datasource-registry/index.js +0 -158
  65. package/lib/registries/index.d.ts +0 -3
  66. package/lib/registries/index.js +0 -4
  67. package/lib/registries/schema-registry/abi.d.ts +0 -218
  68. package/lib/registries/schema-registry/abi.js +0 -246
  69. package/lib/registries/schema-registry/index.d.ts +0 -34
  70. package/lib/registries/schema-registry/index.js +0 -179
  71. package/lib/registries/settlement-registry/abi.d.ts +0 -218
  72. package/lib/registries/settlement-registry/abi.js +0 -259
  73. package/lib/registries/settlement-registry/index.d.ts +0 -63
  74. package/lib/registries/settlement-registry/index.js +0 -325
  75. package/lib/registries/settlement-registry/types.d.ts +0 -54
  76. package/lib/registries/settlement-registry/types.js +0 -1
  77. package/lib/roles/consumer/index.d.ts +0 -39
  78. package/lib/roles/consumer/index.js +0 -150
  79. package/lib/roles/consumer/types.d.ts +0 -47
  80. package/lib/roles/consumer/types.js +0 -1
  81. package/lib/roles/publisher/builders/bundle.d.ts +0 -32
  82. package/lib/roles/publisher/builders/bundle.js +0 -137
  83. package/lib/roles/publisher/builders/record-set.d.ts +0 -17
  84. package/lib/roles/publisher/builders/record-set.js +0 -55
  85. package/lib/roles/publisher/builders/types.d.ts +0 -38
  86. package/lib/roles/publisher/builders/types.js +0 -1
  87. package/lib/roles/publisher/builders/utils.d.ts +0 -9
  88. package/lib/roles/publisher/builders/utils.js +0 -29
  89. package/lib/roles/publisher/encryption.d.ts +0 -18
  90. package/lib/roles/publisher/encryption.js +0 -38
  91. package/lib/roles/publisher/index.d.ts +0 -68
  92. package/lib/roles/publisher/index.js +0 -179
  93. package/lib/roles/publisher/types.d.ts +0 -114
  94. package/lib/roles/publisher/types.js +0 -1
  95. package/lib/roles/schema/index.d.ts +0 -44
  96. package/lib/roles/schema/index.js +0 -127
  97. package/lib/roles/schema/types.d.ts +0 -113
  98. package/lib/roles/schema/types.js +0 -1
  99. package/lib/roles/schema/validate.d.ts +0 -9
  100. package/lib/roles/schema/validate.js +0 -128
  101. package/lib/test/publish_mb_bundle.js +0 -296
  102. /package/lib/{modules/encryption/index.d.ts → contracts/data-registry/test.d.ts} +0 -0
  103. /package/lib/{modules/encryption/index.js → contracts/data-registry/test.js} +0 -0
  104. /package/lib/{modules/encryption → crypto}/types.js +0 -0
  105. /package/lib/{test/publish_mb_bundle.d.ts → harness/examples/hackmd/build-markdown-graph.d.ts} +0 -0
@@ -0,0 +1,262 @@
1
+ import { bytesToNumberLE, ensureBytes, equalBytes } from "./utils.js";
2
+ import { Field, isNegativeLE, mod, pow2 } from "./abstract/modular.js";
3
+ import { pippenger } from "./abstract/curve.js";
4
+ import { PrimeEdwardsPoint, twistedEdwards } from "./abstract/edwards.js";
5
+ import { montgomery } from "./abstract/montgomery.js";
6
+ import { sha512 } from "@noble/hashes/sha2.js";
7
+ import { abytes } from "@noble/hashes/utils.js";
8
+
9
+ //#region node_modules/.pnpm/@noble+curves@1.9.7/node_modules/@noble/curves/esm/ed25519.js
10
+ /**
11
+ * ed25519 Twisted Edwards curve with following addons:
12
+ * - X25519 ECDH
13
+ * - Ristretto cofactor elimination
14
+ * - Elligator hash-to-group / point indistinguishability
15
+ * @module
16
+ */
17
+ /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
18
+ const _0n = /* @__PURE__ */ BigInt(0), _1n = BigInt(1), _2n = BigInt(2), _3n = BigInt(3);
19
+ const _5n = BigInt(5), _8n = BigInt(8);
20
+ const ed25519_CURVE_p = BigInt("0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffed");
21
+ const ed25519_CURVE = /* @__PURE__ */ (() => ({
22
+ p: ed25519_CURVE_p,
23
+ n: BigInt("0x1000000000000000000000000000000014def9dea2f79cd65812631a5cf5d3ed"),
24
+ h: _8n,
25
+ a: BigInt("0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffec"),
26
+ d: BigInt("0x52036cee2b6ffe738cc740797779e89800700a4d4141d8ab75eb4dca135978a3"),
27
+ Gx: BigInt("0x216936d3cd6e53fec0a4e231fdd6dc5c692cc7609525a7b2c9562d608f25d51a"),
28
+ Gy: BigInt("0x6666666666666666666666666666666666666666666666666666666666666658")
29
+ }))();
30
+ function ed25519_pow_2_252_3(x) {
31
+ const _10n = BigInt(10), _20n = BigInt(20), _40n = BigInt(40), _80n = BigInt(80);
32
+ const P = ed25519_CURVE_p;
33
+ const b2 = x * x % P * x % P;
34
+ const b5 = pow2(pow2(b2, _2n, P) * b2 % P, _1n, P) * x % P;
35
+ const b10 = pow2(b5, _5n, P) * b5 % P;
36
+ const b20 = pow2(b10, _10n, P) * b10 % P;
37
+ const b40 = pow2(b20, _20n, P) * b20 % P;
38
+ const b80 = pow2(b40, _40n, P) * b40 % P;
39
+ return {
40
+ pow_p_5_8: pow2(pow2(pow2(pow2(b80, _80n, P) * b80 % P, _80n, P) * b80 % P, _10n, P) * b10 % P, _2n, P) * x % P,
41
+ b2
42
+ };
43
+ }
44
+ function adjustScalarBytes(bytes) {
45
+ bytes[0] &= 248;
46
+ bytes[31] &= 127;
47
+ bytes[31] |= 64;
48
+ return bytes;
49
+ }
50
+ const ED25519_SQRT_M1 = /* @__PURE__ */ BigInt("19681161376707505956807079304988542015446066515923890162744021073123829784752");
51
+ function uvRatio(u, v) {
52
+ const P = ed25519_CURVE_p;
53
+ const v3 = mod(v * v * v, P);
54
+ const pow = ed25519_pow_2_252_3(u * mod(v3 * v3 * v, P)).pow_p_5_8;
55
+ let x = mod(u * v3 * pow, P);
56
+ const vx2 = mod(v * x * x, P);
57
+ const root1 = x;
58
+ const root2 = mod(x * ED25519_SQRT_M1, P);
59
+ const useRoot1 = vx2 === u;
60
+ const useRoot2 = vx2 === mod(-u, P);
61
+ const noRoot = vx2 === mod(-u * ED25519_SQRT_M1, P);
62
+ if (useRoot1) x = root1;
63
+ if (useRoot2 || noRoot) x = root2;
64
+ if (isNegativeLE(x, P)) x = mod(-x, P);
65
+ return {
66
+ isValid: useRoot1 || useRoot2,
67
+ value: x
68
+ };
69
+ }
70
+ const Fp = /* @__PURE__ */ (() => Field(ed25519_CURVE.p, { isLE: true }))();
71
+ const Fn = /* @__PURE__ */ (() => Field(ed25519_CURVE.n, { isLE: true }))();
72
+ const ed25519Defaults = /* @__PURE__ */ (() => ({
73
+ ...ed25519_CURVE,
74
+ Fp,
75
+ hash: sha512,
76
+ adjustScalarBytes,
77
+ uvRatio
78
+ }))();
79
+ /**
80
+ * ed25519 curve with EdDSA signatures.
81
+ * @example
82
+ * import { ed25519 } from '@noble/curves/ed25519';
83
+ * const { secretKey, publicKey } = ed25519.keygen();
84
+ * const msg = new TextEncoder().encode('hello');
85
+ * const sig = ed25519.sign(msg, priv);
86
+ * ed25519.verify(sig, msg, pub); // Default mode: follows ZIP215
87
+ * ed25519.verify(sig, msg, pub, { zip215: false }); // RFC8032 / FIPS 186-5
88
+ */
89
+ const ed25519 = /* @__PURE__ */ (() => twistedEdwards(ed25519Defaults))();
90
+ /**
91
+ * ECDH using curve25519 aka x25519.
92
+ * @example
93
+ * import { x25519 } from '@noble/curves/ed25519';
94
+ * const priv = 'a546e36bf0527c9d3b16154b82465edd62144c0ac1fc5a18506a2244ba449ac4';
95
+ * const pub = 'e6db6867583030db3594c1a424b15f7c726624ec26b3353b10a903a6d0ab1c4c';
96
+ * x25519.getSharedSecret(priv, pub) === x25519.scalarMult(priv, pub); // aliases
97
+ * x25519.getPublicKey(priv) === x25519.scalarMultBase(priv);
98
+ * x25519.getPublicKey(x25519.utils.randomSecretKey());
99
+ */
100
+ const x25519 = /* @__PURE__ */ (() => {
101
+ const P = Fp.ORDER;
102
+ return montgomery({
103
+ P,
104
+ type: "x25519",
105
+ powPminus2: (x) => {
106
+ const { pow_p_5_8, b2 } = ed25519_pow_2_252_3(x);
107
+ return mod(pow2(pow_p_5_8, _3n, P) * b2, P);
108
+ },
109
+ adjustScalarBytes
110
+ });
111
+ })();
112
+ const SQRT_M1 = ED25519_SQRT_M1;
113
+ const SQRT_AD_MINUS_ONE = /* @__PURE__ */ BigInt("25063068953384623474111414158702152701244531502492656460079210482610430750235");
114
+ const INVSQRT_A_MINUS_D = /* @__PURE__ */ BigInt("54469307008909316920995813868745141605393597292927456921205312896311721017578");
115
+ const ONE_MINUS_D_SQ = /* @__PURE__ */ BigInt("1159843021668779879193775521855586647937357759715417654439879720876111806838");
116
+ const D_MINUS_ONE_SQ = /* @__PURE__ */ BigInt("40440834346308536858101042469323190826248399146238708352240133220865137265952");
117
+ const invertSqrt = (number) => uvRatio(_1n, number);
118
+ const MAX_255B = /* @__PURE__ */ BigInt("0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
119
+ const bytes255ToNumberLE = (bytes) => ed25519.Point.Fp.create(bytesToNumberLE(bytes) & MAX_255B);
120
+ /**
121
+ * Computes Elligator map for Ristretto255.
122
+ * Described in [RFC9380](https://www.rfc-editor.org/rfc/rfc9380#appendix-B) and on
123
+ * the [website](https://ristretto.group/formulas/elligator.html).
124
+ */
125
+ function calcElligatorRistrettoMap(r0) {
126
+ const { d } = ed25519_CURVE;
127
+ const P = ed25519_CURVE_p;
128
+ const mod$1 = (n) => Fp.create(n);
129
+ const r = mod$1(SQRT_M1 * r0 * r0);
130
+ const Ns = mod$1((r + _1n) * ONE_MINUS_D_SQ);
131
+ let c = BigInt(-1);
132
+ const D = mod$1((c - d * r) * mod$1(r + d));
133
+ let { isValid: Ns_D_is_sq, value: s } = uvRatio(Ns, D);
134
+ let s_ = mod$1(s * r0);
135
+ if (!isNegativeLE(s_, P)) s_ = mod$1(-s_);
136
+ if (!Ns_D_is_sq) s = s_;
137
+ if (!Ns_D_is_sq) c = r;
138
+ const Nt = mod$1(c * (r - _1n) * D_MINUS_ONE_SQ - D);
139
+ const s2 = s * s;
140
+ const W0 = mod$1((s + s) * D);
141
+ const W1 = mod$1(Nt * SQRT_AD_MINUS_ONE);
142
+ const W2 = mod$1(_1n - s2);
143
+ const W3 = mod$1(_1n + s2);
144
+ return new ed25519.Point(mod$1(W0 * W3), mod$1(W2 * W1), mod$1(W1 * W3), mod$1(W0 * W2));
145
+ }
146
+ function ristretto255_map(bytes) {
147
+ abytes(bytes, 64);
148
+ const R1 = calcElligatorRistrettoMap(bytes255ToNumberLE(bytes.subarray(0, 32)));
149
+ const R2 = calcElligatorRistrettoMap(bytes255ToNumberLE(bytes.subarray(32, 64)));
150
+ return new _RistrettoPoint(R1.add(R2));
151
+ }
152
+ /**
153
+ * Wrapper over Edwards Point for ristretto255.
154
+ *
155
+ * Each ed25519/ExtendedPoint has 8 different equivalent points. This can be
156
+ * a source of bugs for protocols like ring signatures. Ristretto was created to solve this.
157
+ * Ristretto point operates in X:Y:Z:T extended coordinates like ExtendedPoint,
158
+ * but it should work in its own namespace: do not combine those two.
159
+ * See [RFC9496](https://www.rfc-editor.org/rfc/rfc9496).
160
+ */
161
+ var _RistrettoPoint = class _RistrettoPoint extends PrimeEdwardsPoint {
162
+ constructor(ep) {
163
+ super(ep);
164
+ }
165
+ static fromAffine(ap) {
166
+ return new _RistrettoPoint(ed25519.Point.fromAffine(ap));
167
+ }
168
+ assertSame(other) {
169
+ if (!(other instanceof _RistrettoPoint)) throw new Error("RistrettoPoint expected");
170
+ }
171
+ init(ep) {
172
+ return new _RistrettoPoint(ep);
173
+ }
174
+ /** @deprecated use `import { ristretto255_hasher } from '@noble/curves/ed25519.js';` */
175
+ static hashToCurve(hex) {
176
+ return ristretto255_map(ensureBytes("ristrettoHash", hex, 64));
177
+ }
178
+ static fromBytes(bytes) {
179
+ abytes(bytes, 32);
180
+ const { a, d } = ed25519_CURVE;
181
+ const P = ed25519_CURVE_p;
182
+ const mod$1 = (n) => Fp.create(n);
183
+ const s = bytes255ToNumberLE(bytes);
184
+ if (!equalBytes(Fp.toBytes(s), bytes) || isNegativeLE(s, P)) throw new Error("invalid ristretto255 encoding 1");
185
+ const s2 = mod$1(s * s);
186
+ const u1 = mod$1(_1n + a * s2);
187
+ const u2 = mod$1(_1n - a * s2);
188
+ const u1_2 = mod$1(u1 * u1);
189
+ const u2_2 = mod$1(u2 * u2);
190
+ const v = mod$1(a * d * u1_2 - u2_2);
191
+ const { isValid, value: I } = invertSqrt(mod$1(v * u2_2));
192
+ const Dx = mod$1(I * u2);
193
+ const Dy = mod$1(I * Dx * v);
194
+ let x = mod$1((s + s) * Dx);
195
+ if (isNegativeLE(x, P)) x = mod$1(-x);
196
+ const y = mod$1(u1 * Dy);
197
+ const t = mod$1(x * y);
198
+ if (!isValid || isNegativeLE(t, P) || y === _0n) throw new Error("invalid ristretto255 encoding 2");
199
+ return new _RistrettoPoint(new ed25519.Point(x, y, _1n, t));
200
+ }
201
+ /**
202
+ * Converts ristretto-encoded string to ristretto point.
203
+ * Described in [RFC9496](https://www.rfc-editor.org/rfc/rfc9496#name-decode).
204
+ * @param hex Ristretto-encoded 32 bytes. Not every 32-byte string is valid ristretto encoding
205
+ */
206
+ static fromHex(hex) {
207
+ return _RistrettoPoint.fromBytes(ensureBytes("ristrettoHex", hex, 32));
208
+ }
209
+ static msm(points, scalars) {
210
+ return pippenger(_RistrettoPoint, ed25519.Point.Fn, points, scalars);
211
+ }
212
+ /**
213
+ * Encodes ristretto point to Uint8Array.
214
+ * Described in [RFC9496](https://www.rfc-editor.org/rfc/rfc9496#name-encode).
215
+ */
216
+ toBytes() {
217
+ let { X, Y, Z, T } = this.ep;
218
+ const P = ed25519_CURVE_p;
219
+ const mod$1 = (n) => Fp.create(n);
220
+ const u1 = mod$1(mod$1(Z + Y) * mod$1(Z - Y));
221
+ const u2 = mod$1(X * Y);
222
+ const { value: invsqrt } = invertSqrt(mod$1(u1 * mod$1(u2 * u2)));
223
+ const D1 = mod$1(invsqrt * u1);
224
+ const D2 = mod$1(invsqrt * u2);
225
+ const zInv = mod$1(D1 * D2 * T);
226
+ let D;
227
+ if (isNegativeLE(T * zInv, P)) {
228
+ let _x = mod$1(Y * SQRT_M1);
229
+ let _y = mod$1(X * SQRT_M1);
230
+ X = _x;
231
+ Y = _y;
232
+ D = mod$1(D1 * INVSQRT_A_MINUS_D);
233
+ } else D = D2;
234
+ if (isNegativeLE(X * zInv, P)) Y = mod$1(-Y);
235
+ let s = mod$1((Z - Y) * D);
236
+ if (isNegativeLE(s, P)) s = mod$1(-s);
237
+ return Fp.toBytes(s);
238
+ }
239
+ /**
240
+ * Compares two Ristretto points.
241
+ * Described in [RFC9496](https://www.rfc-editor.org/rfc/rfc9496#name-equals).
242
+ */
243
+ equals(other) {
244
+ this.assertSame(other);
245
+ const { X: X1, Y: Y1 } = this.ep;
246
+ const { X: X2, Y: Y2 } = other.ep;
247
+ const mod$1 = (n) => Fp.create(n);
248
+ const one = mod$1(X1 * Y2) === mod$1(Y1 * X2);
249
+ const two = mod$1(Y1 * Y2) === mod$1(X1 * X2);
250
+ return one || two;
251
+ }
252
+ is0() {
253
+ return this.equals(_RistrettoPoint.ZERO);
254
+ }
255
+ };
256
+ _RistrettoPoint.BASE = /* @__PURE__ */ (() => new _RistrettoPoint(ed25519.Point.BASE))();
257
+ _RistrettoPoint.ZERO = /* @__PURE__ */ (() => new _RistrettoPoint(ed25519.Point.ZERO))();
258
+ _RistrettoPoint.Fp = /* @__PURE__ */ (() => Fp)();
259
+ _RistrettoPoint.Fn = /* @__PURE__ */ (() => Fn)();
260
+
261
+ //#endregion
262
+ export { x25519 };
@@ -0,0 +1,150 @@
1
+ import { abytes, abytes as abytes$1, anumber, bytesToHex, bytesToHex as bytesToHex$1, concatBytes as concatBytes$1, hexToBytes, isBytes, isBytes as isBytes$1, randomBytes } from "@noble/hashes/utils.js";
2
+
3
+ //#region node_modules/.pnpm/@noble+curves@1.9.7/node_modules/@noble/curves/esm/utils.js
4
+ /**
5
+ * Hex, bytes and number utilities.
6
+ * @module
7
+ */
8
+ /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
9
+ const _0n = /* @__PURE__ */ BigInt(0);
10
+ const _1n = /* @__PURE__ */ BigInt(1);
11
+ function _abool2(value, title = "") {
12
+ if (typeof value !== "boolean") {
13
+ const prefix = title && `"${title}"`;
14
+ throw new Error(prefix + "expected boolean, got type=" + typeof value);
15
+ }
16
+ return value;
17
+ }
18
+ /** Asserts something is Uint8Array. */
19
+ function _abytes2(value, length, title = "") {
20
+ const bytes = isBytes(value);
21
+ const len = value?.length;
22
+ const needsLen = length !== void 0;
23
+ if (!bytes || needsLen && len !== length) {
24
+ const prefix = title && `"${title}" `;
25
+ const ofLen = needsLen ? ` of length ${length}` : "";
26
+ const got = bytes ? `length=${len}` : `type=${typeof value}`;
27
+ throw new Error(prefix + "expected Uint8Array" + ofLen + ", got " + got);
28
+ }
29
+ return value;
30
+ }
31
+ function hexToNumber(hex) {
32
+ if (typeof hex !== "string") throw new Error("hex string expected, got " + typeof hex);
33
+ return hex === "" ? _0n : BigInt("0x" + hex);
34
+ }
35
+ function bytesToNumberBE(bytes) {
36
+ return hexToNumber(bytesToHex(bytes));
37
+ }
38
+ function bytesToNumberLE(bytes) {
39
+ abytes(bytes);
40
+ return hexToNumber(bytesToHex(Uint8Array.from(bytes).reverse()));
41
+ }
42
+ function numberToBytesBE(n, len) {
43
+ return hexToBytes(n.toString(16).padStart(len * 2, "0"));
44
+ }
45
+ function numberToBytesLE(n, len) {
46
+ return numberToBytesBE(n, len).reverse();
47
+ }
48
+ /**
49
+ * Takes hex string or Uint8Array, converts to Uint8Array.
50
+ * Validates output length.
51
+ * Will throw error for other types.
52
+ * @param title descriptive title for an error e.g. 'secret key'
53
+ * @param hex hex string or Uint8Array
54
+ * @param expectedLength optional, will compare to result array's length
55
+ * @returns
56
+ */
57
+ function ensureBytes(title, hex, expectedLength) {
58
+ let res;
59
+ if (typeof hex === "string") try {
60
+ res = hexToBytes(hex);
61
+ } catch (e) {
62
+ throw new Error(title + " must be hex string or Uint8Array, cause: " + e);
63
+ }
64
+ else if (isBytes(hex)) res = Uint8Array.from(hex);
65
+ else throw new Error(title + " must be hex string or Uint8Array");
66
+ const len = res.length;
67
+ if (typeof expectedLength === "number" && len !== expectedLength) throw new Error(title + " of length " + expectedLength + " expected, got " + len);
68
+ return res;
69
+ }
70
+ function equalBytes(a, b) {
71
+ if (a.length !== b.length) return false;
72
+ let diff = 0;
73
+ for (let i = 0; i < a.length; i++) diff |= a[i] ^ b[i];
74
+ return diff === 0;
75
+ }
76
+ /**
77
+ * Copies Uint8Array. We can't use u8a.slice(), because u8a can be Buffer,
78
+ * and Buffer#slice creates mutable copy. Never use Buffers!
79
+ */
80
+ function copyBytes(bytes) {
81
+ return Uint8Array.from(bytes);
82
+ }
83
+ /**
84
+ * @example utf8ToBytes('abc') // new Uint8Array([97, 98, 99])
85
+ */
86
+ /**
87
+ * Converts bytes to string using UTF8 encoding.
88
+ * @example bytesToUtf8(Uint8Array.from([97, 98, 99])) // 'abc'
89
+ */
90
+ const isPosBig = (n) => typeof n === "bigint" && _0n <= n;
91
+ function inRange(n, min, max) {
92
+ return isPosBig(n) && isPosBig(min) && isPosBig(max) && min <= n && n < max;
93
+ }
94
+ /**
95
+ * Asserts min <= n < max. NOTE: It's < max and not <= max.
96
+ * @example
97
+ * aInRange('x', x, 1n, 256n); // would assume x is in (1n..255n)
98
+ */
99
+ function aInRange(title, n, min, max) {
100
+ if (!inRange(n, min, max)) throw new Error("expected valid " + title + ": " + min + " <= n < " + max + ", got " + n);
101
+ }
102
+ /**
103
+ * Calculates amount of bits in a bigint.
104
+ * Same as `n.toString(2).length`
105
+ * TODO: merge with nLength in modular
106
+ */
107
+ function bitLen(n) {
108
+ let len;
109
+ for (len = 0; n > _0n; n >>= _1n, len += 1);
110
+ return len;
111
+ }
112
+ /**
113
+ * Calculate mask for N bits. Not using ** operator with bigints because of old engines.
114
+ * Same as BigInt(`0b${Array(i).fill('1').join('')}`)
115
+ */
116
+ const bitMask = (n) => (_1n << BigInt(n)) - _1n;
117
+ function _validateObject(object, fields, optFields = {}) {
118
+ if (!object || typeof object !== "object") throw new Error("expected valid options object");
119
+ function checkField(fieldName, expectedType, isOpt) {
120
+ const val = object[fieldName];
121
+ if (isOpt && val === void 0) return;
122
+ const current = typeof val;
123
+ if (current !== expectedType || val === null) throw new Error(`param "${fieldName}" is invalid: expected ${expectedType}, got ${current}`);
124
+ }
125
+ Object.entries(fields).forEach(([k, v]) => checkField(k, v, false));
126
+ Object.entries(optFields).forEach(([k, v]) => checkField(k, v, true));
127
+ }
128
+ /**
129
+ * throws not implemented error
130
+ */
131
+ const notImplemented = () => {
132
+ throw new Error("not implemented");
133
+ };
134
+ /**
135
+ * Memoizes (caches) computation result.
136
+ * Uses WeakMap: the value is going auto-cleaned by GC after last reference is removed.
137
+ */
138
+ function memoized(fn) {
139
+ const map = /* @__PURE__ */ new WeakMap();
140
+ return (arg, ...args) => {
141
+ const val = map.get(arg);
142
+ if (val !== void 0) return val;
143
+ const computed = fn(arg, ...args);
144
+ map.set(arg, computed);
145
+ return computed;
146
+ };
147
+ }
148
+
149
+ //#endregion
150
+ export { _abool2, _abytes2, _validateObject, aInRange, abytes$1 as abytes, anumber, bitLen, bitMask, bytesToHex$1 as bytesToHex, bytesToNumberBE, bytesToNumberLE, concatBytes$1 as concatBytes, copyBytes, ensureBytes, equalBytes, isBytes$1 as isBytes, memoized, notImplemented, numberToBytesBE, numberToBytesLE, randomBytes };
@@ -0,0 +1,36 @@
1
+ import { CID } from "multiformats/cid";
2
+
3
+ //#region src/providers/storage/car.d.ts
4
+ /** Result of packing items into a single CAR. */
5
+ interface PackedCar {
6
+ /** The CAR bytes, ready to upload. */
7
+ bytes: Uint8Array;
8
+ /** Directory root CID — the single root Pinata recursively pins. */
9
+ root: CID;
10
+ /**
11
+ * name → `ipfs://<root>/<name>` path URI. We address chunks by path through
12
+ * the registered root, NOT by bare sub-block CID: a CAR upload registers only
13
+ * the root as a "file", so a Pinata dedicated gateway 403s standalone sub-CIDs
14
+ * but serves `<root>/<name>` (the root + its UnixFS directory entries). The
15
+ * path still resolves deterministically to the file's content.
16
+ */
17
+ uriByName: Record<string, string>;
18
+ }
19
+ /**
20
+ * Pack named payloads into a SINGLE CAR with a UnixFS directory root.
21
+ *
22
+ * Each item is UnixFS-encoded locally (so its real CID is known without a round
23
+ * trip) and linked under one directory; that directory is the CAR's lone root,
24
+ * so a `.car()` upload recursively pins every file block. Retrieval then uses
25
+ * each item's standalone CID directly — the directory wrapper exists only to
26
+ * give Pinata one thing to pin.
27
+ *
28
+ * Memory is ~2× the packed size (blocks held, then serialized to CAR bytes), so
29
+ * callers must bound how much they hand to one call (see CAR_GROUP_* in publish).
30
+ */
31
+ declare function packCar(items: {
32
+ data: unknown;
33
+ name: string;
34
+ }[]): Promise<PackedCar>;
35
+ //#endregion
36
+ export { PackedCar, packCar };
@@ -0,0 +1,80 @@
1
+ import { serialize } from "./utils.js";
2
+ import * as UnixFS from "@ipld/unixfs";
3
+ import { CarWriter } from "@ipld/car";
4
+
5
+ //#region src/providers/storage/car.ts
6
+ const enc = new TextEncoder();
7
+ /** Filenames inside the CAR's UnixFS directory; ':' is legal but we keep it tidy. */
8
+ function sanitize(name) {
9
+ return name.replace(/[/\x00]/g, "_");
10
+ }
11
+ /**
12
+ * Pack named payloads into a SINGLE CAR with a UnixFS directory root.
13
+ *
14
+ * Each item is UnixFS-encoded locally (so its real CID is known without a round
15
+ * trip) and linked under one directory; that directory is the CAR's lone root,
16
+ * so a `.car()` upload recursively pins every file block. Retrieval then uses
17
+ * each item's standalone CID directly — the directory wrapper exists only to
18
+ * give Pinata one thing to pin.
19
+ *
20
+ * Memory is ~2× the packed size (blocks held, then serialized to CAR bytes), so
21
+ * callers must bound how much they hand to one call (see CAR_GROUP_* in publish).
22
+ */
23
+ async function packCar(items) {
24
+ const blocks = [];
25
+ const writable = new WritableStream({ write(b) {
26
+ blocks.push(b);
27
+ } });
28
+ const writer = UnixFS.createWriter({ writable });
29
+ const dir = UnixFS.createDirectoryWriter(writer);
30
+ const dirNames = [];
31
+ for (const { data, name } of items) {
32
+ const bytes$1 = enc.encode(serialize(data));
33
+ const file = UnixFS.createFileWriter(writer);
34
+ await file.write(bytes$1);
35
+ const link = await file.close();
36
+ const entry = sanitize(name);
37
+ dir.set(entry, link);
38
+ dirNames.push({
39
+ name,
40
+ entry
41
+ });
42
+ }
43
+ const dirLink = await dir.close();
44
+ await writer.close();
45
+ const root = dirLink.cid;
46
+ const bytes = await writeCarBytes(root, blocks);
47
+ const uriByName = {};
48
+ for (const { name, entry } of dirNames) uriByName[name] = `ipfs://${root.toString()}/${entry}`;
49
+ return {
50
+ bytes,
51
+ root,
52
+ uriByName
53
+ };
54
+ }
55
+ /** Serialize blocks into CAR v1 bytes under a single known root. */
56
+ async function writeCarBytes(root, blocks) {
57
+ const { writer, out } = CarWriter.create([root]);
58
+ const collected = [];
59
+ const sink = (async () => {
60
+ for await (const part of out) collected.push(part);
61
+ })();
62
+ for (let i = 0; i < blocks.length; i++) {
63
+ await writer.put(blocks[i]);
64
+ blocks[i] = void 0;
65
+ }
66
+ await writer.close();
67
+ await sink;
68
+ let total = 0;
69
+ for (const p of collected) total += p.length;
70
+ const buf = new Uint8Array(total);
71
+ let off = 0;
72
+ for (const p of collected) {
73
+ buf.set(p, off);
74
+ off += p.length;
75
+ }
76
+ return buf;
77
+ }
78
+
79
+ //#endregion
80
+ export { packCar };
@@ -1,4 +1,4 @@
1
- import { MetadataStorage, StorageMeta } from "./types.js";
1
+ import { MetadataStorage, RawBlock, StorageMeta } from "./types.js";
2
2
 
3
3
  //#region src/providers/storage/pinata.d.ts
4
4
  declare class PinataBackend implements MetadataStorage {
@@ -6,12 +6,40 @@ declare class PinataBackend implements MetadataStorage {
6
6
  private gateway;
7
7
  constructor(pinataJwt: string, pinataGateway: string);
8
8
  put(data: unknown, meta?: StorageMeta): Promise<string>;
9
+ update(data: unknown, meta?: StorageMeta): Promise<string>;
10
+ /**
11
+ * Pack every item into ONE locally-built CAR and upload it as a single pin,
12
+ * collapsing N uploads into one request. Returns each item's `ipfs://<root>/<name>`
13
+ * path URI: a CAR upload registers only the directory root as a "file", so the
14
+ * dedicated gateway serves chunks by path through that root (bare sub-block CIDs
15
+ * 403). Callers must bound batch size — the CAR is built in memory.
16
+ */
9
17
  putMany(items: {
10
18
  data: unknown;
11
19
  name: string;
12
20
  }[]): Promise<Record<string, string>>;
21
+ /**
22
+ * Upload precomputed (cid, bytes) blocks, each as its own plain-file pin,
23
+ * so every block is individually addressable on the dedicated gateway
24
+ * (see RAW_CODE comment above for why this can't go through `.car()`).
25
+ * A single pail commit can touch many shard blocks at once (pail shards
26
+ * key-by-character over shared prefixes, so one write under a namespace
27
+ * that already has sibling keys can split into a whole chain of shard
28
+ * blocks) — upload with bounded concurrency rather than one at a time.
29
+ */
30
+ putBlocks(blocks: RawBlock[]): Promise<void>;
13
31
  get<T>(uri: string): Promise<T>;
32
+ /** Raw bytes for a block CID, with no decoding — what blockstore traversal needs. */
33
+ getRawBlock(uri: string): Promise<Uint8Array>;
34
+ /**
35
+ * Shared fetch-with-retry: resolves the raw-codec sibling CID for blocks
36
+ * uploaded via putBlocks(), fetches from the dedicated gateway, and returns
37
+ * the exact bytes (never pre-decoded) plus enough context for get<T>() to
38
+ * decide how to decode them.
39
+ */
40
+ private fetchRaw;
14
41
  static getStatic<T>(uri: string, gateway?: string): Promise<T>;
42
+ getCidByStateRoot(stateRoot: string, retries?: number, delay?: number): Promise<string>;
15
43
  delete(uri: string): Promise<void>;
16
44
  }
17
45
  //#endregion