rm ECSignature, add script.signature instead
This commit is contained in:
parent
77e317d618
commit
c58ada362e
14 changed files with 335 additions and 149 deletions
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
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue