1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | 'use strict'
|
11 |
|
12 | import { Bw } from './bw'
|
13 | import { VarInt } from './var-int'
|
14 | import { OpCode } from './op-code'
|
15 | import { Script } from './script'
|
16 | import { Struct } from './struct'
|
17 |
|
18 | class TxIn extends Struct {
|
19 | constructor (txHashBuf, txOutNum, scriptVi, script, nSequence = 0xffffffff) {
|
20 | super({ txHashBuf, txOutNum, scriptVi, script, nSequence })
|
21 | }
|
22 |
|
23 | setScript (script) {
|
24 | this.scriptVi = VarInt.fromNumber(script.toBuffer().length)
|
25 | this.script = script
|
26 | return this
|
27 | }
|
28 |
|
29 | fromProperties (txHashBuf, txOutNum, script, nSequence) {
|
30 | this.fromObject({ txHashBuf, txOutNum, nSequence })
|
31 | this.setScript(script)
|
32 | return this
|
33 | }
|
34 |
|
35 | static fromProperties (txHashBuf, txOutNum, script, nSequence) {
|
36 | return new this().fromProperties(txHashBuf, txOutNum, script, nSequence)
|
37 | }
|
38 |
|
39 | fromJSON (json) {
|
40 | this.fromObject({
|
41 | txHashBuf:
|
42 | typeof json.txHashBuf !== 'undefined'
|
43 | ? Buffer.from(json.txHashBuf, 'hex')
|
44 | : undefined,
|
45 | txOutNum: json.txOutNum,
|
46 | scriptVi:
|
47 | typeof json.scriptVi !== 'undefined'
|
48 | ? VarInt.fromJSON(json.scriptVi)
|
49 | : undefined,
|
50 | script:
|
51 | typeof json.script !== 'undefined'
|
52 | ? Script.fromJSON(json.script)
|
53 | : undefined,
|
54 | nSequence: json.nSequence
|
55 | })
|
56 | return this
|
57 | }
|
58 |
|
59 | toJSON () {
|
60 | return {
|
61 | txHashBuf:
|
62 | typeof this.txHashBuf !== 'undefined'
|
63 | ? this.txHashBuf.toString('hex')
|
64 | : undefined,
|
65 | txOutNum: this.txOutNum,
|
66 | scriptVi:
|
67 | typeof this.scriptVi !== 'undefined'
|
68 | ? this.scriptVi.toJSON()
|
69 | : undefined,
|
70 | script:
|
71 | typeof this.script !== 'undefined' ? this.script.toJSON() : undefined,
|
72 | nSequence: this.nSequence
|
73 | }
|
74 | }
|
75 |
|
76 | fromBr (br) {
|
77 | this.txHashBuf = br.read(32)
|
78 | this.txOutNum = br.readUInt32LE()
|
79 | this.scriptVi = VarInt.fromBuffer(br.readVarIntBuf())
|
80 | this.script = Script.fromBuffer(br.read(this.scriptVi.toNumber()))
|
81 | this.nSequence = br.readUInt32LE()
|
82 | return this
|
83 | }
|
84 |
|
85 | toBw (bw) {
|
86 | if (!bw) {
|
87 | bw = new Bw()
|
88 | }
|
89 | bw.write(this.txHashBuf)
|
90 | bw.writeUInt32LE(this.txOutNum)
|
91 | bw.write(this.scriptVi.buf)
|
92 | bw.write(this.script.toBuffer())
|
93 | bw.writeUInt32LE(this.nSequence)
|
94 | return bw
|
95 | }
|
96 |
|
97 | |
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 | fromPubKeyHashTxOut (txHashBuf, txOutNum, txOut, pubKey) {
|
104 | const script = new Script()
|
105 | if (txOut.script.isPubKeyHashOut()) {
|
106 | script.writeOpCode(OpCode.OP_0)
|
107 | if (pubKey) {
|
108 | script.writeBuffer(pubKey.toBuffer())
|
109 | } else {
|
110 | script.writeOpCode(OpCode.OP_0)
|
111 | }
|
112 | } else {
|
113 | throw new Error('txOut must be of type pubKeyHash')
|
114 | }
|
115 | this.txHashBuf = txHashBuf
|
116 | this.txOutNum = txOutNum
|
117 | this.setScript(script)
|
118 | return this
|
119 | }
|
120 |
|
121 | hasNullInput () {
|
122 | const hex = this.txHashBuf.toString('hex')
|
123 | if (
|
124 | hex ===
|
125 | '0000000000000000000000000000000000000000000000000000000000000000' &&
|
126 | this.txOutNum === 0xffffffff
|
127 | ) {
|
128 | return true
|
129 | }
|
130 | return false
|
131 | }
|
132 |
|
133 | |
134 |
|
135 |
|
136 | setNullInput () {
|
137 | this.txHashBuf = Buffer.alloc(32)
|
138 | this.txHashBuf.fill(0)
|
139 | this.txOutNum = 0xffffffff
|
140 | }
|
141 | }
|
142 |
|
143 |
|
144 | TxIn.LOCKTIME_VERIFY_SEQUENCE = 1 << 0
|
145 |
|
146 |
|
147 |
|
148 | TxIn.SEQUENCE_FINAL = 0xffffffff
|
149 |
|
150 |
|
151 |
|
152 |
|
153 | TxIn.SEQUENCE_LOCKTIME_DISABLE_FLAG = 1 << 31
|
154 |
|
155 |
|
156 |
|
157 |
|
158 | TxIn.SEQUENCE_LOCKTIME_TYPE_FLAG = 1 << 22
|
159 |
|
160 |
|
161 |
|
162 | TxIn.SEQUENCE_LOCKTIME_MASK = 0x0000ffff
|
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 | TxIn.SEQUENCE_LOCKTIME_GRANULARITY = 9
|
171 |
|
172 | export { TxIn }
|