starknet 2.1.1 → 2.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +45 -0
- package/__tests__/signer.test.ts +17 -0
- package/__tests__/utils/shortString.test.ts +22 -0
- package/__tests__/utils/uint256.test.ts +32 -0
- package/constants.d.ts +5 -5
- package/contract.d.ts +7 -1
- package/contract.js +23 -5
- package/dist/constants.d.ts +5 -5
- package/dist/contract.d.ts +4 -1
- package/dist/contract.js +19 -5
- package/dist/index.d.ts +2 -0
- package/dist/index.js +3 -1
- package/dist/provider/default.d.ts +5 -5
- package/dist/provider/default.js +18 -17
- package/dist/provider/interface.d.ts +4 -4
- package/dist/signer/default.d.ts +2 -2
- package/dist/signer/default.js +20 -15
- package/dist/signer/interface.d.ts +2 -2
- package/dist/types.d.ts +4 -2
- package/dist/utils/shortString.d.ts +4 -0
- package/dist/utils/shortString.js +26 -0
- package/dist/utils/uint256.d.ts +11 -0
- package/dist/utils/uint256.js +28 -0
- package/index.d.ts +2 -0
- package/index.js +5 -1
- package/package.json +5 -2
- package/provider/default.d.ts +8 -5
- package/provider/default.js +57 -38
- package/provider/interface.d.ts +4 -4
- package/signer/default.d.ts +2 -2
- package/signer/default.js +18 -12
- package/signer/interface.d.ts +2 -2
- package/src/constants.ts +4 -6
- package/src/contract.ts +15 -12
- package/src/index.ts +2 -0
- package/src/provider/default.ts +34 -23
- package/src/provider/interface.ts +4 -4
- package/src/signer/default.ts +23 -14
- package/src/signer/interface.ts +4 -2
- package/src/types.ts +4 -2
- package/src/utils/shortString.ts +21 -0
- package/src/utils/uint256.ts +32 -0
- package/types.d.ts +4 -2
- package/utils/shortString.d.ts +4 -0
- package/utils/shortString.js +34 -0
- package/utils/uint256.d.ts +11 -0
- package/utils/uint256.js +38 -0
package/src/provider/default.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import axios from 'axios';
|
|
2
|
+
import urljoin from 'url-join';
|
|
2
3
|
|
|
3
4
|
import {
|
|
4
5
|
AddTransactionResponse,
|
|
@@ -49,15 +50,15 @@ export class Provider implements ProviderInterface {
|
|
|
49
50
|
? optionsOrProvider.baseUrl
|
|
50
51
|
: Provider.getNetworkFromName(optionsOrProvider.network);
|
|
51
52
|
this.baseUrl = baseUrl;
|
|
52
|
-
this.feederGatewayUrl =
|
|
53
|
-
this.gatewayUrl =
|
|
53
|
+
this.feederGatewayUrl = urljoin(baseUrl, 'feeder_gateway');
|
|
54
|
+
this.gatewayUrl = urljoin(baseUrl, 'gateway');
|
|
54
55
|
}
|
|
55
56
|
}
|
|
56
57
|
|
|
57
58
|
protected static getNetworkFromName(name: NetworkName) {
|
|
58
59
|
switch (name) {
|
|
59
60
|
case 'mainnet-alpha':
|
|
60
|
-
return '
|
|
61
|
+
return 'https://alpha-mainnet.starknet.io';
|
|
61
62
|
case 'georli-alpha':
|
|
62
63
|
default:
|
|
63
64
|
return 'https://alpha4.starknet.io';
|
|
@@ -72,7 +73,7 @@ export class Provider implements ProviderInterface {
|
|
|
72
73
|
*/
|
|
73
74
|
public async getContractAddresses(): Promise<GetContractAddressesResponse> {
|
|
74
75
|
const { data } = await axios.get<GetContractAddressesResponse>(
|
|
75
|
-
|
|
76
|
+
urljoin(this.feederGatewayUrl, 'get_contract_addresses')
|
|
76
77
|
);
|
|
77
78
|
return data;
|
|
78
79
|
}
|
|
@@ -82,20 +83,20 @@ export class Provider implements ProviderInterface {
|
|
|
82
83
|
*
|
|
83
84
|
* [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L17-L25)
|
|
84
85
|
*
|
|
85
|
-
* @param
|
|
86
|
+
* @param invokeTransaction - transaction to be invoked
|
|
86
87
|
* @param blockId
|
|
87
88
|
* @returns the result of the function on the smart contract.
|
|
88
89
|
*/
|
|
89
90
|
public async callContract(
|
|
90
|
-
|
|
91
|
+
invokeTransaction: CallContractTransaction,
|
|
91
92
|
blockId?: number
|
|
92
93
|
): Promise<CallContractResponse> {
|
|
93
94
|
const { data } = await axios.post<CallContractResponse>(
|
|
94
|
-
|
|
95
|
+
urljoin(this.feederGatewayUrl, 'call_contract', `?blockId=${blockId ?? 'null'}`),
|
|
95
96
|
{
|
|
96
97
|
signature: [],
|
|
97
98
|
calldata: [],
|
|
98
|
-
...
|
|
99
|
+
...invokeTransaction,
|
|
99
100
|
}
|
|
100
101
|
);
|
|
101
102
|
return data;
|
|
@@ -111,7 +112,7 @@ export class Provider implements ProviderInterface {
|
|
|
111
112
|
*/
|
|
112
113
|
public async getBlock(blockId?: number): Promise<GetBlockResponse> {
|
|
113
114
|
const { data } = await axios.get<GetBlockResponse>(
|
|
114
|
-
|
|
115
|
+
urljoin(this.feederGatewayUrl, 'get_block', `?blockId=${blockId ?? 'null'}`)
|
|
115
116
|
);
|
|
116
117
|
return data;
|
|
117
118
|
}
|
|
@@ -127,9 +128,11 @@ export class Provider implements ProviderInterface {
|
|
|
127
128
|
*/
|
|
128
129
|
public async getCode(contractAddress: string, blockId?: number): Promise<GetCodeResponse> {
|
|
129
130
|
const { data } = await axios.get<GetCodeResponse>(
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
131
|
+
urljoin(
|
|
132
|
+
this.feederGatewayUrl,
|
|
133
|
+
'get_code',
|
|
134
|
+
`?contractAddress=${contractAddress}&blockId=${blockId ?? 'null'}`
|
|
135
|
+
)
|
|
133
136
|
);
|
|
134
137
|
return data;
|
|
135
138
|
}
|
|
@@ -151,9 +154,11 @@ export class Provider implements ProviderInterface {
|
|
|
151
154
|
blockId?: number
|
|
152
155
|
): Promise<object> {
|
|
153
156
|
const { data } = await axios.get<object>(
|
|
154
|
-
|
|
155
|
-
this.feederGatewayUrl
|
|
156
|
-
|
|
157
|
+
urljoin(
|
|
158
|
+
this.feederGatewayUrl,
|
|
159
|
+
'get_storage_at',
|
|
160
|
+
`?contractAddress=${contractAddress}&key=${key}&blockId=${blockId ?? 'null'}`
|
|
161
|
+
)
|
|
157
162
|
);
|
|
158
163
|
return data;
|
|
159
164
|
}
|
|
@@ -169,7 +174,11 @@ export class Provider implements ProviderInterface {
|
|
|
169
174
|
public async getTransactionStatus(txHash: BigNumberish): Promise<GetTransactionStatusResponse> {
|
|
170
175
|
const txHashBn = toBN(txHash);
|
|
171
176
|
const { data } = await axios.get<GetTransactionStatusResponse>(
|
|
172
|
-
|
|
177
|
+
urljoin(
|
|
178
|
+
this.feederGatewayUrl,
|
|
179
|
+
'get_transaction_status',
|
|
180
|
+
`?transactionHash=${toHex(txHashBn)}`
|
|
181
|
+
)
|
|
173
182
|
);
|
|
174
183
|
return data;
|
|
175
184
|
}
|
|
@@ -185,7 +194,7 @@ export class Provider implements ProviderInterface {
|
|
|
185
194
|
public async getTransaction(txHash: BigNumberish): Promise<GetTransactionResponse> {
|
|
186
195
|
const txHashBn = toBN(txHash);
|
|
187
196
|
const { data } = await axios.get<GetTransactionResponse>(
|
|
188
|
-
|
|
197
|
+
urljoin(this.feederGatewayUrl, 'get_transaction', `?transactionHash=${toHex(txHashBn)}`)
|
|
189
198
|
);
|
|
190
199
|
return data;
|
|
191
200
|
}
|
|
@@ -195,17 +204,19 @@ export class Provider implements ProviderInterface {
|
|
|
195
204
|
*
|
|
196
205
|
* [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/gateway/gateway_client.py#L13-L17)
|
|
197
206
|
*
|
|
198
|
-
* @param
|
|
207
|
+
* @param transaction - transaction to be invoked
|
|
199
208
|
* @returns a confirmation of invoking a function on the starknet contract
|
|
200
209
|
*/
|
|
201
|
-
public async addTransaction(
|
|
202
|
-
const signature =
|
|
203
|
-
|
|
210
|
+
public async addTransaction(transaction: Transaction): Promise<AddTransactionResponse> {
|
|
211
|
+
const signature =
|
|
212
|
+
transaction.type === 'INVOKE_FUNCTION' && formatSignature(transaction.signature);
|
|
213
|
+
const contract_address_salt =
|
|
214
|
+
transaction.type === 'DEPLOY' && toHex(toBN(transaction.contract_address_salt));
|
|
204
215
|
|
|
205
216
|
const { data } = await axios.post<AddTransactionResponse>(
|
|
206
|
-
|
|
217
|
+
urljoin(this.gatewayUrl, 'add_transaction'),
|
|
207
218
|
stringify({
|
|
208
|
-
...
|
|
219
|
+
...transaction, // the tx can contain BigInts, so we use our own `stringify`
|
|
209
220
|
...(Array.isArray(signature) && { signature }), // not needed on deploy tx
|
|
210
221
|
...(contract_address_salt && { contract_address_salt }), // not needed on invoke tx
|
|
211
222
|
}),
|
|
@@ -32,12 +32,12 @@ export abstract class ProviderInterface {
|
|
|
32
32
|
*
|
|
33
33
|
* [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L17-L25)
|
|
34
34
|
*
|
|
35
|
-
* @param
|
|
35
|
+
* @param invokeTransaction - transaction to be invoked
|
|
36
36
|
* @param blockId
|
|
37
37
|
* @returns the result of the function on the smart contract.
|
|
38
38
|
*/
|
|
39
39
|
public abstract callContract(
|
|
40
|
-
|
|
40
|
+
invokeTransaction: CallContractTransaction,
|
|
41
41
|
blockId?: number
|
|
42
42
|
): Promise<CallContractResponse>;
|
|
43
43
|
|
|
@@ -104,10 +104,10 @@ export abstract class ProviderInterface {
|
|
|
104
104
|
*
|
|
105
105
|
* [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/gateway/gateway_client.py#L13-L17)
|
|
106
106
|
*
|
|
107
|
-
* @param
|
|
107
|
+
* @param transaction - transaction to be invoked
|
|
108
108
|
* @returns a confirmation of invoking a function on the starknet contract
|
|
109
109
|
*/
|
|
110
|
-
public abstract addTransaction(
|
|
110
|
+
public abstract addTransaction(transaction: Transaction): Promise<AddTransactionResponse>;
|
|
111
111
|
|
|
112
112
|
/**
|
|
113
113
|
* Deploys a given compiled contract (json) to starknet
|
package/src/signer/default.ts
CHANGED
|
@@ -25,26 +25,35 @@ export class Signer extends Provider implements SignerInterface {
|
|
|
25
25
|
*
|
|
26
26
|
* [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/gateway/gateway_client.py#L13-L17)
|
|
27
27
|
*
|
|
28
|
-
* @param
|
|
28
|
+
* @param transaction - transaction to be invoked
|
|
29
29
|
* @returns a confirmation of invoking a function on the starknet contract
|
|
30
30
|
*/
|
|
31
|
-
public override async addTransaction(
|
|
32
|
-
if (
|
|
31
|
+
public override async addTransaction(transaction: Transaction): Promise<AddTransactionResponse> {
|
|
32
|
+
if (transaction.type === 'DEPLOY') return super.addTransaction(transaction);
|
|
33
33
|
|
|
34
|
-
assert(
|
|
34
|
+
assert(
|
|
35
|
+
!transaction.signature,
|
|
36
|
+
"Adding signatures to a signer transaction currently isn't supported"
|
|
37
|
+
);
|
|
35
38
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
|
|
39
|
+
let nonceBn;
|
|
40
|
+
if (transaction.nonce) {
|
|
41
|
+
nonceBn = toBN(transaction.nonce);
|
|
42
|
+
} else {
|
|
43
|
+
const { result } = await this.callContract({
|
|
44
|
+
contract_address: this.address,
|
|
45
|
+
entry_point_selector: getSelectorFromName('get_nonce'),
|
|
46
|
+
});
|
|
47
|
+
nonceBn = toBN(result[0]);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const calldataDecimal = (transaction.calldata || []).map((x) => toBN(x).toString());
|
|
42
51
|
|
|
43
52
|
const msgHash = addHexPrefix(
|
|
44
53
|
hashMessage(
|
|
45
54
|
this.address,
|
|
46
|
-
|
|
47
|
-
|
|
55
|
+
transaction.contract_address,
|
|
56
|
+
transaction.entry_point_selector,
|
|
48
57
|
calldataDecimal,
|
|
49
58
|
nonceBn.toString()
|
|
50
59
|
)
|
|
@@ -56,8 +65,8 @@ export class Signer extends Provider implements SignerInterface {
|
|
|
56
65
|
type: 'INVOKE_FUNCTION',
|
|
57
66
|
entry_point_selector: getSelectorFromName('execute'),
|
|
58
67
|
calldata: [
|
|
59
|
-
|
|
60
|
-
|
|
68
|
+
transaction.contract_address,
|
|
69
|
+
transaction.entry_point_selector,
|
|
61
70
|
calldataDecimal.length.toString(),
|
|
62
71
|
...calldataDecimal,
|
|
63
72
|
nonceBn.toString(),
|
package/src/signer/interface.ts
CHANGED
|
@@ -8,8 +8,10 @@ export abstract class SignerInterface extends Provider {
|
|
|
8
8
|
*
|
|
9
9
|
* [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/gateway/gateway_client.py#L13-L17)
|
|
10
10
|
*
|
|
11
|
-
* @param
|
|
11
|
+
* @param transaction - transaction to be invoked
|
|
12
12
|
* @returns a confirmation of invoking a function on the starknet contract
|
|
13
13
|
*/
|
|
14
|
-
public abstract override addTransaction(
|
|
14
|
+
public abstract override addTransaction(
|
|
15
|
+
transaction: Transaction
|
|
16
|
+
): Promise<AddTransactionResponse>;
|
|
15
17
|
}
|
package/src/types.ts
CHANGED
|
@@ -11,7 +11,7 @@ export type GetContractAddressesResponse = {
|
|
|
11
11
|
};
|
|
12
12
|
|
|
13
13
|
export type Status = 'NOT_RECEIVED' | 'RECEIVED' | 'PENDING' | 'REJECTED' | 'ACCEPTED_ONCHAIN';
|
|
14
|
-
export type
|
|
14
|
+
export type TransactionStatus = 'TRANSACTION_RECEIVED';
|
|
15
15
|
export type Type = 'DEPLOY' | 'INVOKE_FUNCTION';
|
|
16
16
|
export type EntryPointType = 'EXTERNAL';
|
|
17
17
|
export type CompressedProgram = string;
|
|
@@ -51,6 +51,7 @@ export type DeployTransaction = {
|
|
|
51
51
|
contract_definition: CompressedCompiledContract;
|
|
52
52
|
contract_address_salt: BigNumberish;
|
|
53
53
|
constructor_calldata: string[];
|
|
54
|
+
nonce?: BigNumberish;
|
|
54
55
|
};
|
|
55
56
|
|
|
56
57
|
export type InvokeFunctionTransaction = {
|
|
@@ -60,6 +61,7 @@ export type InvokeFunctionTransaction = {
|
|
|
60
61
|
entry_point_type?: EntryPointType;
|
|
61
62
|
entry_point_selector: string;
|
|
62
63
|
calldata?: string[];
|
|
64
|
+
nonce?: BigNumberish;
|
|
63
65
|
};
|
|
64
66
|
|
|
65
67
|
export type CallContractTransaction = Omit<InvokeFunctionTransaction, 'type'>;
|
|
@@ -116,7 +118,7 @@ export type GetTransactionResponse = {
|
|
|
116
118
|
};
|
|
117
119
|
|
|
118
120
|
export type AddTransactionResponse = {
|
|
119
|
-
code:
|
|
121
|
+
code: TransactionStatus;
|
|
120
122
|
transaction_hash: string;
|
|
121
123
|
address?: string;
|
|
122
124
|
};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { addHexPrefix, removeHexPrefix } from './encode';
|
|
2
|
+
|
|
3
|
+
export function isASCII(str: string) {
|
|
4
|
+
// eslint-disable-next-line no-control-regex
|
|
5
|
+
return /^[\x00-\x7F]*$/.test(str);
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
// function to check if string has less or equal 31 characters
|
|
9
|
+
export function isShortString(str: string) {
|
|
10
|
+
return str.length <= 31;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function encodeShortString(str: string) {
|
|
14
|
+
if (!isASCII(str)) throw new Error(`${str} is not an ASCII string`);
|
|
15
|
+
if (!isShortString(str)) throw new Error(`${str} is too long`);
|
|
16
|
+
return addHexPrefix(str.replace(/./g, (char) => char.charCodeAt(0).toString(16)));
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function decodeShortString(str: string) {
|
|
20
|
+
return removeHexPrefix(str).replace(/.{2}/g, (hex) => String.fromCharCode(parseInt(hex, 16)));
|
|
21
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { addHexPrefix } from './encode';
|
|
2
|
+
import { BigNumberish, toBN } from './number';
|
|
3
|
+
|
|
4
|
+
// Represents an integer in the range [0, 2^256).
|
|
5
|
+
export interface Uint256 {
|
|
6
|
+
// The low 128 bits of the value.
|
|
7
|
+
low: BigNumberish;
|
|
8
|
+
// The high 128 bits of the value.
|
|
9
|
+
high: BigNumberish;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
// function to convert Uint256 to BN
|
|
13
|
+
export function uint256ToBN(uint256: Uint256) {
|
|
14
|
+
return toBN(uint256.high).shln(128).add(toBN(uint256.low));
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export const UINT_128_MAX = toBN(1).shln(128).sub(toBN(1));
|
|
18
|
+
export const UINT_256_MAX = toBN(1).shln(256).sub(toBN(1));
|
|
19
|
+
// function to check if BN is smaller or equal 2**256-1
|
|
20
|
+
export function isUint256(bn: BigNumberish): boolean {
|
|
21
|
+
return toBN(bn).lte(UINT_256_MAX);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// function to convert BN to Uint256
|
|
25
|
+
export function bnToUint256(bignumber: BigNumberish): Uint256 {
|
|
26
|
+
const bn = toBN(bignumber);
|
|
27
|
+
if (!isUint256(bn)) throw new Error('Number is too large');
|
|
28
|
+
return {
|
|
29
|
+
low: addHexPrefix(bn.maskn(128).toString(16)),
|
|
30
|
+
high: addHexPrefix(bn.shrn(128).toString(16)),
|
|
31
|
+
};
|
|
32
|
+
}
|
package/types.d.ts
CHANGED
|
@@ -13,7 +13,7 @@ export declare type Status =
|
|
|
13
13
|
| 'PENDING'
|
|
14
14
|
| 'REJECTED'
|
|
15
15
|
| 'ACCEPTED_ONCHAIN';
|
|
16
|
-
export declare type
|
|
16
|
+
export declare type TransactionStatus = 'TRANSACTION_RECEIVED';
|
|
17
17
|
export declare type Type = 'DEPLOY' | 'INVOKE_FUNCTION';
|
|
18
18
|
export declare type EntryPointType = 'EXTERNAL';
|
|
19
19
|
export declare type CompressedProgram = string;
|
|
@@ -50,6 +50,7 @@ export declare type DeployTransaction = {
|
|
|
50
50
|
contract_definition: CompressedCompiledContract;
|
|
51
51
|
contract_address_salt: BigNumberish;
|
|
52
52
|
constructor_calldata: string[];
|
|
53
|
+
nonce?: BigNumberish;
|
|
53
54
|
};
|
|
54
55
|
export declare type InvokeFunctionTransaction = {
|
|
55
56
|
type: 'INVOKE_FUNCTION';
|
|
@@ -58,6 +59,7 @@ export declare type InvokeFunctionTransaction = {
|
|
|
58
59
|
entry_point_type?: EntryPointType;
|
|
59
60
|
entry_point_selector: string;
|
|
60
61
|
calldata?: string[];
|
|
62
|
+
nonce?: BigNumberish;
|
|
61
63
|
};
|
|
62
64
|
export declare type CallContractTransaction = Omit<InvokeFunctionTransaction, 'type'>;
|
|
63
65
|
export declare type Transaction = DeployTransaction | InvokeFunctionTransaction;
|
|
@@ -106,7 +108,7 @@ export declare type GetTransactionResponse = {
|
|
|
106
108
|
transaction_hash: string;
|
|
107
109
|
};
|
|
108
110
|
export declare type AddTransactionResponse = {
|
|
109
|
-
code:
|
|
111
|
+
code: TransactionStatus;
|
|
110
112
|
transaction_hash: string;
|
|
111
113
|
address?: string;
|
|
112
114
|
};
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
3
|
+
exports.decodeShortString =
|
|
4
|
+
exports.encodeShortString =
|
|
5
|
+
exports.isShortString =
|
|
6
|
+
exports.isASCII =
|
|
7
|
+
void 0;
|
|
8
|
+
var encode_1 = require('./encode');
|
|
9
|
+
function isASCII(str) {
|
|
10
|
+
// eslint-disable-next-line no-control-regex
|
|
11
|
+
return /^[\x00-\x7F]*$/.test(str);
|
|
12
|
+
}
|
|
13
|
+
exports.isASCII = isASCII;
|
|
14
|
+
// function to check if string has less or equal 31 characters
|
|
15
|
+
function isShortString(str) {
|
|
16
|
+
return str.length <= 31;
|
|
17
|
+
}
|
|
18
|
+
exports.isShortString = isShortString;
|
|
19
|
+
function encodeShortString(str) {
|
|
20
|
+
if (!isASCII(str)) throw new Error(str + ' is not an ASCII string');
|
|
21
|
+
if (!isShortString(str)) throw new Error(str + ' is too long');
|
|
22
|
+
return (0, encode_1.addHexPrefix)(
|
|
23
|
+
str.replace(/./g, function (char) {
|
|
24
|
+
return char.charCodeAt(0).toString(16);
|
|
25
|
+
})
|
|
26
|
+
);
|
|
27
|
+
}
|
|
28
|
+
exports.encodeShortString = encodeShortString;
|
|
29
|
+
function decodeShortString(str) {
|
|
30
|
+
return (0, encode_1.removeHexPrefix)(str).replace(/.{2}/g, function (hex) {
|
|
31
|
+
return String.fromCharCode(parseInt(hex, 16));
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
exports.decodeShortString = decodeShortString;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/// <reference types="bn.js" />
|
|
2
|
+
import { BigNumberish } from './number';
|
|
3
|
+
export interface Uint256 {
|
|
4
|
+
low: BigNumberish;
|
|
5
|
+
high: BigNumberish;
|
|
6
|
+
}
|
|
7
|
+
export declare function uint256ToBN(uint256: Uint256): import('bn.js');
|
|
8
|
+
export declare const UINT_128_MAX: import('bn.js');
|
|
9
|
+
export declare const UINT_256_MAX: import('bn.js');
|
|
10
|
+
export declare function isUint256(bn: BigNumberish): boolean;
|
|
11
|
+
export declare function bnToUint256(bignumber: BigNumberish): Uint256;
|
package/utils/uint256.js
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
3
|
+
exports.bnToUint256 =
|
|
4
|
+
exports.isUint256 =
|
|
5
|
+
exports.UINT_256_MAX =
|
|
6
|
+
exports.UINT_128_MAX =
|
|
7
|
+
exports.uint256ToBN =
|
|
8
|
+
void 0;
|
|
9
|
+
var encode_1 = require('./encode');
|
|
10
|
+
var number_1 = require('./number');
|
|
11
|
+
// function to convert Uint256 to BN
|
|
12
|
+
function uint256ToBN(uint256) {
|
|
13
|
+
return (0, number_1.toBN)(uint256.high)
|
|
14
|
+
.shln(128)
|
|
15
|
+
.add((0, number_1.toBN)(uint256.low));
|
|
16
|
+
}
|
|
17
|
+
exports.uint256ToBN = uint256ToBN;
|
|
18
|
+
exports.UINT_128_MAX = (0, number_1.toBN)(1)
|
|
19
|
+
.shln(128)
|
|
20
|
+
.sub((0, number_1.toBN)(1));
|
|
21
|
+
exports.UINT_256_MAX = (0, number_1.toBN)(1)
|
|
22
|
+
.shln(256)
|
|
23
|
+
.sub((0, number_1.toBN)(1));
|
|
24
|
+
// function to check if BN is smaller or equal 2**256-1
|
|
25
|
+
function isUint256(bn) {
|
|
26
|
+
return (0, number_1.toBN)(bn).lte(exports.UINT_256_MAX);
|
|
27
|
+
}
|
|
28
|
+
exports.isUint256 = isUint256;
|
|
29
|
+
// function to convert BN to Uint256
|
|
30
|
+
function bnToUint256(bignumber) {
|
|
31
|
+
var bn = (0, number_1.toBN)(bignumber);
|
|
32
|
+
if (!isUint256(bn)) throw new Error('Number is too large');
|
|
33
|
+
return {
|
|
34
|
+
low: (0, encode_1.addHexPrefix)(bn.maskn(128).toString(16)),
|
|
35
|
+
high: (0, encode_1.addHexPrefix)(bn.shrn(128).toString(16)),
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
exports.bnToUint256 = bnToUint256;
|