UNPKG

1.69 kBJavaScriptView Raw
1/**
2 * Transaction Output
3 * ==================
4 *
5 * An output to a transaction. The way you normally want to make one is with
6 * new TxOut(valueBn, script) (i.e., just as with TxIn, you can leave out the
7 * scriptVi, since it can be computed automatically.
8 */
9'use strict'
10
11import { Bn } from './bn'
12import { Bw } from './bw'
13import { Script } from './script'
14import { Struct } from './struct'
15import { VarInt } from './var-int'
16
17class 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
73export { TxOut }