diff --git a/packages/core/src/secp256k1/secp256k1.ts b/packages/core/src/secp256k1/secp256k1.ts index 95b97d4d3..b60a7a6ef 100644 --- a/packages/core/src/secp256k1/secp256k1.ts +++ b/packages/core/src/secp256k1/secp256k1.ts @@ -1,11 +1,7 @@ import { randomBytes } from 'crypto'; -import { - ERRORS, - PRIVATE_KEY_MAX_VALUE, - SIGNATURE_LENGTH, - ZERO_BUFFER -} from '../utils'; +import { PRIVATE_KEY_MAX_VALUE, SIGNATURE_LENGTH, ZERO_BUFFER } from '../utils'; import { ec as EC } from 'elliptic'; +import { buildError, SECP256K1 } from '@vechain-sdk/errors'; // Cureve algorithm const curve = new EC('secp256k1'); @@ -54,7 +50,10 @@ function generatePrivateKey(entropy?: () => Buffer): Buffer { */ function derivePublicKey(privateKey: Buffer): Buffer { if (!isValidPrivateKey(privateKey)) { - throw new Error(ERRORS.SECP256K1.INVALID_PRIVATE_KEY); + throw buildError( + SECP256K1.INVALID_SECP256k1_PRIVATE_KEY, + 'Invalid private key given as input. Length must be 32 bytes' + ); } const keyPair = curve.keyFromPrivate(privateKey); return Buffer.from(keyPair.getPublic().encode('array', false)); @@ -67,11 +66,17 @@ function derivePublicKey(privateKey: Buffer): Buffer { */ function sign(msgHash: Buffer, privKey: Buffer): Buffer { if (!isValidMessageHash(msgHash)) { - throw new Error(ERRORS.SECP256K1.INVALID_MESSAGE_HASH); + throw buildError( + SECP256K1.INVALID_SECP256k1_MESSAGE_HASH, + 'Invalid message hash given as input. Length must be 32 bytes' + ); } if (!isValidPrivateKey(privKey)) { - throw new Error(ERRORS.SECP256K1.INVALID_PRIVATE_KEY); + throw buildError( + SECP256K1.INVALID_SECP256k1_PRIVATE_KEY, + 'Invalid private key given as input. Length must be 32 bytes' + ); } const keyPair = curve.keyFromPrivate(privKey); @@ -90,14 +95,23 @@ function sign(msgHash: Buffer, privKey: Buffer): Buffer { */ function recover(msgHash: Buffer, sig: Buffer): Buffer { if (!isValidMessageHash(msgHash)) { - throw new Error(ERRORS.SECP256K1.INVALID_MESSAGE_HASH); + throw buildError( + SECP256K1.INVALID_SECP256k1_MESSAGE_HASH, + 'Invalid message hash given as input. Length must be 32 bytes' + ); } if (!Buffer.isBuffer(sig) || sig.length !== SIGNATURE_LENGTH) { - throw new Error(ERRORS.SECP256K1.INVALID_SIGNATURE); + throw buildError( + SECP256K1.INVALID_SECP256k1_SIGNATURE, + 'Invalid signature given as input. Length must be 65 bytes' + ); } const recovery = sig[64]; if (recovery !== 0 && recovery !== 1) { - throw new Error(ERRORS.SECP256K1.INVALID_SIGNATURE_RECOVERY); + throw buildError( + SECP256K1.INVALID_SECP256k1_SIGNATURE_RECOVERY, + 'Invalid signature recovery given as input. Signature bytes in position 64 must be 0 or 1' + ); } const rCopy = Uint8Array.from(sig); diff --git a/packages/core/src/utils/errors.ts b/packages/core/src/utils/errors.ts index faffe622d..1ebd88f45 100644 --- a/packages/core/src/utils/errors.ts +++ b/packages/core/src/utils/errors.ts @@ -3,16 +3,6 @@ * @constant */ const ERRORS = { - /** - * Error messages related to Secp256k1 cryptographic operations. - */ - SECP256K1: { - INVALID_PRIVATE_KEY: 'Invalid private key', - INVALID_MESSAGE_HASH: 'Invalid message hash', - INVALID_SIGNATURE: 'Invalid signature', - INVALID_SIGNATURE_RECOVERY: 'Invalid signature recovery' - }, - /** * Error messages concerning operations with keystores. */ diff --git a/packages/core/tests/address/address.test.ts b/packages/core/tests/address/address.test.ts index 4b5a1e9a7..392eef268 100644 --- a/packages/core/tests/address/address.test.ts +++ b/packages/core/tests/address/address.test.ts @@ -1,5 +1,5 @@ import { describe, expect, test } from '@jest/globals'; -import { ERRORS, address, secp256k1 } from '../../src'; +import { address, secp256k1 } from '../../src'; import { checksumedAndUnchecksumedAddresses, invalidPrivateKey, @@ -7,7 +7,10 @@ import { simplePrivateKey, simplePublicKey } from './fixture'; -import { InvalidAddressError } from '@vechain-sdk/errors'; +import { + InvalidAddressError, + InvalidSecp256k1PrivateKeyError +} from '@vechain-sdk/errors'; /** * Test address module @@ -51,7 +54,7 @@ describe('Address', () => { // Invalid private key to derive public key expect(() => secp256k1.derivePublicKey(invalidPrivateKey) - ).toThrowError(ERRORS.SECP256K1.INVALID_PRIVATE_KEY); + ).toThrowError(InvalidSecp256k1PrivateKeyError); }); }); diff --git a/packages/core/tests/keystore/keystore.test.ts b/packages/core/tests/keystore/keystore.test.ts index 6997d634e..bafe9dd32 100644 --- a/packages/core/tests/keystore/keystore.test.ts +++ b/packages/core/tests/keystore/keystore.test.ts @@ -2,6 +2,7 @@ import { describe, test, expect } from '@jest/globals'; import { secp256k1, address, ERRORS, keystore } from '../../src'; import { type Keystore } from '../../src'; import { encryptionPassword } from './fixture'; +import { InvalidSecp256k1PrivateKeyError } from '@vechain-sdk/errors'; /** * Keystore tests @@ -41,7 +42,7 @@ describe('Keystore', () => { Buffer.from('wrong private key', 'hex'), encryptionPassword ) - ).rejects.toThrowError(ERRORS.SECP256K1.INVALID_PRIVATE_KEY); + ).rejects.toThrowError(InvalidSecp256k1PrivateKeyError); }); /** diff --git a/packages/core/tests/secp256k1/secp256k1.test.ts b/packages/core/tests/secp256k1/secp256k1.test.ts index bcea50d13..09cfdb95d 100644 --- a/packages/core/tests/secp256k1/secp256k1.test.ts +++ b/packages/core/tests/secp256k1/secp256k1.test.ts @@ -1,5 +1,5 @@ import { describe, test, expect } from '@jest/globals'; -import { ERRORS, ZERO_BUFFER, secp256k1 } from '../../src'; +import { ZERO_BUFFER, secp256k1 } from '../../src'; import { invalidMessageHashes, invalidPrivateKeys, @@ -11,6 +11,12 @@ import { validMessageHashes, validPrivateKeys } from './fixture'; +import { + InvalidSecp256k1MessageHashError, + InvalidSecp256k1PrivateKeyError, + InvalidSecp256k1SignatureError, + InvalidSecp256k1SignatureRecoveryError +} from '@vechain-sdk/errors'; /** * Secp256k1 tests @@ -68,7 +74,7 @@ describe('Secp256k1', () => { // Invalid private key expect(() => secp256k1.derivePublicKey(ZERO_BUFFER(32))).toThrowError( - ERRORS.SECP256K1.INVALID_PRIVATE_KEY + InvalidSecp256k1PrivateKeyError ); }); @@ -83,7 +89,7 @@ describe('Secp256k1', () => { // Invalid message hash expect(() => secp256k1.sign(Buffer.from('some_invalid_stuff', 'hex'), privateKey) - ).toThrowError(ERRORS.SECP256K1.INVALID_MESSAGE_HASH); + ).toThrowError(InvalidSecp256k1MessageHashError); // Invalid private key expect(() => @@ -94,7 +100,7 @@ describe('Secp256k1', () => { 'hex' ) ) - ).toThrowError(ERRORS.SECP256K1.INVALID_PRIVATE_KEY); + ).toThrowError(InvalidSecp256k1PrivateKeyError); }); /** @@ -111,7 +117,7 @@ describe('Secp256k1', () => { Buffer.from('some_invalid_stuff', 'hex'), signature ) - ).toThrowError(ERRORS.SECP256K1.INVALID_MESSAGE_HASH); + ).toThrowError(InvalidSecp256k1MessageHashError); // Invalid signature expect(() => @@ -119,7 +125,7 @@ describe('Secp256k1', () => { messageHashBuffer, Buffer.from('some_invalid_stuff', 'hex') ) - ).toThrowError(ERRORS.SECP256K1.INVALID_SIGNATURE); + ).toThrowError(InvalidSecp256k1SignatureError); // Invalid signature recovery const invalidSignatureRecovery = new Uint8Array(signature); @@ -129,7 +135,7 @@ describe('Secp256k1', () => { messageHashBuffer, Buffer.from(invalidSignatureRecovery) ) - ).toThrowError(ERRORS.SECP256K1.INVALID_SIGNATURE_RECOVERY); + ).toThrowError(InvalidSecp256k1SignatureRecoveryError); }); /**