|
| 1 | +'use strict'; |
| 2 | +// Reference https://github.com/bitcoin/bips/blob/master/bip-0066.mediawiki |
| 3 | +// Format: 0x30 [total-length] 0x02 [R-length] [R] 0x02 [S-length] [S] |
| 4 | +// NOTE: SIGHASH byte ignored AND restricted, truncate before use |
| 5 | +Object.defineProperty(exports, '__esModule', { value: true }); |
| 6 | +exports.encode = exports.decode = exports.check = void 0; |
| 7 | +function check(buffer) { |
| 8 | + if (buffer.length < 8) return false; |
| 9 | + if (buffer.length > 72) return false; |
| 10 | + if (buffer[0] !== 0x30) return false; |
| 11 | + if (buffer[1] !== buffer.length - 2) return false; |
| 12 | + if (buffer[2] !== 0x02) return false; |
| 13 | + const lenR = buffer[3]; |
| 14 | + if (lenR === 0) return false; |
| 15 | + if (5 + lenR >= buffer.length) return false; |
| 16 | + if (buffer[4 + lenR] !== 0x02) return false; |
| 17 | + const lenS = buffer[5 + lenR]; |
| 18 | + if (lenS === 0) return false; |
| 19 | + if (6 + lenR + lenS !== buffer.length) return false; |
| 20 | + if (buffer[4] & 0x80) return false; |
| 21 | + if (lenR > 1 && buffer[4] === 0x00 && !(buffer[5] & 0x80)) return false; |
| 22 | + if (buffer[lenR + 6] & 0x80) return false; |
| 23 | + if (lenS > 1 && buffer[lenR + 6] === 0x00 && !(buffer[lenR + 7] & 0x80)) |
| 24 | + return false; |
| 25 | + return true; |
| 26 | +} |
| 27 | +exports.check = check; |
| 28 | +function decode(buffer) { |
| 29 | + if (buffer.length < 8) throw new Error('DER sequence length is too short'); |
| 30 | + if (buffer.length > 72) throw new Error('DER sequence length is too long'); |
| 31 | + if (buffer[0] !== 0x30) throw new Error('Expected DER sequence'); |
| 32 | + if (buffer[1] !== buffer.length - 2) |
| 33 | + throw new Error('DER sequence length is invalid'); |
| 34 | + if (buffer[2] !== 0x02) throw new Error('Expected DER integer'); |
| 35 | + const lenR = buffer[3]; |
| 36 | + if (lenR === 0) throw new Error('R length is zero'); |
| 37 | + if (5 + lenR >= buffer.length) throw new Error('R length is too long'); |
| 38 | + if (buffer[4 + lenR] !== 0x02) throw new Error('Expected DER integer (2)'); |
| 39 | + const lenS = buffer[5 + lenR]; |
| 40 | + if (lenS === 0) throw new Error('S length is zero'); |
| 41 | + if (6 + lenR + lenS !== buffer.length) throw new Error('S length is invalid'); |
| 42 | + if (buffer[4] & 0x80) throw new Error('R value is negative'); |
| 43 | + if (lenR > 1 && buffer[4] === 0x00 && !(buffer[5] & 0x80)) |
| 44 | + throw new Error('R value excessively padded'); |
| 45 | + if (buffer[lenR + 6] & 0x80) throw new Error('S value is negative'); |
| 46 | + if (lenS > 1 && buffer[lenR + 6] === 0x00 && !(buffer[lenR + 7] & 0x80)) |
| 47 | + throw new Error('S value excessively padded'); |
| 48 | + // non-BIP66 - extract R, S values |
| 49 | + return { |
| 50 | + r: buffer.slice(4, 4 + lenR), |
| 51 | + s: buffer.slice(6 + lenR), |
| 52 | + }; |
| 53 | +} |
| 54 | +exports.decode = decode; |
| 55 | +/* |
| 56 | + * Expects r and s to be positive DER integers. |
| 57 | + * |
| 58 | + * The DER format uses the most significant bit as a sign bit (& 0x80). |
| 59 | + * If the significant bit is set AND the integer is positive, a 0x00 is prepended. |
| 60 | + * |
| 61 | + * Examples: |
| 62 | + * |
| 63 | + * 0 => 0x00 |
| 64 | + * 1 => 0x01 |
| 65 | + * -1 => 0xff |
| 66 | + * 127 => 0x7f |
| 67 | + * -127 => 0x81 |
| 68 | + * 128 => 0x0080 |
| 69 | + * -128 => 0x80 |
| 70 | + * 255 => 0x00ff |
| 71 | + * -255 => 0xff01 |
| 72 | + * 16300 => 0x3fac |
| 73 | + * -16300 => 0xc054 |
| 74 | + * 62300 => 0x00f35c |
| 75 | + * -62300 => 0xff0ca4 |
| 76 | + */ |
| 77 | +function encode(r, s) { |
| 78 | + const lenR = r.length; |
| 79 | + const lenS = s.length; |
| 80 | + if (lenR === 0) throw new Error('R length is zero'); |
| 81 | + if (lenS === 0) throw new Error('S length is zero'); |
| 82 | + if (lenR > 33) throw new Error('R length is too long'); |
| 83 | + if (lenS > 33) throw new Error('S length is too long'); |
| 84 | + if (r[0] & 0x80) throw new Error('R value is negative'); |
| 85 | + if (s[0] & 0x80) throw new Error('S value is negative'); |
| 86 | + if (lenR > 1 && r[0] === 0x00 && !(r[1] & 0x80)) |
| 87 | + throw new Error('R value excessively padded'); |
| 88 | + if (lenS > 1 && s[0] === 0x00 && !(s[1] & 0x80)) |
| 89 | + throw new Error('S value excessively padded'); |
| 90 | + const signature = Buffer.allocUnsafe(6 + lenR + lenS); |
| 91 | + // 0x30 [total-length] 0x02 [R-length] [R] 0x02 [S-length] [S] |
| 92 | + signature[0] = 0x30; |
| 93 | + signature[1] = signature.length - 2; |
| 94 | + signature[2] = 0x02; |
| 95 | + signature[3] = r.length; |
| 96 | + r.copy(signature, 4); |
| 97 | + signature[4 + lenR] = 0x02; |
| 98 | + signature[5 + lenR] = s.length; |
| 99 | + s.copy(signature, 6 + lenR); |
| 100 | + return signature; |
| 101 | +} |
| 102 | +exports.encode = encode; |
0 commit comments