@ocap/util 1.18.166 → 1.19.1
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/esm/constant.d.ts +1 -0
- package/esm/constant.js +1 -0
- package/esm/create-sorted-list.d.ts +1 -0
- package/esm/create-sorted-list.js +3 -0
- package/esm/error.d.ts +11 -0
- package/esm/error.js +17 -0
- package/esm/get-list-field.d.ts +1 -0
- package/esm/get-list-field.js +2 -0
- package/esm/get-related-addr.d.ts +1 -0
- package/esm/get-related-addr.js +1 -0
- package/esm/index.d.ts +219 -0
- package/esm/index.js +526 -0
- package/esm/md5.d.ts +1 -0
- package/esm/md5.js +3 -0
- package/esm/ready.d.ts +10 -0
- package/esm/ready.js +36 -0
- package/lib/index.d.ts +4 -6
- package/lib/index.js +14 -14
- package/lib/md5.js +1 -0
- package/lib/ready.d.ts +0 -1
- package/package.json +29 -15
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const DEFAULT_TOKEN_DECIMAL = 18;
|
package/esm/constant.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const DEFAULT_TOKEN_DECIMAL = 18;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const createSortedList: (list: $TSFixMe) => any;
|
package/esm/error.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export declare class CustomError extends Error {
|
|
2
|
+
code: string;
|
|
3
|
+
props: {
|
|
4
|
+
persist: boolean;
|
|
5
|
+
[prop: string]: $TSFixMe;
|
|
6
|
+
};
|
|
7
|
+
constructor(code: string, message: string, props?: {});
|
|
8
|
+
}
|
|
9
|
+
export declare class PersistError extends CustomError {
|
|
10
|
+
constructor(code: string, message: string, props?: {});
|
|
11
|
+
}
|
package/esm/error.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/* eslint-disable max-classes-per-file */
|
|
2
|
+
export class CustomError extends Error {
|
|
3
|
+
constructor(code, message, props = {}) {
|
|
4
|
+
super(message);
|
|
5
|
+
if (Error.captureStackTrace) {
|
|
6
|
+
Error.captureStackTrace(this, CustomError);
|
|
7
|
+
}
|
|
8
|
+
this.code = code;
|
|
9
|
+
this.props = { persist: false, ...props };
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
export class PersistError extends CustomError {
|
|
13
|
+
constructor(code, message, props = {}) {
|
|
14
|
+
super(code, message);
|
|
15
|
+
this.props = { persist: true, ...props };
|
|
16
|
+
}
|
|
17
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const getListField: (obj: $TSFixMe, key: string) => any;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const getRelatedAddresses: (state: $TSFixMe) => any[];
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const getRelatedAddresses = (state) => [state.address].concat(state.migratedFrom || []).filter(Boolean);
|
package/esm/index.d.ts
ADDED
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
import type { LiteralUnion } from 'type-fest';
|
|
2
|
+
import rightPad from 'lodash/padEnd';
|
|
3
|
+
import leftPad from 'lodash/padStart';
|
|
4
|
+
import BN from 'bn.js';
|
|
5
|
+
export declare const isBase58btc: (data: any) => boolean;
|
|
6
|
+
export type BytesType = string | Buffer | Uint8Array;
|
|
7
|
+
export type EncodingType = LiteralUnion<'hex' | 'base16' | 'base58' | 'base64' | 'Uint8Array' | 'buffer', string>;
|
|
8
|
+
export type KeyPairType = {
|
|
9
|
+
publicKey: BytesType;
|
|
10
|
+
secretKey: BytesType;
|
|
11
|
+
};
|
|
12
|
+
export { BN, leftPad, rightPad };
|
|
13
|
+
/**
|
|
14
|
+
* Returns a BN object, converts a number value to a BN
|
|
15
|
+
* @param {string|number|BN} `arg` input a string number, hex string number, number, BigNumber or BN object
|
|
16
|
+
* @return {BN} `output` BN object of the number
|
|
17
|
+
* @throws if the argument is not an array, object that isn't a bignumber, not a string number or number
|
|
18
|
+
*/
|
|
19
|
+
export declare const numberToBN: (arg: string | number | BN) => BN;
|
|
20
|
+
/**
|
|
21
|
+
* Returns a `boolean` on whether or not the `string` starts with '0x'
|
|
22
|
+
*
|
|
23
|
+
* @public
|
|
24
|
+
* @static
|
|
25
|
+
* @param {String} str the string input value
|
|
26
|
+
* @return {Boolean} a boolean if it is or is not hex prefixed
|
|
27
|
+
* @throws if the str input is not a string
|
|
28
|
+
*/
|
|
29
|
+
export declare const isHexPrefixed: (str: string) => boolean;
|
|
30
|
+
/**
|
|
31
|
+
* Removes '0x' from a given `String` if present
|
|
32
|
+
*
|
|
33
|
+
* @public
|
|
34
|
+
* @static
|
|
35
|
+
*/
|
|
36
|
+
export declare const stripHexPrefix: (str: string | any) => any;
|
|
37
|
+
/**
|
|
38
|
+
* Returns true if object is BN, otherwise false
|
|
39
|
+
*
|
|
40
|
+
* @public
|
|
41
|
+
* @static
|
|
42
|
+
* @method isBN
|
|
43
|
+
* @param {Object} object
|
|
44
|
+
* @returns {Boolean}
|
|
45
|
+
*/
|
|
46
|
+
export declare const isBN: (object: $TSFixMe) => boolean;
|
|
47
|
+
/**
|
|
48
|
+
* Returns true if object is BigNumber, otherwise false
|
|
49
|
+
*
|
|
50
|
+
* @public
|
|
51
|
+
* @static
|
|
52
|
+
* @method isBigNumber
|
|
53
|
+
* @param {Object} object
|
|
54
|
+
* @returns {Boolean}
|
|
55
|
+
*/
|
|
56
|
+
export declare const isBigNumber: (object: $TSFixMe) => boolean;
|
|
57
|
+
/**
|
|
58
|
+
* Check if string is HEX, requires a 0x in front
|
|
59
|
+
*
|
|
60
|
+
* @public
|
|
61
|
+
* @static
|
|
62
|
+
* @method isHexStrict
|
|
63
|
+
* @param {String} hex to be checked
|
|
64
|
+
* @returns {Boolean}
|
|
65
|
+
*/
|
|
66
|
+
export declare const isHexStrict: (hex: string) => boolean;
|
|
67
|
+
/**
|
|
68
|
+
* Check if string is HEX
|
|
69
|
+
*
|
|
70
|
+
* @public
|
|
71
|
+
* @static
|
|
72
|
+
* @method isHex
|
|
73
|
+
* @param {String} hex to be checked
|
|
74
|
+
* @returns {Boolean}
|
|
75
|
+
*/
|
|
76
|
+
export declare const isHex: (hex: string) => boolean;
|
|
77
|
+
/**
|
|
78
|
+
* Takes an input and transforms it into an BN
|
|
79
|
+
*
|
|
80
|
+
* @public
|
|
81
|
+
* @static
|
|
82
|
+
* @method toBN
|
|
83
|
+
* @param {Number|String|BN} num, string, HEX string or BN
|
|
84
|
+
* @returns {BN} BN
|
|
85
|
+
*/
|
|
86
|
+
export declare const toBN: (num: number | string | BN, base?: number | "hex") => BN;
|
|
87
|
+
/**
|
|
88
|
+
* Should be called to get hex representation (prefixed by 0x) of utf8 string
|
|
89
|
+
*
|
|
90
|
+
* @public
|
|
91
|
+
* @static
|
|
92
|
+
* @method utf8ToHex
|
|
93
|
+
* @param {String} str
|
|
94
|
+
* @returns {String} hex representation of input string
|
|
95
|
+
*/
|
|
96
|
+
export declare const utf8ToHex: (str: string) => string;
|
|
97
|
+
/**
|
|
98
|
+
* Should be called to get utf8 from it's hex representation
|
|
99
|
+
*
|
|
100
|
+
* @public
|
|
101
|
+
* @static
|
|
102
|
+
* @method hexToUtf8
|
|
103
|
+
* @param {String} hex
|
|
104
|
+
* @returns {String} ascii string representation of hex value
|
|
105
|
+
*/
|
|
106
|
+
export declare const hexToUtf8: (hex: string) => string;
|
|
107
|
+
/**
|
|
108
|
+
* Converts value to number representation
|
|
109
|
+
*
|
|
110
|
+
* @public
|
|
111
|
+
* @static
|
|
112
|
+
* @method hexToNumber
|
|
113
|
+
* @param {String|Number|BN} value
|
|
114
|
+
* @returns {Number}
|
|
115
|
+
*/
|
|
116
|
+
export declare const hexToNumber: (value: string | number | BN) => number;
|
|
117
|
+
/**
|
|
118
|
+
* Converts value to hex representation
|
|
119
|
+
*
|
|
120
|
+
* @public
|
|
121
|
+
* @static
|
|
122
|
+
* @method numberToHex
|
|
123
|
+
* @param {String|Number|BN} value
|
|
124
|
+
* @returns {String}
|
|
125
|
+
*/
|
|
126
|
+
export declare const numberToHex: (value: $TSFixMe) => string;
|
|
127
|
+
/**
|
|
128
|
+
* Convert a byte array to a hex string
|
|
129
|
+
* Note: Implementation from crypto-js
|
|
130
|
+
*
|
|
131
|
+
* @public
|
|
132
|
+
* @static
|
|
133
|
+
* @method bytesToHex
|
|
134
|
+
* @param {Array} bytes
|
|
135
|
+
* @returns {String} the hex string
|
|
136
|
+
*/
|
|
137
|
+
export declare const bytesToHex: (bytes: $TSFixMe) => string;
|
|
138
|
+
/**
|
|
139
|
+
* Convert a hex string to a byte array
|
|
140
|
+
* Note: Implementation from crypto-js
|
|
141
|
+
*
|
|
142
|
+
* @public
|
|
143
|
+
* @static
|
|
144
|
+
* @method hexToBytes
|
|
145
|
+
* @param {String} hex
|
|
146
|
+
* @returns {Array} the byte array
|
|
147
|
+
*/
|
|
148
|
+
export declare const hexToBytes: (hex: $TSFixMe) => Array<any>;
|
|
149
|
+
/**
|
|
150
|
+
* Auto converts any given value into it's hex representation.
|
|
151
|
+
* And even stringify objects before.
|
|
152
|
+
*
|
|
153
|
+
* @public
|
|
154
|
+
* @static
|
|
155
|
+
* @method toHex
|
|
156
|
+
* @param {String|Number|BN|Object|TypedArray|Buffer} value
|
|
157
|
+
* @param {Boolean} returnType
|
|
158
|
+
* @returns {String}
|
|
159
|
+
*/
|
|
160
|
+
export declare const toHex: (value: string | number | boolean | BN | Uint8Array | Buffer | (number | {
|
|
161
|
+
test: string;
|
|
162
|
+
})[] | {
|
|
163
|
+
test: string;
|
|
164
|
+
}, returnType?: boolean) => string;
|
|
165
|
+
export declare const numberToString: (arg: $TSFixMe) => any;
|
|
166
|
+
/**
|
|
167
|
+
* Format a big number to human readable number, such as 1_0000_0000_0000_000 => 1 Token
|
|
168
|
+
*/
|
|
169
|
+
export declare const fromUnitToToken: (input: string | number | BN, decimal?: number, optionsInput?: $TSFixMe) => string;
|
|
170
|
+
/**
|
|
171
|
+
* Convert human readable token number to big number instance
|
|
172
|
+
*/
|
|
173
|
+
export declare const fromTokenToUnit: (input: string | number, decimal?: number) => BN;
|
|
174
|
+
/**
|
|
175
|
+
* Validates if a value is an Uint8Array.
|
|
176
|
+
*/
|
|
177
|
+
export declare function isUint8Array(value: $TSFixMe): boolean;
|
|
178
|
+
/**
|
|
179
|
+
* Generate a random UUID
|
|
180
|
+
*/
|
|
181
|
+
export declare function UUID(): string;
|
|
182
|
+
/**
|
|
183
|
+
* Check if a string is valid UUID
|
|
184
|
+
*/
|
|
185
|
+
export declare function isUUID(str: string): boolean;
|
|
186
|
+
/**
|
|
187
|
+
* Convert input to Uint8Array on best effort, base64 node supported
|
|
188
|
+
*/
|
|
189
|
+
export declare function toUint8Array(v: any): Uint8Array;
|
|
190
|
+
/**
|
|
191
|
+
* Convert input to Buffer on best effort, base64 not supported
|
|
192
|
+
*/
|
|
193
|
+
export declare function toBuffer(v: any): Buffer;
|
|
194
|
+
/**
|
|
195
|
+
* Convert input to base58btc format on best effort
|
|
196
|
+
*/
|
|
197
|
+
export declare function toBase58(v: any): string;
|
|
198
|
+
/**
|
|
199
|
+
* Decode base58 string
|
|
200
|
+
*/
|
|
201
|
+
export declare function fromBase58(v: string): Buffer;
|
|
202
|
+
/**
|
|
203
|
+
* Convert input to base64 format
|
|
204
|
+
*/
|
|
205
|
+
export declare function toBase64(v: any, escape?: boolean): string;
|
|
206
|
+
/**
|
|
207
|
+
* Decode base64(base64_url) string to buffer
|
|
208
|
+
*/
|
|
209
|
+
export declare function fromBase64(v: string): Buffer;
|
|
210
|
+
/**
|
|
211
|
+
* Convert did to address: remove `did:abt:` prefix
|
|
212
|
+
*/
|
|
213
|
+
export declare function toAddress(did: string): string;
|
|
214
|
+
/**
|
|
215
|
+
* Convert address to did: prepend `did:abt:` prefix
|
|
216
|
+
*/
|
|
217
|
+
export declare function toDid(address: string): string;
|
|
218
|
+
export declare function isSameDid(a: string, b: string): boolean;
|
|
219
|
+
export declare function formatTxType(type: string): any;
|
package/esm/index.js
ADDED
|
@@ -0,0 +1,526 @@
|
|
|
1
|
+
import isBoolean from 'lodash/isBoolean';
|
|
2
|
+
import isString from 'lodash/isString';
|
|
3
|
+
import isNumber from 'lodash/isNumber';
|
|
4
|
+
import isObject from 'lodash/isObject';
|
|
5
|
+
import upperFirst from 'lodash/upperFirst';
|
|
6
|
+
import camelCase from 'lodash/camelCase';
|
|
7
|
+
import isNull from 'lodash/isNull';
|
|
8
|
+
import rightPad from 'lodash/padEnd';
|
|
9
|
+
import leftPad from 'lodash/padStart';
|
|
10
|
+
import base58 from 'bs58';
|
|
11
|
+
import base64 from 'base64-url';
|
|
12
|
+
import BN from 'bn.js';
|
|
13
|
+
import * as utf8 from 'utf8';
|
|
14
|
+
const DID_PREFIX = 'did:abt:';
|
|
15
|
+
const zero = new BN(0);
|
|
16
|
+
const negative1 = new BN(-1);
|
|
17
|
+
const base58btc = {
|
|
18
|
+
encode: (v) => `z${base58.encode(v)}`,
|
|
19
|
+
decode: (v) => base58.decode(v.slice(1)),
|
|
20
|
+
};
|
|
21
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
22
|
+
export const isBase58btc = (data) => {
|
|
23
|
+
if (typeof data !== 'string') {
|
|
24
|
+
return false;
|
|
25
|
+
}
|
|
26
|
+
if (data[0] === 'z') {
|
|
27
|
+
try {
|
|
28
|
+
base58btc.decode(data);
|
|
29
|
+
return true;
|
|
30
|
+
}
|
|
31
|
+
catch (err) {
|
|
32
|
+
return false;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return false;
|
|
36
|
+
};
|
|
37
|
+
export { BN, leftPad, rightPad };
|
|
38
|
+
/**
|
|
39
|
+
* Returns a BN object, converts a number value to a BN
|
|
40
|
+
* @param {string|number|BN} `arg` input a string number, hex string number, number, BigNumber or BN object
|
|
41
|
+
* @return {BN} `output` BN object of the number
|
|
42
|
+
* @throws if the argument is not an array, object that isn't a bignumber, not a string number or number
|
|
43
|
+
*/
|
|
44
|
+
export const numberToBN = (arg) => {
|
|
45
|
+
if (typeof arg === 'string' || typeof arg === 'number') {
|
|
46
|
+
let multiplier = new BN(1); // eslint-disable-line
|
|
47
|
+
const formattedString = String(arg).toLowerCase().trim();
|
|
48
|
+
const isHexPrefixed = formattedString.substr(0, 2) === '0x' || formattedString.substr(0, 3) === '-0x';
|
|
49
|
+
let stringArg = stripHexPrefix(formattedString); // eslint-disable-line
|
|
50
|
+
if (stringArg.substr(0, 1) === '-') {
|
|
51
|
+
stringArg = stripHexPrefix(stringArg.slice(1));
|
|
52
|
+
multiplier = new BN(-1, 10);
|
|
53
|
+
}
|
|
54
|
+
stringArg = stringArg === '' ? '0' : stringArg;
|
|
55
|
+
if ((!stringArg.match(/^-?[0-9]+$/) && stringArg.match(/^[0-9A-Fa-f]+$/)) ||
|
|
56
|
+
stringArg.match(/^[a-fA-F]+$/) ||
|
|
57
|
+
(isHexPrefixed === true && stringArg.match(/^[0-9A-Fa-f]+$/))) {
|
|
58
|
+
return new BN(stringArg, 16).mul(multiplier);
|
|
59
|
+
}
|
|
60
|
+
if ((stringArg.match(/^-?[0-9]+$/) || stringArg === '') && isHexPrefixed === false) {
|
|
61
|
+
return new BN(stringArg, 10).mul(multiplier);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
if (isBN(arg)) {
|
|
65
|
+
return new BN(arg.toString(10), 10);
|
|
66
|
+
}
|
|
67
|
+
throw new Error(`[number-to-bn] while converting number ${JSON.stringify(arg)} to BN.js instance, error: invalid number value. Value must be an integer, hex string, BN or BigNumber instance. Note, decimals are not supported.`);
|
|
68
|
+
};
|
|
69
|
+
/**
|
|
70
|
+
* Returns a `boolean` on whether or not the `string` starts with '0x'
|
|
71
|
+
*
|
|
72
|
+
* @public
|
|
73
|
+
* @static
|
|
74
|
+
* @param {String} str the string input value
|
|
75
|
+
* @return {Boolean} a boolean if it is or is not hex prefixed
|
|
76
|
+
* @throws if the str input is not a string
|
|
77
|
+
*/
|
|
78
|
+
export const isHexPrefixed = (str) => {
|
|
79
|
+
if (typeof str !== 'string') {
|
|
80
|
+
throw new Error('[is-hex-prefixed] value must be type string');
|
|
81
|
+
}
|
|
82
|
+
return str.slice(0, 2).toLowerCase() === '0x';
|
|
83
|
+
};
|
|
84
|
+
/**
|
|
85
|
+
* Removes '0x' from a given `String` if present
|
|
86
|
+
*
|
|
87
|
+
* @public
|
|
88
|
+
* @static
|
|
89
|
+
*/
|
|
90
|
+
export const stripHexPrefix = (str) => {
|
|
91
|
+
if (typeof str === 'string') {
|
|
92
|
+
return isHexPrefixed(str) ? str.slice(2) : str;
|
|
93
|
+
}
|
|
94
|
+
return str;
|
|
95
|
+
};
|
|
96
|
+
/**
|
|
97
|
+
* Returns true if object is BN, otherwise false
|
|
98
|
+
*
|
|
99
|
+
* @public
|
|
100
|
+
* @static
|
|
101
|
+
* @method isBN
|
|
102
|
+
* @param {Object} object
|
|
103
|
+
* @returns {Boolean}
|
|
104
|
+
*/
|
|
105
|
+
export const isBN = (object) => object instanceof BN || (object && object.constructor && object.constructor.name === 'BN');
|
|
106
|
+
/**
|
|
107
|
+
* Returns true if object is BigNumber, otherwise false
|
|
108
|
+
*
|
|
109
|
+
* @public
|
|
110
|
+
* @static
|
|
111
|
+
* @method isBigNumber
|
|
112
|
+
* @param {Object} object
|
|
113
|
+
* @returns {Boolean}
|
|
114
|
+
*/
|
|
115
|
+
export const isBigNumber = (object) => object && object.constructor && object.constructor.name === 'BigNumber';
|
|
116
|
+
/**
|
|
117
|
+
* Check if string is HEX, requires a 0x in front
|
|
118
|
+
*
|
|
119
|
+
* @public
|
|
120
|
+
* @static
|
|
121
|
+
* @method isHexStrict
|
|
122
|
+
* @param {String} hex to be checked
|
|
123
|
+
* @returns {Boolean}
|
|
124
|
+
*/
|
|
125
|
+
export const isHexStrict = (hex) => (isString(hex) || isNumber(hex)) && /^(-)?0x[0-9a-f]*$/i.test(hex);
|
|
126
|
+
/**
|
|
127
|
+
* Check if string is HEX
|
|
128
|
+
*
|
|
129
|
+
* @public
|
|
130
|
+
* @static
|
|
131
|
+
* @method isHex
|
|
132
|
+
* @param {String} hex to be checked
|
|
133
|
+
* @returns {Boolean}
|
|
134
|
+
*/
|
|
135
|
+
export const isHex = (hex) => (isString(hex) || isNumber(hex)) && /^(-0x|0x|0X|-0X)?[0-9a-f]*$/i.test(hex);
|
|
136
|
+
/**
|
|
137
|
+
* Takes an input and transforms it into an BN
|
|
138
|
+
*
|
|
139
|
+
* @public
|
|
140
|
+
* @static
|
|
141
|
+
* @method toBN
|
|
142
|
+
* @param {Number|String|BN} num, string, HEX string or BN
|
|
143
|
+
* @returns {BN} BN
|
|
144
|
+
*/
|
|
145
|
+
export const toBN = (num, base = 10) => {
|
|
146
|
+
try {
|
|
147
|
+
if (typeof num === 'number' && num > 0) {
|
|
148
|
+
const numStr = Number(num).toLocaleString('fullwide', { useGrouping: false });
|
|
149
|
+
return new BN(numStr, base);
|
|
150
|
+
}
|
|
151
|
+
return numberToBN(num);
|
|
152
|
+
}
|
|
153
|
+
catch (error) {
|
|
154
|
+
throw new Error(`${error} Given value: "${num}"`);
|
|
155
|
+
}
|
|
156
|
+
};
|
|
157
|
+
/**
|
|
158
|
+
* Should be called to get hex representation (prefixed by 0x) of utf8 string
|
|
159
|
+
*
|
|
160
|
+
* @public
|
|
161
|
+
* @static
|
|
162
|
+
* @method utf8ToHex
|
|
163
|
+
* @param {String} str
|
|
164
|
+
* @returns {String} hex representation of input string
|
|
165
|
+
*/
|
|
166
|
+
export const utf8ToHex = (str) => {
|
|
167
|
+
str = utf8.encode(str);
|
|
168
|
+
let hex = '';
|
|
169
|
+
/* eslint-disable no-control-regex */
|
|
170
|
+
// remove \u0000 padding from either side
|
|
171
|
+
str = str.replace(/^(?:\u0000)*/, '');
|
|
172
|
+
str = str.split('').reverse().join('');
|
|
173
|
+
str = str.replace(/^(?:\u0000)*/, '');
|
|
174
|
+
str = str.split('').reverse().join('');
|
|
175
|
+
/* eslint-enable no-control-regex */
|
|
176
|
+
for (let i = 0; i < str.length; i++) {
|
|
177
|
+
const code = str.charCodeAt(i);
|
|
178
|
+
const n = code.toString(16);
|
|
179
|
+
hex += n.length < 2 ? `0${n}` : n;
|
|
180
|
+
}
|
|
181
|
+
return `0x${hex}`;
|
|
182
|
+
};
|
|
183
|
+
/**
|
|
184
|
+
* Should be called to get utf8 from it's hex representation
|
|
185
|
+
*
|
|
186
|
+
* @public
|
|
187
|
+
* @static
|
|
188
|
+
* @method hexToUtf8
|
|
189
|
+
* @param {String} hex
|
|
190
|
+
* @returns {String} ascii string representation of hex value
|
|
191
|
+
*/
|
|
192
|
+
export const hexToUtf8 = (hex) => {
|
|
193
|
+
if (!isHexStrict(hex))
|
|
194
|
+
throw new Error(`The parameter "${hex}" must be a valid HEX string.`);
|
|
195
|
+
let str = '';
|
|
196
|
+
let code = 0;
|
|
197
|
+
hex = hex.replace(/^0x/i, '');
|
|
198
|
+
// remove 00 padding from either side
|
|
199
|
+
hex = hex.replace(/^(?:00)*/, '');
|
|
200
|
+
hex = hex.split('').reverse().join('');
|
|
201
|
+
hex = hex.replace(/^(?:00)*/, '');
|
|
202
|
+
hex = hex.split('').reverse().join('');
|
|
203
|
+
const l = hex.length;
|
|
204
|
+
for (let i = 0; i < l; i += 2) {
|
|
205
|
+
code = parseInt(hex.substr(i, 2), 16);
|
|
206
|
+
// if (code !== 0) {
|
|
207
|
+
str += String.fromCharCode(code);
|
|
208
|
+
// }
|
|
209
|
+
}
|
|
210
|
+
return utf8.decode(str);
|
|
211
|
+
};
|
|
212
|
+
/**
|
|
213
|
+
* Converts value to number representation
|
|
214
|
+
*
|
|
215
|
+
* @public
|
|
216
|
+
* @static
|
|
217
|
+
* @method hexToNumber
|
|
218
|
+
* @param {String|Number|BN} value
|
|
219
|
+
* @returns {Number}
|
|
220
|
+
*/
|
|
221
|
+
export const hexToNumber = (value) => {
|
|
222
|
+
if (!value) {
|
|
223
|
+
return 0;
|
|
224
|
+
}
|
|
225
|
+
return toBN(value).toNumber();
|
|
226
|
+
};
|
|
227
|
+
/**
|
|
228
|
+
* Converts value to hex representation
|
|
229
|
+
*
|
|
230
|
+
* @public
|
|
231
|
+
* @static
|
|
232
|
+
* @method numberToHex
|
|
233
|
+
* @param {String|Number|BN} value
|
|
234
|
+
* @returns {String}
|
|
235
|
+
*/
|
|
236
|
+
export const numberToHex = (value) => {
|
|
237
|
+
if (isNull(value) || typeof value === 'undefined') {
|
|
238
|
+
return value;
|
|
239
|
+
}
|
|
240
|
+
// eslint-disable-next-line no-restricted-globals
|
|
241
|
+
if (!isFinite(value) && !isHex(value)) {
|
|
242
|
+
throw new Error(`Given input "${value}" is not a number.`);
|
|
243
|
+
}
|
|
244
|
+
const num = toBN(value);
|
|
245
|
+
const result = num.toString(16);
|
|
246
|
+
return num.lt(new BN(0)) ? `-0x${result.substr(1)}` : `0x${result}`;
|
|
247
|
+
};
|
|
248
|
+
/**
|
|
249
|
+
* Convert a byte array to a hex string
|
|
250
|
+
* Note: Implementation from crypto-js
|
|
251
|
+
*
|
|
252
|
+
* @public
|
|
253
|
+
* @static
|
|
254
|
+
* @method bytesToHex
|
|
255
|
+
* @param {Array} bytes
|
|
256
|
+
* @returns {String} the hex string
|
|
257
|
+
*/
|
|
258
|
+
export const bytesToHex = (bytes) => {
|
|
259
|
+
const hex = [];
|
|
260
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
261
|
+
hex.push((bytes[i] >>> 4).toString(16));
|
|
262
|
+
hex.push((bytes[i] & 0xf).toString(16));
|
|
263
|
+
}
|
|
264
|
+
return `0x${hex.join('')}`;
|
|
265
|
+
};
|
|
266
|
+
/**
|
|
267
|
+
* Convert a hex string to a byte array
|
|
268
|
+
* Note: Implementation from crypto-js
|
|
269
|
+
*
|
|
270
|
+
* @public
|
|
271
|
+
* @static
|
|
272
|
+
* @method hexToBytes
|
|
273
|
+
* @param {String} hex
|
|
274
|
+
* @returns {Array} the byte array
|
|
275
|
+
*/
|
|
276
|
+
export const hexToBytes = (hex) => {
|
|
277
|
+
hex = hex.toString(16);
|
|
278
|
+
if (!isHex(hex)) {
|
|
279
|
+
throw new Error(`Given value "${hex}" is not a valid hex string.`);
|
|
280
|
+
}
|
|
281
|
+
hex = hex.replace(/^0x/i, '');
|
|
282
|
+
hex = hex.length % 2 ? `0${hex}` : hex;
|
|
283
|
+
const bytes = [];
|
|
284
|
+
for (let c = 0; c < hex.length; c += 2) {
|
|
285
|
+
bytes.push(parseInt(hex.substr(c, 2), 16));
|
|
286
|
+
}
|
|
287
|
+
return bytes;
|
|
288
|
+
};
|
|
289
|
+
/**
|
|
290
|
+
* Auto converts any given value into it's hex representation.
|
|
291
|
+
* And even stringify objects before.
|
|
292
|
+
*
|
|
293
|
+
* @public
|
|
294
|
+
* @static
|
|
295
|
+
* @method toHex
|
|
296
|
+
* @param {String|Number|BN|Object|TypedArray|Buffer} value
|
|
297
|
+
* @param {Boolean} returnType
|
|
298
|
+
* @returns {String}
|
|
299
|
+
*/
|
|
300
|
+
export const toHex = (value, returnType = false) => {
|
|
301
|
+
if (isUint8Array(value) || Buffer.isBuffer(value)) {
|
|
302
|
+
return returnType ? 'bytes' : bytesToHex(value);
|
|
303
|
+
}
|
|
304
|
+
if (isBase58btc(value)) {
|
|
305
|
+
return returnType ? 'bytes' : bytesToHex(base58btc.decode(value));
|
|
306
|
+
}
|
|
307
|
+
if (isBoolean(value)) {
|
|
308
|
+
// eslint-disable-next-line no-nested-ternary
|
|
309
|
+
return returnType ? 'bool' : value ? '0x01' : '0x00';
|
|
310
|
+
}
|
|
311
|
+
if (isObject(value) && !isBigNumber(value) && !isBN(value)) {
|
|
312
|
+
return returnType ? 'string' : utf8ToHex(JSON.stringify(value));
|
|
313
|
+
}
|
|
314
|
+
// if its a negative number, pass it through numberToHex
|
|
315
|
+
if (typeof value === 'string') {
|
|
316
|
+
if (value.indexOf('-0x') === 0 || value.indexOf('-0X') === 0) {
|
|
317
|
+
return returnType ? 'int256' : numberToHex(value);
|
|
318
|
+
}
|
|
319
|
+
if (value.indexOf('0x') === 0 || value.indexOf('0X') === 0) {
|
|
320
|
+
return returnType ? 'bytes' : value;
|
|
321
|
+
}
|
|
322
|
+
// TODO: some edge case may be not properly handled here
|
|
323
|
+
return returnType ? 'string' : utf8ToHex(value);
|
|
324
|
+
}
|
|
325
|
+
// eslint-disable-next-line no-nested-ternary
|
|
326
|
+
return returnType ? (+value < 0 ? 'int256' : 'uint256') : numberToHex(value);
|
|
327
|
+
};
|
|
328
|
+
export const numberToString = (arg) => {
|
|
329
|
+
if (typeof arg === 'string') {
|
|
330
|
+
if (!arg.match(/^-?[0-9.]+$/)) {
|
|
331
|
+
throw new Error(`Invalid value '${arg}' while converting to string, should be a number matching (^-?[0-9.]+).`);
|
|
332
|
+
}
|
|
333
|
+
return arg;
|
|
334
|
+
}
|
|
335
|
+
if (typeof arg === 'number') {
|
|
336
|
+
if (Number.isInteger(arg) && arg > 0) {
|
|
337
|
+
return Number(arg).toLocaleString('fullwide', { useGrouping: false });
|
|
338
|
+
}
|
|
339
|
+
return String(arg);
|
|
340
|
+
}
|
|
341
|
+
if (typeof arg === 'object' && arg.toString && (arg.toTwos || arg.dividedToIntegerBy)) {
|
|
342
|
+
if (arg.toPrecision) {
|
|
343
|
+
return String(arg.toPrecision());
|
|
344
|
+
}
|
|
345
|
+
return arg.toString(10);
|
|
346
|
+
}
|
|
347
|
+
throw new Error(`while converting number to string, invalid number value '${arg}' type ${typeof arg}.`);
|
|
348
|
+
};
|
|
349
|
+
/**
|
|
350
|
+
* Format a big number to human readable number, such as 1_0000_0000_0000_000 => 1 Token
|
|
351
|
+
*/
|
|
352
|
+
export const fromUnitToToken = (input, decimal = 18, optionsInput = {}) => {
|
|
353
|
+
let unit = toBN(input);
|
|
354
|
+
const negative = unit.lt(zero);
|
|
355
|
+
const base = toBN(`1${'0'.repeat(decimal)}`, 10);
|
|
356
|
+
const baseLength = base.toString(10).length - 1 || 1;
|
|
357
|
+
const options = optionsInput || {};
|
|
358
|
+
if (negative) {
|
|
359
|
+
unit = unit.mul(negative1);
|
|
360
|
+
}
|
|
361
|
+
let fraction = unit.mod(base).toString(10);
|
|
362
|
+
while (fraction.length < baseLength) {
|
|
363
|
+
fraction = `0${fraction}`;
|
|
364
|
+
}
|
|
365
|
+
if (!options.pad) {
|
|
366
|
+
// eslint-disable-next-line prefer-destructuring
|
|
367
|
+
fraction = fraction.match(/^([0-9]*[1-9]|0)(0*)/)[1];
|
|
368
|
+
}
|
|
369
|
+
let whole = unit.div(base).toString(10);
|
|
370
|
+
if (options.commify) {
|
|
371
|
+
whole = whole.replace(/\B(?=(\d{3})+(?!\d))/g, ',');
|
|
372
|
+
}
|
|
373
|
+
let value = `${whole}${fraction === '0' ? '' : `.${fraction}`}`;
|
|
374
|
+
if (negative) {
|
|
375
|
+
value = `-${value}`;
|
|
376
|
+
}
|
|
377
|
+
return value;
|
|
378
|
+
};
|
|
379
|
+
/**
|
|
380
|
+
* Convert human readable token number to big number instance
|
|
381
|
+
*/
|
|
382
|
+
export const fromTokenToUnit = (input, decimal = 18) => {
|
|
383
|
+
let token = numberToString(input);
|
|
384
|
+
const base = toBN(`1${'0'.repeat(decimal)}`, 10);
|
|
385
|
+
const baseLength = base.toString(10).length - 1 || 1;
|
|
386
|
+
// Is it negative?
|
|
387
|
+
const negative = token.substring(0, 1) === '-';
|
|
388
|
+
if (negative) {
|
|
389
|
+
token = token.substring(1);
|
|
390
|
+
}
|
|
391
|
+
if (token === '.') {
|
|
392
|
+
throw new Error(`error converting token ${input} to unit, invalid value`);
|
|
393
|
+
}
|
|
394
|
+
// Split it into a whole and fractional part
|
|
395
|
+
const comps = token.split('.');
|
|
396
|
+
if (comps.length > 2) {
|
|
397
|
+
throw new Error(`error converting token ${input} to unit, too many decimal points`);
|
|
398
|
+
}
|
|
399
|
+
let whole = comps[0];
|
|
400
|
+
let fraction = comps[1];
|
|
401
|
+
if (!whole) {
|
|
402
|
+
whole = '0';
|
|
403
|
+
}
|
|
404
|
+
if (!fraction) {
|
|
405
|
+
fraction = '0';
|
|
406
|
+
}
|
|
407
|
+
if (fraction.length > baseLength) {
|
|
408
|
+
throw new Error(`error converting token ${input} to unit, too many decimal places`);
|
|
409
|
+
}
|
|
410
|
+
while (fraction.length < baseLength) {
|
|
411
|
+
fraction += '0';
|
|
412
|
+
}
|
|
413
|
+
whole = new BN(whole);
|
|
414
|
+
fraction = new BN(fraction);
|
|
415
|
+
let unit = whole.mul(base).add(fraction);
|
|
416
|
+
if (negative) {
|
|
417
|
+
unit = unit.mul(negative1);
|
|
418
|
+
}
|
|
419
|
+
return new BN(unit.toString(10), 10);
|
|
420
|
+
};
|
|
421
|
+
/**
|
|
422
|
+
* Validates if a value is an Uint8Array.
|
|
423
|
+
*/
|
|
424
|
+
export function isUint8Array(value) {
|
|
425
|
+
return Object.prototype.toString.call(value) === '[object Uint8Array]';
|
|
426
|
+
}
|
|
427
|
+
/**
|
|
428
|
+
* Generate a random UUID
|
|
429
|
+
*/
|
|
430
|
+
export function UUID() {
|
|
431
|
+
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
|
|
432
|
+
const r = (Math.random() * 16) | 0;
|
|
433
|
+
const v = c === 'x' ? r : (r & 0x3) | 0x8;
|
|
434
|
+
return v.toString(16);
|
|
435
|
+
});
|
|
436
|
+
}
|
|
437
|
+
/**
|
|
438
|
+
* Check if a string is valid UUID
|
|
439
|
+
*/
|
|
440
|
+
export function isUUID(str) {
|
|
441
|
+
return /[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/.test(str);
|
|
442
|
+
}
|
|
443
|
+
/**
|
|
444
|
+
* Convert input to Uint8Array on best effort, base64 node supported
|
|
445
|
+
*/
|
|
446
|
+
export function toUint8Array(v) {
|
|
447
|
+
let vb = null;
|
|
448
|
+
if ([null, undefined, ''].includes(v)) {
|
|
449
|
+
vb = new Uint8Array();
|
|
450
|
+
}
|
|
451
|
+
else if (Buffer.isBuffer(v)) {
|
|
452
|
+
vb = new Uint8Array(v);
|
|
453
|
+
}
|
|
454
|
+
else if (isHexStrict(v)) {
|
|
455
|
+
vb = new Uint8Array(hexToBytes(v));
|
|
456
|
+
}
|
|
457
|
+
else if (isUint8Array(v)) {
|
|
458
|
+
vb = new Uint8Array(v);
|
|
459
|
+
}
|
|
460
|
+
else if (isBase58btc(v)) {
|
|
461
|
+
vb = new Uint8Array(base58btc.decode(v));
|
|
462
|
+
}
|
|
463
|
+
else if (typeof v === 'string') {
|
|
464
|
+
vb = new Uint8Array(hexToBytes(toHex(v)));
|
|
465
|
+
}
|
|
466
|
+
else {
|
|
467
|
+
throw new Error(`Unsupported input type ${typeof v} detected for toBuffer, only Uint8Array/Buffer/Hex/Base58 are supported`);
|
|
468
|
+
}
|
|
469
|
+
return vb;
|
|
470
|
+
}
|
|
471
|
+
/**
|
|
472
|
+
* Convert input to Buffer on best effort, base64 not supported
|
|
473
|
+
*/
|
|
474
|
+
export function toBuffer(v) {
|
|
475
|
+
return Buffer.from(toUint8Array(v));
|
|
476
|
+
}
|
|
477
|
+
/**
|
|
478
|
+
* Convert input to base58btc format on best effort
|
|
479
|
+
*/
|
|
480
|
+
export function toBase58(v) {
|
|
481
|
+
const buf = base58btc.encode(toUint8Array(v));
|
|
482
|
+
return Buffer.from(buf).toString('utf-8');
|
|
483
|
+
}
|
|
484
|
+
/**
|
|
485
|
+
* Decode base58 string
|
|
486
|
+
*/
|
|
487
|
+
export function fromBase58(v) {
|
|
488
|
+
if (isBase58btc(v) === false) {
|
|
489
|
+
throw new Error('fromBase58 expect strict base58 encoded string as input');
|
|
490
|
+
}
|
|
491
|
+
return Buffer.from(base58btc.decode(v));
|
|
492
|
+
}
|
|
493
|
+
/**
|
|
494
|
+
* Convert input to base64 format
|
|
495
|
+
*/
|
|
496
|
+
export function toBase64(v, escape = true) {
|
|
497
|
+
const encoded = base64.encode(toBuffer(v));
|
|
498
|
+
return escape ? base64.escape(encoded) : encoded;
|
|
499
|
+
}
|
|
500
|
+
/**
|
|
501
|
+
* Decode base64(base64_url) string to buffer
|
|
502
|
+
*/
|
|
503
|
+
export function fromBase64(v) {
|
|
504
|
+
if (typeof v !== 'string') {
|
|
505
|
+
throw new Error('fromBase64 requires input to be a string');
|
|
506
|
+
}
|
|
507
|
+
return Buffer.from(base64.unescape(v), 'base64');
|
|
508
|
+
}
|
|
509
|
+
/**
|
|
510
|
+
* Convert did to address: remove `did:abt:` prefix
|
|
511
|
+
*/
|
|
512
|
+
export function toAddress(did) {
|
|
513
|
+
return did.replace(DID_PREFIX, '');
|
|
514
|
+
}
|
|
515
|
+
/**
|
|
516
|
+
* Convert address to did: prepend `did:abt:` prefix
|
|
517
|
+
*/
|
|
518
|
+
export function toDid(address) {
|
|
519
|
+
return `${DID_PREFIX}${toAddress(address)}`;
|
|
520
|
+
}
|
|
521
|
+
export function isSameDid(a, b) {
|
|
522
|
+
return toAddress(a).toLowerCase() === toAddress(b).toLowerCase();
|
|
523
|
+
}
|
|
524
|
+
export function formatTxType(type) {
|
|
525
|
+
return upperFirst(camelCase(type));
|
|
526
|
+
}
|
package/esm/md5.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const md5: (x: $TSFixMe) => string;
|
package/esm/md5.js
ADDED
package/esm/ready.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import EventEmitter from 'events';
|
|
2
|
+
export declare class Ready extends EventEmitter {
|
|
3
|
+
emit: $TSFixMe;
|
|
4
|
+
ready: $TSFixMe;
|
|
5
|
+
readyCallbacks: $TSFixMe;
|
|
6
|
+
readyMarks: $TSFixMe;
|
|
7
|
+
constructor();
|
|
8
|
+
markReady(mark?: $TSFixMe): void;
|
|
9
|
+
onReady(cb: $TSFixMe): void;
|
|
10
|
+
}
|
package/esm/ready.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import EventEmitter from 'events';
|
|
2
|
+
export class Ready extends EventEmitter {
|
|
3
|
+
constructor() {
|
|
4
|
+
super();
|
|
5
|
+
this.ready = false;
|
|
6
|
+
this.readyCallbacks = [];
|
|
7
|
+
this.readyMarks = {};
|
|
8
|
+
}
|
|
9
|
+
markReady(mark) {
|
|
10
|
+
if (this.ready) {
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
if (mark === undefined) {
|
|
14
|
+
this.ready = true;
|
|
15
|
+
this.readyCallbacks.forEach((x) => x());
|
|
16
|
+
this.emit('ready');
|
|
17
|
+
}
|
|
18
|
+
else {
|
|
19
|
+
// console.log('Ready.markReady', this.name, mark);
|
|
20
|
+
this.readyMarks[mark] = true;
|
|
21
|
+
this.ready = Object.values(this.readyMarks).every((x) => !!x);
|
|
22
|
+
if (this.ready) {
|
|
23
|
+
this.readyCallbacks.forEach((cb) => cb());
|
|
24
|
+
this.emit('ready');
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
onReady(cb) {
|
|
29
|
+
if (this.ready) {
|
|
30
|
+
cb();
|
|
31
|
+
}
|
|
32
|
+
else {
|
|
33
|
+
this.readyCallbacks.push(cb);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
package/lib/index.d.ts
CHANGED
|
@@ -1,13 +1,11 @@
|
|
|
1
|
-
/// <reference types="node" />
|
|
2
|
-
/// <reference types="jest" />
|
|
3
1
|
import type { LiteralUnion } from 'type-fest';
|
|
4
2
|
import rightPad from 'lodash/padEnd';
|
|
5
3
|
import leftPad from 'lodash/padStart';
|
|
6
4
|
import BN from 'bn.js';
|
|
7
5
|
export declare const isBase58btc: (data: any) => boolean;
|
|
8
|
-
export
|
|
9
|
-
export
|
|
10
|
-
export
|
|
6
|
+
export type BytesType = string | Buffer | Uint8Array;
|
|
7
|
+
export type EncodingType = LiteralUnion<'hex' | 'base16' | 'base58' | 'base64' | 'Uint8Array' | 'buffer', string>;
|
|
8
|
+
export type KeyPairType = {
|
|
11
9
|
publicKey: BytesType;
|
|
12
10
|
secretKey: BytesType;
|
|
13
11
|
};
|
|
@@ -85,7 +83,7 @@ export declare const isHex: (hex: string) => boolean;
|
|
|
85
83
|
* @param {Number|String|BN} num, string, HEX string or BN
|
|
86
84
|
* @returns {BN} BN
|
|
87
85
|
*/
|
|
88
|
-
export declare const toBN: (num: number | string | BN, base?: number |
|
|
86
|
+
export declare const toBN: (num: number | string | BN, base?: number | "hex") => BN;
|
|
89
87
|
/**
|
|
90
88
|
* Should be called to get hex representation (prefixed by 0x) of utf8 string
|
|
91
89
|
*
|
package/lib/index.js
CHANGED
|
@@ -26,7 +26,20 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
26
26
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
27
|
};
|
|
28
28
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
-
exports.
|
|
29
|
+
exports.fromTokenToUnit = exports.fromUnitToToken = exports.numberToString = exports.toHex = exports.hexToBytes = exports.bytesToHex = exports.numberToHex = exports.hexToNumber = exports.hexToUtf8 = exports.utf8ToHex = exports.toBN = exports.isHex = exports.isHexStrict = exports.isBigNumber = exports.isBN = exports.stripHexPrefix = exports.isHexPrefixed = exports.numberToBN = exports.rightPad = exports.leftPad = exports.BN = exports.isBase58btc = void 0;
|
|
30
|
+
exports.isUint8Array = isUint8Array;
|
|
31
|
+
exports.UUID = UUID;
|
|
32
|
+
exports.isUUID = isUUID;
|
|
33
|
+
exports.toUint8Array = toUint8Array;
|
|
34
|
+
exports.toBuffer = toBuffer;
|
|
35
|
+
exports.toBase58 = toBase58;
|
|
36
|
+
exports.fromBase58 = fromBase58;
|
|
37
|
+
exports.toBase64 = toBase64;
|
|
38
|
+
exports.fromBase64 = fromBase64;
|
|
39
|
+
exports.toAddress = toAddress;
|
|
40
|
+
exports.toDid = toDid;
|
|
41
|
+
exports.isSameDid = isSameDid;
|
|
42
|
+
exports.formatTxType = formatTxType;
|
|
30
43
|
const isBoolean_1 = __importDefault(require("lodash/isBoolean"));
|
|
31
44
|
const isString_1 = __importDefault(require("lodash/isString"));
|
|
32
45
|
const isNumber_1 = __importDefault(require("lodash/isNumber"));
|
|
@@ -474,7 +487,6 @@ exports.fromTokenToUnit = fromTokenToUnit;
|
|
|
474
487
|
function isUint8Array(value) {
|
|
475
488
|
return Object.prototype.toString.call(value) === '[object Uint8Array]';
|
|
476
489
|
}
|
|
477
|
-
exports.isUint8Array = isUint8Array;
|
|
478
490
|
/**
|
|
479
491
|
* Generate a random UUID
|
|
480
492
|
*/
|
|
@@ -485,14 +497,12 @@ function UUID() {
|
|
|
485
497
|
return v.toString(16);
|
|
486
498
|
});
|
|
487
499
|
}
|
|
488
|
-
exports.UUID = UUID;
|
|
489
500
|
/**
|
|
490
501
|
* Check if a string is valid UUID
|
|
491
502
|
*/
|
|
492
503
|
function isUUID(str) {
|
|
493
504
|
return /[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/.test(str);
|
|
494
505
|
}
|
|
495
|
-
exports.isUUID = isUUID;
|
|
496
506
|
/**
|
|
497
507
|
* Convert input to Uint8Array on best effort, base64 node supported
|
|
498
508
|
*/
|
|
@@ -521,14 +531,12 @@ function toUint8Array(v) {
|
|
|
521
531
|
}
|
|
522
532
|
return vb;
|
|
523
533
|
}
|
|
524
|
-
exports.toUint8Array = toUint8Array;
|
|
525
534
|
/**
|
|
526
535
|
* Convert input to Buffer on best effort, base64 not supported
|
|
527
536
|
*/
|
|
528
537
|
function toBuffer(v) {
|
|
529
538
|
return Buffer.from(toUint8Array(v));
|
|
530
539
|
}
|
|
531
|
-
exports.toBuffer = toBuffer;
|
|
532
540
|
/**
|
|
533
541
|
* Convert input to base58btc format on best effort
|
|
534
542
|
*/
|
|
@@ -536,7 +544,6 @@ function toBase58(v) {
|
|
|
536
544
|
const buf = base58btc.encode(toUint8Array(v));
|
|
537
545
|
return Buffer.from(buf).toString('utf-8');
|
|
538
546
|
}
|
|
539
|
-
exports.toBase58 = toBase58;
|
|
540
547
|
/**
|
|
541
548
|
* Decode base58 string
|
|
542
549
|
*/
|
|
@@ -546,7 +553,6 @@ function fromBase58(v) {
|
|
|
546
553
|
}
|
|
547
554
|
return Buffer.from(base58btc.decode(v));
|
|
548
555
|
}
|
|
549
|
-
exports.fromBase58 = fromBase58;
|
|
550
556
|
/**
|
|
551
557
|
* Convert input to base64 format
|
|
552
558
|
*/
|
|
@@ -554,7 +560,6 @@ function toBase64(v, escape = true) {
|
|
|
554
560
|
const encoded = base64_url_1.default.encode(toBuffer(v));
|
|
555
561
|
return escape ? base64_url_1.default.escape(encoded) : encoded;
|
|
556
562
|
}
|
|
557
|
-
exports.toBase64 = toBase64;
|
|
558
563
|
/**
|
|
559
564
|
* Decode base64(base64_url) string to buffer
|
|
560
565
|
*/
|
|
@@ -564,26 +569,21 @@ function fromBase64(v) {
|
|
|
564
569
|
}
|
|
565
570
|
return Buffer.from(base64_url_1.default.unescape(v), 'base64');
|
|
566
571
|
}
|
|
567
|
-
exports.fromBase64 = fromBase64;
|
|
568
572
|
/**
|
|
569
573
|
* Convert did to address: remove `did:abt:` prefix
|
|
570
574
|
*/
|
|
571
575
|
function toAddress(did) {
|
|
572
576
|
return did.replace(DID_PREFIX, '');
|
|
573
577
|
}
|
|
574
|
-
exports.toAddress = toAddress;
|
|
575
578
|
/**
|
|
576
579
|
* Convert address to did: prepend `did:abt:` prefix
|
|
577
580
|
*/
|
|
578
581
|
function toDid(address) {
|
|
579
582
|
return `${DID_PREFIX}${toAddress(address)}`;
|
|
580
583
|
}
|
|
581
|
-
exports.toDid = toDid;
|
|
582
584
|
function isSameDid(a, b) {
|
|
583
585
|
return toAddress(a).toLowerCase() === toAddress(b).toLowerCase();
|
|
584
586
|
}
|
|
585
|
-
exports.isSameDid = isSameDid;
|
|
586
587
|
function formatTxType(type) {
|
|
587
588
|
return (0, upperFirst_1.default)((0, camelCase_1.default)(type));
|
|
588
589
|
}
|
|
589
|
-
exports.formatTxType = formatTxType;
|
package/lib/md5.js
CHANGED
|
@@ -5,5 +5,6 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.md5 = void 0;
|
|
7
7
|
const crypto_1 = __importDefault(require("crypto"));
|
|
8
|
+
// eslint-disable-next-line import/prefer-default-export
|
|
8
9
|
const md5 = (x) => crypto_1.default.createHash('md5').update(x).digest('hex');
|
|
9
10
|
exports.md5 = md5;
|
package/lib/ready.d.ts
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ocap/util",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.19.1",
|
|
4
4
|
"description": "utils shared across multiple forge js libs, works in both node.js and browser",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"arcblock",
|
|
@@ -22,17 +22,17 @@
|
|
|
22
22
|
"elliptic": "6.5.3"
|
|
23
23
|
},
|
|
24
24
|
"devDependencies": {
|
|
25
|
-
"@arcblock/eslint-config-ts": "0.
|
|
26
|
-
"@types/base64-url": "^2.2.
|
|
27
|
-
"@types/jest": "^29.5.
|
|
28
|
-
"@types/lodash": "^4.17.
|
|
29
|
-
"@types/node": "^
|
|
30
|
-
"@types/utf8": "^3.0.
|
|
31
|
-
"eslint": "^8.
|
|
25
|
+
"@arcblock/eslint-config-ts": "0.3.3",
|
|
26
|
+
"@types/base64-url": "^2.2.2",
|
|
27
|
+
"@types/jest": "^29.5.13",
|
|
28
|
+
"@types/lodash": "^4.17.10",
|
|
29
|
+
"@types/node": "^22.7.5",
|
|
30
|
+
"@types/utf8": "^3.0.3",
|
|
31
|
+
"eslint": "^8.57.0",
|
|
32
32
|
"jest": "^29.7.0",
|
|
33
33
|
"ts-jest": "^29.2.5",
|
|
34
34
|
"type-fest": "^3.1.0",
|
|
35
|
-
"typescript": "^
|
|
35
|
+
"typescript": "^5.6.2"
|
|
36
36
|
},
|
|
37
37
|
"author": {
|
|
38
38
|
"name": "wangshijun",
|
|
@@ -44,10 +44,22 @@
|
|
|
44
44
|
],
|
|
45
45
|
"homepage": "https://github.com/ArcBlock/blockchain/tree/master/core/forge-util",
|
|
46
46
|
"license": "Apache-2.0",
|
|
47
|
-
"main": "lib/index.js",
|
|
48
|
-
"
|
|
47
|
+
"main": "./lib/index.js",
|
|
48
|
+
"module": "./lib/index.js",
|
|
49
|
+
"types": "./esm/index.d.ts",
|
|
50
|
+
"exports": {
|
|
51
|
+
".": {
|
|
52
|
+
"import": "./esm/index.js",
|
|
53
|
+
"require": "./lib/index.js",
|
|
54
|
+
"default": "./esm/index.js"
|
|
55
|
+
},
|
|
56
|
+
"./lib/*": {
|
|
57
|
+
"require": "./lib/*.js"
|
|
58
|
+
}
|
|
59
|
+
},
|
|
49
60
|
"files": [
|
|
50
|
-
"lib"
|
|
61
|
+
"lib",
|
|
62
|
+
"esm"
|
|
51
63
|
],
|
|
52
64
|
"repository": {
|
|
53
65
|
"type": "git",
|
|
@@ -58,13 +70,15 @@
|
|
|
58
70
|
"lint:fix": "npm run lint -- --fix",
|
|
59
71
|
"test": "jest --forceExit --detectOpenHandles",
|
|
60
72
|
"coverage": "npm run test -- --coverage",
|
|
61
|
-
"clean": "rm -fr lib",
|
|
73
|
+
"clean": "rm -fr lib esm",
|
|
62
74
|
"prebuild": "npm run clean",
|
|
63
|
-
"build": "tsc",
|
|
75
|
+
"build:cjs": "tsc -p tsconfig.cjs.json",
|
|
76
|
+
"build:esm": "tsc -p tsconfig.esm.json",
|
|
77
|
+
"build": "npm run build:cjs && npm run build:esm",
|
|
64
78
|
"build:watch": "npm run build -- -w"
|
|
65
79
|
},
|
|
66
80
|
"bugs": {
|
|
67
81
|
"url": "https://github.com/ArcBlock/blockchain/issues"
|
|
68
82
|
},
|
|
69
|
-
"gitHead": "
|
|
83
|
+
"gitHead": "21184488172c6c824ebd1714f728ff2aee4a3ac0"
|
|
70
84
|
}
|