Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use noble-hashes and noble-curves #315

Merged
merged 5 commits into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
},
"dependencies": {
"@metamask/utils": "^8.1.0",
"@noble/secp256k1": "^1.7.1",
"@noble/curves": "^1.2.0",
"@noble/hashes": "^1.3.2",
"superstruct": "^1.0.3"
},
"devDependencies": {
Expand All @@ -43,7 +44,6 @@
"@metamask/snaps-controllers": "^3.4.0",
"@metamask/snaps-utils": "^5.0.0",
"@noble/curves": "^1.2.0",
"@noble/hashes": "^1.3.2",
"@types/jest": "^28.1.6",
"@types/node": "^17.0.23",
"@typescript-eslint/eslint-plugin": "^5.43.0",
Expand Down
2 changes: 1 addition & 1 deletion scripts/create-key.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { bytesToHex, hasProperty } from '@metamask/utils';
import * as secp256k1 from '@noble/secp256k1';
import { secp256k1 } from '@noble/curves/secp256k1';
import assert from 'assert';
import * as dotenv from 'dotenv';
import fs from 'fs/promises';
Expand Down
2 changes: 1 addition & 1 deletion scripts/verify-registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ async function main() {
assertStruct(signature, SignatureStruct);
const registry = await fs.readFile(registryPath, 'utf-8');

const isValid = await verify({ registry, signature, publicKey });
const isValid = verify({ registry, signature, publicKey });
if (!isValid) {
console.error('Signature is invalid.');
// eslint-disable-next-line n/no-process-exit
Expand Down
8 changes: 4 additions & 4 deletions src/verify.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const MOCK_SIGNATURE = {
describe('verify', () => {
it('verifies a valid signature', async () => {
expect(
await verify({
verify({
registry: MOCK_REGISTRY,
signature: MOCK_SIGNATURE,
publicKey: MOCK_PUBLIC_KEY,
Expand All @@ -23,7 +23,7 @@ describe('verify', () => {

it('rejects an invalid signature', async () => {
expect(
await verify({
verify({
registry: MOCK_REGISTRY,
signature: {
...MOCK_SIGNATURE,
Expand All @@ -36,7 +36,7 @@ describe('verify', () => {
});

it('throws an error if the signature format is invalid', async () => {
await expect(
expect(() =>
verify({
registry: MOCK_REGISTRY,
signature: {
Expand All @@ -45,7 +45,7 @@ describe('verify', () => {
},
publicKey: MOCK_PUBLIC_KEY,
}),
).rejects.toThrow(
).toThrow(
'Invalid signature object: At path: signature -- Expected a string, but received: undefined.',
);
});
Expand Down
17 changes: 7 additions & 10 deletions src/verify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,8 @@ import {
assertStruct,
hexToBytes,
} from '@metamask/utils';
import {
verify as nobleVerify,
utils,
Signature as NobleSignature,
} from '@noble/secp256k1';
import { secp256k1 } from '@noble/curves/secp256k1';
import { sha256 } from '@noble/hashes/sha256';
import type { Infer } from 'superstruct';
import { literal, object, pattern, string } from 'superstruct';

Expand Down Expand Up @@ -37,18 +34,18 @@ type VerifyArgs = {
* the signature to.
* @returns Whether the signature is valid.
*/
export async function verify({
export function verify({
registry,
signature,
publicKey,
}: VerifyArgs): Promise<boolean> {
}: VerifyArgs): boolean {
assertStruct(signature, SignatureStruct, 'Invalid signature object');

const publicKeyBytes = hexToBytes(publicKey);

return nobleVerify(
NobleSignature.fromHex(remove0x(signature.signature)),
await utils.sha256(stringToBytes(registry)),
return secp256k1.verify(
remove0x(signature.signature),
sha256(stringToBytes(registry)),
publicKeyBytes,
);
}
3 changes: 1 addition & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1182,7 +1182,6 @@ __metadata:
"@metamask/utils": ^8.1.0
"@noble/curves": ^1.2.0
"@noble/hashes": ^1.3.2
"@noble/secp256k1": ^1.7.1
"@types/jest": ^28.1.6
"@types/node": ^17.0.23
"@typescript-eslint/eslint-plugin": ^5.43.0
Expand Down Expand Up @@ -1351,7 +1350,7 @@ __metadata:
languageName: node
linkType: hard

"@noble/secp256k1@npm:^1.5.5, @noble/secp256k1@npm:^1.7.1":
"@noble/secp256k1@npm:^1.5.5":
version: 1.7.1
resolution: "@noble/secp256k1@npm:1.7.1"
checksum: d2301f1f7690368d8409a3152450458f27e54df47e3f917292de3de82c298770890c2de7c967d237eff9c95b70af485389a9695f73eb05a43e2bd562d18b18cb
Expand Down