@blocklet/util 0.12.90 → 0.12.91

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.cjs ADDED
@@ -0,0 +1,90 @@
1
+ 'use strict';
2
+
3
+ const jwt = require('@arcblock/jwt');
4
+ const did = require('@arcblock/did');
5
+ const isEmpty = require('lodash/isEmpty');
6
+ const util = require('@ocap/util');
7
+
8
+ function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
9
+
10
+ const isEmpty__default = /*#__PURE__*/_interopDefaultCompat(isEmpty);
11
+
12
+ function removeTrailingZeros(numStr) {
13
+ if (!numStr.includes(".")) {
14
+ return numStr;
15
+ }
16
+ return numStr.replace(/(\.\d*?[1-9])0+$|\.0*$/, "$1");
17
+ }
18
+ function calcPercent(input, percent, points = "100") {
19
+ let price = typeof input === "number" ? input.toString() : input;
20
+ price = price.replace(/(\.\d{18})\d+/, "$1");
21
+ let bnWatchPrice = new util.BN(price.replace(".", ""));
22
+ let decimalPlaces = price.split(".")[1] ? price.split(".")[1].length : 0;
23
+ if (decimalPlaces > 18) {
24
+ decimalPlaces = 0;
25
+ }
26
+ const times = 18;
27
+ const precision = new util.BN(10).pow(new util.BN(times - decimalPlaces));
28
+ bnWatchPrice = bnWatchPrice.mul(precision);
29
+ const percentBN = new util.BN(percent);
30
+ const pointsBN = new util.BN(points);
31
+ const finalResultBN = bnWatchPrice.mul(percentBN).div(pointsBN);
32
+ let finalResultStr = finalResultBN.toString();
33
+ const finalResultStrLen = finalResultStr.length;
34
+ if (finalResultStrLen <= times) {
35
+ return removeTrailingZeros(`0.${"0".repeat(times - finalResultStrLen)}${finalResultStr}`);
36
+ }
37
+ finalResultStr = [
38
+ finalResultStr.slice(0, finalResultStr.length - times),
39
+ finalResultStr.slice(finalResultStr.length - times)
40
+ ].join(".");
41
+ return removeTrailingZeros(finalResultStr);
42
+ }
43
+
44
+ async function verifyDownloadToken({
45
+ blockletDid,
46
+ downloadToken,
47
+ storePublicKey,
48
+ serverDid,
49
+ serverPublicKey,
50
+ serverSignature
51
+ }) {
52
+ if (isEmpty__default(downloadToken)) {
53
+ throw new Error("downloadToken must be provided");
54
+ }
55
+ if (!jwt.verify(downloadToken, storePublicKey)) {
56
+ throw new Error("downloadToken is invalid");
57
+ }
58
+ const payloadFromStoreSignature = jwt.decode(downloadToken, true);
59
+ if (payloadFromStoreSignature.serverDid !== serverDid) {
60
+ throw new Error("serverDid mismatch");
61
+ }
62
+ if (payloadFromStoreSignature.blockletDid !== blockletDid) {
63
+ throw new Error("blockletDid mismatch");
64
+ }
65
+ if (isEmpty__default(serverPublicKey)) {
66
+ throw new Error("serverPublicKey must be provided");
67
+ }
68
+ if (!did.isFromPublicKey(serverDid, serverPublicKey)) {
69
+ throw new Error("serverDid and serverPublicKey mismatch");
70
+ }
71
+ if (isEmpty__default(serverSignature)) {
72
+ throw new Error("serverSignature must be provided");
73
+ }
74
+ if (!jwt.verify(serverSignature, serverPublicKey)) {
75
+ throw new Error("serverSignature is invalid");
76
+ }
77
+ }
78
+ const isFreeBlocklet = (blocklet) => blocklet?.pricing?.paymentType === "free";
79
+ const parsePaymentPriceLabel = (pricing) => {
80
+ if (!pricing) {
81
+ return "";
82
+ }
83
+ return `${pricing.price} ${pricing.symbol || "ABT"}`;
84
+ };
85
+
86
+ exports.calcPercent = calcPercent;
87
+ exports.isFreeBlocklet = isFreeBlocklet;
88
+ exports.parsePaymentPriceLabel = parsePaymentPriceLabel;
89
+ exports.removeTrailingZeros = removeTrailingZeros;
90
+ exports.verifyDownloadToken = verifyDownloadToken;
@@ -0,0 +1,29 @@
1
+ declare function removeTrailingZeros(numStr: string): string;
2
+ /**
3
+ * 计算给定价格的比例, 默认使用百分制,使用 18 位精度进行高精度计算。
4
+ * @example
5
+ */
6
+ declare function calcPercent(input: string | number, percent: string, points?: string): string;
7
+
8
+ declare function verifyDownloadToken({ blockletDid, downloadToken, storePublicKey, serverDid, serverPublicKey, serverSignature, }: {
9
+ blockletDid: string;
10
+ downloadToken: string;
11
+ storePublicKey: string;
12
+ serverDid: string;
13
+ serverPublicKey: string;
14
+ serverSignature: string;
15
+ }): Promise<void>;
16
+ declare const isFreeBlocklet: (blocklet: {
17
+ pricing: {
18
+ paymentType: string;
19
+ };
20
+ }) => boolean;
21
+ /**
22
+ * 根据 prices 的结构, 获取支付价格的 label
23
+ */
24
+ declare const parsePaymentPriceLabel: (pricing?: {
25
+ price?: string;
26
+ symbol?: string;
27
+ }) => string;
28
+
29
+ export { calcPercent, isFreeBlocklet, parsePaymentPriceLabel, removeTrailingZeros, verifyDownloadToken };
@@ -0,0 +1,29 @@
1
+ declare function removeTrailingZeros(numStr: string): string;
2
+ /**
3
+ * 计算给定价格的比例, 默认使用百分制,使用 18 位精度进行高精度计算。
4
+ * @example
5
+ */
6
+ declare function calcPercent(input: string | number, percent: string, points?: string): string;
7
+
8
+ declare function verifyDownloadToken({ blockletDid, downloadToken, storePublicKey, serverDid, serverPublicKey, serverSignature, }: {
9
+ blockletDid: string;
10
+ downloadToken: string;
11
+ storePublicKey: string;
12
+ serverDid: string;
13
+ serverPublicKey: string;
14
+ serverSignature: string;
15
+ }): Promise<void>;
16
+ declare const isFreeBlocklet: (blocklet: {
17
+ pricing: {
18
+ paymentType: string;
19
+ };
20
+ }) => boolean;
21
+ /**
22
+ * 根据 prices 的结构, 获取支付价格的 label
23
+ */
24
+ declare const parsePaymentPriceLabel: (pricing?: {
25
+ price?: string;
26
+ symbol?: string;
27
+ }) => string;
28
+
29
+ export { calcPercent, isFreeBlocklet, parsePaymentPriceLabel, removeTrailingZeros, verifyDownloadToken };
package/dist/index.d.ts CHANGED
@@ -1,3 +1,10 @@
1
+ declare function removeTrailingZeros(numStr: string): string;
2
+ /**
3
+ * 计算给定价格的比例, 默认使用百分制,使用 18 位精度进行高精度计算。
4
+ * @example
5
+ */
6
+ declare function calcPercent(input: string | number, percent: string, points?: string): string;
7
+
1
8
  declare function verifyDownloadToken({ blockletDid, downloadToken, storePublicKey, serverDid, serverPublicKey, serverSignature, }: {
2
9
  blockletDid: string;
3
10
  downloadToken: string;
@@ -6,4 +13,17 @@ declare function verifyDownloadToken({ blockletDid, downloadToken, storePublicKe
6
13
  serverPublicKey: string;
7
14
  serverSignature: string;
8
15
  }): Promise<void>;
9
- export { verifyDownloadToken };
16
+ declare const isFreeBlocklet: (blocklet: {
17
+ pricing: {
18
+ paymentType: string;
19
+ };
20
+ }) => boolean;
21
+ /**
22
+ * 根据 prices 的结构, 获取支付价格的 label
23
+ */
24
+ declare const parsePaymentPriceLabel: (pricing?: {
25
+ price?: string;
26
+ symbol?: string;
27
+ }) => string;
28
+
29
+ export { calcPercent, isFreeBlocklet, parsePaymentPriceLabel, removeTrailingZeros, verifyDownloadToken };
package/dist/index.mjs ADDED
@@ -0,0 +1,80 @@
1
+ import { verify, decode } from '@arcblock/jwt';
2
+ import { isFromPublicKey } from '@arcblock/did';
3
+ import isEmpty from 'lodash/isEmpty';
4
+ import { BN } from '@ocap/util';
5
+
6
+ function removeTrailingZeros(numStr) {
7
+ if (!numStr.includes(".")) {
8
+ return numStr;
9
+ }
10
+ return numStr.replace(/(\.\d*?[1-9])0+$|\.0*$/, "$1");
11
+ }
12
+ function calcPercent(input, percent, points = "100") {
13
+ let price = typeof input === "number" ? input.toString() : input;
14
+ price = price.replace(/(\.\d{18})\d+/, "$1");
15
+ let bnWatchPrice = new BN(price.replace(".", ""));
16
+ let decimalPlaces = price.split(".")[1] ? price.split(".")[1].length : 0;
17
+ if (decimalPlaces > 18) {
18
+ decimalPlaces = 0;
19
+ }
20
+ const times = 18;
21
+ const precision = new BN(10).pow(new BN(times - decimalPlaces));
22
+ bnWatchPrice = bnWatchPrice.mul(precision);
23
+ const percentBN = new BN(percent);
24
+ const pointsBN = new BN(points);
25
+ const finalResultBN = bnWatchPrice.mul(percentBN).div(pointsBN);
26
+ let finalResultStr = finalResultBN.toString();
27
+ const finalResultStrLen = finalResultStr.length;
28
+ if (finalResultStrLen <= times) {
29
+ return removeTrailingZeros(`0.${"0".repeat(times - finalResultStrLen)}${finalResultStr}`);
30
+ }
31
+ finalResultStr = [
32
+ finalResultStr.slice(0, finalResultStr.length - times),
33
+ finalResultStr.slice(finalResultStr.length - times)
34
+ ].join(".");
35
+ return removeTrailingZeros(finalResultStr);
36
+ }
37
+
38
+ async function verifyDownloadToken({
39
+ blockletDid,
40
+ downloadToken,
41
+ storePublicKey,
42
+ serverDid,
43
+ serverPublicKey,
44
+ serverSignature
45
+ }) {
46
+ if (isEmpty(downloadToken)) {
47
+ throw new Error("downloadToken must be provided");
48
+ }
49
+ if (!verify(downloadToken, storePublicKey)) {
50
+ throw new Error("downloadToken is invalid");
51
+ }
52
+ const payloadFromStoreSignature = decode(downloadToken, true);
53
+ if (payloadFromStoreSignature.serverDid !== serverDid) {
54
+ throw new Error("serverDid mismatch");
55
+ }
56
+ if (payloadFromStoreSignature.blockletDid !== blockletDid) {
57
+ throw new Error("blockletDid mismatch");
58
+ }
59
+ if (isEmpty(serverPublicKey)) {
60
+ throw new Error("serverPublicKey must be provided");
61
+ }
62
+ if (!isFromPublicKey(serverDid, serverPublicKey)) {
63
+ throw new Error("serverDid and serverPublicKey mismatch");
64
+ }
65
+ if (isEmpty(serverSignature)) {
66
+ throw new Error("serverSignature must be provided");
67
+ }
68
+ if (!verify(serverSignature, serverPublicKey)) {
69
+ throw new Error("serverSignature is invalid");
70
+ }
71
+ }
72
+ const isFreeBlocklet = (blocklet) => blocklet?.pricing?.paymentType === "free";
73
+ const parsePaymentPriceLabel = (pricing) => {
74
+ if (!pricing) {
75
+ return "";
76
+ }
77
+ return `${pricing.price} ${pricing.symbol || "ABT"}`;
78
+ };
79
+
80
+ export { calcPercent, isFreeBlocklet, parsePaymentPriceLabel, removeTrailingZeros, verifyDownloadToken };
package/package.json CHANGED
@@ -1,11 +1,17 @@
1
1
  {
2
2
  "name": "@blocklet/util",
3
- "version": "0.12.90",
3
+ "version": "0.12.91",
4
4
  "author": "skypesky ye",
5
5
  "license": "ISC",
6
6
  "description": "",
7
- "main": "dist/index.js",
8
- "typings": "dist/index.d.ts",
7
+ "exports": {
8
+ ".": {
9
+ "import": "./dist/index.mjs",
10
+ "require": "./dist/index.cjs"
11
+ }
12
+ },
13
+ "main": "dist/index.cjs",
14
+ "types": "dist/index.d.ts",
9
15
  "publishConfig": {
10
16
  "access": "public"
11
17
  },
@@ -13,27 +19,32 @@
13
19
  "dist"
14
20
  ],
15
21
  "scripts": {
16
- "test": "jest",
22
+ "test": "vitest run",
23
+ "test:watch": "vitest",
17
24
  "lint": "eslint src tests",
18
- "lint:fix": "yarn lint --fix",
19
- "coverage": "npm run test -- --coverage --forceExit",
25
+ "lint:fix": "pnpm lint --fix",
26
+ "coverage": "vitest run --coverage",
20
27
  "prebuild": "rm -fr dist",
21
- "build": "tsc"
28
+ "build": "unbuild"
22
29
  },
23
30
  "devDependencies": {
24
31
  "@arcblock/eslint-config-ts": "^0.3.0",
25
- "@types/jest": "^28.1.7",
32
+ "@types/lodash": "^4.17.4",
33
+ "@types/node": "^20.12.12",
26
34
  "@typescript-eslint/eslint-plugin": "^5.59.5",
35
+ "@vitest/coverage-v8": "^1.6.0",
27
36
  "eslint": "^8.22.0",
28
- "jest": "^28.1.3",
29
- "ts-jest": "^28.0.8",
30
- "typescript": "~5.0.4"
37
+ "typescript": "5.1.6",
38
+ "vitest": "^1.6.0"
31
39
  },
32
40
  "dependencies": {
33
- "@arcblock/did": "^1.18.117",
34
- "@arcblock/jwt": "^1.18.117",
35
- "@ocap/wallet": "^1.18.117",
36
- "lodash": "^4.17.21"
41
+ "@arcblock/did": "^1.18.120",
42
+ "@arcblock/jwt": "^1.18.120",
43
+ "@ocap/util": "^1.18.120",
44
+ "@ocap/wallet": "^1.18.120",
45
+ "lodash": "^4.17.21",
46
+ "tslib": "^2.6.2",
47
+ "unbuild": "^2.0.0"
37
48
  },
38
- "gitHead": "5055b698e31c8555b6b266901f3f5863c740d6f0"
49
+ "gitHead": "960c4679590e2f9a2a70e2848631934f02deba8a"
39
50
  }
package/dist/index.js DELETED
@@ -1,50 +0,0 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- var __importDefault = (this && this.__importDefault) || function (mod) {
12
- return (mod && mod.__esModule) ? mod : { "default": mod };
13
- };
14
- Object.defineProperty(exports, "__esModule", { value: true });
15
- exports.verifyDownloadToken = void 0;
16
- /* eslint-disable import/prefer-default-export */
17
- const jwt_1 = require("@arcblock/jwt");
18
- const did_1 = require("@arcblock/did");
19
- const isEmpty_1 = __importDefault(require("lodash/isEmpty"));
20
- // eslint-disable-next-line require-await
21
- function verifyDownloadToken({ blockletDid, downloadToken, storePublicKey, serverDid, serverPublicKey, serverSignature, }) {
22
- return __awaiter(this, void 0, void 0, function* () {
23
- if ((0, isEmpty_1.default)(downloadToken)) {
24
- throw new Error('downloadToken must be provided');
25
- }
26
- if (!(0, jwt_1.verify)(downloadToken, storePublicKey)) {
27
- throw new Error('downloadToken is invalid');
28
- }
29
- const payloadFromStoreSignature = (0, jwt_1.decode)(downloadToken, true);
30
- if (payloadFromStoreSignature.serverDid !== serverDid) {
31
- throw new Error('serverDid mismatch');
32
- }
33
- if (payloadFromStoreSignature.blockletDid !== blockletDid) {
34
- throw new Error('blockletDid mismatch');
35
- }
36
- if ((0, isEmpty_1.default)(serverPublicKey)) {
37
- throw new Error('serverPublicKey must be provided');
38
- }
39
- if (!(0, did_1.isFromPublicKey)(serverDid, serverPublicKey)) {
40
- throw new Error('serverDid and serverPublicKey mismatch');
41
- }
42
- if ((0, isEmpty_1.default)(serverSignature)) {
43
- throw new Error('serverSignature must be provided');
44
- }
45
- if (!(0, jwt_1.verify)(serverSignature, serverPublicKey)) {
46
- throw new Error('serverSignature is invalid');
47
- }
48
- });
49
- }
50
- exports.verifyDownloadToken = verifyDownloadToken;