@polkadot-api/substrate-bindings 0.20.3-canary.fb86767 → 0.20.3
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/utils/ss58-util.js
CHANGED
|
@@ -33,13 +33,23 @@ const prefixBytesToNumber = (bytes) => {
|
|
|
33
33
|
const dv = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
34
34
|
return dv.byteLength === 1 ? dv.getUint8(0) : dv.getUint16(0);
|
|
35
35
|
};
|
|
36
|
+
const pkToBigint = (publicKey) => {
|
|
37
|
+
let result = 0n;
|
|
38
|
+
const dv = new DataView(publicKey.buffer);
|
|
39
|
+
const big64s = Math.floor(publicKey.length / 8);
|
|
40
|
+
for (let i = 0; i < big64s; i++)
|
|
41
|
+
result = result << 64n | dv.getBigUint64(i * 8);
|
|
42
|
+
for (let offset = big64s * 8; offset < publicKey.length; offset++)
|
|
43
|
+
result = result << 8n | BigInt(dv.getUint8(offset));
|
|
44
|
+
return result;
|
|
45
|
+
};
|
|
36
46
|
const withSs58Cache = (fn) => {
|
|
37
|
-
let cache =
|
|
47
|
+
let cache = /* @__PURE__ */ new Map();
|
|
38
48
|
let activityCount = 0;
|
|
39
49
|
let latestCount = 0;
|
|
40
50
|
const checkActivity = () => {
|
|
41
51
|
if (activityCount === latestCount) {
|
|
42
|
-
cache
|
|
52
|
+
cache.clear();
|
|
43
53
|
activityCount = latestCount = 0;
|
|
44
54
|
} else {
|
|
45
55
|
latestCount = activityCount;
|
|
@@ -47,12 +57,12 @@ const withSs58Cache = (fn) => {
|
|
|
47
57
|
}
|
|
48
58
|
};
|
|
49
59
|
return (publicKey) => {
|
|
50
|
-
var _a, _b;
|
|
51
60
|
if (++activityCount === 1) checkActivity();
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
61
|
+
const bigKey = pkToBigint(publicKey);
|
|
62
|
+
if (!cache.has(bigKey)) {
|
|
63
|
+
cache.set(bigKey, fn(publicKey));
|
|
64
|
+
}
|
|
65
|
+
return cache.get(bigKey);
|
|
56
66
|
};
|
|
57
67
|
};
|
|
58
68
|
const fromBufferToBase58 = (ss58Format) => {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ss58-util.js","sources":["../../src/utils/ss58-util.ts"],"sourcesContent":["import { base58 } from \"@scure/base\"\nimport { blake2b } from \"@noble/hashes/blake2.js\"\n\nconst SS58_PREFIX = new TextEncoder().encode(\"SS58PRE\")\nconst CHECKSUM_LENGTH = 2\n\nexport type SS58String = string & { __SS58String?: unknown }\nexport type SS58AddressInfo =\n | { isValid: false }\n | { isValid: true; ss58Format: number; publicKey: Uint8Array }\n\nexport const getSs58AddressInfo = (address: SS58String): SS58AddressInfo => {\n try {\n const decoded = base58.decode(address)\n const prefixBytes = decoded.subarray(0, decoded[0] & 0b0100_0000 ? 2 : 1)\n const publicKey = decoded.subarray(\n prefixBytes.length,\n decoded.length - CHECKSUM_LENGTH,\n )\n\n const checksum = decoded.subarray(prefixBytes.length + publicKey.length)\n const expectedChecksum = blake2b(\n Uint8Array.of(...SS58_PREFIX, ...prefixBytes, ...publicKey),\n {\n dkLen: 64,\n },\n ).subarray(0, CHECKSUM_LENGTH)\n\n const isChecksumValid =\n checksum[0] === expectedChecksum[0] && checksum[1] === expectedChecksum[1]\n\n if (!isChecksumValid) return { isValid: false }\n\n return {\n isValid: true,\n ss58Format: prefixBytesToNumber(prefixBytes),\n publicKey: publicKey.slice(),\n }\n } catch (_) {\n return { isValid: false }\n }\n}\n\nconst prefixBytesToNumber = (bytes: Uint8Array) => {\n const dv = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength)\n return dv.byteLength === 1 ? dv.getUint8(0) : dv.getUint16(0)\n}\n\nconst withSs58Cache = (fn: (publicKey: Uint8Array) => SS58String) => {\n let cache
|
|
1
|
+
{"version":3,"file":"ss58-util.js","sources":["../../src/utils/ss58-util.ts"],"sourcesContent":["import { base58 } from \"@scure/base\"\nimport { blake2b } from \"@noble/hashes/blake2.js\"\n\nconst SS58_PREFIX = new TextEncoder().encode(\"SS58PRE\")\nconst CHECKSUM_LENGTH = 2\n\nexport type SS58String = string & { __SS58String?: unknown }\nexport type SS58AddressInfo =\n | { isValid: false }\n | { isValid: true; ss58Format: number; publicKey: Uint8Array }\n\nexport const getSs58AddressInfo = (address: SS58String): SS58AddressInfo => {\n try {\n const decoded = base58.decode(address)\n const prefixBytes = decoded.subarray(0, decoded[0] & 0b0100_0000 ? 2 : 1)\n const publicKey = decoded.subarray(\n prefixBytes.length,\n decoded.length - CHECKSUM_LENGTH,\n )\n\n const checksum = decoded.subarray(prefixBytes.length + publicKey.length)\n const expectedChecksum = blake2b(\n Uint8Array.of(...SS58_PREFIX, ...prefixBytes, ...publicKey),\n {\n dkLen: 64,\n },\n ).subarray(0, CHECKSUM_LENGTH)\n\n const isChecksumValid =\n checksum[0] === expectedChecksum[0] && checksum[1] === expectedChecksum[1]\n\n if (!isChecksumValid) return { isValid: false }\n\n return {\n isValid: true,\n ss58Format: prefixBytesToNumber(prefixBytes),\n publicKey: publicKey.slice(),\n }\n } catch (_) {\n return { isValid: false }\n }\n}\n\nconst prefixBytesToNumber = (bytes: Uint8Array) => {\n const dv = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength)\n return dv.byteLength === 1 ? dv.getUint8(0) : dv.getUint16(0)\n}\n\nconst pkToBigint = (publicKey: Uint8Array) => {\n let result = 0n\n const dv = new DataView(publicKey.buffer)\n const big64s = Math.floor(publicKey.length / 8)\n for (let i = 0; i < big64s; i++)\n result = (result << 64n) | dv.getBigUint64(i * 8)\n for (let offset = big64s * 8; offset < publicKey.length; offset++)\n result = (result << 8n) | BigInt(dv.getUint8(offset))\n return result\n}\n\nconst withSs58Cache = (fn: (publicKey: Uint8Array) => SS58String) => {\n let cache = new Map<bigint, SS58String>()\n let activityCount = 0\n let latestCount = 0\n const checkActivity = () => {\n if (activityCount === latestCount) {\n cache.clear()\n activityCount = latestCount = 0\n } else {\n latestCount = activityCount\n setTimeout(checkActivity, 0)\n }\n }\n\n return (publicKey: Uint8Array): SS58String => {\n if (++activityCount === 1) checkActivity()\n\n const bigKey = pkToBigint(publicKey)\n if (!cache.has(bigKey)) {\n cache.set(bigKey, fn(publicKey))\n }\n return cache.get(bigKey)!\n }\n}\n\nexport const fromBufferToBase58 = (ss58Format: number) => {\n const prefixBytes =\n ss58Format < 64\n ? Uint8Array.of(ss58Format)\n : Uint8Array.of(\n ((ss58Format & 0b0000_0000_1111_1100) >> 2) | 0b0100_0000,\n (ss58Format >> 8) | ((ss58Format & 0b0000_0000_0000_0011) << 6),\n )\n\n return withSs58Cache((publicKey: Uint8Array): SS58String => {\n const checksum = blake2b(\n Uint8Array.of(...SS58_PREFIX, ...prefixBytes, ...publicKey),\n {\n dkLen: 64,\n },\n ).subarray(0, CHECKSUM_LENGTH)\n return base58.encode(\n Uint8Array.of(...prefixBytes, ...publicKey, ...checksum),\n )\n })\n}\n"],"names":[],"mappings":";;;AAGA,MAAM,WAAA,GAAc,IAAI,WAAA,EAAY,CAAE,OAAO,SAAS,CAAA;AACtD,MAAM,eAAA,GAAkB,CAAA;AAOjB,MAAM,kBAAA,GAAqB,CAAC,OAAA,KAAyC;AAC1E,EAAA,IAAI;AACF,IAAA,MAAM,OAAA,GAAU,MAAA,CAAO,MAAA,CAAO,OAAO,CAAA;AACrC,IAAA,MAAM,WAAA,GAAc,QAAQ,QAAA,CAAS,CAAA,EAAG,QAAQ,CAAC,CAAA,GAAI,EAAA,GAAc,CAAA,GAAI,CAAC,CAAA;AACxE,IAAA,MAAM,YAAY,OAAA,CAAQ,QAAA;AAAA,MACxB,WAAA,CAAY,MAAA;AAAA,MACZ,QAAQ,MAAA,GAAS;AAAA,KACnB;AAEA,IAAA,MAAM,WAAW,OAAA,CAAQ,QAAA,CAAS,WAAA,CAAY,MAAA,GAAS,UAAU,MAAM,CAAA;AACvE,IAAA,MAAM,gBAAA,GAAmB,OAAA;AAA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ACT,WAAW,EAAA,CAAG,UAAU,IACxB,UAAA,CAAW,EAAA;AAAA,IAAA,CACP,UAAA,GAAa,QAA0B,CAAA,GAAK,EAAA;AAAA,IAC7C,UAAA,IAAc,CAAA,GAAA,CAAO,UAAA,GAAa,CAAA,KAA0B;AAAA,GAC/D;AAEN,EAAA,OAAO,aAAA,CAAc,CAAC,SAAA,KAAsC;AAC1D,IAAA,MAAM,QAAA,GAAW,OAAA;AAAA,MACf,WAAW,EAAA,CAAG,GAAG,aAAa,GAAG,WAAA,EAAa,GAAG,SAAS,CAAA;AAAA,MAC1D;AAAA,QACE,KAAA,EAAO;AAAA;AACT,KACF,CAAE,QAAA,CAAS,CAAA,EAAG,eAAe,CAAA;AAC7B,IAAA,OAAO,MAAA,CAAO,MAAA;AAAA,MACZ,WAAW,EAAA,CAAG,GAAG,aAAa,GAAG,SAAA,EAAW,GAAG,QAAQ;AAAA,KACzD;AAAA,EACF,CAAC,CAAA;AACH;;;;"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@polkadot-api/substrate-bindings",
|
|
3
|
-
"version": "0.20.3
|
|
3
|
+
"version": "0.20.3",
|
|
4
4
|
"author": "Josep M Sobrepere (https://github.com/josepot)",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -28,7 +28,7 @@
|
|
|
28
28
|
"@noble/hashes": "^2.2.0",
|
|
29
29
|
"@scure/base": "^2.2.0",
|
|
30
30
|
"scale-ts": "^1.6.1",
|
|
31
|
-
"@polkadot-api/utils": "0.4.
|
|
31
|
+
"@polkadot-api/utils": "0.4.0"
|
|
32
32
|
},
|
|
33
33
|
"devDependencies": {
|
|
34
34
|
"bigint-conversion": "^2.4.3",
|