Skip to content
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"type": "module",
"name": "dlc-btc-lib",
"version": "2.6.6",
"version": "2.6.7",
"description": "This library provides a comprehensive set of interfaces and functions for minting dlcBTC tokens on supported blockchains.",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
26 changes: 26 additions & 0 deletions src/functions/proof-of-reserve/proof-of-reserve-functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,32 @@ import {
fetchBitcoinTransaction,
} from '../bitcoin/bitcoin-request-functions.js';

/**
* Gets the address of a vault.
*
* @param vault - The vault object containing transaction IDs and other relevant data
* @param extendedAttestorGroupPublicKey - The extended public key of the attestor group
* @param bitcoinNetwork - The Bitcoin network to use
* @returns A promise that resolves to the address of the vault
*/
export async function getVaultAddress(
vault: RawVault,
extendedAttestorGroupPublicKey: string,
bitcoinNetwork: Network
): Promise<string | undefined> {
try {
const { uuid, taprootPubKey, fundingTxId } = vault;

if (!fundingTxId) return;

return getVaultPayment(uuid, taprootPubKey, extendedAttestorGroupPublicKey, bitcoinNetwork)
.address!;
} catch (error) {
console.log(`Error getting Vault Address: ${error}`);
return;
}
}

/**
* Calculates the value of the vault's output in the transaction in satoshis.
*
Expand Down
20 changes: 19 additions & 1 deletion src/proof-of-reserve-handlers/proof-of-reserve-handler.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { Network } from 'bitcoinjs-lib';
import { isNotNil } from 'ramda';

import { fetchBitcoinBlockchainBlockHeight } from '../functions/bitcoin/bitcoin-request-functions.js';
import { getVaultDepositAmount } from '../functions/proof-of-reserve/proof-of-reserve-functions.js';
import {
getVaultAddress,
getVaultDepositAmount,
} from '../functions/proof-of-reserve/proof-of-reserve-functions.js';
import { RawVault } from '../models/ethereum-models.js';

/**
Expand Down Expand Up @@ -29,6 +33,20 @@ export class ProofOfReserveHandler {
this.extendedAttestorGroupPublicKey = extendedAttestorGroupPublicKey;
}

/**
* Gets all vault addresses from a list of vaults.
*
* @param vaults - An array of vault objects containing address information
* @returns A promise that resolves to an array of vault addresses
*/
async getAllVaultAddresses(vaults: RawVault[]): Promise<string[]> {
return Promise.all(
vaults.map(vault =>
getVaultAddress(vault, this.extendedAttestorGroupPublicKey, this.bitcoinNetwork)
)
).then(addresses => addresses.filter(isNotNil));
}

/**
* Calculates the total value of deposits for a list of vaults in satoshis.
*
Expand Down
30 changes: 28 additions & 2 deletions tests/unit/proof-of-reserve.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { bitcoin, testnet } from 'bitcoinjs-lib/src/networks.js';

import * as bitcoinRequestFunctions from '../../src/functions/bitcoin/bitcoin-request-functions.js';
import { getVaultDepositAmount } from '../../src/functions/proof-of-reserve/proof-of-reserve-functions.js';
import {
getVaultAddress,
getVaultDepositAmount,
} from '../../src/functions/proof-of-reserve/proof-of-reserve-functions.js';
import {
TEST_MAINNET_BITCOIN_BLOCKCHAIN_API,
TEST_TESTNET_BITCOIN_BLOCKCHAIN_API,
Expand All @@ -21,7 +24,11 @@ import {
TEST_BITCOIN_BLOCKCHAIN_BLOCK_HEIGHT_2,
TEST_BITCOIN_BLOCKCHAIN_BLOCK_HEIGHT_3,
} from '../mocks/bitcoin.test.constants.js';
import { TEST_VAULT_2, TEST_VAULT_3 } from '../mocks/ethereum-vault.test.constants.js';
import {
TEST_VAULT_2,
TEST_VAULT_3,
TEST_VAULT_4,
} from '../mocks/ethereum-vault.test.constants.js';

describe('Proof of Reserve Calculation', () => {
beforeEach(() => {
Expand Down Expand Up @@ -126,4 +133,23 @@ describe('Proof of Reserve Calculation', () => {
expect(result).toBe(100000);
});
});
describe('getVaultAddress', () => {
it('should return the vault address', async () => {
const result = await getVaultAddress(
TEST_VAULT_2,
TEST_TESTNET_ATTESTOR_EXTENDED_GROUP_PUBLIC_KEY_1,
testnet
);
expect(result).toBe('tb1pd4l9qxw8jhg9l57ls9cnq6d28gcfayf2v9244vlt6mj80apvracqgdt090');
});

it('should return none if the vault has no funding transaction', async () => {
const result = await getVaultAddress(
TEST_VAULT_4,
TEST_TESTNET_ATTESTOR_EXTENDED_GROUP_PUBLIC_KEY_1,
testnet
);
expect(result).toBeUndefined();
});
});
});
Loading