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

Scan and getBalance support #22

Merged
merged 2 commits into from
Jan 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 4 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ jobs:
command: curl --fail -X GET http://localhost:8094/regtest/api/blocks/tip/height
- name: Run unit tests
run: npm run test
env:
BITCOIN_RPC_USER: alice
BITCOIN_RPC_PASSWORD: password
BITCOIN_RPC_HOST: localhost:18443
- name: Fetch esplora logs
if: always()
run: docker-compose -f "./test/helpers/docker-compose.yaml" logs esplora
Expand Down
1 change: 1 addition & 0 deletions jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ module.exports = {
collectCoverageFrom: ['<rootDir>/src/**/*.(t|j)s'],
coveragePathIgnorePatterns: ['.*.spec.ts'],
coverageDirectory: './coverage',
testTimeout: 30000,
};
46 changes: 46 additions & 0 deletions src/wallet/coin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { toOutputScript } from 'bitcoinjs-lib/src/address';
import { Network } from 'bitcoinjs-lib';

type CoinStatus = {
isConfirmed: boolean;
blockHeight?: number;
blockHash?: string;
blockTime?: number;
};

export class Coin {
txid: string; // previous transaction id
vout: number; // index of the output in the previous transaction
value: number;
address: string;
status: CoinStatus;

constructor(partial: Partial<Coin>) {
Object.assign(this, partial);
}

static fromJSON(json: string): Coin {
return new Coin(JSON.parse(json));
}

toJSON(): string {
return JSON.stringify({
txid: this.txid,
vout: this.vout,
value: this.value,
address: this.address,
status: this.status,
});
}

toInput(network: Network) {
return {
hash: this.txid,
index: this.vout,
witnessUtxo: {
script: toOutputScript(this.address, network),
value: this.value,
},
};
}
}
4 changes: 4 additions & 0 deletions src/wallet/db/db.interface.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Buffer } from 'buffer';
import { Coin } from '../coin.ts';

export type DbInterface = {
open(): Promise<void>;
Expand All @@ -15,4 +16,7 @@ export type DbInterface = {
setReceiveDepth(depth: number): Promise<void>;
getChangeDepth(): Promise<number>;
setChangeDepth(depth: number): Promise<void>;
getAllAddresses(): Promise<string[]>;
saveUnspentCoins(coins: Coin[]): Promise<void>;
getUnspentCoins(): Promise<Coin[]>;
};
14 changes: 14 additions & 0 deletions src/wallet/db/level/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Level } from 'level';
import { wdb } from './layout.ts';
import { DbInterface } from '../db.interface.ts';
import { Buffer } from 'buffer';
import { Coin } from '../../coin.ts';

export type LevelDBConfigOptions = {
location: string;
Expand Down Expand Up @@ -86,4 +87,17 @@ export class WalletDB implements DbInterface {
async setChangeDepth(depth: number): Promise<void> {
await this.db.sublevel(wdb.A).put('changeDepth', depth.toString());
}

async getAllAddresses(): Promise<string[]> {
return await this.db.sublevel(wdb.A).keys().all();
}

async saveUnspentCoins(coins: Coin[]): Promise<void> {
await this.db.sublevel(wdb.C).put('unspent', JSON.stringify(coins));
}

async getUnspentCoins(): Promise<Coin[]> {
const coins = JSON.parse(await this.db.sublevel(wdb.C).get('unspent'));
return coins.map((coin: string) => Coin.fromJSON(coin));
}
}
1 change: 1 addition & 0 deletions src/wallet/db/level/layout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ export const wdb = {
V: 'V', // Version
M: 'M', // Master key
A: 'A', // Address
C: 'C', // Coins
};
10 changes: 10 additions & 0 deletions src/wallet/network/esplora.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { URL } from 'url';
import axios, { AxiosError, AxiosRequestConfig } from 'axios';
import { Network } from 'bitcoinjs-lib';
import { regtest, testnet, bitcoin } from 'bitcoinjs-lib/src/networks';
import { Coin } from '../coin.ts';

export type EsploraConfigOptions = {
network: 'testnet' | 'main' | 'regtest';
Expand Down Expand Up @@ -82,4 +83,13 @@ export class EsploraClient implements NetworkInterface {
url: `${this.url}//block-height/${height}`,
});
}

async getUTXOs(address: string): Promise<Coin[]> {
return (
await this.request({
method: 'GET',
url: `${this.url}/address/${address}/utxo`,
})
).map((utxo: object) => new Coin({ ...utxo, address }));
}
}
2 changes: 2 additions & 0 deletions src/wallet/network/network.interface.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { Network } from 'bitcoinjs-lib';
import { Coin } from '../coin.ts';

export type NetworkInterface = {
get network(): Network;
getLatestBlockHeight(): Promise<number>;
getLatestBlockHash(): Promise<string>;
getBlockHash(height: number): Promise<string>;
getUTXOs(address: string): Promise<Coin[]>;
};
16 changes: 16 additions & 0 deletions src/wallet/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,20 @@ export class Wallet {
this.changeDepth++;
return address;
}

