-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.js
More file actions
432 lines (382 loc) · 15 KB
/
Copy pathindex.js
File metadata and controls
432 lines (382 loc) · 15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
'use strict'
// @utexo/rgb-lightning-node-nodejs — Node façade for the rgb-lightning-node C-FFI.
//
// 1. Platform-detect + load the matching .node prebuild.
// 2. Wrap the raw napi classes with the JSON-stringify/parse marshalling
// layer so the surface matches @utexo/rgb-lightning-node-bare's index.js
// 1:1 — consumers can pass plain JS objects and get plain JS objects
// back. This is what lets @utexo/wdk-rgb-lightning's `bare-binding.js`
// and `node-binding.js` be structurally identical.
const os = require('os')
const path = require('path')
const fs = require('fs')
// ─── 1. Native addon resolution ──────────────────────────────────────────
const platform = os.platform()
const arch = os.arch()
function resolvePlatformSuffix () {
if (platform === 'darwin' && arch === 'arm64') return 'darwin-arm64'
if (platform === 'darwin' && arch === 'x64') return 'darwin-x64'
if (platform === 'linux' && arch === 'x64') {
if (fs.existsSync('/etc/alpine-release')) return 'linux-x64-musl'
return 'linux-x64-gnu'
}
if (platform === 'linux' && arch === 'arm64') return 'linux-arm64-gnu'
return null
}
const suffix = resolvePlatformSuffix()
if (!suffix) {
throw new Error(
`[@utexo/rgb-lightning-node-nodejs] Unsupported platform: ${platform}-${arch}. ` +
'Supported: darwin-arm64, darwin-x64, linux-x64-gnu, linux-x64-musl, linux-arm64-gnu.'
)
}
const addonPath = path.join(__dirname, `index-${suffix}.node`)
if (!fs.existsSync(addonPath)) {
throw new Error(
`[@utexo/rgb-lightning-node-nodejs] Native addon not found at ${addonPath}. ` +
'If postinstall was skipped (npm install --ignore-scripts), run ' +
'`bash scripts/download-libs.sh` manually or rebuild via `npm run build`.'
)
}
const napi = require(addonPath)
// ─── 2. SdkNode wrapper ─────────────────────────────────────────────────
class SdkNode {
constructor (inner) {
this._inner = inner
this._closed = false
}
static create (request) {
return new SdkNode(napi.SdkNode.create(JSON.stringify(request)))
}
// External-signer lifecycle (matches bare addon)
initWithNativeExternalSigner (signer) {
this._inner.initWithNativeExternalSigner(signer._inner)
}
attachNativeExternalSigner (signer) {
this._inner.attachNativeExternalSigner(signer._inner)
}
unlockWithNativeExternalSigner (signer, request) {
this._inner.unlockWithNativeExternalSigner(signer._inner, JSON.stringify(request))
}
startUnlockWithNativeExternalSigner (signer, request) {
return JSON.parse(
this._inner.startUnlockWithNativeExternalSigner(signer._inner, JSON.stringify(request))
)
}
nativeOperationStatus (operationId) {
return JSON.parse(this._inner.nativeOperationStatus(operationId))
}
adoptNativeOperation (operationId) {
return JSON.parse(this._inner.adoptNativeOperation(operationId))
}
cancelNativeOperation (operationId) {
return JSON.parse(this._inner.cancelNativeOperation(operationId))
}
initWithExternalSigner (bootstrap) {
this._inner.initWithExternalSigner(JSON.stringify(bootstrap))
}
detachExternalSigner () { this._inner.detachExternalSigner() }
unlockWithAttachedExternalSigner (request) {
this._inner.unlockWithAttachedExternalSigner(JSON.stringify(request))
}
shutdown () {
if (this._closed) return
try {
this._inner.shutdown()
} finally {
this._inner = null
this._closed = true
}
}
// Forces takeover of a stale VSS ownership fence after a previous node
// died holding it. Throws if VSS isn't configured. Pointing two live
// nodes at the same VSS store corrupts state — call only when certain
// the previous owner is gone.
vssClearFence (request) { this._inner.vssClearFence(JSON.stringify(request)) }
// Force an immediate VSS backup flush. Returns `{ version }` where
// version is the snapshot index just persisted. Throws if VSS isn't
// configured / the flush fails. Backed by upstream vss_backup() PR.
vssBackup () { return JSON.parse(this._inner.vssBackup()) }
vssDeleteAll (request) {
return JSON.parse(this._inner.vssDeleteAll(JSON.stringify(request)))
}
// APay receiver-side: register this node with an LSP as an async-order
// recipient. Pass the LSP's node_id (hex). Returns the parsed
// AsyncOrderNewResponse (request_id, host_node_id, protocol_version,
// order_id, status, accepted_through_index, next_index_expected,
// unused_hashes, refill_batch_size, first_hash_index).
apayNew (hostNodeId) { return JSON.parse(this._inner.apayNew(hostNodeId)) }
// Register the APay hash batch and bind a signed username@domain
// attestation to the wallet node identity.
apayNewWithAddress (hostNodeId, username, domain) {
return JSON.parse(this._inner.apayNewWithAddress(hostNodeId, username, domain))
}
// Info / network / sync
nodeInfo () { return JSON.parse(this._inner.nodeInfo()) }
networkInfo () { return JSON.parse(this._inner.networkInfo()) }
sync () { return JSON.parse(this._inner.sync()) }
syncWallet (request) {
return JSON.parse(this._inner.syncWallet(JSON.stringify(request)))
}
walletSnapshot (request = {}) {
return JSON.parse(this._inner.walletSnapshot(JSON.stringify(request)))
}
rotateAddress () { return JSON.parse(this._inner.rotateAddress()) }
// Peers / channels
// C-FFI's `rln_connect_peer` takes the raw pubkey@addr string (not a
// JSON envelope) — matches @utexo/rgb-lightning-node-bare/index.js.
connectPeer (peerPubkeyAndAddr) {
return JSON.parse(this._inner.connectPeer(peerPubkeyAndAddr))
}
disconnectPeer (request) {
return JSON.parse(this._inner.disconnectPeer(JSON.stringify(request)))
}
listPeers () { return JSON.parse(this._inner.listPeers()) }
openChannel (request) {
return JSON.parse(this._inner.openChannel(JSON.stringify(request)))
}
closeChannel (request) {
return JSON.parse(this._inner.closeChannel(JSON.stringify(request)))
}
listChannels () { return JSON.parse(this._inner.listChannels()) }
getChannelId (temporaryChannelIdHex) {
return JSON.parse(this._inner.getChannelId(temporaryChannelIdHex))
}
// BTC + UTXOs
getAddress () { return JSON.parse(this._inner.getAddress()) }
// Alias to match bare addon's `address()` method name; both work.
address () { return JSON.parse(this._inner.getAddress()) }
btcBalance (skipSync = false) {
return JSON.parse(this._inner.getBtcBalance(!!skipSync))
}
listUnspents (skipSync = false) {
return JSON.parse(this._inner.listUnspents(!!skipSync))
}
listTransactions (skipSync = false) {
return JSON.parse(this._inner.listTransactions(!!skipSync))
}
listTransactionsByTxid (txid, skipSync = false) {
return JSON.parse(this._inner.listTransactionsByTxid(txid, !!skipSync))
}
sendBtc (request) {
return JSON.parse(this._inner.sendBtc(JSON.stringify(request)))
}
prepareBtcSend (request) {
return JSON.parse(this._inner.prepareBtcSend(JSON.stringify(request)))
}
commitPreparedBtcSend (request) {
return JSON.parse(this._inner.commitPreparedBtcSend(JSON.stringify(request)))
}
cancelBtcSendPlan (request) {
return JSON.parse(this._inner.cancelBtcSendPlan(JSON.stringify(request)))
}
prepareCreateUtxos (request) {
return JSON.parse(this._inner.prepareCreateUtxos(JSON.stringify(request)))
}
commitPreparedCreateUtxos (request) {
return JSON.parse(this._inner.commitPreparedCreateUtxos(JSON.stringify(request)))
}
cancelCreateUtxosPlan (request) {
return JSON.parse(this._inner.cancelCreateUtxosPlan(JSON.stringify(request)))
}
listPendingVanillaTransactions () {
return JSON.parse(this._inner.listPendingVanillaTransactions())
}
listAddressReceipts (address) {
return JSON.parse(this._inner.listAddressReceipts(address))
}
createUtxos (request) {
return JSON.parse(this._inner.createUtxos(JSON.stringify(request)))
}
// blocks: 1..=65535 — sat/vB fee rate target
estimateFee (blocks) {
return JSON.parse(this._inner.estimateFee(blocks >>> 0))
}
// Lightning invoices / payments
lnInvoice (request) {
return JSON.parse(this._inner.lnInvoice(JSON.stringify(request)))
}
decodeLnInvoice (invoice) {
// C-FFI expects the raw BOLT11 string (matches bare addon).
return JSON.parse(this._inner.decodeLnInvoice(invoice))
}
invoiceStatus (invoice) {
return JSON.parse(this._inner.invoiceStatus(invoice))
}
cancelHodlInvoice (request) {
return JSON.parse(this._inner.cancelHodlInvoice(JSON.stringify(request)))
}
claimHodlInvoice (request) {
return JSON.parse(this._inner.claimHodlInvoice(JSON.stringify(request)))
}
sendPayment (request) {
return JSON.parse(this._inner.sendPayment(JSON.stringify(request)))
}
keysend (request) {
return JSON.parse(this._inner.keysend(JSON.stringify(request)))
}
listPayments () { return JSON.parse(this._inner.listPayments()) }
getPayment (paymentHashHex, paymentType) {
return JSON.parse(this._inner.getPayment(paymentHashHex, paymentType))
}
// Atomic swaps (parity with bare addon; WDK does not surface these)
makerInit (request) {
return JSON.parse(this._inner.makerInit(JSON.stringify(request)))
}
makerExecute (request) {
return JSON.parse(this._inner.makerExecute(JSON.stringify(request)))
}
taker (request) {
return JSON.parse(this._inner.taker(JSON.stringify(request)))
}
listSwaps () { return JSON.parse(this._inner.listSwaps()) }
getSwap (paymentHash, takerFlag) {
return JSON.parse(this._inner.getSwap(paymentHash, !!takerFlag))
}
// RGB assets — issuance
issueAssetNia (request) {
return JSON.parse(this._inner.issueAssetNia(JSON.stringify(request)))
}
issueAssetUda (request) {
return JSON.parse(this._inner.issueAssetUda(JSON.stringify(request)))
}
issueAssetCfa (request) {
return JSON.parse(this._inner.issueAssetCfa(JSON.stringify(request)))
}
issueAssetIfa (request) {
return JSON.parse(this._inner.issueAssetIfa(JSON.stringify(request)))
}
// RGB assets — listing / metadata / balance
listAssets (filterAssetSchemas) {
return JSON.parse(this._inner.listAssets(JSON.stringify(filterAssetSchemas ?? [])))
}
assetBalance (assetId) {
return JSON.parse(this._inner.getAssetBalance(assetId))
}
assetLinkCreate (request) {
return JSON.parse(this._inner.assetLinkCreate(JSON.stringify(request)))
}
assetMetadata (assetId) {
return JSON.parse(this._inner.assetMetadata(assetId))
}
// RGB invoices / transfers
rgbInvoice (request) {
return JSON.parse(this._inner.rgbInvoice(JSON.stringify(request)))
}
decodeRgbInvoice (invoice) {
return JSON.parse(this._inner.decodeRgbInvoice(invoice))
}
sendRgb (request) {
return JSON.parse(this._inner.sendRgb(JSON.stringify(request)))
}
importRgbTransferConsignment (request) {
return JSON.parse(this._inner.importRgbTransferConsignment(JSON.stringify(request)))
}
importRgbContract (request) {
return JSON.parse(this._inner.importRgbContract(JSON.stringify(request)))
}
prepareRgbSend (request) {
return JSON.parse(this._inner.prepareRgbSend(JSON.stringify(request)))
}
commitPreparedRgbSend (request) {
return JSON.parse(this._inner.commitPreparedRgbSend(JSON.stringify(request)))
}
cancelRgbSendPlan (request) {
return JSON.parse(this._inner.cancelRgbSendPlan(JSON.stringify(request)))
}
listPendingRgbSendPlans () {
return JSON.parse(this._inner.listPendingRgbSendPlans())
}
refreshTransfers (request) {
this._inner.refreshTransfers(JSON.stringify(request))
return { ok: true }
}
failTransfers (request) {
return JSON.parse(this._inner.failTransfers(JSON.stringify(request)))
}
inflate (request) {
return JSON.parse(this._inner.inflate(JSON.stringify(request)))
}
listTransfers (assetId) {
return JSON.parse(this._inner.listTransfers(assetId))
}
listTransfersByTxid (txid) {
return JSON.parse(this._inner.listTransfersByTxid(txid))
}
// RGB asset media
getAssetMedia (digest) {
return JSON.parse(this._inner.getAssetMedia(digest))
}
postAssetMedia (request) {
return JSON.parse(this._inner.postAssetMedia(JSON.stringify(request)))
}
// Signing / onion / diagnostics
signMessage (message) {
return JSON.parse(this._inner.signMessage(message))
}
verifyMessage (message, signature) {
return JSON.parse(this._inner.verifyMessage(message, signature))
}
sendOnionMessage (request) {
return JSON.parse(this._inner.sendOnionMessage(JSON.stringify(request)))
}
checkIndexerUrl (indexerUrl) {
return JSON.parse(this._inner.checkIndexerUrl(indexerUrl))
}
checkProxyEndpoint (proxyEndpoint) {
return JSON.parse(this._inner.checkProxyEndpoint(proxyEndpoint))
}
}
// ─── 3. NativeExternalSigner wrapper ────────────────────────────────────
class NativeExternalSigner {
constructor (inner) {
this._inner = inner
this._destroyed = false
}
static create (seedHex, network, permissivePolicy = true) {
if (typeof seedHex !== 'string' || seedHex.length !== 64) {
throw new Error('NativeExternalSigner.create: seedHex must be a 64-char hex string')
}
return new NativeExternalSigner(
napi.NativeExternalSigner.create(seedHex, network, !!permissivePolicy)
)
}
static createWithStorage (seedHex, network, storageDirPath, permissivePolicy = false) {
if (typeof seedHex !== 'string' || seedHex.length !== 64) {
throw new Error('NativeExternalSigner.createWithStorage: seedHex must be a 64-char hex string')
}
if (typeof storageDirPath !== 'string' || storageDirPath.length === 0) {
throw new Error('NativeExternalSigner.createWithStorage: storageDirPath is required')
}
return new NativeExternalSigner(
napi.NativeExternalSigner.createWithStorage(
seedHex,
network,
storageDirPath,
!!permissivePolicy
)
)
}
bootstrap () {
if (this._destroyed) throw new Error('NativeExternalSigner already destroyed')
return JSON.parse(this._inner.bootstrap())
}
destroy () {
if (this._destroyed) return
try { this._inner.destroy() } catch { /* ignore */ }
this._destroyed = true
}
}
// ─── 4. Module-level helpers (parity with bare addon, no-ops for now) ───
// The bare addon exposes uniffiHealthcheck / uniffiIsInitialized /
// sdkInitialize / sdkShutdown. The napi binding doesn't surface these
// yet — wdk-rgb-lightning's BareRgbLightningBinding / NodeRgbLightningBinding
// only call them as static helpers, so we stub them with sensible
// fallbacks. Wire real implementations when we surface the module-level
// uniffi entry points through napi.
exports.SdkNode = SdkNode
exports.NativeExternalSigner = NativeExternalSigner
exports.uniffiHealthcheck = () => 'unsupported-in-node-binding'
exports.uniffiIsInitialized = () => true
exports.sdkInitialize = () => undefined
exports.sdkShutdown = () => undefined