-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfin.js
48 lines (39 loc) · 962 Bytes
/
fin.js
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
const defaultEncoding = require('./encoding')
const { unpack } = require('./unpack')
const messages = require('./messages')
const { pack } = require('./pack')
const crypto = require('crypto')
const varint = require('varint')
class Fin {
static get WIRE_TYPE() {
return 0x05
}
static from(buffer, encoding) {
const type = varint.decode(buffer)
if (Fin.WIRE_TYPE !== type) {
throw new TypeError('Invalid wire type for Fin')
}
encoding = encoding || defaultEncoding
const decoded = messages.Fin.decode(unpack(buffer))
return new Fin(encoding, decoded.id, decoded.nonce)
}
constructor(encoding, id, nonce) {
this.id = id
this.nonce = nonce || crypto.randomBytes(32)
}
toJSON() {
return{
id: this.id,
nonce: this.nonce
}
}
toBuffer() {
return messages.Fin.encode(this.toJSON())
}
pack() {
return pack(Fin.WIRE_TYPE, this.toBuffer())
}
}
module.exports = {
Fin
}