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.7",
"version": "2.6.8",
"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
19 changes: 10 additions & 9 deletions src/functions/proof-of-reserve/proof-of-reserve-functions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Network } from 'bitcoinjs-lib';
import { VaultProofOfReserveData } from 'src/models/proof-of-reserve.models.js';

import { RawVault } from '../../models/ethereum-models.js';
import { isNonEmptyString } from '../../utilities/index.js';
Expand Down Expand Up @@ -49,20 +50,20 @@ export async function getVaultAddress(
* @returns A promise that resolves to the value of the vault's output in the transaction in satoshis, or 0 if the transaction is not confirmed or invalid
* @throws Will log an error message if there is an issue verifying the vault deposit
*/
export async function getVaultDepositAmount(
export async function getVaultProofOfReserveData(
vault: RawVault,
extendedAttestorGroupPublicKey: string,
bitcoinBlockchainBlockHeight: number,
bitcoinBlockchainAPI: string,
bitcoinNetwork: Network
): Promise<number> {
): Promise<VaultProofOfReserveData | undefined> {
try {
const { uuid, taprootPubKey, wdTxId, fundingTxId } = vault;

const hasWithdrawDepositTransaction = isNonEmptyString(wdTxId);
const hasFundingTransaction = isNonEmptyString(fundingTxId);

if (!hasWithdrawDepositTransaction && !hasFundingTransaction) return 0;
if (!hasWithdrawDepositTransaction && !hasFundingTransaction) return;

const txID = hasWithdrawDepositTransaction ? wdTxId : fundingTxId;

Expand All @@ -83,21 +84,21 @@ export async function getVaultDepositAmount(
if (!isVaultTransactionConfirmed) {
const vaultMultisigInput = findVaultMultisigInput(vaultTransaction, vaultPayment.address!);

return vaultMultisigInput ? vaultMultisigInput.prevout.value : 0;
return vaultMultisigInput
? { amount: vaultMultisigInput.prevout.value, address: vaultPayment.address! }
: undefined;
}

const vaultTransactionOutput = getScriptMatchingOutputFromTransaction(
vaultTransaction,
vaultPayment.script
);

if (!vaultTransactionOutput) {
return 0;
}
if (!vaultTransactionOutput) return;

return vaultTransactionOutput.value;
return { amount: vaultTransactionOutput.value, address: vaultPayment.address! };
} catch (error) {
console.log(`Error verifying Vault Deposit: ${error}`);
return 0;
return;
}
}
9 changes: 9 additions & 0 deletions src/models/proof-of-reserve.models.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export interface VaultProofOfReserveData {
amount: number;
address: string;
}

export interface ProofOfReserveData {
proofOfReserve: number;
vaultAddresses: string[];
}
55 changes: 24 additions & 31 deletions src/proof-of-reserve-handlers/proof-of-reserve-handler.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { Network } from 'bitcoinjs-lib';
import { isNotNil } from 'ramda';
import { ProofOfReserveData } from 'src/models/proof-of-reserve.models.js';

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

/**
Expand Down Expand Up @@ -34,42 +32,37 @@ export class ProofOfReserveHandler {
}

/**
* 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.
* Gets the Proof of Reserve data for a list of vaults.
*
* @param vaults - An array of vault objects containing deposit information
* @returns A promise that resolves to the total value of deposits in the vaults in satoshis
* @returns A promise that resolves to the Proof of Reserve data for the vaults
*/
async calculateProofOfReserve(vaults: RawVault[]): Promise<number> {
async getProofOfReserveData(vaults: RawVault[]): Promise<ProofOfReserveData> {
const bitcoinBlockchainBlockHeight = await fetchBitcoinBlockchainBlockHeight(
this.bitcoinBlockchainAPI
);

const depositAmounts = await Promise.all(
vaults.map(vault =>
getVaultDepositAmount(
vault,
this.extendedAttestorGroupPublicKey,
bitcoinBlockchainBlockHeight,
this.bitcoinBlockchainAPI,
this.bitcoinNetwork
const proofOfReserveData = (
await Promise.all(
vaults.map(vault =>
getVaultProofOfReserveData(
vault,
this.extendedAttestorGroupPublicKey,
bitcoinBlockchainBlockHeight,
this.bitcoinBlockchainAPI,
this.bitcoinNetwork
)
)
)
);
).filter(isNotNil);

return depositAmounts.reduce((a, b) => a + b, 0);
return proofOfReserveData.reduce(
(acc, curr) => {
acc.proofOfReserve += curr.amount;
acc.vaultAddresses.push(curr.address);
return acc;
},
{ proofOfReserve: 0, vaultAddresses: [] as string[] }
);
}
}
60 changes: 20 additions & 40 deletions tests/unit/proof-of-reserve.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import { bitcoin, testnet } from 'bitcoinjs-lib/src/networks.js';

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

describe('Proof of Reserve Calculation', () => {
beforeEach(() => {
Expand All @@ -40,15 +33,18 @@ describe('Proof of Reserve Calculation', () => {
.spyOn(bitcoinRequestFunctions, 'fetchBitcoinTransaction')
.mockImplementationOnce(async () => TEST_TESTNET_FUNDING_TRANSACTION_1);

const result = await getVaultDepositAmount(
const result = await getVaultProofOfReserveData(
TEST_VAULT_2,
TEST_TESTNET_ATTESTOR_EXTENDED_GROUP_PUBLIC_KEY_1,
TEST_BITCOIN_BLOCKCHAIN_BLOCK_HEIGHT_1,
TEST_TESTNET_BITCOIN_BLOCKCHAIN_API,
testnet
);

expect(result).toBe(10000000);
expect(result).toStrictEqual({
amount: 10000000,
address: 'tb1pd4l9qxw8jhg9l57ls9cnq6d28gcfayf2v9244vlt6mj80apvracqgdt090',
});
});

it('should return 0 if the funding transaction is not found', async () => {
Expand All @@ -58,98 +54,82 @@ describe('Proof of Reserve Calculation', () => {
throw new Error('Transaction not found');
});

const result = await getVaultDepositAmount(
const result = await getVaultProofOfReserveData(
TEST_VAULT_2,
TEST_TESTNET_ATTESTOR_EXTENDED_GROUP_PUBLIC_KEY_1,
TEST_BITCOIN_BLOCKCHAIN_BLOCK_HEIGHT_1,
TEST_TESTNET_BITCOIN_BLOCKCHAIN_API,
testnet
);

expect(result).toBe(0);
expect(result).toBeUndefined();
});

it("should return 0 when the vault's funding transaction is not yet confirmed", async () => {
jest
.spyOn(bitcoinRequestFunctions, 'fetchBitcoinTransaction')
.mockImplementationOnce(async () => TEST_TESTNET_FUNDING_TRANSACTION_1);

const result = await getVaultDepositAmount(
const result = await getVaultProofOfReserveData(
TEST_VAULT_2,
TEST_TESTNET_ATTESTOR_EXTENDED_GROUP_PUBLIC_KEY_1,
TEST_BITCOIN_BLOCKCHAIN_BLOCK_HEIGHT_2,
TEST_TESTNET_BITCOIN_BLOCKCHAIN_API,
testnet
);

expect(result).toBe(0);
expect(result).toBeUndefined();
});

it("should return 0 if the vault's funding transaction lacks an output with the multisig's script", async () => {
jest
.spyOn(bitcoinRequestFunctions, 'fetchBitcoinTransaction')
.mockImplementationOnce(async () => TEST_TESTNET_FUNDING_TRANSACTION_2);

const result = await getVaultDepositAmount(
const result = await getVaultProofOfReserveData(
TEST_VAULT_2,
TEST_TESTNET_ATTESTOR_EXTENDED_GROUP_PUBLIC_KEY_1,
TEST_BITCOIN_BLOCKCHAIN_BLOCK_HEIGHT_1,
TEST_TESTNET_BITCOIN_BLOCKCHAIN_API,
testnet
);

expect(result).toBe(0);
expect(result).toBeUndefined();
});

it("should return 0 if the vault is legacy and it's funding transaction lacks an output with the multisig's script", async () => {
jest
.spyOn(bitcoinRequestFunctions, 'fetchBitcoinTransaction')
.mockImplementationOnce(async () => TEST_MAINNET_FUNDING_TRANSACTION_1);

const result = await getVaultDepositAmount(
const result = await getVaultProofOfReserveData(
TEST_VAULT_3,
TEST_MAINNET_ATTESTOR_EXTENDED_GROUP_PUBLIC_KEY_1,
TEST_BITCOIN_BLOCKCHAIN_BLOCK_HEIGHT_3,
TEST_MAINNET_BITCOIN_BLOCKCHAIN_API,
bitcoin
);

expect(result).toBe(0);
expect(result).toBeUndefined();
});

it("should return the vault's previous deposit amount when the vault's funding transaction is not yet confirmed", async () => {
jest
.spyOn(bitcoinRequestFunctions, 'fetchBitcoinTransaction')
.mockImplementationOnce(async () => TEST_TESTNET_FUNDING_TRANSACTION_7);

const result = await getVaultDepositAmount(
const result = await getVaultProofOfReserveData(
TEST_VAULT_2,
TEST_TESTNET_ATTESTOR_EXTENDED_GROUP_PUBLIC_KEY_1,
TEST_BITCOIN_BLOCKCHAIN_BLOCK_HEIGHT_2,
TEST_TESTNET_BITCOIN_BLOCKCHAIN_API,
testnet
);

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();
expect(result).toStrictEqual({
amount: 100000,
address: 'tb1pd4l9qxw8jhg9l57ls9cnq6d28gcfayf2v9244vlt6mj80apvracqgdt090',
});
});
});
});
Loading