async scan() {
const addresses = await this.db.getAllAddresses();
const coins = (
await Promise.all(
addresses.map((address) => this.network.getUTXOs(address)),
)
).reduce((acc, utxos) => [...acc, ...utxos], []);

await this.db.saveUnspentCoins(coins);
}

async getBalance(): Promise<number> {
const coins = await this.db.getUnspentCoins();
return coins.reduce((acc, coin) => acc + coin.value, 0);
}
}
148 changes: 148 additions & 0 deletions test/helpers/bitcoin-rpc-client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
import axios, { AxiosError, AxiosRequestConfig } from 'axios';

export class BitcoinRpcClient {
url: string;
config: AxiosRequestConfig;

constructor() {
const user = process.env.BITCOIN_RPC_USER;
const password = process.env.BITCOIN_RPC_PASSWORD;
const host = process.env.BITCOIN_RPC_HOST;
this.url = `http://${user}:${password}@${host}`;
this.config = {
method: 'POST',
headers: {
'content-type': 'application/json',
},
};
}

async init() {
let loadWallet = false;
try {
await this.createWallet('default');
} catch (e) {
if (
e instanceof AxiosError &&
e.response?.data.error.message.includes(
'Database already exists.',
)
) {
loadWallet = true;
} else {
throw e;
}
}
try {
const result = await this.getWalletInfo();
if (result['walletname'] === 'default') {
loadWallet = false;
}
} catch (e) {
if (
e instanceof AxiosError &&
!e.response?.data.error.message.includes('No wallet is loaded.')
) {
throw e;
}
}
try {
if (loadWallet) {
await this.loadWallet('default');
const address = await this.getNewAddress();
await this.mineToAddress(150, address);
}
} catch (e) {
if (
e instanceof AxiosError &&
!e.response?.data.error.message.includes(
'Unable to obtain an exclusive lock on the database',
)
) {
throw e;
}
}
}

private async request(config: AxiosRequestConfig) {
try {
const response = await axios.request({
...this.config,
...config,
});
return response.data?.result;
} catch (e) {
if (e instanceof AxiosError) {
if (e.response?.data.error) {
// eslint-disable-next-line no-console
console.log(e.response.data);
throw new Error(e.response.data.error.message);
} else {
throw new Error(e.message);
}
} else {
throw e;
}
}
}

async createWallet(walletName: string) {
return await this.request({
url: this.url,
data: {
method: 'createwallet',
params: [walletName],
},
});
}

async getWalletInfo() {
return await this.request({
url: this.url,
data: {
method: 'getwalletinfo',
params: [],
},
});
}

async loadWallet(walletName: string) {
return await this.request({
url: this.url,
data: {
method: 'loadwallet',
params: [walletName],
},
});
}

async getNewAddress() {
return await this.request({
url: this.url,
data: {
method: 'getnewaddress',
params: [],
},
});
}

async mineToAddress(numBlocks: number, address: string) {
return await this.request({
url: this.url,
data: {
method: 'generatetoaddress',
params: [numBlocks, address],
},
});
}

async sendToAddress(address: string, amount: number) {
return await this.request({
url: this.url,
data: {
method: 'sendtoaddress',
params: [address, amount],
},
});
}
}
38 changes: 37 additions & 1 deletion test/wallet.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { EsploraClient, Wallet, WalletDB } from '../src/wallet';
import * as fs from 'fs';
import { BitcoinRpcClient } from './helpers/bitcoin-rpc-client';

describe('Wallet', () => {
let wallet: Wallet;
let address: string;
let bitcoinRpcClient: BitcoinRpcClient;

beforeAll(async () => {
const walletDB = new WalletDB({
Expand All @@ -17,6 +20,14 @@ describe('Wallet', () => {
network: 'regtest',
}),
});

bitcoinRpcClient = new BitcoinRpcClient();
await bitcoinRpcClient.init();

await bitcoinRpcClient.mineToAddress(
101,
await bitcoinRpcClient.getNewAddress(),
);
});

it('should initialise the wallet', async () => {
Expand All @@ -26,7 +37,7 @@ describe('Wallet', () => {
});

it('should derive first receive address', async () => {
const address = await wallet.deriveReceiveAddress();
address = await wallet.deriveReceiveAddress();
expect(address).toBe('bcrt1qcr8te4kr609gcawutmrza0j4xv80jy8zeqchgx');
});

Expand All @@ -40,6 +51,31 @@ describe('Wallet', () => {
expect(address).toBe('bcrt1q8c6fshw2dlwun7ekn9qwf37cu2rn755ufhry49');
});

it('should rescan all addresses', async () => {
await bitcoinRpcClient.sendToAddress(address, 0.1);
await bitcoinRpcClient.mineToAddress(
3,
await bitcoinRpcClient.getNewAddress(),
);

await wallet.scan();
});

it('should get balance', async () => {
// we have to do this because esplora is not always in sync with the node
let retryCount = 5;
while (retryCount > 0) {
const balance = await wallet.getBalance();
if (balance === 0) {
await new Promise((resolve) => setTimeout(resolve, 1000));
await wallet.scan();
}
retryCount--;
}

expect(await wallet.getBalance()).toBe(10000000);
});

afterAll(async () => {
await wallet.close();
fs.rmSync('./test/wallet', { recursive: true, force: true });
Expand Down
Loading