@moonbeam-network/xcm-utils 3.0.6 → 3.1.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/build/index.d.ts +2 -1
- package/build/index.mjs +7 -4
- package/build/index.mjs.map +1 -1
- package/package.json +5 -5
package/build/index.d.ts
CHANGED
|
@@ -2,6 +2,7 @@ import { RoundingMode } from 'big.js';
|
|
|
2
2
|
import { ApiPromise } from '@polkadot/api';
|
|
3
3
|
|
|
4
4
|
declare function isHexString(asset: unknown): boolean;
|
|
5
|
+
declare function isEthAddress(address: string): boolean;
|
|
5
6
|
|
|
6
7
|
declare function formatAssetIdToERC20(id: string): string;
|
|
7
8
|
declare function convertAddressTo32Bytes(address: string): string;
|
|
@@ -41,4 +42,4 @@ declare function getMultilocationDerivedAddresses({ paraId, address, isParents,
|
|
|
41
42
|
address32: `0x${string}`;
|
|
42
43
|
};
|
|
43
44
|
|
|
44
|
-
export { MRLTypes, convertAddressTo32Bytes, convertDecimals, formatAssetIdToERC20, getMultilocationDerivedAddresses, getPolkadotApi, getPolkadotAppsUrl, getSovereignAccountAddresses, hasDecimalOverflow, isHexString, toBigInt, toDecimal };
|
|
45
|
+
export { MRLTypes, convertAddressTo32Bytes, convertDecimals, formatAssetIdToERC20, getMultilocationDerivedAddresses, getPolkadotApi, getPolkadotAppsUrl, getSovereignAccountAddresses, hasDecimalOverflow, isEthAddress, isHexString, toBigInt, toDecimal };
|
package/build/index.mjs
CHANGED
|
@@ -2,6 +2,9 @@
|
|
|
2
2
|
function isHexString(asset) {
|
|
3
3
|
return typeof asset === "string" && asset.startsWith("0x");
|
|
4
4
|
}
|
|
5
|
+
function isEthAddress(address) {
|
|
6
|
+
return address.length === 42 && address.startsWith("0x");
|
|
7
|
+
}
|
|
5
8
|
|
|
6
9
|
// src/format/asset.ts
|
|
7
10
|
function formatAssetIdToERC20(id) {
|
|
@@ -122,15 +125,14 @@ function getMultilocationDerivedAddresses({
|
|
|
122
125
|
isParents = false
|
|
123
126
|
}) {
|
|
124
127
|
const parents = isParents ? 1 : 0;
|
|
125
|
-
const
|
|
126
|
-
const
|
|
127
|
-
const decodedAddress = isEthAddress ? hexToU8a(address) : decodeAddress(address);
|
|
128
|
+
const accType = isEthAddress(address) ? "AccountKey20" : "AccountId32";
|
|
129
|
+
const decodedAddress = isEthAddress(address) ? hexToU8a(address) : decodeAddress(address);
|
|
128
130
|
const family = parents === 0 && paraId ? "ChildChain" : parents === 1 && !paraId ? "ParentChain" : "SiblingChain";
|
|
129
131
|
const blake = blake2AsU8a(
|
|
130
132
|
new Uint8Array([
|
|
131
133
|
...stringToU8a(family),
|
|
132
134
|
...paraId ? compactToU8a(paraId) : [],
|
|
133
|
-
...compactToU8a(accType.length + (isEthAddress ? 20 : 32)),
|
|
135
|
+
...compactToU8a(accType.length + (isEthAddress(address) ? 20 : 32)),
|
|
134
136
|
...stringToU8a(accType),
|
|
135
137
|
...decodedAddress
|
|
136
138
|
])
|
|
@@ -152,6 +154,7 @@ export {
|
|
|
152
154
|
getPolkadotAppsUrl,
|
|
153
155
|
getSovereignAccountAddresses,
|
|
154
156
|
hasDecimalOverflow,
|
|
157
|
+
isEthAddress,
|
|
155
158
|
isHexString,
|
|
156
159
|
toBigInt,
|
|
157
160
|
toDecimal
|
package/build/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/format/address.ts","../src/format/asset.ts","../src/numbers/decimals.ts","../src/polkadot/polkadot.api.ts","../src/polkadot/polkadot.apps.ts","../src/polkadot/polkadot.address.ts"],"sourcesContent":["export function isHexString(asset: unknown): boolean {\n return typeof asset === 'string' && asset.startsWith('0x');\n}\n","export function formatAssetIdToERC20(id: string) {\n if (id.startsWith('0x')) {\n return id;\n }\n\n if (!/^\\d{38,39}$/.test(id)) {\n throw new Error(`Asset id: ${id} must be a string and have 38-39 digits`);\n }\n\n return `0xffffffff${BigInt(id).toString(16).padStart(32, '0')}`;\n}\n\nexport function convertAddressTo32Bytes(address: string): string {\n if (!/^0x[a-fA-F0-9]{40}$/.test(address)) {\n throw new Error(`Invalid address: ${address}`);\n }\n\n return `0x000000000000000000000000${address.substring(2)}`;\n}\n","import Big, { type RoundingMode } from 'big.js';\n\nBig.NE = -18;\n\nexport function toDecimal(\n number: bigint | number | string,\n decimals: number,\n maxDecimal = 6,\n roundType?: RoundingMode,\n): string {\n const dividend = Big(number.toString().replace(/[^0-9]/g, ''));\n const divisor = Big(10).pow(decimals);\n const result = dividend.div(divisor).round(maxDecimal, roundType);\n\n return result.toString();\n}\n\nexport function toBigInt(\n amount: bigint | string | number,\n decimals: number,\n): bigint {\n if (typeof amount === 'bigint') {\n return amount;\n }\n\n const multiplier = Big(10).pow(decimals);\n const result = Big(amount).mul(multiplier);\n\n return BigInt(result.toFixed(0, Big.roundDown));\n}\n\nexport function convertDecimals(\n number: string | bigint,\n decimals: number,\n targetDecimals: number,\n): bigint {\n const decimalNumber = toDecimal(number, decimals, decimals);\n\n return toBigInt(decimalNumber.toString(), targetDecimals);\n}\n\nexport function hasDecimalOverflow(fl: number | string, maxDecimal: number) {\n const parts = fl.toString().split('.');\n return parts.length > 1 && parts[1].length > maxDecimal;\n}\n","import { ApiPromise, WsProvider } from '@polkadot/api';\nimport { typesBundle } from '@polkadot/apps-config';\nimport { LRUCache } from 'lru-cache';\n\nexport enum MRLTypes {\n // TODO handle both types according to RT version\n XcmVersionedMultiLocation = 'XcmVersionedMultiLocation',\n XcmVersionedLocation = 'XcmVersionedLocation',\n XcmRoutingUserAction = 'XcmRoutingUserAction',\n VersionedUserAction = 'VersionedUserAction',\n}\n\nconst cache = new LRUCache<string, Promise<ApiPromise>>({\n max: 20,\n dispose: async (promise: Promise<ApiPromise>) => {\n const api = await promise;\n\n if (api.isConnected) {\n api.disconnect();\n }\n },\n});\n\nexport async function getPolkadotApi(\n ws: string | string[],\n): Promise<ApiPromise> {\n const key = Array.isArray(ws) ? ws.join(';') : ws;\n const promise =\n cache.get(key) ||\n ApiPromise.create({\n noInitWarn: true,\n provider: new WsProvider(ws),\n types: {\n [MRLTypes.XcmRoutingUserAction]: {\n destination: MRLTypes.XcmVersionedLocation,\n },\n [MRLTypes.VersionedUserAction]: {\n _enum: { V1: MRLTypes.XcmRoutingUserAction },\n },\n },\n typesBundle,\n });\n\n cache.set(key, promise);\n\n const api = await promise;\n\n await api.isReady;\n\n return api;\n}\n","export function getPolkadotAppsUrl(ws: string) {\n return `https://polkadot.js.org/apps/?rpc=${encodeURIComponent(ws)}#/explorer/query`;\n}\n","import {\n bnToU8a,\n compactToU8a,\n hexToU8a,\n stringToU8a,\n u8aToHex,\n} from '@polkadot/util';\nimport { blake2AsU8a, decodeAddress } from '@polkadot/util-crypto';\n\n/**\n * reference: https://github.com/Moonsong-Labs/xcm-tools/blob/main/scripts/calculate-sovereign-account.ts\n */\n\nexport function getSovereignAccountAddresses(paraId: number) {\n const paraIdU8a = bnToU8a(paraId, { bitLength: 32 });\n const relay = u8aToHex(\n new Uint8Array([...stringToU8a('para'), ...paraIdU8a]),\n ).padEnd(66, '0');\n const generic = u8aToHex(\n new Uint8Array([...stringToU8a('sibl'), ...paraIdU8a]),\n ).padEnd(66, '0');\n const moonbeam = generic.slice(0, 42);\n\n return {\n generic,\n moonbeam,\n relay,\n };\n}\n\n/**\n * reference: https://github.com/Moonsong-Labs/xcm-tools/blob/main/scripts/calculate-multilocation-derivative-account.ts\n */\n\nexport function getMultilocationDerivedAddresses({\n paraId,\n address,\n isParents = false,\n}: {\n paraId?: number;\n address: string;\n isParents?: boolean;\n}) {\n const parents = isParents ? 1 : 0;\n const
|
|
1
|
+
{"version":3,"sources":["../src/format/address.ts","../src/format/asset.ts","../src/numbers/decimals.ts","../src/polkadot/polkadot.api.ts","../src/polkadot/polkadot.apps.ts","../src/polkadot/polkadot.address.ts"],"sourcesContent":["export function isHexString(asset: unknown): boolean {\n return typeof asset === 'string' && asset.startsWith('0x');\n}\n\nexport function isEthAddress(address: string): boolean {\n return address.length === 42 && address.startsWith('0x');\n}\n","export function formatAssetIdToERC20(id: string) {\n if (id.startsWith('0x')) {\n return id;\n }\n\n if (!/^\\d{38,39}$/.test(id)) {\n throw new Error(`Asset id: ${id} must be a string and have 38-39 digits`);\n }\n\n return `0xffffffff${BigInt(id).toString(16).padStart(32, '0')}`;\n}\n\nexport function convertAddressTo32Bytes(address: string): string {\n if (!/^0x[a-fA-F0-9]{40}$/.test(address)) {\n throw new Error(`Invalid address: ${address}`);\n }\n\n return `0x000000000000000000000000${address.substring(2)}`;\n}\n","import Big, { type RoundingMode } from 'big.js';\n\nBig.NE = -18;\n\nexport function toDecimal(\n number: bigint | number | string,\n decimals: number,\n maxDecimal = 6,\n roundType?: RoundingMode,\n): string {\n const dividend = Big(number.toString().replace(/[^0-9]/g, ''));\n const divisor = Big(10).pow(decimals);\n const result = dividend.div(divisor).round(maxDecimal, roundType);\n\n return result.toString();\n}\n\nexport function toBigInt(\n amount: bigint | string | number,\n decimals: number,\n): bigint {\n if (typeof amount === 'bigint') {\n return amount;\n }\n\n const multiplier = Big(10).pow(decimals);\n const result = Big(amount).mul(multiplier);\n\n return BigInt(result.toFixed(0, Big.roundDown));\n}\n\nexport function convertDecimals(\n number: string | bigint,\n decimals: number,\n targetDecimals: number,\n): bigint {\n const decimalNumber = toDecimal(number, decimals, decimals);\n\n return toBigInt(decimalNumber.toString(), targetDecimals);\n}\n\nexport function hasDecimalOverflow(fl: number | string, maxDecimal: number) {\n const parts = fl.toString().split('.');\n return parts.length > 1 && parts[1].length > maxDecimal;\n}\n","import { ApiPromise, WsProvider } from '@polkadot/api';\nimport { typesBundle } from '@polkadot/apps-config';\nimport { LRUCache } from 'lru-cache';\n\nexport enum MRLTypes {\n // TODO handle both types according to RT version\n XcmVersionedMultiLocation = 'XcmVersionedMultiLocation',\n XcmVersionedLocation = 'XcmVersionedLocation',\n XcmRoutingUserAction = 'XcmRoutingUserAction',\n VersionedUserAction = 'VersionedUserAction',\n}\n\nconst cache = new LRUCache<string, Promise<ApiPromise>>({\n max: 20,\n dispose: async (promise: Promise<ApiPromise>) => {\n const api = await promise;\n\n if (api.isConnected) {\n api.disconnect();\n }\n },\n});\n\nexport async function getPolkadotApi(\n ws: string | string[],\n): Promise<ApiPromise> {\n const key = Array.isArray(ws) ? ws.join(';') : ws;\n const promise =\n cache.get(key) ||\n ApiPromise.create({\n noInitWarn: true,\n provider: new WsProvider(ws),\n types: {\n [MRLTypes.XcmRoutingUserAction]: {\n destination: MRLTypes.XcmVersionedLocation,\n },\n [MRLTypes.VersionedUserAction]: {\n _enum: { V1: MRLTypes.XcmRoutingUserAction },\n },\n },\n typesBundle,\n });\n\n cache.set(key, promise);\n\n const api = await promise;\n\n await api.isReady;\n\n return api;\n}\n","export function getPolkadotAppsUrl(ws: string) {\n return `https://polkadot.js.org/apps/?rpc=${encodeURIComponent(ws)}#/explorer/query`;\n}\n","import {\n bnToU8a,\n compactToU8a,\n hexToU8a,\n stringToU8a,\n u8aToHex,\n} from '@polkadot/util';\nimport { blake2AsU8a, decodeAddress } from '@polkadot/util-crypto';\nimport { isEthAddress } from '../format';\n\n/**\n * reference: https://github.com/Moonsong-Labs/xcm-tools/blob/main/scripts/calculate-sovereign-account.ts\n */\n\nexport function getSovereignAccountAddresses(paraId: number) {\n const paraIdU8a = bnToU8a(paraId, { bitLength: 32 });\n const relay = u8aToHex(\n new Uint8Array([...stringToU8a('para'), ...paraIdU8a]),\n ).padEnd(66, '0');\n const generic = u8aToHex(\n new Uint8Array([...stringToU8a('sibl'), ...paraIdU8a]),\n ).padEnd(66, '0');\n const moonbeam = generic.slice(0, 42);\n\n return {\n generic,\n moonbeam,\n relay,\n };\n}\n\n/**\n * reference: https://github.com/Moonsong-Labs/xcm-tools/blob/main/scripts/calculate-multilocation-derivative-account.ts\n */\n\nexport function getMultilocationDerivedAddresses({\n paraId,\n address,\n isParents = false,\n}: {\n paraId?: number;\n address: string;\n isParents?: boolean;\n}) {\n const parents = isParents ? 1 : 0;\n const accType = isEthAddress(address) ? 'AccountKey20' : 'AccountId32';\n const decodedAddress = isEthAddress(address)\n ? hexToU8a(address)\n : decodeAddress(address);\n\n // Describe Family\n // https://github.com/paritytech/polkadot/blob/master/xcm/xcm-builder/src/location_conversion.rs#L96-L118\n const family =\n parents === 0 && paraId\n ? 'ChildChain'\n : parents === 1 && !paraId\n ? 'ParentChain'\n : 'SiblingChain';\n\n const blake = blake2AsU8a(\n new Uint8Array([\n ...stringToU8a(family),\n ...(paraId ? compactToU8a(paraId) : []),\n ...compactToU8a(accType.length + (isEthAddress(address) ? 20 : 32)),\n ...stringToU8a(accType),\n ...decodedAddress,\n ]),\n );\n\n const address20 = u8aToHex(blake.slice(0, 20));\n const address32 = u8aToHex(blake.slice(0, 32));\n\n return {\n address20,\n address32,\n };\n}\n"],"mappings":";AAAO,SAAS,YAAY,OAAyB;AACnD,SAAO,OAAO,UAAU,YAAY,MAAM,WAAW,IAAI;AAC3D;AAEO,SAAS,aAAa,SAA0B;AACrD,SAAO,QAAQ,WAAW,MAAM,QAAQ,WAAW,IAAI;AACzD;;;ACNO,SAAS,qBAAqB,IAAY;AAC/C,MAAI,GAAG,WAAW,IAAI,GAAG;AACvB,WAAO;AAAA,EACT;AAEA,MAAI,CAAC,cAAc,KAAK,EAAE,GAAG;AAC3B,UAAM,IAAI,MAAM,aAAa,EAAE,yCAAyC;AAAA,EAC1E;AAEA,SAAO,aAAa,OAAO,EAAE,EAAE,SAAS,EAAE,EAAE,SAAS,IAAI,GAAG,CAAC;AAC/D;AAEO,SAAS,wBAAwB,SAAyB;AAC/D,MAAI,CAAC,sBAAsB,KAAK,OAAO,GAAG;AACxC,UAAM,IAAI,MAAM,oBAAoB,OAAO,EAAE;AAAA,EAC/C;AAEA,SAAO,6BAA6B,QAAQ,UAAU,CAAC,CAAC;AAC1D;;;AClBA,OAAO,SAAgC;AAEvC,IAAI,KAAK;AAEF,SAAS,UACd,QACA,UACA,aAAa,GACb,WACQ;AACR,QAAM,WAAW,IAAI,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AAM,SACJ,YAAY,KAAK,SACb,eACA,YAAY,KAAK,CAAC,SAChB,gBACA;AAER,QAAM,QAAQ;AAAA,IACZ,IAAI,WAAW;AAAA,MACb,GAAG,YAAY,MAAM;AAAA,MACrB,GAAI,SAAS,aAAa,MAAM,IAAI,CAAC;AAAA,MACrC,GAAG,aAAa,QAAQ,UAAU,aAAa,OAAO,IAAI,KAAK,GAAG;AAAA,MAClE,GAAG,YAAY,OAAO;AAAA,MACtB,GAAG;AAAA,IACL,CAAC;AAAA,EACH;AAEA,QAAM,YAAY,SAAS,MAAM,MAAM,GAAG,EAAE,CAAC;AAC7C,QAAM,YAAY,SAAS,MAAM,MAAM,GAAG,EAAE,CAAC;AAE7C,SAAO;AAAA,IACL;AAAA,IACA;AAAA,EACF;AACF;","names":["MRLTypes"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@moonbeam-network/xcm-utils",
|
|
3
|
-
"version": "3.0
|
|
3
|
+
"version": "3.1.0",
|
|
4
4
|
"description": "Moonbeam XCM utilities",
|
|
5
5
|
"author": "moonbeam-foundation",
|
|
6
6
|
"license": "MIT",
|
|
@@ -38,10 +38,10 @@
|
|
|
38
38
|
"@types/big.js": "^6.2.2"
|
|
39
39
|
},
|
|
40
40
|
"peerDependencies": {
|
|
41
|
-
"@polkadot/api": "15.
|
|
42
|
-
"@polkadot/apps-config": "0.
|
|
43
|
-
"@polkadot/util": "13.3
|
|
44
|
-
"@polkadot/util-crypto": "13.3
|
|
41
|
+
"@polkadot/api": "15.8.1",
|
|
42
|
+
"@polkadot/apps-config": "0.151.1",
|
|
43
|
+
"@polkadot/util": "13.4.3",
|
|
44
|
+
"@polkadot/util-crypto": "13.4.3"
|
|
45
45
|
},
|
|
46
46
|
"scripts": {
|
|
47
47
|
"build": "tsup",
|