starknet 2.7.2 → 2.8.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 CHANGED
@@ -1,3 +1,10 @@
1
+ # [2.8.0](https://github.com/seanjameshan/starknet.js/compare/v2.7.2...v2.8.0) (2022-02-02)
2
+
3
+ ### Features
4
+
5
+ - add tests ([e495d48](https://github.com/seanjameshan/starknet.js/commit/e495d4899141a79fe310d4fe76f70df03b1551ca))
6
+ - implement verifyMessage and verifyMessageHash ([bc9c4e9](https://github.com/seanjameshan/starknet.js/commit/bc9c4e9574cc453af35705eb4488602ea33cc2cb))
7
+
1
8
  ## [2.7.2](https://github.com/seanjameshan/starknet.js/compare/v2.7.1...v2.7.2) (2022-01-20)
2
9
 
3
10
  ### Bug Fixes
@@ -0,0 +1,35 @@
1
+ {
2
+ "types": {
3
+ "StarkNetDomain": [
4
+ { "name": "name", "type": "felt" },
5
+ { "name": "version", "type": "felt" },
6
+ { "name": "chainId", "type": "felt" }
7
+ ],
8
+ "Person": [
9
+ { "name": "name", "type": "felt" },
10
+ { "name": "wallet", "type": "felt" }
11
+ ],
12
+ "Mail": [
13
+ { "name": "from", "type": "Person" },
14
+ { "name": "to", "type": "Person" },
15
+ { "name": "contents", "type": "felt" }
16
+ ]
17
+ },
18
+ "primaryType": "Mail",
19
+ "domain": {
20
+ "name": "StarkNet Mail",
21
+ "version": "1",
22
+ "chainId": 1
23
+ },
24
+ "message": {
25
+ "from": {
26
+ "name": "Cow",
27
+ "wallet": "0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826"
28
+ },
29
+ "to": {
30
+ "name": "Bob",
31
+ "wallet": "0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB"
32
+ },
33
+ "contents": "Hello, Bob!"
34
+ }
35
+ }
@@ -1,5 +1,6 @@
1
1
  import fs from 'fs';
2
2
 
3
+ import typedDataExample from '../__mocks__/typedDataExample.json';
3
4
  import {
4
5
  CompiledContract,
5
6
  Contract,
@@ -116,4 +117,9 @@ describe('deploy and test Wallet', () => {
116
117
  expect(code).toBe('TRANSACTION_RECEIVED');
117
118
  await defaultProvider.waitForTx(transaction_hash);
118
119
  });
120
+ test('sign and verify offchain message', async () => {
121
+ const signature = await signer.signMessage(typedDataExample);
122
+
123
+ expect(await signer.verifyMessage(typedDataExample, signature)).toBe(true);
124
+ });
119
125
  });
@@ -1,41 +1,6 @@
1
+ import typedDataExample from '../../__mocks__/typedDataExample.json';
1
2
  import { encodeType, getMessageHash, getStructHash, getTypeHash } from '../../src/utils/typedData';
2
3
 
3
- const typedDataExample = {
4
- types: {
5
- StarkNetDomain: [
6
- { name: 'name', type: 'felt' },
7
- { name: 'version', type: 'felt' },
8
- { name: 'chainId', type: 'felt' },
9
- ],
10
- Person: [
11
- { name: 'name', type: 'felt' },
12
- { name: 'wallet', type: 'felt' },
13
- ],
14
- Mail: [
15
- { name: 'from', type: 'Person' },
16
- { name: 'to', type: 'Person' },
17
- { name: 'contents', type: 'felt' },
18
- ],
19
- },
20
- primaryType: 'Mail',
21
- domain: {
22
- name: 'StarkNet Mail',
23
- version: '1',
24
- chainId: 1,
25
- },
26
- message: {
27
- from: {
28
- name: 'Cow',
29
- wallet: '0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826',
30
- },
31
- to: {
32
- name: 'Bob',
33
- wallet: '0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB',
34
- },
35
- contents: 'Hello, Bob!',
36
- },
37
- };
38
-
39
4
  describe('typedData', () => {
40
5
  test('should get right type encoding', () => {
41
6
  const typeEncoding = encodeType(typedDataExample, 'Mail');
@@ -1,5 +1,6 @@
1
1
  import { Provider } from '../provider';
2
2
  import { AddTransactionResponse, KeyPair, Signature, Transaction } from '../types';
3
+ import { BigNumberish } from '../utils/number';
3
4
  import { TypedData } from '../utils/typedData';
4
5
  import { SignerInterface } from './interface';
5
6
  export declare class Signer extends Provider implements SignerInterface {
@@ -31,4 +32,23 @@ export declare class Signer extends Provider implements SignerInterface {
31
32
  * @throws {Error} if the JSON object is not a valid JSON
32
33
  */
33
34
  hashMessage(typedData: TypedData): Promise<string>;
35
+ /**
36
+ * Verify a signature of a JSON object
37
+ *
38
+ * @param json - JSON object to be verified
39
+ * @param signature - signature of the JSON object
40
+ * @returns true if the signature is valid, false otherwise
41
+ * @throws {Error} if the JSON object is not a valid JSON or the signature is not a valid signature
42
+ */
43
+ verifyMessageHash(hash: BigNumberish, signature: Signature): Promise<boolean>;
44
+ /**
45
+ * Verify a signature of a given hash
46
+ * @warning This method is not recommended, use verifyMessage instead
47
+ *
48
+ * @param hash - hash to be verified
49
+ * @param signature - signature of the hash
50
+ * @returns true if the signature is valid, false otherwise
51
+ * @throws {Error} if the signature is not a valid signature
52
+ */
53
+ verifyMessage(typedData: TypedData, signature: Signature): Promise<boolean>;
34
54
  }
@@ -81,6 +81,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
81
81
  Object.defineProperty(exports, "__esModule", { value: true });
82
82
  exports.Signer = void 0;
83
83
  var minimalistic_assert_1 = __importDefault(require("minimalistic-assert"));
84
+ var contract_1 = require("../contract");
84
85
  var provider_1 = require("../provider");
85
86
  var ellipticCurve_1 = require("../utils/ellipticCurve");
86
87
  var encode_1 = require("../utils/encode");
@@ -180,6 +181,62 @@ var Signer = /** @class */ (function (_super) {
180
181
  });
181
182
  });
182
183
  };
184
+ /**
185
+ * Verify a signature of a JSON object
186
+ *
187
+ * @param json - JSON object to be verified
188
+ * @param signature - signature of the JSON object
189
+ * @returns true if the signature is valid, false otherwise
190
+ * @throws {Error} if the JSON object is not a valid JSON or the signature is not a valid signature
191
+ */
192
+ Signer.prototype.verifyMessageHash = function (hash, signature) {
193
+ return __awaiter(this, void 0, void 0, function () {
194
+ var _a;
195
+ return __generator(this, function (_b) {
196
+ switch (_b.label) {
197
+ case 0:
198
+ _b.trys.push([0, 2, , 3]);
199
+ return [4 /*yield*/, this.callContract({
200
+ contract_address: this.address,
201
+ entry_point_selector: (0, stark_1.getSelectorFromName)('is_valid_signature'),
202
+ calldata: (0, contract_1.compileCalldata)({
203
+ hash: (0, number_1.toBN)(hash).toString(),
204
+ signature: signature.map(function (x) { return (0, number_1.toBN)(x).toString(); }),
205
+ }),
206
+ })];
207
+ case 1:
208
+ _b.sent();
209
+ return [2 /*return*/, true];
210
+ case 2:
211
+ _a = _b.sent();
212
+ return [2 /*return*/, false];
213
+ case 3: return [2 /*return*/];
214
+ }
215
+ });
216
+ });
217
+ };
218
+ /**
219
+ * Verify a signature of a given hash
220
+ * @warning This method is not recommended, use verifyMessage instead
221
+ *
222
+ * @param hash - hash to be verified
223
+ * @param signature - signature of the hash
224
+ * @returns true if the signature is valid, false otherwise
225
+ * @throws {Error} if the signature is not a valid signature
226
+ */
227
+ Signer.prototype.verifyMessage = function (typedData, signature) {
228
+ return __awaiter(this, void 0, void 0, function () {
229
+ var hash;
230
+ return __generator(this, function (_a) {
231
+ switch (_a.label) {
232
+ case 0: return [4 /*yield*/, this.hashMessage(typedData)];
233
+ case 1:
234
+ hash = _a.sent();
235
+ return [2 /*return*/, this.verifyMessageHash(hash, signature)];
236
+ }
237
+ });
238
+ });
239
+ };
183
240
  return Signer;
184
241
  }(provider_1.Provider));
185
242
  exports.Signer = Signer;
@@ -1,5 +1,6 @@
1
1
  import { Provider } from '../provider';
2
2
  import { AddTransactionResponse, Signature, Transaction } from '../types';
3
+ import { BigNumberish } from '../utils/number';
3
4
  import { TypedData } from '../utils/typedData/types';
4
5
  export declare abstract class SignerInterface extends Provider {
5
6
  abstract address: string;
@@ -30,4 +31,23 @@ export declare abstract class SignerInterface extends Provider {
30
31
  * @throws {Error} if the JSON object is not a valid JSON
31
32
  */
32
33
  abstract hashMessage(typedData: TypedData): Promise<string>;
34
+ /**
35
+ * Verify a signature of a JSON object
36
+ *
37
+ * @param json - JSON object to be verified
38
+ * @param signature - signature of the JSON object
39
+ * @returns true if the signature is valid, false otherwise
40
+ * @throws {Error} if the JSON object is not a valid JSON or the signature is not a valid signature
41
+ */
42
+ abstract verifyMessage(typedData: TypedData, signature: Signature): Promise<boolean>;
43
+ /**
44
+ * Verify a signature of a given hash
45
+ * @warning This method is not recommended, use verifyMessage instead
46
+ *
47
+ * @param hash - hash to be verified
48
+ * @param signature - signature of the hash
49
+ * @returns true if the signature is valid, false otherwise
50
+ * @throws {Error} if the signature is not a valid signature
51
+ */
52
+ abstract verifyMessageHash(hash: BigNumberish, signature: Signature): Promise<boolean>;
33
53
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "starknet",
3
- "version": "2.7.2",
3
+ "version": "2.8.0",
4
4
  "description": "JavaScript library for StarkNet",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -1,5 +1,6 @@
1
1
  import { Provider } from '../provider';
2
2
  import { AddTransactionResponse, KeyPair, Signature, Transaction } from '../types';
3
+ import { BigNumberish } from '../utils/number';
3
4
  import { TypedData } from '../utils/typedData';
4
5
  import { SignerInterface } from './interface';
5
6
  export declare class Signer extends Provider implements SignerInterface {
@@ -31,4 +32,23 @@ export declare class Signer extends Provider implements SignerInterface {
31
32
  * @throws {Error} if the JSON object is not a valid JSON
32
33
  */
33
34
  hashMessage(typedData: TypedData): Promise<string>;
35
+ /**
36
+ * Verify a signature of a JSON object
37
+ *
38
+ * @param json - JSON object to be verified
39
+ * @param signature - signature of the JSON object
40
+ * @returns true if the signature is valid, false otherwise
41
+ * @throws {Error} if the JSON object is not a valid JSON or the signature is not a valid signature
42
+ */
43
+ verifyMessageHash(hash: BigNumberish, signature: Signature): Promise<boolean>;
44
+ /**
45
+ * Verify a signature of a given hash
46
+ * @warning This method is not recommended, use verifyMessage instead
47
+ *
48
+ * @param hash - hash to be verified
49
+ * @param signature - signature of the hash
50
+ * @returns true if the signature is valid, false otherwise
51
+ * @throws {Error} if the signature is not a valid signature
52
+ */
53
+ verifyMessage(typedData: TypedData, signature: Signature): Promise<boolean>;
34
54
  }
package/signer/default.js CHANGED
@@ -197,6 +197,7 @@ var __importDefault =
197
197
  Object.defineProperty(exports, '__esModule', { value: true });
198
198
  exports.Signer = void 0;
199
199
  var minimalistic_assert_1 = __importDefault(require('minimalistic-assert'));
200
+ var contract_1 = require('../contract');
200
201
  var provider_1 = require('../provider');
201
202
  var ellipticCurve_1 = require('../utils/ellipticCurve');
202
203
  var encode_1 = require('../utils/encode');
@@ -323,6 +324,69 @@ var Signer = /** @class */ (function (_super) {
323
324
  });
324
325
  });
325
326
  };
327
+ /**
328
+ * Verify a signature of a JSON object
329
+ *
330
+ * @param json - JSON object to be verified
331
+ * @param signature - signature of the JSON object
332
+ * @returns true if the signature is valid, false otherwise
333
+ * @throws {Error} if the JSON object is not a valid JSON or the signature is not a valid signature
334
+ */
335
+ Signer.prototype.verifyMessageHash = function (hash, signature) {
336
+ return __awaiter(this, void 0, void 0, function () {
337
+ var _a;
338
+ return __generator(this, function (_b) {
339
+ switch (_b.label) {
340
+ case 0:
341
+ _b.trys.push([0, 2, , 3]);
342
+ return [
343
+ 4 /*yield*/,
344
+ this.callContract({
345
+ contract_address: this.address,
346
+ entry_point_selector: (0, stark_1.getSelectorFromName)('is_valid_signature'),
347
+ calldata: (0, contract_1.compileCalldata)({
348
+ hash: (0, number_1.toBN)(hash).toString(),
349
+ signature: signature.map(function (x) {
350
+ return (0, number_1.toBN)(x).toString();
351
+ }),
352
+ }),
353
+ }),
354
+ ];
355
+ case 1:
356
+ _b.sent();
357
+ return [2 /*return*/, true];
358
+ case 2:
359
+ _a = _b.sent();
360
+ return [2 /*return*/, false];
361
+ case 3:
362
+ return [2 /*return*/];
363
+ }
364
+ });
365
+ });
366
+ };
367
+ /**
368
+ * Verify a signature of a given hash
369
+ * @warning This method is not recommended, use verifyMessage instead
370
+ *
371
+ * @param hash - hash to be verified
372
+ * @param signature - signature of the hash
373
+ * @returns true if the signature is valid, false otherwise
374
+ * @throws {Error} if the signature is not a valid signature
375
+ */
376
+ Signer.prototype.verifyMessage = function (typedData, signature) {
377
+ return __awaiter(this, void 0, void 0, function () {
378
+ var hash;
379
+ return __generator(this, function (_a) {
380
+ switch (_a.label) {
381
+ case 0:
382
+ return [4 /*yield*/, this.hashMessage(typedData)];
383
+ case 1:
384
+ hash = _a.sent();
385
+ return [2 /*return*/, this.verifyMessageHash(hash, signature)];
386
+ }
387
+ });
388
+ });
389
+ };
326
390
  return Signer;
327
391
  })(provider_1.Provider);
328
392
  exports.Signer = Signer;
@@ -1,5 +1,6 @@
1
1
  import { Provider } from '../provider';
2
2
  import { AddTransactionResponse, Signature, Transaction } from '../types';
3
+ import { BigNumberish } from '../utils/number';
3
4
  import { TypedData } from '../utils/typedData/types';
4
5
  export declare abstract class SignerInterface extends Provider {
5
6
  abstract address: string;
@@ -30,4 +31,23 @@ export declare abstract class SignerInterface extends Provider {
30
31
  * @throws {Error} if the JSON object is not a valid JSON
31
32
  */
32
33
  abstract hashMessage(typedData: TypedData): Promise<string>;
34
+ /**
35
+ * Verify a signature of a JSON object
36
+ *
37
+ * @param json - JSON object to be verified
38
+ * @param signature - signature of the JSON object
39
+ * @returns true if the signature is valid, false otherwise
40
+ * @throws {Error} if the JSON object is not a valid JSON or the signature is not a valid signature
41
+ */
42
+ abstract verifyMessage(typedData: TypedData, signature: Signature): Promise<boolean>;
43
+ /**
44
+ * Verify a signature of a given hash
45
+ * @warning This method is not recommended, use verifyMessage instead
46
+ *
47
+ * @param hash - hash to be verified
48
+ * @param signature - signature of the hash
49
+ * @returns true if the signature is valid, false otherwise
50
+ * @throws {Error} if the signature is not a valid signature
51
+ */
52
+ abstract verifyMessageHash(hash: BigNumberish, signature: Signature): Promise<boolean>;
33
53
  }
@@ -1,11 +1,12 @@
1
1
  import assert from 'minimalistic-assert';
2
2
 
3
+ import { compileCalldata } from '../contract';
3
4
  import { Provider } from '../provider';
4
5
  import { AddTransactionResponse, KeyPair, Signature, Transaction } from '../types';
5
6
  import { sign } from '../utils/ellipticCurve';
6
7
  import { addHexPrefix } from '../utils/encode';
7
8
  import { hashMessage } from '../utils/hash';
8
- import { toBN } from '../utils/number';
9
+ import { BigNumberish, toBN } from '../utils/number';
9
10
  import { getSelectorFromName } from '../utils/stark';
10
11
  import { TypedData, getMessageHash } from '../utils/typedData';
11
12
  import { SignerInterface } from './interface';
@@ -98,4 +99,42 @@ export class Signer extends Provider implements SignerInterface {
98
99
  public async hashMessage(typedData: TypedData): Promise<string> {
99
100
  return getMessageHash(typedData, this.address);
100
101
  }
102
+
103
+ /**
104
+ * Verify a signature of a JSON object
105
+ *
106
+ * @param json - JSON object to be verified
107
+ * @param signature - signature of the JSON object
108
+ * @returns true if the signature is valid, false otherwise
109
+ * @throws {Error} if the JSON object is not a valid JSON or the signature is not a valid signature
110
+ */
111
+ public async verifyMessageHash(hash: BigNumberish, signature: Signature): Promise<boolean> {
112
+ try {
113
+ await this.callContract({
114
+ contract_address: this.address,
115
+ entry_point_selector: getSelectorFromName('is_valid_signature'),
116
+ calldata: compileCalldata({
117
+ hash: toBN(hash).toString(),
118
+ signature: signature.map((x) => toBN(x).toString()),
119
+ }),
120
+ });
121
+ return true;
122
+ } catch {
123
+ return false;
124
+ }
125
+ }
126
+
127
+ /**
128
+ * Verify a signature of a given hash
129
+ * @warning This method is not recommended, use verifyMessage instead
130
+ *
131
+ * @param hash - hash to be verified
132
+ * @param signature - signature of the hash
133
+ * @returns true if the signature is valid, false otherwise
134
+ * @throws {Error} if the signature is not a valid signature
135
+ */
136
+ public async verifyMessage(typedData: TypedData, signature: Signature): Promise<boolean> {
137
+ const hash = await this.hashMessage(typedData);
138
+ return this.verifyMessageHash(hash, signature);
139
+ }
101
140
  }
@@ -1,5 +1,6 @@
1
1
  import { Provider } from '../provider';
2
2
  import { AddTransactionResponse, Signature, Transaction } from '../types';
3
+ import { BigNumberish } from '../utils/number';
3
4
  import { TypedData } from '../utils/typedData/types';
4
5
 
5
6
  export abstract class SignerInterface extends Provider {
@@ -35,4 +36,25 @@ export abstract class SignerInterface extends Provider {
35
36
  * @throws {Error} if the JSON object is not a valid JSON
36
37
  */
37
38
  public abstract hashMessage(typedData: TypedData): Promise<string>;
39
+
40
+ /**
41
+ * Verify a signature of a JSON object
42
+ *
43
+ * @param json - JSON object to be verified
44
+ * @param signature - signature of the JSON object
45
+ * @returns true if the signature is valid, false otherwise
46
+ * @throws {Error} if the JSON object is not a valid JSON or the signature is not a valid signature
47
+ */
48
+ public abstract verifyMessage(typedData: TypedData, signature: Signature): Promise<boolean>;
49
+
50
+ /**
51
+ * Verify a signature of a given hash
52
+ * @warning This method is not recommended, use verifyMessage instead
53
+ *
54
+ * @param hash - hash to be verified
55
+ * @param signature - signature of the hash
56
+ * @returns true if the signature is valid, false otherwise
57
+ * @throws {Error} if the signature is not a valid signature
58
+ */
59
+ public abstract verifyMessageHash(hash: BigNumberish, signature: Signature): Promise<boolean>;
38
60
  }