lightning-agent 0.3.0 → 0.3.1

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.
Files changed (2) hide show
  1. package/lib/auth.js +40 -11
  2. package/package.json +16 -2
package/lib/auth.js CHANGED
@@ -156,6 +156,10 @@ function createAuthServer(opts = {}) {
156
156
  /**
157
157
  * Sign an LNURL-auth challenge with a private key.
158
158
  *
159
+ * Uses the native secp256k1 package (libsecp256k1 bindings) for signing,
160
+ * which produces DER signatures accepted by all LNURL-auth implementations.
161
+ * Falls back to @noble/curves if secp256k1 is not available.
162
+ *
159
163
  * @param {string} k1 - Challenge hex (32 bytes)
160
164
  * @param {string|Uint8Array} privateKey - Signing private key (hex or bytes)
161
165
  * @returns {{ sig: string, key: string }}
@@ -165,14 +169,27 @@ function createAuthServer(opts = {}) {
165
169
  * // Send sig + key back to the auth server
166
170
  */
167
171
  function signAuth(k1, privateKey) {
168
- const secp = getSecp256k1();
169
- const privBytes = typeof privateKey === 'string'
170
- ? Uint8Array.from(Buffer.from(privateKey, 'hex'))
171
- : privateKey;
172
- const msgBytes = Uint8Array.from(Buffer.from(k1, 'hex'));
172
+ const privBuf = typeof privateKey === 'string'
173
+ ? Buffer.from(privateKey, 'hex')
174
+ : Buffer.from(privateKey);
175
+ const msgBuf = Buffer.from(k1, 'hex');
176
+
177
+ // Prefer native secp256k1 (libsecp256k1 bindings) — produces DER signatures
178
+ // accepted by all LNURL-auth server implementations
179
+ try {
180
+ const secp = require('secp256k1');
181
+ const { signature } = secp.ecdsaSign(msgBuf, privBuf);
182
+ const derSig = Buffer.from(secp.signatureExport(signature)).toString('hex');
183
+ const pubkey = Buffer.from(secp.publicKeyCreate(privBuf, true)).toString('hex');
184
+ return { sig: derSig, key: pubkey };
185
+ } catch (e) {
186
+ if (e.code !== 'MODULE_NOT_FOUND') throw e;
187
+ }
173
188
 
174
- // secp256k1.sign returns 64-byte compact sig (r||s) in @noble/curves v2
175
- const compactSig = secp.sign(msgBytes, privBytes);
189
+ // Fallback: @noble/curves with manual DER encoding
190
+ const secp = getSecp256k1();
191
+ const privBytes = Uint8Array.from(privBuf);
192
+ const compactSig = secp.sign(msgBuf, privBytes);
176
193
  const sigDer = compactToDER(compactSig);
177
194
  const pubkey = Buffer.from(secp.getPublicKey(privBytes, true)).toString('hex');
178
195
 
@@ -231,13 +248,25 @@ function getSecp256k1() {
231
248
  throw new Error('secp256k1 not available — install @noble/curves or nostr-tools');
232
249
  }
233
250
 
234
- // Verify a secp256k1 DER signature over a message hash
251
+ // Verify a secp256k1 DER signature over a message
235
252
  function verifySignature(k1Hex, sigHex, pubkeyHex) {
253
+ const msg = Buffer.from(k1Hex, 'hex');
254
+ const sigBuf = Buffer.from(sigHex, 'hex');
255
+ const pub = Buffer.from(pubkeyHex, 'hex');
256
+
257
+ // Prefer native secp256k1
258
+ try {
259
+ const secp = require('secp256k1');
260
+ const compact = secp.signatureImport(sigBuf);
261
+ return secp.ecdsaVerify(compact, msg, pub);
262
+ } catch (e) {
263
+ if (e.code !== 'MODULE_NOT_FOUND') throw e;
264
+ }
265
+
266
+ // Fallback: @noble/curves
236
267
  const secp = getSecp256k1();
237
- const msg = Uint8Array.from(Buffer.from(k1Hex, 'hex'));
238
268
  const compactSig = derToCompact(sigHex);
239
- const pub = Uint8Array.from(Buffer.from(pubkeyHex, 'hex'));
240
- return secp.verify(compactSig, msg, pub);
269
+ return secp.verify(compactSig, Uint8Array.from(msg), Uint8Array.from(pub));
241
270
  }
242
271
 
243
272
  // Convert 64-byte compact signature (r||s) to DER hex
package/package.json CHANGED
@@ -1,16 +1,30 @@
1
1
  {
2
2
  "name": "lightning-agent",
3
- "version": "0.3.0",
3
+ "version": "0.3.1",
4
4
  "description": "Lightning toolkit for AI agents. Payments, auth, escrow, and streaming micropayments.",
5
5
  "main": "lib/index.js",
6
6
  "bin": {
7
7
  "lightning-agent": "bin/lightning-agent.js"
8
8
  },
9
- "keywords": ["lightning", "bitcoin", "ai", "agent", "nostr", "nwc", "payments", "escrow", "streaming", "lnurl", "auth", "micropayments"],
9
+ "keywords": [
10
+ "lightning",
11
+ "bitcoin",
12
+ "ai",
13
+ "agent",
14
+ "nostr",
15
+ "nwc",
16
+ "payments",
17
+ "escrow",
18
+ "streaming",
19
+ "lnurl",
20
+ "auth",
21
+ "micropayments"
22
+ ],
10
23
  "author": "Jeletor",
11
24
  "license": "MIT",
12
25
  "dependencies": {
13
26
  "nostr-tools": "^2.0.0",
27
+ "secp256k1": "^5.0.1",
14
28
  "ws": "^8.0.0"
15
29
  }
16
30
  }