Skip to content

Commit

Permalink
feat: add codecs for ilp fulfillment packets
Browse files Browse the repository at this point in the history
  • Loading branch information
justmoon committed Nov 14, 2017
1 parent 608cfeb commit 1176ff0
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 4 deletions.
47 changes: 45 additions & 2 deletions packages/ilp-packet/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ enum Type {
TYPE_ILQP_BY_SOURCE_RESPONSE = 5,
TYPE_ILQP_BY_DESTINATION_REQUEST = 6,
TYPE_ILQP_BY_DESTINATION_RESPONSE = 7,
TYPE_ILP_ERROR = 8
TYPE_ILP_ERROR = 8,
TYPE_ILP_FULFILLMENT = 9
}

const serializeEnvelope = (type: number, contents: Buffer) => {
Expand Down Expand Up @@ -456,6 +457,40 @@ const deserializeIlpError = (binary: Buffer): IlpError => {
}
}

interface IlpFulfillment {
data: string
}

const serializeIlpFulfillment = (json: IlpFulfillment) => {
const writer = new Writer()

// data
writer.writeVarOctetString(Buffer.from(json.data || '', 'base64'))

// extensibility
writer.writeUInt8(0)

return serializeEnvelope(Type.TYPE_ILP_FULFILLMENT, writer.getBuffer())
}

const deserializeIlpFulfillment = (binary: Buffer): IlpFulfillment => {
const { type, contents } = deserializeEnvelope(binary)

if (type !== Type.TYPE_ILP_FULFILLMENT) {
throw new Error('Packet has incorrect type')
}

const reader = Reader.from(contents)

const data = base64url(reader.readVarOctetString())

// Ignore remaining bytes for extensibility

return {
data
}
}

const serializeIlpPacket = (obj: IlpPacket) => {
switch (obj.type) {
case Type.TYPE_ILP_PAYMENT: return serializeIlpPayment(obj.data)
Expand All @@ -466,12 +501,14 @@ const serializeIlpPacket = (obj: IlpPacket) => {
case Type.TYPE_ILQP_BY_DESTINATION_REQUEST: return serializeIlqpByDestinationRequest(obj.data)
case Type.TYPE_ILQP_BY_DESTINATION_RESPONSE: return serializeIlqpByDestinationResponse(obj.data)
case Type.TYPE_ILP_ERROR: return serializeIlpError(obj.data)
case Type.TYPE_ILP_FULFILLMENT: return serializeIlpFulfillment(obj.data)
default: throw new Error('Object has invalid type')
}
}

const deserializeIlpPacket = (binary: Buffer) => {
let packet, typeString
let packet
let typeString
switch (binary[0]) {
case Type.TYPE_ILP_PAYMENT:
packet = deserializeIlpPayment(binary)
Expand Down Expand Up @@ -505,6 +542,10 @@ const deserializeIlpPacket = (binary: Buffer) => {
packet = deserializeIlpError(binary)
typeString = 'ilp_error'
break
case Type.TYPE_ILP_FULFILLMENT:
packet = deserializeIlpFulfillment(binary)
typeString = 'ilp_fulfillment'
break
default:
throw new Error('Packet has invalid type')
}
Expand Down Expand Up @@ -533,6 +574,8 @@ module.exports = {
deserializeIlqpByDestinationResponse,
serializeIlpError,
deserializeIlpError,
serializeIlpFulfillment,
deserializeIlpFulfillment,
serializeIlpPacket,
deserializeIlpPacket
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "example 1",
"binary": "09020000",
"json": {
"data": ""
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "example 2",
"binary": "09050365965900",
"json": {
"data": "ZZZZ"
}
}
36 changes: 34 additions & 2 deletions packages/ilp-packet/test/parser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ describe('Parser', function () {
})

describe('serializeIlpError', function () {
describe('correctly serializes valid ilqp by destination responses', function () {
describe('correctly serializes valid ilp errors', function () {
const validTests = loadTests({ type: 'ilp_error' })

for (let test of validTests) {
Expand All @@ -254,7 +254,7 @@ describe('Parser', function () {
})

describe('deserializeIlpError', function () {
describe('correctly parses valid ilqp by destination responses', function () {
describe('correctly parses valid ilp errors', function () {
const validTests = loadTests({ type: 'ilp_error' })

for (let test of validTests) {
Expand All @@ -271,6 +271,38 @@ describe('Parser', function () {
})
})

describe('serializeIlpFulfillment', function () {
describe('correctly serializes valid ilp fulfillments', function () {
const validTests = loadTests({ type: 'ilp_fulfillment' })

for (let test of validTests) {
it(test.name, function () {
const json = test.json

const serialized = Parser.serializeIlpFulfillment(json)

assert.deepEqual(serialized.toString('hex'), test.binary)
})
}
})
})

describe('deserializeIlpFulfillment', function () {
describe('correctly parses valid ilp fulfillments', function () {
const validTests = loadTests({ type: 'ilp_fulfillment' })

for (let test of validTests) {
it(test.name, function () {
const binary = new Buffer(test.binary, 'hex')

const parsed = Parser.deserializeIlpFulfillment(binary)

assert.deepEqual(parsed, test.json)
})
}
})
})

describe('deserializeIlpPacket', function () {
describe('correctly parses valid ilp packets', function () {
testPackets('ilp_payment', Parser.Type.TYPE_ILP_PAYMENT)
Expand Down

0 comments on commit 1176ff0

Please sign in to comment.