Skip to content

Commit

Permalink
KISS fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
junderw committed Jun 26, 2024
1 parent 11a7438 commit 1d0c9bc
Show file tree
Hide file tree
Showing 9 changed files with 128 additions and 179 deletions.
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
102 changes: 42 additions & 60 deletions src/cjs/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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]
Expand All @@ -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;
95 changes: 38 additions & 57 deletions src/esm/index.js
Original file line number Diff line number Diff line change
@@ -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)
};
}
/*
Expand Down Expand Up @@ -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]
Expand All @@ -125,4 +107,3 @@ export function encode(r, s) {
signature.set(s, 6 + lenR);
return signature;
}
/* eslint @typescript-eslint/strict-boolean-expressions: 1 */
4 changes: 2 additions & 2 deletions test/fixtures.json
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@
"DER": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
},
{
"exception": "^Error: Expected DER sequence$",
"exception": "^Error: Expected DER sequence \\(48\\)$",
"DER": "00ffff0400ffffff020400ffffff"
},
{
Expand All @@ -111,7 +111,7 @@
"DER": "300c030400ffffff030400ffffff0000"
},
{
"exception": "^Error: Expected DER integer$",
"exception": "^Error: Expected DER integer \\(2\\)$",
"DER": "300cff0400ffffff020400ffffff"
},
{
Expand Down
Loading

0 comments on commit 1d0c9bc

Please sign in to comment.