@parity/product-sdk 0.0.0-dev.312.0 → 0.0.0-dev.329.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (57) hide show
  1. package/dist/address/index.d.ts +3 -1
  2. package/dist/{chunk-BRZNHTLQ.js → chunk-7GM3HUZF.js} +2 -2
  3. package/dist/chunk-7GM3HUZF.js.map +1 -0
  4. package/dist/chunk-TUXU2VPS.js +14 -0
  5. package/dist/chunk-TUXU2VPS.js.map +1 -0
  6. package/dist/{chunk-DHRCWMY5.js → chunk-VWD6HK2C.js} +29 -20
  7. package/dist/chunk-VWD6HK2C.js.map +1 -0
  8. package/dist/core/index.d.ts +50 -3
  9. package/dist/core/index.js +3 -2
  10. package/dist/{dotns-bswWAmR2.d.ts → dotns-CtmOwVQW.d.ts} +2 -2
  11. package/dist/identity/index.d.ts +118 -89
  12. package/dist/identity/index.js +200 -78
  13. package/dist/identity/index.js.map +1 -1
  14. package/dist/index.d.ts +3 -53
  15. package/dist/index.js +3 -2
  16. package/dist/nfts/index.d.ts +1 -0
  17. package/dist/nfts/index.js +3 -0
  18. package/dist/nfts/index.js.map +1 -0
  19. package/dist/react/index.d.ts +5 -5
  20. package/dist/react/index.js +4 -3
  21. package/dist/react/index.js.map +1 -1
  22. package/dist/renderer/index.d.ts +1 -0
  23. package/dist/renderer/index.js +3 -0
  24. package/dist/renderer/index.js.map +1 -0
  25. package/dist/testing/index.d.ts +2 -2
  26. package/dist/testing/index.js +3 -5
  27. package/dist/testing/index.js.map +1 -1
  28. package/dist/{types-B5Zx4RNq.d.ts → types-BpWw-OJl.d.ts} +19 -9
  29. package/package.json +23 -12
  30. package/src/core/createApp.test.ts +194 -0
  31. package/src/core/createApp.ts +35 -22
  32. package/src/core/local-storage-api.ts +74 -0
  33. package/src/core/types.ts +18 -7
  34. package/src/identity/dotns-abis.ts +44 -9
  35. package/src/identity/dotns-addresses.test.ts +536 -0
  36. package/src/identity/dotns-addresses.ts +320 -0
  37. package/src/identity/dotns-errors.ts +2 -0
  38. package/src/identity/dotns-namehash.ts +2 -2
  39. package/src/identity/dotns-registry.test.ts +158 -3
  40. package/src/identity/dotns-registry.ts +71 -73
  41. package/src/identity/dotns.ts +2 -2
  42. package/src/identity/index.test.ts +43 -0
  43. package/src/identity/index.ts +10 -6
  44. package/src/identity/types.ts +0 -19
  45. package/src/index.ts +3 -2
  46. package/src/individuality/contract.test.ts +426 -2
  47. package/src/nfts/contract.test.ts +58 -0
  48. package/src/nfts/index.ts +8 -0
  49. package/src/react/context.ts +1 -1
  50. package/src/react/index.ts +1 -1
  51. package/src/react/provider.tsx +3 -3
  52. package/src/renderer/index.ts +10 -0
  53. package/src/testing.ts +14 -11
  54. package/dist/chunk-BRZNHTLQ.js.map +0 -1
  55. package/dist/chunk-DHRCWMY5.js.map +0 -1
  56. package/src/identity/product-account.test.ts +0 -130
  57. package/src/identity/product-account.ts +0 -115
