UNPKG

1.15 kBJavaScriptView Raw
1/**
2 * Hash Cache
3 * ==========
4 *
5 * For use in sighash.
6 */
7'use strict'
8
9import { Struct } from './struct'
10
11class HashCache extends Struct {
12 constructor (prevoutsHashBuf, sequenceHashBuf, outputsHashBuf) {
13 super()
14 this.fromObject({ prevoutsHashBuf, sequenceHashBuf, outputsHashBuf })
15 }
16
17 fromBuffer (buf) {
18 return this.fromJSON(JSON.parse(buf.toString()))
19 }
20
21 toBuffer () {
22 return Buffer.from(JSON.stringify(this.toJSON()))
23 }
24
25 fromJSON (json) {
26 this.prevoutsHashBuf = json.prevoutsHashBuf ? Buffer.from(json.prevoutsHashBuf, 'hex') : undefined
27 this.sequenceHashBuf = json.sequenceHashBuf ? Buffer.from(json.sequenceHashBuf, 'hex') : undefined
28 this.outputsHashBuf = json.outputsHashBuf ? Buffer.from(json.outputsHashBuf, 'hex') : undefined
29 return this
30 }
31
32 toJSON () {
33 return {
34 prevoutsHashBuf: this.prevoutsHashBuf ? this.prevoutsHashBuf.toString('hex') : undefined,
35 sequenceHashBuf: this.sequenceHashBuf ? this.sequenceHashBuf.toString('hex') : undefined,
36 outputsHashBuf: this.outputsHashBuf ? this.outputsHashBuf.toString('hex') : undefined
37 }
38 }
39}
40
41export { HashCache }