This commit is contained in:
hatim boufnichel 2024-04-24 16:17:59 +02:00
parent 30bc1f22f9
commit 10458bc5b5
5 changed files with 66 additions and 38 deletions

View file

@ -27,7 +27,7 @@ export interface LightningHandler {
PayInvoice(invoice: string, amount: number, feeLimit: number): Promise<PaidInvoice> PayInvoice(invoice: string, amount: number, feeLimit: number): Promise<PaidInvoice>
EstimateChainFees(address: string, amount: number, targetConf: number): Promise<EstimateFeeResponse> EstimateChainFees(address: string, amount: number, targetConf: number): Promise<EstimateFeeResponse>
PayAddress(address: string, amount: number, satPerVByte: number, label?: string): Promise<SendCoinsResponse> PayAddress(address: string, amount: number, satPerVByte: number, label?: string): Promise<SendCoinsResponse>
OpenChannel(destination: string, closeAddress: string, fundingAmount: number, pushSats: number): Promise<string> //OpenChannel(destination: string, closeAddress: string, fundingAmount: number, pushSats: number): Promise<string>
SetMockInvoiceAsPaid(invoice: string, amount: number): Promise<void> SetMockInvoiceAsPaid(invoice: string, amount: number): Promise<void>
ChannelBalance(): Promise<{ local: number, remote: number }> ChannelBalance(): Promise<{ local: number, remote: number }>
GetTransactions(startHeight: number): Promise<TransactionDetails> GetTransactions(startHeight: number): Promise<TransactionDetails>

View file

@ -346,27 +346,25 @@ export default class {
return res.response return res.response
} }
async OpenChannel(destination: string, closeAddress: string, fundingAmount: number, pushSats: number): Promise<string> { async ConnectPeer(pubkey: string, host: string) {
const res = await this.lightning.connectPeer({
addr: { pubkey, host },
perm: true,
timeout: 0n
}, DeadLineMetadata())
return res.response
}
async OpenChannel(destination: string, closeAddress: string, fundingAmount: number, pushSats: number) {
await this.Health() await this.Health()
const abortController = new AbortController() const abortController = new AbortController()
const req = OpenChannelReq(destination, closeAddress, fundingAmount, pushSats) const req = OpenChannelReq(destination, closeAddress, fundingAmount, pushSats)
const stream = this.lightning.openChannel(req, { abort: abortController.signal }) const stream = this.lightning.openChannel(req, { abort: abortController.signal })
return new Promise((res, rej) => { stream.responses.onMessage(message => {
stream.responses.onMessage(message => { console.log("message", message)
})
switch (message.update.oneofKind) { stream.responses.onError(error => {
case 'chanPending': console.log("error", error)
abortController.abort()
res(Buffer.from(message.pendingChanId).toString('base64'))
break
default:
abortController.abort()
rej("unexpected state response: " + message.update.oneofKind)
}
})
stream.responses.onError(error => {
rej(error)
})
}) })
} }
} }

View file

