@talismn/util 0.1.1 → 0.1.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/CHANGELOG.md +12 -0
- package/dist/BigMath.js +4 -1
- package/dist/FunctionPropertyNames.js +2 -1
- package/dist/blake2Concat.d.ts +1 -0
- package/dist/blake2Concat.js +10 -0
- package/dist/decodeAnyAddress.d.ts +1 -0
- package/dist/decodeAnyAddress.js +19 -0
- package/dist/encodeAnyAddress.d.ts +1 -0
- package/dist/encodeAnyAddress.js +18 -0
- package/dist/formatDecimals.js +15 -8
- package/dist/getBase64ImageUrl.js +5 -1
- package/dist/hasOwnProperty.d.ts +1 -0
- package/dist/hasOwnProperty.js +7 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +28 -7
- package/dist/isArrayOf.js +5 -1
- package/dist/planckToTokens.js +12 -5
- package/dist/tokensToPlanck.js +12 -5
- package/dist/twox64Concat.d.ts +2 -0
- package/dist/twox64Concat.js +10 -0
- package/package.json +5 -2
package/CHANGELOG.md
CHANGED
@@ -1,5 +1,17 @@
|
|
1
1
|
# @talismn/util
|
2
2
|
|
3
|
+
## 0.1.3
|
4
|
+
|
5
|
+
### Patch Changes
|
6
|
+
|
7
|
+
- 5af305c: switched build output from esm to commonjs for ecosystem compatibility
|
8
|
+
|
9
|
+
## 0.1.2
|
10
|
+
|
11
|
+
### Patch Changes
|
12
|
+
|
13
|
+
- 2ccd51b: feat: implemented @talismn/balances-substrate-native
|
14
|
+
|
3
15
|
## 0.1.1
|
4
16
|
|
5
17
|
### Patch Changes
|
package/dist/BigMath.js
CHANGED
@@ -1,8 +1,11 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.BigMath = void 0;
|
1
4
|
/**
|
2
5
|
* Javascript's `Math` library for `BigInt`.
|
3
6
|
* Taken from https://stackoverflow.com/questions/51867270/is-there-a-library-similar-to-math-that-supports-javascript-bigint/64953280#64953280
|
4
7
|
*/
|
5
|
-
|
8
|
+
exports.BigMath = {
|
6
9
|
abs(x) {
|
7
10
|
return x < BigInt("0") ? -x : x;
|
8
11
|
},
|
@@ -0,0 +1 @@
|
|
1
|
+
export declare function blake2Concat(input: Uint8Array): `0x${string}`;
|
@@ -0,0 +1,10 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.blake2Concat = void 0;
|
4
|
+
const util_1 = require("@polkadot/util");
|
5
|
+
const util_crypto_1 = require("@polkadot/util-crypto");
|
6
|
+
const bitLength = 128;
|
7
|
+
function blake2Concat(input) {
|
8
|
+
return (0, util_1.u8aToHex)((0, util_1.u8aConcat)((0, util_crypto_1.blake2AsU8a)(input, bitLength), (0, util_1.u8aToU8a)(input)));
|
9
|
+
}
|
10
|
+
exports.blake2Concat = blake2Concat;
|
@@ -0,0 +1 @@
|
|
1
|
+
export declare function decodeAnyAddress(encoded?: string | Uint8Array | null | undefined, ignoreChecksum?: boolean | undefined, ss58Format?: number | undefined): Uint8Array;
|
@@ -0,0 +1,19 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.decodeAnyAddress = void 0;
|
4
|
+
const keyring_1 = require("@polkadot/keyring");
|
5
|
+
const util_1 = require("@polkadot/util");
|
6
|
+
const util_crypto_1 = require("@polkadot/util-crypto");
|
7
|
+
function decodeAnyAddress(encoded, ignoreChecksum, ss58Format) {
|
8
|
+
try {
|
9
|
+
return (0, keyring_1.decodeAddress)(encoded, ignoreChecksum, ss58Format);
|
10
|
+
}
|
11
|
+
catch (error) {
|
12
|
+
if (typeof encoded !== "string")
|
13
|
+
throw error;
|
14
|
+
if (!(0, util_crypto_1.isEthereumAddress)(encoded))
|
15
|
+
throw error;
|
16
|
+
return (0, util_1.hexToU8a)(encoded.slice("0x".length));
|
17
|
+
}
|
18
|
+
}
|
19
|
+
exports.decodeAnyAddress = decodeAnyAddress;
|
@@ -0,0 +1 @@
|
|
1
|
+
export declare function encodeAnyAddress(key: string | Uint8Array, ss58Format?: number | undefined): string;
|
@@ -0,0 +1,18 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.encodeAnyAddress = void 0;
|
4
|
+
const keyring_1 = require("@polkadot/keyring");
|
5
|
+
const util_crypto_1 = require("@polkadot/util-crypto");
|
6
|
+
function encodeAnyAddress(key, ss58Format) {
|
7
|
+
try {
|
8
|
+
return (0, keyring_1.encodeAddress)(key, ss58Format);
|
9
|
+
}
|
10
|
+
catch (error) {
|
11
|
+
if (typeof key !== "string")
|
12
|
+
throw error;
|
13
|
+
if (!(0, util_crypto_1.isEthereumAddress)(key))
|
14
|
+
throw error;
|
15
|
+
return (0, util_crypto_1.ethereumEncode)(key);
|
16
|
+
}
|
17
|
+
}
|
18
|
+
exports.encodeAnyAddress = encodeAnyAddress;
|
package/dist/formatDecimals.js
CHANGED
@@ -1,5 +1,11 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
"use strict";
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
|
+
};
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
6
|
+
exports.formatDecimals = exports.MAX_DECIMALS_FORMAT = void 0;
|
7
|
+
const bignumber_js_1 = __importDefault(require("bignumber.js"));
|
8
|
+
exports.MAX_DECIMALS_FORMAT = 12;
|
3
9
|
/**
|
4
10
|
* Custom decimal number formatting for Talisman
|
5
11
|
* note that the NumberFormat().format() call is the ressource heavy part, it's not worth trying to optimize other parts
|
@@ -9,14 +15,14 @@ export const MAX_DECIMALS_FORMAT = 12;
|
|
9
15
|
* @param options formatting options
|
10
16
|
* @returns the formatted value
|
11
17
|
*/
|
12
|
-
|
18
|
+
const formatDecimals = (num, digits = 4, options = {}, locale = "en-US") => {
|
13
19
|
if (num === null || num === undefined)
|
14
20
|
return "";
|
15
|
-
const value = new
|
21
|
+
const value = new bignumber_js_1.default(num);
|
16
22
|
// very small numbers should display "< 0.0001"
|
17
23
|
const minDisplayVal = 1 / Math.pow(10, digits);
|
18
24
|
if (value.gt(0) && value.lt(minDisplayVal))
|
19
|
-
return `< ${formatDecimals(minDisplayVal)}`;
|
25
|
+
return `< ${(0, exports.formatDecimals)(minDisplayVal)}`;
|
20
26
|
// count digits
|
21
27
|
const flooredValue = value.integerValue();
|
22
28
|
const intDigits = flooredValue.isEqualTo(0) ? 0 : flooredValue.toString().length;
|
@@ -24,14 +30,14 @@ export const formatDecimals = (num, digits = 4, options = {}, locale = "en-US")
|
|
24
30
|
// to prevent JS default rounding, we will remove/truncate insignificant digits ourselves before formatting
|
25
31
|
let truncatedValue = value;
|
26
32
|
//remove insignificant fraction digits
|
27
|
-
const excessFractionDigitsPow10 = new
|
33
|
+
const excessFractionDigitsPow10 = new bignumber_js_1.default(10).pow(digits > intDigits ? digits - intDigits : 0);
|
28
34
|
truncatedValue = truncatedValue
|
29
35
|
.multipliedBy(excessFractionDigitsPow10)
|
30
36
|
.integerValue()
|
31
37
|
.dividedBy(excessFractionDigitsPow10);
|
32
38
|
//remove insignificant integer digits
|
33
|
-
const excessIntegerDigits = new
|
34
|
-
const excessIntegerDigitsPow10 = new
|
39
|
+
const excessIntegerDigits = new bignumber_js_1.default(intDigits > digits ? intDigits - digits : 0);
|
40
|
+
const excessIntegerDigitsPow10 = new bignumber_js_1.default(10).pow(excessIntegerDigits);
|
35
41
|
if (excessIntegerDigits.gt(0))
|
36
42
|
truncatedValue = truncatedValue
|
37
43
|
.dividedBy(excessIntegerDigitsPow10)
|
@@ -42,3 +48,4 @@ export const formatDecimals = (num, digits = 4, options = {}, locale = "en-US")
|
|
42
48
|
//compact notation (K, M, B) if above 9999
|
43
49
|
notation: truncatedValue.gt(9999) ? "compact" : "standard", maximumSignificantDigits: digits + (truncatedValue.lt(1) ? 1 : 0) }, options)).format(truncatedValue.toNumber());
|
44
50
|
};
|
51
|
+
exports.formatDecimals = formatDecimals;
|
@@ -1,4 +1,7 @@
|
|
1
|
-
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.getBase64ImageUrl = void 0;
|
4
|
+
const getBase64ImageUrl = (base64) => {
|
2
5
|
const match = /^data:([^;]*?);base64,(.*?)$/.exec(base64);
|
3
6
|
if (!match)
|
4
7
|
return null;
|
@@ -6,3 +9,4 @@ export const getBase64ImageUrl = (base64) => {
|
|
6
9
|
const blob = new Blob([new Uint8Array(buffer, 0, buffer.length)], { type: match[1] });
|
7
10
|
return URL.createObjectURL(blob);
|
8
11
|
};
|
12
|
+
exports.getBase64ImageUrl = getBase64ImageUrl;
|
@@ -0,0 +1 @@
|
|
1
|
+
export declare function hasOwnProperty<X, Y extends PropertyKey>(obj: X, prop: Y): obj is X & Record<Y, unknown>;
|
package/dist/index.d.ts
CHANGED
@@ -1,7 +1,12 @@
|
|
1
1
|
export * from "./BigMath";
|
2
2
|
export * from "./FunctionPropertyNames";
|
3
|
+
export * from "./blake2Concat";
|
4
|
+
export * from "./decodeAnyAddress";
|
5
|
+
export * from "./encodeAnyAddress";
|
3
6
|
export * from "./formatDecimals";
|
4
7
|
export * from "./getBase64ImageUrl";
|
8
|
+
export * from "./hasOwnProperty";
|
5
9
|
export * from "./isArrayOf";
|
6
10
|
export * from "./planckToTokens";
|
7
11
|
export * from "./tokensToPlanck";
|
12
|
+
export * from "./twox64Concat";
|
package/dist/index.js
CHANGED
@@ -1,7 +1,28 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
1
|
+
"use strict";
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
3
|
+
if (k2 === undefined) k2 = k;
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
7
|
+
}
|
8
|
+
Object.defineProperty(o, k2, desc);
|
9
|
+
}) : (function(o, m, k, k2) {
|
10
|
+
if (k2 === undefined) k2 = k;
|
11
|
+
o[k2] = m[k];
|
12
|
+
}));
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
15
|
+
};
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
17
|
+
__exportStar(require("./BigMath"), exports);
|
18
|
+
__exportStar(require("./FunctionPropertyNames"), exports);
|
19
|
+
__exportStar(require("./blake2Concat"), exports);
|
20
|
+
__exportStar(require("./decodeAnyAddress"), exports);
|
21
|
+
__exportStar(require("./encodeAnyAddress"), exports);
|
22
|
+
__exportStar(require("./formatDecimals"), exports);
|
23
|
+
__exportStar(require("./getBase64ImageUrl"), exports);
|
24
|
+
__exportStar(require("./hasOwnProperty"), exports);
|
25
|
+
__exportStar(require("./isArrayOf"), exports);
|
26
|
+
__exportStar(require("./planckToTokens"), exports);
|
27
|
+
__exportStar(require("./tokensToPlanck"), exports);
|
28
|
+
__exportStar(require("./twox64Concat"), exports);
|
package/dist/isArrayOf.js
CHANGED
@@ -1,5 +1,9 @@
|
|
1
|
-
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.isArrayOf = void 0;
|
4
|
+
function isArrayOf(array, func) {
|
2
5
|
if (array.length > 0 && array[0] instanceof func)
|
3
6
|
return true;
|
4
7
|
return false;
|
5
8
|
}
|
9
|
+
exports.isArrayOf = isArrayOf;
|
package/dist/planckToTokens.js
CHANGED
@@ -1,9 +1,16 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
"use strict";
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
|
+
};
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
6
|
+
exports.planckToTokens = void 0;
|
7
|
+
const bignumber_js_1 = __importDefault(require("bignumber.js"));
|
8
|
+
function planckToTokens(planck, tokenDecimals) {
|
3
9
|
if (typeof planck !== "string" || typeof tokenDecimals !== "number")
|
4
10
|
return;
|
5
|
-
const base = new
|
6
|
-
const exponent = new
|
11
|
+
const base = new bignumber_js_1.default(10);
|
12
|
+
const exponent = new bignumber_js_1.default(tokenDecimals).negated();
|
7
13
|
const multiplier = base.pow(exponent);
|
8
|
-
return new
|
14
|
+
return new bignumber_js_1.default(planck).multipliedBy(multiplier).toString(10);
|
9
15
|
}
|
16
|
+
exports.planckToTokens = planckToTokens;
|
package/dist/tokensToPlanck.js
CHANGED
@@ -1,9 +1,16 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
"use strict";
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
|
+
};
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
6
|
+
exports.tokensToPlanck = void 0;
|
7
|
+
const bignumber_js_1 = __importDefault(require("bignumber.js"));
|
8
|
+
function tokensToPlanck(tokens, tokenDecimals) {
|
3
9
|
if (typeof tokens !== "string" || typeof tokenDecimals !== "number")
|
4
10
|
return;
|
5
|
-
const base = new
|
6
|
-
const exponent = new
|
11
|
+
const base = new bignumber_js_1.default(10);
|
12
|
+
const exponent = new bignumber_js_1.default(tokenDecimals);
|
7
13
|
const multiplier = base.pow(exponent);
|
8
|
-
return new
|
14
|
+
return new bignumber_js_1.default(tokens).multipliedBy(multiplier).toString(10);
|
9
15
|
}
|
16
|
+
exports.tokensToPlanck = tokensToPlanck;
|
@@ -0,0 +1,10 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.twox64Concat = void 0;
|
4
|
+
const util_1 = require("@polkadot/util");
|
5
|
+
const util_crypto_1 = require("@polkadot/util-crypto");
|
6
|
+
const bitLength = 64;
|
7
|
+
function twox64Concat(input) {
|
8
|
+
return (0, util_1.u8aToHex)((0, util_1.u8aConcat)((0, util_crypto_1.xxhashAsU8a)(input, bitLength), (0, util_1.u8aToU8a)(input)));
|
9
|
+
}
|
10
|
+
exports.twox64Concat = twox64Concat;
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@talismn/util",
|
3
|
-
"version": "0.1.
|
3
|
+
"version": "0.1.3",
|
4
4
|
"author": "Talisman",
|
5
5
|
"homepage": "https://talisman.xyz",
|
6
6
|
"license": "UNLICENSED",
|
@@ -30,11 +30,14 @@
|
|
30
30
|
"clean": "rm -rf dist && rm -rf .turbo rm -rf node_modules"
|
31
31
|
},
|
32
32
|
"dependencies": {
|
33
|
+
"@polkadot/keyring": "^10.1.7",
|
34
|
+
"@polkadot/util": "^10.1.7",
|
35
|
+
"@polkadot/util-crypto": "^10.1.7",
|
33
36
|
"bignumber.js": "^9.1.0"
|
34
37
|
},
|
35
38
|
"devDependencies": {
|
36
39
|
"@talismn/eslint-config": "^0.0.0",
|
37
|
-
"@talismn/tsconfig": "^0.0.
|
40
|
+
"@talismn/tsconfig": "^0.0.1",
|
38
41
|
"@types/jest": "^27.5.1",
|
39
42
|
"eslint": "^8.4.0",
|
40
43
|
"jest": "^28.1.0",
|