-
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: add unit test for creating and signing vault related transactions
- Loading branch information
1 parent
51e2ab0
commit b5c3224
Showing
7 changed files
with
1,708 additions
and
73 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
export default { | ||
preset: 'ts-jest', | ||
testEnvironment: 'node', | ||
testMatch: ['**/?(*.)+(spec|test).ts'], | ||
moduleNameMapper: { | ||
'^(\\.\\.?\\/.+)\\.js$': '$1', | ||
}, | ||
}; |
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
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,60 @@ | ||
import { regtest } from 'bitcoinjs-lib/src/networks.js'; | ||
import { BigNumber } from 'ethers'; | ||
|
||
import { RawVault } from '../../src/models/ethereum-models.js'; | ||
|
||
// Bitcoin | ||
export const TEST_BITCOIN_NETWORK = regtest; | ||
export const TEST_BITCOIN_ADDRESS = 'bcrt1q4htslvp40rf6epd2hqyfww2mkprzy0yjmagsy5'; | ||
export const TEST_BITCOIN_EXTENDED_PRIVATE_KEY = | ||
'tprv8ZgxMBicQKsPeJ7iQfVEb34R3JoSyJ1J9z6wv1yXJkd1NMTRbmQiLkcZXgqQ277LMtszhnp2L2VmmHhFzoLD12fjyXcAvfnvs6qTJMMcKFq'; | ||
export const TEST_BITCOIN_WALLET_ACCOUNT_INDEX = 0; | ||
export const TEST_BITCOIN_BLOCKCHAIN_API = 'https://devnet.dlc.link/electrs'; | ||
export const TEST_BITCOIN_BLOCKCHAIN_FEE_RECOMMENDATION_API = | ||
'https://devnet.dlc.link/electrs/fee-estimates'; | ||
export const TEST_BITCOIN_AMOUNT = 0.01; | ||
|
||
// Ethereum | ||
export const TEST_ETHEREUM_PRIVATE_KEY = ''; | ||
export const TEST_ETHEREUM_NODE_API = 'https://sepolia-rollup.arbitrum.io/rpc'; | ||
export const TEST_ETHEREUM_READ_ONLY_NODE_API = 'https://sepolia-rollup.arbitrum.io/rpc'; | ||
export const TEST_ETHEREUM_GITHUB_DEPLOYMENT_PLAN_ROOT_URL = | ||
'https://raw.githubusercontent.com/DLC-link/dlc-solidity'; | ||
export const TEST_ETHEREUM_DEVNET_GITHUB_DEPLOYMENT_PLAN_BRANCH = 'dev'; | ||
export const TEST_ETHEREUM_TESTNET_GITHUB_DEPLOYMENT_PLAN_BRANCH = 'testnet-rolling'; | ||
export const TEST_ETHEREUM_ATTESTOR_CHAIN_ID = 'evm-arbsepolia'; | ||
|
||
// Attestor | ||
export const TEST_REGTEST_ATTESTOR_APIS = [ | ||
'https://devnet.dlc.link/attestor-1', | ||
'https://devnet.dlc.link/attestor-2', | ||
'https://devnet.dlc.link/attestor-3', | ||
]; | ||
|
||
// export const TEST_TESTNET_ATTESTOR_APIS = [ | ||
// 'https://testnet.dlc.link/attestor-1', | ||
// 'https://testnet.dlc.link/attestor-2', | ||
// 'https://testnet.dlc.link/attestor-3', | ||
// ]; | ||
// export const TEST_TESTNET_BITCOIN_BLOCKCHAIN_API = 'https://testnet.dlc.link/electrs'; | ||
|
||
// export const TEST_TESTNET_ATTESTOR_GROUP_PUBLIC_KEY_V1 = | ||
// '0c0bf55fa1ab72462467b973b13e556b07d2fdd8d7a30cdfc10f337e23c7ac00'; | ||
|
||
export const TEST_REGTEST_ATTESTOR_EXTENDED_GROUP_PUBLIC_KEY = | ||
'tpubDDqN2CmTDKaGeqXMayfCZEvjZqntifi4r1ztmRWsGuE1VE4bosR3mBKQwVaCxZcmg8R1nHDMDzDmzjoccBMgwZV1hhz51tAXVnhjABCQcwA'; | ||
|
||
export const TEST_VAULT: RawVault = { | ||
uuid: '0x400ca1a687f9c8241566d334fcb4b33efab8e540b943be1455143284c5afc962', | ||
protocolContract: '0x6e692DB944162f8b4250aA25eCEe80608457D7a7', | ||
timestamp: BigNumber.from('0x665da025'), | ||
valueLocked: BigNumber.from('0x0f4240'), | ||
creator: '0x0DD4f29E21F10cb2E485cf9bDAb9F2dD1f240Bfa', | ||
status: 0, | ||
fundingTxId: '', | ||
closingTxId: '', | ||
btcFeeRecipient: '031131cd88bcea8c1d84da8e034bb24c2f6e748c571922dc363e7e088f5df0436c', | ||
btcMintFeeBasisPoints: BigNumber.from('0x64'), | ||
btcRedeemFeeBasisPoints: BigNumber.from('0x64'), | ||
taprootPubKey: '', | ||
}; |
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,107 @@ | ||
import { Transaction, p2wpkh } from '@scure/btc-signer'; | ||
|
||
import { PrivateKeyDLCHandler } from '../../src/index.js'; | ||
import { | ||
TEST_BITCOIN_BLOCKCHAIN_API, | ||
TEST_BITCOIN_BLOCKCHAIN_FEE_RECOMMENDATION_API, | ||
TEST_BITCOIN_EXTENDED_PRIVATE_KEY, | ||
TEST_BITCOIN_NETWORK, | ||
TEST_BITCOIN_WALLET_ACCOUNT_INDEX, | ||
TEST_REGTEST_ATTESTOR_EXTENDED_GROUP_PUBLIC_KEY, | ||
TEST_VAULT, | ||
} from '../mocks/constants.js'; | ||
|
||
describe('Create and Sign Vault related Transactions', () => { | ||
let dlcHandler: PrivateKeyDLCHandler; | ||
let fundingTransaction: Transaction; | ||
let signedFundingTransaction: Transaction; | ||
let closingTransaction: Transaction; | ||
let partiallySignedClosingTransaction: Transaction; | ||
|
||
it('should initialize a Private Key DLC Handler', async () => { | ||
dlcHandler = new PrivateKeyDLCHandler( | ||
TEST_BITCOIN_EXTENDED_PRIVATE_KEY, | ||
TEST_BITCOIN_WALLET_ACCOUNT_INDEX, | ||
TEST_BITCOIN_NETWORK, | ||
TEST_BITCOIN_BLOCKCHAIN_API, | ||
TEST_BITCOIN_BLOCKCHAIN_FEE_RECOMMENDATION_API | ||
); | ||
}); | ||
|
||
it('should create a funding transaction', async () => { | ||
fundingTransaction = await dlcHandler.createFundingPSBT( | ||
TEST_VAULT, | ||
TEST_REGTEST_ATTESTOR_EXTENDED_GROUP_PUBLIC_KEY | ||
); | ||
|
||
const vaultAmount = TEST_VAULT.valueLocked.toBigInt(); | ||
const feeAmount = vaultAmount / TEST_VAULT.btcMintFeeBasisPoints.toBigInt(); | ||
|
||
const feeRecipientScript = p2wpkh( | ||
Buffer.from(TEST_VAULT.btcFeeRecipient, 'hex'), | ||
TEST_BITCOIN_NETWORK | ||
).script; | ||
const multisigScript = dlcHandler.payment?.taprootMultisigPayment.script; | ||
|
||
const outputs = Array.from({ length: fundingTransaction.outputsLength }, (_, index) => | ||
fundingTransaction.getOutput(index) | ||
); | ||
|
||
const multisigOutput = outputs.find( | ||
output => output.script?.toString() === multisigScript?.toString() | ||
); | ||
const feeOutput = outputs.find( | ||
output => output.script?.toString() === feeRecipientScript.toString() | ||
); | ||
|
||
expect(fundingTransaction).toBeDefined(); | ||
expect(multisigOutput?.amount === vaultAmount).toBeTruthy(); | ||
expect(feeOutput?.amount === feeAmount).toBeTruthy(); | ||
}); | ||
|
||
it('should sign a funding transaction', async () => { | ||
signedFundingTransaction = dlcHandler.signPSBT(fundingTransaction, 'funding'); | ||
|
||
expect(signedFundingTransaction.isFinal).toBeTruthy(); | ||
}); | ||
|
||
it('should create a closing transaction', async () => { | ||
closingTransaction = await dlcHandler.createClosingPSBT( | ||
TEST_VAULT, | ||
signedFundingTransaction.id | ||
); | ||
|
||
const vaultAmount = TEST_VAULT.valueLocked.toBigInt(); | ||
const feeAmount = vaultAmount / TEST_VAULT.btcMintFeeBasisPoints.toBigInt(); | ||
|
||
const feeRecipientScript = p2wpkh( | ||
Buffer.from(TEST_VAULT.btcFeeRecipient, 'hex'), | ||
TEST_BITCOIN_NETWORK | ||
).script; | ||
const userScript = dlcHandler.payment?.nativeSegwitPayment.script; | ||
|
||
const outputs = Array.from({ length: fundingTransaction.outputsLength }, (_, index) => | ||
fundingTransaction.getOutput(index) | ||
); | ||
|
||
const userOutput = outputs.find(output => output.script?.toString() === userScript?.toString()); | ||
const feeOutput = outputs.find( | ||
output => output.script?.toString() === feeRecipientScript.toString() | ||
); | ||
|
||
expect(closingTransaction).toBeDefined(); | ||
expect(userOutput).toBeDefined(); | ||
expect(feeOutput?.amount === feeAmount).toBeTruthy(); | ||
expect( | ||
closingTransaction.getInput(0).witnessUtxo?.script.toString() == | ||
dlcHandler.payment?.taprootMultisigPayment.script.toString() | ||
).toBeTruthy(); | ||
}); | ||
|
||
it('should sign a closing transaction', async () => { | ||
partiallySignedClosingTransaction = dlcHandler.signPSBT(closingTransaction, 'closing'); | ||
|
||
expect(closingTransaction.isFinal).toBeFalsy(); | ||
expect(partiallySignedClosingTransaction).toBeDefined(); | ||
}); | ||
}); |
Oops, something went wrong.