Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bitcoind for onchain #317

Closed
wants to merge 38 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
d7b3016
Adding temporary bitcoindHotWalletClient
jotapea Jul 9, 2021
91efc67
Starting to add bitcoind functions that will replace lnd equivalents …
jotapea Jul 9, 2021
d65b6b0
Bitcoind sendToAddress is in BTC
jotapea Jul 9, 2021
f25b124
Getting the fee from getTransaction directly
jotapea Jul 9, 2021
e2a4b73
Logs showing most of bitcoind values working (tests pass)
jotapea Jul 9, 2021
4055d24
Revert "Logs showing most of bitcoind values working (tests pass)"
jotapea Jul 9, 2021
b2cb90b
Prettier modifications
jotapea Jul 9, 2021
f892602
Disable unused variable warning
jotapea Jul 9, 2021
efa79b6
Use bitcoindHotWalletClient instead of bitcoindDefaultClient
jotapea Jul 9, 2021
f53c0e4
Keep imports in alphabetical order
jotapea Jul 9, 2021
722eaac
Add (temporary?) PayOnChainClient class
jotapea Jul 12, 2021
b42e713
Add BitcoindClient class and one line switch
jotapea Jul 12, 2021
846163a
Switch back to lnd client
jotapea Jul 12, 2021
eaae5d9
Comments of failed bitcoind tests
jotapea Jul 12, 2021
c9f5023
Removing try catch from test
jotapea Jul 12, 2021
931930a
Name the default bitcoind wallet 'default'
jotapea Jul 12, 2021
7a8c996
Prettier
jotapea Jul 12, 2021
d2b9993
Adding test to funding new hot wallet fails
jotapea Jul 12, 2021
f191f71
Comment out failing test
jotapea Jul 12, 2021
2195777
Rename default test and remove hot wallet
jotapea Jul 14, 2021
7b3014d
Rename test from fund bitcoind to outside
jotapea Jul 14, 2021
d75007f
Rename onchain_funding to lnd_onchain_funding
jotapea Jul 14, 2021
74cc217
Add test that creates hot bitcoind wallet
jotapea Jul 14, 2021
5e85a45
One more location to set the "default" wallet
jotapea Jul 14, 2021
ddcef66
Tests pass deposit to bitcoind test commented out
jotapea Jul 14, 2021
2ff75ba
Add bitcoind client version of onchain methods
jotapea Jul 15, 2021
f5403dc
Add alternate bitcoind_onchain_funding function
jotapea Jul 15, 2021
0d34550
Enable bitcoind client and funding tests TODO
jotapea Jul 15, 2021
a5d0433
Revert "Enable bitcoind client and funding tests TODO"
jotapea Jul 15, 2021
cdb2130
Fix: Define type for transactions_bitcoind
jotapea Jul 15, 2021
8c2c32d
Onchain receive test passes
jotapea Jul 16, 2021
3137c35
Temporary removal
jotapea Jul 16, 2021
a88636e
Revert "Temporary removal"
jotapea Jul 16, 2021
9eed585
Rename class avoid conflict with BitcoindClient
jotapea Jul 16, 2021
2daa2dc
Rename 'default' wallet to 'outside'
jotapea Jul 16, 2021
2d47cb9
Rename to bitcoindHotWalletClient
jotapea Jul 16, 2021
d20f6de
Keep default bitcoind client while having outside
jotapea Jul 16, 2021
038be28
Temporary remove
jotapea Jul 16, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
368 changes: 355 additions & 13 deletions src/OnChain.ts

Large diffs are not rendered by default.

15 changes: 10 additions & 5 deletions src/SpecterWallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ export class SpecterWallet {
this.logger.info("specter wallet has not been instantiated")

// currently use for testing purpose. need to refactor
return ""
return "outside"
// return ""
}

