Skip to content

Commit 00a78e9

Browse files
authored
feat(csi-785): unified ILP interface (#217)
* feat(csi-785): unified ILP interface * chore(snapshot): 19.2.0-snapshot.0
1 parent c67463c commit 00a78e9

8 files changed

Lines changed: 84 additions & 35 deletions

File tree

‎.eslintrc.json‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
"jest": true
66
},
77
"extends": "eslint:recommended",
8+
"parserOptions": {
9+
"ecmaVersion": 13
10+
},
811
"rules": {
912
"indent": [
1013
"error",

‎package-lock.json‎

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@mojaloop/sdk-standard-components",
3-
"version": "19.1.3",
3+
"version": "19.2.0-snapshot.0",
44
"description": "A set of standard components for connecting to Mojaloop API enabled Switches",
55
"main": "src/index.js",
66
"types": "src/index.d.ts",

‎src/ilp.d.ts‎

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,25 @@
11
// todo: think, how to generate JSDocs based on these TS types
2+
type Prettify<T> = { //to make hover overlay more readable
3+
[K in keyof T]: T[K];
4+
} & {};
5+
6+
type Amount = {
7+
amount: string;
8+
currency: string;
9+
};
10+
211
export type IlpResponse = {
312
fulfilment: string;
413
condition: string;
514
ilpPacket: string;
615
}
716

8-
export type TransactionObject = {
9-
quoteId: string;
10-
transactionId: string;
11-
transactionType: TransactionType;
12-
payee: Party;
13-
payer: Party;
14-
expiration: string;
15-
amount: {
16-
amount: string;
17-
currency: string;
18-
};
19-
note?: string;
20-
};
17+
export type TransactionObject = Prettify<
18+
Pick<QuoteRequest, 'quoteId' | 'transactionId' | 'transactionType' | 'payee' | 'payer' | 'expiration'> & {
19+
amount: Amount;
20+
note?: string;
21+
}
22+
>;
2123

2224
export type IlpInputV1 = {
2325
amount: string;
@@ -64,14 +66,30 @@ export type TransactionType = {
6466
subScenario?: string;
6567
}
6668

69+
export type QuoteRequest = {
70+
quoteId: string;
71+
transactionId: string;
72+
transactionType: TransactionType;
73+
payee: Party;
74+
payer: Party;
75+
expiration: string;
76+
// add more fields, if needed
77+
}
78+
79+
export type QuoteResponse = {
80+
transferAmount: Amount;
81+
note?: string;
82+
// add more fields, if needed
83+
};
84+
6785
export type FxQuoteRequest = {
6886
conversionRequestId: string;
69-
// todo: define the rest fields
87+
// add more fields, if needed
7088
}
7189

7290
export type FxQuoteBeResponse = {
7391
conversionTerms: {
7492
expiration: string;
7593
};
76-
// todo: define the rest fields
94+
// add more fields, if needed
7795
};

‎src/index.d.ts‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -875,13 +875,14 @@ declare namespace SDKStandardComponents {
875875
new (options: IlpOptions): IlpProcessor;
876876
version: keyof typeof Ilp.ILP_VERSIONS;
877877
calculateConditionFromFulfil(fulfilment: string): string;
878-
calculateFulfil(transactionObject: ilp.TransactionObject | string): string
878+
calculateFulfil(ilpPacket: string): string
879879
decodeIlpPacket(ilpPacket: string): ilp.IlpInputV1 | ilp.IlpInputV4;
880880
getFxQuoteResponseIlp(fxQuoteRequest: ilp.FxQuoteRequest, fxQuoteBeResponse: ilp.FxQuoteBeResponse): ilp.IlpResponse;
881+
getQuoteResponseIlp(quoteRequest: ilp.QuoteRequest, quoteResponse: ilp.QuoteResponse): ilp.IlpResponse;
881882
getResponseIlp(transactionObject: ilp.TransactionObject): ilp.IlpResponse;
882883
getTransactionObject(ilpPacket: string): ilp.TransactionObject;
883-
validateFulfil(fulfilment: string, condition: string): boolean
884-
// add other public methods and define needed types
884+
validateFulfil(fulfilment: string, condition: string): boolean;
885+
validateIlpAgainstTransferRequest(transactionObject: ilp.TransactionObject): boolean;
885886
}
886887

887888
type IlpOptions = {

‎src/lib/ilp/IlpV4.js‎

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class IlpV4 extends IlpBase {
4343
* @returns {IlpResponse} - object containing the fulfilment, condition and ilp packet (v4)
4444
*/
4545
getResponseIlp(transactionObject) {
46-
const fulfilment = this.calculateFulfil(transactionObject);
46+
const fulfilment = this.#calculateFulfilFromTransactionObject(transactionObject);
4747
const condition = super.calculateConditionFromFulfil(fulfilment);
4848
const ilpPacket = this.calculateIlpPacket(transactionObject, condition);
4949

@@ -57,6 +57,17 @@ class IlpV4 extends IlpBase {
5757
return result;
5858
}
5959

60+
/**
61+
* Calculates a fulfilment given a base64 encoded ilp packet
62+
*
63+
* @returns {string} - string containing base64 encoded fulfilment
64+
*/
65+
calculateFulfil(base64EncodedPacket) {
66+
const transactionObject = this.getTransactionObject(base64EncodedPacket);
67+
const { fulfilment } = this.getResponseIlp(transactionObject);
68+
return fulfilment;
69+
}
70+
6071
/**
6172
* @typedef {Object} IlpInputV4
6273
* @prop {String} amount - Transfer amount.
@@ -110,18 +121,6 @@ class IlpV4 extends IlpBase {
110121
return ILP_ADDRESS;
111122
}
112123

113-
/**
114-
* Calculates a fulfilment given a transaction object and a secret
115-
*
116-
* @returns {string} - string containing base64 encoded fulfilment
117-
*/
118-
calculateFulfil(transactionObject) {
119-
const base64EncodedTransaction = Buffer.from(safeStringify(transactionObject)).toString('base64');
120-
const encodedSecret = Buffer.from(this.secret).toString('base64');
121-
122-
return super._createHmac(base64EncodedTransaction, encodedSecret);
123-
}
124-
125124
calculateIlpPacket(transactionObject, condition) {
126125
const packetInput = this.makeQuotePacketInput(transactionObject, condition);
127126
const packet = ilpPacket.serializeIlpPrepare(packetInput);
@@ -138,6 +137,18 @@ class IlpV4 extends IlpBase {
138137
const binaryPacket = Buffer.from(inputIlpPacket, 'base64');
139138
return ilpPacket.deserializeIlpPrepare(binaryPacket);
140139
}
140+
141+
/**
142+
* Calculates a fulfilment given a transaction object and a secret
143+
*
144+
* @returns {string} - string containing base64 encoded fulfilment
145+
*/
146+
#calculateFulfilFromTransactionObject(transactionObject) {
147+
const base64EncodedTransaction = Buffer.from(safeStringify(transactionObject)).toString('base64');
148+
const encodedSecret = Buffer.from(this.secret).toString('base64');
149+
150+
return super._createHmac(base64EncodedTransaction, encodedSecret);
151+
}
141152
}
142153

143154
module.exports = IlpV4;

‎test/unit/lib/ilp/IlpV1.test.js‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,14 @@ describe('IlpV1 Tests -->', () => {
101101
expect(condition).toBeTruthy();
102102
});
103103

104+
test('should calculate fulfilment based on ilpPacket sting', () => {
105+
const transactionObj = dto.transactionObjectDto(quoteRequest, partialResponse);
106+
const ilpCombo = ilp.getResponseIlp(transactionObj);
107+
108+
const fulfilment = ilp.calculateFulfil(ilpCombo.ilpPacket);
109+
expect(fulfilment).toBe(ilpCombo.fulfilment);
110+
});
111+
104112
describe('Ilp Packet Serialize tests -->', () => {
105113
const createIlpJson = (amount) => ({
106114
data: Buffer.from('data'),

‎test/unit/lib/ilp/IlpV4.test.js‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,14 @@ describe('IlpV4 Tests -->', () => {
134134
expect(json).toBeTruthy();
135135
});
136136

137+
test('should calculate fulfilment based on ilpPacket sting', () => {
138+
const transactionObj = dto.transactionObjectDto(quoteRequest, partialResponse);
139+
const ilpCombo = ilp.getResponseIlp(transactionObj);
140+
141+
const fulfilment = ilp.calculateFulfil(ilpCombo.ilpPacket);
142+
expect(fulfilment).toBe(ilpCombo.fulfilment);
143+
});
144+
137145
describe('Ilp Packet Serialize tests -->', () => {
138146
const createIlpJson = (amount) => ({
139147
amount,
@@ -197,7 +205,7 @@ describe('IlpV4 Tests -->', () => {
197205
expect(transaction.amount).toEqual(partialResponse.transferAmount);
198206
});
199207

200-
test('Should validate the transfer request against the decoded Ilp packet', () => {
208+
test('should validate the transfer request against the decoded Ilp packet', () => {
201209
const validation = ilp.validateIlpAgainstTransferRequest(transferRequest);
202210
expect(validation).toBe(true);
203211
});

0 commit comments

Comments
 (0)