rm ECSignature, add script.signature instead
This commit is contained in:
parent
77e317d618
commit
c58ada362e
14 changed files with 335 additions and 149 deletions
|
@ -4,7 +4,6 @@ var typeforce = require('typeforce')
|
|||
var types = require('./types')
|
||||
|
||||
var BigInteger = require('bigi')
|
||||
var ECSignature = require('./ecsignature')
|
||||
|
||||
var ZERO = Buffer.alloc(1, 0)
|
||||
var ONE = Buffer.alloc(1, 1)
|
||||
|
@ -102,7 +101,10 @@ function sign (hash, d) {
|
|||
s = n.subtract(s)
|
||||
}
|
||||
|
||||
return new ECSignature(r, s)
|
||||
return {
|
||||
r: r,
|
||||
s: s
|
||||
}
|
||||
}
|
||||
|
||||
function verify (hash, signature, Q) {
|
||||
|
|
|
@ -1,97 +0,0 @@
|
|||
var bip66 = require('bip66')
|
||||
var typeforce = require('typeforce')
|
||||
var types = require('./types')
|
||||
|
||||
var BigInteger = require('bigi')
|
||||
|
||||
function ECSignature (r, s) {
|
||||
typeforce(types.tuple(types.BigInt, types.BigInt), arguments)
|
||||
|
||||
this.r = r
|
||||
this.s = s
|
||||
}
|
||||
|
||||
ECSignature.parseCompact = function (buffer) {
|
||||
typeforce(types.BufferN(65), buffer)
|
||||
|
||||
var flagByte = buffer.readUInt8(0) - 27
|
||||
if (flagByte !== (flagByte & 7)) throw new Error('Invalid signature parameter')
|
||||
|
||||
var compressed = !!(flagByte & 4)
|
||||
var recoveryParam = flagByte & 3
|
||||
var signature = ECSignature.fromRSBuffer(buffer.slice(1))
|
||||
|
||||
return {
|
||||
compressed: compressed,
|
||||
i: recoveryParam,
|
||||
signature: signature
|
||||
}
|
||||
}
|
||||
|
||||
ECSignature.fromRSBuffer = function (buffer) {
|
||||
typeforce(types.BufferN(64), buffer)
|
||||
|
||||
var r = BigInteger.fromBuffer(buffer.slice(0, 32))
|
||||
var s = BigInteger.fromBuffer(buffer.slice(32, 64))
|
||||
return new ECSignature(r, s)
|
||||
}
|
||||
|
||||
ECSignature.fromDER = function (buffer) {
|
||||
var decode = bip66.decode(buffer)
|
||||
var r = BigInteger.fromDERInteger(decode.r)
|
||||
var s = BigInteger.fromDERInteger(decode.s)
|
||||
|
||||
return new ECSignature(r, s)
|
||||
}
|
||||
|
||||
// BIP62: 1 byte hashType flag (only 0x01, 0x02, 0x03, 0x81, 0x82 and 0x83 are allowed)
|
||||
ECSignature.parseScriptSignature = function (buffer) {
|
||||
var hashType = buffer.readUInt8(buffer.length - 1)
|
||||
var hashTypeMod = hashType & ~0x80
|
||||
|
||||
if (hashTypeMod <= 0x00 || hashTypeMod >= 0x04) throw new Error('Invalid hashType ' + hashType)
|
||||
|
||||
return {
|
||||
signature: ECSignature.fromDER(buffer.slice(0, -1)),
|
||||
hashType: hashType
|
||||
}
|
||||
}
|
||||
|
||||
ECSignature.prototype.toCompact = function (i, compressed) {
|
||||
if (compressed) {
|
||||
i += 4
|
||||
}
|
||||
|
||||
i += 27
|
||||
|
||||
var buffer = Buffer.alloc(65)
|
||||
buffer.writeUInt8(i, 0)
|
||||
this.toRSBuffer(buffer, 1)
|
||||
return buffer
|
||||
}
|
||||
|
||||
ECSignature.prototype.toDER = function () {
|
||||
var r = Buffer.from(this.r.toDERInteger())
|
||||
var s = Buffer.from(this.s.toDERInteger())
|
||||
|
||||
return bip66.encode(r, s)
|
||||
}
|
||||
|
||||
ECSignature.prototype.toRSBuffer = function (buffer, offset) {
|
||||
buffer = buffer || Buffer.alloc(64)
|
||||
this.r.toBuffer(32).copy(buffer, offset)
|
||||
this.s.toBuffer(32).copy(buffer, offset + 32)
|
||||
return buffer
|
||||
}
|
||||
|
||||
ECSignature.prototype.toScriptSignature = function (hashType) {
|
||||
var hashTypeMod = hashType & ~0x80
|
||||
if (hashTypeMod <= 0 || hashTypeMod >= 4) throw new Error('Invalid hashType ' + hashType)
|
||||
|
||||
var hashTypeBuffer = Buffer.alloc(1)
|
||||
hashTypeBuffer.writeUInt8(hashType, 0)
|
||||
|
||||
return Buffer.concat([this.toDER(), hashTypeBuffer])
|
||||
}
|
||||
|
||||
module.exports = ECSignature
|
|
@ -8,7 +8,6 @@ for (var key in templates) {
|
|||
module.exports = {
|
||||
Block: require('./block'),
|
||||
ECPair: require('./ecpair'),
|
||||
ECSignature: require('./ecsignature'),
|
||||
HDNode: require('./hdnode'),
|
||||
Transaction: require('./transaction'),
|
||||
TransactionBuilder: require('./transaction_builder'),
|
||||
|
|
|
@ -206,6 +206,7 @@ module.exports = {
|
|||
toStack: toStack,
|
||||
|
||||
number: require('./script_number'),
|
||||
signature: require('./script_signature'),
|
||||
|
||||
isCanonicalPubKey: isCanonicalPubKey,
|
||||
isCanonicalSignature: isCanonicalSignature,
|
||||
|
|
51
src/script_signature.js
Normal file
51
src/script_signature.js
Normal file
|
@ -0,0 +1,51 @@
|
|||
var bip66 = require('bip66')
|
||||
var BigInteger = require('bigi')
|
||||
var typeforce = require('typeforce')
|
||||
var types = require('./types')
|
||||
|
||||
// BIP62: 1 byte hashType flag (only 0x01, 0x02, 0x03, 0x81, 0x82 and 0x83 are allowed)
|
||||
function decode (buffer) {
|
||||
var hashType = buffer.readUInt8(buffer.length - 1)
|
||||
var hashTypeMod = hashType & ~0x80
|
||||
if (hashTypeMod <= 0 || hashTypeMod >= 4) throw new Error('Invalid hashType ' + hashType)
|
||||
|
||||
var decode = bip66.decode(buffer.slice(0, -1))
|
||||
|
||||
return {
|
||||
signature: {
|
||||
r: BigInteger.fromDERInteger(decode.r),
|
||||
s: BigInteger.fromDERInteger(decode.s)
|
||||
},
|
||||
hashType: hashType
|
||||
}
|
||||
}
|
||||
|
||||
function fromRSBuffer (buffer) {
|
||||
typeforce(types.BufferN(64), buffer)
|
||||
|
||||
var r = BigInteger.fromBuffer(buffer.slice(0, 32))
|
||||
var s = BigInteger.fromBuffer(buffer.slice(32, 64))
|
||||
return { r: r, s: s }
|
||||
}
|
||||
|
||||
function encode (signature, hashType) {
|
||||
var hashTypeMod = hashType & ~0x80
|
||||
if (hashTypeMod <= 0 || hashTypeMod >= 4) throw new Error('Invalid hashType ' + hashType)
|
||||
|
||||
var hashTypeBuffer = new Buffer(1)
|
||||
hashTypeBuffer.writeUInt8(hashType, 0)
|
||||
|
||||
var r = new Buffer(signature.r.toDERInteger())
|
||||
var s = new Buffer(signature.s.toDERInteger())
|
||||
|
||||
return Buffer.concat([
|
||||
bip66.encode(r, s),
|
||||
hashTypeBuffer
|
||||
])
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
fromRSBuffer,
|
||||
decode: decode,
|
||||
encode: encode
|
||||
}
|
|
@ -12,7 +12,6 @@ var SIGNABLE = [btemplates.types.P2PKH, btemplates.types.P2PK, btemplates.types.
|
|||
var P2SH = SIGNABLE.concat([btemplates.types.P2WPKH, btemplates.types.P2WSH])
|
||||
|
||||
var ECPair = require('./ecpair')
|
||||
var ECSignature = require('./ecsignature')
|
||||
var Transaction = require('./transaction')
|
||||
|
||||
function supportedType (type) {
|
||||
|
@ -190,7 +189,7 @@ function fixMultisigOrder (input, transaction, vin) {
|
|||
if (!signature) return false
|
||||
|
||||
// TODO: avoid O(n) hashForSignature
|
||||
var parsed = ECSignature.parseScriptSignature(signature)
|
||||
var parsed = bscript.signature.decode(signature)
|
||||
var hash = transaction.hashForSignature(vin, input.redeemScript, parsed.hashType)
|
||||
|
||||
// skip if signature does not match pubKey
|
||||
|
@ -717,9 +716,9 @@ TransactionBuilder.prototype.sign = function (vin, keyPair, redeemScript, hashTy
|
|||
)) throw new Error('BIP143 rejects uncompressed public keys in P2WPKH or P2WSH')
|
||||
|
||||
var signature = keyPair.sign(signatureHash)
|
||||
if (Buffer.isBuffer(signature)) signature = ECSignature.fromRSBuffer(signature)
|
||||
if (Buffer.isBuffer(signature)) signature = bscript.signature.fromRSBuffer(signature)
|
||||
|
||||
input.signatures[i] = signature.toScriptSignature(hashType)
|
||||
input.signatures[i] = bscript.signature.encode(signature, hashType)
|
||||
return true
|
||||
})
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue