starknet 2.9.0 → 3.0.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/.eslintrc +3 -1
- package/CHANGELOG.md +24 -0
- package/README.md +16 -14
- package/__tests__/account.test.ts +52 -87
- package/__tests__/accountContract.test.ts +160 -0
- package/__tests__/contract.test.ts +3 -1
- package/__tests__/jest.setup.ts +9 -0
- package/__tests__/provider.test.ts +15 -30
- package/account/default.d.ts +66 -0
- package/account/default.js +440 -0
- package/account/index.d.ts +2 -0
- package/account/index.js +27 -0
- package/account/interface.d.ts +83 -0
- package/account/interface.js +37 -0
- package/contract.d.ts +6 -6
- package/contract.js +16 -14
- package/dist/account/default.d.ts +55 -0
- package/dist/account/default.js +272 -0
- package/dist/account/index.d.ts +2 -0
- package/dist/account/index.js +14 -0
- package/dist/account/interface.d.ts +69 -0
- package/dist/account/interface.js +27 -0
- package/dist/contract.d.ts +6 -6
- package/dist/contract.js +9 -12
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/provider/default.d.ts +23 -13
- package/dist/provider/default.js +150 -93
- package/dist/provider/interface.d.ts +22 -22
- package/dist/provider/utils.d.ts +4 -4
- package/dist/provider/utils.js +16 -6
- package/dist/signer/default.d.ts +7 -51
- package/dist/signer/default.js +24 -177
- package/dist/signer/index.d.ts +1 -1
- package/dist/signer/index.js +1 -1
- package/dist/signer/interface.d.ts +16 -37
- package/dist/signer/interface.js +2 -20
- package/dist/{types.d.ts → types/api.d.ts} +72 -41
- package/dist/{types.js → types/api.js} +0 -0
- package/dist/types/index.d.ts +3 -0
- package/dist/types/index.js +15 -0
- package/dist/types/lib.d.ts +57 -0
- package/dist/types/lib.js +2 -0
- package/dist/types/signer.d.ts +4 -0
- package/dist/types/signer.js +2 -0
- package/dist/utils/number.d.ts +1 -0
- package/dist/utils/number.js +5 -1
- package/index.d.ts +1 -1
- package/index.js +1 -1
- package/package.json +8 -2
- package/provider/default.d.ts +40 -25
- package/provider/default.js +205 -178
- package/provider/interface.d.ts +28 -34
- package/provider/utils.d.ts +4 -4
- package/provider/utils.js +15 -6
- package/signer/default.d.ts +11 -51
- package/signer/default.js +51 -232
- package/signer/index.d.ts +1 -1
- package/signer/index.js +1 -1
- package/signer/interface.d.ts +20 -37
- package/signer/interface.js +3 -32
- package/src/account/default.ts +152 -0
- package/src/account/index.ts +2 -0
- package/src/account/interface.ts +91 -0
- package/src/contract.ts +17 -18
- package/src/index.ts +1 -1
- package/src/provider/default.ts +137 -99
- package/src/provider/interface.ts +28 -34
- package/src/provider/utils.ts +16 -6
- package/src/signer/default.ts +33 -115
- package/src/signer/index.ts +1 -1
- package/src/signer/interface.ts +20 -41
- package/src/types/api.ts +165 -0
- package/src/types/index.ts +3 -0
- package/src/types/lib.ts +73 -0
- package/src/types/signer.ts +5 -0
- package/src/utils/number.ts +4 -0
- package/types/api.d.ts +152 -0
- package/{types.js → types/api.js} +0 -0
- package/types/index.d.ts +3 -0
- package/types/index.js +28 -0
- package/types/lib.d.ts +64 -0
- package/types/lib.js +2 -0
- package/types/signer.d.ts +4 -0
- package/types/signer.js +2 -0
- package/utils/number.d.ts +3 -0
- package/utils/number.js +8 -1
- package/__tests__/signer.test.ts +0 -125
- package/src/types.ts +0 -131
- package/types.d.ts +0 -116
|
@@ -0,0 +1,440 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
var __extends =
|
|
3
|
+
(this && this.__extends) ||
|
|
4
|
+
(function () {
|
|
5
|
+
var extendStatics = function (d, b) {
|
|
6
|
+
extendStatics =
|
|
7
|
+
Object.setPrototypeOf ||
|
|
8
|
+
({ __proto__: [] } instanceof Array &&
|
|
9
|
+
function (d, b) {
|
|
10
|
+
d.__proto__ = b;
|
|
11
|
+
}) ||
|
|
12
|
+
function (d, b) {
|
|
13
|
+
for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p];
|
|
14
|
+
};
|
|
15
|
+
return extendStatics(d, b);
|
|
16
|
+
};
|
|
17
|
+
return function (d, b) {
|
|
18
|
+
if (typeof b !== 'function' && b !== null)
|
|
19
|
+
throw new TypeError('Class extends value ' + String(b) + ' is not a constructor or null');
|
|
20
|
+
extendStatics(d, b);
|
|
21
|
+
function __() {
|
|
22
|
+
this.constructor = d;
|
|
23
|
+
}
|
|
24
|
+
d.prototype = b === null ? Object.create(b) : ((__.prototype = b.prototype), new __());
|
|
25
|
+
};
|
|
26
|
+
})();
|
|
27
|
+
var __assign =
|
|
28
|
+
(this && this.__assign) ||
|
|
29
|
+
function () {
|
|
30
|
+
__assign =
|
|
31
|
+
Object.assign ||
|
|
32
|
+
function (t) {
|
|
33
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
34
|
+
s = arguments[i];
|
|
35
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
|
|
36
|
+
}
|
|
37
|
+
return t;
|
|
38
|
+
};
|
|
39
|
+
return __assign.apply(this, arguments);
|
|
40
|
+
};
|
|
41
|
+
var __awaiter =
|
|
42
|
+
(this && this.__awaiter) ||
|
|
43
|
+
function (thisArg, _arguments, P, generator) {
|
|
44
|
+
function adopt(value) {
|
|
45
|
+
return value instanceof P
|
|
46
|
+
? value
|
|
47
|
+
: new P(function (resolve) {
|
|
48
|
+
resolve(value);
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
52
|
+
function fulfilled(value) {
|
|
53
|
+
try {
|
|
54
|
+
step(generator.next(value));
|
|
55
|
+
} catch (e) {
|
|
56
|
+
reject(e);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
function rejected(value) {
|
|
60
|
+
try {
|
|
61
|
+
step(generator['throw'](value));
|
|
62
|
+
} catch (e) {
|
|
63
|
+
reject(e);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
function step(result) {
|
|
67
|
+
result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected);
|
|
68
|
+
}
|
|
69
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
70
|
+
});
|
|
71
|
+
};
|
|
72
|
+
var __generator =
|
|
73
|
+
(this && this.__generator) ||
|
|
74
|
+
function (thisArg, body) {
|
|
75
|
+
var _ = {
|
|
76
|
+
label: 0,
|
|
77
|
+
sent: function () {
|
|
78
|
+
if (t[0] & 1) throw t[1];
|
|
79
|
+
return t[1];
|
|
80
|
+
},
|
|
81
|
+
trys: [],
|
|
82
|
+
ops: [],
|
|
83
|
+
},
|
|
84
|
+
f,
|
|
85
|
+
y,
|
|
86
|
+
t,
|
|
87
|
+
g;
|
|
88
|
+
return (
|
|
89
|
+
(g = { next: verb(0), throw: verb(1), return: verb(2) }),
|
|
90
|
+
typeof Symbol === 'function' &&
|
|
91
|
+
(g[Symbol.iterator] = function () {
|
|
92
|
+
return this;
|
|
93
|
+
}),
|
|
94
|
+
g
|
|
95
|
+
);
|
|
96
|
+
function verb(n) {
|
|
97
|
+
return function (v) {
|
|
98
|
+
return step([n, v]);
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
function step(op) {
|
|
102
|
+
if (f) throw new TypeError('Generator is already executing.');
|
|
103
|
+
while (_)
|
|
104
|
+
try {
|
|
105
|
+
if (
|
|
106
|
+
((f = 1),
|
|
107
|
+
y &&
|
|
108
|
+
(t =
|
|
109
|
+
op[0] & 2
|
|
110
|
+
? y['return']
|
|
111
|
+
: op[0]
|
|
112
|
+
? y['throw'] || ((t = y['return']) && t.call(y), 0)
|
|
113
|
+
: y.next) &&
|
|
114
|
+
!(t = t.call(y, op[1])).done)
|
|
115
|
+
)
|
|
116
|
+
return t;
|
|
117
|
+
if (((y = 0), t)) op = [op[0] & 2, t.value];
|
|
118
|
+
switch (op[0]) {
|
|
119
|
+
case 0:
|
|
120
|
+
case 1:
|
|
121
|
+
t = op;
|
|
122
|
+
break;
|
|
123
|
+
case 4:
|
|
124
|
+
_.label++;
|
|
125
|
+
return { value: op[1], done: false };
|
|
126
|
+
case 5:
|
|
127
|
+
_.label++;
|
|
128
|
+
y = op[1];
|
|
129
|
+
op = [0];
|
|
130
|
+
continue;
|
|
131
|
+
case 7:
|
|
132
|
+
op = _.ops.pop();
|
|
133
|
+
_.trys.pop();
|
|
134
|
+
continue;
|
|
135
|
+
default:
|
|
136
|
+
if (
|
|
137
|
+
!((t = _.trys), (t = t.length > 0 && t[t.length - 1])) &&
|
|
138
|
+
(op[0] === 6 || op[0] === 2)
|
|
139
|
+
) {
|
|
140
|
+
_ = 0;
|
|
141
|
+
continue;
|
|
142
|
+
}
|
|
143
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) {
|
|
144
|
+
_.label = op[1];
|
|
145
|
+
break;
|
|
146
|
+
}
|
|
147
|
+
if (op[0] === 6 && _.label < t[1]) {
|
|
148
|
+
_.label = t[1];
|
|
149
|
+
t = op;
|
|
150
|
+
break;
|
|
151
|
+
}
|
|
152
|
+
if (t && _.label < t[2]) {
|
|
153
|
+
_.label = t[2];
|
|
154
|
+
_.ops.push(op);
|
|
155
|
+
break;
|
|
156
|
+
}
|
|
157
|
+
if (t[2]) _.ops.pop();
|
|
158
|
+
_.trys.pop();
|
|
159
|
+
continue;
|
|
160
|
+
}
|
|
161
|
+
op = body.call(thisArg, _);
|
|
162
|
+
} catch (e) {
|
|
163
|
+
op = [6, e];
|
|
164
|
+
y = 0;
|
|
165
|
+
} finally {
|
|
166
|
+
f = t = 0;
|
|
167
|
+
}
|
|
168
|
+
if (op[0] & 5) throw op[1];
|
|
169
|
+
return { value: op[0] ? op[1] : void 0, done: true };
|
|
170
|
+
}
|
|
171
|
+
};
|
|
172
|
+
var __rest =
|
|
173
|
+
(this && this.__rest) ||
|
|
174
|
+
function (s, e) {
|
|
175
|
+
var t = {};
|
|
176
|
+
for (var p in s)
|
|
177
|
+
if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
|
|
178
|
+
if (s != null && typeof Object.getOwnPropertySymbols === 'function')
|
|
179
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
180
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
181
|
+
t[p[i]] = s[p[i]];
|
|
182
|
+
}
|
|
183
|
+
return t;
|
|
184
|
+
};
|
|
185
|
+
var __read =
|
|
186
|
+
(this && this.__read) ||
|
|
187
|
+
function (o, n) {
|
|
188
|
+
var m = typeof Symbol === 'function' && o[Symbol.iterator];
|
|
189
|
+
if (!m) return o;
|
|
190
|
+
var i = m.call(o),
|
|
191
|
+
r,
|
|
192
|
+
ar = [],
|
|
193
|
+
e;
|
|
194
|
+
try {
|
|
195
|
+
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
196
|
+
} catch (error) {
|
|
197
|
+
e = { error: error };
|
|
198
|
+
} finally {
|
|
199
|
+
try {
|
|
200
|
+
if (r && !r.done && (m = i['return'])) m.call(i);
|
|
201
|
+
} finally {
|
|
202
|
+
if (e) throw e.error;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
return ar;
|
|
206
|
+
};
|
|
207
|
+
var __spreadArray =
|
|
208
|
+
(this && this.__spreadArray) ||
|
|
209
|
+
function (to, from, pack) {
|
|
210
|
+
if (pack || arguments.length === 2)
|
|
211
|
+
for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
212
|
+
if (ar || !(i in from)) {
|
|
213
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
214
|
+
ar[i] = from[i];
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
218
|
+
};
|
|
219
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
220
|
+
exports.Account = void 0;
|
|
221
|
+
var contract_1 = require('../contract');
|
|
222
|
+
var provider_1 = require('../provider');
|
|
223
|
+
var signer_1 = require('../signer');
|
|
224
|
+
var number_1 = require('../utils/number');
|
|
225
|
+
var stark_1 = require('../utils/stark');
|
|
226
|
+
var typedData_1 = require('../utils/typedData');
|
|
227
|
+
var Account = /** @class */ (function (_super) {
|
|
228
|
+
__extends(Account, _super);
|
|
229
|
+
function Account(provider, address, keyPair) {
|
|
230
|
+
var _this = _super.call(this, provider) || this;
|
|
231
|
+
_this.signer = new signer_1.Signer(keyPair);
|
|
232
|
+
_this.address = address;
|
|
233
|
+
return _this;
|
|
234
|
+
}
|
|
235
|
+
Account.prototype.getNonce = function () {
|
|
236
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
237
|
+
var result;
|
|
238
|
+
return __generator(this, function (_a) {
|
|
239
|
+
switch (_a.label) {
|
|
240
|
+
case 0:
|
|
241
|
+
return [
|
|
242
|
+
4 /*yield*/,
|
|
243
|
+
this.callContract({
|
|
244
|
+
contractAddress: this.address,
|
|
245
|
+
entrypoint: 'get_nonce',
|
|
246
|
+
}),
|
|
247
|
+
];
|
|
248
|
+
case 1:
|
|
249
|
+
result = _a.sent().result;
|
|
250
|
+
return [2 /*return*/, (0, number_1.toHex)((0, number_1.toBN)(result[0]))];
|
|
251
|
+
}
|
|
252
|
+
});
|
|
253
|
+
});
|
|
254
|
+
};
|
|
255
|
+
/**
|
|
256
|
+
* Invoke execute function in account contract
|
|
257
|
+
*
|
|
258
|
+
* [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/gateway/gateway_client.py#L13-L17)
|
|
259
|
+
*
|
|
260
|
+
* @param transaction - transaction to be invoked
|
|
261
|
+
* @returns a confirmation of invoking a function on the starknet contract
|
|
262
|
+
*/
|
|
263
|
+
Account.prototype.execute = function (transactions, abis, transactionsDetail) {
|
|
264
|
+
if (abis === void 0) {
|
|
265
|
+
abis = [];
|
|
266
|
+
}
|
|
267
|
+
if (transactionsDetail === void 0) {
|
|
268
|
+
transactionsDetail = {};
|
|
269
|
+
}
|
|
270
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
271
|
+
var _a,
|
|
272
|
+
contractAddress,
|
|
273
|
+
_b,
|
|
274
|
+
calldata,
|
|
275
|
+
entrypoint,
|
|
276
|
+
invocation,
|
|
277
|
+
nonce,
|
|
278
|
+
nonceBn,
|
|
279
|
+
_c,
|
|
280
|
+
_d,
|
|
281
|
+
calldataDecimal,
|
|
282
|
+
signature,
|
|
283
|
+
entrypointSelector;
|
|
284
|
+
return __generator(this, function (_e) {
|
|
285
|
+
switch (_e.label) {
|
|
286
|
+
case 0:
|
|
287
|
+
if (Array.isArray(transactions) && transactions.length !== 1) {
|
|
288
|
+
throw new Error('Only one transaction at a time is currently supported');
|
|
289
|
+
}
|
|
290
|
+
(_a = Array.isArray(transactions) ? transactions[0] : transactions),
|
|
291
|
+
(contractAddress = _a.contractAddress),
|
|
292
|
+
(_b = _a.calldata),
|
|
293
|
+
(calldata = _b === void 0 ? [] : _b),
|
|
294
|
+
(entrypoint = _a.entrypoint),
|
|
295
|
+
(invocation = __rest(_a, ['contractAddress', 'calldata', 'entrypoint']));
|
|
296
|
+
nonce = transactionsDetail.nonce;
|
|
297
|
+
_c = number_1.toBN;
|
|
298
|
+
if (!(nonce !== null && nonce !== void 0)) return [3 /*break*/, 1];
|
|
299
|
+
_d = nonce;
|
|
300
|
+
return [3 /*break*/, 3];
|
|
301
|
+
case 1:
|
|
302
|
+
return [4 /*yield*/, this.getNonce()];
|
|
303
|
+
case 2:
|
|
304
|
+
_d = _e.sent();
|
|
305
|
+
_e.label = 3;
|
|
306
|
+
case 3:
|
|
307
|
+
nonceBn = _c.apply(void 0, [_d]);
|
|
308
|
+
calldataDecimal = (0, number_1.bigNumberishArrayToDecimalStringArray)(calldata);
|
|
309
|
+
return [
|
|
310
|
+
4 /*yield*/,
|
|
311
|
+
this.signer.signTransaction(
|
|
312
|
+
[
|
|
313
|
+
__assign(__assign({}, invocation), {
|
|
314
|
+
contractAddress: contractAddress,
|
|
315
|
+
calldata: calldataDecimal,
|
|
316
|
+
entrypoint: entrypoint,
|
|
317
|
+
}),
|
|
318
|
+
],
|
|
319
|
+
{ walletAddress: this.address, nonce: nonceBn },
|
|
320
|
+
abis
|
|
321
|
+
),
|
|
322
|
+
];
|
|
323
|
+
case 4:
|
|
324
|
+
signature = _e.sent();
|
|
325
|
+
entrypointSelector = (0, stark_1.getSelectorFromName)(entrypoint);
|
|
326
|
+
return [
|
|
327
|
+
2 /*return*/,
|
|
328
|
+
_super.prototype.invokeFunction.call(this, {
|
|
329
|
+
contractAddress: this.address,
|
|
330
|
+
entrypoint: 'execute',
|
|
331
|
+
calldata: __spreadArray(
|
|
332
|
+
__spreadArray(
|
|
333
|
+
[contractAddress, entrypointSelector, calldataDecimal.length.toString()],
|
|
334
|
+
__read(calldataDecimal),
|
|
335
|
+
false
|
|
336
|
+
),
|
|
337
|
+
[nonceBn.toString()],
|
|
338
|
+
false
|
|
339
|
+
),
|
|
340
|
+
signature: signature,
|
|
341
|
+
}),
|
|
342
|
+
];
|
|
343
|
+
}
|
|
344
|
+
});
|
|
345
|
+
});
|
|
346
|
+
};
|
|
347
|
+
/**
|
|
348
|
+
* Sign an JSON object with the starknet private key and return the signature
|
|
349
|
+
*
|
|
350
|
+
* @param json - JSON object to be signed
|
|
351
|
+
* @returns the signature of the JSON object
|
|
352
|
+
* @throws {Error} if the JSON object is not a valid JSON
|
|
353
|
+
*/
|
|
354
|
+
Account.prototype.signMessage = function (typedData) {
|
|
355
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
356
|
+
return __generator(this, function (_a) {
|
|
357
|
+
return [2 /*return*/, this.signer.signMessage(typedData, this.address)];
|
|
358
|
+
});
|
|
359
|
+
});
|
|
360
|
+
};
|
|
361
|
+
/**
|
|
362
|
+
* Hash a JSON object with pederson hash and return the hash
|
|
363
|
+
*
|
|
364
|
+
* @param json - JSON object to be hashed
|
|
365
|
+
* @returns the hash of the JSON object
|
|
366
|
+
* @throws {Error} if the JSON object is not a valid JSON
|
|
367
|
+
*/
|
|
368
|
+
Account.prototype.hashMessage = function (typedData) {
|
|
369
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
370
|
+
return __generator(this, function (_a) {
|
|
371
|
+
return [2 /*return*/, (0, typedData_1.getMessageHash)(typedData, this.address)];
|
|
372
|
+
});
|
|
373
|
+
});
|
|
374
|
+
};
|
|
375
|
+
/**
|
|
376
|
+
* Verify a signature of a JSON object
|
|
377
|
+
*
|
|
378
|
+
* @param json - JSON object to be verified
|
|
379
|
+
* @param signature - signature of the JSON object
|
|
380
|
+
* @returns true if the signature is valid, false otherwise
|
|
381
|
+
* @throws {Error} if the JSON object is not a valid JSON or the signature is not a valid signature
|
|
382
|
+
*/
|
|
383
|
+
Account.prototype.verifyMessageHash = function (hash, signature) {
|
|
384
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
385
|
+
var _a;
|
|
386
|
+
return __generator(this, function (_b) {
|
|
387
|
+
switch (_b.label) {
|
|
388
|
+
case 0:
|
|
389
|
+
_b.trys.push([0, 2, , 3]);
|
|
390
|
+
return [
|
|
391
|
+
4 /*yield*/,
|
|
392
|
+
this.callContract({
|
|
393
|
+
contractAddress: this.address,
|
|
394
|
+
entrypoint: 'is_valid_signature',
|
|
395
|
+
calldata: (0, contract_1.compileCalldata)({
|
|
396
|
+
hash: (0, number_1.toBN)(hash).toString(),
|
|
397
|
+
signature: signature.map(function (x) {
|
|
398
|
+
return (0, number_1.toBN)(x).toString();
|
|
399
|
+
}),
|
|
400
|
+
}),
|
|
401
|
+
}),
|
|
402
|
+
];
|
|
403
|
+
case 1:
|
|
404
|
+
_b.sent();
|
|
405
|
+
return [2 /*return*/, true];
|
|
406
|
+
case 2:
|
|
407
|
+
_a = _b.sent();
|
|
408
|
+
return [2 /*return*/, false];
|
|
409
|
+
case 3:
|
|
410
|
+
return [2 /*return*/];
|
|
411
|
+
}
|
|
412
|
+
});
|
|
413
|
+
});
|
|
414
|
+
};
|
|
415
|
+
/**
|
|
416
|
+
* Verify a signature of a given hash
|
|
417
|
+
* @warning This method is not recommended, use verifyMessage instead
|
|
418
|
+
*
|
|
419
|
+
* @param hash - hash to be verified
|
|
420
|
+
* @param signature - signature of the hash
|
|
421
|
+
* @returns true if the signature is valid, false otherwise
|
|
422
|
+
* @throws {Error} if the signature is not a valid signature
|
|
423
|
+
*/
|
|
424
|
+
Account.prototype.verifyMessage = function (typedData, signature) {
|
|
425
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
426
|
+
var hash;
|
|
427
|
+
return __generator(this, function (_a) {
|
|
428
|
+
switch (_a.label) {
|
|
429
|
+
case 0:
|
|
430
|
+
return [4 /*yield*/, this.hashMessage(typedData)];
|
|
431
|
+
case 1:
|
|
432
|
+
hash = _a.sent();
|
|
433
|
+
return [2 /*return*/, this.verifyMessageHash(hash, signature)];
|
|
434
|
+
}
|
|
435
|
+
});
|
|
436
|
+
});
|
|
437
|
+
};
|
|
438
|
+
return Account;
|
|
439
|
+
})(provider_1.Provider);
|
|
440
|
+
exports.Account = Account;
|
package/account/index.js
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
var __createBinding =
|
|
3
|
+
(this && this.__createBinding) ||
|
|
4
|
+
(Object.create
|
|
5
|
+
? function (o, m, k, k2) {
|
|
6
|
+
if (k2 === undefined) k2 = k;
|
|
7
|
+
Object.defineProperty(o, k2, {
|
|
8
|
+
enumerable: true,
|
|
9
|
+
get: function () {
|
|
10
|
+
return m[k];
|
|
11
|
+
},
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
: function (o, m, k, k2) {
|
|
15
|
+
if (k2 === undefined) k2 = k;
|
|
16
|
+
o[k2] = m[k];
|
|
17
|
+
});
|
|
18
|
+
var __exportStar =
|
|
19
|
+
(this && this.__exportStar) ||
|
|
20
|
+
function (m, exports) {
|
|
21
|
+
for (var p in m)
|
|
22
|
+
if (p !== 'default' && !Object.prototype.hasOwnProperty.call(exports, p))
|
|
23
|
+
__createBinding(exports, m, p);
|
|
24
|
+
};
|
|
25
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
26
|
+
__exportStar(require('./default'), exports);
|
|
27
|
+
__exportStar(require('./interface'), exports);
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { ProviderInterface } from '../provider';
|
|
2
|
+
import {
|
|
3
|
+
Abi,
|
|
4
|
+
AddTransactionResponse,
|
|
5
|
+
DeployContractPayload,
|
|
6
|
+
ExecuteInvocation,
|
|
7
|
+
InvocationsDetails,
|
|
8
|
+
Signature,
|
|
9
|
+
} from '../types';
|
|
10
|
+
import { BigNumberish } from '../utils/number';
|
|
11
|
+
import { TypedData } from '../utils/typedData/types';
|
|
12
|
+
export declare abstract class AccountInterface extends ProviderInterface {
|
|
13
|
+
abstract address: string;
|
|
14
|
+
/**
|
|
15
|
+
* Deploys a given compiled contract (json) to starknet
|
|
16
|
+
*
|
|
17
|
+
* @param payload payload to be deployed containing:
|
|
18
|
+
* - compiled contract code
|
|
19
|
+
* - constructor calldata
|
|
20
|
+
* - address salt
|
|
21
|
+
* @param abi the abi of the contract
|
|
22
|
+
* @returns a confirmation of sending a transaction on the starknet contract
|
|
23
|
+
*/
|
|
24
|
+
abstract deployContract(
|
|
25
|
+
payload: DeployContractPayload,
|
|
26
|
+
abi?: Abi
|
|
27
|
+
): Promise<AddTransactionResponse>;
|
|
28
|
+
/**
|
|
29
|
+
* Invoke execute function in account contract
|
|
30
|
+
*
|
|
31
|
+
* @param transactions the invocation object or an array of them, containing:
|
|
32
|
+
* - contractAddress - the address of the contract
|
|
33
|
+
* - entrypoint - the entrypoint of the contract
|
|
34
|
+
* - calldata - (defaults to []) the calldata
|
|
35
|
+
* - signature - (defaults to []) the signature
|
|
36
|
+
* @param abi (optional) the abi of the contract for better displaying
|
|
37
|
+
*
|
|
38
|
+
* @returns response from addTransaction
|
|
39
|
+
*/
|
|
40
|
+
abstract execute(
|
|
41
|
+
transactions: ExecuteInvocation | ExecuteInvocation[],
|
|
42
|
+
abis?: Abi[],
|
|
43
|
+
transactionsDetail?: InvocationsDetails
|
|
44
|
+
): Promise<AddTransactionResponse>;
|
|
45
|
+
/**
|
|
46
|
+
* Sign an JSON object for off-chain usage with the starknet private key and return the signature
|
|
47
|
+
* This adds a message prefix so it cant be interchanged with transactions
|
|
48
|
+
*
|
|
49
|
+
* @param json - JSON object to be signed
|
|
50
|
+
* @returns the signature of the JSON object
|
|
51
|
+
* @throws {Error} if the JSON object is not a valid JSON
|
|
52
|
+
*/
|
|
53
|
+
abstract signMessage(typedData: TypedData): Promise<Signature>;
|
|
54
|
+
/**
|
|
55
|
+
* Hash a JSON object with pederson hash and return the hash
|
|
56
|
+
* This adds a message prefix so it cant be interchanged with transactions
|
|
57
|
+
*
|
|
58
|
+
* @param json - JSON object to be hashed
|
|
59
|
+
* @returns the hash of the JSON object
|
|
60
|
+
* @throws {Error} if the JSON object is not a valid JSON
|
|
61
|
+
*/
|
|
62
|
+
abstract hashMessage(typedData: TypedData): Promise<string>;
|
|
63
|
+
/**
|
|
64
|
+
* Verify a signature of a JSON object
|
|
65
|
+
*
|
|
66
|
+
* @param json - JSON object to be verified
|
|
67
|
+
* @param signature - signature of the JSON object
|
|
68
|
+
* @returns true if the signature is valid, false otherwise
|
|
69
|
+
* @throws {Error} if the JSON object is not a valid JSON or the signature is not a valid signature
|
|
70
|
+
*/
|
|
71
|
+
abstract verifyMessage(typedData: TypedData, signature: Signature): Promise<boolean>;
|
|
72
|
+
/**
|
|
73
|
+
* Verify a signature of a given hash
|
|
74
|
+
* @warning This method is not recommended, use verifyMessage instead
|
|
75
|
+
*
|
|
76
|
+
* @param hash - hash to be verified
|
|
77
|
+
* @param signature - signature of the hash
|
|
78
|
+
* @returns true if the signature is valid, false otherwise
|
|
79
|
+
* @throws {Error} if the signature is not a valid signature
|
|
80
|
+
*/
|
|
81
|
+
abstract verifyMessageHash(hash: BigNumberish, signature: Signature): Promise<boolean>;
|
|
82
|
+
abstract getNonce(): Promise<string>;
|
|
83
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
var __extends =
|
|
3
|
+
(this && this.__extends) ||
|
|
4
|
+
(function () {
|
|
5
|
+
var extendStatics = function (d, b) {
|
|
6
|
+
extendStatics =
|
|
7
|
+
Object.setPrototypeOf ||
|
|
8
|
+
({ __proto__: [] } instanceof Array &&
|
|
9
|
+
function (d, b) {
|
|
10
|
+
d.__proto__ = b;
|
|
11
|
+
}) ||
|
|
12
|
+
function (d, b) {
|
|
13
|
+
for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p];
|
|
14
|
+
};
|
|
15
|
+
return extendStatics(d, b);
|
|
16
|
+
};
|
|
17
|
+
return function (d, b) {
|
|
18
|
+
if (typeof b !== 'function' && b !== null)
|
|
19
|
+
throw new TypeError('Class extends value ' + String(b) + ' is not a constructor or null');
|
|
20
|
+
extendStatics(d, b);
|
|
21
|
+
function __() {
|
|
22
|
+
this.constructor = d;
|
|
23
|
+
}
|
|
24
|
+
d.prototype = b === null ? Object.create(b) : ((__.prototype = b.prototype), new __());
|
|
25
|
+
};
|
|
26
|
+
})();
|
|
27
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
28
|
+
exports.AccountInterface = void 0;
|
|
29
|
+
var provider_1 = require('../provider');
|
|
30
|
+
var AccountInterface = /** @class */ (function (_super) {
|
|
31
|
+
__extends(AccountInterface, _super);
|
|
32
|
+
function AccountInterface() {
|
|
33
|
+
return (_super !== null && _super.apply(this, arguments)) || this;
|
|
34
|
+
}
|
|
35
|
+
return AccountInterface;
|
|
36
|
+
})(provider_1.ProviderInterface);
|
|
37
|
+
exports.AccountInterface = AccountInterface;
|
package/contract.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { Provider } from './provider';
|
|
2
|
-
import {
|
|
2
|
+
import { BlockIdentifier } from './provider/utils';
|
|
3
|
+
import { Abi, RawCalldata, Signature, StructAbi } from './types';
|
|
3
4
|
import { BigNumberish } from './utils/number';
|
|
4
5
|
export declare type Args = {
|
|
5
6
|
[inputName: string]:
|
|
@@ -10,11 +11,10 @@ export declare type Args = {
|
|
|
10
11
|
[k: string]: BigNumberish;
|
|
11
12
|
};
|
|
12
13
|
};
|
|
13
|
-
export declare
|
|
14
|
-
export declare function compileCalldata(args: Args): Calldata;
|
|
14
|
+
export declare function compileCalldata(args: Args): RawCalldata;
|
|
15
15
|
export declare class Contract {
|
|
16
16
|
connectedTo: string | null;
|
|
17
|
-
abi: Abi
|
|
17
|
+
abi: Abi;
|
|
18
18
|
structs: {
|
|
19
19
|
[name: string]: StructAbi;
|
|
20
20
|
};
|
|
@@ -25,7 +25,7 @@ export declare class Contract {
|
|
|
25
25
|
* @param abi - Abi of the contract object
|
|
26
26
|
* @param address (optional) - address to connect to
|
|
27
27
|
*/
|
|
28
|
-
constructor(abi: Abi
|
|
28
|
+
constructor(abi: Abi, address?: string | null, provider?: Provider);
|
|
29
29
|
connect(address: string): Contract;
|
|
30
30
|
private validateMethodAndArgs;
|
|
31
31
|
private parseResponseField;
|
|
@@ -35,5 +35,5 @@ export declare class Contract {
|
|
|
35
35
|
args?: Args,
|
|
36
36
|
signature?: Signature
|
|
37
37
|
): Promise<import('./types').AddTransactionResponse>;
|
|
38
|
-
call(method: string, args?: Args): Promise<Args>;
|
|
38
|
+
call(method: string, args?: Args, blockIdentifier?: BlockIdentifier): Promise<Args>;
|
|
39
39
|
}
|
package/contract.js
CHANGED
|
@@ -188,7 +188,6 @@ exports.Contract = exports.compileCalldata = void 0;
|
|
|
188
188
|
var minimalistic_assert_1 = __importDefault(require('minimalistic-assert'));
|
|
189
189
|
var provider_1 = require('./provider');
|
|
190
190
|
var number_1 = require('./utils/number');
|
|
191
|
-
var stark_1 = require('./utils/stark');
|
|
192
191
|
function parseFelt(candidate) {
|
|
193
192
|
try {
|
|
194
193
|
return (0, number_1.toBN)(candidate);
|
|
@@ -355,22 +354,23 @@ var Contract = /** @class */ (function () {
|
|
|
355
354
|
// validate method and args
|
|
356
355
|
this.validateMethodAndArgs('INVOKE', method, args);
|
|
357
356
|
// compile calldata
|
|
358
|
-
var entrypointSelector = (0, stark_1.getSelectorFromName)(method);
|
|
359
357
|
var calldata = compileCalldata(args);
|
|
360
|
-
return this.provider.
|
|
361
|
-
|
|
362
|
-
contract_address: this.connectedTo,
|
|
358
|
+
return this.provider.invokeFunction({
|
|
359
|
+
contractAddress: this.connectedTo,
|
|
363
360
|
signature: signature,
|
|
364
361
|
calldata: calldata,
|
|
365
|
-
|
|
362
|
+
entrypoint: method,
|
|
366
363
|
});
|
|
367
364
|
};
|
|
368
|
-
Contract.prototype.call = function (method, args) {
|
|
365
|
+
Contract.prototype.call = function (method, args, blockIdentifier) {
|
|
369
366
|
if (args === void 0) {
|
|
370
367
|
args = {};
|
|
371
368
|
}
|
|
369
|
+
if (blockIdentifier === void 0) {
|
|
370
|
+
blockIdentifier = null;
|
|
371
|
+
}
|
|
372
372
|
return __awaiter(this, void 0, void 0, function () {
|
|
373
|
-
var
|
|
373
|
+
var calldata;
|
|
374
374
|
var _this = this;
|
|
375
375
|
return __generator(this, function (_a) {
|
|
376
376
|
// ensure contract is connected
|
|
@@ -378,16 +378,18 @@ var Contract = /** @class */ (function () {
|
|
|
378
378
|
minimalistic_assert_1.default)(this.connectedTo !== null, 'contract isnt connected to an address');
|
|
379
379
|
// validate method and args
|
|
380
380
|
this.validateMethodAndArgs('CALL', method, args);
|
|
381
|
-
entrypointSelector = (0, stark_1.getSelectorFromName)(method);
|
|
382
381
|
calldata = compileCalldata(args);
|
|
383
382
|
return [
|
|
384
383
|
2 /*return*/,
|
|
385
384
|
this.provider
|
|
386
|
-
.callContract(
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
385
|
+
.callContract(
|
|
386
|
+
{
|
|
387
|
+
contractAddress: this.connectedTo,
|
|
388
|
+
entrypoint: method,
|
|
389
|
+
calldata: calldata,
|
|
390
|
+
},
|
|
391
|
+
blockIdentifier
|
|
392
|
+
)
|
|
391
393
|
.then(function (x) {
|
|
392
394
|
return _this.parseResponse(method, x.result);
|
|
393
395
|
}),
|