if (specterWallets.length > 1) {
Expand Down Expand Up @@ -77,7 +78,8 @@ export class SpecterWallet {
async getBitcoindBalance(): Promise<number> {
if (!this.bitcoindClient) {
const wallet = await this.setBitcoindClient()
if (wallet === "") {
if (wallet === "outside") {
// if (wallet === "") {
return 0
}
}
Expand All @@ -90,7 +92,8 @@ export class SpecterWallet {

if (!this.bitcoindClient) {
const wallet = await this.setBitcoindClient()
if (wallet === "") {
if (wallet === "outside") {
// if (wallet === "") {
return
}
}
Expand Down Expand Up @@ -169,7 +172,8 @@ export class SpecterWallet {
async toColdStorage({ sats }) {
if (!this.bitcoindClient) {
const wallet = await this.setBitcoindClient()
if (wallet === "") {
if (wallet === "outside") {
// if (wallet === "") {
this.logger.warn("no wallet has been setup")
return
}
Expand Down Expand Up @@ -221,7 +225,8 @@ export class SpecterWallet {
async toLndWallet({ sats }) {
if (!this.bitcoindClient) {
const wallet = await this.setBitcoindClient()
if (wallet === "") {
if (wallet === "outside") {
// if (wallet === "") {
return
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/bitcoind.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ export const getBalancesDetail = async (): Promise<
const balances: { wallet: string; balance: number }[] = []

for await (const wallet of wallets) {
// do not use the default wallet for now (expect for testing).
if (wallet === "") {
// do not use the outside wallet for now (except for testing).
if (wallet === "outside") {
// if (wallet === "") {
continue
}

Expand Down
6 changes: 6 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,16 @@ const connection_obj = {
version: "0.21.0",
}

// Using a single bitcoind node client
export const BitcoindClient = ({ wallet = "" }) =>
new bitcoindClient({ ...connection_obj, wallet })

// For use when only the bitcoind client itself is needed, not wallet specific
export const bitcoindDefaultClient = BitcoindClient({ wallet: "" })

export const bitcoindOutsideWalletClient = BitcoindClient({ wallet: "outside" })
export const bitcoindHotWalletClient = BitcoindClient({ wallet: "hot" })

export const addContact = async ({ uid, username }) => {
// https://stackoverflow.com/questions/37427610/mongodb-update-or-insert-object-in-array

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import { createChainAddress } from "lightning"
import mongoose from "mongoose"
import { setupMongoConnection } from "src/mongodb"
import { bitcoindDefaultClient } from "src/utils"
import { bitcoindDefaultClient, bitcoindOutsideWalletClient } from "src/utils"
import {
checkIsBalanced,
lnd1,
Expand Down Expand Up @@ -45,23 +45,22 @@ afterAll(async () => {
jest.restoreAllMocks()
})

it("funds bitcoind wallet", async () => {
it("funds outside bitcoind wallet", async () => {
// it("funds bitcoind wallet", async () => {
let balance

try {
// depend of bitcoind version. needed in < 0.20 but failed in 0.21?
const { name } = await bitcoindDefaultClient.createWallet("")
expect(name).toBe("")
} catch (err) {
console.log({ err })
}
// depend of bitcoind version. needed in < 0.20 but failed in 0.21?
const { name } = await bitcoindDefaultClient.createWallet("outside")
expect(name).toBe("outside")
// const { name } = await bitcoindDefaultClient.createWallet("")
// expect(name).toBe("")

balance = await bitcoindDefaultClient.getBalance()
balance = await bitcoindOutsideWalletClient.getBalance()

const bitcoindAddress = await bitcoindDefaultClient.getNewAddress()
await bitcoindDefaultClient.generateToAddress(numOfBlock, bitcoindAddress)
await bitcoindDefaultClient.generateToAddress(100, RANDOM_ADDRESS)
balance = await bitcoindDefaultClient.getBalance()
const bitcoindAddress = await bitcoindOutsideWalletClient.getNewAddress()
await bitcoindOutsideWalletClient.generateToAddress(numOfBlock, bitcoindAddress)
await bitcoindOutsideWalletClient.generateToAddress(100, RANDOM_ADDRESS)
balance = await bitcoindOutsideWalletClient.getBalance()
expect(balance).toBe(initialBitcoinWalletBalance + blockReward * numOfBlock)
})

Expand All @@ -71,8 +70,9 @@ it("funds outside lnd node", async () => {
).address
expect(lndOutside1_wallet_addr.substr(0, 4)).toBe("bcrt")

await bitcoindDefaultClient.sendToAddress(lndOutside1_wallet_addr, amount_BTC)
await bitcoindDefaultClient.generateToAddress(6, RANDOM_ADDRESS)
// Funded by the outside wallet
await bitcoindOutsideWalletClient.sendToAddress(lndOutside1_wallet_addr, amount_BTC)
await bitcoindOutsideWalletClient.generateToAddress(6, RANDOM_ADDRESS)

await waitUntilBlockHeight({ lnd: lnd1, blockHeight: 100 + numOfBlock + 6 })
await waitUntilBlockHeight({ lnd: lndOutside1, blockHeight: 100 + numOfBlock + 6 })
Expand Down
Loading