-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rm ECSignature, add script.signature instead
- Loading branch information
Showing
12 changed files
with
336 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
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 toRSBuffer (signature) { | ||
var r = signature.r.toBuffer(32) | ||
var s = signature.s.toBuffer(32) | ||
return Buffer.concat([r, s]) | ||
} | ||
|
||
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 = { | ||
toRSBuffer, | ||
fromRSBuffer, | ||
decode: decode, | ||
encode: encode | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.