ccxt 4.3.70 → 4.3.71
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 +6 -5
- package/dist/ccxt.browser.min.js +13 -10
- package/dist/cjs/ccxt.js +6 -1
- package/dist/cjs/src/abstract/paradex.js +9 -0
- package/dist/cjs/src/base/Exchange.js +49 -0
- package/dist/cjs/src/paradex.js +2075 -0
- package/dist/cjs/src/pro/bequant.js +4 -0
- package/dist/cjs/src/pro/paradex.js +365 -0
- package/dist/cjs/src/static_dependencies/noble-curves/abstract/poseidon.js +100 -0
- package/dist/cjs/src/static_dependencies/noble-curves/abstract/weierstrass.js +1 -0
- package/dist/cjs/src/static_dependencies/scure-starknet/index.js +284 -0
- package/dist/cjs/src/static_dependencies/starknet/constants.js +60 -0
- package/dist/cjs/src/static_dependencies/starknet/types/calldata.js +26 -0
- package/dist/cjs/src/static_dependencies/starknet/types/lib/contract/abi.js +8 -0
- package/dist/cjs/src/static_dependencies/starknet/types/lib/contract/index.js +13 -0
- package/dist/cjs/src/static_dependencies/starknet/types/lib/index.js +56 -0
- package/dist/cjs/src/static_dependencies/starknet/types/typedData.js +19 -0
- package/dist/cjs/src/static_dependencies/starknet/utils/assert.js +15 -0
- package/dist/cjs/src/static_dependencies/starknet/utils/cairoDataTypes/felt.js +44 -0
- package/dist/cjs/src/static_dependencies/starknet/utils/cairoDataTypes/uint256.js +122 -0
- package/dist/cjs/src/static_dependencies/starknet/utils/cairoDataTypes/uint512.js +137 -0
- package/dist/cjs/src/static_dependencies/starknet/utils/calldata/byteArray.js +61 -0
- package/dist/cjs/src/static_dependencies/starknet/utils/calldata/cairo.js +218 -0
- package/dist/cjs/src/static_dependencies/starknet/utils/calldata/enum/CairoCustomEnum.js +57 -0
- package/dist/cjs/src/static_dependencies/starknet/utils/calldata/enum/CairoOption.js +64 -0
- package/dist/cjs/src/static_dependencies/starknet/utils/calldata/enum/CairoResult.js +63 -0
- package/dist/cjs/src/static_dependencies/starknet/utils/calldata/formatter.js +66 -0
- package/dist/cjs/src/static_dependencies/starknet/utils/calldata/index.js +281 -0
- package/dist/cjs/src/static_dependencies/starknet/utils/calldata/parser/index.js +33 -0
- package/dist/cjs/src/static_dependencies/starknet/utils/calldata/parser/parser-0-1.1.0.js +37 -0
- package/dist/cjs/src/static_dependencies/starknet/utils/calldata/parser/parser-2.0.0.js +40 -0
- package/dist/cjs/src/static_dependencies/starknet/utils/calldata/propertyOrder.js +156 -0
- package/dist/cjs/src/static_dependencies/starknet/utils/calldata/requestParser.js +250 -0
- package/dist/cjs/src/static_dependencies/starknet/utils/calldata/responseParser.js +215 -0
- package/dist/cjs/src/static_dependencies/starknet/utils/calldata/tuple.js +112 -0
- package/dist/cjs/src/static_dependencies/starknet/utils/calldata/validate.js +206 -0
- package/dist/cjs/src/static_dependencies/starknet/utils/encode.js +58 -0
- package/dist/cjs/src/static_dependencies/starknet/utils/hash/classHash.js +57 -0
- package/dist/cjs/src/static_dependencies/starknet/utils/merkle.js +76 -0
- package/dist/cjs/src/static_dependencies/starknet/utils/num.js +92 -0
- package/dist/cjs/src/static_dependencies/starknet/utils/selector.js +48 -0
- package/dist/cjs/src/static_dependencies/starknet/utils/shortString.js +101 -0
- package/dist/cjs/src/static_dependencies/starknet/utils/typedData.js +334 -0
- package/dist/cjs/src/woo.js +4 -2
- package/js/ccxt.d.ts +8 -2
- package/js/ccxt.js +6 -2
- package/js/src/abstract/paradex.d.ts +43 -0
- package/js/src/abstract/paradex.js +11 -0
- package/js/src/base/Exchange.d.ts +7 -0
- package/js/src/base/Exchange.js +45 -0
- package/js/src/paradex.d.ts +76 -0
- package/js/src/paradex.js +2075 -0
- package/js/src/pro/bequant.js +4 -0
- package/js/src/pro/paradex.d.ts +15 -0
- package/js/src/pro/paradex.js +366 -0
- package/js/src/static_dependencies/noble-curves/abstract/weierstrass.d.ts +24 -0
- package/js/src/static_dependencies/noble-curves/abstract/weierstrass.js +1 -1
- package/js/src/static_dependencies/scure-starknet/index.d.ts +79 -0
- package/js/src/static_dependencies/scure-starknet/index.js +323 -0
- package/js/src/static_dependencies/starknet/constants.d.ts +61 -0
- package/js/src/static_dependencies/starknet/constants.js +67 -0
- package/js/src/static_dependencies/starknet/index.d.ts +7 -0
- package/js/src/static_dependencies/starknet/index.js +50 -0
- package/js/src/static_dependencies/starknet/types/cairoEnum.d.ts +2 -0
- package/js/src/static_dependencies/starknet/types/cairoEnum.js +7 -0
- package/js/src/static_dependencies/starknet/types/calldata.d.ts +19 -0
- package/js/src/static_dependencies/starknet/types/calldata.js +28 -0
- package/js/src/static_dependencies/starknet/types/index.d.ts +13 -0
- package/js/src/static_dependencies/starknet/types/index.js +16 -0
- package/js/src/static_dependencies/starknet/types/lib/contract/abi.d.ts +71 -0
- package/js/src/static_dependencies/starknet/types/lib/contract/abi.js +13 -0
- package/js/src/static_dependencies/starknet/types/lib/contract/index.d.ts +24 -0
- package/js/src/static_dependencies/starknet/types/lib/contract/index.js +16 -0
- package/js/src/static_dependencies/starknet/types/lib/contract/legacy.d.ts +33 -0
- package/js/src/static_dependencies/starknet/types/lib/contract/legacy.js +7 -0
- package/js/src/static_dependencies/starknet/types/lib/contract/sierra.d.ts +52 -0
- package/js/src/static_dependencies/starknet/types/lib/contract/sierra.js +7 -0
- package/js/src/static_dependencies/starknet/types/lib/index.d.ts +248 -0
- package/js/src/static_dependencies/starknet/types/lib/index.js +52 -0
- package/js/src/static_dependencies/starknet/types/typedData.d.ts +44 -0
- package/js/src/static_dependencies/starknet/types/typedData.js +19 -0
- package/js/src/static_dependencies/starknet/utils/address.d.ts +53 -0
- package/js/src/static_dependencies/starknet/utils/address.js +89 -0
- package/js/src/static_dependencies/starknet/utils/assert.d.ts +7 -0
- package/js/src/static_dependencies/starknet/utils/assert.js +17 -0
- package/js/src/static_dependencies/starknet/utils/cairoDataTypes/felt.d.ts +6 -0
- package/js/src/static_dependencies/starknet/utils/cairoDataTypes/felt.js +43 -0
- package/js/src/static_dependencies/starknet/utils/cairoDataTypes/uint256.d.ts +72 -0
- package/js/src/static_dependencies/starknet/utils/cairoDataTypes/uint256.js +117 -0
- package/js/src/static_dependencies/starknet/utils/cairoDataTypes/uint512.d.ts +76 -0
- package/js/src/static_dependencies/starknet/utils/cairoDataTypes/uint512.js +136 -0
- package/js/src/static_dependencies/starknet/utils/calldata/byteArray.d.ts +32 -0
- package/js/src/static_dependencies/starknet/utils/calldata/byteArray.js +59 -0
- package/js/src/static_dependencies/starknet/utils/calldata/cairo.d.ts +183 -0
- package/js/src/static_dependencies/starknet/utils/calldata/cairo.js +229 -0
- package/js/src/static_dependencies/starknet/utils/calldata/enum/CairoCustomEnum.d.ts +38 -0
- package/js/src/static_dependencies/starknet/utils/calldata/enum/CairoCustomEnum.js +57 -0
- package/js/src/static_dependencies/starknet/utils/calldata/enum/CairoOption.d.ts +35 -0
- package/js/src/static_dependencies/starknet/utils/calldata/enum/CairoOption.js +64 -0
- package/js/src/static_dependencies/starknet/utils/calldata/enum/CairoResult.d.ts +34 -0
- package/js/src/static_dependencies/starknet/utils/calldata/enum/CairoResult.js +63 -0
- package/js/src/static_dependencies/starknet/utils/calldata/enum/index.d.ts +3 -0
- package/js/src/static_dependencies/starknet/utils/calldata/enum/index.js +9 -0
- package/js/src/static_dependencies/starknet/utils/calldata/formatter.d.ts +9 -0
- package/js/src/static_dependencies/starknet/utils/calldata/formatter.js +67 -0
- package/js/src/static_dependencies/starknet/utils/calldata/index.d.ts +89 -0
- package/js/src/static_dependencies/starknet/utils/calldata/index.js +280 -0
- package/js/src/static_dependencies/starknet/utils/calldata/parser/index.d.ts +5 -0
- package/js/src/static_dependencies/starknet/utils/calldata/parser/index.js +30 -0
- package/js/src/static_dependencies/starknet/utils/calldata/parser/interface.d.ts +20 -0
- package/js/src/static_dependencies/starknet/utils/calldata/parser/interface.js +8 -0
- package/js/src/static_dependencies/starknet/utils/calldata/parser/parser-0-1.1.0.d.ts +24 -0
- package/js/src/static_dependencies/starknet/utils/calldata/parser/parser-0-1.1.0.js +36 -0
- package/js/src/static_dependencies/starknet/utils/calldata/parser/parser-2.0.0.d.ts +23 -0
- package/js/src/static_dependencies/starknet/utils/calldata/parser/parser-2.0.0.js +40 -0
- package/js/src/static_dependencies/starknet/utils/calldata/propertyOrder.d.ts +2 -0
- package/js/src/static_dependencies/starknet/utils/calldata/propertyOrder.js +155 -0
- package/js/src/static_dependencies/starknet/utils/calldata/requestParser.d.ts +11 -0
- package/js/src/static_dependencies/starknet/utils/calldata/requestParser.js +248 -0
- package/js/src/static_dependencies/starknet/utils/calldata/responseParser.d.ts +11 -0
- package/js/src/static_dependencies/starknet/utils/calldata/responseParser.js +214 -0
- package/js/src/static_dependencies/starknet/utils/calldata/tuple.d.ts +6 -0
- package/js/src/static_dependencies/starknet/utils/calldata/tuple.js +113 -0
- package/js/src/static_dependencies/starknet/utils/calldata/validate.d.ts +6 -0
- package/js/src/static_dependencies/starknet/utils/calldata/validate.js +208 -0
- package/js/src/static_dependencies/starknet/utils/encode.d.ts +207 -0
- package/js/src/static_dependencies/starknet/utils/encode.js +282 -0
- package/js/src/static_dependencies/starknet/utils/hash/classHash.d.ts +57 -0
- package/js/src/static_dependencies/starknet/utils/hash/classHash.js +224 -0
- package/js/src/static_dependencies/starknet/utils/hash/index.d.ts +6 -0
- package/js/src/static_dependencies/starknet/utils/hash/index.js +13 -0
- package/js/src/static_dependencies/starknet/utils/json.d.ts +24 -0
- package/js/src/static_dependencies/starknet/utils/json.js +43 -0
- package/js/src/static_dependencies/starknet/utils/merkle.d.ts +35 -0
- package/js/src/static_dependencies/starknet/utils/merkle.js +84 -0
- package/js/src/static_dependencies/starknet/utils/num.d.ts +182 -0
- package/js/src/static_dependencies/starknet/utils/num.js +244 -0
- package/js/src/static_dependencies/starknet/utils/selector.d.ts +48 -0
- package/js/src/static_dependencies/starknet/utils/selector.js +85 -0
- package/js/src/static_dependencies/starknet/utils/shortString.d.ts +57 -0
- package/js/src/static_dependencies/starknet/utils/shortString.js +96 -0
- package/js/src/static_dependencies/starknet/utils/starknetId.d.ts +113 -0
- package/js/src/static_dependencies/starknet/utils/starknetId.js +265 -0
- package/js/src/static_dependencies/starknet/utils/typedData.d.ts +54 -0
- package/js/src/static_dependencies/starknet/utils/typedData.js +321 -0
- package/js/src/static_dependencies/starknet/utils/uint256.d.ts +21 -0
- package/js/src/static_dependencies/starknet/utils/uint256.js +32 -0
- package/js/src/static_dependencies/starknet/utils/url.d.ts +29 -0
- package/js/src/static_dependencies/starknet/utils/url.js +70 -0
- package/js/src/woo.js +4 -2
- package/package.json +1 -1
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var calldata = require('../../types/calldata.js');
|
|
4
|
+
require('../../types/lib/index.js');
|
|
5
|
+
var assert = require('../assert.js');
|
|
6
|
+
var uint256 = require('../cairoDataTypes/uint256.js');
|
|
7
|
+
var uint512 = require('../cairoDataTypes/uint512.js');
|
|
8
|
+
var num = require('../num.js');
|
|
9
|
+
var shortString = require('../shortString.js');
|
|
10
|
+
var cairo = require('./cairo.js');
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Validate cairo contract method arguments
|
|
14
|
+
* Flow: Determine type from abi and than validate against parameter
|
|
15
|
+
*/
|
|
16
|
+
const validateFelt = (parameter, input) => {
|
|
17
|
+
assert(shortString.isString(parameter) || num.isNumber(parameter) || num.isBigInt(parameter), `Validate: arg ${input.name} should be a felt typed as (String, Number or BigInt)`);
|
|
18
|
+
if (shortString.isString(parameter) && !num.isHex(parameter))
|
|
19
|
+
return; // shortstring
|
|
20
|
+
const param = BigInt(parameter.toString(10));
|
|
21
|
+
assert(
|
|
22
|
+
// from : https://github.com/starkware-libs/starknet-specs/blob/29bab650be6b1847c92d4461d4c33008b5e50b1a/api/starknet_api_openrpc.json#L1266
|
|
23
|
+
param >= 0n && param <= 2n ** 252n - 1n, `Validate: arg ${input.name} cairo typed ${input.type} should be in range [0, 2^252-1]`);
|
|
24
|
+
};
|
|
25
|
+
const validateBytes31 = (parameter, input) => {
|
|
26
|
+
assert(shortString.isString(parameter), `Validate: arg ${input.name} should be a string.`);
|
|
27
|
+
assert(parameter.length < 32, `Validate: arg ${input.name} cairo typed ${input.type} should be a string of less than 32 characters.`);
|
|
28
|
+
};
|
|
29
|
+
const validateByteArray = (parameter, input) => {
|
|
30
|
+
assert(shortString.isString(parameter), `Validate: arg ${input.name} should be a string.`);
|
|
31
|
+
};
|
|
32
|
+
const validateUint = (parameter, input) => {
|
|
33
|
+
if (num.isNumber(parameter)) {
|
|
34
|
+
assert(parameter <= Number.MAX_SAFE_INTEGER, `Validation: Parameter is to large to be typed as Number use (BigInt or String)`);
|
|
35
|
+
}
|
|
36
|
+
assert(shortString.isString(parameter) ||
|
|
37
|
+
num.isNumber(parameter) ||
|
|
38
|
+
num.isBigInt(parameter) ||
|
|
39
|
+
(typeof parameter === 'object' && 'low' in parameter && 'high' in parameter) ||
|
|
40
|
+
(typeof parameter === 'object' &&
|
|
41
|
+
['limb0', 'limb1', 'limb2', 'limb3'].every((key) => key in parameter)), `Validate: arg ${input.name} of cairo type ${input.type} should be type (String, Number or BigInt), but is ${typeof parameter} ${parameter}.`);
|
|
42
|
+
let param;
|
|
43
|
+
switch (input.type) {
|
|
44
|
+
case calldata.Uint.u256:
|
|
45
|
+
param = new uint256.CairoUint256(parameter).toBigInt();
|
|
46
|
+
break;
|
|
47
|
+
case calldata.Uint.u512:
|
|
48
|
+
param = new uint512.CairoUint512(parameter).toBigInt();
|
|
49
|
+
break;
|
|
50
|
+
default:
|
|
51
|
+
param = num.toBigInt(parameter);
|
|
52
|
+
}
|
|
53
|
+
switch (input.type) {
|
|
54
|
+
case calldata.Uint.u8:
|
|
55
|
+
assert(param >= 0n && param <= 255n, `Validate: arg ${input.name} cairo typed ${input.type} should be in range [0 - 255]`);
|
|
56
|
+
break;
|
|
57
|
+
case calldata.Uint.u16:
|
|
58
|
+
assert(param >= 0n && param <= 65535n, `Validate: arg ${input.name} cairo typed ${input.type} should be in range [0, 65535]`);
|
|
59
|
+
break;
|
|
60
|
+
case calldata.Uint.u32:
|
|
61
|
+
assert(param >= 0n && param <= 4294967295n, `Validate: arg ${input.name} cairo typed ${input.type} should be in range [0, 4294967295]`);
|
|
62
|
+
break;
|
|
63
|
+
case calldata.Uint.u64:
|
|
64
|
+
assert(param >= 0n && param <= 2n ** 64n - 1n, `Validate: arg ${input.name} cairo typed ${input.type} should be in range [0, 2^64-1]`);
|
|
65
|
+
break;
|
|
66
|
+
case calldata.Uint.u128:
|
|
67
|
+
assert(param >= 0n && param <= 2n ** 128n - 1n, `Validate: arg ${input.name} cairo typed ${input.type} should be in range [0, 2^128-1]`);
|
|
68
|
+
break;
|
|
69
|
+
case calldata.Uint.u256:
|
|
70
|
+
assert(param >= 0n && param <= 2n ** 256n - 1n, `Validate: arg ${input.name} is ${input.type} 0 - 2^256-1`);
|
|
71
|
+
break;
|
|
72
|
+
case calldata.Uint.u512:
|
|
73
|
+
assert(uint512.CairoUint512.is(param), `Validate: arg ${input.name} is ${input.type} 0 - 2^512-1`);
|
|
74
|
+
break;
|
|
75
|
+
case calldata.Literal.ClassHash:
|
|
76
|
+
assert(
|
|
77
|
+
// from : https://github.com/starkware-libs/starknet-specs/blob/29bab650be6b1847c92d4461d4c33008b5e50b1a/api/starknet_api_openrpc.json#L1670
|
|
78
|
+
param >= 0n && param <= 2n ** 252n - 1n, `Validate: arg ${input.name} cairo typed ${input.type} should be in range [0, 2^252-1]`);
|
|
79
|
+
break;
|
|
80
|
+
case calldata.Literal.ContractAddress:
|
|
81
|
+
assert(
|
|
82
|
+
// from : https://github.com/starkware-libs/starknet-specs/blob/29bab650be6b1847c92d4461d4c33008b5e50b1a/api/starknet_api_openrpc.json#L1245
|
|
83
|
+
param >= 0n && param <= 2n ** 252n - 1n, `Validate: arg ${input.name} cairo typed ${input.type} should be in range [0, 2^252-1]`);
|
|
84
|
+
break;
|
|
85
|
+
case calldata.Literal.Secp256k1Point: {
|
|
86
|
+
assert(param >= 0n && param <= 2n ** 512n - 1n, `Validate: arg ${input.name} must be ${input.type} : a 512 bits number.`);
|
|
87
|
+
break;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
const validateBool = (parameter, input) => {
|
|
92
|
+
assert(num.isBoolean(parameter), `Validate: arg ${input.name} of cairo type ${input.type} should be type (Boolean)`);
|
|
93
|
+
};
|
|
94
|
+
const validateStruct = (parameter, input, structs) => {
|
|
95
|
+
// c1v2 uint256 or u512 in struct
|
|
96
|
+
if (input.type === calldata.Uint.u256 || input.type === calldata.Uint.u512) {
|
|
97
|
+
validateUint(parameter, input);
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
if (input.type === 'core::starknet::eth_address::EthAddress') {
|
|
101
|
+
assert(typeof parameter !== 'object', `EthAddress type is waiting a BigNumberish. Got ${parameter}`);
|
|
102
|
+
const param = BigInt(parameter.toString(10));
|
|
103
|
+
assert(
|
|
104
|
+
// from : https://github.com/starkware-libs/starknet-specs/blob/29bab650be6b1847c92d4461d4c33008b5e50b1a/api/starknet_api_openrpc.json#L1259
|
|
105
|
+
param >= 0n && param <= 2n ** 160n - 1n, `Validate: arg ${input.name} cairo typed ${input.type} should be in range [0, 2^160-1]`);
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
assert(typeof parameter === 'object' && !Array.isArray(parameter), `Validate: arg ${input.name} is cairo type struct (${input.type}), and should be defined as js object (not array)`);
|
|
109
|
+
// shallow struct validation, only first depth level
|
|
110
|
+
structs[input.type].members.forEach(({ name }) => {
|
|
111
|
+
assert(Object.keys(parameter).includes(name), `Validate: arg ${input.name} should have a property ${name}`);
|
|
112
|
+
});
|
|
113
|
+
};
|
|
114
|
+
const validateEnum = (parameter, input) => {
|
|
115
|
+
assert(typeof parameter === 'object' && !Array.isArray(parameter), `Validate: arg ${input.name} is cairo type Enum (${input.type}), and should be defined as js object (not array)`);
|
|
116
|
+
const methodsKeys = Object.getOwnPropertyNames(Object.getPrototypeOf(parameter));
|
|
117
|
+
const keys = [...Object.getOwnPropertyNames(parameter), ...methodsKeys];
|
|
118
|
+
if (cairo.isTypeOption(input.type) && keys.includes('isSome') && keys.includes('isNone')) {
|
|
119
|
+
return; // Option Enum
|
|
120
|
+
}
|
|
121
|
+
if (cairo.isTypeResult(input.type) && keys.includes('isOk') && keys.includes('isErr')) {
|
|
122
|
+
return; // Result Enum
|
|
123
|
+
}
|
|
124
|
+
if (keys.includes('variant') && keys.includes('activeVariant')) {
|
|
125
|
+
return; // Custom Enum
|
|
126
|
+
}
|
|
127
|
+
throw new Error(`Validate Enum: argument ${input.name}, type ${input.type}, value received ${parameter}, is not an Enum.`);
|
|
128
|
+
};
|
|
129
|
+
const validateTuple = (parameter, input) => {
|
|
130
|
+
assert(typeof parameter === 'object' && !Array.isArray(parameter), `Validate: arg ${input.name} should be a tuple (defined as object)`);
|
|
131
|
+
// todo: skip tuple structural validation for now
|
|
132
|
+
};
|
|
133
|
+
const validateArray = (parameter, input, structs, enums) => {
|
|
134
|
+
const baseType = cairo.getArrayType(input.type);
|
|
135
|
+
// Long text (special case when parameter is not an array but long text)
|
|
136
|
+
if (cairo.isTypeFelt(baseType) && shortString.isLongText(parameter)) {
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
assert(Array.isArray(parameter), `Validate: arg ${input.name} should be an Array`);
|
|
140
|
+
switch (true) {
|
|
141
|
+
case cairo.isTypeFelt(baseType):
|
|
142
|
+
parameter.forEach((param) => validateFelt(param, input));
|
|
143
|
+
break;
|
|
144
|
+
case cairo.isTypeTuple(baseType):
|
|
145
|
+
parameter.forEach((it) => validateTuple(it, { name: input.name, type: baseType }));
|
|
146
|
+
break;
|
|
147
|
+
case cairo.isTypeArray(baseType):
|
|
148
|
+
parameter.forEach((param) => validateArray(param, { name: '', type: baseType }, structs, enums));
|
|
149
|
+
break;
|
|
150
|
+
case cairo.isTypeStruct(baseType, structs):
|
|
151
|
+
parameter.forEach((it) => validateStruct(it, { name: input.name, type: baseType }, structs));
|
|
152
|
+
break;
|
|
153
|
+
case cairo.isTypeEnum(baseType, enums):
|
|
154
|
+
parameter.forEach((it) => validateEnum(it, { name: input.name, type: baseType }));
|
|
155
|
+
break;
|
|
156
|
+
case cairo.isTypeUint(baseType) || cairo.isTypeLiteral(baseType):
|
|
157
|
+
parameter.forEach((param) => validateUint(param, { name: '', type: baseType }));
|
|
158
|
+
break;
|
|
159
|
+
case cairo.isTypeBool(baseType):
|
|
160
|
+
parameter.forEach((param) => validateBool(param, input));
|
|
161
|
+
break;
|
|
162
|
+
default:
|
|
163
|
+
throw new Error(`Validate Unhandled: argument ${input.name}, type ${input.type}, value ${parameter}`);
|
|
164
|
+
}
|
|
165
|
+
};
|
|
166
|
+
function validateFields(abiMethod, args, structs, enums) {
|
|
167
|
+
abiMethod.inputs.reduce((acc, input) => {
|
|
168
|
+
const parameter = args[acc];
|
|
169
|
+
switch (true) {
|
|
170
|
+
case cairo.isLen(input.name):
|
|
171
|
+
return acc;
|
|
172
|
+
case cairo.isTypeFelt(input.type):
|
|
173
|
+
validateFelt(parameter, input);
|
|
174
|
+
break;
|
|
175
|
+
case cairo.isTypeBytes31(input.type):
|
|
176
|
+
validateBytes31(parameter, input);
|
|
177
|
+
break;
|
|
178
|
+
case cairo.isTypeUint(input.type) || cairo.isTypeLiteral(input.type):
|
|
179
|
+
validateUint(parameter, input);
|
|
180
|
+
break;
|
|
181
|
+
case cairo.isTypeBool(input.type):
|
|
182
|
+
validateBool(parameter, input);
|
|
183
|
+
break;
|
|
184
|
+
case cairo.isTypeByteArray(input.type):
|
|
185
|
+
validateByteArray(parameter, input);
|
|
186
|
+
break;
|
|
187
|
+
case cairo.isTypeArray(input.type):
|
|
188
|
+
validateArray(parameter, input, structs, enums);
|
|
189
|
+
break;
|
|
190
|
+
case cairo.isTypeStruct(input.type, structs):
|
|
191
|
+
validateStruct(parameter, input, structs);
|
|
192
|
+
break;
|
|
193
|
+
case cairo.isTypeEnum(input.type, enums):
|
|
194
|
+
validateEnum(parameter, input);
|
|
195
|
+
break;
|
|
196
|
+
case cairo.isTypeTuple(input.type):
|
|
197
|
+
validateTuple(parameter, input);
|
|
198
|
+
break;
|
|
199
|
+
default:
|
|
200
|
+
throw new Error(`Validate Unhandled: argument ${input.name}, type ${input.type}, value ${parameter}`);
|
|
201
|
+
}
|
|
202
|
+
return acc + 1;
|
|
203
|
+
}, 0);
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
module.exports = validateFields;
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
require('../../scure-base/index.js');
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Convert utf8-string to Uint8Array
|
|
9
|
+
*
|
|
10
|
+
* *[internal usage]*
|
|
11
|
+
*
|
|
12
|
+
* @param {string} str The UTF-8 string to convert.
|
|
13
|
+
* @returns {Uint8Array} The encoded Uint8Array.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```typescript
|
|
17
|
+
* const myString = 'Hi';
|
|
18
|
+
* const result = encode.utf8ToArray(myString);
|
|
19
|
+
* // result = Uint8Array(2) [ 72, 105 ]
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
function utf8ToArray(str) {
|
|
23
|
+
return new TextEncoder().encode(str);
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Remove hex prefix '0x' from hex-string
|
|
27
|
+
* @param hex hex-string
|
|
28
|
+
* @returns {string} The hex-string
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* ```typescript
|
|
32
|
+
* const hexStringWithPrefix = '0x48656c6c6f';
|
|
33
|
+
* const result = encode.removeHexPrefix(hexStringWithPrefix);
|
|
34
|
+
* // result: "48656c6c6f"
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
function removeHexPrefix(hex) {
|
|
38
|
+
return hex.replace(/^0x/i, '');
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Add hex prefix '0x' to base16-string
|
|
42
|
+
* @param hex base16-string
|
|
43
|
+
* @returns {string} The hex-string
|
|
44
|
+
*
|
|
45
|
+
* @example
|
|
46
|
+
* ```typescript
|
|
47
|
+
* const plainHexString = '48656c6c6f';
|
|
48
|
+
* const result = encode.addHexPrefix(plainHexString);
|
|
49
|
+
* // result: "0x48656c6c6f"
|
|
50
|
+
* ```
|
|
51
|
+
*/
|
|
52
|
+
function addHexPrefix(hex) {
|
|
53
|
+
return `0x${removeHexPrefix(hex)}`;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
exports.addHexPrefix = addHexPrefix;
|
|
57
|
+
exports.removeHexPrefix = removeHexPrefix;
|
|
58
|
+
exports.utf8ToArray = utf8ToArray;
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var index$1 = require('../../../scure-starknet/index.js');
|
|
6
|
+
var constants = require('../../constants.js');
|
|
7
|
+
var index = require('../calldata/index.js');
|
|
8
|
+
var cairo = require('../calldata/cairo.js');
|
|
9
|
+
require('../../../scure-base/index.js');
|
|
10
|
+
var num = require('../num.js');
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Class Hash
|
|
14
|
+
*/
|
|
15
|
+
function computePedersenHash(a, b) {
|
|
16
|
+
return index$1.pedersen(BigInt(a), BigInt(b));
|
|
17
|
+
}
|
|
18
|
+
function computePoseidonHash(a, b) {
|
|
19
|
+
return num.toHex(index$1.poseidonHash(BigInt(a), BigInt(b)));
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Compute pedersen hash from data
|
|
23
|
+
* @returns format: hex-string - pedersen hash
|
|
24
|
+
*/
|
|
25
|
+
function computeHashOnElements(data) {
|
|
26
|
+
return [...data, data.length]
|
|
27
|
+
.reduce((x, y) => index$1.pedersen(BigInt(x), BigInt(y)), 0)
|
|
28
|
+
.toString();
|
|
29
|
+
}
|
|
30
|
+
const computePedersenHashOnElements = computeHashOnElements;
|
|
31
|
+
function computePoseidonHashOnElements(data) {
|
|
32
|
+
return num.toHex(index$1.poseidonHashMany(data.map((x) => BigInt(x))));
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Calculate contract address from class hash
|
|
36
|
+
* @returns format: hex-string
|
|
37
|
+
*/
|
|
38
|
+
function calculateContractAddressFromHash(salt, classHash, constructorCalldata, deployerAddress) {
|
|
39
|
+
const compiledCalldata = index.CallData.compile(constructorCalldata);
|
|
40
|
+
const constructorCalldataHash = computeHashOnElements(compiledCalldata);
|
|
41
|
+
const CONTRACT_ADDRESS_PREFIX = cairo.felt('0x535441524b4e45545f434f4e54524143545f41444452455353'); // Equivalent to 'STARKNET_CONTRACT_ADDRESS'
|
|
42
|
+
const hash = computeHashOnElements([
|
|
43
|
+
CONTRACT_ADDRESS_PREFIX,
|
|
44
|
+
deployerAddress,
|
|
45
|
+
salt,
|
|
46
|
+
classHash,
|
|
47
|
+
constructorCalldataHash,
|
|
48
|
+
]);
|
|
49
|
+
return num.toHex(BigInt(hash) % constants.ADDR_BOUND);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
exports.calculateContractAddressFromHash = calculateContractAddressFromHash;
|
|
53
|
+
exports.computeHashOnElements = computeHashOnElements;
|
|
54
|
+
exports.computePedersenHash = computePedersenHash;
|
|
55
|
+
exports.computePedersenHashOnElements = computePedersenHashOnElements;
|
|
56
|
+
exports.computePoseidonHash = computePoseidonHash;
|
|
57
|
+
exports.computePoseidonHashOnElements = computePoseidonHashOnElements;
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
require('../../noble-curves/abstract/modular.js');
|
|
6
|
+
require('../../scure-starknet/index.js');
|
|
7
|
+
require('../constants.js');
|
|
8
|
+
require('../../scure-base/index.js');
|
|
9
|
+
require('../../noble-curves/abstract/utils.js');
|
|
10
|
+
var classHash = require('./hash/classHash.js');
|
|
11
|
+
|
|
12
|
+
class MerkleTree {
|
|
13
|
+
constructor(leafHashes, hashMethod = classHash.computePedersenHash) {
|
|
14
|
+
this.branches = [];
|
|
15
|
+
this.hashMethod = hashMethod;
|
|
16
|
+
this.leaves = leafHashes;
|
|
17
|
+
this.root = this.build(leafHashes);
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Create Merkle tree
|
|
21
|
+
* @param leaves hex-string array
|
|
22
|
+
* @returns format: hex-string; Merkle tree root
|
|
23
|
+
*/
|
|
24
|
+
build(leaves) {
|
|
25
|
+
if (leaves.length === 1) {
|
|
26
|
+
return leaves[0];
|
|
27
|
+
}
|
|
28
|
+
if (leaves.length !== this.leaves.length) {
|
|
29
|
+
this.branches.push(leaves);
|
|
30
|
+
}
|
|
31
|
+
const newLeaves = [];
|
|
32
|
+
for (let i = 0; i < leaves.length; i += 2) {
|
|
33
|
+
if (i + 1 === leaves.length) {
|
|
34
|
+
newLeaves.push(MerkleTree.hash(leaves[i], '0x0', this.hashMethod));
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
newLeaves.push(MerkleTree.hash(leaves[i], leaves[i + 1], this.hashMethod));
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
return this.build(newLeaves);
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Create hash from ordered a and b, Pedersen hash default
|
|
44
|
+
* @returns format: hex-string
|
|
45
|
+
*/
|
|
46
|
+
static hash(a, b, hashMethod = classHash.computePedersenHash) {
|
|
47
|
+
const [aSorted, bSorted] = [BigInt(a), BigInt(b)].sort((x, y) => (x >= y ? 1 : -1));
|
|
48
|
+
return hashMethod(aSorted, bSorted);
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Return path to leaf
|
|
52
|
+
* @param leaf hex-string
|
|
53
|
+
* @param branch hex-string array
|
|
54
|
+
* @param hashPath hex-string array
|
|
55
|
+
* @returns format: hex-string array
|
|
56
|
+
*/
|
|
57
|
+
getProof(leaf, branch = this.leaves, hashPath = []) {
|
|
58
|
+
const index = branch.indexOf(leaf);
|
|
59
|
+
if (index === -1) {
|
|
60
|
+
throw new Error('leaf not found');
|
|
61
|
+
}
|
|
62
|
+
if (branch.length === 1) {
|
|
63
|
+
return hashPath;
|
|
64
|
+
}
|
|
65
|
+
const isLeft = index % 2 === 0;
|
|
66
|
+
const neededBranch = (isLeft ? branch[index + 1] : branch[index - 1]) ?? '0x0';
|
|
67
|
+
const newHashPath = [...hashPath, neededBranch];
|
|
68
|
+
const currentBranchLevelIndex = this.leaves.length === branch.length
|
|
69
|
+
? -1
|
|
70
|
+
: this.branches.findIndex((b) => b.length === branch.length);
|
|
71
|
+
const nextBranch = this.branches[currentBranchLevelIndex + 1] ?? [this.root];
|
|
72
|
+
return this.getProof(MerkleTree.hash(isLeft ? leaf : neededBranch, isLeft ? neededBranch : leaf, this.hashMethod), nextBranch, newHashPath);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
exports.MerkleTree = MerkleTree;
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
require('../../noble-curves/abstract/utils.js');
|
|
6
|
+
var encode = require('./encode.js');
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Test if string is hex-string
|
|
10
|
+
* @param hex hex-string
|
|
11
|
+
* @returns {boolean} True if the input string is a hexadecimal string, false otherwise
|
|
12
|
+
* @example
|
|
13
|
+
* ```typescript
|
|
14
|
+
* const hexString1 = "0x2fd23d9182193775423497fc0c472e156c57c69e4089a1967fb288a2d84e914";
|
|
15
|
+
* const result1 = isHex(hexString1);
|
|
16
|
+
* // result1 = true
|
|
17
|
+
*
|
|
18
|
+
* const hexString2 = "2fd23d9182193775423497fc0c472e156c57c69e4089a1967fb288a2d84e914";
|
|
19
|
+
* const result2 = isHex(hexString2);
|
|
20
|
+
* // result2 = false
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
function isHex(hex) {
|
|
24
|
+
return /^0x[0-9a-f]*$/i.test(hex);
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Convert BigNumberish to bigint
|
|
28
|
+
*/
|
|
29
|
+
function toBigInt(value) {
|
|
30
|
+
return BigInt(value);
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Test if value is bigint
|
|
34
|
+
*/
|
|
35
|
+
function isBigInt(value) {
|
|
36
|
+
return typeof value === 'bigint';
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Convert BigNumberish to hex-string
|
|
40
|
+
* @returns format: hex-string
|
|
41
|
+
*/
|
|
42
|
+
function toHex(number) {
|
|
43
|
+
return encode.addHexPrefix(toBigInt(number).toString(16));
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Test if string is whole number (0, 1, 2, 3...)
|
|
47
|
+
*/
|
|
48
|
+
const isStringWholeNumber = (value) => /^\d+$/.test(value);
|
|
49
|
+
/**
|
|
50
|
+
* Check if a value is a number.
|
|
51
|
+
*
|
|
52
|
+
* @param {unknown} value - The value to check.
|
|
53
|
+
* @returns {boolean} Returns true if the value is a number, otherwise returns false.
|
|
54
|
+
* @example
|
|
55
|
+
* ```typescript
|
|
56
|
+
* const result = isNumber(123);
|
|
57
|
+
* // result = true
|
|
58
|
+
*
|
|
59
|
+
* const result2 = isNumber("123");
|
|
60
|
+
* // result2 = false
|
|
61
|
+
* ```
|
|
62
|
+
* @return {boolean} Returns true if the value is a number, otherwise returns false.
|
|
63
|
+
*/
|
|
64
|
+
function isNumber(value) {
|
|
65
|
+
return typeof value === 'number';
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Checks if a given value is of boolean type.
|
|
69
|
+
*
|
|
70
|
+
* @param {unknown} value - The value to check.
|
|
71
|
+
* @returns {boolean} - True if the value is of boolean type, false otherwise.
|
|
72
|
+
* @example
|
|
73
|
+
* ```typescript
|
|
74
|
+
* const result = isBoolean(true);
|
|
75
|
+
* // result = true
|
|
76
|
+
*
|
|
77
|
+
* const result2 = isBoolean(false);
|
|
78
|
+
* // result2 = false
|
|
79
|
+
* ```
|
|
80
|
+
* @return {boolean} - True if the value is of boolean type, false otherwise.
|
|
81
|
+
*/
|
|
82
|
+
function isBoolean(value) {
|
|
83
|
+
return typeof value === 'boolean';
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
exports.isBigInt = isBigInt;
|
|
87
|
+
exports.isBoolean = isBoolean;
|
|
88
|
+
exports.isHex = isHex;
|
|
89
|
+
exports.isNumber = isNumber;
|
|
90
|
+
exports.isStringWholeNumber = isStringWholeNumber;
|
|
91
|
+
exports.toBigInt = toBigInt;
|
|
92
|
+
exports.toHex = toHex;
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var index = require('../../scure-starknet/index.js');
|
|
6
|
+
var constants = require('../constants.js');
|
|
7
|
+
var encode = require('./encode.js');
|
|
8
|
+
var num = require('./num.js');
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Calculate hex-string keccak hash for a given string
|
|
12
|
+
*
|
|
13
|
+
* String -> hex-string keccak hash
|
|
14
|
+
* @returns format: hex-string
|
|
15
|
+
*/
|
|
16
|
+
function keccakHex(str) {
|
|
17
|
+
return encode.addHexPrefix(index.keccak(encode.utf8ToArray(str)).toString(16));
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Calculate bigint keccak hash for a given string
|
|
21
|
+
*
|
|
22
|
+
* String -> bigint keccak hash
|
|
23
|
+
*
|
|
24
|
+
* [Reference](https://github.com/starkware-libs/cairo-lang/blob/master/src/starkware/starknet/public/abi.py#L17-L22)
|
|
25
|
+
* @param str the value you want to get the keccak hash from
|
|
26
|
+
* @returns starknet keccak hash as BigInt
|
|
27
|
+
*/
|
|
28
|
+
function starknetKeccak(str) {
|
|
29
|
+
const hash = BigInt(keccakHex(str));
|
|
30
|
+
// eslint-disable-next-line no-bitwise
|
|
31
|
+
return hash & constants.MASK_250;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Calculate hex-string selector for a given abi-function-name
|
|
35
|
+
*
|
|
36
|
+
* Abi-function-name -> hex-string selector
|
|
37
|
+
*
|
|
38
|
+
* [Reference](https://github.com/starkware-libs/cairo-lang/blob/master/src/starkware/starknet/public/abi.py#L25-L26)
|
|
39
|
+
* @param funcName ascii-string of 'abi function name'
|
|
40
|
+
* @returns format: hex-string; selector for 'abi function name'
|
|
41
|
+
*/
|
|
42
|
+
function getSelectorFromName(funcName) {
|
|
43
|
+
// sometimes BigInteger pads the hex string with zeros, which is not allowed in the starknet api
|
|
44
|
+
return num.toHex(starknetKeccak(funcName));
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
exports.getSelectorFromName = getSelectorFromName;
|
|
48
|
+
exports.starknetKeccak = starknetKeccak;
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var constants = require('../constants.js');
|
|
6
|
+
var encode = require('./encode.js');
|
|
7
|
+
var num = require('./num.js');
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Test if string contains only ASCII characters (string can be ascii text)
|
|
11
|
+
*/
|
|
12
|
+
function isASCII(str) {
|
|
13
|
+
// eslint-disable-next-line no-control-regex
|
|
14
|
+
return /^[\x00-\x7F]*$/.test(str);
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Test if string is a Cairo short string (string has less or equal 31 characters)
|
|
18
|
+
*/
|
|
19
|
+
function isShortString(str) {
|
|
20
|
+
return str.length <= constants.TEXT_TO_FELT_MAX_LEN;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Test if string contains only numbers (string can be converted to decimal number)
|
|
24
|
+
*/
|
|
25
|
+
function isDecimalString(str) {
|
|
26
|
+
return /^[0-9]*$/i.test(str);
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Checks if a given value is a string.
|
|
30
|
+
*
|
|
31
|
+
* @param {unknown} value - The value to be checked.
|
|
32
|
+
* @return {boolean} - Returns true if the value is a string, false otherwise.
|
|
33
|
+
*/
|
|
34
|
+
function isString(value) {
|
|
35
|
+
return typeof value === 'string';
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Test if value is a free-from string text, and not a hex string or number string
|
|
39
|
+
*/
|
|
40
|
+
function isText(val) {
|
|
41
|
+
return isString(val) && !num.isHex(val) && !num.isStringWholeNumber(val);
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Test if value is long text
|
|
45
|
+
*/
|
|
46
|
+
const isLongText = (val) => isText(val) && !isShortString(val);
|
|
47
|
+
/**
|
|
48
|
+
* Split long text into short strings
|
|
49
|
+
*/
|
|
50
|
+
function splitLongString(longStr) {
|
|
51
|
+
const regex = RegExp(`[^]{1,${constants.TEXT_TO_FELT_MAX_LEN}}`, 'g');
|
|
52
|
+
return longStr.match(regex) || [];
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Convert an ASCII string to a hexadecimal string.
|
|
56
|
+
* @param str short string (ASCII string, 31 characters max)
|
|
57
|
+
* @returns format: hex-string; 248 bits max
|
|
58
|
+
* @example
|
|
59
|
+
* ```typescript
|
|
60
|
+
* const myEncodedString: string = encodeShortString("uri/pict/t38.jpg");
|
|
61
|
+
* // return hex string (ex."0x7572692f706963742f7433382e6a7067")
|
|
62
|
+
* ```
|
|
63
|
+
*/
|
|
64
|
+
function encodeShortString(str) {
|
|
65
|
+
if (!isASCII(str))
|
|
66
|
+
throw new Error(`${str} is not an ASCII string`);
|
|
67
|
+
if (!isShortString(str))
|
|
68
|
+
throw new Error(`${str} is too long`);
|
|
69
|
+
return encode.addHexPrefix(str.replace(/./g, (char) => char.charCodeAt(0).toString(16)));
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Convert a hexadecimal or decimal string to an ASCII string.
|
|
73
|
+
* @param str representing a 248 bit max number (ex. "0x1A4F64EA56" or "236942575435676423")
|
|
74
|
+
* @returns format: short string; 31 characters max
|
|
75
|
+
* @example
|
|
76
|
+
* ```typescript
|
|
77
|
+
* const myDecodedString: string = decodeShortString("0x7572692f706963742f7433382e6a7067");
|
|
78
|
+
* // return string (ex."uri/pict/t38.jpg")
|
|
79
|
+
* ```
|
|
80
|
+
*/
|
|
81
|
+
function decodeShortString(str) {
|
|
82
|
+
if (!isASCII(str))
|
|
83
|
+
throw new Error(`${str} is not an ASCII string`);
|
|
84
|
+
if (num.isHex(str)) {
|
|
85
|
+
return encode.removeHexPrefix(str).replace(/.{2}/g, (hex) => String.fromCharCode(parseInt(hex, 16)));
|
|
86
|
+
}
|
|
87
|
+
if (isDecimalString(str)) {
|
|
88
|
+
return decodeShortString('0X'.concat(BigInt(str).toString(16)));
|
|
89
|
+
}
|
|
90
|
+
throw new Error(`${str} is not Hex or decimal`);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
exports.decodeShortString = decodeShortString;
|
|
94
|
+
exports.encodeShortString = encodeShortString;
|
|
95
|
+
exports.isASCII = isASCII;
|
|
96
|
+
exports.isDecimalString = isDecimalString;
|
|
97
|
+
exports.isLongText = isLongText;
|
|
98
|
+
exports.isShortString = isShortString;
|
|
99
|
+
exports.isString = isString;
|
|
100
|
+
exports.isText = isText;
|
|
101
|
+
exports.splitLongString = splitLongString;
|