@@ -1,10 +1,10 @@
1
- import { normalizeDotNsName, isResolvableDotNsName, namehash, isValidDotNsName, stripSuffix, isConsistentDotNsTld, DOT_TLD, dotNsTld } from '../chunk-BRZNHTLQ.js';
2
- export { DOT_NODE, DOT_TLD, accountIdHexToBytes, accountIdToHex, dotNsTld, isConsistentDotNsTld, isResolvableDotNsName, isValidDotNsName, namehash, normalizeDotNsName, resolvePeopleUsernameOwner, stripSuffix } from '../chunk-BRZNHTLQ.js';
3
- import { err, ok } from '@parity/result';
4
- import { QUERY_FALLBACK_ORIGIN, createContract } from '@parity/product-sdk-contracts';
5
- import { bytesToHex, randomBytes, blake2b256 } from '@parity/product-sdk-crypto';
6
- import { ss58ToH160, ss58Decode, ss58Encode, deriveH160 } from '@parity/product-sdk-address';
1
+ import { normalizeDotNsName, isResolvableDotNsName, namehash, isValidDotNsName, stripSuffix, isConsistentDotNsTld, DOT_TLD, dotNsTld } from '../chunk-7GM3HUZF.js';
2
+ export { DOT_NODE, DOT_TLD, accountIdHexToBytes, accountIdToHex, dotNsTld, isConsistentDotNsTld, isResolvableDotNsName, isValidDotNsName, namehash, normalizeDotNsName, resolvePeopleUsernameOwner, stripSuffix } from '../chunk-7GM3HUZF.js';
3
+ import { ok, err } from '@parity/result';
4
+ import { bytesToHex, randomBytes } from '@parity/product-sdk-crypto';
5
+ import { ss58ToH160 } from '@parity/product-sdk-address';
7
6
  import { createLogger } from '@parity/product-sdk-logger';
7
+ import { QUERY_FALLBACK_ORIGIN, createContract } from '@parity/product-sdk-contracts';
8
8
 
9
9
  // src/identity/dotns-abis.ts
10
10
  var DOTNS_REGISTRY_ABI = [
@@ -194,6 +194,31 @@ var DOTNS_PROTOCOL_REGISTRY_ABI = [
194
194
  inputs: [],
195
195
  outputs: [{ name: "node", type: "bytes32" }],
196
196
  stateMutability: "view"
197
+ },
198
+ {
199
+ type: "function",
200
+ name: "get",
201
+ inputs: [{ name: "key", type: "bytes32" }],
202
+ outputs: [{ name: "addr", type: "address" }],
203
+ stateMutability: "view"
204
+ }
205
+ ];
206
+ var DOTNS_ROOT_GATEWAY_DISPATCHER_ABI = [
207
+ {
208
+ type: "function",
209
+ name: "TARGET",
210
+ inputs: [],
211
+ outputs: [{ name: "", type: "address" }],
212
+ stateMutability: "view"
213
+ }
214
+ ];
215
+ var DOTNS_POP_CONTROLLER_ABI = [
216
+ {
217
+ type: "function",
218
+ name: "protocolRegistry",
219
+ inputs: [],
220
+ outputs: [{ name: "", type: "address" }],
221
+ stateMutability: "view"
197
222
  }
198
223
  ];
199
224
  var POP_STATUS = { NoStatus: 0, PopLite: 1, PopFull: 2, Reserved: 3 };
@@ -217,22 +242,159 @@ var DotNsError = class extends Error {
217
242
  this.reason = reason;
218
243
  }
219
244
  };
220
-
221
- // src/identity/dotns-registry.ts
222
- var log = createLogger("identity:dotns");
245
+ var DOTNS_REGISTRY_KEYS = {
246
+ registry: "0x7265676973747279000000000000000000000000000000000000000000000000",
247
+ registrarController: "0x636f6e74726f6c6c657200000000000000000000000000000000000000000000",
248
+ reverseResolver: "0x726576657273655265736f6c7665720000000000000000000000000000000000",
249
+ resolver: "0x7265736f6c766572000000000000000000000000000000000000000000000000",
250
+ popRules: "0x706f7052756c6573000000000000000000000000000000000000000000000000"
251
+ };
223
252
  var ZERO_ADDRESS = "0x0000000000000000000000000000000000000000";
224
- function contractOf(runtime, address, abi) {
225
- return createContract(runtime, address, abi);
226
- }
227
253
  function isZero(addr) {
228
254
  return typeof addr === "string" && addr.toLowerCase() === ZERO_ADDRESS;
229
255
  }
256
+ function sameAddress(a, b) {
257
+ return typeof a === "string" && typeof b === "string" && a.toLowerCase() === b.toLowerCase();
258
+ }
259
+ function cachedPerRuntime(cache, runtime, key, load) {
260
+ let byKey = cache.get(runtime);
261
+ if (!byKey) {
262
+ byKey = /* @__PURE__ */ new Map();
263
+ cache.set(runtime, byKey);
264
+ }
265
+ const cached = byKey.get(key);
266
+ if (cached) return cached;
267
+ const pending = load();
268
+ byKey.set(key, pending);
269
+ pending.then((result) => {
270
+ if (!result.ok) byKey?.delete(key);
271
+ });
272
+ return pending;
273
+ }
274
+ function withOverrides(base, opts) {
275
+ return {
276
+ registry: opts.registryAddress ?? base.registry,
277
+ reverseResolver: opts.reverseResolverAddress ?? base.reverseResolver,
278
+ resolver: opts.resolverAddress ?? base.resolver,
279
+ registrarController: opts.registrarControllerAddress ?? base.registrarController,
280
+ popRules: opts.popRulesAddress ?? base.popRules,
281
+ protocolRegistry: opts.protocolRegistryAddress ?? base.protocolRegistry
282
+ };
283
+ }
284
+ var walkCache = /* @__PURE__ */ new WeakMap();
285
+ var WALK_KEY = "discovered";
286
+ async function resolveDotNsAddresses(opts) {
287
+ if (opts.addressSource !== "discovered") return ok(withOverrides(DOTNS_ADDRESSES, opts));
288
+ const walked = await cachedPerRuntime(
289
+ walkCache,
290
+ opts.runtime,
291
+ WALK_KEY,
292
+ () => discoverDotNsAddresses(opts)
293
+ );
294
+ return walked.ok ? ok(withOverrides(walked.value, opts)) : walked;
295
+ }
296
+ var discoveryFailed = (step, cause) => err(new DotNsError("AddressDiscovery", `DotNS address discovery failed: ${step}`, { cause }));
297
+ function contractOf(runtime, address, abi) {
298
+ return createContract(runtime, address, abi);
299
+ }
230
300
  function readOrigin(opts) {
231
301
  return opts.origin ?? QUERY_FALLBACK_ORIGIN;
232
302
  }
