Skip to content

Commit 631c811

Browse files
authored
typescript (#29)
* typescript * code rabbit fixes * fixes part dos * code rabbit part three
1 parent 10438b6 commit 631c811

33 files changed

+5968
-18
lines changed

AGENTS.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# AGENTS.md
2+
3+
This file defines stable, repository-level guidance for future coding agents.
4+
5+
## Primary Goal
6+
- Maintain and extend LNI bindings with consistent API behavior across languages.
7+
- Current priority for TypeScript handoff: implement Spark support in `bindings/typescript`.
8+
9+
## Spark Implementation Guidance (TypeScript)
10+
- Treat Rust Spark implementation as source of truth:
11+
- `crates/lni/spark/api.rs`
12+
- `crates/lni/spark/lib.rs`
13+
- `crates/lni/spark/types.rs`
14+
- Match existing TypeScript node conventions:
15+
- Implement class in `bindings/typescript/src/nodes/`.
16+
- Use shared types from `bindings/typescript/src/types.ts`.
17+
- Reuse helpers from `bindings/typescript/src/internal/*`.
18+
- Export from `bindings/typescript/src/index.ts`.
19+
20+
## Test/Validation Expectations
21+
- Prefer real integration-style tests over mocks for node adapters.
22+
- Add/maintain node-specific integration tests under:
23+
- `bindings/typescript/src/__tests__/integration/`
24+
- Ensure `npm run typecheck` passes.
25+
- Ensure `npm run pack:dry-run` succeeds and does not include tests/secrets.
26+
27+
## Security Expectations
28+
- Never commit credentials or local machine details.
29+
- Do not print sensitive runtime values in tests/logging:
30+
- API keys, macaroons, runes, passwords, NWC URIs, full invoices/preimages.
31+
- Keep examples sanitized.
32+
33+
## Handoff Context
34+
- High-level sanitized thread context is in:
35+
- `docs/THREAD_CONTEXT.md`

bindings/typescript/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules/
2+
dist/

bindings/typescript/README.md

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
# Lightning Node Interface
2+
3+
Remote connect to major Lightning node implementations with one TypeScript interface.
4+
5+
- Supports major nodes: CLN, LND, Phoenixd
6+
- Supports protocols: BOLT11, BOLT12, NWC
7+
- Includes custodial APIs: Strike, Speed, Blink
8+
- LNURL + Lightning Address support (`user@domain.com`, `lnurl1...`)
9+
- Frontend-capable TypeScript runtime (`fetch`-based)
10+
11+
## Install
12+
13+
```bash
14+
npm install @sunnyln/lni
15+
```
16+
17+
## TypeScript Examples
18+
19+
### Node API
20+
21+
```ts
22+
import {
23+
createNode,
24+
InvoiceType,
25+
type BackendNodeConfig,
26+
} from '@sunnyln/lni';
27+
28+
const backend: BackendNodeConfig = {
29+
kind: 'lnd',
30+
config: {
31+
url: 'https://lnd.example.com',
32+
macaroon: '...',
33+
},
34+
};
35+
36+
const node = createNode(backend);
37+
38+
const info = await node.getInfo();
39+
40+
const invoiceParams = {
41+
invoiceType: InvoiceType.Bolt11,
42+
amountMsats: 2000,
43+
description: 'your memo',
44+
expiry: 3600,
45+
};
46+
47+
const invoice = await node.createInvoice(invoiceParams);
48+
49+
const payInvoiceParams = {
50+
invoice: invoice.invoice,
51+
feeLimitPercentage: 1,
52+
allowSelfPayment: true,
53+
};
54+
55+
const payment = await node.payInvoice(payInvoiceParams);
56+
57+
const status = await node.lookupInvoice({ paymentHash: invoice.paymentHash });
58+
59+
const txs = await node.listTransactions({ from: 0, limit: 10 });
60+
```
61+
62+
For NWC specifically, `createNode` returns `NwcNode` when `kind: 'nwc'`, so you can close it:
63+
64+
```ts
65+
const nwcNode = createNode({ kind: 'nwc', config: { nwcUri: 'nostr+walletconnect://...' } });
66+
// ... use node
67+
nwcNode.close();
68+
```
69+
70+
### LNURL + Lightning Address
71+
72+
```ts
73+
import { detectPaymentType, needsResolution, getPaymentInfo, resolveToBolt11 } from '@sunnyln/lni';
74+
75+
const destination = 'user@domain.com';
76+
77+
const type = detectPaymentType(destination);
78+
const requiresResolution = needsResolution(destination);
79+
const info = await getPaymentInfo(destination, 100_000);
80+
const bolt11 = await resolveToBolt11(destination, 100_000);
81+
```
82+
83+
### Invoice Event Polling
84+
85+
```ts
86+
await node.onInvoiceEvents(
87+
{
88+
paymentHash: invoice.paymentHash,
89+
pollingDelaySec: 3,
90+
maxPollingSec: 60,
91+
},
92+
(status, tx) => {
93+
console.log('Invoice event:', status, tx);
94+
},
95+
);
96+
```
97+
98+
## Implemented in this package
99+
100+
- `PhoenixdNode`
101+
- `ClnNode`
102+
- `LndNode`
103+
- `NwcNode`
104+
- `StrikeNode`
105+
- `SpeedNode`
106+
- `BlinkNode`
107+
- LNURL helpers (`detectPaymentType`, `needsResolution`, `resolveToBolt11`, `getPaymentInfo`)
108+
109+
Not included yet:
110+
- `SparkNode` (planned)
111+
112+
## Frontend Runtime Notes
113+
114+
- Uses `fetch`, no Node-native runtime dependency required.
115+
- You can inject custom fetch via constructor options:
116+
- `new LndNode(config, { fetch: customFetch })`
117+
- Most backends require secrets (API keys, macaroons, runes, passwords). For production web apps, use a backend proxy/BFF to protect credentials.
118+
119+
## Build and Publish (package maintainers)
120+
121+
```bash
122+
npm run prepack
123+
npm run pack:dry-run
124+
npm run publish:public
125+
```
126+
127+
## Integration tests
128+
129+
```bash
130+
npm run test:integration
131+
```
132+
133+
These scripts set `NODE_TLS_REJECT_UNAUTHORIZED=0` because many local Lightning nodes use self-signed certs in test environments. Do not use this in production.

0 commit comments

Comments
 (0)