Comparing version 2.7.2 to 2.8.0
import fs from 'fs'; | ||
import typedDataExample from '../__mocks__/typedDataExample.json'; | ||
import { | ||
@@ -119,2 +120,7 @@ CompiledContract, | ||
}); | ||
test('sign and verify offchain message', async () => { | ||
const signature = await signer.signMessage(typedDataExample); | ||
expect(await signer.verifyMessage(typedDataExample, signature)).toBe(true); | ||
}); | ||
}); |
@@ -0,39 +1,4 @@ | ||
import typedDataExample from '../../__mocks__/typedDataExample.json'; | ||
import { encodeType, getMessageHash, getStructHash, getTypeHash } from '../../src/utils/typedData'; | ||
const typedDataExample = { | ||
types: { | ||
StarkNetDomain: [ | ||
{ name: 'name', type: 'felt' }, | ||
{ name: 'version', type: 'felt' }, | ||
{ name: 'chainId', type: 'felt' }, | ||
], | ||
Person: [ | ||
{ name: 'name', type: 'felt' }, | ||
{ name: 'wallet', type: 'felt' }, | ||
], | ||
Mail: [ | ||
{ name: 'from', type: 'Person' }, | ||
{ name: 'to', type: 'Person' }, | ||
{ name: 'contents', type: 'felt' }, | ||
], | ||
}, | ||
primaryType: 'Mail', | ||
domain: { | ||
name: 'StarkNet Mail', | ||
version: '1', | ||
chainId: 1, | ||
}, | ||
message: { | ||
from: { | ||
name: 'Cow', | ||
wallet: '0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826', | ||
}, | ||
to: { | ||
name: 'Bob', | ||
wallet: '0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB', | ||
}, | ||
contents: 'Hello, Bob!', | ||
}, | ||
}; | ||
describe('typedData', () => { | ||
@@ -40,0 +5,0 @@ test('should get right type encoding', () => { |
@@ -0,1 +1,8 @@ | ||
# [2.8.0](https://github.com/seanjameshan/starknet.js/compare/v2.7.2...v2.8.0) (2022-02-02) | ||
### Features | ||
- add tests ([e495d48](https://github.com/seanjameshan/starknet.js/commit/e495d4899141a79fe310d4fe76f70df03b1551ca)) | ||
- implement verifyMessage and verifyMessageHash ([bc9c4e9](https://github.com/seanjameshan/starknet.js/commit/bc9c4e9574cc453af35705eb4488602ea33cc2cb)) | ||
## [2.7.2](https://github.com/seanjameshan/starknet.js/compare/v2.7.1...v2.7.2) (2022-01-20) | ||
@@ -2,0 +9,0 @@ |
import { Provider } from '../provider'; | ||
import { AddTransactionResponse, KeyPair, Signature, Transaction } from '../types'; | ||
import { BigNumberish } from '../utils/number'; | ||
import { TypedData } from '../utils/typedData'; | ||
@@ -34,2 +35,21 @@ import { SignerInterface } from './interface'; | ||
hashMessage(typedData: TypedData): Promise<string>; | ||
/** | ||
* Verify a signature of a JSON object | ||
* | ||
* @param json - JSON object to be verified | ||
* @param signature - signature of the JSON object | ||
* @returns true if the signature is valid, false otherwise | ||
* @throws {Error} if the JSON object is not a valid JSON or the signature is not a valid signature | ||
*/ | ||
verifyMessageHash(hash: BigNumberish, signature: Signature): Promise<boolean>; | ||
/** | ||
* Verify a signature of a given hash | ||
* @warning This method is not recommended, use verifyMessage instead | ||
* | ||
* @param hash - hash to be verified | ||
* @param signature - signature of the hash | ||
* @returns true if the signature is valid, false otherwise | ||
* @throws {Error} if the signature is not a valid signature | ||
*/ | ||
verifyMessage(typedData: TypedData, signature: Signature): Promise<boolean>; | ||
} |
@@ -84,2 +84,3 @@ "use strict"; | ||
var minimalistic_assert_1 = __importDefault(require("minimalistic-assert")); | ||
var contract_1 = require("../contract"); | ||
var provider_1 = require("../provider"); | ||
@@ -184,4 +185,60 @@ var ellipticCurve_1 = require("../utils/ellipticCurve"); | ||
}; | ||
/** | ||
* Verify a signature of a JSON object | ||
* | ||
* @param json - JSON object to be verified | ||
* @param signature - signature of the JSON object | ||
* @returns true if the signature is valid, false otherwise | ||
* @throws {Error} if the JSON object is not a valid JSON or the signature is not a valid signature | ||
*/ | ||
Signer.prototype.verifyMessageHash = function (hash, signature) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var _a; | ||
return __generator(this, function (_b) { | ||
switch (_b.label) { | ||
case 0: | ||
_b.trys.push([0, 2, , 3]); | ||
return [4 /*yield*/, this.callContract({ | ||
contract_address: this.address, | ||
entry_point_selector: (0, stark_1.getSelectorFromName)('is_valid_signature'), | ||
calldata: (0, contract_1.compileCalldata)({ | ||
hash: (0, number_1.toBN)(hash).toString(), | ||
signature: signature.map(function (x) { return (0, number_1.toBN)(x).toString(); }), | ||
}), | ||
})]; | ||
case 1: | ||
_b.sent(); | ||
return [2 /*return*/, true]; | ||
case 2: | ||
_a = _b.sent(); | ||
return [2 /*return*/, false]; | ||
case 3: return [2 /*return*/]; | ||
} | ||
}); | ||
}); | ||
}; | ||
/** | ||
* Verify a signature of a given hash | ||
* @warning This method is not recommended, use verifyMessage instead | ||
* | ||
* @param hash - hash to be verified | ||
* @param signature - signature of the hash | ||
* @returns true if the signature is valid, false otherwise | ||
* @throws {Error} if the signature is not a valid signature | ||
*/ | ||
Signer.prototype.verifyMessage = function (typedData, signature) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var hash; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, this.hashMessage(typedData)]; | ||
case 1: | ||
hash = _a.sent(); | ||
return [2 /*return*/, this.verifyMessageHash(hash, signature)]; | ||
} | ||
}); | ||
}); | ||
}; | ||
return Signer; | ||
}(provider_1.Provider)); | ||
exports.Signer = Signer; |
import { Provider } from '../provider'; | ||
import { AddTransactionResponse, Signature, Transaction } from '../types'; | ||
import { BigNumberish } from '../utils/number'; | ||
import { TypedData } from '../utils/typedData/types'; | ||
@@ -33,2 +34,21 @@ export declare abstract class SignerInterface extends Provider { | ||
abstract hashMessage(typedData: TypedData): Promise<string>; | ||
/** | ||
* Verify a signature of a JSON object | ||
* | ||
* @param json - JSON object to be verified | ||
* @param signature - signature of the JSON object | ||
* @returns true if the signature is valid, false otherwise | ||
* @throws {Error} if the JSON object is not a valid JSON or the signature is not a valid signature | ||
*/ | ||
abstract verifyMessage(typedData: TypedData, signature: Signature): Promise<boolean>; | ||
/** | ||
* Verify a signature of a given hash | ||
* @warning This method is not recommended, use verifyMessage instead | ||
* | ||
* @param hash - hash to be verified | ||
* @param signature - signature of the hash | ||
* @returns true if the signature is valid, false otherwise | ||
* @throws {Error} if the signature is not a valid signature | ||
*/ | ||
abstract verifyMessageHash(hash: BigNumberish, signature: Signature): Promise<boolean>; | ||
} |
{ | ||
"name": "starknet", | ||
"version": "2.7.2", | ||
"version": "2.8.0", | ||
"description": "JavaScript library for StarkNet", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
import { Provider } from '../provider'; | ||
import { AddTransactionResponse, KeyPair, Signature, Transaction } from '../types'; | ||
import { BigNumberish } from '../utils/number'; | ||
import { TypedData } from '../utils/typedData'; | ||
@@ -34,2 +35,21 @@ import { SignerInterface } from './interface'; | ||
hashMessage(typedData: TypedData): Promise<string>; | ||
/** | ||
* Verify a signature of a JSON object | ||
* | ||
* @param json - JSON object to be verified | ||
* @param signature - signature of the JSON object | ||
* @returns true if the signature is valid, false otherwise | ||
* @throws {Error} if the JSON object is not a valid JSON or the signature is not a valid signature | ||
*/ | ||
verifyMessageHash(hash: BigNumberish, signature: Signature): Promise<boolean>; | ||
/** | ||
* Verify a signature of a given hash | ||
* @warning This method is not recommended, use verifyMessage instead | ||
* | ||
* @param hash - hash to be verified | ||
* @param signature - signature of the hash | ||
* @returns true if the signature is valid, false otherwise | ||
* @throws {Error} if the signature is not a valid signature | ||
*/ | ||
verifyMessage(typedData: TypedData, signature: Signature): Promise<boolean>; | ||
} |
@@ -200,2 +200,3 @@ 'use strict'; | ||
var minimalistic_assert_1 = __importDefault(require('minimalistic-assert')); | ||
var contract_1 = require('../contract'); | ||
var provider_1 = require('../provider'); | ||
@@ -327,4 +328,67 @@ var ellipticCurve_1 = require('../utils/ellipticCurve'); | ||
}; | ||
/** | ||
* Verify a signature of a JSON object | ||
* | ||
* @param json - JSON object to be verified | ||
* @param signature - signature of the JSON object | ||
* @returns true if the signature is valid, false otherwise | ||
* @throws {Error} if the JSON object is not a valid JSON or the signature is not a valid signature | ||
*/ | ||
Signer.prototype.verifyMessageHash = function (hash, signature) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var _a; | ||
return __generator(this, function (_b) { | ||
switch (_b.label) { | ||
case 0: | ||
_b.trys.push([0, 2, , 3]); | ||
return [ | ||
4 /*yield*/, | ||
this.callContract({ | ||
contract_address: this.address, | ||
entry_point_selector: (0, stark_1.getSelectorFromName)('is_valid_signature'), | ||
calldata: (0, contract_1.compileCalldata)({ | ||
hash: (0, number_1.toBN)(hash).toString(), | ||
signature: signature.map(function (x) { | ||
return (0, number_1.toBN)(x).toString(); | ||
}), | ||
}), | ||
}), | ||
]; | ||
case 1: | ||
_b.sent(); | ||
return [2 /*return*/, true]; | ||
case 2: | ||
_a = _b.sent(); | ||
return [2 /*return*/, false]; | ||
case 3: | ||
return [2 /*return*/]; | ||
} | ||
}); | ||
}); | ||
}; | ||
/** | ||
* Verify a signature of a given hash | ||
* @warning This method is not recommended, use verifyMessage instead | ||
* | ||
* @param hash - hash to be verified | ||
* @param signature - signature of the hash | ||
* @returns true if the signature is valid, false otherwise | ||
* @throws {Error} if the signature is not a valid signature | ||
*/ | ||
Signer.prototype.verifyMessage = function (typedData, signature) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var hash; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
return [4 /*yield*/, this.hashMessage(typedData)]; | ||
case 1: | ||
hash = _a.sent(); | ||
return [2 /*return*/, this.verifyMessageHash(hash, signature)]; | ||
} | ||
}); | ||
}); | ||
}; | ||
return Signer; | ||
})(provider_1.Provider); | ||
exports.Signer = Signer; |
import { Provider } from '../provider'; | ||
import { AddTransactionResponse, Signature, Transaction } from '../types'; | ||
import { BigNumberish } from '../utils/number'; | ||
import { TypedData } from '../utils/typedData/types'; | ||
@@ -33,2 +34,21 @@ export declare abstract class SignerInterface extends Provider { | ||
abstract hashMessage(typedData: TypedData): Promise<string>; | ||
/** | ||
* Verify a signature of a JSON object | ||
* | ||
* @param json - JSON object to be verified | ||
* @param signature - signature of the JSON object | ||
* @returns true if the signature is valid, false otherwise | ||
* @throws {Error} if the JSON object is not a valid JSON or the signature is not a valid signature | ||
*/ | ||
abstract verifyMessage(typedData: TypedData, signature: Signature): Promise<boolean>; | ||
/** | ||
* Verify a signature of a given hash | ||
* @warning This method is not recommended, use verifyMessage instead | ||
* | ||
* @param hash - hash to be verified | ||
* @param signature - signature of the hash | ||
* @returns true if the signature is valid, false otherwise | ||
* @throws {Error} if the signature is not a valid signature | ||
*/ | ||
abstract verifyMessageHash(hash: BigNumberish, signature: Signature): Promise<boolean>; | ||
} |
import assert from 'minimalistic-assert'; | ||
import { compileCalldata } from '../contract'; | ||
import { Provider } from '../provider'; | ||
@@ -8,3 +9,3 @@ import { AddTransactionResponse, KeyPair, Signature, Transaction } from '../types'; | ||
import { hashMessage } from '../utils/hash'; | ||
import { toBN } from '../utils/number'; | ||
import { BigNumberish, toBN } from '../utils/number'; | ||
import { getSelectorFromName } from '../utils/stark'; | ||
@@ -102,2 +103,40 @@ import { TypedData, getMessageHash } from '../utils/typedData'; | ||
} | ||
/** | ||
* Verify a signature of a JSON object | ||
* | ||
* @param json - JSON object to be verified | ||
* @param signature - signature of the JSON object | ||
* @returns true if the signature is valid, false otherwise | ||
* @throws {Error} if the JSON object is not a valid JSON or the signature is not a valid signature | ||
*/ | ||
public async verifyMessageHash(hash: BigNumberish, signature: Signature): Promise<boolean> { | ||
try { | ||
await this.callContract({ | ||
contract_address: this.address, | ||
entry_point_selector: getSelectorFromName('is_valid_signature'), | ||
calldata: compileCalldata({ | ||
hash: toBN(hash).toString(), | ||
signature: signature.map((x) => toBN(x).toString()), | ||
}), | ||
}); | ||
return true; | ||
} catch { | ||
return false; | ||
} | ||
} | ||
/** | ||
* Verify a signature of a given hash | ||
* @warning This method is not recommended, use verifyMessage instead | ||
* | ||
* @param hash - hash to be verified | ||
* @param signature - signature of the hash | ||
* @returns true if the signature is valid, false otherwise | ||
* @throws {Error} if the signature is not a valid signature | ||
*/ | ||
public async verifyMessage(typedData: TypedData, signature: Signature): Promise<boolean> { | ||
const hash = await this.hashMessage(typedData); | ||
return this.verifyMessageHash(hash, signature); | ||
} | ||
} |
import { Provider } from '../provider'; | ||
import { AddTransactionResponse, Signature, Transaction } from '../types'; | ||
import { BigNumberish } from '../utils/number'; | ||
import { TypedData } from '../utils/typedData/types'; | ||
@@ -38,2 +39,23 @@ | ||
public abstract hashMessage(typedData: TypedData): Promise<string>; | ||
/** | ||
* Verify a signature of a JSON object | ||
* | ||
* @param json - JSON object to be verified | ||
* @param signature - signature of the JSON object | ||
* @returns true if the signature is valid, false otherwise | ||
* @throws {Error} if the JSON object is not a valid JSON or the signature is not a valid signature | ||
*/ | ||
public abstract verifyMessage(typedData: TypedData, signature: Signature): Promise<boolean>; | ||
/** | ||
* Verify a signature of a given hash | ||
* @warning This method is not recommended, use verifyMessage instead | ||
* | ||
* @param hash - hash to be verified | ||
* @param signature - signature of the hash | ||
* @returns true if the signature is valid, false otherwise | ||
* @throws {Error} if the signature is not a valid signature | ||
*/ | ||
public abstract verifyMessageHash(hash: BigNumberish, signature: Signature): Promise<boolean>; | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
5453835
145
141687