Skip to content

Commit c58ada3

Browse files
committed
rm ECSignature, add script.signature instead
1 parent 77e317d commit c58ada3

File tree

14 files changed

+335
-149
lines changed

14 files changed

+335
-149
lines changed

src/ecdsa.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ var typeforce = require('typeforce')
44
var types = require('./types')
55

66
var BigInteger = require('bigi')
7-
var ECSignature = require('./ecsignature')
87

98
var ZERO = Buffer.alloc(1, 0)
109
var ONE = Buffer.alloc(1, 1)
@@ -102,7 +101,10 @@ function sign (hash, d) {
102101
s = n.subtract(s)
103102
}
104103

105-
return new ECSignature(r, s)
104+
return {
105+
r: r,
106+
s: s
107+
}
106108
}
107109

108110
function verify (hash, signature, Q) {

src/ecsignature.js

Lines changed: 0 additions & 97 deletions
This file was deleted.

src/index.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ for (var key in templates) {
88
module.exports = {
99
Block: require('./block'),
1010
ECPair: require('./ecpair'),
11-
ECSignature: require('./ecsignature'),
1211
HDNode: require('./hdnode'),
1312
Transaction: require('./transaction'),
1413
TransactionBuilder: require('./transaction_builder'),

src/script.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ module.exports = {
206206
toStack: toStack,
207207

208208
number: require('./script_number'),
209+
signature: require('./script_signature'),
209210

210211
isCanonicalPubKey: isCanonicalPubKey,
211212
isCanonicalSignature: isCanonicalSignature,

src/script_signature.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
var bip66 = require('bip66')
2+
var BigInteger = require('bigi')
3+
var typeforce = require('typeforce')
4+
var types = require('./types')
5+
6+
// BIP62: 1 byte hashType flag (only 0x01, 0x02, 0x03, 0x81, 0x82 and 0x83 are allowed)
7+
function decode (buffer) {
8+
var hashType = buffer.readUInt8(buffer.length - 1)
9+
var hashTypeMod = hashType & ~0x80
10+
if (hashTypeMod <= 0 || hashTypeMod >= 4) throw new Error('Invalid hashType ' + hashType)
11+
12+
var decode = bip66.decode(buffer.slice(0, -1))
13+
14+
return {
15+
signature: {
16+
r: BigInteger.fromDERInteger(decode.r),
17+
s: BigInteger.fromDERInteger(decode.s)
18+
},
19+
hashType: hashType
20+
}
21+
}
22+
23+
function fromRSBuffer (buffer) {
24+
typeforce(types.BufferN(64), buffer)
25+
26+
var r = BigInteger.fromBuffer(buffer.slice(0, 32))
27+
var s = BigInteger.fromBuffer(buffer.slice(32, 64))
28+
return { r: r, s: s }
29+
}
30+
31+
function encode (signature, hashType) {
32+
var hashTypeMod = hashType & ~0x80
33+
if (hashTypeMod <= 0 || hashTypeMod >= 4) throw new Error('Invalid hashType ' + hashType)
34+
35+
var hashTypeBuffer = new Buffer(1)
36+
hashTypeBuffer.writeUInt8(hashType, 0)
37+
38+
var r = new Buffer(signature.r.toDERInteger())
39+
var s = new Buffer(signature.s.toDERInteger())
40+
41+
return Buffer.concat([
42+
bip66.encode(r, s),
43+
hashTypeBuffer
44+
])
45+
}
46+
47+
module.exports = {
48+
fromRSBuffer,
49+
decode: decode,
50+
encode: encode
51+
}

src/transaction_builder.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ var SIGNABLE = [btemplates.types.P2PKH, btemplates.types.P2PK, btemplates.types.
1212
var P2SH = SIGNABLE.concat([btemplates.types.P2WPKH, btemplates.types.P2WSH])
1313

1414
var ECPair = require('./ecpair')
15-
var ECSignature = require('./ecsignature')
1615
var Transaction = require('./transaction')
1716

1817
function supportedType (type) {
@@ -190,7 +189,7 @@ function fixMultisigOrder (input, transaction, vin) {
190189
if (!signature) return false
191190

192191
// TODO: avoid O(n) hashForSignature
193-
var parsed = ECSignature.parseScriptSignature(signature)
192+
var parsed = bscript.signature.decode(signature)
194193
var hash = transaction.hashForSignature(vin, input.redeemScript, parsed.hashType)
195194

196195
// skip if signature does not match pubKey
@@ -717,9 +716,9 @@ TransactionBuilder.prototype.sign = function (vin, keyPair, redeemScript, hashTy
717716
)) throw new Error('BIP143 rejects uncompressed public keys in P2WPKH or P2WSH')
718717

719718
var signature = keyPair.sign(signatureHash)
720-
if (Buffer.isBuffer(signature)) signature = ECSignature.fromRSBuffer(signature)
719+
if (Buffer.isBuffer(signature)) signature = bscript.signature.fromRSBuffer(signature)
721720

722-
input.signatures[i] = signature.toScriptSignature(hashType)
721+
input.signatures[i] = bscript.signature.encode(signature, hashType)
723722
return true
724723
})
725724

test/bitcoin.core.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -198,13 +198,14 @@ describe('Bitcoin-core', function () {
198198
})
199199
})
200200

201-
describe('ECSignature.parseScriptSignature', function () {
201+
describe('script.signature.decode', function () {
202202
sigCanonical.forEach(function (hex) {
203203
var buffer = Buffer.from(hex, 'hex')
204204

205205
it('can parse ' + hex, function () {
206-
var parsed = bitcoin.ECSignature.parseScriptSignature(buffer)
207-
var actual = parsed.signature.toScriptSignature(parsed.hashType)
206+
var parsed = bitcoin.script.signature.decode(buffer)
207+
var actual = bitcoin.script.signature.encode(parsed.signature, parsed.hashType)
208+
208209
assert.strictEqual(actual.toString('hex'), hex)
209210
})
210211
})
@@ -218,7 +219,7 @@ describe('Bitcoin-core', function () {
218219

219220
it('throws on ' + description, function () {
220221
assert.throws(function () {
221-
bitcoin.ECSignature.parseScriptSignature(buffer)
222+
bitcoin.script.signature.decode(buffer)
222223
}, /Expected DER (integer|sequence)|(R|S) value (excessively padded|is negative)|(R|S|DER sequence) length is (zero|too short|too long|invalid)|Invalid hashType/)
223224
})
224225
})

test/ecdsa.js

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,31 @@
22

33
var assert = require('assert')
44
var bcrypto = require('../src/crypto')
5+
var bscript = require('../src/script')
56
var ecdsa = require('../src/ecdsa')
67
var hoodwink = require('hoodwink')
78

89
var BigInteger = require('bigi')
9-
var ECSignature = require('../src/ecsignature')
1010

1111
var curve = ecdsa.__curve
1212

1313
var fixtures = require('./fixtures/ecdsa.json')
1414

1515
describe('ecdsa', function () {
16+
function fromRaw (signature) {
17+
return {
18+
r: new BigInteger(signature.r, 16),
19+
s: new BigInteger(signature.s, 16)
20+
}
21+
}
22+
23+
function toRaw (signature) {
24+
return {
25+
r: signature.r.toHex(),
26+
s: signature.s.toHex()
27+
}
28+
}
29+
1630
describe('deterministicGenerateK', function () {
1731
function checkSig () {
1832
return true
@@ -80,9 +94,9 @@ describe('ecdsa', function () {
8094
it('produces a deterministic signature for "' + f.message + '"', function () {
8195
var d = BigInteger.fromHex(f.d)
8296
var hash = bcrypto.sha256(f.message)
83-
var signature = ecdsa.sign(hash, d).toDER()
97+
var signature = ecdsa.sign(hash, d)
8498

85-
assert.strictEqual(signature.toString('hex'), f.signature)
99+
assert.deepEqual(toRaw(signature), f.signature)
86100
})
87101
})
88102

@@ -101,7 +115,7 @@ describe('ecdsa', function () {
101115
it('verifies a valid signature for "' + f.message + '"', function () {
102116
var d = BigInteger.fromHex(f.d)
103117
var H = bcrypto.sha256(f.message)
104-
var signature = ECSignature.fromDER(Buffer.from(f.signature, 'hex'))
118+
var signature = fromRaw(f.signature)
105119
var Q = curve.G.multiply(d)
106120

107121
assert(ecdsa.verify(H, signature, Q))
@@ -112,14 +126,7 @@ describe('ecdsa', function () {
112126
it('fails to verify with ' + f.description, function () {
113127
var H = bcrypto.sha256(f.message)
114128
var d = BigInteger.fromHex(f.d)
115-
116-
var signature
117-
if (f.signature) {
118-
signature = ECSignature.fromDER(Buffer.from(f.signature, 'hex'))
119-
} else if (f.signatureRaw) {
120-
signature = new ECSignature(new BigInteger(f.signatureRaw.r, 16), new BigInteger(f.signatureRaw.s, 16))
121-
}
122-
129+
var signature = fromRaw(f.signature)
123130
var Q = curve.G.multiply(d)
124131

125132
assert.strictEqual(ecdsa.verify(H, signature, Q), false)

0 commit comments

Comments
 (0)