Skip to content

Commit

Permalink
Merge pull request #140 from getAlby/feat/decode-invoice-v2
Browse files Browse the repository at this point in the history
feat: decode invoice
  • Loading branch information
rolznz authored Nov 17, 2023
2 parents ddd7df4 + 128146c commit 7ed7fe6
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 2 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ Please have a look a the Alby OAuth2 Wallet API:
- outgoingInvoices
- getInvoice
- createInvoice
- decodeInvoice
- keysend
- sendPayment
- sendBoostagram
Expand Down Expand Up @@ -454,9 +455,12 @@ console.log(response.keysends);
For quick invoice decoding without an API request please see Alby's [Lightning Tools package](https://github.com/getAlby/js-lightning-tools#basic-invoice-decoding).
For more invoice details you can use the Alby Wallet API.
For more invoice details you can use the Alby Wallet API:
**COMING SOON**
```js
const decodedInvoice = await client.decodeInvoice(paymentRequest);
const {payment_hash, amount, description, ...} = decodedInvoice;
```
## fetch() dependency
Expand Down
35 changes: 35 additions & 0 deletions examples/decode-invoice.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import * as readline from "node:readline/promises";
import { stdin as input, stdout as output } from "node:process";
import { auth, Client } from "../dist/index.module.js";

const rl = readline.createInterface({ input, output });

const paymentRequest =
"lnbc10u1pj4t6w0pp54wm83znxp8xly6qzuff2z7u6585rnlcw9uduf2haa42qcz09f5wqdq023jhxapqd4jk6mccqzzsxqyz5vqsp5mlvjs8nktpz98s5dcrhsuelrz94kl2vjukvu789yzkewast6m00q9qyyssqupynqdv7e5y8nlul0trva5t97g7v3gwx7akhu2dvu4pn66eu2pr5zkcnegp8myz3wrpj9ht06pwyfn4dvpmnr96ejq6ygex43ymaffqq3gud4d";

const authClient = new auth.OAuth2User({
client_id: process.env.CLIENT_ID,
client_secret: process.env.CLIENT_SECRET,
callback: "http://localhost:8080",
scopes: ["invoices:read"], // this scope isn't needed, but at least one scope is required to get an access token
token: {
access_token: undefined,
refresh_token: undefined,
expires_at: undefined,
}, // initialize with existing token
});

console.log(`Open the following URL and authenticate the app:`);
console.log(authClient.generateAuthURL());
console.log("----\n");

const code = await rl.question("Code: (localhost:8080?code=[THIS CODE]: ");
rl.close();

await authClient.requestAccessToken(code);
console.log(authClient.token);
const client = new Client(authClient);

const response = await client.decodeInvoice(paymentRequest);

console.log(JSON.stringify(response, null, 2));
14 changes: 14 additions & 0 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
CreateSwapResponse,
CreateWebhookEndpointParams,
CreateWebhookEndpointResponse,
DecodedInvoice,
GetAccountBalanceResponse,
GetAccountInformationResponse,
GetInvoicesRequestParams,
Expand Down Expand Up @@ -151,6 +152,19 @@ export class Client {
});
}

decodeInvoice(
paymentRequest: string,
request_options?: Partial<RequestOptions>,
): Promise<DecodedInvoice> {
return rest({
auth: this.auth,
...this.defaultRequestOptions,
...request_options,
endpoint: `/decode/bolt11/${paymentRequest}`,
method: "GET",
});
}

createInvoice(
invoice: InvoiceRequestParams,
request_options?: Partial<RequestOptions>,
Expand Down
19 changes: 19 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,4 +265,23 @@ export type GetAccountInformationResponse = {
nostr_pubkey?: string;
};

export type DecodedInvoice = {
currency: string;
/**
* unix timestamp in seconds
*/
created_at: number;
/**
* expiry from the created_at time in seconds (not a timestamp)
*/
expiry: number;
payee: string;
msatoshi: number;
description: string;
payment_hash: string;
min_final_cltv_expiry: number;
amount: number;
payee_alias: string;
};

export { AlbyResponseError, RequestOptions };

0 comments on commit 7ed7fe6

Please sign in to comment.