astra-lightning 1.1.4 → 1.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -20,6 +20,10 @@ const { parse } = Date;
20
20
  const { round } = Math;
21
21
  const tokensAsMtok = (tokens) => (BigInt(tokens) * BigInt(1e3)).toString();
22
22
  const type = "default";
23
+ const isDuplicateInvoiceError = (err = {}) => {
24
+ const message = err.details || err.message || String();
25
+ return typeof message === "string" && message.includes(invoiceExistsError);
26
+ };
23
27
 
24
28
  /** Create a Lightning invoice.
25
29
 
@@ -81,7 +85,10 @@ module.exports = (args, cbk) => {
81
85
 
82
86
  // Check arguments
83
87
  validate: (cbk) => {
84
- if (!!args.expires_at && new Date().toISOString() > args.expires_at) {
88
+ if (
89
+ !!args.expires_at &&
90
+ Math.floor(Date.now() / 1000) > Number(args.expires_at)
91
+ ) {
85
92
  return cbk([400, "ExpectedFutureDateForInvoiceExpiration"]);
86
93
  }
87
94
 
@@ -163,9 +170,10 @@ module.exports = (args, cbk) => {
163
170
  ({ addAddress, hints, mtokens, preimage }, cbk) => {
164
171
  const fallbackAddress = !addAddress ? String() : addAddress.address;
165
172
  const createdAt = new Date();
166
- const expireAt = !args.expires_at ? null : parse(args.expires_at);
167
-
173
+ const expireAt = !args.expires_at ? null : Math.floor(args.expires_at*msPerSec);
174
+
168
175
  const expiryMs = !expireAt ? null : expireAt - createdAt.getTime();
176
+
169
177
  if (!args.asset_id) {
170
178
  return args.lnd.default.addInvoice(
171
179
  {
@@ -183,7 +191,7 @@ module.exports = (args, cbk) => {
183
191
  value_msat: mtokens,
184
192
  },
185
193
  (err, response) => {
186
- if (!!err && err.details === invoiceExistsError) {
194
+ if (!!err && isDuplicateInvoiceError(err)) {
187
195
  return cbk([409, "InvoiceWithGivenHashAlreadyExists"]);
188
196
  }
189
197
 
@@ -228,6 +236,10 @@ module.exports = (args, cbk) => {
228
236
  }
229
237
  );
230
238
  } else {
239
+ const expiry = !expiryMs
240
+ ? defaultExpirySec
241
+ : round(expiryMs / msPerSec)
242
+ console.log('expiry',expiry,preimage,args.asset_id)
231
243
  return args.tpr.taproot_asset_channels.addInvoice(
232
244
  {
233
245
  asset_id: hexAsBuffer(args.asset_id),
@@ -236,9 +248,7 @@ module.exports = (args, cbk) => {
236
248
  invoice_request: {
237
249
  cltv_expiry: !args.cltv_delta ? undefined : args.cltv_delta,
238
250
  description_hash: hexAsBuffer(args.description_hash),
239
- expiry: !expiryMs
240
- ? defaultExpirySec
241
- : round(expiryMs / msPerSec),
251
+ expiry: expiry,
242
252
  fallback_addr: fallbackAddress,
243
253
  is_blinded: !!args.is_encrypting_routes,
244
254
  memo: args.description,
@@ -248,10 +258,9 @@ module.exports = (args, cbk) => {
248
258
  },
249
259
  },
250
260
  (err, response) => {
251
- if (!!err && err.details === invoiceExistsError) {
261
+ if (!!err && isDuplicateInvoiceError(err)) {
252
262
  return cbk([409, "InvoiceWithGivenHashAlreadyExists"]);
253
263
  }
254
-
255
264
  if (!!err) {
256
265
  return cbk([503, "AddInvoiceError", { err }]);
257
266
  }
package/package.json CHANGED
@@ -53,5 +53,5 @@
53
53
  "directory": "test/typescript"
54
54
  },
55
55
  "types": "index.d.ts",
56
- "version": "1.1.4"
56
+ "version": "1.1.5"
57
57
  }
package/test.js CHANGED
@@ -5,6 +5,8 @@ const {
5
5
  decodePaymentRequest,
6
6
  decodeAssetPayReq,
7
7
  createInvoice,
8
+ subscribeToInvoices,
9
+ subscribeToPastPayments
8
10
  } = require("./index.js");
9
11
  const fs = require("fs");
10
12
  // const dayjs = require("dayjs");
@@ -180,9 +182,11 @@ const addTapdInvoice = async () => {
180
182
  lnd,
181
183
  tpr,
182
184
  mtokens: 1000,
185
+ expires_at:'1764839000',
183
186
  asset_id: assetId,
187
+ secret:'dfdd9d34ddf25728e84f6d2a32ed37a88e2b516e3488d051c70a62686d889481'
184
188
  }).catch((e) => {
185
- console.error("Failed to create invoice:", e.message);
189
+ console.error("Failed to create invoice:", e);
186
190
  });
187
191
  console.log("🚀 ~ addTapdInvoice ~ ret:", ret);
188
192
  };
@@ -337,6 +341,11 @@ const getBestLndChannel = async (asset) => {
337
341
  };
338
342
 
339
343
  const test = async () => {
344
+
345
+ /* const sub = subscribeToInvoices({ lnd });
346
+ sub.on("invoice_updated",async(invoice)=>{
347
+ console.log('invoice',invoice)
348
+ }) */
340
349
  await addTapdInvoice();
341
350
  //await decodeTapdInvoice();
342
351
  //await listChannels();
@@ -361,6 +370,11 @@ const test = async () => {
361
370
  // },
362
371
  // });
363
372
  // console.log("🚀 ~ test ~ retPay:", retPay)
373
+
374
+ /* const sub = subscribeToPastPayments({ lnd });
375
+ sub.on("payment", async (payment) => {
376
+ console.log('payment',payment)
377
+ }); */
364
378
 
365
379
  };
366
380
  test();