diff --git a/package.json b/package.json index 386221f..c47d20d 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/functions/proof-of-reserve/proof-of-reserve-functions.ts b/src/functions/proof-of-reserve/proof-of-reserve-functions.ts index 1c4615e..b57a339 100644 --- a/src/functions/proof-of-reserve/proof-of-reserve-functions.ts +++ b/src/functions/proof-of-reserve/proof-of-reserve-functions.ts @@ -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 { + 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. * diff --git a/src/proof-of-reserve-handlers/proof-of-reserve-handler.ts b/src/proof-of-reserve-handlers/proof-of-reserve-handler.ts index 1ebadab..12ea169 100644 --- a/src/proof-of-reserve-handlers/proof-of-reserve-handler.ts +++ b/src/proof-of-reserve-handlers/proof-of-reserve-handler.ts @@ -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'; /** @@ -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 { + 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. * diff --git a/tests/unit/proof-of-reserve.test.ts b/tests/unit/proof-of-reserve.test.ts index d1dfa43..e5a561c 100644 --- a/tests/unit/proof-of-reserve.test.ts +++ b/tests/unit/proof-of-reserve.test.ts @@ -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, @@ -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(() => { @@ -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(); + }); + }); });