UNPKG

2.31 kBJavaScriptView Raw
1/**
2 * Version
3 * =======
4 *
5 * This data structure is used to specify details about what version of the
6 * p2p network is supported by this or other nodes.
7 */
8'use strict'
9
10import { Bw } from './bw'
11import { Constants as Cst } from './constants'
12import { Struct } from './struct'
13import { VarInt } from './var-int'
14
15const Constants = Cst.Default
16
17class Version extends Struct {
18 constructor (
19 versionBytesNum = Constants.Msg.versionBytesNum,
20 servicesBuf,
21 timeBn,
22 addrRecvServicesBuf,
23 addrRecvIpAddrBuf,
24 addrRecvPort,
25 addrTransServicesBuf,
26 addrTransIpAddrBuf,
27 addrTransPort,
28 nonceBuf,
29 userAgentVi,
30 userAgentBuf,
31 startHeightNum,
32 relay
33 ) {
34 super({
35 versionBytesNum,
36 servicesBuf,
37 timeBn,
38 addrRecvServicesBuf,
39 addrRecvIpAddrBuf,
40 addrRecvPort,
41 addrTransServicesBuf,
42 addrTransIpAddrBuf,
43 addrTransPort,
44 nonceBuf,
45 userAgentVi,
46 userAgentBuf,
47 startHeightNum,
48 relay
49 })
50 }
51
52 toBw (bw) {
53 if (!bw) {
54 bw = new Bw()
55 }
56 bw.writeUInt32LE(this.versionBytesNum)
57 bw.write(this.servicesBuf)
58 bw.writeUInt64LEBn(this.timeBn)
59 bw.write(this.addrRecvServicesBuf)
60 bw.write(this.addrRecvIpAddrBuf)
61 bw.writeUInt16BE(this.addrRecvPort) // note BE
62 bw.write(this.addrTransServicesBuf)
63 bw.write(this.addrTransIpAddrBuf)
64 bw.writeUInt16BE(this.addrTransPort) // note BE
65 bw.write(this.nonceBuf)
66 bw.write(this.userAgentVi.buf)
67 bw.write(this.userAgentBuf)
68 bw.writeUInt32LE(this.startHeightNum)
69 bw.writeUInt8(Number(this.relay))
70 return bw
71 }
72
73 fromBr (br) {
74 this.versionBytesNum = br.readUInt32LE()
75 this.servicesBuf = br.read(8)
76 this.timeBn = br.readUInt64LEBn()
77 this.addrRecvServicesBuf = br.read(8)
78 this.addrRecvIpAddrBuf = br.read(16)
79 this.addrRecvPort = br.readUInt16BE() // note BE
80 this.addrTransServicesBuf = br.read(8)
81 this.addrTransIpAddrBuf = br.read(16)
82 this.addrTransPort = br.readUInt16BE() // note BE
83 this.nonceBuf = br.read(8)
84 this.userAgentVi = new VarInt(br.readVarIntBuf())
85 this.userAgentBuf = br.read(this.userAgentVi.toNumber())
86 this.startHeightNum = br.readUInt32LE()
87 this.relay = Boolean(br.readUInt8())
88 return this
89 }
90}
91
92export { Version }