This repository was archived by the owner on Feb 7, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmessage.js
More file actions
193 lines (172 loc) · 4.92 KB
/
message.js
File metadata and controls
193 lines (172 loc) · 4.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
const ip = require('ip')
const rlp = require('rlp-encoding')
const secp256k1 = require('secp256k1')
const Buffer = require('safe-buffer').Buffer
const { keccak256, int2buffer, buffer2int, assertEq } = require('../util')
function getTimestamp () {
return (Date.now() / 1000) | 0
}
const timestamp = {
encode: function (value = getTimestamp() + 60) {
const buffer = Buffer.allocUnsafe(4)
buffer.writeUInt32BE(value)
return buffer
},
decode: function (buffer) {
if (buffer.length !== 4) throw new RangeError(`Invalid timestamp buffer :${buffer.toString('hex')}`)
return buffer.readUInt32BE(0)
}
}
const address = {
encode: function (value) {
if (ip.isV4Format(value)) return ip.toBuffer(value)
if (ip.isV6Format(value)) return ip.toBuffer(value)
throw new Error(`Invalid address: ${value}`)
},
decode: function (buffer) {
if (buffer.length === 4) return ip.toString(buffer)
if (buffer.length === 16) return ip.toString(buffer)
const str = buffer.toString()
if (ip.isV4Format(str) || ip.isV6Format(str)) return str
// also can be host, but skip it right now (because need async function for resolve)
throw new Error(`Invalid address buffer: ${buffer.toString('hex')}`)
}
}
const port = {
encode: function (value) {
if (value === null) return Buffer.allocUnsafe(0)
if ((value >>> 16) > 0) throw new RangeError(`Invalid port: ${value}`)
return Buffer.from([ (value >>> 8) & 0xff, (value >>> 0) & 0xff ])
},
decode: function (buffer) {
if (buffer.length === 0) return null
// if (buffer.length !== 2) throw new RangeError(`Invalid port buffer: ${buffer.toString('hex')}`)
return buffer2int(buffer)
}
}
const endpoint = {
encode: function (obj) {
return [
address.encode(obj.address),
port.encode(obj.udpPort),
port.encode(obj.tcpPort)
]
},
decode: function (payload) {
return {
address: address.decode(payload[0]),
udpPort: port.decode(payload[1]),
tcpPort: port.decode(payload[2])
}
}
}
const ping = {
encode: function (obj) {
return [
int2buffer(obj.version),
endpoint.encode(obj.from),
endpoint.encode(obj.to),
timestamp.encode(obj.timestamp)
]
},
decode: function (payload) {
return {
version: buffer2int(payload[0]),
from: endpoint.decode(payload[1]),
to: endpoint.decode(payload[2]),
timestamp: timestamp.decode(payload[3])
}
}
}
const pong = {
encode: function (obj) {
return [
endpoint.encode(obj.to),
obj.hash,
timestamp.encode(obj.timestamp)
]
},
decode: function (payload) {
return {
to: endpoint.decode(payload[0]),
hash: payload[1],
timestamp: timestamp.decode(payload[2])
}
}
}
const findneighbours = {
encode: function (obj) {
return [
obj.id,
timestamp.encode(obj.timestamp)
]
},
decode: function (payload) {
return {
id: payload[0],
timestamp: timestamp.decode(payload[1])
}
}
}
const neighbours = {
encode: function (obj) {
return [
obj.peers.map((peer) => endpoint.encode(peer).concat(peer.id)),
timestamp.encode(obj.timestamp)
]
},
decode: function (payload) {
return {
peers: payload[0].map((data) => {
return { endpoint: endpoint.decode(data), id: data[3] } // hack for id
}),
timestamp: timestamp.decode(payload[1])
}
}
}
const messages = { ping, pong, findneighbours, neighbours }
const types = {
byName: {
ping: 0x01,
pong: 0x02,
findneighbours: 0x03,
neighbours: 0x04
},
byType: {
0x01: 'ping',
0x02: 'pong',
0x03: 'findneighbours',
0x04: 'neighbours'
}
}
// [0, 32) data hash
// [32, 96) signature
// 96 recoveryId
// 97 type
// [98, length) data
function encode (typename, data, privateKey) {
const type = types.byName[typename]
if (type === undefined) throw new Error(`Invalid typename: ${typename}`)
const encodedMsg = messages[typename].encode(data)
const typedata = Buffer.concat([ Buffer.from([ type ]), rlp.encode(encodedMsg) ])
const sighash = keccak256(typedata)
const sig = secp256k1.sign(sighash, privateKey)
const hashdata = Buffer.concat([ sig.signature, Buffer.from([ sig.recovery ]), typedata ])
const hash = keccak256(hashdata)
return Buffer.concat([ hash, hashdata ])
}
function decode (buffer) {
const hash = keccak256(buffer.slice(32))
assertEq(buffer.slice(0, 32), hash, 'Hash verification failed')
const typedata = buffer.slice(97)
const type = typedata[0]
const typename = types.byType[type]
if (typename === undefined) throw new Error(`Invalid type: ${type}`)
const data = messages[typename].decode(rlp.decode(typedata.slice(1)))
const sighash = keccak256(typedata)
const signature = buffer.slice(32, 96)
const recoverId = buffer[96]
const publicKey = secp256k1.recover(sighash, signature, recoverId, false)
return { typename, data, publicKey }
}
module.exports = { encode, decode }