1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | 'use strict'
|
10 |
|
11 | import { Bn } from './bn'
|
12 | import { Bw } from './bw'
|
13 | import { Script } from './script'
|
14 | import { Struct } from './struct'
|
15 | import { VarInt } from './var-int'
|
16 |
|
17 | class TxOut extends Struct {
|
18 | constructor (valueBn, scriptVi, script) {
|
19 | super({ valueBn, scriptVi, script })
|
20 | }
|
21 |
|
22 | setScript (script) {
|
23 | this.scriptVi = VarInt.fromNumber(script.toBuffer().length)
|
24 | this.script = script
|
25 | return this
|
26 | }
|
27 |
|
28 | fromProperties (valueBn, script) {
|
29 | this.fromObject({ valueBn })
|
30 | this.setScript(script)
|
31 | return this
|
32 | }
|
33 |
|
34 | static fromProperties (valueBn, script) {
|
35 | return new this().fromProperties(valueBn, script)
|
36 | }
|
37 |
|
38 | fromJSON (json) {
|
39 | this.fromObject({
|
40 | valueBn: new Bn().fromJSON(json.valueBn),
|
41 | scriptVi: new VarInt().fromJSON(json.scriptVi),
|
42 | script: new Script().fromJSON(json.script)
|
43 | })
|
44 | return this
|
45 | }
|
46 |
|
47 | toJSON () {
|
48 | return {
|
49 | valueBn: this.valueBn.toJSON(),
|
50 | scriptVi: this.scriptVi.toJSON(),
|
51 | script: this.script.toJSON()
|
52 | }
|
53 | }
|
54 |
|
55 | fromBr (br) {
|
56 | this.valueBn = br.readUInt64LEBn()
|
57 | this.scriptVi = VarInt.fromNumber(br.readVarIntNum())
|
58 | this.script = new Script().fromBuffer(br.read(this.scriptVi.toNumber()))
|
59 | return this
|
60 | }
|
61 |
|
62 | toBw (bw) {
|
63 | if (!bw) {
|
64 | bw = new Bw()
|
65 | }
|
66 | bw.writeUInt64LEBn(this.valueBn)
|
67 | bw.write(this.scriptVi.buf)
|
68 | bw.write(this.script.toBuffer())
|
69 | return bw
|
70 | }
|
71 | }
|
72 |
|
73 | export { TxOut }
|