-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.ts
323 lines (275 loc) · 8.85 KB
/
index.ts
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
import { Predictor, Reader, Writer, WriterInterface } from 'oer-utils'
import dateFormat = require('dateformat')
// These constants are increased by 1 for BTP version Alpha
export enum Type {
TYPE_RESPONSE = 1,
TYPE_ERROR = 2,
TYPE_MESSAGE = 6,
TYPE_TRANSFER = 7
}
export const TYPE_RESPONSE = Type.TYPE_RESPONSE
export const TYPE_ERROR = Type.TYPE_ERROR
export const TYPE_MESSAGE = Type.TYPE_MESSAGE
export const TYPE_TRANSFER = Type.TYPE_TRANSFER
export const MIME_APPLICATION_OCTET_STREAM = 0
export const MIME_TEXT_PLAIN_UTF8 = 1
export const MIME_APPLICATION_JSON = 2
export function typeToString (type: Type) {
switch (type) {
case Type.TYPE_RESPONSE: return 'TYPE_RESPONSE'
case Type.TYPE_ERROR: return 'TYPE_ERROR'
case Type.TYPE_MESSAGE: return 'TYPE_MESSAGE'
case Type.TYPE_TRANSFER: return 'TYPE_TRANSFER'
default: throw new Error('Unrecognized BTP packet type')
}
}
const GENERALIZED_TIME_REGEX =
/^([0-9]{4})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2}\.[0-9]{3}Z)$/
const protocolNameCache: {[s: string]: Buffer} = {}
// Generate a cache of the most commonly used BTP subprotocol names.
// The goal is to avoid an extra buffer allocation when serializing.
registerProtocolNames([
'ilp',
// BTP authentication:
'auth',
'auth_username',
'auth_token',
// ilp-plugin-xrp-asym-{client,server}:
'channel',
'channel_signature',
'claim',
'fund_channel',
'info',
'last_claim'
])
export function registerProtocolNames (names: string[]) {
// Cache the most common BTP subprotocol names so that a new buffer doesn't need
// to be allocated each serialize().
for (const protocolName of names) {
protocolNameCache[protocolName] = Buffer.from(protocolName, 'ascii')
}
}
// Notes about variable naming - comparison with asn.1 definition:
//
// The term 'Envelope' here correspond to the
// whole BilateralTransferProtocolPacket, see:
// https://github.com/interledger/rfcs/blob/master/asn1/BilateralTransferProtocol.asn
export function base64url (input: Buffer) {
return input.toString('base64')
.replace(/=/g, '')
.replace(/\+/g, '-')
.replace(/\//g, '_')
}
function toGeneralizedTimeBuffer (date: string) {
return Buffer.from(dateFormat(date, "UTC:yyyymmddHHMMss.l'Z'"))
}
function readGeneralizedTime (reader: Reader) {
const generalizedTime = reader.readVarOctetString().toString()
const date = generalizedTime.replace(
GENERALIZED_TIME_REGEX,
'$1-$2-$3T$4:$5:$6')
return new Date(date)
}
export interface ProtocolData {
protocolName: string
contentType: number
data: Buffer
}
function writeProtocolData (writer: WriterInterface, protocolData: ProtocolData[]) {
if (!Array.isArray(protocolData)) {
throw new Error('protocolData must be an array')
}
const lengthPrefix = protocolData.length
const lengthPrefixLengthPrefix = Math.max(1,
Math.ceil((Math.log(protocolData.length + 1) / Math.log(2)) / 8))
writer.writeUInt8(lengthPrefixLengthPrefix)
writer.writeUInt(lengthPrefix, lengthPrefixLengthPrefix)
for (const p of protocolData) {
writer.writeVarOctetString(
protocolNameCache[p.protocolName] ||
Buffer.from(p.protocolName, 'ascii'))
writer.writeUInt8(p.contentType)
writer.writeVarOctetString(p.data)
}
}
function readProtocolData (reader: Reader) {
const lengthPrefixPrefix = reader.readUInt8Number()
const lengthPrefix = reader.readUIntNumber(lengthPrefixPrefix)
const protocolData = []
for (let i = 0; i < lengthPrefix; ++i) {
const protocolName = reader.readVarOctetString().toString('ascii')
const contentType = reader.readUInt8Number()
const data = reader.readVarOctetString()
protocolData.push({
protocolName,
contentType,
data
})
}
return protocolData
}
export interface BtpTransfer {
amount: string
protocolData: ProtocolData[]
}
function writeTransfer (writer: WriterInterface, data: BtpTransfer) {
writer.writeUInt64(data.amount)
writeProtocolData(writer, data.protocolData)
}
export interface BtpError {
code: string
name: string
triggeredAt: string
data: string
protocolData: ProtocolData[]
}
function writeError (writer: WriterInterface, data: BtpError) {
if (data.code.length !== 3) {
throw new Error(`error code must be 3 characters, got: "${data.code}"`)
}
const codeBuffer = Buffer.from(data.code, 'ascii')
const nameBuffer = Buffer.from(data.name, 'ascii')
const triggeredAtBuffer = toGeneralizedTimeBuffer(data.triggeredAt)
const dataBuffer = Buffer.from(data.data, 'utf8')
writer.write(codeBuffer)
writer.writeVarOctetString(nameBuffer)
writer.writeVarOctetString(triggeredAtBuffer)
writer.writeVarOctetString(dataBuffer)
writeProtocolData(writer, data.protocolData)
}
export interface BtpMessage {
protocolData: ProtocolData[]
}
export interface BtpMessagePacket {
type: Type.TYPE_MESSAGE
requestId: number
data: BtpMessage
}
export interface BtpResponsePacket {
type: Type.TYPE_RESPONSE
requestId: number
data: BtpMessage
}
export interface BtpTransferPacket {
type: Type.TYPE_TRANSFER
requestId: number
data: BtpTransfer
}
export interface BtpErrorPacket {
type: Type.TYPE_ERROR
requestId: number
data: BtpError
}
export type BtpPacket = BtpResponsePacket | BtpMessagePacket | BtpTransferPacket | BtpErrorPacket
function writeContents (writer: WriterInterface, obj: BtpPacket) {
switch (obj.type) {
case Type.TYPE_RESPONSE:
case Type.TYPE_MESSAGE:
writeProtocolData(writer, obj.data.protocolData)
break
case Type.TYPE_TRANSFER:
writeTransfer(writer, obj.data)
break
case Type.TYPE_ERROR:
writeError(writer, obj.data)
break
default:
throw new Error('Unrecognized type')
}
}
export function serialize (obj: BtpPacket): Buffer {
const contentsPredictor = new Predictor()
writeContents(contentsPredictor, obj)
const envelopeSize = 1 + 4 +
Predictor.measureVarOctetString(contentsPredictor.length)
const envelopeWriter = new Writer(envelopeSize)
envelopeWriter.writeUInt8(obj.type)
envelopeWriter.writeUInt32(obj.requestId)
const contentsWriter = envelopeWriter.createVarOctetString(contentsPredictor.length)
writeContents(contentsWriter, obj)
return envelopeWriter.getBuffer()
}
function readTransfer (reader: Reader): BtpTransfer {
const amount = reader.readUInt64()
const protocolData = readProtocolData(reader)
return { amount, protocolData }
}
function readError (reader: Reader) {
const code = reader.read(3).toString('ascii')
const name = reader.readVarOctetString().toString('ascii')
const triggeredAt = readGeneralizedTime(reader)
const data = reader.readVarOctetString().toString('utf8')
const protocolData = readProtocolData(reader)
return { code, name, triggeredAt, data, protocolData }
}
export function deserialize (buffer: Buffer) {
const envelopeReader = Reader.from(buffer)
const type = envelopeReader.readUInt8Number()
const requestId = envelopeReader.readUInt32Number()
const dataBuff = envelopeReader.readVarOctetString()
const reader = new Reader(dataBuff)
let data
switch (type) {
case Type.TYPE_RESPONSE:
case Type.TYPE_MESSAGE:
data = { protocolData: readProtocolData(reader) }
break
case Type.TYPE_TRANSFER:
data = readTransfer(reader)
break
case Type.TYPE_ERROR:
data = readError(reader)
break
default:
throw new Error('Unrecognized type')
}
return { type, requestId, data }
}
interface BtpTransferWithoutProtocolData {
amount: string
}
interface BtpErrorWithoutProtocolData {
code: string
name: string
triggeredAt: string
data: string
}
// The following functions use an alternative format to access the exposed
// serialize/deserialize functionality. There is one such serialize* function per BTP call.
// The arguments passed to them are aligned with the objects defined in the Ledger-Plugin-Interface (LPI),
// which makes these functions convenient to use when working with LPI objects.
export const serializeResponse = (requestId: number, protocolData: ProtocolData[]) => {
return serialize({
type: Type.TYPE_RESPONSE,
requestId,
data: { protocolData }
})
}
export const serializeError = (error: BtpErrorWithoutProtocolData, requestId: number, protocolData: ProtocolData[]) => {
let dataFields
const { code, name, triggeredAt, data } = error
dataFields = { code, name, triggeredAt, data, protocolData }
return serialize({
type: Type.TYPE_ERROR,
requestId,
data: dataFields
})
}
export const serializeMessage = (requestId: number, protocolData: ProtocolData[]) => {
return serialize({
type: Type.TYPE_MESSAGE,
requestId,
data: { protocolData }
})
}
export const serializeTransfer = (transfer: BtpTransferWithoutProtocolData, requestId: number, protocolData: ProtocolData[]) => {
const { amount } = transfer
return serialize({
type: Type.TYPE_TRANSFER,
requestId,
data: {
amount,
protocolData
}
})
}