up
This commit is contained in:
parent
30bc1f22f9
commit
10458bc5b5
5 changed files with 66 additions and 38 deletions
|
|
@ -27,7 +27,7 @@ export interface LightningHandler {
|
|||
PayInvoice(invoice: string, amount: number, feeLimit: number): Promise<PaidInvoice>
|
||||
EstimateChainFees(address: string, amount: number, targetConf: number): Promise<EstimateFeeResponse>
|
||||
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>
|
||||
ChannelBalance(): Promise<{ local: number, remote: number }>
|
||||
GetTransactions(startHeight: number): Promise<TransactionDetails>
|
||||
|
|
|
|||
|
|
@ -346,27 +346,25 @@ export default class {
|
|||
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()
|
||||
const abortController = new AbortController()
|
||||
const req = OpenChannelReq(destination, closeAddress, fundingAmount, pushSats)
|
||||
const stream = this.lightning.openChannel(req, { abort: abortController.signal })
|
||||
return new Promise((res, rej) => {
|
||||
stream.responses.onMessage(message => {
|
||||
|
||||
switch (message.update.oneofKind) {
|
||||
case 'chanPending':
|
||||
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)
|
||||
})
|
||||
stream.responses.onMessage(message => {
|
||||
console.log("message", message)
|
||||
})
|
||||
stream.responses.onError(error => {
|
||||
console.log("error", error)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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 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> { }
|
||||
|
|
|
|||
|
|
@ -41,6 +41,8 @@ export type NodeInfo = {
|
|||
syncedToGraph: boolean
|
||||
blockHeight: number
|
||||
blockHash: string
|
||||
identityPubkey: string
|
||||
uris: string[]
|
||||
}
|
||||
export type Invoice = {
|
||||
payRequest: string
|
||||
|
|
|
|||
|
|
@ -2,11 +2,24 @@
|
|||
import BitcoinCore from 'bitcoin-core';
|
||||
import { LoadTestSettingsFromEnv, TestSettings } from "../services/main/settings.js"
|
||||
import LND from '../services/lnd/lnd.js'
|
||||
import { AddressType } from '../../proto/autogenerated/ts/types.js';
|
||||
// dave <--> alice <--> carol <--> bob
|
||||
export const setupNetwork = async () => {
|
||||
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 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) => {
|
||||
|
|
@ -26,22 +39,37 @@ const initLndInstances = async (settings: TestSettings) => {
|
|||
await dave.Warmup()
|
||||
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) => {
|
||||
const 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 wallet = await core.createWallet('');
|
||||
console.log({ wallet })
|
||||
const addr = await core.getNewAddress()
|
||||
console.log({ addr })
|
||||
await core.generateToAddress(101, addr)
|
||||
const info = await core.getWalletInfo();
|
||||
console.log({ info })
|
||||
}
|
||||
Init = async () => {
|
||||
const wallet = await this.core.createWallet('');
|
||||
console.log({ wallet })
|
||||
this.addr = await this.core.getNewAddress()
|
||||
console.log({ addr: this.addr })
|
||||
await this.Mine(101)
|
||||
const info = await this.core.getWalletInfo();
|
||||
console.log({ info })
|
||||
}
|
||||
|
||||
Mine = async (blocks: number) => {
|
||||
await this.core.generateToAddress(blocks, this.addr)
|
||||
}
|
||||
|
||||
SendToAddress = async (address: string, amount: number) => {
|
||||
const tx = await this.core.sendToAddress(address, amount)
|
||||
console.log({ tx })
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue