Skip to content

Commit

Permalink
fix: invoice decoding library (#180)
Browse files Browse the repository at this point in the history
  • Loading branch information
michael1011 authored Jan 17, 2020
1 parent 57360a0 commit 8a1dc47
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 80 deletions.
3 changes: 1 addition & 2 deletions lib/Utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,11 @@ export const minutesToMilliseconds = (minutes: number) => {
return minutes * 60 * 1000;
};

// TODO: fix invoice decoding on testnet
/**
* Gets the amount of an invoice in satoshis
*/
export const getInvoiceAmt = (invoice: string): number => {
return Number(bolt11.decode(invoice).millisatoshis) / 1000;
return bolt11.decode(invoice).satoshis || 0;
};

/**
Expand Down
5 changes: 2 additions & 3 deletions lib/service/Service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
getLightningCurrency,
getSwapMemo,
getVersion,
getInvoiceAmt,
} from '../Utils';
import {
Balance,
Expand Down Expand Up @@ -336,9 +337,7 @@ class Service {

const timeoutBlockDelta = this.timeoutDeltaProvider.getTimeout(pairId, side, false);

const { lndClient } = this.currencies.get(lightningCurrency)!;
const { numSatoshis: invoiceAmount } = await lndClient!.decodePayReq(invoice);

const invoiceAmount = getInvoiceAmt(invoice);
const rate = getRate(pairRate, side, false);

this.verifyAmount(pairId, rate, invoiceAmount, side, false);
Expand Down
104 changes: 32 additions & 72 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
"LICENSE"
],
"dependencies": {
"@boltz/bolt11": "^1.2.5",
"@boltz/bolt11": "^1.2.7",
"@google-cloud/storage": "^4.2.0",
"@iarna/toml": "^2.2.3",
"async-lock": "^1.2.2",
Expand Down Expand Up @@ -86,7 +86,7 @@
"@types/async-lock": "^1.1.1",
"@types/cors": "^2.8.6",
"@types/express": "^4.17.2",
"@types/jest": "^24.0.25",
"@types/jest": "^24.9.0",
"@types/node-forge": "^0.9.1",
"@types/node-schedule": "^1.3.0",
"@types/uuid": "^3.4.6",
Expand All @@ -95,7 +95,7 @@
"concurrently": "^5.0.2",
"conventional-changelog": "^3.1.18",
"conventional-changelog-cli": "^2.0.31",
"get-port": "^5.1.0",
"get-port": "^5.1.1",
"grpc-tools": "^1.8.1",
"grpc_tools_node_protoc_ts": "^2.5.9",
"jest": "^24.9.0",
Expand Down
10 changes: 10 additions & 0 deletions test/unit/Utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,16 @@ describe('Utils', () => {
// tslint:disable-next-line: max-line-length
'lnbcrt987650n1pwddnskpp5d4tw4gpjgqdqlgkq5yc309r2kguure53cff8a0kjta5hurltc4yqdqqcqzpgzeu404h9udp5ay39kdvau7m5kdkvycajfhx46slgkfgyhpngnztptulxpx8s7qncp45v5nxjulje5268cu22gxysg9hm3ul8ktrw5zgqcg98hg',
)).toEqual(98765);

expect(utils.getInvoiceAmt(
// tslint:disable-next-line: max-line-length
'lntltc1u1p0p7fuypp52k9p5mszjc9x0swfw5tum8rsdguzgxmqtg4rr2xaczhkrssvse0sdqqcqzjqxuya98m2834m3xdn7pe6kj5t2q8n3rx9ft37ygvkj07g4a2h7q3xp5mkh44f4jyuupg9t69xf0xknzqa7ufah4dqms5xz4veqplykmcqp4fue6',
)).toEqual(100);

expect(utils.getInvoiceAmt(
// tslint:disable-next-line: max-line-length
'lntb1p0p7f6lpp58zpsxk88e8uz2lndqrf9radths484jqlhmas92kt7md2cqszpn6qdqqcqzpgrzjqtejjv7p39kcv5gezydzgse8ea3kcw8zqe36afy64zem09us5hjgcxgphsqqqzsqqyqqqqlgqqqqqqgq9q4wqv5a4uwhhvfh93k2ue75lrre50tk99pk689qgf6ul5my5vr749689wcunnv7zjcuk7jlpwz44fv87ra2snsjzw34pnfs5d477u82cpm6cym6',
)).toEqual(0);
});

test('should get rate', () => {
Expand Down

0 comments on commit 8a1dc47

Please sign in to comment.