@dynamic-labs-sdk/aleo 1.28.0 → 1.30.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.
- package/README.md +20 -0
- package/dist/AleoWalletProvider.types.d.ts +7 -0
- package/dist/AleoWalletProvider.types.d.ts.map +1 -1
- package/dist/InvalidAleoTransactionIdError-CZoNf2SE.esm.js +50 -0
- package/dist/InvalidAleoTransactionIdError-CZoNf2SE.esm.js.map +1 -0
- package/dist/InvalidAleoTransactionIdError-DRDfoh4x.cjs +61 -0
- package/dist/InvalidAleoTransactionIdError-DRDfoh4x.cjs.map +1 -0
- package/dist/{getNetworkByChainId-XUAi3XmZ.cjs → constants-BoB0_3u4.cjs} +2 -46
- package/dist/constants-BoB0_3u4.cjs.map +1 -0
- package/dist/constants-DZ9sVn7x.esm.js +43 -0
- package/dist/constants-DZ9sVn7x.esm.js.map +1 -0
- package/dist/errors/AleoInvalidTransactionError/AleoInvalidTransactionError.d.ts +19 -0
- package/dist/errors/AleoInvalidTransactionError/AleoInvalidTransactionError.d.ts.map +1 -0
- package/dist/errors/AleoInvalidTransactionError/index.d.ts +2 -0
- package/dist/errors/AleoInvalidTransactionError/index.d.ts.map +1 -0
- package/dist/exports/index.d.ts +1 -0
- package/dist/exports/index.d.ts.map +1 -1
- package/dist/exports/waas.d.ts +3 -0
- package/dist/exports/waas.d.ts.map +1 -0
- package/dist/getNetworkByChainId-CydH2vdT.cjs +47 -0
- package/dist/getNetworkByChainId-CydH2vdT.cjs.map +1 -0
- package/dist/getNetworkByChainId-DL6E07FO.esm.js +36 -0
- package/dist/getNetworkByChainId-DL6E07FO.esm.js.map +1 -0
- package/dist/index.cjs +13 -31
- package/dist/index.cjs.map +1 -1
- package/dist/index.esm.js +4 -23
- package/dist/index.esm.js.map +1 -1
- package/dist/registerAleoNetworkProviderBuilder-ByQwv6MA.cjs +158 -0
- package/dist/registerAleoNetworkProviderBuilder-ByQwv6MA.cjs.map +1 -0
- package/dist/registerAleoNetworkProviderBuilder-CMcQJWs3.esm.js +153 -0
- package/dist/registerAleoNetworkProviderBuilder-CMcQJWs3.esm.js.map +1 -0
- package/dist/tsconfig.lib.tsbuildinfo +1 -1
- package/dist/waas/WaasAleoWalletProvider.types.d.ts +12 -0
- package/dist/waas/WaasAleoWalletProvider.types.d.ts.map +1 -0
- package/dist/waas/addWaasAleoExtension/addWaasAleoExtension.d.ts +12 -0
- package/dist/waas/addWaasAleoExtension/addWaasAleoExtension.d.ts.map +1 -0
- package/dist/waas/addWaasAleoExtension/index.d.ts +2 -0
- package/dist/waas/addWaasAleoExtension/index.d.ts.map +1 -0
- package/dist/waas/utils/createWalletProviderForWaasAleo/createWalletProviderForWaasAleo.d.ts +25 -0
- package/dist/waas/utils/createWalletProviderForWaasAleo/createWalletProviderForWaasAleo.d.ts.map +1 -0
- package/dist/waas/utils/createWalletProviderForWaasAleo/index.d.ts +2 -0
- package/dist/waas/utils/createWalletProviderForWaasAleo/index.d.ts.map +1 -0
- package/dist/waas/utils/inferAleoInputType/index.d.ts +2 -0
- package/dist/waas/utils/inferAleoInputType/index.d.ts.map +1 -0
- package/dist/waas/utils/inferAleoInputType/inferAleoInputType.d.ts +18 -0
- package/dist/waas/utils/inferAleoInputType/inferAleoInputType.d.ts.map +1 -0
- package/dist/waas.cjs +217 -0
- package/dist/waas.cjs.map +1 -0
- package/dist/waas.esm.js +217 -0
- package/dist/waas.esm.js.map +1 -0
- package/dist/walletStandard.cjs +12 -158
- package/dist/walletStandard.cjs.map +1 -1
- package/dist/walletStandard.esm.js +4 -150
- package/dist/walletStandard.esm.js.map +1 -1
- package/package.json +9 -4
- package/dist/getNetworkByChainId-BfyC4NXK.esm.js +0 -75
- package/dist/getNetworkByChainId-BfyC4NXK.esm.js.map +0 -1
- package/dist/getNetworkByChainId-XUAi3XmZ.cjs.map +0 -1
package/dist/index.esm.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.esm.js","names":["packageName","packageVersion"],"sources":["../src/errors/
|
|
1
|
+
{"version":3,"file":"index.esm.js","names":["packageName","packageVersion"],"sources":["../src/errors/NotAleoProviderError/NotAleoProviderError.ts","../src/isAleoNetworkProvider/isAleoNetworkProvider.ts","../src/isAleoWalletAccount/isAleoWalletAccount.ts","../src/isAleoWalletProvider/isAleoWalletProvider.ts","../src/utils/isAleoMainnet/isAleoMainnet.ts","../src/utils/getAleoExplorerTxUrl/getAleoExplorerTxUrl.ts","../src/exports/index.ts"],"sourcesContent":["import { BaseError } from '@dynamic-labs-sdk/client';\n\n/**\n * Thrown by Aleo-specific top-level helpers (`signMessage`,\n * `requestTransaction`, `decrypt`, `requestRecords`) when the wallet\n * provider resolved from a wallet account is not an Aleo provider — i.e.\n * the caller passed a wallet account from a different chain. Carries the\n * offending provider's key on `providerKey`.\n */\nexport class NotAleoProviderError extends BaseError {\n public readonly providerKey: string;\n\n constructor(params: { providerKey: string }) {\n super({\n cause: null,\n code: 'not_aleo_provider_error',\n docsUrl: null,\n name: 'NotAleoProviderError',\n shortMessage: 'Provider is not an Aleo wallet provider',\n });\n\n this.providerKey = params.providerKey;\n }\n}\n","import type { NetworkProvider } from '@dynamic-labs-sdk/client';\n\nimport type { AleoNetworkProvider } from '../AleoNetworkProvider.types';\n\n/**\n * Type guard narrowing a generic `NetworkProvider` to `AleoNetworkProvider`.\n * Use this to filter the network-provider registry down to the Aleo\n * network providers.\n *\n * @example\n * ```ts\n * if (isAleoNetworkProvider(networkProvider)) {\n * const network = networkProvider.getNetwork();\n * }\n * ```\n *\n * @param networkProvider - The network provider to check.\n * @returns True if the network provider is an Aleo network provider, false otherwise.\n * @see isAleoWalletAccount\n * @see getNetworkByChainId\n * @notInstrumented\n */\nexport const isAleoNetworkProvider = (\n networkProvider: NetworkProvider\n): networkProvider is AleoNetworkProvider => networkProvider.chain === 'ALEO';\n","import type { WalletAccount } from '@dynamic-labs-sdk/client';\n\nimport type { AleoWalletAccount } from '../AleoWalletAccount.types';\n\n/**\n * Type guard narrowing a generic `WalletAccount` to `AleoWalletAccount`.\n * Use this to filter mixed-chain wallet account lists down to the Aleo\n * accounts.\n *\n * @example\n * ```ts\n * const accounts = getWalletAccounts();\n * const aleoAccounts = accounts.filter(isAleoWalletAccount);\n * ```\n *\n * @param walletAccount - The wallet account to check.\n * @returns True if the wallet account is an Aleo wallet account, false otherwise.\n * @see isAleoNetworkProvider\n * @see isAleoWalletProvider\n * @notInstrumented\n */\nexport const isAleoWalletAccount = (\n walletAccount: WalletAccount\n): walletAccount is AleoWalletAccount => walletAccount.chain === 'ALEO';\n","import type { WalletProvider } from '@dynamic-labs-sdk/client/core';\n\nimport type { AleoWalletProvider } from '../AleoWalletProvider.types';\n\n/**\n * Type guard narrowing a generic `WalletProvider` to `AleoWalletProvider`.\n * Returns `true` when the provider's chain is `'ALEO'`. Mirrors the\n * `is{Chain}WalletProvider` pattern used by the sibling chain packages\n * (Tron, Sui, Aptos) — exposes the Aleo-specific methods like\n * `requestTransaction`, `decrypt`, and `requestRecords` to the caller.\n *\n * @example\n * ```ts\n * if (isAleoWalletProvider(provider)) {\n * const records = await provider.requestRecords({ program: 'credits.aleo' });\n * }\n * ```\n *\n * @param provider - The wallet provider to check.\n * @returns True if the wallet provider is an Aleo wallet provider, false otherwise.\n * @see isAleoWalletAccount\n * @see isAleoNetworkProvider\n * @notInstrumented\n */\n// eslint-disable-next-line custom-rules/require-single-object-param -- standard type-guard signature shared with sibling chains\nexport const isAleoWalletProvider = (\n provider: WalletProvider | AleoWalletProvider\n): provider is AleoWalletProvider => {\n return provider.chain === 'ALEO';\n};\n","import { ALEO_MAINNET_NETWORK_ID } from '../constants';\n\n/**\n * Whether a numeric/string chain id points at the Aleo mainnet (`0`)\n * rather than a testnet. Both numeric and string ids are accepted so\n * callers can pass `walletAccount.activeNetworkId` (which is stringly-\n * typed) directly. `undefined` and `null` resolve to `false` so a\n * stale or unknown id falls back to testnet behaviour at the call\n * site.\n *\n * @notInstrumented\n */\n// eslint-disable-next-line custom-rules/require-single-object-param -- single primitive arg; named-arg ceremony would not improve clarity\nexport const isAleoMainnet = (\n networkId: number | string | undefined | null\n): boolean => {\n if (networkId === undefined || networkId === null) return false;\n if (typeof networkId === 'number') {\n return networkId === ALEO_MAINNET_NETWORK_ID;\n }\n return Number(networkId) === ALEO_MAINNET_NETWORK_ID;\n};\n","import { InvalidAleoTransactionIdError } from '../../errors/InvalidAleoTransactionIdError';\nimport { isAleoMainnet } from '../isAleoMainnet';\n\n/**\n * Aleo transaction ids are bech32-encoded with the `at1` prefix and a body\n * of lowercase alphanumerics — same character set as account addresses. We\n * validate strictly before letting the id reach the URL, refusing `/`, `?`,\n * `#`, `..`, etc. so a hostile id cannot produce path traversal or an open\n * redirect when the rendered URL is opened.\n */\nconst ALEO_TX_ID_PATTERN = /^at1[a-z0-9]{1,62}$/;\n\ntype GetAleoExplorerTxUrlParams = {\n /**\n * `0` = mainnet, `1` = testnet. When omitted, defaults to testnet so a\n * stale UI never accidentally links to a mainnet explorer for a testnet\n * transaction.\n */\n networkId?: number | string;\n /** Aleo transaction id (`at1...`). */\n txId: string;\n};\n\n/**\n * Build the Provable explorer URL for an Aleo transaction id.\n *\n * @example\n * ```ts\n * const url = getAleoExplorerTxUrl({ txId: 'at1abc...', networkId: 0 });\n * // → 'https://explorer.provable.com/transaction/at1abc...'\n * ```\n *\n * @param params.txId - Aleo transaction id (`at1...`).\n * @param [params.networkId] - `0` = mainnet, `1` = testnet.\n * @returns The Provable explorer URL for the transaction.\n * @throws {InvalidAleoTransactionIdError} When `txId` is not a well-formed bech32 Aleo transaction id.\n * @see getNetworkByChainId\n * @see isAleoWalletAccount\n * @notInstrumented\n */\nexport const getAleoExplorerTxUrl = ({\n txId,\n networkId,\n}: GetAleoExplorerTxUrlParams): string => {\n if (!ALEO_TX_ID_PATTERN.test(txId)) {\n throw new InvalidAleoTransactionIdError({ txId });\n }\n const subdomain = isAleoMainnet(networkId) ? '' : 'testnet.';\n return `https://${subdomain}explorer.provable.com/transaction/${txId}`;\n};\n","import { assertPackageVersion } from '@dynamic-labs-sdk/assert-package-version';\n\nimport {\n name as packageName,\n version as packageVersion,\n} from '../../package.json';\nassertPackageVersion(packageName, packageVersion);\n\nexport { AleoFeatureUnsupportedError } from '../errors/AleoFeatureUnsupportedError';\nexport { AleoInvalidTransactionError } from '../errors/AleoInvalidTransactionError';\nexport { InvalidAleoTransactionIdError } from '../errors/InvalidAleoTransactionIdError';\nexport { NotAleoProviderError } from '../errors/NotAleoProviderError';\nexport { isAleoNetworkProvider } from '../isAleoNetworkProvider';\nexport { isAleoWalletAccount } from '../isAleoWalletAccount';\nexport { isAleoWalletProvider } from '../isAleoWalletProvider';\nexport type { AleoNetworkProvider } from '../AleoNetworkProvider.types';\nexport type { AleoWalletAccount } from '../AleoWalletAccount.types';\nexport type {\n AleoDecryptOptions,\n AleoRequestRecordsOptions,\n AleoTransaction,\n AleoTransition,\n AleoWalletProvider,\n} from '../AleoWalletProvider.types';\nexport { getAleoExplorerTxUrl } from '../utils/getAleoExplorerTxUrl';\nexport {\n ALEO_NETWORK_TO_ID_MAP,\n getNetworkByChainId,\n} from '../utils/getNetworkByChainId';\nexport {\n ALEO_MAINNET_NETWORK_ID,\n ALEO_TESTNET_NETWORK_ID,\n MICROCREDITS_PER_CREDIT,\n} from '../utils/constants';\n"],"mappings":";;;;;;;;;;;;;;AASA,IAAa,uBAAb,cAA0C,UAAU;CAClD,AAAgB;CAEhB,YAAY,QAAiC;AAC3C,QAAM;GACJ,OAAO;GACP,MAAM;GACN,SAAS;GACT,MAAM;GACN,cAAc;GACf,CAAC;AAEF,OAAK,cAAc,OAAO;;;;;;;;;;;;;;;;;;;;;;;;ACC9B,MAAa,yBACX,oBAC2C,gBAAgB,UAAU;;;;;;;;;;;;;;;;;;;;;ACHvE,MAAa,uBACX,kBACuC,cAAc,UAAU;;;;;;;;;;;;;;;;;;;;;;;;ACEjE,MAAa,wBACX,aACmC;AACnC,QAAO,SAAS,UAAU;;;;;;;;;;;;;;;ACf5B,MAAa,iBACX,cACY;AACZ,KAAI,cAAc,UAAa,cAAc,KAAM,QAAO;AAC1D,KAAI,OAAO,cAAc,SACvB,QAAO,cAAc;AAEvB,QAAO,OAAO,UAAU,KAAK;;;;;;;;;;;;ACV/B,MAAM,qBAAqB;;;;;;;;;;;;;;;;;;AA8B3B,MAAa,wBAAwB,EACnC,MACA,gBACwC;AACxC,KAAI,CAAC,mBAAmB,KAAK,KAAK,CAChC,OAAM,IAAI,8BAA8B,EAAE,MAAM,CAAC;AAGnD,QAAO,WADW,cAAc,UAAU,GAAG,KAAK,WACtB,oCAAoC;;;;;AC1ClE,qBAAqBA,MAAaC,QAAe"}
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
const require_constants = require('./constants-BoB0_3u4.cjs');
|
|
2
|
+
let _dynamic_labs_sdk_client_core = require("@dynamic-labs-sdk/client/core");
|
|
3
|
+
|
|
4
|
+
//#region src/utils/formatMicrocreditsAsCredits/formatMicrocreditsAsCredits.ts
|
|
5
|
+
const MICROCREDITS_PER_CREDIT_BIGINT = BigInt(require_constants.MICROCREDITS_PER_CREDIT);
|
|
6
|
+
const MICROCREDIT_DECIMAL_DIGITS = String(require_constants.MICROCREDITS_PER_CREDIT).length - 1;
|
|
7
|
+
/**
|
|
8
|
+
* Bounded trailing-zero strip. `padStart` produces a fractional string
|
|
9
|
+
* of exactly `MICROCREDIT_DECIMAL_DIGITS` characters, so the quantifier
|
|
10
|
+
* upper bound matches the maximum possible run of trailing zeros —
|
|
11
|
+
* keeps the regex linear and avoids the unbounded-quantifier hotspot.
|
|
12
|
+
*/
|
|
13
|
+
const TRAILING_ZEROS_PATTERN = /0{1,6}$/;
|
|
14
|
+
/**
|
|
15
|
+
* Format microcredits (bigint) as a decimal credits string without
|
|
16
|
+
* losing precision for balances beyond `Number.MAX_SAFE_INTEGER`.
|
|
17
|
+
*
|
|
18
|
+
* The integer credits portion and the microcredits remainder are
|
|
19
|
+
* computed in the bigint domain, then the remainder is zero-padded to
|
|
20
|
+
* six digits and trailing fractional zeros are trimmed. Whole-credit
|
|
21
|
+
* balances render without a redundant `.000000` suffix.
|
|
22
|
+
*
|
|
23
|
+
* @notInstrumented
|
|
24
|
+
*/
|
|
25
|
+
const formatMicrocreditsAsCredits = (microcredits) => {
|
|
26
|
+
const credits = microcredits / MICROCREDITS_PER_CREDIT_BIGINT;
|
|
27
|
+
const fractional = (microcredits % MICROCREDITS_PER_CREDIT_BIGINT).toString().padStart(MICROCREDIT_DECIMAL_DIGITS, "0").replace(TRAILING_ZEROS_PATTERN, "");
|
|
28
|
+
return fractional.length > 0 ? `${credits}.${fractional}` : `${credits}`;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
//#endregion
|
|
32
|
+
//#region src/utils/parseAleoMicrocreditsLiteral/parseAleoMicrocreditsLiteral.ts
|
|
33
|
+
const U64_PRIVATE_SUFFIX = "u64.private";
|
|
34
|
+
const U64_SUFFIX = "u64";
|
|
35
|
+
/**
|
|
36
|
+
* Parse an Aleo VM mapping plaintext literal into microcredits.
|
|
37
|
+
*
|
|
38
|
+
* Accepts the shapes the Aleo `account` mapping endpoint actually
|
|
39
|
+
* emits — e.g. `1500000u64.private`, `1_500_000u64`, or a bare integer
|
|
40
|
+
* wrapped in JSON quotes. Strips the trailing `u64` / `u64.private`
|
|
41
|
+
* type suffix, surrounding quotes, and the cosmetic `_` group
|
|
42
|
+
* separators Aleo plaintext literals allow.
|
|
43
|
+
*
|
|
44
|
+
* Returns `0n` for empty input and for any payload that doesn't parse
|
|
45
|
+
* as an integer. `BigInt()` throws `SyntaxError` on anything that
|
|
46
|
+
* isn't a parseable integer — a corrupted RPC response, a mid-flight
|
|
47
|
+
* truncation, or an upstream API change could deliver such a payload,
|
|
48
|
+
* and we'd rather show a zero balance than crash the network-provider
|
|
49
|
+
* machinery with an unhandled rejection.
|
|
50
|
+
*
|
|
51
|
+
* @notInstrumented
|
|
52
|
+
*/
|
|
53
|
+
const parseAleoMicrocreditsLiteral = (raw) => {
|
|
54
|
+
let cleaned = raw.trim();
|
|
55
|
+
if (cleaned.startsWith("\"") && cleaned.endsWith("\"")) cleaned = cleaned.slice(1, -1);
|
|
56
|
+
if (cleaned.endsWith(U64_PRIVATE_SUFFIX)) cleaned = cleaned.slice(0, -11);
|
|
57
|
+
else if (cleaned.endsWith(U64_SUFFIX)) cleaned = cleaned.slice(0, -3);
|
|
58
|
+
cleaned = cleaned.split("_").join("").trim();
|
|
59
|
+
if (!cleaned) return BigInt(0);
|
|
60
|
+
try {
|
|
61
|
+
return BigInt(cleaned);
|
|
62
|
+
} catch {
|
|
63
|
+
return BigInt(0);
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
//#endregion
|
|
68
|
+
//#region src/utils/fetchAleoBalance/fetchAleoBalance.ts
|
|
69
|
+
/**
|
|
70
|
+
* Aleo addresses are bech32-encoded — lowercase alphanumerics only — so we
|
|
71
|
+
* can validate strictly. Refusing anything else before it reaches the URL
|
|
72
|
+
* keeps `/`, `?`, `#`, `..`, etc. out of the request path (SSRF / path
|
|
73
|
+
* injection defence).
|
|
74
|
+
*/
|
|
75
|
+
const ALEO_ADDRESS_PATTERN = /^aleo1[a-z0-9]{1,62}$/;
|
|
76
|
+
/**
|
|
77
|
+
* Fetches the public credits balance for an Aleo address by querying the
|
|
78
|
+
* `account` mapping on `credits.aleo` via the network's HTTP RPC. Returns
|
|
79
|
+
* the balance as a credits string (private records are not visible here —
|
|
80
|
+
* they live on the wallet and require a connected adapter to enumerate).
|
|
81
|
+
*
|
|
82
|
+
* Returns `null` when the address is malformed or no RPC URL is
|
|
83
|
+
* configured; returns `'0'` for any other failure mode (non-OK status,
|
|
84
|
+
* empty body, `null` mapping, unparseable payload). This keeps the
|
|
85
|
+
* function fail-soft for the network-provider machinery, which surfaces
|
|
86
|
+
* the result directly to the UI.
|
|
87
|
+
*
|
|
88
|
+
* @notInstrumented
|
|
89
|
+
*/
|
|
90
|
+
const fetchAleoBalance = async ({ address, networkData }) => {
|
|
91
|
+
if (!ALEO_ADDRESS_PATTERN.test(address)) return null;
|
|
92
|
+
const baseUrl = networkData.rpcUrls.http[0];
|
|
93
|
+
if (!baseUrl) return null;
|
|
94
|
+
const base = baseUrl.endsWith("/") ? baseUrl : `${baseUrl}/`;
|
|
95
|
+
const url = new URL(`program/credits.aleo/mapping/account/${encodeURIComponent(address)}`, base);
|
|
96
|
+
const response = await fetch(url.toString());
|
|
97
|
+
if (!response.ok) return "0";
|
|
98
|
+
const text = await response.text();
|
|
99
|
+
if (!text || text === "null") return "0";
|
|
100
|
+
const microcredits = parseAleoMicrocreditsLiteral(text);
|
|
101
|
+
if (microcredits <= BigInt(0)) return "0";
|
|
102
|
+
return formatMicrocreditsAsCredits(microcredits);
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
//#endregion
|
|
106
|
+
//#region src/utils/createAleoNetworkProvider/createAleoNetworkProvider.ts
|
|
107
|
+
/**
|
|
108
|
+
* Build an `AleoNetworkProvider` bound to a specific `NetworkData`
|
|
109
|
+
* (mainnet vs. testnet — anything the SDK config exposes). The provider
|
|
110
|
+
* exposes a `getBalance({ address })` that proxies to
|
|
111
|
+
* `fetchAleoBalance` over the configured RPC, returning public credits
|
|
112
|
+
* only (private records require a connected wallet adapter).
|
|
113
|
+
*
|
|
114
|
+
* Called by `registerAleoNetworkProviderBuilder` — not invoked directly.
|
|
115
|
+
*
|
|
116
|
+
* @notInstrumented
|
|
117
|
+
*/
|
|
118
|
+
const createAleoNetworkProvider = (networkData) => ({
|
|
119
|
+
...(0, _dynamic_labs_sdk_client_core.createBaseNetworkProvider)("ALEO", networkData),
|
|
120
|
+
getBalance: async ({ address }) => ({ balance: await fetchAleoBalance({
|
|
121
|
+
address,
|
|
122
|
+
networkData
|
|
123
|
+
}) })
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
//#endregion
|
|
127
|
+
//#region src/registerAleoNetworkProviderBuilder/registerAleoNetworkProviderBuilder.ts
|
|
128
|
+
/**
|
|
129
|
+
* Register the Aleo network-provider builder on a Dynamic client.
|
|
130
|
+
*
|
|
131
|
+
* Wires `createAleoNetworkProvider` into the client's network-provider
|
|
132
|
+
* builder registry under the `'ALEO'` chain key, which is how the SDK's
|
|
133
|
+
* network machinery materializes a per-network provider on demand.
|
|
134
|
+
* Idempotent — a second call is a no-op once the builder is registered.
|
|
135
|
+
*
|
|
136
|
+
* Internal: called by the Aleo extension entry points
|
|
137
|
+
* (`addAleoWalletStandardExtension`, `addAleoWalletAdaptersExtension`,
|
|
138
|
+
* `addAleoExtension`). Not exported from the package's public surface.
|
|
139
|
+
*
|
|
140
|
+
* @notInstrumented
|
|
141
|
+
*/
|
|
142
|
+
const registerAleoNetworkProviderBuilder = (client) => {
|
|
143
|
+
const networkProviderBuilderRegistry = (0, _dynamic_labs_sdk_client_core.getNetworkProviderBuilderRegistry)(client);
|
|
144
|
+
if (networkProviderBuilderRegistry.get().get("ALEO")) return;
|
|
145
|
+
networkProviderBuilderRegistry.register({
|
|
146
|
+
builder: createAleoNetworkProvider,
|
|
147
|
+
chain: "ALEO"
|
|
148
|
+
});
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
//#endregion
|
|
152
|
+
Object.defineProperty(exports, 'registerAleoNetworkProviderBuilder', {
|
|
153
|
+
enumerable: true,
|
|
154
|
+
get: function () {
|
|
155
|
+
return registerAleoNetworkProviderBuilder;
|
|
156
|
+
}
|
|
157
|
+
});
|
|
158
|
+
//# sourceMappingURL=registerAleoNetworkProviderBuilder-ByQwv6MA.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"registerAleoNetworkProviderBuilder-ByQwv6MA.cjs","names":["MICROCREDITS_PER_CREDIT"],"sources":["../src/utils/formatMicrocreditsAsCredits/formatMicrocreditsAsCredits.ts","../src/utils/parseAleoMicrocreditsLiteral/parseAleoMicrocreditsLiteral.ts","../src/utils/fetchAleoBalance/fetchAleoBalance.ts","../src/utils/createAleoNetworkProvider/createAleoNetworkProvider.ts","../src/registerAleoNetworkProviderBuilder/registerAleoNetworkProviderBuilder.ts"],"sourcesContent":["import { MICROCREDITS_PER_CREDIT } from '../constants';\n\nconst MICROCREDITS_PER_CREDIT_BIGINT = BigInt(MICROCREDITS_PER_CREDIT);\nconst MICROCREDIT_DECIMAL_DIGITS = String(MICROCREDITS_PER_CREDIT).length - 1;\n\n/**\n * Bounded trailing-zero strip. `padStart` produces a fractional string\n * of exactly `MICROCREDIT_DECIMAL_DIGITS` characters, so the quantifier\n * upper bound matches the maximum possible run of trailing zeros —\n * keeps the regex linear and avoids the unbounded-quantifier hotspot.\n */\nconst TRAILING_ZEROS_PATTERN = /0{1,6}$/;\n\n/**\n * Format microcredits (bigint) as a decimal credits string without\n * losing precision for balances beyond `Number.MAX_SAFE_INTEGER`.\n *\n * The integer credits portion and the microcredits remainder are\n * computed in the bigint domain, then the remainder is zero-padded to\n * six digits and trailing fractional zeros are trimmed. Whole-credit\n * balances render without a redundant `.000000` suffix.\n *\n * @notInstrumented\n */\n// eslint-disable-next-line custom-rules/require-single-object-param -- single bigint arg; named-arg ceremony would not improve clarity\nexport const formatMicrocreditsAsCredits = (microcredits: bigint): string => {\n const credits = microcredits / MICROCREDITS_PER_CREDIT_BIGINT;\n const remainder = microcredits % MICROCREDITS_PER_CREDIT_BIGINT;\n const fractional = remainder\n .toString()\n .padStart(MICROCREDIT_DECIMAL_DIGITS, '0')\n .replace(TRAILING_ZEROS_PATTERN, '');\n return fractional.length > 0 ? `${credits}.${fractional}` : `${credits}`;\n};\n","const U64_PRIVATE_SUFFIX = 'u64.private';\nconst U64_SUFFIX = 'u64';\n\n/**\n * Parse an Aleo VM mapping plaintext literal into microcredits.\n *\n * Accepts the shapes the Aleo `account` mapping endpoint actually\n * emits — e.g. `1500000u64.private`, `1_500_000u64`, or a bare integer\n * wrapped in JSON quotes. Strips the trailing `u64` / `u64.private`\n * type suffix, surrounding quotes, and the cosmetic `_` group\n * separators Aleo plaintext literals allow.\n *\n * Returns `0n` for empty input and for any payload that doesn't parse\n * as an integer. `BigInt()` throws `SyntaxError` on anything that\n * isn't a parseable integer — a corrupted RPC response, a mid-flight\n * truncation, or an upstream API change could deliver such a payload,\n * and we'd rather show a zero balance than crash the network-provider\n * machinery with an unhandled rejection.\n *\n * @notInstrumented\n */\n// eslint-disable-next-line custom-rules/require-single-object-param -- single string arg; named-arg ceremony would not improve clarity\nexport const parseAleoMicrocreditsLiteral = (raw: string): bigint => {\n let cleaned = raw.trim();\n if (cleaned.startsWith('\"') && cleaned.endsWith('\"')) {\n cleaned = cleaned.slice(1, -1);\n }\n if (cleaned.endsWith(U64_PRIVATE_SUFFIX)) {\n cleaned = cleaned.slice(0, -U64_PRIVATE_SUFFIX.length);\n } else if (cleaned.endsWith(U64_SUFFIX)) {\n cleaned = cleaned.slice(0, -U64_SUFFIX.length);\n }\n cleaned = cleaned.split('_').join('').trim();\n if (!cleaned) return BigInt(0);\n try {\n return BigInt(cleaned);\n } catch {\n return BigInt(0);\n }\n};\n","import type { NetworkData } from '@dynamic-labs-sdk/client';\n\nimport { formatMicrocreditsAsCredits } from '../formatMicrocreditsAsCredits';\nimport { parseAleoMicrocreditsLiteral } from '../parseAleoMicrocreditsLiteral';\n\ntype FetchAleoBalanceParams = {\n address: string;\n networkData: NetworkData;\n};\n\n/**\n * Aleo addresses are bech32-encoded — lowercase alphanumerics only — so we\n * can validate strictly. Refusing anything else before it reaches the URL\n * keeps `/`, `?`, `#`, `..`, etc. out of the request path (SSRF / path\n * injection defence).\n */\nconst ALEO_ADDRESS_PATTERN = /^aleo1[a-z0-9]{1,62}$/;\n\n/**\n * Fetches the public credits balance for an Aleo address by querying the\n * `account` mapping on `credits.aleo` via the network's HTTP RPC. Returns\n * the balance as a credits string (private records are not visible here —\n * they live on the wallet and require a connected adapter to enumerate).\n *\n * Returns `null` when the address is malformed or no RPC URL is\n * configured; returns `'0'` for any other failure mode (non-OK status,\n * empty body, `null` mapping, unparseable payload). This keeps the\n * function fail-soft for the network-provider machinery, which surfaces\n * the result directly to the UI.\n *\n * @notInstrumented\n */\nexport const fetchAleoBalance = async ({\n address,\n networkData,\n}: FetchAleoBalanceParams): Promise<string | null> => {\n if (!ALEO_ADDRESS_PATTERN.test(address)) {\n return null;\n }\n\n const baseUrl = networkData.rpcUrls.http[0];\n if (!baseUrl) {\n return null;\n }\n\n const base = baseUrl.endsWith('/') ? baseUrl : `${baseUrl}/`;\n const url = new URL(\n `program/credits.aleo/mapping/account/${encodeURIComponent(address)}`,\n base\n );\n const response = await fetch(url.toString());\n if (!response.ok) {\n return '0';\n }\n\n const text = await response.text();\n if (!text || text === 'null') {\n return '0';\n }\n\n const microcredits = parseAleoMicrocreditsLiteral(text);\n if (microcredits <= BigInt(0)) {\n return '0';\n }\n\n return formatMicrocreditsAsCredits(microcredits);\n};\n","import type { NetworkData } from '@dynamic-labs-sdk/client';\nimport { createBaseNetworkProvider } from '@dynamic-labs-sdk/client/core';\n\nimport type { AleoNetworkProvider } from '../../AleoNetworkProvider.types';\nimport { fetchAleoBalance } from '../fetchAleoBalance';\n\n/**\n * Build an `AleoNetworkProvider` bound to a specific `NetworkData`\n * (mainnet vs. testnet — anything the SDK config exposes). The provider\n * exposes a `getBalance({ address })` that proxies to\n * `fetchAleoBalance` over the configured RPC, returning public credits\n * only (private records require a connected wallet adapter).\n *\n * Called by `registerAleoNetworkProviderBuilder` — not invoked directly.\n *\n * @notInstrumented\n */\n \nexport const createAleoNetworkProvider = (\n networkData: NetworkData\n): AleoNetworkProvider => ({\n ...createBaseNetworkProvider('ALEO', networkData),\n getBalance: async ({ address }) => ({\n balance: await fetchAleoBalance({\n address,\n networkData,\n }),\n }),\n});\n","import type { DynamicClient } from '@dynamic-labs-sdk/client';\nimport { getNetworkProviderBuilderRegistry } from '@dynamic-labs-sdk/client/core';\n\nimport { createAleoNetworkProvider } from '../utils/createAleoNetworkProvider';\n\n/**\n * Register the Aleo network-provider builder on a Dynamic client.\n *\n * Wires `createAleoNetworkProvider` into the client's network-provider\n * builder registry under the `'ALEO'` chain key, which is how the SDK's\n * network machinery materializes a per-network provider on demand.\n * Idempotent — a second call is a no-op once the builder is registered.\n *\n * Internal: called by the Aleo extension entry points\n * (`addAleoWalletStandardExtension`, `addAleoWalletAdaptersExtension`,\n * `addAleoExtension`). Not exported from the package's public surface.\n *\n * @notInstrumented\n */\nexport const registerAleoNetworkProviderBuilder = (\n client: DynamicClient\n): void => {\n const networkProviderBuilderRegistry =\n getNetworkProviderBuilderRegistry(client);\n\n if (networkProviderBuilderRegistry.get().get('ALEO')) {\n return;\n }\n\n networkProviderBuilderRegistry.register({\n builder: createAleoNetworkProvider,\n chain: 'ALEO',\n });\n};\n"],"mappings":";;;;AAEA,MAAM,iCAAiC,OAAOA,0CAAwB;AACtE,MAAM,6BAA6B,OAAOA,0CAAwB,CAAC,SAAS;;;;;;;AAQ5E,MAAM,yBAAyB;;;;;;;;;;;;AAc/B,MAAa,+BAA+B,iBAAiC;CAC3E,MAAM,UAAU,eAAe;CAE/B,MAAM,cADY,eAAe,gCAE9B,UAAU,CACV,SAAS,4BAA4B,IAAI,CACzC,QAAQ,wBAAwB,GAAG;AACtC,QAAO,WAAW,SAAS,IAAI,GAAG,QAAQ,GAAG,eAAe,GAAG;;;;;AChCjE,MAAM,qBAAqB;AAC3B,MAAM,aAAa;;;;;;;;;;;;;;;;;;;AAqBnB,MAAa,gCAAgC,QAAwB;CACnE,IAAI,UAAU,IAAI,MAAM;AACxB,KAAI,QAAQ,WAAW,KAAI,IAAI,QAAQ,SAAS,KAAI,CAClD,WAAU,QAAQ,MAAM,GAAG,GAAG;AAEhC,KAAI,QAAQ,SAAS,mBAAmB,CACtC,WAAU,QAAQ,MAAM,GAAG,IAA2B;UAC7C,QAAQ,SAAS,WAAW,CACrC,WAAU,QAAQ,MAAM,GAAG,GAAmB;AAEhD,WAAU,QAAQ,MAAM,IAAI,CAAC,KAAK,GAAG,CAAC,MAAM;AAC5C,KAAI,CAAC,QAAS,QAAO,OAAO,EAAE;AAC9B,KAAI;AACF,SAAO,OAAO,QAAQ;SAChB;AACN,SAAO,OAAO,EAAE;;;;;;;;;;;;ACrBpB,MAAM,uBAAuB;;;;;;;;;;;;;;;AAgB7B,MAAa,mBAAmB,OAAO,EACrC,SACA,kBACoD;AACpD,KAAI,CAAC,qBAAqB,KAAK,QAAQ,CACrC,QAAO;CAGT,MAAM,UAAU,YAAY,QAAQ,KAAK;AACzC,KAAI,CAAC,QACH,QAAO;CAGT,MAAM,OAAO,QAAQ,SAAS,IAAI,GAAG,UAAU,GAAG,QAAQ;CAC1D,MAAM,MAAM,IAAI,IACd,wCAAwC,mBAAmB,QAAQ,IACnE,KACD;CACD,MAAM,WAAW,MAAM,MAAM,IAAI,UAAU,CAAC;AAC5C,KAAI,CAAC,SAAS,GACZ,QAAO;CAGT,MAAM,OAAO,MAAM,SAAS,MAAM;AAClC,KAAI,CAAC,QAAQ,SAAS,OACpB,QAAO;CAGT,MAAM,eAAe,6BAA6B,KAAK;AACvD,KAAI,gBAAgB,OAAO,EAAE,CAC3B,QAAO;AAGT,QAAO,4BAA4B,aAAa;;;;;;;;;;;;;;;;AC/ClD,MAAa,6BACX,iBACyB;CACzB,gEAA6B,QAAQ,YAAY;CACjD,YAAY,OAAO,EAAE,eAAe,EAClC,SAAS,MAAM,iBAAiB;EAC9B;EACA;EACD,CAAC,EACH;CACF;;;;;;;;;;;;;;;;;;ACTD,MAAa,sCACX,WACS;CACT,MAAM,sGAC8B,OAAO;AAE3C,KAAI,+BAA+B,KAAK,CAAC,IAAI,OAAO,CAClD;AAGF,gCAA+B,SAAS;EACtC,SAAS;EACT,OAAO;EACR,CAAC"}
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
import { r as MICROCREDITS_PER_CREDIT } from "./constants-DZ9sVn7x.esm.js";
|
|
2
|
+
import { createBaseNetworkProvider, getNetworkProviderBuilderRegistry } from "@dynamic-labs-sdk/client/core";
|
|
3
|
+
|
|
4
|
+
//#region src/utils/formatMicrocreditsAsCredits/formatMicrocreditsAsCredits.ts
|
|
5
|
+
const MICROCREDITS_PER_CREDIT_BIGINT = BigInt(MICROCREDITS_PER_CREDIT);
|
|
6
|
+
const MICROCREDIT_DECIMAL_DIGITS = String(MICROCREDITS_PER_CREDIT).length - 1;
|
|
7
|
+
/**
|
|
8
|
+
* Bounded trailing-zero strip. `padStart` produces a fractional string
|
|
9
|
+
* of exactly `MICROCREDIT_DECIMAL_DIGITS` characters, so the quantifier
|
|
10
|
+
* upper bound matches the maximum possible run of trailing zeros —
|
|
11
|
+
* keeps the regex linear and avoids the unbounded-quantifier hotspot.
|
|
12
|
+
*/
|
|
13
|
+
const TRAILING_ZEROS_PATTERN = /0{1,6}$/;
|
|
14
|
+
/**
|
|
15
|
+
* Format microcredits (bigint) as a decimal credits string without
|
|
16
|
+
* losing precision for balances beyond `Number.MAX_SAFE_INTEGER`.
|
|
17
|
+
*
|
|
18
|
+
* The integer credits portion and the microcredits remainder are
|
|
19
|
+
* computed in the bigint domain, then the remainder is zero-padded to
|
|
20
|
+
* six digits and trailing fractional zeros are trimmed. Whole-credit
|
|
21
|
+
* balances render without a redundant `.000000` suffix.
|
|
22
|
+
*
|
|
23
|
+
* @notInstrumented
|
|
24
|
+
*/
|
|
25
|
+
const formatMicrocreditsAsCredits = (microcredits) => {
|
|
26
|
+
const credits = microcredits / MICROCREDITS_PER_CREDIT_BIGINT;
|
|
27
|
+
const fractional = (microcredits % MICROCREDITS_PER_CREDIT_BIGINT).toString().padStart(MICROCREDIT_DECIMAL_DIGITS, "0").replace(TRAILING_ZEROS_PATTERN, "");
|
|
28
|
+
return fractional.length > 0 ? `${credits}.${fractional}` : `${credits}`;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
//#endregion
|
|
32
|
+
//#region src/utils/parseAleoMicrocreditsLiteral/parseAleoMicrocreditsLiteral.ts
|
|
33
|
+
const U64_PRIVATE_SUFFIX = "u64.private";
|
|
34
|
+
const U64_SUFFIX = "u64";
|
|
35
|
+
/**
|
|
36
|
+
* Parse an Aleo VM mapping plaintext literal into microcredits.
|
|
37
|
+
*
|
|
38
|
+
* Accepts the shapes the Aleo `account` mapping endpoint actually
|
|
39
|
+
* emits — e.g. `1500000u64.private`, `1_500_000u64`, or a bare integer
|
|
40
|
+
* wrapped in JSON quotes. Strips the trailing `u64` / `u64.private`
|
|
41
|
+
* type suffix, surrounding quotes, and the cosmetic `_` group
|
|
42
|
+
* separators Aleo plaintext literals allow.
|
|
43
|
+
*
|
|
44
|
+
* Returns `0n` for empty input and for any payload that doesn't parse
|
|
45
|
+
* as an integer. `BigInt()` throws `SyntaxError` on anything that
|
|
46
|
+
* isn't a parseable integer — a corrupted RPC response, a mid-flight
|
|
47
|
+
* truncation, or an upstream API change could deliver such a payload,
|
|
48
|
+
* and we'd rather show a zero balance than crash the network-provider
|
|
49
|
+
* machinery with an unhandled rejection.
|
|
50
|
+
*
|
|
51
|
+
* @notInstrumented
|
|
52
|
+
*/
|
|
53
|
+
const parseAleoMicrocreditsLiteral = (raw) => {
|
|
54
|
+
let cleaned = raw.trim();
|
|
55
|
+
if (cleaned.startsWith("\"") && cleaned.endsWith("\"")) cleaned = cleaned.slice(1, -1);
|
|
56
|
+
if (cleaned.endsWith(U64_PRIVATE_SUFFIX)) cleaned = cleaned.slice(0, -11);
|
|
57
|
+
else if (cleaned.endsWith(U64_SUFFIX)) cleaned = cleaned.slice(0, -3);
|
|
58
|
+
cleaned = cleaned.split("_").join("").trim();
|
|
59
|
+
if (!cleaned) return BigInt(0);
|
|
60
|
+
try {
|
|
61
|
+
return BigInt(cleaned);
|
|
62
|
+
} catch {
|
|
63
|
+
return BigInt(0);
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
//#endregion
|
|
68
|
+
//#region src/utils/fetchAleoBalance/fetchAleoBalance.ts
|
|
69
|
+
/**
|
|
70
|
+
* Aleo addresses are bech32-encoded — lowercase alphanumerics only — so we
|
|
71
|
+
* can validate strictly. Refusing anything else before it reaches the URL
|
|
72
|
+
* keeps `/`, `?`, `#`, `..`, etc. out of the request path (SSRF / path
|
|
73
|
+
* injection defence).
|
|
74
|
+
*/
|
|
75
|
+
const ALEO_ADDRESS_PATTERN = /^aleo1[a-z0-9]{1,62}$/;
|
|
76
|
+
/**
|
|
77
|
+
* Fetches the public credits balance for an Aleo address by querying the
|
|
78
|
+
* `account` mapping on `credits.aleo` via the network's HTTP RPC. Returns
|
|
79
|
+
* the balance as a credits string (private records are not visible here —
|
|
80
|
+
* they live on the wallet and require a connected adapter to enumerate).
|
|
81
|
+
*
|
|
82
|
+
* Returns `null` when the address is malformed or no RPC URL is
|
|
83
|
+
* configured; returns `'0'` for any other failure mode (non-OK status,
|
|
84
|
+
* empty body, `null` mapping, unparseable payload). This keeps the
|
|
85
|
+
* function fail-soft for the network-provider machinery, which surfaces
|
|
86
|
+
* the result directly to the UI.
|
|
87
|
+
*
|
|
88
|
+
* @notInstrumented
|
|
89
|
+
*/
|
|
90
|
+
const fetchAleoBalance = async ({ address, networkData }) => {
|
|
91
|
+
if (!ALEO_ADDRESS_PATTERN.test(address)) return null;
|
|
92
|
+
const baseUrl = networkData.rpcUrls.http[0];
|
|
93
|
+
if (!baseUrl) return null;
|
|
94
|
+
const base = baseUrl.endsWith("/") ? baseUrl : `${baseUrl}/`;
|
|
95
|
+
const url = new URL(`program/credits.aleo/mapping/account/${encodeURIComponent(address)}`, base);
|
|
96
|
+
const response = await fetch(url.toString());
|
|
97
|
+
if (!response.ok) return "0";
|
|
98
|
+
const text = await response.text();
|
|
99
|
+
if (!text || text === "null") return "0";
|
|
100
|
+
const microcredits = parseAleoMicrocreditsLiteral(text);
|
|
101
|
+
if (microcredits <= BigInt(0)) return "0";
|
|
102
|
+
return formatMicrocreditsAsCredits(microcredits);
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
//#endregion
|
|
106
|
+
//#region src/utils/createAleoNetworkProvider/createAleoNetworkProvider.ts
|
|
107
|
+
/**
|
|
108
|
+
* Build an `AleoNetworkProvider` bound to a specific `NetworkData`
|
|
109
|
+
* (mainnet vs. testnet — anything the SDK config exposes). The provider
|
|
110
|
+
* exposes a `getBalance({ address })` that proxies to
|
|
111
|
+
* `fetchAleoBalance` over the configured RPC, returning public credits
|
|
112
|
+
* only (private records require a connected wallet adapter).
|
|
113
|
+
*
|
|
114
|
+
* Called by `registerAleoNetworkProviderBuilder` — not invoked directly.
|
|
115
|
+
*
|
|
116
|
+
* @notInstrumented
|
|
117
|
+
*/
|
|
118
|
+
const createAleoNetworkProvider = (networkData) => ({
|
|
119
|
+
...createBaseNetworkProvider("ALEO", networkData),
|
|
120
|
+
getBalance: async ({ address }) => ({ balance: await fetchAleoBalance({
|
|
121
|
+
address,
|
|
122
|
+
networkData
|
|
123
|
+
}) })
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
//#endregion
|
|
127
|
+
//#region src/registerAleoNetworkProviderBuilder/registerAleoNetworkProviderBuilder.ts
|
|
128
|
+
/**
|
|
129
|
+
* Register the Aleo network-provider builder on a Dynamic client.
|
|
130
|
+
*
|
|
131
|
+
* Wires `createAleoNetworkProvider` into the client's network-provider
|
|
132
|
+
* builder registry under the `'ALEO'` chain key, which is how the SDK's
|
|
133
|
+
* network machinery materializes a per-network provider on demand.
|
|
134
|
+
* Idempotent — a second call is a no-op once the builder is registered.
|
|
135
|
+
*
|
|
136
|
+
* Internal: called by the Aleo extension entry points
|
|
137
|
+
* (`addAleoWalletStandardExtension`, `addAleoWalletAdaptersExtension`,
|
|
138
|
+
* `addAleoExtension`). Not exported from the package's public surface.
|
|
139
|
+
*
|
|
140
|
+
* @notInstrumented
|
|
141
|
+
*/
|
|
142
|
+
const registerAleoNetworkProviderBuilder = (client) => {
|
|
143
|
+
const networkProviderBuilderRegistry = getNetworkProviderBuilderRegistry(client);
|
|
144
|
+
if (networkProviderBuilderRegistry.get().get("ALEO")) return;
|
|
145
|
+
networkProviderBuilderRegistry.register({
|
|
146
|
+
builder: createAleoNetworkProvider,
|
|
147
|
+
chain: "ALEO"
|
|
148
|
+
});
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
//#endregion
|
|
152
|
+
export { registerAleoNetworkProviderBuilder as t };
|
|
153
|
+
//# sourceMappingURL=registerAleoNetworkProviderBuilder-CMcQJWs3.esm.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"registerAleoNetworkProviderBuilder-CMcQJWs3.esm.js","names":[],"sources":["../src/utils/formatMicrocreditsAsCredits/formatMicrocreditsAsCredits.ts","../src/utils/parseAleoMicrocreditsLiteral/parseAleoMicrocreditsLiteral.ts","../src/utils/fetchAleoBalance/fetchAleoBalance.ts","../src/utils/createAleoNetworkProvider/createAleoNetworkProvider.ts","../src/registerAleoNetworkProviderBuilder/registerAleoNetworkProviderBuilder.ts"],"sourcesContent":["import { MICROCREDITS_PER_CREDIT } from '../constants';\n\nconst MICROCREDITS_PER_CREDIT_BIGINT = BigInt(MICROCREDITS_PER_CREDIT);\nconst MICROCREDIT_DECIMAL_DIGITS = String(MICROCREDITS_PER_CREDIT).length - 1;\n\n/**\n * Bounded trailing-zero strip. `padStart` produces a fractional string\n * of exactly `MICROCREDIT_DECIMAL_DIGITS` characters, so the quantifier\n * upper bound matches the maximum possible run of trailing zeros —\n * keeps the regex linear and avoids the unbounded-quantifier hotspot.\n */\nconst TRAILING_ZEROS_PATTERN = /0{1,6}$/;\n\n/**\n * Format microcredits (bigint) as a decimal credits string without\n * losing precision for balances beyond `Number.MAX_SAFE_INTEGER`.\n *\n * The integer credits portion and the microcredits remainder are\n * computed in the bigint domain, then the remainder is zero-padded to\n * six digits and trailing fractional zeros are trimmed. Whole-credit\n * balances render without a redundant `.000000` suffix.\n *\n * @notInstrumented\n */\n// eslint-disable-next-line custom-rules/require-single-object-param -- single bigint arg; named-arg ceremony would not improve clarity\nexport const formatMicrocreditsAsCredits = (microcredits: bigint): string => {\n const credits = microcredits / MICROCREDITS_PER_CREDIT_BIGINT;\n const remainder = microcredits % MICROCREDITS_PER_CREDIT_BIGINT;\n const fractional = remainder\n .toString()\n .padStart(MICROCREDIT_DECIMAL_DIGITS, '0')\n .replace(TRAILING_ZEROS_PATTERN, '');\n return fractional.length > 0 ? `${credits}.${fractional}` : `${credits}`;\n};\n","const U64_PRIVATE_SUFFIX = 'u64.private';\nconst U64_SUFFIX = 'u64';\n\n/**\n * Parse an Aleo VM mapping plaintext literal into microcredits.\n *\n * Accepts the shapes the Aleo `account` mapping endpoint actually\n * emits — e.g. `1500000u64.private`, `1_500_000u64`, or a bare integer\n * wrapped in JSON quotes. Strips the trailing `u64` / `u64.private`\n * type suffix, surrounding quotes, and the cosmetic `_` group\n * separators Aleo plaintext literals allow.\n *\n * Returns `0n` for empty input and for any payload that doesn't parse\n * as an integer. `BigInt()` throws `SyntaxError` on anything that\n * isn't a parseable integer — a corrupted RPC response, a mid-flight\n * truncation, or an upstream API change could deliver such a payload,\n * and we'd rather show a zero balance than crash the network-provider\n * machinery with an unhandled rejection.\n *\n * @notInstrumented\n */\n// eslint-disable-next-line custom-rules/require-single-object-param -- single string arg; named-arg ceremony would not improve clarity\nexport const parseAleoMicrocreditsLiteral = (raw: string): bigint => {\n let cleaned = raw.trim();\n if (cleaned.startsWith('\"') && cleaned.endsWith('\"')) {\n cleaned = cleaned.slice(1, -1);\n }\n if (cleaned.endsWith(U64_PRIVATE_SUFFIX)) {\n cleaned = cleaned.slice(0, -U64_PRIVATE_SUFFIX.length);\n } else if (cleaned.endsWith(U64_SUFFIX)) {\n cleaned = cleaned.slice(0, -U64_SUFFIX.length);\n }\n cleaned = cleaned.split('_').join('').trim();\n if (!cleaned) return BigInt(0);\n try {\n return BigInt(cleaned);\n } catch {\n return BigInt(0);\n }\n};\n","import type { NetworkData } from '@dynamic-labs-sdk/client';\n\nimport { formatMicrocreditsAsCredits } from '../formatMicrocreditsAsCredits';\nimport { parseAleoMicrocreditsLiteral } from '../parseAleoMicrocreditsLiteral';\n\ntype FetchAleoBalanceParams = {\n address: string;\n networkData: NetworkData;\n};\n\n/**\n * Aleo addresses are bech32-encoded — lowercase alphanumerics only — so we\n * can validate strictly. Refusing anything else before it reaches the URL\n * keeps `/`, `?`, `#`, `..`, etc. out of the request path (SSRF / path\n * injection defence).\n */\nconst ALEO_ADDRESS_PATTERN = /^aleo1[a-z0-9]{1,62}$/;\n\n/**\n * Fetches the public credits balance for an Aleo address by querying the\n * `account` mapping on `credits.aleo` via the network's HTTP RPC. Returns\n * the balance as a credits string (private records are not visible here —\n * they live on the wallet and require a connected adapter to enumerate).\n *\n * Returns `null` when the address is malformed or no RPC URL is\n * configured; returns `'0'` for any other failure mode (non-OK status,\n * empty body, `null` mapping, unparseable payload). This keeps the\n * function fail-soft for the network-provider machinery, which surfaces\n * the result directly to the UI.\n *\n * @notInstrumented\n */\nexport const fetchAleoBalance = async ({\n address,\n networkData,\n}: FetchAleoBalanceParams): Promise<string | null> => {\n if (!ALEO_ADDRESS_PATTERN.test(address)) {\n return null;\n }\n\n const baseUrl = networkData.rpcUrls.http[0];\n if (!baseUrl) {\n return null;\n }\n\n const base = baseUrl.endsWith('/') ? baseUrl : `${baseUrl}/`;\n const url = new URL(\n `program/credits.aleo/mapping/account/${encodeURIComponent(address)}`,\n base\n );\n const response = await fetch(url.toString());\n if (!response.ok) {\n return '0';\n }\n\n const text = await response.text();\n if (!text || text === 'null') {\n return '0';\n }\n\n const microcredits = parseAleoMicrocreditsLiteral(text);\n if (microcredits <= BigInt(0)) {\n return '0';\n }\n\n return formatMicrocreditsAsCredits(microcredits);\n};\n","import type { NetworkData } from '@dynamic-labs-sdk/client';\nimport { createBaseNetworkProvider } from '@dynamic-labs-sdk/client/core';\n\nimport type { AleoNetworkProvider } from '../../AleoNetworkProvider.types';\nimport { fetchAleoBalance } from '../fetchAleoBalance';\n\n/**\n * Build an `AleoNetworkProvider` bound to a specific `NetworkData`\n * (mainnet vs. testnet — anything the SDK config exposes). The provider\n * exposes a `getBalance({ address })` that proxies to\n * `fetchAleoBalance` over the configured RPC, returning public credits\n * only (private records require a connected wallet adapter).\n *\n * Called by `registerAleoNetworkProviderBuilder` — not invoked directly.\n *\n * @notInstrumented\n */\n \nexport const createAleoNetworkProvider = (\n networkData: NetworkData\n): AleoNetworkProvider => ({\n ...createBaseNetworkProvider('ALEO', networkData),\n getBalance: async ({ address }) => ({\n balance: await fetchAleoBalance({\n address,\n networkData,\n }),\n }),\n});\n","import type { DynamicClient } from '@dynamic-labs-sdk/client';\nimport { getNetworkProviderBuilderRegistry } from '@dynamic-labs-sdk/client/core';\n\nimport { createAleoNetworkProvider } from '../utils/createAleoNetworkProvider';\n\n/**\n * Register the Aleo network-provider builder on a Dynamic client.\n *\n * Wires `createAleoNetworkProvider` into the client's network-provider\n * builder registry under the `'ALEO'` chain key, which is how the SDK's\n * network machinery materializes a per-network provider on demand.\n * Idempotent — a second call is a no-op once the builder is registered.\n *\n * Internal: called by the Aleo extension entry points\n * (`addAleoWalletStandardExtension`, `addAleoWalletAdaptersExtension`,\n * `addAleoExtension`). Not exported from the package's public surface.\n *\n * @notInstrumented\n */\nexport const registerAleoNetworkProviderBuilder = (\n client: DynamicClient\n): void => {\n const networkProviderBuilderRegistry =\n getNetworkProviderBuilderRegistry(client);\n\n if (networkProviderBuilderRegistry.get().get('ALEO')) {\n return;\n }\n\n networkProviderBuilderRegistry.register({\n builder: createAleoNetworkProvider,\n chain: 'ALEO',\n });\n};\n"],"mappings":";;;;AAEA,MAAM,iCAAiC,OAAO,wBAAwB;AACtE,MAAM,6BAA6B,OAAO,wBAAwB,CAAC,SAAS;;;;;;;AAQ5E,MAAM,yBAAyB;;;;;;;;;;;;AAc/B,MAAa,+BAA+B,iBAAiC;CAC3E,MAAM,UAAU,eAAe;CAE/B,MAAM,cADY,eAAe,gCAE9B,UAAU,CACV,SAAS,4BAA4B,IAAI,CACzC,QAAQ,wBAAwB,GAAG;AACtC,QAAO,WAAW,SAAS,IAAI,GAAG,QAAQ,GAAG,eAAe,GAAG;;;;;AChCjE,MAAM,qBAAqB;AAC3B,MAAM,aAAa;;;;;;;;;;;;;;;;;;;AAqBnB,MAAa,gCAAgC,QAAwB;CACnE,IAAI,UAAU,IAAI,MAAM;AACxB,KAAI,QAAQ,WAAW,KAAI,IAAI,QAAQ,SAAS,KAAI,CAClD,WAAU,QAAQ,MAAM,GAAG,GAAG;AAEhC,KAAI,QAAQ,SAAS,mBAAmB,CACtC,WAAU,QAAQ,MAAM,GAAG,IAA2B;UAC7C,QAAQ,SAAS,WAAW,CACrC,WAAU,QAAQ,MAAM,GAAG,GAAmB;AAEhD,WAAU,QAAQ,MAAM,IAAI,CAAC,KAAK,GAAG,CAAC,MAAM;AAC5C,KAAI,CAAC,QAAS,QAAO,OAAO,EAAE;AAC9B,KAAI;AACF,SAAO,OAAO,QAAQ;SAChB;AACN,SAAO,OAAO,EAAE;;;;;;;;;;;;ACrBpB,MAAM,uBAAuB;;;;;;;;;;;;;;;AAgB7B,MAAa,mBAAmB,OAAO,EACrC,SACA,kBACoD;AACpD,KAAI,CAAC,qBAAqB,KAAK,QAAQ,CACrC,QAAO;CAGT,MAAM,UAAU,YAAY,QAAQ,KAAK;AACzC,KAAI,CAAC,QACH,QAAO;CAGT,MAAM,OAAO,QAAQ,SAAS,IAAI,GAAG,UAAU,GAAG,QAAQ;CAC1D,MAAM,MAAM,IAAI,IACd,wCAAwC,mBAAmB,QAAQ,IACnE,KACD;CACD,MAAM,WAAW,MAAM,MAAM,IAAI,UAAU,CAAC;AAC5C,KAAI,CAAC,SAAS,GACZ,QAAO;CAGT,MAAM,OAAO,MAAM,SAAS,MAAM;AAClC,KAAI,CAAC,QAAQ,SAAS,OACpB,QAAO;CAGT,MAAM,eAAe,6BAA6B,KAAK;AACvD,KAAI,gBAAgB,OAAO,EAAE,CAC3B,QAAO;AAGT,QAAO,4BAA4B,aAAa;;;;;;;;;;;;;;;;AC/ClD,MAAa,6BACX,iBACyB;CACzB,GAAG,0BAA0B,QAAQ,YAAY;CACjD,YAAY,OAAO,EAAE,eAAe,EAClC,SAAS,MAAM,iBAAiB;EAC9B;EACA;EACD,CAAC,EACH;CACF;;;;;;;;;;;;;;;;;;ACTD,MAAa,sCACX,WACS;CACT,MAAM,iCACJ,kCAAkC,OAAO;AAE3C,KAAI,+BAA+B,KAAK,CAAC,IAAI,OAAO,CAClD;AAGF,gCAA+B,SAAS;EACtC,SAAS;EACT,OAAO;EACR,CAAC"}
|