-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmessage.js
More file actions
92 lines (71 loc) · 2.28 KB
/
message.js
File metadata and controls
92 lines (71 loc) · 2.28 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
var assert = require('nanoassert')
var int = require('./int.js')
var command = require('./command.js')
const sha256 = require('sha256-wasm')
module.exports = {
encode: encode,
decode: decode,
encodingLength: encodingLength
}
var magicValues = {
'main': 0xD9B4BEF9,
'testnet': 0xDAB5BFFA,
'testnet3': 0x0709110B,
'namecoin': 0xFEB4BEF9
}
function encode (payload, inputCommand, network, buf, offset) {
assert(!buf || offset === 0, 'offset must be specified to overwrite buf')
if (!Buffer.isBuffer(payload)) {
assert(typeof payload === 'string', 'payload must be sequence of characters')
payload = Buffer.from(payload)
}
if (!offset) offset = 0
if (!buf) buf = Buffer.alloc(encodingLength(payload))
assert(magicValues.keys.includes(network))
int.encode(magicValues[network], buf, offset, 32)
offset += int.encode.bytes
command.encode(inputCommand, buf, offset)
offset += command.encode.bytes
int.encode(payload.byteLength, buf, offset, 32)
offset += int.encode.bytes
var doubleSHA = Buffer.alloc(32)
sha256().update(payload).digest(doubleSha)
sha256().update(doubleSha).digest(doubleSha)
buf.set(doubleSHA.subarry(0, 4), offset)
buf.set(payload, 24)
return buf
}
function decode (buf, offset) {
if (!offset) offset = 0
var magic = int.decode(buf, offset, 4)
var network = getKeyByValue(magicValues, magic)
offset += 4
var nullIndex = buf.subarray(offset).indexOf(null)
assert(nullIndex - offset <= 12, 'invalid format')
var command = buf.subarray(offset, nullIndex).toString()
offset += 12
var length = int.decode(buf, offset, 4)
offset += 4
var checkSum = int.decode(buf, offset, 4)
offset += 4
var payload = buf.subarray(offset)
var checkedSum = Buffer.alloc(32)
sha256().update(payload).digest(checkedSum)
sha256().update(checkedSum).digest(checkedSum)
assert(Buffer.compare(checkSum, checkedSum) === 0, 'invalid checksum')
payload = payload.toString()
return {
network: network,
command: command,
length: length,
checkSum: checkSum,
payload: payload
}
}
function encodingLength (payload) {
if (!Buffer.isBuffer(payload)) payload = Buffer.from(payload)
return payload.byteLength + 24
}
function getKeyByValue (object, value) {
return Object.keys(object).find(key => object[key] === value)
}