Skip to content

Commit

Permalink
up
Browse files Browse the repository at this point in the history
  • Loading branch information
boufni95 committed Apr 24, 2024
1 parent 3644352 commit 6564fca
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 38 deletions.
2 changes: 1 addition & 1 deletion src/services/lnd/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
32 changes: 15 additions & 17 deletions src/services/lnd/lnd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/services/lnd/mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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> { }
Expand Down
2 changes: 2 additions & 0 deletions src/services/lnd/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ export type NodeInfo = {
syncedToGraph: boolean
blockHeight: number
blockHash: string
identityPubkey: string
uris: string[]
}
export type Invoice = {
payRequest: string
Expand Down
66 changes: 47 additions & 19 deletions src/tests/networkSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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 })
}
}

0 comments on commit 6564fca

Please sign in to comment.