@dotdm/env 2.0.0-dev.1779912670 → 2.0.1-dev.1779942376

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -53,6 +53,12 @@ type KnownChainName = keyof typeof KNOWN_CHAINS;
53
53
  declare function isKnownChainPreset(name: string): boolean;
54
54
  declare function getChainPreset(name: string): ChainPreset;
55
55
 
56
+ interface QueryOriginTarget {
57
+ chainName?: string;
58
+ assethubUrl?: string;
59
+ }
60
+ declare function resolveQueryOrigin(target?: string | QueryOriginTarget): string;
61
+
56
62
  type CdmDeployAssetHubDescriptor = typeof paseo_asset_hub;
57
63
  type CdmAssetHubDescriptor = CdmDeployAssetHubDescriptor | typeof polkadot_asset_hub;
58
64
  type CdmBulletinDescriptor = typeof paseo_bulletin;
@@ -153,4 +159,4 @@ declare function prepareSignerFromMnemonic(mnemonic: string): polkadot_api.Polka
153
159
  */
154
160
  declare function prepareSignerFromSuri(suri: string): polkadot_api.PolkadotSigner;
155
161
 
156
- export { type AssetHubConnection, type BulletinConnection, type CdmAssetHubApi, type CdmAssetHubClient, type CdmBulletinApi, type CdmChainClient, type CdmChainEndpoints, type CdmDeployAssetHubApi, type ChainFaucet, type ChainPreset, type IpfsGateway, type KnownChainName, ProductSdkEnvironment, connectIpfsGateway, createCdmAssetHubClient, createCdmChainClient, getChainPreset, isKnownChainPreset, prepareSigner, prepareSignerFromMnemonic, prepareSignerFromSuri };
162
+ export { type AssetHubConnection, type BulletinConnection, type CdmAssetHubApi, type CdmAssetHubClient, type CdmBulletinApi, type CdmChainClient, type CdmChainEndpoints, type CdmDeployAssetHubApi, type ChainFaucet, type ChainPreset, type IpfsGateway, type KnownChainName, ProductSdkEnvironment, connectIpfsGateway, createCdmAssetHubClient, createCdmChainClient, getChainPreset, isKnownChainPreset, prepareSigner, prepareSignerFromMnemonic, prepareSignerFromSuri, resolveQueryOrigin };
package/dist/index.js CHANGED
@@ -54,6 +54,81 @@ function getChainPreset(name) {
54
54
  }
55
55
  return preset;
56
56
  }
57
+ function findKnownChainByAssetHubUrl(url) {
58
+ const normalizedUrl = url.replace(/\/+$/, "");
59
+ return Object.entries(KNOWN_CHAINS).find(
60
+ ([, preset]) => preset.assethubUrl.replace(/\/+$/, "") === normalizedUrl
61
+ )?.[0];
62
+ }
63
+
64
+ // src/query_origin.ts
65
+ import { ALICE_SS58 } from "@dotdm/utils";
66
+ import { getAccount } from "@dotdm/utils/accounts";
67
+ function resolveQueryAccountName(target) {
68
+ const chainName = typeof target === "string" ? target : target?.chainName;
69
+ const normalized = chainName ? normalizeChainName(chainName) : void 0;
70
+ if (normalized && normalized !== "custom") return normalized;
71
+ const assethubUrl = typeof target === "string" ? void 0 : target?.assethubUrl;
72
+ return assethubUrl ? findKnownChainByAssetHubUrl(assethubUrl) : void 0;
73
+ }
74
+ function resolveQueryOrigin(target) {
75
+ const accountName = resolveQueryAccountName(target);
76
+ if (accountName) {
77
+ const account = getAccount(accountName);
78
+ if (account) return account.address;
79
+ }
80
+ return ALICE_SS58;
81
+ }
82
+ if (import.meta.vitest) {
83
+ const { afterEach, describe, expect, test } = import.meta.vitest;
84
+ const { mkdtempSync, rmSync } = await import("fs");
85
+ const { tmpdir } = await import("os");
86
+ const { join } = await import("path");
87
+ const { saveAccount } = await import("@dotdm/utils/accounts");
88
+ describe("resolveQueryOrigin", () => {
89
+ const originalCdmRoot = process.env.CDM_ROOT;
90
+ afterEach(() => {
91
+ if (originalCdmRoot === void 0) {
92
+ delete process.env.CDM_ROOT;
93
+ } else {
94
+ process.env.CDM_ROOT = originalCdmRoot;
95
+ }
96
+ });
97
+ test("uses the normalized chain account when present", () => {
98
+ const root = mkdtempSync(join(tmpdir(), "cdm-query-origin-"));
99
+ process.env.CDM_ROOT = root;
100
+ try {
101
+ saveAccount("paseo", { mnemonic: "test", address: "paseo-address" });
102
+ expect(resolveQueryOrigin("paseo-v2")).toBe("paseo-address");
103
+ } finally {
104
+ rmSync(root, { recursive: true, force: true });
105
+ }
106
+ });
107
+ test("infers a known chain account from the Asset Hub URL", () => {
108
+ const root = mkdtempSync(join(tmpdir(), "cdm-query-origin-"));
109
+ process.env.CDM_ROOT = root;
110
+ try {
111
+ saveAccount("paseo", { mnemonic: "test", address: "paseo-address" });
112
+ expect(
113
+ resolveQueryOrigin({
114
+ assethubUrl: "wss://paseo-asset-hub-next-rpc.polkadot.io"
115
+ })
116
+ ).toBe("paseo-address");
117
+ } finally {
118
+ rmSync(root, { recursive: true, force: true });
119
+ }
120
+ });
121
+ test("falls back to Alice when no chain account is available", () => {
122
+ const root = mkdtempSync(join(tmpdir(), "cdm-query-origin-"));
123
+ process.env.CDM_ROOT = root;
124
+ try {
125
+ expect(resolveQueryOrigin("paseo")).toBe(ALICE_SS58);
126
+ } finally {
127
+ rmSync(root, { recursive: true, force: true });
128
+ }
129
+ });
130
+ });
131
+ }
57
132
 
58
133
  // src/index.ts
59
134
  import { DEFAULT_NODE_URL } from "@dotdm/utils";
@@ -194,5 +269,6 @@ export {
194
269
  prepareSigner,
195
270
  prepareSignerFromMnemonic,
196
271
  prepareSignerFromSuri,
272
+ resolveQueryOrigin,
197
273
  ss58Address
198
274
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dotdm/env",
3
- "version": "2.0.0-dev.1779912670",
3
+ "version": "2.0.1-dev.1779942376",
4
4
  "type": "module",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",