@ -45,7 +45,7 @@ export default class {
async GetForwardingHistory(indexOffset: number): Promise<{ fee: number, chanIdIn: string, chanIdOut: string, timestampNs: number, offset: number }[]> { throw new Error("GetForwardingHistory disabled in mock mode") } async GetForwardingHistory(indexOffset: number): Promise<{ fee: number, chanIdIn: string, chanIdOut: string, timestampNs: number, offset: number }[]> { throw new Error("GetForwardingHistory disabled in mock mode") }
async GetInfo(): Promise<NodeInfo> { async GetInfo(): Promise<NodeInfo> {
return { alias: "mock", syncedToChain: true, syncedToGraph: true, blockHeight: 1, blockHash: "" } return { alias: "mock", syncedToChain: true, syncedToGraph: true, blockHeight: 1, blockHash: "", identityPubkey: "mock", uris: [] }
} }
async Health(): Promise<void> { } async Health(): Promise<void> { }

View file

@ -41,6 +41,8 @@ export type NodeInfo = {
syncedToGraph: boolean syncedToGraph: boolean
blockHeight: number blockHeight: number
blockHash: string blockHash: string
identityPubkey: string
uris: string[]
} }
export type Invoice = { export type Invoice = {
payRequest: string payRequest: string

View file

@ -2,11 +2,24 @@
import BitcoinCore from 'bitcoin-core'; import BitcoinCore from 'bitcoin-core';
import { LoadTestSettingsFromEnv, TestSettings } from "../services/main/settings.js" import { LoadTestSettingsFromEnv, TestSettings } from "../services/main/settings.js"
import LND from '../services/lnd/lnd.js' import LND from '../services/lnd/lnd.js'
import { AddressType } from '../../proto/autogenerated/ts/types.js';
// dave <--> alice <--> carol <--> bob // dave <--> alice <--> carol <--> bob
export const setupNetwork = async () => { export const setupNetwork = async () => {
const settings = LoadTestSettingsFromEnv() const settings = LoadTestSettingsFromEnv()
const core = await initBitcoinCore(settings) const core = new Core(settings)
await core.Init()
const { alice, bob, carol, dave } = await initLndInstances(settings) const { alice, bob, carol, dave } = await initLndInstances(settings)
const aliceAddr = await alice.NewAddress(AddressType.WITNESS_PUBKEY_HASH)
const bobAddr = await bob.NewAddress(AddressType.WITNESS_PUBKEY_HASH)
const carolAddr = await carol.NewAddress(AddressType.WITNESS_PUBKEY_HASH)
const daveAddr = await dave.NewAddress(AddressType.WITNESS_PUBKEY_HASH)
await core.SendToAddress(aliceAddr.address, 10)
await core.SendToAddress(bobAddr.address, 10)
await core.SendToAddress(carolAddr.address, 10)
await core.SendToAddress(daveAddr.address, 10)
await core.Mine(6)
const alicePub = await alice.GetInfo()
console.log({ alicePub })
} }
const initLndInstances = async (settings: TestSettings) => { const initLndInstances = async (settings: TestSettings) => {
@ -26,22 +39,37 @@ const initLndInstances = async (settings: TestSettings) => {
await dave.Warmup() await dave.Warmup()
return { alice, bob, carol, dave } return { alice, bob, carol, dave }
} }
class Core {
core: BitcoinCore
addr: { address: string }
constructor(settings: TestSettings) {
this.core = new BitcoinCore({
//network: 'regtest',
host: '127.0.0.1',
port: `${settings.bitcoinCoreSettings.port}`,
username: settings.bitcoinCoreSettings.user,
password: settings.bitcoinCoreSettings.pass,
// use a long timeout due to the time it takes to mine a lot of blocks
timeout: 5 * 60 * 1000,
})
}
const initBitcoinCore = async (settings: TestSettings) => { Init = async () => {
const core = new BitcoinCore({ const wallet = await this.core.createWallet('');
//network: 'regtest', console.log({ wallet })
host: '127.0.0.1', this.addr = await this.core.getNewAddress()
port: `${settings.bitcoinCoreSettings.port}`, console.log({ addr: this.addr })
username: settings.bitcoinCoreSettings.user, await this.Mine(101)
password: settings.bitcoinCoreSettings.pass, const info = await this.core.getWalletInfo();
// use a long timeout due to the time it takes to mine a lot of blocks console.log({ info })
timeout: 5 * 60 * 1000, }
})
const wallet = await core.createWallet(''); Mine = async (blocks: number) => {
console.log({ wallet }) await this.core.generateToAddress(blocks, this.addr)
const addr = await core.getNewAddress() }
console.log({ addr })
await core.generateToAddress(101, addr) SendToAddress = async (address: string, amount: number) => {
const info = await core.getWalletInfo(); const tx = await this.core.sendToAddress(address, amount)
console.log({ info }) console.log({ tx })
} }
}