astra-lightning 1.1.5 → 1.1.6

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.
@@ -31,6 +31,9 @@ export type CreateInvoiceArgs = AuthenticatedLightningArgs<{
31
31
  asset_id?: string;
32
32
  peer_pubkey?: string;
33
33
  tpr?: any;
34
+ group_key?: string;
35
+ price_oracle_metadata?: string;
36
+
34
37
  }>;
35
38
 
36
39
  export type CreateInvoiceResult = {
@@ -239,7 +239,7 @@ module.exports = (args, cbk) => {
239
239
  const expiry = !expiryMs
240
240
  ? defaultExpirySec
241
241
  : round(expiryMs / msPerSec)
242
- console.log('expiry',expiry,preimage,args.asset_id)
242
+
243
243
  return args.tpr.taproot_asset_channels.addInvoice(
244
244
  {
245
245
  asset_id: hexAsBuffer(args.asset_id),
@@ -256,6 +256,8 @@ module.exports = (args, cbk) => {
256
256
  r_preimage: preimage || undefined,
257
257
  route_hints: hints || undefined,
258
258
  },
259
+ group_key: args.group_key ? hexAsBuffer(args.group_key) : undefined,
260
+ price_oracle_metadata: args.price_oracle_metadata,
259
261
  },
260
262
  (err, response) => {
261
263
  if (!!err && isDuplicateInvoiceError(err)) {
@@ -7,6 +7,8 @@ export type PaymentTaprootInvoiceArgs = AuthenticatedLightningArgs<{
7
7
  asset_id: string;
8
8
  asset_amount: number;
9
9
  peer_pubkey: string;
10
+ price_oracle_metadata?: string;
11
+ group_key?: string;
10
12
  payment_request: {
11
13
  payment_request: string;
12
14
  fee_limit_sat: number;
@@ -43,6 +43,8 @@ module.exports = (args) => {
43
43
  asset_amount: args.asset_amount,
44
44
  peer_pubkey: hexAsBuffer(args.peer_pubkey),
45
45
  payment_request: args.payment_request,
46
+ price_oracle_metadata: args.price_oracle_metadata ? args.price_oracle_metadata : undefined,
47
+ group_key: args.group_key ? hexAsBuffer(args.group_key) : undefined,
46
48
  }
47
49
  const sub = args.tpr.taproot_asset_channels.sendPayment(callRequest);
48
50
  const ret = {
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.5"
56
+ "version": "1.1.6"
57
57
  }
package/test.js CHANGED
@@ -5,10 +5,15 @@ const {
5
5
  decodePaymentRequest,
6
6
  decodeAssetPayReq,
7
7
  createInvoice,
8
+ signMessage,
9
+ getIdentity,
8
10
  subscribeToInvoices,
9
11
  subscribeToPastPayments
10
12
  } = require("./index.js");
11
13
  const fs = require("fs");
14
+ const crypto = require("crypto");
15
+ const { createHash } = require("crypto");
16
+
12
17
  // const dayjs = require("dayjs");
13
18
  /* const LND_TLS_CERT =
14
19
  "/Users/wjl/Documents/gamichi/2024/astra/volumes/server/tls.cert";
@@ -24,17 +29,17 @@ const LND_SOCKET = "54.92.19.81:10109"; */
24
29
  // const invoice =
25
30
  // "lnbcrt10u1p5q3r0zpp50v0asn72aav354e27cals6l7j76sln0pzzvxktgur2wmlkf44mfqdy0f3hxv6fmv9ehxet5f9zr5dtzvscnywtyx56rjcnyxcuxxctyxc6xvvnrxfjxyvf5xymnxvnpx5erxef5x56rvcf58ymrvvnrxvunqefcvvcngv3ex3skzdm98q6rkctdda6kuap6xycrqwscqzzsxqrrssrzjqg3zz9zaagxqxan9fqzr6k7yvf900uf26m0z7c2g0rf55raupqclpakyc0a4gzq2v5qqqqlgqqqqqqgq2qsp58qmfwjwdrvr9dzjs2esms3cpvvgff3g4hsunszne3v7wszwcq62s9qxpqysgqscq2e3sqa0pfe5yzjdnp8q4temjazdw6y77c49tpvnhz0d4kjux92tw3gy9vvrh68qqef594frdp2uqhvwjy0sd7ezdcanvlln2rusgpg5w96t";
26
31
 
27
- const LND_SOCKET = "127.0.0.1:8444";
28
32
  const LND_MACAROON =
29
- "0201036c6e6402f801030a10cd485d60d65e48a8f253aeca1f20d5e51201301a160a0761646472657373120472656164120577726974651a130a04696e666f120472656164120577726974651a170a08696e766f69636573120472656164120577726974651a210a086d616361726f6f6e120867656e6572617465120472656164120577726974651a160a076d657373616765120472656164120577726974651a170a086f6666636861696e120472656164120577726974651a160a076f6e636861696e120472656164120577726974651a140a057065657273120472656164120577726974651a180a067369676e6572120867656e657261746512047265616400000620d0b51747c52a6444a78c9e08947671f20362dfffc89404c10afb68d09e72a6e2";
33
+ "0201036c6e6402f801030a1020a1bf5a962e198f49d7d0cb5dacb7221201301a160a0761646472657373120472656164120577726974651a130a04696e666f120472656164120577726974651a170a08696e766f69636573120472656164120577726974651a210a086d616361726f6f6e120867656e6572617465120472656164120577726974651a160a076d657373616765120472656164120577726974651a170a086f6666636861696e120472656164120577726974651a160a076f6e636861696e120472656164120577726974651a140a057065657273120472656164120577726974651a180a067369676e6572120867656e6572617465120472656164000006207db76ec2a58ad259d4a4a5d3c4cff4bbd48b8667dee0cfdbf7e0299303fae9c6";
30
34
  const LND_TPROOT_ASSET_MACAROON =
31
- "0201047461706402d001030a10d5485d60d65e48a8f253aeca1f20d5e51201301a180a09616464726573736573120472656164120577726974651a150a06617373657473120472656164120577726974651a170a086368616e6e656c73120472656164120577726974651a150a066461656d6f6e120472656164120577726974651a130a046d696e74120472656164120577726974651a150a0670726f6f6673120472656164120577726974651a120a03726671120472656164120577726974651a170a08756e6976657273651204726561641205777269746500000620501385af6ee7b06d073829f55af1a69d6a3ecc4cfb59129c88c379a429f44440";
32
- const base64Cert =
33
- "LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUNERENDQWJLZ0F3SUJBZ0lSQUo3Rys0QzdYWldMN3hzMzk3bjMyUGd3Q2dZSUtvWkl6ajBFQXdJd01ERWcKTUI0R0ExVUVDaE1YYkdsMFpDQmhkWFJ2WjJWdVpYSmhkR1ZrSUdObGNuUXhEREFLQmdOVkJBTVRBMkp2WWpBZQpGdzB5TlRFeU1ESXdNakEzTURoYUZ3MHlOekF4TWpjd01qQTNNRGhhTURBeElEQWVCZ05WQkFvVEYyeHBkR1FnCllYVjBiMmRsYm1WeVlYUmxaQ0JqWlhKME1Rd3dDZ1lEVlFRREV3TmliMkl3V1RBVEJnY3Foa2pPUFFJQkJnZ3EKaGtqT1BRTUJCd05DQUFTU3BKdmh6M1lXVG5jTFVNR3Rvb1poUHNTVmlwR09RTWszbHkyMGRqUUk2OXd1M2gyRAphdG5TVGRSOTBCT2FhWUlrZ1lXRWpWV3Era1VDSWxXdjliMmJvNEdzTUlHcE1BNEdBMVVkRHdFQi93UUVBd0lDCnBEQVRCZ05WSFNVRUREQUtCZ2dyQmdFRkJRY0RBVEFQQmdOVkhSTUJBZjhFQlRBREFRSC9NQjBHQTFVZERnUVcKQkJTaE9PZDArRmxSL0xzZ0t0MWROQktKQXBwSkREQlNCZ05WSFJFRVN6QkpnZ05pYjJLQ0NXeHZZMkZzYUc5egpkSUlFZFc1cGVJSUtkVzVwZUhCaFkydGxkSUlIWW5WbVkyOXVib2NFZndBQUFZY1FBQUFBQUFBQUFBQUFBQUFBCkFBQUFBWWNFckJNQUF6QUtCZ2dxaGtqT1BRUURBZ05JQURCRkFpRUExRm8xakg3Z0IzY2pBblNSaUNKbDF3a1kKZGYxYnZVd1JSSzl0V1NIclFlc0NJR2tKQWkxWk5hNm9DeXFpemNObXZORmpUWXJHY1BmRUxxTCtiSjAyZUIvbAotLS0tLUVORCBDRVJUSUZJQ0FURS0tLS0tCg==";
34
- const assetId =
35
- "b787a089e60451305c090b7b3604b7a107e13d0a955c8ed1043d874a448aa465";
36
- const invoice =
35
+ "0201047461706402d001030a1027a1bf5a962e198f49d7d0cb5dacb7221201301a180a09616464726573736573120472656164120577726974651a150a06617373657473120472656164120577726974651a170a086368616e6e656c73120472656164120577726974651a150a066461656d6f6e120472656164120577726974651a130a046d696e74120472656164120577726974651a150a0670726f6f6673120472656164120577726974651a120a03726671120472656164120577726974651a170a08756e69766572736512047265616412057772697465000006201bcecd78be5724f8127277b4b68fb65ce6cf2b502585d20318da22b3d473a20e";
36
+ const LND_SOCKET = "regtest.lnfi.network:10019";
37
+ const LND_TLS_CERT =
38
+ "/Users/jim/Documents/gamichi/2024/astra/packages/server/volumes/tls.cert";
39
+ const base64Cert = fs.readFileSync(LND_TLS_CERT).toString("base64");
37
40
  "";
41
+ const assetId ="f7ac99f2c068f1157c787012f50cb043437505c309c6d8685e135cd8481b1e9d";
42
+
38
43
 
39
44
  const { lnd } = authenticatedLndGrpc({
40
45
  cert: base64Cert,
@@ -47,6 +52,37 @@ const { lnd: tpr } = authenticatedLndGrpc({
47
52
  socket: LND_SOCKET,
48
53
  });
49
54
 
55
+ const getPriceOrcaleMetadata = async ({
56
+ assetId,
57
+ transaction_type = "0",
58
+ amount = "1000",
59
+ request_id,
60
+ }) => {
61
+ if (!request_id) {
62
+ throw new Error("request_id is required to build price_oracle_metadata");
63
+ }
64
+
65
+ const price_oracle_metadata = {
66
+ type: "bridge",
67
+ asset_id: assetId,
68
+ transaction_type,
69
+ amount,
70
+ request_id,
71
+ };
72
+
73
+ const messageToSign = Object.values(price_oracle_metadata).join("");
74
+ const { signature } = await signMessage({ lnd, message: messageToSign });
75
+ const public_key =
76
+ "02607b201503d39fe886cc13637f4dcfa45ea6a83d6b1f5ab8ce780e1157af1718";
77
+
78
+ return {
79
+ ...price_oracle_metadata,
80
+ public_key,
81
+ sign: signature,
82
+ raw_data: JSON.stringify(price_oracle_metadata)
83
+ };
84
+ };
85
+
50
86
  const payWithTpr = async (invoice, assetId) => {
51
87
  // const provider = new ethers.JsonRpcProvider('https://eth-sepolia.g.alchemy.com/v2/ClvXGqSMvZ6nEb_G5ekJGsZwNdsp72oz');
52
88
  // const lspSigner = new ethers.Wallet('dd5862c69e641fb052092d34724ce62764186abf87474656f2b2149684dca754', provider);
@@ -59,7 +95,7 @@ const payWithTpr = async (invoice, assetId) => {
59
95
  // request: invoice
60
96
  // })
61
97
 
62
- const ret = await getChannels({ lnd }).catch((error) => console.log(error));
98
+ /* const ret = await getChannels({ lnd }).catch((error) => console.log(error));
63
99
 
64
100
  const filterChannels = ret.channels.filter(
65
101
  (channel) =>
@@ -104,23 +140,34 @@ const payWithTpr = async (invoice, assetId) => {
104
140
  throw new Error(
105
141
  `channelId: ${maxLocalBalanceChannel.channel_id} not enough balance`
106
142
  );
107
- }
108
- const peerPubkey = maxLocalBalanceChannel.partner_public_key;
143
+ } */
144
+ const peerPubkey = "03b24a4bf911ffd26ac1d5e5f2440a3c2f6974e4cc85d2ef54e17ee6d3717433d3";
145
+
146
+
147
+ const decodedReq = await decodeAssetPayReq({ lnd, request: invoice }).then(res => res.destination);
148
+ const priceOracleMetadata = await getPriceOrcaleMetadata({
149
+ assetId,
150
+ transaction_type: "1",
151
+ amount: 0,
152
+ request_id: decodedReq.pay_req.payment_hash,
153
+ });
109
154
  const retPay = await payTaprootInvoice({
110
155
  lnd: lnd,
111
156
  tpr: tpr,
112
157
  asset_id: assetId,
113
- asset_amount: 10,
158
+ asset_amount: decodedReq.asset_amount,
114
159
  peer_pubkey: peerPubkey,
115
160
  payment_request: {
116
161
  payment_request: invoice,
117
162
  fee_limit_sat: 1000,
118
163
  allow_self_payment: true,
119
164
  timeout_seconds: 60 * 5,
120
- outgoing_chan_id: maxLocalBalanceChannel.channel_id,
165
+ outgoing_chan_id: "118422899869679616",
121
166
  },
167
+ price_oracle_metadata: priceOracleMetadata.raw_data,
122
168
  }).catch((e) => {
123
169
  console.log("error", e.message);
170
+ throw e;
124
171
  });
125
172
  console.log("ret", retPay);
126
173
 
@@ -177,14 +224,45 @@ const payWithTpr = async (invoice, assetId) => {
177
224
  // }
178
225
  // queryBalance();
179
226
 
227
+
228
+
180
229
  const addTapdInvoice = async () => {
230
+ const secret = crypto.randomBytes(32);
231
+ const preimage = secret.toString("hex");
232
+ const hash = createHash("sha256").update(secret).digest("hex");
233
+
234
+ /*
235
+ "sign": "lncli.signmessage(type + asset_id+ transaction_type+ amount + request_id )"
236
+ {
237
+ "type": "bridge",
238
+ "asset_id": "9fe1643dd1ae81208fde67d1bce2b3a3a6cd158a868d3c6774eb075b4f032004",
239
+ "transaction_type": "0", //0买 1卖
240
+ "amount": "4100",
241
+ "request_id": "hash(preimage)",
242
+ "public_key": "0261f6cc4cdc3b59c9910badebd6d0c34f7f9357320a349af906ecf750dda0bcc4",
243
+ "sign": "lncli.signmessage(type + asset_id+ transaction_type+ amount + request_id )"
244
+ }
245
+ */
246
+ const price_oracle_metadata = await getPriceOrcaleMetadata({
247
+ assetId,
248
+ transaction_type: "0",
249
+ amount: "1000",
250
+ request_id: hash,
251
+ });
252
+
253
+ console.log("price_oracle_metadata", price_oracle_metadata);
254
+
255
+ //return;
181
256
  const ret = await createInvoice({
182
257
  lnd,
183
258
  tpr,
184
- mtokens: 1000,
185
- expires_at:'1764839000',
259
+ mtokens: 1000000,
260
+ // expires_at:'1764839000',
261
+ peer_pubkey:"03b24a4bf911ffd26ac1d5e5f2440a3c2f6974e4cc85d2ef54e17ee6d3717433d3",
186
262
  asset_id: assetId,
187
- secret:'dfdd9d34ddf25728e84f6d2a32ed37a88e2b516e3488d051c70a62686d889481'
263
+ price_oracle_metadata: JSON.stringify(price_oracle_metadata),
264
+ secret: preimage,
265
+
188
266
  }).catch((e) => {
189
267
  console.error("Failed to create invoice:", e);
190
268
  });
@@ -192,7 +270,7 @@ const addTapdInvoice = async () => {
192
270
  };
193
271
 
194
272
  const decodeTapdInvoice = async()=>{
195
- const invoice ='lnbcrt1200u1p5ja2pgpp58ahjcpwgt9zcvda6cchnrl5wyr4r9vjg487qrxvlh7vd88rxnhssdqqcqzzsxqr23srzjqw7ng5fg7qnyr49nm9qtt6nvnwxfefpygrctxdmfx9uf8l0awcaura2jdhvguyylxuqqqqlgqqqqqqgq2qsp5g54dp5lug8dqne4pglj0j0tglv3rtawk76rlqmqcppavfy8dzr4q9qxpqysgqt9jde9vcwpc27ayzpv7q3j63nej52xfjeuhew65y22zvd0vjans9hz206sxf67n8zz6mr803d7skpcz762vqaj6mrf0uwlxtdtv6u9qpahal9g'
273
+ const invoice ='lnbcrt114742690p1p55hxxjpp584aulkj02vhaqujfv0lv5dtyuw4vrnk67uvx77q4y20md22sxz3sdqqcqzzsxqr23srzjqf7j7xl8rhpycc932pcy382w7f6d664vydks93nuwmtfxh00hft2da0glksvd4c3acqqqqlgqqqqqqgq2qsp5duc6ezxqyllkxqvevmr34d852nplfknk3jc2dvwktv2gh3pdrxus9qxpqysgqc67uh6vl2epzj32s0qf86v7hpygs3tps3ejy9uca78p2w0dgfjnzshlxpfmwws6xwecw0c7z47jvuglywhqg4tq260h7tv30sxkvxqgqpeufpe'
196
274
  const ret = await decodeAssetPayReq({
197
275
  asset_id: assetId,
198
276
  pay_req_string: invoice,
@@ -214,7 +292,7 @@ const getBestLndChannel = async (asset) => {
214
292
  return {channels: []};
215
293
  });
216
294
 
217
- console.log("🚀 ~ getBestLndChannel ~ ret:", ret);
295
+
218
296
 
219
297
  let filterChannels = [];
220
298
  let sortTprChannels = [];
@@ -261,6 +339,8 @@ const getBestLndChannel = async (asset) => {
261
339
  return hasLocal || hasFunding || hasRemote;
262
340
  });
263
341
 
342
+ console.log('filterChannels',filterChannels)
343
+
264
344
 
265
345
  sortTprChannels = filterChannels.sort((a, b) => {
266
346
  let aData;
@@ -341,12 +421,14 @@ const getBestLndChannel = async (asset) => {
341
421
  };
342
422
 
343
423
  const test = async () => {
344
-
424
+ const ret = await getIdentity({ lnd });
425
+ console.log("🚀 ~ test ~ ret:", ret)
426
+ //getBestLndChannel({assetId, isBTC: false})
345
427
  /* const sub = subscribeToInvoices({ lnd });
346
428
  sub.on("invoice_updated",async(invoice)=>{
347
429
  console.log('invoice',invoice)
348
430
  }) */
349
- await addTapdInvoice();
431
+ //await addTapdInvoice();
350
432
  //await decodeTapdInvoice();
351
433
  //await listChannels();
352
434
  // const ret = await getBestLndChannel({