starknet 3.12.1 → 3.13.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/.github/workflows/pr.yml +3 -0
- package/.github/workflows/release.yml +4 -0
- package/CHANGELOG.md +39 -0
- package/README.md +2 -0
- package/__mocks__/Account.json +25468 -0
- package/__tests__/account.test.ts +102 -65
- package/__tests__/contract.test.ts +23 -65
- package/__tests__/fixtures.ts +21 -1
- package/__tests__/jest.setup.ts +23 -4
- package/__tests__/provider.test.ts +20 -1
- package/account/index.js +10 -6
- package/contract/default.js +20 -21
- package/contract/index.js +10 -6
- package/dist/account/index.js +5 -1
- package/dist/contract/default.js +18 -18
- package/dist/contract/index.js +5 -1
- package/dist/index.js +5 -1
- package/dist/provider/default.d.ts +1 -1
- package/dist/provider/default.js +35 -49
- package/dist/provider/index.js +5 -1
- package/dist/provider/interface.d.ts +1 -1
- package/dist/provider/utils.js +5 -5
- package/dist/signer/index.js +5 -1
- package/dist/types/api.d.ts +1 -1
- package/dist/types/index.js +5 -1
- package/dist/utils/ellipticCurve.js +1 -1
- package/dist/utils/encode.js +1 -1
- package/dist/utils/hash.js +1 -1
- package/dist/utils/number.js +8 -4
- package/dist/utils/shortString.js +2 -2
- package/dist/utils/typedData/index.d.ts +2 -36
- package/dist/utils/typedData/index.js +8 -4
- package/dist/utils/typedData/types.d.ts +15 -70
- package/dist/utils/typedData/types.js +0 -45
- package/dist/utils/typedData/utils.d.ts +2 -18
- package/dist/utils/typedData/utils.js +4 -3
- package/index.js +10 -6
- package/package.json +33 -31
- package/provider/default.d.ts +1 -1
- package/provider/default.js +44 -65
- package/provider/index.js +10 -6
- package/provider/interface.d.ts +1 -1
- package/provider/utils.js +5 -5
- package/signer/index.js +10 -6
- package/src/provider/default.ts +24 -31
- package/src/provider/interface.ts +1 -1
- package/src/types/api.ts +1 -1
- package/src/utils/typedData/types.ts +15 -68
- package/src/utils/typedData/utils.ts +7 -4
- package/types/api.d.ts +1 -1
- package/types/index.js +10 -6
- package/utils/ellipticCurve.js +1 -1
- package/utils/encode.js +1 -1
- package/utils/hash.js +1 -1
- package/utils/number.js +13 -9
- package/utils/shortString.js +2 -2
- package/utils/typedData/index.d.ts +2 -46
- package/utils/typedData/index.js +15 -13
- package/utils/typedData/types.d.ts +15 -91
- package/utils/typedData/types.js +0 -55
- package/utils/typedData/utils.d.ts +2 -21
- package/utils/typedData/utils.js +4 -3
- package/www/guides/account.md +21 -7
- package/www/guides/erc20.md +15 -27
- package/__tests__/accountContract.test.ts +0 -110
package/src/provider/default.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import fetch from 'cross-fetch';
|
|
2
2
|
import urljoin from 'url-join';
|
|
3
3
|
|
|
4
4
|
import { StarknetChainId } from '../constants';
|
|
@@ -10,7 +10,6 @@ import {
|
|
|
10
10
|
CompiledContract,
|
|
11
11
|
DeployContractPayload,
|
|
12
12
|
Endpoints,
|
|
13
|
-
EstimateFeeResponse,
|
|
14
13
|
GetBlockResponse,
|
|
15
14
|
GetCodeResponse,
|
|
16
15
|
GetContractAddressesResponse,
|
|
@@ -32,7 +31,9 @@ type NetworkName = 'mainnet-alpha' | 'goerli-alpha';
|
|
|
32
31
|
type ProviderOptions = { network: NetworkName } | { baseUrl: string };
|
|
33
32
|
|
|
34
33
|
function wait(delay: number) {
|
|
35
|
-
return new Promise((res) =>
|
|
34
|
+
return new Promise((res) => {
|
|
35
|
+
setTimeout(res, delay);
|
|
36
|
+
});
|
|
36
37
|
}
|
|
37
38
|
|
|
38
39
|
function isEmptyQueryObject(obj?: Record<any, any>): obj is undefined {
|
|
@@ -125,7 +126,7 @@ export class Provider implements ProviderInterface {
|
|
|
125
126
|
return `?${queryString}`;
|
|
126
127
|
}
|
|
127
128
|
|
|
128
|
-
private getHeaders(method: 'POST' | 'GET'):
|
|
129
|
+
private getHeaders(method: 'POST' | 'GET'): Record<string, string> | undefined {
|
|
129
130
|
if (method === 'POST') {
|
|
130
131
|
return {
|
|
131
132
|
'Content-Type': 'application/json',
|
|
@@ -150,33 +151,25 @@ export class Provider implements ProviderInterface {
|
|
|
150
151
|
const method = this.getFetchMethod(endpoint);
|
|
151
152
|
const queryString = this.getQueryString(query);
|
|
152
153
|
const headers = this.getHeaders(method);
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
154
|
+
const url = urljoin(baseUrl, endpoint, queryString);
|
|
155
|
+
|
|
156
|
+
return fetch(url, {
|
|
157
|
+
method,
|
|
158
|
+
body: stringify(request),
|
|
159
|
+
headers,
|
|
160
|
+
})
|
|
161
|
+
.then((res) => res.text())
|
|
162
|
+
.then((res) => {
|
|
163
|
+
if (endpoint === 'estimate_fee') {
|
|
164
|
+
return parse(res, (_, v) => {
|
|
165
|
+
if (v && typeof v === 'bigint') {
|
|
166
|
+
return toBN(v.toString());
|
|
167
|
+
}
|
|
168
|
+
return v;
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
return parse(res) as Endpoints[T]['RESPONSE'];
|
|
171
172
|
});
|
|
172
|
-
return data;
|
|
173
|
-
} catch (error: any) {
|
|
174
|
-
const data = error?.response?.data;
|
|
175
|
-
if (data?.message) {
|
|
176
|
-
throw new Error(`${data.code}: ${data.message}`);
|
|
177
|
-
}
|
|
178
|
-
throw error;
|
|
179
|
-
}
|
|
180
173
|
}
|
|
181
174
|
|
|
182
175
|
/**
|
|
@@ -259,7 +252,7 @@ export class Provider implements ProviderInterface {
|
|
|
259
252
|
*/
|
|
260
253
|
public async getStorageAt(
|
|
261
254
|
contractAddress: string,
|
|
262
|
-
key:
|
|
255
|
+
key: BigNumberish,
|
|
263
256
|
blockIdentifier: BlockIdentifier = 'pending'
|
|
264
257
|
): Promise<object> {
|
|
265
258
|
return this.fetchEndpoint('get_storage_at', { blockIdentifier, contractAddress, key });
|
package/src/types/api.ts
CHANGED
|
@@ -1,82 +1,29 @@
|
|
|
1
|
-
import {
|
|
2
|
-
Infer,
|
|
3
|
-
array,
|
|
4
|
-
intersection,
|
|
5
|
-
number,
|
|
6
|
-
object,
|
|
7
|
-
optional,
|
|
8
|
-
record,
|
|
9
|
-
refine,
|
|
10
|
-
string,
|
|
11
|
-
type as t,
|
|
12
|
-
union,
|
|
13
|
-
} from 'superstruct';
|
|
14
|
-
|
|
15
|
-
export const ATOMIC_TYPES = ['felt', 'felt*'];
|
|
16
|
-
|
|
17
|
-
// Source: https://github.com/Mrtenz/eip-712/blob/master/src/eip-712.ts
|
|
18
|
-
// and modified to support starknet types
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* Checks if a type is valid with the given `typedData`. The following types are valid:
|
|
22
|
-
* - Atomic types: felt, felt*
|
|
23
|
-
* - Reference types: struct type (e.g. SomeStruct)
|
|
24
|
-
*
|
|
25
|
-
* @param {Record<string, unknown>} types
|
|
26
|
-
* @param {string} type
|
|
27
|
-
* @return {boolean}
|
|
28
|
-
*/
|
|
29
|
-
export const isValidType = (types: Record<string, unknown>, type: string): boolean => {
|
|
30
|
-
if (ATOMIC_TYPES.includes(type as string)) {
|
|
31
|
-
return true;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
if (types[type]) {
|
|
35
|
-
return true;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
return false;
|
|
39
|
-
};
|
|
40
|
-
|
|
41
|
-
const TYPE = refine(string(), 'Type', (type, context) => {
|
|
42
|
-
return isValidType(context.branch[0].types, type);
|
|
43
|
-
});
|
|
44
|
-
|
|
45
|
-
export const STARKNET_TYPE = object({
|
|
46
|
-
name: string(),
|
|
47
|
-
type: TYPE,
|
|
48
|
-
});
|
|
49
|
-
|
|
50
1
|
/**
|
|
51
2
|
* A single type, as part of a struct. The `type` field can be any of the EIP-712 supported types.
|
|
52
3
|
*
|
|
53
4
|
* Note that the `uint` and `int` aliases like in Solidity, and fixed point numbers are not supported by the EIP-712
|
|
54
5
|
* standard.
|
|
55
6
|
*/
|
|
56
|
-
export
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
version: optional(string()),
|
|
61
|
-
chainId: optional(union([string(), number()])),
|
|
62
|
-
});
|
|
7
|
+
export interface StarkNetType {
|
|
8
|
+
name: string;
|
|
9
|
+
type: 'felt' | 'felt*' | string;
|
|
10
|
+
}
|
|
63
11
|
|
|
64
12
|
/**
|
|
65
13
|
* The EIP712 domain struct. Any of these fields are optional, but it must contain at least one field.
|
|
66
14
|
*/
|
|
67
|
-
export
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
record(string(), array(STARKNET_TYPE)),
|
|
73
|
-
]),
|
|
74
|
-
primaryType: string(),
|
|
75
|
-
domain: STARKNET_DOMAIN_TYPE,
|
|
76
|
-
message: object(),
|
|
77
|
-
});
|
|
15
|
+
export interface StarkNetDomain extends Record<string, unknown> {
|
|
16
|
+
name?: string;
|
|
17
|
+
version?: string;
|
|
18
|
+
chainId?: string | number;
|
|
19
|
+
}
|
|
78
20
|
|
|
79
21
|
/**
|
|
80
22
|
* The complete typed data, with all the structs, domain data, primary type of the message, and the message itself.
|
|
81
23
|
*/
|
|
82
|
-
export
|
|
24
|
+
export interface TypedData {
|
|
25
|
+
types: Record<string, StarkNetType[]>;
|
|
26
|
+
primaryType: string;
|
|
27
|
+
domain: StarkNetDomain;
|
|
28
|
+
message: Record<string, unknown>;
|
|
29
|
+
}
|
|
@@ -1,6 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
import { STARKNET_TYPED_DATA_TYPE, TypedData } from './types';
|
|
1
|
+
import { TypedData } from './types';
|
|
4
2
|
|
|
5
3
|
/**
|
|
6
4
|
* Validates that `data` matches the EIP-712 JSON schema.
|
|
@@ -9,5 +7,10 @@ import { STARKNET_TYPED_DATA_TYPE, TypedData } from './types';
|
|
|
9
7
|
* @return {boolean}
|
|
10
8
|
*/
|
|
11
9
|
export const validateTypedData = (data: unknown): data is TypedData => {
|
|
12
|
-
|
|
10
|
+
const typedData = data as TypedData;
|
|
11
|
+
|
|
12
|
+
// Validate that the data matches the EIP-712 JSON schema
|
|
13
|
+
const valid = Boolean(typedData.types && typedData.primaryType && typedData.message);
|
|
14
|
+
|
|
15
|
+
return valid;
|
|
13
16
|
};
|
package/types/api.d.ts
CHANGED
package/types/index.js
CHANGED
|
@@ -4,12 +4,16 @@ var __createBinding =
|
|
|
4
4
|
(Object.create
|
|
5
5
|
? function (o, m, k, k2) {
|
|
6
6
|
if (k2 === undefined) k2 = k;
|
|
7
|
-
Object.
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
7
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
8
|
+
if (!desc || ('get' in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
9
|
+
desc = {
|
|
10
|
+
enumerable: true,
|
|
11
|
+
get: function () {
|
|
12
|
+
return m[k];
|
|
13
|
+
},
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
Object.defineProperty(o, k2, desc);
|
|
13
17
|
}
|
|
14
18
|
: function (o, m, k, k2) {
|
|
15
19
|
if (k2 === undefined) k2 = k;
|
package/utils/ellipticCurve.js
CHANGED
|
@@ -68,7 +68,7 @@ function fixMessage(msg) {
|
|
|
68
68
|
}
|
|
69
69
|
(0, minimalistic_assert_1.default)(pureHex.length === 63);
|
|
70
70
|
// In this case delta will be 4 so we perform a shift-left of 4 bits by adding a ZERO_BN.
|
|
71
|
-
return pureHex
|
|
71
|
+
return ''.concat(pureHex, '0');
|
|
72
72
|
}
|
|
73
73
|
exports.genKeyPair = exports.ec.genKeyPair.bind(exports.ec);
|
|
74
74
|
function getKeyPair(pk) {
|
package/utils/encode.js
CHANGED
|
@@ -76,7 +76,7 @@ function removeHexPrefix(hex) {
|
|
|
76
76
|
}
|
|
77
77
|
exports.removeHexPrefix = removeHexPrefix;
|
|
78
78
|
function addHexPrefix(hex) {
|
|
79
|
-
return '0x'
|
|
79
|
+
return '0x'.concat(removeHexPrefix(hex));
|
|
80
80
|
}
|
|
81
81
|
exports.addHexPrefix = addHexPrefix;
|
|
82
82
|
function padString(str, length, left, padding) {
|
package/utils/hash.js
CHANGED
|
@@ -97,7 +97,7 @@ function pedersen(input) {
|
|
|
97
97
|
(0, minimalistic_assert_1.default)(
|
|
98
98
|
x.gte(constants_1.ZERO) &&
|
|
99
99
|
x.lt((0, number_1.toBN)((0, encode_1.addHexPrefix)(constants_1.FIELD_PRIME))),
|
|
100
|
-
'Invalid input: '
|
|
100
|
+
'Invalid input: '.concat(input[i])
|
|
101
101
|
);
|
|
102
102
|
for (var j = 0; j < 252; j += 1) {
|
|
103
103
|
var pt = constantPoints[2 + i * 252 + j];
|
package/utils/number.js
CHANGED
|
@@ -4,12 +4,16 @@ var __createBinding =
|
|
|
4
4
|
(Object.create
|
|
5
5
|
? function (o, m, k, k2) {
|
|
6
6
|
if (k2 === undefined) k2 = k;
|
|
7
|
-
Object.
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
7
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
8
|
+
if (!desc || ('get' in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
9
|
+
desc = {
|
|
10
|
+
enumerable: true,
|
|
11
|
+
get: function () {
|
|
12
|
+
return m[k];
|
|
13
|
+
},
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
Object.defineProperty(o, k2, desc);
|
|
13
17
|
}
|
|
14
18
|
: function (o, m, k, k2) {
|
|
15
19
|
if (k2 === undefined) k2 = k;
|
|
@@ -68,7 +72,7 @@ function toHex(number) {
|
|
|
68
72
|
}
|
|
69
73
|
exports.toHex = toHex;
|
|
70
74
|
function hexToDecimalString(hex) {
|
|
71
|
-
return toBN('0x'
|
|
75
|
+
return toBN('0x'.concat(hex.replace(/^0x/, ''))).toString();
|
|
72
76
|
}
|
|
73
77
|
exports.hexToDecimalString = hexToDecimalString;
|
|
74
78
|
function toFelt(num) {
|
|
@@ -88,11 +92,11 @@ function assertInRange(input, lowerBound, upperBound, inputName) {
|
|
|
88
92
|
if (inputName === void 0) {
|
|
89
93
|
inputName = '';
|
|
90
94
|
}
|
|
91
|
-
var messageSuffix = inputName === '' ? 'invalid length' : 'invalid '
|
|
95
|
+
var messageSuffix = inputName === '' ? 'invalid length' : 'invalid '.concat(inputName, ' length');
|
|
92
96
|
var inputBn = toBN(input);
|
|
93
97
|
(0, minimalistic_assert_1.default)(
|
|
94
98
|
inputBn.gte(toBN(lowerBound)) && inputBn.lt(toBN(upperBound)),
|
|
95
|
-
'Message not signable, '
|
|
99
|
+
'Message not signable, '.concat(messageSuffix, '.')
|
|
96
100
|
);
|
|
97
101
|
}
|
|
98
102
|
exports.assertInRange = assertInRange;
|
package/utils/shortString.js
CHANGED
|
@@ -17,8 +17,8 @@ function isShortString(str) {
|
|
|
17
17
|
}
|
|
18
18
|
exports.isShortString = isShortString;
|
|
19
19
|
function encodeShortString(str) {
|
|
20
|
-
if (!isASCII(str)) throw new Error(str
|
|
21
|
-
if (!isShortString(str)) throw new Error(str
|
|
20
|
+
if (!isASCII(str)) throw new Error(''.concat(str, ' is not an ASCII string'));
|
|
21
|
+
if (!isShortString(str)) throw new Error(''.concat(str, ' is too long'));
|
|
22
22
|
return (0, encode_1.addHexPrefix)(
|
|
23
23
|
str.replace(/./g, function (char) {
|
|
24
24
|
return char.charCodeAt(0).toString(16);
|
|
@@ -39,29 +39,7 @@ export declare const getTypeHash: (typedData: TypedData, type: string) => string
|
|
|
39
39
|
* @param {string} type
|
|
40
40
|
* @param {Record<string, any>} data
|
|
41
41
|
*/
|
|
42
|
-
export declare const encodeData: <
|
|
43
|
-
T extends {
|
|
44
|
-
types: {
|
|
45
|
-
StarkNetDomain: {
|
|
46
|
-
type: string;
|
|
47
|
-
name: string;
|
|
48
|
-
}[];
|
|
49
|
-
} & Record<
|
|
50
|
-
string,
|
|
51
|
-
{
|
|
52
|
-
type: string;
|
|
53
|
-
name: string;
|
|
54
|
-
}[]
|
|
55
|
-
>;
|
|
56
|
-
primaryType: string;
|
|
57
|
-
domain: {
|
|
58
|
-
version?: string | undefined;
|
|
59
|
-
chainId?: string | number | undefined;
|
|
60
|
-
name?: string | undefined;
|
|
61
|
-
};
|
|
62
|
-
message: Record<string, unknown>;
|
|
63
|
-
}
|
|
64
|
-
>(
|
|
42
|
+
export declare const encodeData: <T extends TypedData>(
|
|
65
43
|
typedData: T,
|
|
66
44
|
type: string,
|
|
67
45
|
data: T['message']
|
|
@@ -75,29 +53,7 @@ export declare const encodeData: <
|
|
|
75
53
|
* @param {Record<string, any>} data
|
|
76
54
|
* @return {Buffer}
|
|
77
55
|
*/
|
|
78
|
-
export declare const getStructHash: <
|
|
79
|
-
T extends {
|
|
80
|
-
types: {
|
|
81
|
-
StarkNetDomain: {
|
|
82
|
-
type: string;
|
|
83
|
-
name: string;
|
|
84
|
-
}[];
|
|
85
|
-
} & Record<
|
|
86
|
-
string,
|
|
87
|
-
{
|
|
88
|
-
type: string;
|
|
89
|
-
name: string;
|
|
90
|
-
}[]
|
|
91
|
-
>;
|
|
92
|
-
primaryType: string;
|
|
93
|
-
domain: {
|
|
94
|
-
version?: string | undefined;
|
|
95
|
-
chainId?: string | number | undefined;
|
|
96
|
-
name?: string | undefined;
|
|
97
|
-
};
|
|
98
|
-
message: Record<string, unknown>;
|
|
99
|
-
}
|
|
100
|
-
>(
|
|
56
|
+
export declare const getStructHash: <T extends TypedData>(
|
|
101
57
|
typedData: T,
|
|
102
58
|
type: string,
|
|
103
59
|
data: T['message']
|
package/utils/typedData/index.js
CHANGED
|
@@ -4,12 +4,16 @@ var __createBinding =
|
|
|
4
4
|
(Object.create
|
|
5
5
|
? function (o, m, k, k2) {
|
|
6
6
|
if (k2 === undefined) k2 = k;
|
|
7
|
-
Object.
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
7
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
8
|
+
if (!desc || ('get' in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
9
|
+
desc = {
|
|
10
|
+
enumerable: true,
|
|
11
|
+
get: function () {
|
|
12
|
+
return m[k];
|
|
13
|
+
},
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
Object.defineProperty(o, k2, desc);
|
|
13
17
|
}
|
|
14
18
|
: function (o, m, k, k2) {
|
|
15
19
|
if (k2 === undefined) k2 = k;
|
|
@@ -76,7 +80,7 @@ function getHex(value) {
|
|
|
76
80
|
if (typeof value === 'string') {
|
|
77
81
|
return (0, number_1.toHex)((0, number_1.toBN)((0, shortString_1.encodeShortString)(value)));
|
|
78
82
|
}
|
|
79
|
-
throw new Error('Invalid BigNumberish: '
|
|
83
|
+
throw new Error('Invalid BigNumberish: '.concat(value));
|
|
80
84
|
}
|
|
81
85
|
}
|
|
82
86
|
/**
|
|
@@ -135,12 +139,10 @@ var encodeType = function (typedData, type) {
|
|
|
135
139
|
var types = __spreadArray([primary], __read(dependencies.sort()), false);
|
|
136
140
|
return types
|
|
137
141
|
.map(function (dependency) {
|
|
138
|
-
return (
|
|
139
|
-
dependency +
|
|
140
|
-
'(' +
|
|
142
|
+
return ''.concat(dependency, '(').concat(
|
|
141
143
|
typedData.types[dependency].map(function (t) {
|
|
142
|
-
return t.name
|
|
143
|
-
})
|
|
144
|
+
return ''.concat(t.name, ':').concat(t.type);
|
|
145
|
+
}),
|
|
144
146
|
')'
|
|
145
147
|
);
|
|
146
148
|
})
|
|
@@ -193,7 +195,7 @@ var encodeData = function (typedData, type, data) {
|
|
|
193
195
|
ts = _b[0],
|
|
194
196
|
vs = _b[1];
|
|
195
197
|
if (data[field.name] === undefined || data[field.name] === null) {
|
|
196
|
-
throw new Error("Cannot encode data: missing data for '"
|
|
198
|
+
throw new Error("Cannot encode data: missing data for '".concat(field.name, "'"));
|
|
197
199
|
}
|
|
198
200
|
var value = data[field.name];
|
|
199
201
|
var _c = __read(encodeValue(typedData, field.type, value), 2),
|
|
@@ -1,103 +1,27 @@
|
|
|
1
|
-
import { Infer } from 'superstruct';
|
|
2
|
-
export declare const ATOMIC_TYPES: string[];
|
|
3
|
-
/**
|
|
4
|
-
* Checks if a type is valid with the given `typedData`. The following types are valid:
|
|
5
|
-
* - Atomic types: felt, felt*
|
|
6
|
-
* - Reference types: struct type (e.g. SomeStruct)
|
|
7
|
-
*
|
|
8
|
-
* @param {Record<string, unknown>} types
|
|
9
|
-
* @param {string} type
|
|
10
|
-
* @return {boolean}
|
|
11
|
-
*/
|
|
12
|
-
export declare const isValidType: (types: Record<string, unknown>, type: string) => boolean;
|
|
13
|
-
export declare const STARKNET_TYPE: import('superstruct').Struct<
|
|
14
|
-
{
|
|
15
|
-
type: string;
|
|
16
|
-
name: string;
|
|
17
|
-
},
|
|
18
|
-
{
|
|
19
|
-
name: import('superstruct').Struct<string, null>;
|
|
20
|
-
type: import('superstruct').Struct<string, null>;
|
|
21
|
-
}
|
|
22
|
-
>;
|
|
23
1
|
/**
|
|
24
2
|
* A single type, as part of a struct. The `type` field can be any of the EIP-712 supported types.
|
|
25
3
|
*
|
|
26
4
|
* Note that the `uint` and `int` aliases like in Solidity, and fixed point numbers are not supported by the EIP-712
|
|
27
5
|
* standard.
|
|
28
6
|
*/
|
|
29
|
-
export
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
chainId?: string | number | undefined;
|
|
34
|
-
name?: string | undefined;
|
|
35
|
-
},
|
|
36
|
-
{
|
|
37
|
-
name: import('superstruct').Struct<string | undefined, null>;
|
|
38
|
-
version: import('superstruct').Struct<string | undefined, null>;
|
|
39
|
-
chainId: import('superstruct').Struct<string | number | undefined, null>;
|
|
40
|
-
}
|
|
41
|
-
>;
|
|
7
|
+
export interface StarkNetType {
|
|
8
|
+
name: string;
|
|
9
|
+
type: 'felt' | 'felt*' | string;
|
|
10
|
+
}
|
|
42
11
|
/**
|
|
43
12
|
* The EIP712 domain struct. Any of these fields are optional, but it must contain at least one field.
|
|
44
13
|
*/
|
|
45
|
-
export
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
type: string;
|
|
51
|
-
name: string;
|
|
52
|
-
}[];
|
|
53
|
-
} & Record<
|
|
54
|
-
string,
|
|
55
|
-
{
|
|
56
|
-
type: string;
|
|
57
|
-
name: string;
|
|
58
|
-
}[]
|
|
59
|
-
>;
|
|
60
|
-
primaryType: string;
|
|
61
|
-
domain: {
|
|
62
|
-
version?: string | undefined;
|
|
63
|
-
chainId?: string | number | undefined;
|
|
64
|
-
name?: string | undefined;
|
|
65
|
-
};
|
|
66
|
-
message: Record<string, unknown>;
|
|
67
|
-
},
|
|
68
|
-
{
|
|
69
|
-
types: import('superstruct').Struct<
|
|
70
|
-
{
|
|
71
|
-
StarkNetDomain: {
|
|
72
|
-
type: string;
|
|
73
|
-
name: string;
|
|
74
|
-
}[];
|
|
75
|
-
} & Record<
|
|
76
|
-
string,
|
|
77
|
-
{
|
|
78
|
-
type: string;
|
|
79
|
-
name: string;
|
|
80
|
-
}[]
|
|
81
|
-
>,
|
|
82
|
-
null
|
|
83
|
-
>;
|
|
84
|
-
primaryType: import('superstruct').Struct<string, null>;
|
|
85
|
-
domain: import('superstruct').Struct<
|
|
86
|
-
{
|
|
87
|
-
version?: string | undefined;
|
|
88
|
-
chainId?: string | number | undefined;
|
|
89
|
-
name?: string | undefined;
|
|
90
|
-
},
|
|
91
|
-
{
|
|
92
|
-
name: import('superstruct').Struct<string | undefined, null>;
|
|
93
|
-
version: import('superstruct').Struct<string | undefined, null>;
|
|
94
|
-
chainId: import('superstruct').Struct<string | number | undefined, null>;
|
|
95
|
-
}
|
|
96
|
-
>;
|
|
97
|
-
message: import('superstruct').Struct<Record<string, unknown>, null>;
|
|
98
|
-
}
|
|
99
|
-
>;
|
|
14
|
+
export interface StarkNetDomain extends Record<string, unknown> {
|
|
15
|
+
name?: string;
|
|
16
|
+
version?: string;
|
|
17
|
+
chainId?: string | number;
|
|
18
|
+
}
|
|
100
19
|
/**
|
|
101
20
|
* The complete typed data, with all the structs, domain data, primary type of the message, and the message itself.
|
|
102
21
|
*/
|
|
103
|
-
export
|
|
22
|
+
export interface TypedData {
|
|
23
|
+
types: Record<string, StarkNetType[]>;
|
|
24
|
+
primaryType: string;
|
|
25
|
+
domain: StarkNetDomain;
|
|
26
|
+
message: Record<string, unknown>;
|
|
27
|
+
}
|
package/utils/typedData/types.js
CHANGED
|
@@ -1,57 +1,2 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
3
|
-
exports.STARKNET_TYPED_DATA_TYPE =
|
|
4
|
-
exports.STARKNET_DOMAIN_TYPE =
|
|
5
|
-
exports.STARKNET_TYPE =
|
|
6
|
-
exports.isValidType =
|
|
7
|
-
exports.ATOMIC_TYPES =
|
|
8
|
-
void 0;
|
|
9
|
-
var superstruct_1 = require('superstruct');
|
|
10
|
-
exports.ATOMIC_TYPES = ['felt', 'felt*'];
|
|
11
|
-
// Source: https://github.com/Mrtenz/eip-712/blob/master/src/eip-712.ts
|
|
12
|
-
// and modified to support starknet types
|
|
13
|
-
/**
|
|
14
|
-
* Checks if a type is valid with the given `typedData`. The following types are valid:
|
|
15
|
-
* - Atomic types: felt, felt*
|
|
16
|
-
* - Reference types: struct type (e.g. SomeStruct)
|
|
17
|
-
*
|
|
18
|
-
* @param {Record<string, unknown>} types
|
|
19
|
-
* @param {string} type
|
|
20
|
-
* @return {boolean}
|
|
21
|
-
*/
|
|
22
|
-
var isValidType = function (types, type) {
|
|
23
|
-
if (exports.ATOMIC_TYPES.includes(type)) {
|
|
24
|
-
return true;
|
|
25
|
-
}
|
|
26
|
-
if (types[type]) {
|
|
27
|
-
return true;
|
|
28
|
-
}
|
|
29
|
-
return false;
|
|
30
|
-
};
|
|
31
|
-
exports.isValidType = isValidType;
|
|
32
|
-
var TYPE = (0, superstruct_1.refine)((0, superstruct_1.string)(), 'Type', function (type, context) {
|
|
33
|
-
return (0, exports.isValidType)(context.branch[0].types, type);
|
|
34
|
-
});
|
|
35
|
-
exports.STARKNET_TYPE = (0, superstruct_1.object)({
|
|
36
|
-
name: (0, superstruct_1.string)(),
|
|
37
|
-
type: TYPE,
|
|
38
|
-
});
|
|
39
|
-
exports.STARKNET_DOMAIN_TYPE = (0, superstruct_1.object)({
|
|
40
|
-
name: (0, superstruct_1.optional)((0, superstruct_1.string)()),
|
|
41
|
-
version: (0, superstruct_1.optional)((0, superstruct_1.string)()),
|
|
42
|
-
chainId: (0, superstruct_1.optional)(
|
|
43
|
-
(0, superstruct_1.union)([(0, superstruct_1.string)(), (0, superstruct_1.number)()])
|
|
44
|
-
),
|
|
45
|
-
});
|
|
46
|
-
exports.STARKNET_TYPED_DATA_TYPE = (0, superstruct_1.object)({
|
|
47
|
-
types: (0, superstruct_1.intersection)([
|
|
48
|
-
(0, superstruct_1.type)({ StarkNetDomain: (0, superstruct_1.array)(exports.STARKNET_TYPE) }),
|
|
49
|
-
(0, superstruct_1.record)(
|
|
50
|
-
(0, superstruct_1.string)(),
|
|
51
|
-
(0, superstruct_1.array)(exports.STARKNET_TYPE)
|
|
52
|
-
),
|
|
53
|
-
]),
|
|
54
|
-
primaryType: (0, superstruct_1.string)(),
|
|
55
|
-
domain: exports.STARKNET_DOMAIN_TYPE,
|
|
56
|
-
message: (0, superstruct_1.object)(),
|
|
57
|
-
});
|
|
@@ -1,27 +1,8 @@
|
|
|
1
|
+
import { TypedData } from './types';
|
|
1
2
|
/**
|
|
2
3
|
* Validates that `data` matches the EIP-712 JSON schema.
|
|
3
4
|
*
|
|
4
5
|
* @param {any} data
|
|
5
6
|
* @return {boolean}
|
|
6
7
|
*/
|
|
7
|
-
export declare const validateTypedData: (data: unknown) => data is
|
|
8
|
-
types: {
|
|
9
|
-
StarkNetDomain: {
|
|
10
|
-
type: string;
|
|
11
|
-
name: string;
|
|
12
|
-
}[];
|
|
13
|
-
} & Record<
|
|
14
|
-
string,
|
|
15
|
-
{
|
|
16
|
-
type: string;
|
|
17
|
-
name: string;
|
|
18
|
-
}[]
|
|
19
|
-
>;
|
|
20
|
-
primaryType: string;
|
|
21
|
-
domain: {
|
|
22
|
-
version?: string | undefined;
|
|
23
|
-
chainId?: string | number | undefined;
|
|
24
|
-
name?: string | undefined;
|
|
25
|
-
};
|
|
26
|
-
message: Record<string, unknown>;
|
|
27
|
-
};
|
|
8
|
+
export declare const validateTypedData: (data: unknown) => data is TypedData;
|