ccxt 4.2.60 → 4.2.61
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/README.md +103 -102
- package/build.sh +1 -1
- package/dist/ccxt.browser.js +107943 -100121
- package/dist/ccxt.browser.min.js +7 -7
- package/dist/cjs/ccxt.js +4 -1
- package/dist/cjs/src/abstract/hyperliquid.js +9 -0
- package/dist/cjs/src/base/Exchange.js +22 -1
- package/dist/cjs/src/base/functions/encode.js +5 -0
- package/dist/cjs/src/base/functions.js +1 -0
- package/dist/cjs/src/bitfinex2.js +1 -1
- package/dist/cjs/src/coinbase.js +108 -103
- package/dist/cjs/src/coinex.js +61 -1
- package/dist/cjs/src/hyperliquid.js +2035 -0
- package/dist/cjs/src/kraken.js +8 -2
- package/dist/cjs/src/krakenfutures.js +28 -0
- package/dist/cjs/src/kucoinfutures.js +2 -2
- package/dist/cjs/src/okx.js +1 -1
- package/dist/cjs/src/phemex.js +2 -2
- package/dist/cjs/src/pro/bingx.js +1 -0
- package/dist/cjs/src/static_dependencies/ethers/abi-coder.js +158 -0
- package/dist/cjs/src/static_dependencies/ethers/address/address.js +144 -0
- package/dist/cjs/src/static_dependencies/ethers/coders/abstract-coder.js +407 -0
- package/dist/cjs/src/static_dependencies/ethers/coders/address.js +45 -0
- package/dist/cjs/src/static_dependencies/ethers/coders/anonymous.js +28 -0
- package/dist/cjs/src/static_dependencies/ethers/coders/array.js +176 -0
- package/dist/cjs/src/static_dependencies/ethers/coders/boolean.js +27 -0
- package/dist/cjs/src/static_dependencies/ethers/coders/bytes.js +52 -0
- package/dist/cjs/src/static_dependencies/ethers/coders/fixed-bytes.js +45 -0
- package/dist/cjs/src/static_dependencies/ethers/coders/null.js +30 -0
- package/dist/cjs/src/static_dependencies/ethers/coders/number.js +60 -0
- package/dist/cjs/src/static_dependencies/ethers/coders/string.js +27 -0
- package/dist/cjs/src/static_dependencies/ethers/coders/tuple.js +68 -0
- package/dist/cjs/src/static_dependencies/ethers/fragments.js +614 -0
- package/dist/cjs/src/static_dependencies/ethers/hash/typed-data.js +492 -0
- package/dist/cjs/src/static_dependencies/ethers/index.js +35 -0
- package/dist/cjs/src/static_dependencies/ethers/interface.js +44 -0
- package/dist/cjs/src/static_dependencies/ethers/typed.js +618 -0
- package/dist/cjs/src/static_dependencies/ethers/utils/base58.js +20 -0
- package/dist/cjs/src/static_dependencies/ethers/utils/data.js +134 -0
- package/dist/cjs/src/static_dependencies/ethers/utils/errors.js +228 -0
- package/dist/cjs/src/static_dependencies/ethers/utils/events.js +13 -0
- package/dist/cjs/src/static_dependencies/ethers/utils/fixednumber.js +29 -0
- package/dist/cjs/src/static_dependencies/ethers/utils/index.js +53 -0
- package/dist/cjs/src/static_dependencies/ethers/utils/maths.js +231 -0
- package/dist/cjs/src/static_dependencies/ethers/utils/properties.js +47 -0
- package/dist/cjs/src/static_dependencies/ethers/utils/utf8.js +218 -0
- package/dist/cjs/src/static_dependencies/messagepack/msgpack.js +292 -0
- package/dist/cjs/src/tokocrypto.js +22 -2
- package/dist/cjs/src/wazirx.js +308 -3
- package/js/ccxt.d.ts +5 -2
- package/js/ccxt.js +4 -2
- package/js/src/abstract/coinbase.d.ts +3 -3
- package/js/src/abstract/hyperliquid.d.ts +9 -0
- package/js/src/abstract/hyperliquid.js +11 -0
- package/js/src/base/Exchange.d.ts +4 -0
- package/js/src/base/Exchange.js +13 -1
- package/js/src/base/functions/encode.d.ts +2 -1
- package/js/src/base/functions/encode.js +5 -1
- package/js/src/bitfinex2.js +1 -1
- package/js/src/coinbase.js +108 -103
- package/js/src/coinex.d.ts +3 -1
- package/js/src/coinex.js +61 -1
- package/js/src/hyperliquid.d.ts +83 -0
- package/js/src/hyperliquid.js +2036 -0
- package/js/src/kraken.js +8 -2
- package/js/src/krakenfutures.d.ts +2 -1
- package/js/src/krakenfutures.js +28 -0
- package/js/src/kucoinfutures.js +2 -2
- package/js/src/okx.js +1 -1
- package/js/src/phemex.js +2 -2
- package/js/src/pro/bingx.js +1 -0
- package/js/src/static_dependencies/ethers/abi-coder.d.ts +50 -0
- package/js/src/static_dependencies/ethers/abi-coder.js +148 -0
- package/js/src/static_dependencies/ethers/address/address.d.ts +55 -0
- package/js/src/static_dependencies/ethers/address/address.js +162 -0
- package/js/src/static_dependencies/ethers/address/checks.d.ts +80 -0
- package/js/src/static_dependencies/ethers/address/checks.js +119 -0
- package/js/src/static_dependencies/ethers/address/contract-address.d.ts +47 -0
- package/js/src/static_dependencies/ethers/address/contract-address.js +73 -0
- package/js/src/static_dependencies/ethers/address/index.d.ts +48 -0
- package/js/src/static_dependencies/ethers/address/index.js +24 -0
- package/js/src/static_dependencies/ethers/bytes32.d.ts +14 -0
- package/js/src/static_dependencies/ethers/bytes32.js +45 -0
- package/js/src/static_dependencies/ethers/coders/abstract-coder.d.ts +120 -0
- package/js/src/static_dependencies/ethers/coders/abstract-coder.js +424 -0
- package/js/src/static_dependencies/ethers/coders/address.d.ts +12 -0
- package/js/src/static_dependencies/ethers/coders/address.js +34 -0
- package/js/src/static_dependencies/ethers/coders/anonymous.d.ts +14 -0
- package/js/src/static_dependencies/ethers/coders/anonymous.js +27 -0
- package/js/src/static_dependencies/ethers/coders/array.d.ts +24 -0
- package/js/src/static_dependencies/ethers/coders/array.js +162 -0
- package/js/src/static_dependencies/ethers/coders/boolean.d.ts +12 -0
- package/js/src/static_dependencies/ethers/coders/boolean.js +26 -0
- package/js/src/static_dependencies/ethers/coders/bytes.d.ts +18 -0
- package/js/src/static_dependencies/ethers/coders/bytes.js +39 -0
- package/js/src/static_dependencies/ethers/coders/fixed-bytes.d.ts +14 -0
- package/js/src/static_dependencies/ethers/coders/fixed-bytes.js +32 -0
- package/js/src/static_dependencies/ethers/coders/null.d.ts +11 -0
- package/js/src/static_dependencies/ethers/coders/null.js +29 -0
- package/js/src/static_dependencies/ethers/coders/number.d.ts +15 -0
- package/js/src/static_dependencies/ethers/coders/number.js +48 -0
- package/js/src/static_dependencies/ethers/coders/string.d.ts +12 -0
- package/js/src/static_dependencies/ethers/coders/string.js +26 -0
- package/js/src/static_dependencies/ethers/coders/tuple.d.ts +15 -0
- package/js/src/static_dependencies/ethers/coders/tuple.js +67 -0
- package/js/src/static_dependencies/ethers/fragments.d.ts +458 -0
- package/js/src/static_dependencies/ethers/fragments.js +1252 -0
- package/js/src/static_dependencies/ethers/hash/index.d.ts +10 -0
- package/js/src/static_dependencies/ethers/hash/index.js +15 -0
- package/js/src/static_dependencies/ethers/hash/solidity.d.ts +30 -0
- package/js/src/static_dependencies/ethers/hash/solidity.js +107 -0
- package/js/src/static_dependencies/ethers/hash/typed-data.d.ts +144 -0
- package/js/src/static_dependencies/ethers/hash/typed-data.js +490 -0
- package/js/src/static_dependencies/ethers/index.d.ts +19 -0
- package/js/src/static_dependencies/ethers/index.js +22 -0
- package/js/src/static_dependencies/ethers/interface.d.ts +380 -0
- package/js/src/static_dependencies/ethers/interface.js +990 -0
- package/js/src/static_dependencies/ethers/typed.d.ts +569 -0
- package/js/src/static_dependencies/ethers/typed.js +608 -0
- package/js/src/static_dependencies/ethers/utils/base58.d.ts +22 -0
- package/js/src/static_dependencies/ethers/utils/base58.js +68 -0
- package/js/src/static_dependencies/ethers/utils/base64-browser.d.ts +3 -0
- package/js/src/static_dependencies/ethers/utils/base64-browser.js +24 -0
- package/js/src/static_dependencies/ethers/utils/base64.d.ts +39 -0
- package/js/src/static_dependencies/ethers/utils/base64.js +58 -0
- package/js/src/static_dependencies/ethers/utils/data.d.ts +92 -0
- package/js/src/static_dependencies/ethers/utils/data.js +175 -0
- package/js/src/static_dependencies/ethers/utils/errors.d.ts +509 -0
- package/js/src/static_dependencies/ethers/utils/errors.js +227 -0
- package/js/src/static_dependencies/ethers/utils/events.d.ts +76 -0
- package/js/src/static_dependencies/ethers/utils/events.js +52 -0
- package/js/src/static_dependencies/ethers/utils/fixednumber.d.ts +251 -0
- package/js/src/static_dependencies/ethers/utils/fixednumber.js +529 -0
- package/js/src/static_dependencies/ethers/utils/index.d.ts +30 -0
- package/js/src/static_dependencies/ethers/utils/index.js +38 -0
- package/js/src/static_dependencies/ethers/utils/maths.d.ts +65 -0
- package/js/src/static_dependencies/ethers/utils/maths.js +220 -0
- package/js/src/static_dependencies/ethers/utils/properties.d.ts +22 -0
- package/js/src/static_dependencies/ethers/utils/properties.js +59 -0
- package/js/src/static_dependencies/ethers/utils/rlp-decode.d.ts +5 -0
- package/js/src/static_dependencies/ethers/utils/rlp-decode.js +84 -0
- package/js/src/static_dependencies/ethers/utils/rlp-encode.d.ts +5 -0
- package/js/src/static_dependencies/ethers/utils/rlp-encode.js +54 -0
- package/js/src/static_dependencies/ethers/utils/rlp.d.ts +16 -0
- package/js/src/static_dependencies/ethers/utils/rlp.js +14 -0
- package/js/src/static_dependencies/ethers/utils/units.d.ts +23 -0
- package/js/src/static_dependencies/ethers/utils/units.js +88 -0
- package/js/src/static_dependencies/ethers/utils/utf8.d.ts +95 -0
- package/js/src/static_dependencies/ethers/utils/utf8.js +225 -0
- package/js/src/static_dependencies/ethers/utils/uuid.d.ts +7 -0
- package/js/src/static_dependencies/ethers/utils/uuid.js +35 -0
- package/js/src/static_dependencies/messagepack/msgpack.d.ts +2 -0
- package/js/src/static_dependencies/messagepack/msgpack.js +572 -0
- package/js/src/tokocrypto.js +22 -2
- package/js/src/wazirx.d.ts +12 -1
- package/js/src/wazirx.js +308 -3
- package/package.json +1 -1
- package/skip-tests.json +18 -0
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
// ----------------------------------------------------------------------------
|
|
2
|
+
|
|
3
|
+
// PLEASE DO NOT EDIT THIS FILE, IT IS GENERATED AND WILL BE OVERWRITTEN:
|
|
4
|
+
// https://github.com/ccxt/ccxt/blob/master/CONTRIBUTING.md#how-to-contribute-code
|
|
5
|
+
// EDIT THE CORRESPONDENT .ts FILE INSTEAD
|
|
6
|
+
|
|
7
|
+
// utils/base64-browser
|
|
8
|
+
import { getBytes } from "./data.js";
|
|
9
|
+
export function decodeBase64(textData) {
|
|
10
|
+
textData = atob(textData);
|
|
11
|
+
const data = new Uint8Array(textData.length);
|
|
12
|
+
for (let i = 0; i < textData.length; i++) {
|
|
13
|
+
data[i] = textData.charCodeAt(i);
|
|
14
|
+
}
|
|
15
|
+
return getBytes(data);
|
|
16
|
+
}
|
|
17
|
+
export function encodeBase64(_data) {
|
|
18
|
+
const data = getBytes(_data);
|
|
19
|
+
let textData = "";
|
|
20
|
+
for (let i = 0; i < data.length; i++) {
|
|
21
|
+
textData += String.fromCharCode(data[i]);
|
|
22
|
+
}
|
|
23
|
+
return btoa(textData);
|
|
24
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type { BytesLike } from "./data.js";
|
|
2
|
+
/**
|
|
3
|
+
* Decodes the base-64 encoded %%value%%.
|
|
4
|
+
*
|
|
5
|
+
* @example:
|
|
6
|
+
* // The decoded value is always binary data...
|
|
7
|
+
* result = decodeBase64("SGVsbG8gV29ybGQhIQ==")
|
|
8
|
+
* //_result:
|
|
9
|
+
*
|
|
10
|
+
* // ...use toUtf8String to convert it to a string.
|
|
11
|
+
* toUtf8String(result)
|
|
12
|
+
* //_result:
|
|
13
|
+
*
|
|
14
|
+
* // Decoding binary data
|
|
15
|
+
* decodeBase64("EjQ=")
|
|
16
|
+
* //_result:
|
|
17
|
+
*/
|
|
18
|
+
export declare function decodeBase64(value: string): Uint8Array;
|
|
19
|
+
/**
|
|
20
|
+
* Encodes %%data%% as a base-64 encoded string.
|
|
21
|
+
*
|
|
22
|
+
* @example:
|
|
23
|
+
* // Encoding binary data as a hexstring
|
|
24
|
+
* encodeBase64("0x1234")
|
|
25
|
+
* //_result:
|
|
26
|
+
*
|
|
27
|
+
* // Encoding binary data as a Uint8Array
|
|
28
|
+
* encodeBase64(new Uint8Array([ 0x12, 0x34 ]))
|
|
29
|
+
* //_result:
|
|
30
|
+
*
|
|
31
|
+
* // The input MUST be data...
|
|
32
|
+
* encodeBase64("Hello World!!")
|
|
33
|
+
* //_error:
|
|
34
|
+
*
|
|
35
|
+
* // ...use toUtf8Bytes for this.
|
|
36
|
+
* encodeBase64(toUtf8Bytes("Hello World!!"))
|
|
37
|
+
* //_result:
|
|
38
|
+
*/
|
|
39
|
+
export declare function encodeBase64(data: BytesLike): string;
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
// ----------------------------------------------------------------------------
|
|
2
|
+
|
|
3
|
+
// PLEASE DO NOT EDIT THIS FILE, IT IS GENERATED AND WILL BE OVERWRITTEN:
|
|
4
|
+
// https://github.com/ccxt/ccxt/blob/master/CONTRIBUTING.md#how-to-contribute-code
|
|
5
|
+
// EDIT THE CORRESPONDENT .ts FILE INSTEAD
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* [Base64 encoding](link-wiki-base64) using 6-bit words to encode
|
|
9
|
+
* arbitrary bytes into a string using 65 printable symbols, the
|
|
10
|
+
* upper-case and lower-case alphabet, the digits ``0`` through ``9``,
|
|
11
|
+
* ``"+"`` and ``"/"`` with the ``"="`` used for padding.
|
|
12
|
+
*
|
|
13
|
+
* @_subsection: api/utils:Base64 Encoding [about-base64]
|
|
14
|
+
*/
|
|
15
|
+
import { getBytes, getBytesCopy } from "./data.js";
|
|
16
|
+
/**
|
|
17
|
+
* Decodes the base-64 encoded %%value%%.
|
|
18
|
+
*
|
|
19
|
+
* @example:
|
|
20
|
+
* // The decoded value is always binary data...
|
|
21
|
+
* result = decodeBase64("SGVsbG8gV29ybGQhIQ==")
|
|
22
|
+
* //_result:
|
|
23
|
+
*
|
|
24
|
+
* // ...use toUtf8String to convert it to a string.
|
|
25
|
+
* toUtf8String(result)
|
|
26
|
+
* //_result:
|
|
27
|
+
*
|
|
28
|
+
* // Decoding binary data
|
|
29
|
+
* decodeBase64("EjQ=")
|
|
30
|
+
* //_result:
|
|
31
|
+
*/
|
|
32
|
+
export function decodeBase64(value) {
|
|
33
|
+
return getBytesCopy(Buffer.from(value, "base64"));
|
|
34
|
+
}
|
|
35
|
+
;
|
|
36
|
+
/**
|
|
37
|
+
* Encodes %%data%% as a base-64 encoded string.
|
|
38
|
+
*
|
|
39
|
+
* @example:
|
|
40
|
+
* // Encoding binary data as a hexstring
|
|
41
|
+
* encodeBase64("0x1234")
|
|
42
|
+
* //_result:
|
|
43
|
+
*
|
|
44
|
+
* // Encoding binary data as a Uint8Array
|
|
45
|
+
* encodeBase64(new Uint8Array([ 0x12, 0x34 ]))
|
|
46
|
+
* //_result:
|
|
47
|
+
*
|
|
48
|
+
* // The input MUST be data...
|
|
49
|
+
* encodeBase64("Hello World!!")
|
|
50
|
+
* //_error:
|
|
51
|
+
*
|
|
52
|
+
* // ...use toUtf8Bytes for this.
|
|
53
|
+
* encodeBase64(toUtf8Bytes("Hello World!!"))
|
|
54
|
+
* //_result:
|
|
55
|
+
*/
|
|
56
|
+
export function encodeBase64(data) {
|
|
57
|
+
return Buffer.from(getBytes(data)).toString("base64");
|
|
58
|
+
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A [[HexString]] whose length is even, which ensures it is a valid
|
|
3
|
+
* representation of binary data.
|
|
4
|
+
*/
|
|
5
|
+
export declare type DataHexString = string;
|
|
6
|
+
/**
|
|
7
|
+
* A string which is prefixed with ``0x`` and followed by any number
|
|
8
|
+
* of case-agnostic hexadecimal characters.
|
|
9
|
+
*
|
|
10
|
+
* It must match the regular expression ``/0x[0-9A-Fa-f]*\/``.
|
|
11
|
+
*/
|
|
12
|
+
export declare type HexString = string;
|
|
13
|
+
/**
|
|
14
|
+
* An object that can be used to represent binary data.
|
|
15
|
+
*/
|
|
16
|
+
export declare type BytesLike = DataHexString | Uint8Array;
|
|
17
|
+
/**
|
|
18
|
+
* Get a typed Uint8Array for %%value%%. If already a Uint8Array
|
|
19
|
+
* the original %%value%% is returned; if a copy is required use
|
|
20
|
+
* [[getBytesCopy]].
|
|
21
|
+
*
|
|
22
|
+
* @see: getBytesCopy
|
|
23
|
+
*/
|
|
24
|
+
export declare function getBytes(value: BytesLike, name?: string): Uint8Array;
|
|
25
|
+
/**
|
|
26
|
+
* Get a typed Uint8Array for %%value%%, creating a copy if necessary
|
|
27
|
+
* to prevent any modifications of the returned value from being
|
|
28
|
+
* reflected elsewhere.
|
|
29
|
+
*
|
|
30
|
+
* @see: getBytes
|
|
31
|
+
*/
|
|
32
|
+
export declare function getBytesCopy(value: BytesLike, name?: string): Uint8Array;
|
|
33
|
+
/**
|
|
34
|
+
* Returns true if %%value%% is a valid [[HexString]].
|
|
35
|
+
*
|
|
36
|
+
* If %%length%% is ``true`` or a //number//, it also checks that
|
|
37
|
+
* %%value%% is a valid [[DataHexString]] of %%length%% (if a //number//)
|
|
38
|
+
* bytes of data (e.g. ``0x1234`` is 2 bytes).
|
|
39
|
+
*/
|
|
40
|
+
export declare function isHexString(value: any, length?: number | boolean): value is `0x${string}`;
|
|
41
|
+
/**
|
|
42
|
+
* Returns true if %%value%% is a valid representation of arbitrary
|
|
43
|
+
* data (i.e. a valid [[DataHexString]] or a Uint8Array).
|
|
44
|
+
*/
|
|
45
|
+
export declare function isBytesLike(value: any): value is BytesLike;
|
|
46
|
+
/**
|
|
47
|
+
* Returns a [[DataHexString]] representation of %%data%%.
|
|
48
|
+
*/
|
|
49
|
+
export declare function hexlify(data: BytesLike): string;
|
|
50
|
+
/**
|
|
51
|
+
* Returns a [[DataHexString]] by concatenating all values
|
|
52
|
+
* within %%data%%.
|
|
53
|
+
*/
|
|
54
|
+
export declare function concat(datas: ReadonlyArray<BytesLike>): string;
|
|
55
|
+
/**
|
|
56
|
+
* Returns the length of %%data%%, in bytes.
|
|
57
|
+
*/
|
|
58
|
+
export declare function dataLength(data: BytesLike): number;
|
|
59
|
+
/**
|
|
60
|
+
* Returns a [[DataHexString]] by slicing %%data%% from the %%start%%
|
|
61
|
+
* offset to the %%end%% offset.
|
|
62
|
+
*
|
|
63
|
+
* By default %%start%% is 0 and %%end%% is the length of %%data%%.
|
|
64
|
+
*/
|
|
65
|
+
export declare function dataSlice(data: BytesLike, start?: number, end?: number): string;
|
|
66
|
+
/**
|
|
67
|
+
* Return the [[DataHexString]] result by stripping all **leading**
|
|
68
|
+
** zero bytes from %%data%%.
|
|
69
|
+
*/
|
|
70
|
+
export declare function stripZerosLeft(data: BytesLike): string;
|
|
71
|
+
/**
|
|
72
|
+
* Return the [[DataHexString]] of %%data%% padded on the **left**
|
|
73
|
+
* to %%length%% bytes.
|
|
74
|
+
*
|
|
75
|
+
* If %%data%% already exceeds %%length%%, a [[BufferOverrunError]] is
|
|
76
|
+
* thrown.
|
|
77
|
+
*
|
|
78
|
+
* This pads data the same as **values** are in Solidity
|
|
79
|
+
* (e.g. ``uint128``).
|
|
80
|
+
*/
|
|
81
|
+
export declare function zeroPadValue(data: BytesLike, length: number): string;
|
|
82
|
+
/**
|
|
83
|
+
* Return the [[DataHexString]] of %%data%% padded on the **right**
|
|
84
|
+
* to %%length%% bytes.
|
|
85
|
+
*
|
|
86
|
+
* If %%data%% already exceeds %%length%%, a [[BufferOverrunError]] is
|
|
87
|
+
* thrown.
|
|
88
|
+
*
|
|
89
|
+
* This pads data the same as **bytes** are in Solidity
|
|
90
|
+
* (e.g. ``bytes16``).
|
|
91
|
+
*/
|
|
92
|
+
export declare function zeroPadBytes(data: BytesLike, length: number): string;
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
// ----------------------------------------------------------------------------
|
|
2
|
+
|
|
3
|
+
// PLEASE DO NOT EDIT THIS FILE, IT IS GENERATED AND WILL BE OVERWRITTEN:
|
|
4
|
+
// https://github.com/ccxt/ccxt/blob/master/CONTRIBUTING.md#how-to-contribute-code
|
|
5
|
+
// EDIT THE CORRESPONDENT .ts FILE INSTEAD
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Some data helpers.
|
|
9
|
+
*
|
|
10
|
+
*
|
|
11
|
+
* @_subsection api/utils:Data Helpers [about-data]
|
|
12
|
+
*/
|
|
13
|
+
import { assert, assertArgument } from "./errors.js";
|
|
14
|
+
function _getBytes(value, name, copy) {
|
|
15
|
+
if (value instanceof Uint8Array) {
|
|
16
|
+
if (copy) {
|
|
17
|
+
return new Uint8Array(value);
|
|
18
|
+
}
|
|
19
|
+
return value;
|
|
20
|
+
}
|
|
21
|
+
if (typeof (value) === "string" && value.match(/^0x([0-9a-f][0-9a-f])*$/i)) {
|
|
22
|
+
const result = new Uint8Array((value.length - 2) / 2);
|
|
23
|
+
let offset = 2;
|
|
24
|
+
for (let i = 0; i < result.length; i++) {
|
|
25
|
+
result[i] = parseInt(value.substring(offset, offset + 2), 16);
|
|
26
|
+
offset += 2;
|
|
27
|
+
}
|
|
28
|
+
return result;
|
|
29
|
+
}
|
|
30
|
+
assertArgument(false, "invalid BytesLike value", name || "value", value);
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Get a typed Uint8Array for %%value%%. If already a Uint8Array
|
|
34
|
+
* the original %%value%% is returned; if a copy is required use
|
|
35
|
+
* [[getBytesCopy]].
|
|
36
|
+
*
|
|
37
|
+
* @see: getBytesCopy
|
|
38
|
+
*/
|
|
39
|
+
export function getBytes(value, name) {
|
|
40
|
+
return _getBytes(value, name, false);
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Get a typed Uint8Array for %%value%%, creating a copy if necessary
|
|
44
|
+
* to prevent any modifications of the returned value from being
|
|
45
|
+
* reflected elsewhere.
|
|
46
|
+
*
|
|
47
|
+
* @see: getBytes
|
|
48
|
+
*/
|
|
49
|
+
export function getBytesCopy(value, name) {
|
|
50
|
+
return _getBytes(value, name, true);
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Returns true if %%value%% is a valid [[HexString]].
|
|
54
|
+
*
|
|
55
|
+
* If %%length%% is ``true`` or a //number//, it also checks that
|
|
56
|
+
* %%value%% is a valid [[DataHexString]] of %%length%% (if a //number//)
|
|
57
|
+
* bytes of data (e.g. ``0x1234`` is 2 bytes).
|
|
58
|
+
*/
|
|
59
|
+
export function isHexString(value, length) {
|
|
60
|
+
if (typeof (value) !== "string" || !value.match(/^0x[0-9A-Fa-f]*$/)) {
|
|
61
|
+
return false;
|
|
62
|
+
}
|
|
63
|
+
if (typeof (length) === "number" && value.length !== 2 + 2 * length) {
|
|
64
|
+
return false;
|
|
65
|
+
}
|
|
66
|
+
if (length === true && (value.length % 2) !== 0) {
|
|
67
|
+
return false;
|
|
68
|
+
}
|
|
69
|
+
return true;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Returns true if %%value%% is a valid representation of arbitrary
|
|
73
|
+
* data (i.e. a valid [[DataHexString]] or a Uint8Array).
|
|
74
|
+
*/
|
|
75
|
+
export function isBytesLike(value) {
|
|
76
|
+
return (isHexString(value, true) || (value instanceof Uint8Array));
|
|
77
|
+
}
|
|
78
|
+
const HexCharacters = "0123456789abcdef";
|
|
79
|
+
/**
|
|
80
|
+
* Returns a [[DataHexString]] representation of %%data%%.
|
|
81
|
+
*/
|
|
82
|
+
export function hexlify(data) {
|
|
83
|
+
const bytes = getBytes(data);
|
|
84
|
+
let result = "0x";
|
|
85
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
86
|
+
const v = bytes[i];
|
|
87
|
+
result += HexCharacters[(v & 0xf0) >> 4] + HexCharacters[v & 0x0f];
|
|
88
|
+
}
|
|
89
|
+
return result;
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Returns a [[DataHexString]] by concatenating all values
|
|
93
|
+
* within %%data%%.
|
|
94
|
+
*/
|
|
95
|
+
export function concat(datas) {
|
|
96
|
+
return "0x" + datas.map((d) => hexlify(d).substring(2)).join("");
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Returns the length of %%data%%, in bytes.
|
|
100
|
+
*/
|
|
101
|
+
export function dataLength(data) {
|
|
102
|
+
if (isHexString(data, true)) {
|
|
103
|
+
return (data.length - 2) / 2;
|
|
104
|
+
}
|
|
105
|
+
return getBytes(data).length;
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Returns a [[DataHexString]] by slicing %%data%% from the %%start%%
|
|
109
|
+
* offset to the %%end%% offset.
|
|
110
|
+
*
|
|
111
|
+
* By default %%start%% is 0 and %%end%% is the length of %%data%%.
|
|
112
|
+
*/
|
|
113
|
+
export function dataSlice(data, start, end) {
|
|
114
|
+
const bytes = getBytes(data);
|
|
115
|
+
if (end != null && end > bytes.length) {
|
|
116
|
+
assert(false, "cannot slice beyond data bounds", "BUFFER_OVERRUN", {
|
|
117
|
+
buffer: bytes, length: bytes.length, offset: end
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
return hexlify(bytes.slice((start == null) ? 0 : start, (end == null) ? bytes.length : end));
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Return the [[DataHexString]] result by stripping all **leading**
|
|
124
|
+
** zero bytes from %%data%%.
|
|
125
|
+
*/
|
|
126
|
+
export function stripZerosLeft(data) {
|
|
127
|
+
let bytes = hexlify(data).substring(2);
|
|
128
|
+
while (bytes.startsWith("00")) {
|
|
129
|
+
bytes = bytes.substring(2);
|
|
130
|
+
}
|
|
131
|
+
return "0x" + bytes;
|
|
132
|
+
}
|
|
133
|
+
function zeroPad(data, length, left) {
|
|
134
|
+
const bytes = getBytes(data);
|
|
135
|
+
assert(length >= bytes.length, "padding exceeds data length", "BUFFER_OVERRUN", {
|
|
136
|
+
buffer: new Uint8Array(bytes),
|
|
137
|
+
length: length,
|
|
138
|
+
offset: length + 1
|
|
139
|
+
});
|
|
140
|
+
const result = new Uint8Array(length);
|
|
141
|
+
result.fill(0);
|
|
142
|
+
if (left) {
|
|
143
|
+
result.set(bytes, length - bytes.length);
|
|
144
|
+
}
|
|
145
|
+
else {
|
|
146
|
+
result.set(bytes, 0);
|
|
147
|
+
}
|
|
148
|
+
return hexlify(result);
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Return the [[DataHexString]] of %%data%% padded on the **left**
|
|
152
|
+
* to %%length%% bytes.
|
|
153
|
+
*
|
|
154
|
+
* If %%data%% already exceeds %%length%%, a [[BufferOverrunError]] is
|
|
155
|
+
* thrown.
|
|
156
|
+
*
|
|
157
|
+
* This pads data the same as **values** are in Solidity
|
|
158
|
+
* (e.g. ``uint128``).
|
|
159
|
+
*/
|
|
160
|
+
export function zeroPadValue(data, length) {
|
|
161
|
+
return zeroPad(data, length, true);
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Return the [[DataHexString]] of %%data%% padded on the **right**
|
|
165
|
+
* to %%length%% bytes.
|
|
166
|
+
*
|
|
167
|
+
* If %%data%% already exceeds %%length%%, a [[BufferOverrunError]] is
|
|
168
|
+
* thrown.
|
|
169
|
+
*
|
|
170
|
+
* This pads data the same as **bytes** are in Solidity
|
|
171
|
+
* (e.g. ``bytes16``).
|
|
172
|
+
*/
|
|
173
|
+
export function zeroPadBytes(data, length) {
|
|
174
|
+
return zeroPad(data, length, false);
|
|
175
|
+
}
|