From 1d0c9bc890ba453d6bca3ffe13242eb9c8a45a54 Mon Sep 17 00:00:00 2001 From: junderw Date: Wed, 26 Jun 2024 17:17:49 +0900 Subject: [PATCH] KISS fixes --- package-lock.json | 14 +++---- package.json | 2 +- src/cjs/index.cjs | 102 +++++++++++++++++++-------------------------- src/esm/index.js | 95 +++++++++++++++++------------------------ test/fixtures.json | 4 +- ts_src/index.ts | 79 +++++++++++++++-------------------- tsconfig.base.json | 2 - tsconfig.cjs.json | 2 +- tsconfig.json | 7 ++-- 9 files changed, 128 insertions(+), 179 deletions(-) diff --git a/package-lock.json b/package-lock.json index 17e5665..1f77d04 100644 --- a/package-lock.json +++ b/package-lock.json @@ -14,7 +14,7 @@ "rimraf": "^5.0.7", "tape": "^5.3.0", "ts-standard": "^12.0.2", - "typescript": "^5.5.2" + "typescript": "~5.1.0" } }, "node_modules/@bcoe/v8-coverage": { @@ -4620,9 +4620,9 @@ } }, "node_modules/typescript": { - "version": "5.5.2", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.5.2.tgz", - "integrity": "sha512-NcRtPEOsPFFWjobJEtfihkLCZCXZt/os3zf8nTxjVH3RvTSxjrCamJpbExGvYOF+tFHc3pA65qpdwPbzjohhew==", + "version": "5.1.6", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.1.6.tgz", + "integrity": "sha512-zaWCozRZ6DLEWAWFrVDz1H6FVXzUSfTy5FUMWsQlU8Ym5JP9eO4xkTIROFCQvhQf61z6O/G6ugw3SgAnvvm+HA==", "dev": true, "bin": { "tsc": "bin/tsc", @@ -8102,9 +8102,9 @@ } }, "typescript": { - "version": "5.5.2", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.5.2.tgz", - "integrity": "sha512-NcRtPEOsPFFWjobJEtfihkLCZCXZt/os3zf8nTxjVH3RvTSxjrCamJpbExGvYOF+tFHc3pA65qpdwPbzjohhew==", + "version": "5.1.6", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.1.6.tgz", + "integrity": "sha512-zaWCozRZ6DLEWAWFrVDz1H6FVXzUSfTy5FUMWsQlU8Ym5JP9eO4xkTIROFCQvhQf61z6O/G6ugw3SgAnvvm+HA==", "dev": true }, "unbox-primitive": { diff --git a/package.json b/package.json index 9d56c68..a06aafd 100644 --- a/package.json +++ b/package.json @@ -47,6 +47,6 @@ "rimraf": "^5.0.7", "tape": "^5.3.0", "ts-standard": "^12.0.2", - "typescript": "^5.5.2" + "typescript": "~5.1.0" } } diff --git a/src/cjs/index.cjs b/src/cjs/index.cjs index 8251d6e..e52532d 100644 --- a/src/cjs/index.cjs +++ b/src/cjs/index.cjs @@ -2,81 +2,63 @@ // Reference https://github.com/bitcoin/bips/blob/master/bip-0066.mediawiki // Format: 0x30 [total-length] 0x02 [R-length] [R] 0x02 [S-length] [S] // NOTE: SIGHASH byte ignored AND restricted, truncate before use -/* eslint @typescript-eslint/strict-boolean-expressions: 0 */ Object.defineProperty(exports, "__esModule", { value: true }); -exports.check = check; -exports.decode = decode; -exports.encode = encode; +exports.encode = exports.decode = exports.check = void 0; function check(buffer) { - if (buffer.length < 8) - return false; - if (buffer.length > 72) + var ret = internalCheck(buffer); + if (typeof ret === 'string') { return false; - if (buffer[0] !== 0x30) - return false; - if (buffer[1] !== buffer.length - 2) - return false; - if (buffer[2] !== 0x02) - return false; - var lenR = buffer[3]; - if (lenR === 0) - return false; - if (5 + lenR >= buffer.length) - return false; - if (buffer[4 + lenR] !== 0x02) - return false; - var lenS = buffer[5 + lenR]; - if (lenS === 0) - return false; - if ((6 + lenR + lenS) !== buffer.length) - return false; - if (buffer[4] & 0x80) - return false; - if (lenR > 1 && (buffer[4] === 0x00) && !(buffer[5] & 0x80)) - return false; - if (buffer[lenR + 6] & 0x80) - return false; - if (lenS > 1 && (buffer[lenR + 6] === 0x00) && !(buffer[lenR + 7] & 0x80)) - return false; - return true; + } + else { + return true; + } } -function decode(buffer) { +exports.check = check; +function internalCheck(buffer) { if (buffer.length < 8) - throw new Error('DER sequence length is too short'); + return 'DER sequence length is too short'; if (buffer.length > 72) - throw new Error('DER sequence length is too long'); + return 'DER sequence length is too long'; if (buffer[0] !== 0x30) - throw new Error('Expected DER sequence'); + return 'Expected DER sequence (48)'; if (buffer[1] !== buffer.length - 2) - throw new Error('DER sequence length is invalid'); + return 'DER sequence length is invalid'; if (buffer[2] !== 0x02) - throw new Error('Expected DER integer'); + return 'Expected DER integer (2)'; var lenR = buffer[3]; if (lenR === 0) - throw new Error('R length is zero'); + return 'R length is zero'; if (5 + lenR >= buffer.length) - throw new Error('R length is too long'); + return 'R length is too long'; if (buffer[4 + lenR] !== 0x02) - throw new Error('Expected DER integer (2)'); + return 'Expected DER integer (2)'; var lenS = buffer[5 + lenR]; if (lenS === 0) - throw new Error('S length is zero'); + return 'S length is zero'; if ((6 + lenR + lenS) !== buffer.length) - throw new Error('S length is invalid'); - if (buffer[4] & 0x80) - throw new Error('R value is negative'); - if (lenR > 1 && (buffer[4] === 0x00) && !(buffer[5] & 0x80)) - throw new Error('R value excessively padded'); - if (buffer[lenR + 6] & 0x80) - throw new Error('S value is negative'); - if (lenS > 1 && (buffer[lenR + 6] === 0x00) && !(buffer[lenR + 7] & 0x80)) - throw new Error('S value excessively padded'); + return 'S length is invalid'; + if ((buffer[4] & 0x80) !== 0) + return 'R value is negative'; + if (lenR > 1 && (buffer[4] === 0x00) && (buffer[5] & 0x80) === 0) + return 'R value excessively padded'; + if ((buffer[lenR + 6] & 0x80) !== 0) + return 'S value is negative'; + if (lenS > 1 && (buffer[lenR + 6] === 0x00) && (buffer[lenR + 7] & 0x80) === 0) + return 'S value excessively padded'; + return lenR; +} +function decode(buffer) { + var ret = internalCheck(buffer); + if (typeof ret === 'string') { + throw new Error(ret); + } // non-BIP66 - extract R, S values return { - r: buffer.subarray(4, 4 + lenR), - s: buffer.subarray(6 + lenR) + r: buffer.subarray(4, 4 + ret), + s: buffer.subarray(6 + ret) }; } +exports.decode = decode; /* * Expects r and s to be positive DER integers. * @@ -110,13 +92,13 @@ function encode(r, s) { throw new Error('R length is too long'); if (lenS > 33) throw new Error('S length is too long'); - if (r[0] & 0x80) + if ((r[0] & 0x80) !== 0) throw new Error('R value is negative'); - if (s[0] & 0x80) + if ((s[0] & 0x80) !== 0) throw new Error('S value is negative'); - if (lenR > 1 && (r[0] === 0x00) && !(r[1] & 0x80)) + if (lenR > 1 && (r[0] === 0x00) && (r[1] & 0x80) === 0) throw new Error('R value excessively padded'); - if (lenS > 1 && (s[0] === 0x00) && !(s[1] & 0x80)) + if (lenS > 1 && (s[0] === 0x00) && (s[1] & 0x80) === 0) throw new Error('S value excessively padded'); var signature = new Uint8Array(6 + lenR + lenS); // 0x30 [total-length] 0x02 [R-length] [R] 0x02 [S-length] [S] @@ -130,4 +112,4 @@ function encode(r, s) { signature.set(s, 6 + lenR); return signature; } -/* eslint @typescript-eslint/strict-boolean-expressions: 1 */ +exports.encode = encode; diff --git a/src/esm/index.js b/src/esm/index.js index 16c7ca2..b6c55e0 100644 --- a/src/esm/index.js +++ b/src/esm/index.js @@ -1,75 +1,57 @@ // Reference https://github.com/bitcoin/bips/blob/master/bip-0066.mediawiki // Format: 0x30 [total-length] 0x02 [R-length] [R] 0x02 [S-length] [S] // NOTE: SIGHASH byte ignored AND restricted, truncate before use -/* eslint @typescript-eslint/strict-boolean-expressions: 0 */ export function check(buffer) { - if (buffer.length < 8) - return false; - if (buffer.length > 72) - return false; - if (buffer[0] !== 0x30) - return false; - if (buffer[1] !== buffer.length - 2) - return false; - if (buffer[2] !== 0x02) + var ret = internalCheck(buffer); + if (typeof ret === 'string') { return false; - var lenR = buffer[3]; - if (lenR === 0) - return false; - if (5 + lenR >= buffer.length) - return false; - if (buffer[4 + lenR] !== 0x02) - return false; - var lenS = buffer[5 + lenR]; - if (lenS === 0) - return false; - if ((6 + lenR + lenS) !== buffer.length) - return false; - if (buffer[4] & 0x80) - return false; - if (lenR > 1 && (buffer[4] === 0x00) && !(buffer[5] & 0x80)) - return false; - if (buffer[lenR + 6] & 0x80) - return false; - if (lenS > 1 && (buffer[lenR + 6] === 0x00) && !(buffer[lenR + 7] & 0x80)) - return false; - return true; + } + else { + return true; + } } -export function decode(buffer) { +function internalCheck(buffer) { if (buffer.length < 8) - throw new Error('DER sequence length is too short'); + return 'DER sequence length is too short'; if (buffer.length > 72) - throw new Error('DER sequence length is too long'); + return 'DER sequence length is too long'; if (buffer[0] !== 0x30) - throw new Error('Expected DER sequence'); + return 'Expected DER sequence (48)'; if (buffer[1] !== buffer.length - 2) - throw new Error('DER sequence length is invalid'); + return 'DER sequence length is invalid'; if (buffer[2] !== 0x02) - throw new Error('Expected DER integer'); + return 'Expected DER integer (2)'; var lenR = buffer[3]; if (lenR === 0) - throw new Error('R length is zero'); + return 'R length is zero'; if (5 + lenR >= buffer.length) - throw new Error('R length is too long'); + return 'R length is too long'; if (buffer[4 + lenR] !== 0x02) - throw new Error('Expected DER integer (2)'); + return 'Expected DER integer (2)'; var lenS = buffer[5 + lenR]; if (lenS === 0) - throw new Error('S length is zero'); + return 'S length is zero'; if ((6 + lenR + lenS) !== buffer.length) - throw new Error('S length is invalid'); - if (buffer[4] & 0x80) - throw new Error('R value is negative'); - if (lenR > 1 && (buffer[4] === 0x00) && !(buffer[5] & 0x80)) - throw new Error('R value excessively padded'); - if (buffer[lenR + 6] & 0x80) - throw new Error('S value is negative'); - if (lenS > 1 && (buffer[lenR + 6] === 0x00) && !(buffer[lenR + 7] & 0x80)) - throw new Error('S value excessively padded'); + return 'S length is invalid'; + if ((buffer[4] & 0x80) !== 0) + return 'R value is negative'; + if (lenR > 1 && (buffer[4] === 0x00) && (buffer[5] & 0x80) === 0) + return 'R value excessively padded'; + if ((buffer[lenR + 6] & 0x80) !== 0) + return 'S value is negative'; + if (lenS > 1 && (buffer[lenR + 6] === 0x00) && (buffer[lenR + 7] & 0x80) === 0) + return 'S value excessively padded'; + return lenR; +} +export function decode(buffer) { + var ret = internalCheck(buffer); + if (typeof ret === 'string') { + throw new Error(ret); + } // non-BIP66 - extract R, S values return { - r: buffer.subarray(4, 4 + lenR), - s: buffer.subarray(6 + lenR) + r: buffer.subarray(4, 4 + ret), + s: buffer.subarray(6 + ret) }; } /* @@ -105,13 +87,13 @@ export function encode(r, s) { throw new Error('R length is too long'); if (lenS > 33) throw new Error('S length is too long'); - if (r[0] & 0x80) + if ((r[0] & 0x80) !== 0) throw new Error('R value is negative'); - if (s[0] & 0x80) + if ((s[0] & 0x80) !== 0) throw new Error('S value is negative'); - if (lenR > 1 && (r[0] === 0x00) && !(r[1] & 0x80)) + if (lenR > 1 && (r[0] === 0x00) && (r[1] & 0x80) === 0) throw new Error('R value excessively padded'); - if (lenS > 1 && (s[0] === 0x00) && !(s[1] & 0x80)) + if (lenS > 1 && (s[0] === 0x00) && (s[1] & 0x80) === 0) throw new Error('S value excessively padded'); var signature = new Uint8Array(6 + lenR + lenS); // 0x30 [total-length] 0x02 [R-length] [R] 0x02 [S-length] [S] @@ -125,4 +107,3 @@ export function encode(r, s) { signature.set(s, 6 + lenR); return signature; } -/* eslint @typescript-eslint/strict-boolean-expressions: 1 */ diff --git a/test/fixtures.json b/test/fixtures.json index 4905241..88c173d 100644 --- a/test/fixtures.json +++ b/test/fixtures.json @@ -99,7 +99,7 @@ "DER": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" }, { - "exception": "^Error: Expected DER sequence$", + "exception": "^Error: Expected DER sequence \\(48\\)$", "DER": "00ffff0400ffffff020400ffffff" }, { @@ -111,7 +111,7 @@ "DER": "300c030400ffffff030400ffffff0000" }, { - "exception": "^Error: Expected DER integer$", + "exception": "^Error: Expected DER integer \\(2\\)$", "DER": "300cff0400ffffff020400ffffff" }, { diff --git a/ts_src/index.ts b/ts_src/index.ts index ffe2b88..8ad8c2b 100644 --- a/ts_src/index.ts +++ b/ts_src/index.ts @@ -1,61 +1,52 @@ // Reference https://github.com/bitcoin/bips/blob/master/bip-0066.mediawiki // Format: 0x30 [total-length] 0x02 [R-length] [R] 0x02 [S-length] [S] // NOTE: SIGHASH byte ignored AND restricted, truncate before use -/* eslint @typescript-eslint/strict-boolean-expressions: 0 */ export function check (buffer: Uint8Array): boolean { - if (buffer.length < 8) return false - if (buffer.length > 72) return false - if (buffer[0] !== 0x30) return false - if (buffer[1] !== buffer.length - 2) return false - if (buffer[2] !== 0x02) return false + const ret = internalCheck(buffer) + if (typeof ret === 'string') { + return false + } else { + return true + } +} +function internalCheck (buffer: Uint8Array): string | number { + if (buffer.length < 8) return 'DER sequence length is too short' + if (buffer.length > 72) return 'DER sequence length is too long' + if (buffer[0] !== 0x30) return 'Expected DER sequence (48)' + if (buffer[1] !== buffer.length - 2) return 'DER sequence length is invalid' + + if (buffer[2] !== 0x02) return 'Expected DER integer (2)' const lenR = buffer[3] - if (lenR === 0) return false - if (5 + lenR >= buffer.length) return false - if (buffer[4 + lenR] !== 0x02) return false + if (lenR === 0) return 'R length is zero' + if (5 + lenR >= buffer.length) return 'R length is too long' + if (buffer[4 + lenR] !== 0x02) return 'Expected DER integer (2)' const lenS = buffer[5 + lenR] - if (lenS === 0) return false - if ((6 + lenR + lenS) !== buffer.length) return false + if (lenS === 0) return 'S length is zero' + if ((6 + lenR + lenS) !== buffer.length) return 'S length is invalid' - if (buffer[4] & 0x80) return false - if (lenR > 1 && (buffer[4] === 0x00) && !(buffer[5] & 0x80)) return false + if ((buffer[4] & 0x80) !== 0) return 'R value is negative' + if (lenR > 1 && (buffer[4] === 0x00) && (buffer[5] & 0x80) === 0) return 'R value excessively padded' - if (buffer[lenR + 6] & 0x80) return false - if (lenS > 1 && (buffer[lenR + 6] === 0x00) && !(buffer[lenR + 7] & 0x80)) return false - return true + if ((buffer[lenR + 6] & 0x80) !== 0) return 'S value is negative' + if (lenS > 1 && (buffer[lenR + 6] === 0x00) && (buffer[lenR + 7] & 0x80) === 0) return 'S value excessively padded' + return lenR } export function decode (buffer: Uint8Array): { r: Uint8Array s: Uint8Array } { - if (buffer.length < 8) throw new Error('DER sequence length is too short') - if (buffer.length > 72) throw new Error('DER sequence length is too long') - if (buffer[0] !== 0x30) throw new Error('Expected DER sequence') - if (buffer[1] !== buffer.length - 2) throw new Error('DER sequence length is invalid') - if (buffer[2] !== 0x02) throw new Error('Expected DER integer') - - const lenR = buffer[3] - if (lenR === 0) throw new Error('R length is zero') - if (5 + lenR >= buffer.length) throw new Error('R length is too long') - if (buffer[4 + lenR] !== 0x02) throw new Error('Expected DER integer (2)') - - const lenS = buffer[5 + lenR] - if (lenS === 0) throw new Error('S length is zero') - if ((6 + lenR + lenS) !== buffer.length) throw new Error('S length is invalid') - - if (buffer[4] & 0x80) throw new Error('R value is negative') - if (lenR > 1 && (buffer[4] === 0x00) && !(buffer[5] & 0x80)) throw new Error('R value excessively padded') - - if (buffer[lenR + 6] & 0x80) throw new Error('S value is negative') - if (lenS > 1 && (buffer[lenR + 6] === 0x00) && !(buffer[lenR + 7] & 0x80)) throw new Error('S value excessively padded') - + const ret = internalCheck(buffer) + if (typeof ret === 'string') { + throw new Error(ret) + } // non-BIP66 - extract R, S values return { - r: buffer.subarray(4, 4 + lenR), - s: buffer.subarray(6 + lenR) + r: buffer.subarray(4, 4 + ret), + s: buffer.subarray(6 + ret) } } @@ -88,10 +79,10 @@ export function encode (r: Uint8Array, s: Uint8Array): Uint8Array { if (lenS === 0) throw new Error('S length is zero') if (lenR > 33) throw new Error('R length is too long') if (lenS > 33) throw new Error('S length is too long') - if (r[0] & 0x80) throw new Error('R value is negative') - if (s[0] & 0x80) throw new Error('S value is negative') - if (lenR > 1 && (r[0] === 0x00) && !(r[1] & 0x80)) throw new Error('R value excessively padded') - if (lenS > 1 && (s[0] === 0x00) && !(s[1] & 0x80)) throw new Error('S value excessively padded') + if ((r[0] & 0x80) !== 0) throw new Error('R value is negative') + if ((s[0] & 0x80) !== 0) throw new Error('S value is negative') + if (lenR > 1 && (r[0] === 0x00) && (r[1] & 0x80) === 0) throw new Error('R value excessively padded') + if (lenS > 1 && (s[0] === 0x00) && (s[1] & 0x80) === 0) throw new Error('S value excessively padded') const signature = new Uint8Array(6 + lenR + lenS) @@ -107,5 +98,3 @@ export function encode (r: Uint8Array, s: Uint8Array): Uint8Array { return signature } - -/* eslint @typescript-eslint/strict-boolean-expressions: 1 */ diff --git a/tsconfig.base.json b/tsconfig.base.json index 9fc4ee5..0553a7c 100644 --- a/tsconfig.base.json +++ b/tsconfig.base.json @@ -1,8 +1,6 @@ { "compilerOptions": { "target": "ES5", - "module": "commonjs", - "outDir": "./src", "rootDir": "./ts_src", "types": ["node"], "allowJs": false, diff --git a/tsconfig.cjs.json b/tsconfig.cjs.json index 1417240..71c90e8 100644 --- a/tsconfig.cjs.json +++ b/tsconfig.cjs.json @@ -2,7 +2,7 @@ "extends": "./tsconfig.base.json", "compilerOptions": { "declaration": true, - "emitDeclarationOnly": false, + "esModuleInterop": true, "outDir": "src/cjs", "module": "commonjs" } diff --git a/tsconfig.json b/tsconfig.json index 3567526..596a392 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -2,9 +2,8 @@ "extends": "./tsconfig.base.json", "compilerOptions": { "outDir": "src/esm", - "esModuleInterop": true, "resolveJsonModule": true, - "module": "ESNext", - "moduleResolution": "Node" + "module": "NodeNext", + "moduleResolution": "NodeNext" }, -} \ No newline at end of file +}