@@ -13,14 +13,15 @@ var ECDSA = require('../crypto/ecdsa')
1313var $ = require ( '../util/preconditions' )
1414var Interpreter = require ( '../script/interpreter' )
1515var _ = require ( '../util/_' )
16+ var HashCache = require ( '../hash-cache' )
1617
1718var SIGHASH_SINGLE_BUG = Buffer . from ( '0000000000000000000000000000000000000000000000000000000000000001' , 'hex' )
1819var BITS_64_ON = 'ffffffffffffffff'
1920
2021// By default, we sign with sighash_forkid
2122var DEFAULT_SIGN_FLAGS = Interpreter . SCRIPT_ENABLE_SIGHASH_FORKID
2223
23- var sighashPreimageForForkId = function ( transaction , sighashType , inputNumber , subscript , satoshisBN ) {
24+ var sighashPreimageForForkId = function ( transaction , sighashType , inputNumber , subscript , satoshisBN , hashCache = new HashCache ( ) ) {
2425 var input = transaction . inputs [ inputNumber ]
2526 $ . checkArgument (
2627 satoshisBN instanceof BN ,
@@ -73,17 +74,17 @@ var sighashPreimageForForkId = function (transaction, sighashType, inputNumber,
7374 var hashOutputs = Buffer . alloc ( 32 )
7475
7576 if ( ! ( sighashType & Signature . SIGHASH_ANYONECANPAY ) ) {
76- hashPrevouts = GetPrevoutHash ( transaction )
77+ hashPrevouts = hashCache . prevoutsHashBuf ? hashCache . prevoutsHashBuf : hashCache . prevoutsHashBuf = GetPrevoutHash ( transaction )
7778 }
7879
7980 if ( ! ( sighashType & Signature . SIGHASH_ANYONECANPAY ) &&
8081 ( sighashType & 31 ) !== Signature . SIGHASH_SINGLE &&
8182 ( sighashType & 31 ) !== Signature . SIGHASH_NONE ) {
82- hashSequence = GetSequenceHash ( transaction )
83+ hashSequence = hashCache . sequenceHashBuf ? hashCache . sequenceHashBuf : hashCache . sequenceHashBuf = GetSequenceHash ( transaction )
8384 }
8485
8586 if ( ( sighashType & 31 ) !== Signature . SIGHASH_SINGLE && ( sighashType & 31 ) !== Signature . SIGHASH_NONE ) {
86- hashOutputs = GetOutputsHash ( transaction )
87+ hashOutputs = hashCache . outputsHashBuf ? hashCache . outputsHashBuf : hashCache . outputsHashBuf = GetOutputsHash ( transaction )
8788 } else if ( ( sighashType & 31 ) === Signature . SIGHASH_SINGLE && inputNumber < transaction . outputs . length ) {
8889 hashOutputs = GetOutputsHash ( transaction , inputNumber )
8990 }
@@ -136,7 +137,7 @@ var sighashPreimageForForkId = function (transaction, sighashType, inputNumber,
136137 * @param {satoshisBN } input's amount (for ForkId signatures)
137138 *
138139 */
139- var sighashPreimage = function sighashPreimage ( transaction , sighashType , inputNumber , subscript , satoshisBN , flags ) {
140+ var sighashPreimage = function sighashPreimage ( transaction , sighashType , inputNumber , subscript , satoshisBN , flags , hashCache = new HashCache ( ) ) {
140141 var Transaction = require ( './transaction' )
141142 var Input = require ( './input' )
142143
@@ -227,8 +228,8 @@ var sighashPreimage = function sighashPreimage (transaction, sighashType, inputN
227228 * @param {satoshisBN } input's amount (for ForkId signatures)
228229 *
229230 */
230- var sighash = function sighash ( transaction , sighashType , inputNumber , subscript , satoshisBN , flags ) {
231- var preimage = sighashPreimage ( transaction , sighashType , inputNumber , subscript , satoshisBN , flags )
231+ var sighash = function sighash ( transaction , sighashType , inputNumber , subscript , satoshisBN , flags , hashCache = new HashCache ( ) ) {
232+ var preimage = sighashPreimage ( transaction , sighashType , inputNumber , subscript , satoshisBN , flags , hashCache )
232233 if ( preimage . compare ( SIGHASH_SINGLE_BUG ) === 0 ) return preimage
233234 var ret = Hash . sha256sha256 ( preimage )
234235 ret = new BufferReader ( ret ) . readReverse ( )
@@ -247,8 +248,8 @@ var sighash = function sighash (transaction, sighashType, inputNumber, subscript
247248 * @param {satoshisBN } input's amount
248249 * @return {Signature }
249250 */
250- function sign ( transaction , privateKey , sighashType , inputIndex , subscript , satoshisBN , flags ) {
251- var hashbuf = sighash ( transaction , sighashType , inputIndex , subscript , satoshisBN , flags )
251+ function sign ( transaction , privateKey , sighashType , inputIndex , subscript , satoshisBN , flags , hashCache = new HashCache ( ) ) {
252+ var hashbuf = sighash ( transaction , sighashType , inputIndex , subscript , satoshisBN , flags , hashCache )
252253
253254 var sig = ECDSA . sign ( hashbuf , privateKey , 'little' ) . set ( {
254255 nhashtype : sighashType
@@ -269,10 +270,10 @@ function sign (transaction, privateKey, sighashType, inputIndex, subscript, sato
269270 * @param {flags } verification flags
270271 * @return {boolean }
271272 */
272- function verify ( transaction , signature , publicKey , inputIndex , subscript , satoshisBN , flags ) {
273+ function verify ( transaction , signature , publicKey , inputIndex , subscript , satoshisBN , flags , hashCache = new HashCache ( ) ) {
273274 $ . checkArgument ( ! _ . isUndefined ( transaction ) )
274275 $ . checkArgument ( ! _ . isUndefined ( signature ) && ! _ . isUndefined ( signature . nhashtype ) )
275- var hashbuf = sighash ( transaction , signature . nhashtype , inputIndex , subscript , satoshisBN , flags )
276+ var hashbuf = sighash ( transaction , signature . nhashtype , inputIndex , subscript , satoshisBN , flags , hashCache )
276277 return ECDSA . verify ( hashbuf , signature , publicKey , 'little' )
277278}
278279
0 commit comments