233
- function sameAddress(a, b) {
234
- return typeof a === "string" && typeof b === "string" && a.toLowerCase() === b.toLowerCase();
303
+ async function readAddress(contract, method, origin) {
304
+ const res = await contract[method].query({ origin });
305
+ if (!res.success) return discoveryFailed(`${method}() failed`, res.value);
306
+ const value = res.value;
307
+ if (typeof value !== "string" || isZero(value)) {
308
+ return discoveryFailed(`${method}() returned no address`);
309
+ }
310
+ return ok(value);
311
+ }
312
+ function asH160(value) {
313
+ if (typeof value === "string") return value;
314
+ if (value && typeof value.asHex === "function") {
315
+ return value.asHex();
316
+ }
317
+ return null;
235
318
  }
319
+ function gatewayOf(opts) {
320
+ if (opts.gatewayApi) return opts.gatewayApi;
321
+ const api = opts.runtime?.api;
322
+ const getValue = api?.query?.DotnsGateway?.DispatcherAddress?.getValue;
323
+ return typeof getValue === "function" ? api : null;
324
+ }
325
+ async function discoverDotNsAddresses(opts) {
326
+ try {
327
+ const gateway = gatewayOf(opts);
328
+ if (!gateway) {
329
+ return discoveryFailed(
330
+ "no DotnsGateway.DispatcherAddress storage on this runtime; pass opts.gatewayApi"
331
+ );
332
+ }
333
+ const dispatcher = asH160(await gateway.query.DotnsGateway.DispatcherAddress.getValue());
334
+ if (!dispatcher || isZero(dispatcher)) {
335
+ return discoveryFailed("DotnsGateway.DispatcherAddress is unset");
336
+ }
337
+ const origin = readOrigin(opts);
338
+ const target = await readAddress(
339
+ contractOf(opts.runtime, dispatcher, DOTNS_ROOT_GATEWAY_DISPATCHER_ABI),
340
+ "TARGET",
341
+ origin
342
+ );
343
+ if (!target.ok) return target;
344
+ const protocolRegistry = await readAddress(
345
+ contractOf(opts.runtime, target.value, DOTNS_POP_CONTROLLER_ABI),
346
+ "protocolRegistry",
347
+ origin
348
+ );
349
+ if (!protocolRegistry.ok) return protocolRegistry;
350
+ const registry = contractOf(
351
+ opts.runtime,
352
+ protocolRegistry.value,
353
+ DOTNS_PROTOCOL_REGISTRY_ABI
354
+ );
355
+ const roles = Object.entries(DOTNS_REGISTRY_KEYS);
356
+ const found = await Promise.all(
357
+ roles.map(async ([role, key]) => {
358
+ const res = await registry.get.query(key, { origin });
359
+ if (!res.success) return discoveryFailed(`get(${role}) failed`, res.value);
360
+ const value = res.value;
361
+ if (typeof value !== "string" || isZero(value)) {
362
+ return discoveryFailed(`${role} is unset on the protocol registry`);
363
+ }
364
+ return ok([role, value]);
365
+ })
366
+ );
367
+ const failed = found.find((r) => !r.ok);
368
+ if (failed && !failed.ok) return failed;
369
+ const byRole = Object.fromEntries(found.flatMap((r) => r.ok ? [r.value] : []));
370
+ return ok({ ...byRole, protocolRegistry: protocolRegistry.value });
371
+ } catch (cause) {
372
+ return discoveryFailed("unexpected error", cause);
373
+ }
374
+ }
375
+ async function verifyDotNsAddresses(opts) {
376
+ const discovered = await cachedPerRuntime(
377
+ walkCache,
378
+ opts.runtime,
379
+ WALK_KEY,
380
+ () => discoverDotNsAddresses(opts)
381
+ );
382
+ if (!discovered.ok) return discovered;
383
+ const inUse = withOverrides(
384
+ opts.addressSource === "discovered" ? discovered.value : DOTNS_ADDRESSES,
385
+ opts
386
+ );
387
+ const drift = Object.keys(inUse).filter((role) => !sameAddress(inUse[role], discovered.value[role])).map((role) => `${role}: using ${inUse[role]}, chain says ${discovered.value[role]}`);
388
+ return drift.length ? err(
389
+ new DotNsError(
390
+ "AddressMismatch",
391
+ `DotNS addresses disagree with the deployment \u2014 ${drift.join("; ")}`
392
+ )
393
+ ) : ok(inUse);
394
+ }
395
+
396
+ // src/identity/dotns-registry.ts
397
+ var log = createLogger("identity:dotns");
236
398
  var tldCache = /* @__PURE__ */ new WeakMap();
237
399
  function isEmptyRevert(value) {
238
400
  return typeof value === "object" && value !== null && value.type === "ContractRevertedWithPayload" && value.data === "0x";
@@ -246,20 +408,10 @@ async function resolveTld(opts) {
246
408
  )
247
409
  );
248
410
  }
249
- const address = opts.protocolRegistryAddress ?? DOTNS_ADDRESSES.protocolRegistry;
250
- let perAddress = tldCache.get(opts.runtime);
251
- if (!perAddress) {
252
- perAddress = /* @__PURE__ */ new Map();
253
- tldCache.set(opts.runtime, perAddress);
254
- }
255
- const cached = perAddress.get(address);
256
- if (cached) return cached;
257
- const pending = readTld(opts, address);
258
- perAddress.set(address, pending);
259
- pending.then((result) => {
260
- if (!result.ok) perAddress?.delete(address);
261
- });
262
- return pending;
411
+ const addresses = await resolveDotNsAddresses(opts);
412
+ if (!addresses.ok) return addresses;
413
+ const address = addresses.value.protocolRegistry;
414
+ return cachedPerRuntime(tldCache, opts.runtime, address, () => readTld(opts, address));
263
415
  }
264
416
  async function readTld(opts, address) {
265
417
  try {
@@ -329,7 +481,9 @@ async function resolveDotNs(name, opts) {
329
481
  return err(nameError(name, normalized, tld));
330
482
  }
331
483
  const node = namehash(normalized, tld);
332
- const registryAddr = opts.registryAddress ?? DOTNS_ADDRESSES.registry;
484
+ const addrs = await resolveDotNsAddresses(opts);
485
+ if (!addrs.ok) return addrs;
486
+ const registryAddr = addrs.value.registry;
333
487
  const origin = readOrigin(opts);
334
488
  log.debug("resolveDotNs", { name: normalized, node, registry: registryAddr });
335
489
  try {
@@ -355,7 +509,7 @@ async function resolveDotNs(name, opts) {
355
509
  const owner = ownerRes.value;
356
510
  if (isZero(owner)) return ok(null);
357
511
  const resolverAddr = resolverRes.value;
358
- const forwardAddr = opts.resolverAddress ?? DOTNS_ADDRESSES.resolver;
512
+ const forwardAddr = addrs.value.resolver;
359
513
  if (!sameAddress(resolverAddr, forwardAddr)) {
360
514
  return ok({ name: normalized, owner });
361
515
  }
@@ -378,7 +532,9 @@ async function resolveDotNs(name, opts) {
378
532
  }
379
533
  }
380
534
  async function reverseDotNs(address, opts) {
381
- const reverseAddr = opts.reverseResolverAddress ?? DOTNS_ADDRESSES.reverseResolver;
535
+ const addrs = await resolveDotNsAddresses(opts);
536
+ if (!addrs.ok) return addrs;
537
+ const reverseAddr = addrs.value.reverseResolver;
382
538
  log.debug("reverseDotNs", { address, reverseResolver: reverseAddr });
383
539
  try {
384
540
  const reverse = contractOf(
@@ -411,7 +567,9 @@ async function isDotNsAvailable(name, opts) {
411
567
  return err(nameError(name, normalized, tld));
412
568
  }
413
569
  const label = stripSuffix(normalized, tld.suffix);
414
- const controllerAddr = opts.registrarControllerAddress ?? DOTNS_ADDRESSES.registrarController;
570
+ const addrs = await resolveDotNsAddresses(opts);
571
+ if (!addrs.ok) return addrs;
572
+ const controllerAddr = addrs.value.registrarController;
415
573
  log.debug("isDotNsAvailable", { name: normalized, label, controller: controllerAddr });
416
574
  try {
417
575
  const controller = contractOf(
@@ -419,11 +577,7 @@ async function isDotNsAvailable(name, opts) {
419
577
  controllerAddr,
420
578
  DOTNS_REGISTRAR_CONTROLLER_ABI
421
579
  );
422
- const popRules = contractOf(
423
- opts.runtime,
424
- opts.popRulesAddress ?? DOTNS_ADDRESSES.popRules,
425
- DOTNS_POP_RULES_ABI
426
- );
580
+ const popRules = contractOf(opts.runtime, addrs.value.popRules, DOTNS_POP_RULES_ABI);
427
581
  const origin = readOrigin(opts);
428
582
  const [res, classified] = await Promise.all([
429
583
  controller.available.query(label, { origin }),
@@ -467,8 +621,10 @@ async function setDotNsRecord(args, opts) {
467
621
  return err(new DotNsError("MissingOrigin", "setDotNsRecord needs opts.origin (SS58)"));
468
622
  }
469
623
  const node = namehash(normalized, tld);
470
- const registryAddr = opts.registryAddress ?? DOTNS_ADDRESSES.registry;
471
- const resolverAddr = opts.resolverAddress ?? DOTNS_ADDRESSES.resolver;
624
+ const addrs = await resolveDotNsAddresses(opts);
625
+ if (!addrs.ok) return addrs;
626
+ const registryAddr = addrs.value.registry;
627
+ const resolverAddr = addrs.value.resolver;
472
628
  try {
473
629
  const registry = contractOf(opts.runtime, registryAddr, DOTNS_REGISTRY_ABI);
474
630
  const currentRes = await registry.resolver.query(node, { origin });
@@ -559,8 +715,10 @@ async function prepareDotNsRegistration(args, opts) {
559
715
  );
560
716
  }
561
717
  const label = stripSuffix(normalized, tld.suffix);
562
- const controllerAddr = opts.registrarControllerAddress ?? DOTNS_ADDRESSES.registrarController;
563
- const popRulesAddr = opts.popRulesAddress ?? DOTNS_ADDRESSES.popRules;
718
+ const addrs = await resolveDotNsAddresses(opts);
719
+ if (!addrs.ok) return addrs;
720
+ const controllerAddr = addrs.value.registrarController;
721
+ const popRulesAddr = addrs.value.popRules;
564
722
  const secret = `0x${bytesToHex(randomBytes(32))}`;
565
723
  const registration = { label, owner: args.owner, secret, reserved: args.reserved ?? false };
566
724
  let payer;
@@ -649,43 +807,7 @@ async function prepareDotNsRegistration(args, opts) {
649
807
  return err(new DotNsError("RegistryCall", "DotNS registration prepare failed", { cause }));
650
808
  }
651
809
  }
652
- var log2 = createLogger("identity");
653
- function deriveContextAlias(parentAddress, context, ss58Prefix = 42) {
654
- const { publicKey: parentPublicKey } = ss58Decode(parentAddress);
655
- const contextBytes = new TextEncoder().encode(context);
656
- const combined = new Uint8Array(parentPublicKey.length + contextBytes.length);
657
- combined.set(parentPublicKey, 0);
658
- combined.set(contextBytes, parentPublicKey.length);
659
- const aliasPublicKey = blake2b256(combined);
660
- const address = ss58Encode(aliasPublicKey, ss58Prefix);
661
- const h160Address = deriveH160(aliasPublicKey);
662
- log2.debug("Derived context alias", {
663
- parentAddress,
664
- context,
665
- address
666
- });
667
- return {
668
- address,
669
- h160Address,
670
- parentAddress,
671
- context
672
- };
673
- }
674
- function verifyContextAlias(aliasAddress, parentAddress, context) {
675
- try {
676
- const derived = deriveContextAlias(parentAddress, context);
677
- const { publicKey: aliasKey } = ss58Decode(aliasAddress);
678
- const { publicKey: derivedKey } = ss58Decode(derived.address);
679
- if (aliasKey.length !== derivedKey.length) return false;
680
- for (let i = 0; i < aliasKey.length; i++) {
681
- if (aliasKey[i] !== derivedKey[i]) return false;
682
- }
683
- return true;
684
- } catch {
685
- return false;
686
- }
687
- }
688
810
 
689
- export { DOTNS_ADDRESSES, DotNsError, POP_STATUS, deriveContextAlias, isDotNsAvailable, prepareDotNsRegistration, resolveDotNs, reverseDotNs, setDotNsRecord, verifyContextAlias };
811
+ export { DOTNS_ADDRESSES, DOTNS_REGISTRY_KEYS, DotNsError, POP_STATUS, discoverDotNsAddresses, isDotNsAvailable, prepareDotNsRegistration, resolveDotNs, resolveDotNsAddresses, reverseDotNs, setDotNsRecord, verifyDotNsAddresses };
690
812
  //# sourceMappingURL=index.js.map
691
813
  //# sourceMappingURL=index.js.map