-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: modify por calculation, add tests
- Loading branch information
1 parent
ea53ec0
commit f06efd1
Showing
10 changed files
with
236 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
src/functions/proof-of-reserve/proof-of-reserve-functions.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { Network } from 'bitcoinjs-lib'; | ||
import { RawVault } from 'src/models/ethereum-models.js'; | ||
|
||
import { | ||
createTaprootMultisigPayment, | ||
deriveUnhardenedPublicKey, | ||
getScriptMatchingOutputFromTransaction, | ||
getUnspendableKeyCommittedToUUID, | ||
} from '../bitcoin/bitcoin-functions.js'; | ||
import { | ||
checkBitcoinTransactionConfirmations, | ||
fetchBitcoinTransaction, | ||
} from '../bitcoin/bitcoin-request-functions.js'; | ||
|
||
export async function verifyVaultDeposit( | ||
vault: RawVault, | ||
attestorGroupPublicKey: Buffer, | ||
bitcoinBlockchainBlockHeight: number, | ||
bitcoinBlockchainAPI: string, | ||
bitcoinNetwork: Network | ||
): Promise<boolean> { | ||
try { | ||
const fundingTransaction = await fetchBitcoinTransaction( | ||
vault.fundingTxId, | ||
bitcoinBlockchainAPI | ||
); | ||
|
||
const isFundingTransactionConfirmed = await checkBitcoinTransactionConfirmations( | ||
fundingTransaction, | ||
bitcoinBlockchainBlockHeight | ||
); | ||
|
||
if (!isFundingTransactionConfirmed) { | ||
return false; | ||
} | ||
|
||
const unspendableKeyCommittedToUUID = deriveUnhardenedPublicKey( | ||
getUnspendableKeyCommittedToUUID(vault.uuid, bitcoinNetwork), | ||
bitcoinNetwork | ||
); | ||
|
||
const taprootMultisigPayment = createTaprootMultisigPayment( | ||
unspendableKeyCommittedToUUID, | ||
attestorGroupPublicKey, | ||
Buffer.from(vault.taprootPubKey, 'hex'), | ||
bitcoinNetwork | ||
); | ||
|
||
const vaultTransactionOutput = getScriptMatchingOutputFromTransaction( | ||
fundingTransaction, | ||
taprootMultisigPayment.script | ||
); | ||
|
||
if (!vaultTransactionOutput) { | ||
return false; | ||
} | ||
|
||
if (vaultTransactionOutput.value !== vault.valueLocked.toNumber()) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} catch (error) { | ||
console.log(`Error verifying Vault Deposit: ${error}`); | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export const TEST_REGTEST_BITCOIN_BLOCKCHAIN_API = 'https://devnet.dlc.link/electrs'; | ||
export const TEST_TESTNET_BITCOIN_BLOCKCHAIN_API = 'https://testnet.dlc.link/electrs'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
export const TEST_REGTEST_ATTESTOR_EXTENDED_GROUP_PUBLIC_KEY_1 = | ||
'tpubDDqN2CmTDKaGeqXMayfCZEvjZqntifi4r1ztmRWsGuE1VE4bosR3mBKQwVaCxZcmg8R1nHDMDzDmzjoccBMgwZV1hhz51tAXVnhjABCQcwA'; | ||
|
||
export const TEST_TESTNET_ATTESTOR_EXTENDED_GROUP_PUBLIC_KEY_1 = | ||
'tpubDDRekL64eJJav32TLhNhG59qra7wAMaei8YMGXNiJE8ksdYrKgvaFM1XG6JrSt31W97XryScrX37RUEujjZT4qScNf8Zu1JxWj4VYkwz4rU'; | ||
|
||
export const TEST_TESTNET_ATTESTOR_UNHARDENED_DERIVED_PUBLIC_KEY_1 = | ||
'027eda4d625f781dcc98bf68901360fdaaacce8ed466096c1dfe4865209b28c058'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export const TEST_BITCOIN_BLOCKCHAIN_BLOCK_HEIGHT_1 = 2867441; | ||
export const TEST_BITCOIN_BLOCKCHAIN_BLOCK_HEIGHT_2 = 2867285; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { testnet } from 'bitcoinjs-lib/src/networks.js'; | ||
|
||
import { verifyVaultDeposit } from '../../src/functions/proof-of-reserve/proof-of-reserve-functions.js'; | ||
import { TEST_TESTNET_BITCOIN_BLOCKCHAIN_API } from '../mocks/api.test.constants.js'; | ||
import { TEST_TESTNET_ATTESTOR_UNHARDENED_DERIVED_PUBLIC_KEY_1 } from '../mocks/attestor.test.constants.js'; | ||
import { | ||
TEST_BITCOIN_BLOCKCHAIN_BLOCK_HEIGHT_1, | ||
TEST_BITCOIN_BLOCKCHAIN_BLOCK_HEIGHT_2, | ||
} from '../mocks/bitcoin.test.constants.js'; | ||
import { TEST_TESTNET_FUNDING_TRANSACTION, TEST_VAULT_2 } from '../mocks/constants'; | ||
|
||
jest.mock('../../src/functions/bitcoin/bitcoin-request-functions.js', () => { | ||
const actual = jest.requireActual('../../src/functions/bitcoin/bitcoin-request-functions.js'); | ||
return { | ||
...actual, | ||
fetchBitcoinTransaction: () => TEST_TESTNET_FUNDING_TRANSACTION, | ||
}; | ||
}); | ||
|
||
describe('Proof of Reserve Calculation', () => { | ||
describe('verifyVaultDeposit', () => { | ||
it("should return true when the vault's funding transaction is confirmed, contains an output with the multisig's script, and the output's value matches the vault's valueLocked field", async () => { | ||
const result = await verifyVaultDeposit( | ||
TEST_VAULT_2, | ||
Buffer.from(TEST_TESTNET_ATTESTOR_UNHARDENED_DERIVED_PUBLIC_KEY_1, 'hex'), | ||
TEST_BITCOIN_BLOCKCHAIN_BLOCK_HEIGHT_1, | ||
TEST_TESTNET_BITCOIN_BLOCKCHAIN_API, | ||
testnet | ||
); | ||
|
||
expect(result).toBe(true); | ||
}); | ||
|
||
it("should return false when the vault's funding transaction is not yet confirmed", async () => { | ||
const result = await verifyVaultDeposit( | ||
TEST_VAULT_2, | ||
Buffer.from(TEST_TESTNET_ATTESTOR_UNHARDENED_DERIVED_PUBLIC_KEY_1, 'hex'), | ||
TEST_BITCOIN_BLOCKCHAIN_BLOCK_HEIGHT_2, | ||
TEST_TESTNET_BITCOIN_BLOCKCHAIN_API, | ||
testnet | ||
); | ||
|
||
expect(result).toBe(false); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.