@unicitylabs/sphere-sdk 0.1.3 → 0.1.5

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.
@@ -36,48 +36,43 @@ __export(ipfs_exports, {
36
36
  module.exports = __toCommonJS(ipfs_exports);
37
37
 
38
38
  // constants.ts
39
- var STORAGE_PREFIX = "sphere_";
40
- var STORAGE_KEYS = {
39
+ var STORAGE_KEYS_GLOBAL = {
41
40
  /** Encrypted BIP39 mnemonic */
42
- MNEMONIC: `${STORAGE_PREFIX}mnemonic`,
41
+ MNEMONIC: "mnemonic",
43
42
  /** Encrypted master private key */
44
- MASTER_KEY: `${STORAGE_PREFIX}master_key`,
43
+ MASTER_KEY: "master_key",
45
44
  /** BIP32 chain code */
46
- CHAIN_CODE: `${STORAGE_PREFIX}chain_code`,
45
+ CHAIN_CODE: "chain_code",
47
46
  /** HD derivation path (full path like m/44'/0'/0'/0/0) */
48
- DERIVATION_PATH: `${STORAGE_PREFIX}derivation_path`,
47
+ DERIVATION_PATH: "derivation_path",
49
48
  /** Base derivation path (like m/44'/0'/0' without chain/index) */
50
- BASE_PATH: `${STORAGE_PREFIX}base_path`,
49
+ BASE_PATH: "base_path",
51
50
  /** Derivation mode: bip32, wif_hmac, legacy_hmac */
52
- DERIVATION_MODE: `${STORAGE_PREFIX}derivation_mode`,
51
+ DERIVATION_MODE: "derivation_mode",
53
52
  /** Wallet source: mnemonic, file, unknown */
54
- WALLET_SOURCE: `${STORAGE_PREFIX}wallet_source`,
53
+ WALLET_SOURCE: "wallet_source",
55
54
  /** Wallet existence flag */
56
- WALLET_EXISTS: `${STORAGE_PREFIX}wallet_exists`,
57
- /** Registered nametag (legacy - single address) */
58
- NAMETAG: `${STORAGE_PREFIX}nametag`,
55
+ WALLET_EXISTS: "wallet_exists",
59
56
  /** Current active address index */
60
- CURRENT_ADDRESS_INDEX: `${STORAGE_PREFIX}current_address_index`,
61
- /** Address nametags map (JSON: { "0": "alice", "1": "bob" }) */
62
- ADDRESS_NAMETAGS: `${STORAGE_PREFIX}address_nametags`,
63
- /** Token data */
64
- TOKENS: `${STORAGE_PREFIX}tokens`,
65
- /** Pending transfers */
66
- PENDING_TRANSFERS: `${STORAGE_PREFIX}pending_transfers`,
67
- /** Transfer outbox */
68
- OUTBOX: `${STORAGE_PREFIX}outbox`,
69
- /** Conversations */
70
- CONVERSATIONS: `${STORAGE_PREFIX}conversations`,
71
- /** Messages */
72
- MESSAGES: `${STORAGE_PREFIX}messages`,
73
- /** Transaction history */
74
- TRANSACTION_HISTORY: `${STORAGE_PREFIX}transaction_history`,
75
- /** Archived tokens (spent token history) */
76
- ARCHIVED_TOKENS: `${STORAGE_PREFIX}archived_tokens`,
77
- /** Tombstones (records of deleted/spent tokens) */
78
- TOMBSTONES: `${STORAGE_PREFIX}tombstones`,
79
- /** Forked tokens (alternative histories) */
80
- FORKED_TOKENS: `${STORAGE_PREFIX}forked_tokens`
57
+ CURRENT_ADDRESS_INDEX: "current_address_index",
58
+ /** Index of address nametags (JSON: { "0": "alice", "1": "bob" }) - for discovery */
59
+ ADDRESS_NAMETAGS: "address_nametags"
60
+ };
61
+ var STORAGE_KEYS_ADDRESS = {
62
+ /** Pending transfers for this address */
63
+ PENDING_TRANSFERS: "pending_transfers",
64
+ /** Transfer outbox for this address */
65
+ OUTBOX: "outbox",
66
+ /** Conversations for this address */
67
+ CONVERSATIONS: "conversations",
68
+ /** Messages for this address */
69
+ MESSAGES: "messages",
70
+ /** Transaction history for this address */
71
+ TRANSACTION_HISTORY: "transaction_history"
72
+ };
73
+ var STORAGE_KEYS = {
74
+ ...STORAGE_KEYS_GLOBAL,
75
+ ...STORAGE_KEYS_ADDRESS
81
76
  };
82
77
  var DEFAULT_IPFS_GATEWAYS = [
83
78
  "https://ipfs.unicity.network",
@@ -93,9 +88,443 @@ var DEFAULT_IPFS_BOOTSTRAP_PEERS = [
93
88
  var DEFAULT_BASE_PATH = "m/44'/0'/0'";
94
89
  var DEFAULT_DERIVATION_PATH = `${DEFAULT_BASE_PATH}/0/0`;
95
90
 
91
+ // node_modules/@noble/hashes/utils.js
92
+ function isBytes(a) {
93
+ return a instanceof Uint8Array || ArrayBuffer.isView(a) && a.constructor.name === "Uint8Array";
94
+ }
95
+ function anumber(n, title = "") {
96
+ if (!Number.isSafeInteger(n) || n < 0) {
97
+ const prefix = title && `"${title}" `;
98
+ throw new Error(`${prefix}expected integer >= 0, got ${n}`);
99
+ }
100
+ }
101
+ function abytes(value, length, title = "") {
102
+ const bytes = isBytes(value);
103
+ const len = value?.length;
104
+ const needsLen = length !== void 0;
105
+ if (!bytes || needsLen && len !== length) {
106
+ const prefix = title && `"${title}" `;
107
+ const ofLen = needsLen ? ` of length ${length}` : "";
108
+ const got = bytes ? `length=${len}` : `type=${typeof value}`;
109
+ throw new Error(prefix + "expected Uint8Array" + ofLen + ", got " + got);
110
+ }
111
+ return value;
112
+ }
113
+ function ahash(h) {
114
+ if (typeof h !== "function" || typeof h.create !== "function")
115
+ throw new Error("Hash must wrapped by utils.createHasher");
116
+ anumber(h.outputLen);
117
+ anumber(h.blockLen);
118
+ }
119
+ function aexists(instance, checkFinished = true) {
120
+ if (instance.destroyed)
121
+ throw new Error("Hash instance has been destroyed");
122
+ if (checkFinished && instance.finished)
123
+ throw new Error("Hash#digest() has already been called");
124
+ }
125
+ function aoutput(out, instance) {
126
+ abytes(out, void 0, "digestInto() output");
127
+ const min = instance.outputLen;
128
+ if (out.length < min) {
129
+ throw new Error('"digestInto() output" expected to be of length >=' + min);
130
+ }
131
+ }
132
+ function clean(...arrays) {
133
+ for (let i = 0; i < arrays.length; i++) {
134
+ arrays[i].fill(0);
135
+ }
136
+ }
137
+ function createView(arr) {
138
+ return new DataView(arr.buffer, arr.byteOffset, arr.byteLength);
139
+ }
140
+ function rotr(word, shift) {
141
+ return word << 32 - shift | word >>> shift;
142
+ }
143
+ function createHasher(hashCons, info = {}) {
144
+ const hashC = (msg, opts) => hashCons(opts).update(msg).digest();
145
+ const tmp = hashCons(void 0);
146
+ hashC.outputLen = tmp.outputLen;
147
+ hashC.blockLen = tmp.blockLen;
148
+ hashC.create = (opts) => hashCons(opts);
149
+ Object.assign(hashC, info);
150
+ return Object.freeze(hashC);
151
+ }
152
+ var oidNist = (suffix) => ({
153
+ oid: Uint8Array.from([6, 9, 96, 134, 72, 1, 101, 3, 4, 2, suffix])
154
+ });
155
+
156
+ // node_modules/@noble/hashes/hmac.js
157
+ var _HMAC = class {
158
+ oHash;
159
+ iHash;
160
+ blockLen;
161
+ outputLen;
162
+ finished = false;
163
+ destroyed = false;
164
+ constructor(hash, key) {
165
+ ahash(hash);
166
+ abytes(key, void 0, "key");
167
+ this.iHash = hash.create();
168
+ if (typeof this.iHash.update !== "function")
169
+ throw new Error("Expected instance of class which extends utils.Hash");
170
+ this.blockLen = this.iHash.blockLen;
171
+ this.outputLen = this.iHash.outputLen;
172
+ const blockLen = this.blockLen;
173
+ const pad = new Uint8Array(blockLen);
174
+ pad.set(key.length > blockLen ? hash.create().update(key).digest() : key);
175
+ for (let i = 0; i < pad.length; i++)
176
+ pad[i] ^= 54;
177
+ this.iHash.update(pad);
178
+ this.oHash = hash.create();
179
+ for (let i = 0; i < pad.length; i++)
180
+ pad[i] ^= 54 ^ 92;
181
+ this.oHash.update(pad);
182
+ clean(pad);
183
+ }
184
+ update(buf) {
185
+ aexists(this);
186
+ this.iHash.update(buf);
187
+ return this;
188
+ }
189
+ digestInto(out) {
190
+ aexists(this);
191
+ abytes(out, this.outputLen, "output");
192
+ this.finished = true;
193
+ this.iHash.digestInto(out);
194
+ this.oHash.update(out);
195
+ this.oHash.digestInto(out);
196
+ this.destroy();
197
+ }
198
+ digest() {
199
+ const out = new Uint8Array(this.oHash.outputLen);
200
+ this.digestInto(out);
201
+ return out;
202
+ }
203
+ _cloneInto(to) {
204
+ to ||= Object.create(Object.getPrototypeOf(this), {});
205
+ const { oHash, iHash, finished, destroyed, blockLen, outputLen } = this;
206
+ to = to;
207
+ to.finished = finished;
208
+ to.destroyed = destroyed;
209
+ to.blockLen = blockLen;
210
+ to.outputLen = outputLen;
211
+ to.oHash = oHash._cloneInto(to.oHash);
212
+ to.iHash = iHash._cloneInto(to.iHash);
213
+ return to;
214
+ }
215
+ clone() {
216
+ return this._cloneInto();
217
+ }
218
+ destroy() {
219
+ this.destroyed = true;
220
+ this.oHash.destroy();
221
+ this.iHash.destroy();
222
+ }
223
+ };
224
+ var hmac = (hash, key, message) => new _HMAC(hash, key).update(message).digest();
225
+ hmac.create = (hash, key) => new _HMAC(hash, key);
226
+
227
+ // node_modules/@noble/hashes/hkdf.js
228
+ function extract(hash, ikm, salt) {
229
+ ahash(hash);
230
+ if (salt === void 0)
231
+ salt = new Uint8Array(hash.outputLen);
232
+ return hmac(hash, salt, ikm);
233
+ }
234
+ var HKDF_COUNTER = /* @__PURE__ */ Uint8Array.of(0);
235
+ var EMPTY_BUFFER = /* @__PURE__ */ Uint8Array.of();
236
+ function expand(hash, prk, info, length = 32) {
237
+ ahash(hash);
238
+ anumber(length, "length");
239
+ const olen = hash.outputLen;
240
+ if (length > 255 * olen)
241
+ throw new Error("Length must be <= 255*HashLen");
242
+ const blocks = Math.ceil(length / olen);
243
+ if (info === void 0)
244
+ info = EMPTY_BUFFER;
245
+ else
246
+ abytes(info, void 0, "info");
247
+ const okm = new Uint8Array(blocks * olen);
248
+ const HMAC = hmac.create(hash, prk);
249
+ const HMACTmp = HMAC._cloneInto();
250
+ const T = new Uint8Array(HMAC.outputLen);
251
+ for (let counter = 0; counter < blocks; counter++) {
252
+ HKDF_COUNTER[0] = counter + 1;
253
+ HMACTmp.update(counter === 0 ? EMPTY_BUFFER : T).update(info).update(HKDF_COUNTER).digestInto(T);
254
+ okm.set(T, olen * counter);
255
+ HMAC._cloneInto(HMACTmp);
256
+ }
257
+ HMAC.destroy();
258
+ HMACTmp.destroy();
259
+ clean(T, HKDF_COUNTER);
260
+ return okm.slice(0, length);
261
+ }
262
+ var hkdf = (hash, ikm, salt, info, length) => expand(hash, extract(hash, ikm, salt), info, length);
263
+
264
+ // node_modules/@noble/hashes/_md.js
265
+ function Chi(a, b, c) {
266
+ return a & b ^ ~a & c;
267
+ }
268
+ function Maj(a, b, c) {
269
+ return a & b ^ a & c ^ b & c;
270
+ }
271
+ var HashMD = class {
272
+ blockLen;
273
+ outputLen;
274
+ padOffset;
275
+ isLE;
276
+ // For partial updates less than block size
277
+ buffer;
278
+ view;
279
+ finished = false;
280
+ length = 0;
281
+ pos = 0;
282
+ destroyed = false;
283
+ constructor(blockLen, outputLen, padOffset, isLE) {
284
+ this.blockLen = blockLen;
285
+ this.outputLen = outputLen;
286
+ this.padOffset = padOffset;
287
+ this.isLE = isLE;
288
+ this.buffer = new Uint8Array(blockLen);
289
+ this.view = createView(this.buffer);
290
+ }
291
+ update(data) {
292
+ aexists(this);
293
+ abytes(data);
294
+ const { view, buffer, blockLen } = this;
295
+ const len = data.length;
296
+ for (let pos = 0; pos < len; ) {
297
+ const take = Math.min(blockLen - this.pos, len - pos);
298
+ if (take === blockLen) {
299
+ const dataView = createView(data);
300
+ for (; blockLen <= len - pos; pos += blockLen)
301
+ this.process(dataView, pos);
302
+ continue;
303
+ }
304
+ buffer.set(data.subarray(pos, pos + take), this.pos);
305
+ this.pos += take;
306
+ pos += take;
307
+ if (this.pos === blockLen) {
308
+ this.process(view, 0);
309
+ this.pos = 0;
310
+ }
311
+ }
312
+ this.length += data.length;
313
+ this.roundClean();
314
+ return this;
315
+ }
316
+ digestInto(out) {
317
+ aexists(this);
318
+ aoutput(out, this);
319
+ this.finished = true;
320
+ const { buffer, view, blockLen, isLE } = this;
321
+ let { pos } = this;
322
+ buffer[pos++] = 128;
323
+ clean(this.buffer.subarray(pos));
324
+ if (this.padOffset > blockLen - pos) {
325
+ this.process(view, 0);
326
+ pos = 0;
327
+ }
328
+ for (let i = pos; i < blockLen; i++)
329
+ buffer[i] = 0;
330
+ view.setBigUint64(blockLen - 8, BigInt(this.length * 8), isLE);
331
+ this.process(view, 0);
332
+ const oview = createView(out);
333
+ const len = this.outputLen;
334
+ if (len % 4)
335
+ throw new Error("_sha2: outputLen must be aligned to 32bit");
336
+ const outLen = len / 4;
337
+ const state = this.get();
338
+ if (outLen > state.length)
339
+ throw new Error("_sha2: outputLen bigger than state");
340
+ for (let i = 0; i < outLen; i++)
341
+ oview.setUint32(4 * i, state[i], isLE);
342
+ }
343
+ digest() {
344
+ const { buffer, outputLen } = this;
345
+ this.digestInto(buffer);
346
+ const res = buffer.slice(0, outputLen);
347
+ this.destroy();
348
+ return res;
349
+ }
350
+ _cloneInto(to) {
351
+ to ||= new this.constructor();
352
+ to.set(...this.get());
353
+ const { blockLen, buffer, length, finished, destroyed, pos } = this;
354
+ to.destroyed = destroyed;
355
+ to.finished = finished;
356
+ to.length = length;
357
+ to.pos = pos;
358
+ if (length % blockLen)
359
+ to.buffer.set(buffer);
360
+ return to;
361
+ }
362
+ clone() {
363
+ return this._cloneInto();
364
+ }
365
+ };
366
+ var SHA256_IV = /* @__PURE__ */ Uint32Array.from([
367
+ 1779033703,
368
+ 3144134277,
369
+ 1013904242,
370
+ 2773480762,
371
+ 1359893119,
372
+ 2600822924,
373
+ 528734635,
374
+ 1541459225
375
+ ]);
376
+
377
+ // node_modules/@noble/hashes/sha2.js
378
+ var SHA256_K = /* @__PURE__ */ Uint32Array.from([
379
+ 1116352408,
380
+ 1899447441,
381
+ 3049323471,
382
+ 3921009573,
383
+ 961987163,
384
+ 1508970993,
385
+ 2453635748,
386
+ 2870763221,
387
+ 3624381080,
388
+ 310598401,
389
+ 607225278,
390
+ 1426881987,
391
+ 1925078388,
392
+ 2162078206,
393
+ 2614888103,
394
+ 3248222580,
395
+ 3835390401,
396
+ 4022224774,
397
+ 264347078,
398
+ 604807628,
399
+ 770255983,
400
+ 1249150122,
401
+ 1555081692,
402
+ 1996064986,
403
+ 2554220882,
404
+ 2821834349,
405
+ 2952996808,
406
+ 3210313671,
407
+ 3336571891,
408
+ 3584528711,
409
+ 113926993,
410
+ 338241895,
411
+ 666307205,
412
+ 773529912,
413
+ 1294757372,
414
+ 1396182291,
415
+ 1695183700,
416
+ 1986661051,
417
+ 2177026350,
418
+ 2456956037,
419
+ 2730485921,
420
+ 2820302411,
421
+ 3259730800,
422
+ 3345764771,
423
+ 3516065817,
424
+ 3600352804,
425
+ 4094571909,
426
+ 275423344,
427
+ 430227734,
428
+ 506948616,
429
+ 659060556,
430
+ 883997877,
431
+ 958139571,
432
+ 1322822218,
433
+ 1537002063,
434
+ 1747873779,
435
+ 1955562222,
436
+ 2024104815,
437
+ 2227730452,
438
+ 2361852424,
439
+ 2428436474,
440
+ 2756734187,
441
+ 3204031479,
442
+ 3329325298
443
+ ]);
444
+ var SHA256_W = /* @__PURE__ */ new Uint32Array(64);
445
+ var SHA2_32B = class extends HashMD {
446
+ constructor(outputLen) {
447
+ super(64, outputLen, 8, false);
448
+ }
449
+ get() {
450
+ const { A, B, C, D, E, F, G, H } = this;
451
+ return [A, B, C, D, E, F, G, H];
452
+ }
453
+ // prettier-ignore
454
+ set(A, B, C, D, E, F, G, H) {
455
+ this.A = A | 0;
456
+ this.B = B | 0;
457
+ this.C = C | 0;
458
+ this.D = D | 0;
459
+ this.E = E | 0;
460
+ this.F = F | 0;
461
+ this.G = G | 0;
462
+ this.H = H | 0;
463
+ }
464
+ process(view, offset) {
465
+ for (let i = 0; i < 16; i++, offset += 4)
466
+ SHA256_W[i] = view.getUint32(offset, false);
467
+ for (let i = 16; i < 64; i++) {
468
+ const W15 = SHA256_W[i - 15];
469
+ const W2 = SHA256_W[i - 2];
470
+ const s0 = rotr(W15, 7) ^ rotr(W15, 18) ^ W15 >>> 3;
471
+ const s1 = rotr(W2, 17) ^ rotr(W2, 19) ^ W2 >>> 10;
472
+ SHA256_W[i] = s1 + SHA256_W[i - 7] + s0 + SHA256_W[i - 16] | 0;
473
+ }
474
+ let { A, B, C, D, E, F, G, H } = this;
475
+ for (let i = 0; i < 64; i++) {
476
+ const sigma1 = rotr(E, 6) ^ rotr(E, 11) ^ rotr(E, 25);
477
+ const T1 = H + sigma1 + Chi(E, F, G) + SHA256_K[i] + SHA256_W[i] | 0;
478
+ const sigma0 = rotr(A, 2) ^ rotr(A, 13) ^ rotr(A, 22);
479
+ const T2 = sigma0 + Maj(A, B, C) | 0;
480
+ H = G;
481
+ G = F;
482
+ F = E;
483
+ E = D + T1 | 0;
484
+ D = C;
485
+ C = B;
486
+ B = A;
487
+ A = T1 + T2 | 0;
488
+ }
489
+ A = A + this.A | 0;
490
+ B = B + this.B | 0;
491
+ C = C + this.C | 0;
492
+ D = D + this.D | 0;
493
+ E = E + this.E | 0;
494
+ F = F + this.F | 0;
495
+ G = G + this.G | 0;
496
+ H = H + this.H | 0;
497
+ this.set(A, B, C, D, E, F, G, H);
498
+ }
499
+ roundClean() {
500
+ clean(SHA256_W);
501
+ }
502
+ destroy() {
503
+ this.set(0, 0, 0, 0, 0, 0, 0, 0);
504
+ clean(this.buffer);
505
+ }
506
+ };
507
+ var _SHA256 = class extends SHA2_32B {
508
+ // We cannot use array here since array allows indexing by variable
509
+ // which means optimizer/compiler cannot use registers.
510
+ A = SHA256_IV[0] | 0;
511
+ B = SHA256_IV[1] | 0;
512
+ C = SHA256_IV[2] | 0;
513
+ D = SHA256_IV[3] | 0;
514
+ E = SHA256_IV[4] | 0;
515
+ F = SHA256_IV[5] | 0;
516
+ G = SHA256_IV[6] | 0;
517
+ H = SHA256_IV[7] | 0;
518
+ constructor() {
519
+ super(32);
520
+ }
521
+ };
522
+ var sha256 = /* @__PURE__ */ createHasher(
523
+ () => new _SHA256(),
524
+ /* @__PURE__ */ oidNist(1)
525
+ );
526
+
96
527
  // impl/browser/storage/IpfsStorageProvider.ts
97
- var import_hkdf = require("@noble/hashes/hkdf.js");
98
- var import_sha2 = require("@noble/hashes/sha2.js");
99
528
  var heliaModule = null;
100
529
  var heliaJsonModule = null;
101
530
  var libp2pBootstrapModule = null;
@@ -236,7 +665,7 @@ var IpfsStorageProvider = class {
236
665
  try {
237
666
  const { generateKeyPairFromSeed, peerIdFromPrivateKey } = await loadHeliaModules();
238
667
  const walletSecret = this.hexToBytes(identity.privateKey);
239
- const derivedKey = (0, import_hkdf.hkdf)(import_sha2.sha256, walletSecret, void 0, HKDF_INFO, 32);
668
+ const derivedKey = hkdf(sha256, walletSecret, void 0, HKDF_INFO, 32);
240
669
  this.ipnsKeyPair = await generateKeyPairFromSeed("Ed25519", derivedKey);
241
670
  const peerId = peerIdFromPrivateKey(this.ipnsKeyPair);
242
671
  this.ipnsName = peerId.toString();
@@ -403,7 +832,7 @@ var IpfsStorageProvider = class {
403
832
  const emptyData = {
404
833
  _meta: {
405
834
  version: 0,
406
- address: this.identity?.address ?? "",
835
+ address: this.identity?.l1Address ?? "",
407
836
  formatVersion: "2.0",
408
837
  updatedAt: Date.now()
409
838
  },
@@ -670,4 +1099,9 @@ var IpfsStorageProvider = class {
670
1099
  function createIpfsStorageProvider(config) {
671
1100
  return new IpfsStorageProvider(config);
672
1101
  }
1102
+ /*! Bundled license information:
1103
+
1104
+ @noble/hashes/utils.js:
1105
+ (*! noble-hashes - MIT License (c) 2022 Paul Miller (paulmillr.com) *)
1106
+ */
673
1107
  //# sourceMappingURL=ipfs.cjs.map