diff --git a/system-tests/package.json b/system-tests/package.json index cef56ce98..e5ba6c709 100644 --- a/system-tests/package.json +++ b/system-tests/package.json @@ -18,7 +18,7 @@ "test/**/*" ], "dependencies": { - "@keep-network/tbtc-v2.ts": "2.0.0-dev.0", + "@keep-network/tbtc-v2.ts": "2.1.0-dev.0", "elliptic": "^6.5.4", "wif": "^2.0.6" }, diff --git a/system-tests/test/deposit-redemption.test.ts b/system-tests/test/deposit-redemption.test.ts index c5052fef7..f89994552 100644 --- a/system-tests/test/deposit-redemption.test.ts +++ b/system-tests/test/deposit-redemption.test.ts @@ -1,29 +1,36 @@ import { - TBTC, - SpvMaintainer, + extractBitcoinRawTxVectors, + Hex, + BitcoinTxHash, + BitcoinNetwork, ElectrumClient, EthereumBridge, - BitcoinTransactionHash, - BitcoinNetwork, -} from "@keep-network/tbtc-v2.ts/dist/src" -import { computeHash160, createAddressFromPublicKey } from "@keep-network/tbtc-v2.ts/dist/src/bitcoin" -import { Hex } from "@keep-network/tbtc-v2.ts/dist/src/hex" + EthereumTBTCToken, + EthereumTBTCVault, + DepositFunding, + DepositScript, + BitcoinAddressConverter, + WalletTx, + EthereumWalletRegistry, + MaintenanceService, + BitcoinHashUtils, + EthereumAddress, +} from "@keep-network/tbtc-v2.ts" import { BigNumber, constants, Contract } from "ethers" import chai, { expect } from "chai" -import { submitDepositTransaction } from "@keep-network/tbtc-v2.ts/dist/src/deposit" -import { submitDepositSweepTransaction } from "@keep-network/tbtc-v2.ts/dist/src/deposit-sweep" -import { submitRedemptionTransaction } from "@keep-network/tbtc-v2.ts/dist/src/redemption" import chaiAsPromised from "chai-as-promised" import { setupSystemTestsContext } from "./utils/context" -import { generateDeposit } from "./utils/deposit" +import { createDepositReceipt } from "./utils/deposit" import { fakeRelayDifficulty, waitTransactionConfirmed } from "./utils/bitcoin" -import type { UnspentTransactionOutput } from "@keep-network/tbtc-v2.ts/dist/src/bitcoin" +import type { + RedemptionRequest, + BitcoinUtxo, + DepositReceipt, + TBTCContracts, +} from "@keep-network/tbtc-v2.ts" import type { SystemTestsContext } from "./utils/context" -import type { RedemptionRequest } from "@keep-network/tbtc-v2.ts/dist/src/redemption" -import type { Deposit } from "@keep-network/tbtc-v2.ts/dist/src/deposit" -import { TBTCToken } from "@keep-network/tbtc-v2.ts/dist/src/ethereum" chai.use(chaiAsPromised) @@ -52,9 +59,17 @@ describe("System Test - Deposit and redemption", () => { let tbtcTokenAddress: string let bridgeAddress: string let vaultAddress: string - let tbtcTokenHandle: TBTCToken + let walletRegistryAddress: string + let tbtcTokenHandle: EthereumTBTCToken + let vaultHandle: EthereumTBTCVault let maintainerBridgeHandle: EthereumBridge let depositorBridgeHandle: EthereumBridge + let walletRegistryHandle: EthereumWalletRegistry + let tbtcContracts: TBTCContracts + + let walletTx: WalletTx + let maintenanceService: MaintenanceService + let bank: Contract let relay: Contract let tbtc: Contract @@ -70,10 +85,10 @@ describe("System Test - Deposit and redemption", () => { // Multiplier to convert satoshi to TBTC token units. const SATOSHI_MULTIPLIER = BigNumber.from(10000000000) - let deposit: Deposit - let depositUtxo: UnspentTransactionOutput - let sweepUtxo: UnspentTransactionOutput - let redemptionUtxo: UnspentTransactionOutput | undefined + let depositReceipt: DepositReceipt + let depositUtxo: BitcoinUtxo + let sweepUtxo: BitcoinUtxo + let redemptionUtxo: BitcoinUtxo | undefined before(async () => { systemTestsContext = await setupSystemTestsContext() @@ -90,12 +105,18 @@ describe("System Test - Deposit and redemption", () => { tbtcTokenAddress = deployedContracts.TBTC.address bridgeAddress = deployedContracts.Bridge.address vaultAddress = deployedContracts.TBTCVault.address + walletRegistryAddress = deployedContracts.WalletRegistry.address - tbtcTokenHandle = new TBTCToken({ + tbtcTokenHandle = new EthereumTBTCToken({ address: tbtcTokenAddress, signerOrProvider: depositor, }) + vaultHandle = new EthereumTBTCVault({ + address: vaultAddress, + signerOrProvider: depositor, + }) + maintainerBridgeHandle = new EthereumBridge({ address: bridgeAddress, signerOrProvider: maintainer, @@ -106,6 +127,22 @@ describe("System Test - Deposit and redemption", () => { signerOrProvider: depositor, }) + walletRegistryHandle = new EthereumWalletRegistry({ + address: walletRegistryAddress, + signerOrProvider: depositor, + }) + + tbtcContracts = { + bridge: maintainerBridgeHandle, + tbtcToken: tbtcTokenHandle, + tbtcVault: vaultHandle, + walletRegistry: walletRegistryHandle, + } + + walletTx = new WalletTx(tbtcContracts, electrumClient) + + maintenanceService = new MaintenanceService(tbtcContracts, electrumClient) + const bankDeploymentInfo = deployedContracts.Bank bank = new Contract( bankDeploymentInfo.address, @@ -130,32 +167,39 @@ describe("System Test - Deposit and redemption", () => { context("when deposit is made and revealed without a vault", () => { before("make and reveal deposit", async () => { - deposit = generateDeposit( + depositReceipt = createDepositReceipt( systemTestsContext.depositor.address, - depositAmount, systemTestsContext.walletBitcoinKeyPair.publicKey.compressed ) console.log(` Generated deposit data: - ${JSON.stringify(deposit)} + ${JSON.stringify(depositReceipt)} `) - const depositorBitcoinAddress = createAddressFromPublicKey( - Hex.from(systemTestsContext.depositorBitcoinKeyPair.publicKey.compressed), - BitcoinNetwork.Testnet, - ) - const depositorUtxos = await electrumClient.findAllUnspentTransactionOutputs( - depositorBitcoinAddress + const depositorBitcoinAddress = + BitcoinAddressConverter.publicKeyToAddress( + Hex.from( + systemTestsContext.depositorBitcoinKeyPair.publicKey.compressed + ), + BitcoinNetwork.Testnet + ) + + const depositorUtxos = + await electrumClient.findAllUnspentTransactionOutputs( + depositorBitcoinAddress + ) + + const depositFunding = DepositFunding.fromScript( + DepositScript.fromReceipt(depositReceipt, true) ) - ;({ depositUtxo } = await submitDepositTransaction( - deposit, - systemTestsContext.depositorBitcoinKeyPair.wif, - electrumClient, - true, + ;({ depositUtxo } = await depositFunding.submitTransaction( + depositAmount, depositorUtxos, - depositTxFee + depositTxFee, + systemTestsContext.depositorBitcoinKeyPair.wif, + electrumClient )) console.log(` @@ -167,11 +211,16 @@ describe("System Test - Deposit and redemption", () => { // Since the reveal deposit logic does not perform SPV proof, we // can reveal the deposit transaction immediately without waiting // for confirmations. - await TBTC.revealDeposit( - depositUtxo, - deposit, - electrumClient, - depositorBridgeHandle + const rawDepositTransaction = await electrumClient.getRawTransaction( + depositUtxo.transactionHash + ) + const depositRawTxVectors = extractBitcoinRawTxVectors( + rawDepositTransaction + ) + depositorBridgeHandle.revealDeposit( + depositRawTxVectors, + depositUtxo.outputIndex, + depositReceipt ) console.log(` @@ -187,23 +236,22 @@ describe("System Test - Deposit and redemption", () => { }) it("should reveal the deposit to the bridge", async () => { - const { revealedAt } = await TBTC.getRevealedDeposit( - depositUtxo, - maintainerBridgeHandle + const { revealedAt } = await maintainerBridgeHandle.deposits( + depositUtxo.transactionHash, + depositUtxo.outputIndex ) expect(revealedAt).to.be.greaterThan(0) }) context("when deposit is swept and sweep proof submitted", () => { before("sweep the deposit and submit sweep proof", async () => { - ;({ newMainUtxo: sweepUtxo } = await submitDepositSweepTransaction( - electrumClient, - depositSweepTxFee, - systemTestsContext.walletBitcoinKeyPair.wif, - true, - [depositUtxo], - [deposit] - )) + ;({ newMainUtxo: sweepUtxo } = + await walletTx.depositSweep.submitTransaction( + depositSweepTxFee, + systemTestsContext.walletBitcoinKeyPair.wif, + [depositUtxo], + [depositReceipt] + )) console.log(` Deposit swept on Bitcoin chain: @@ -227,17 +275,14 @@ describe("System Test - Deposit and redemption", () => { // TODO: Consider fetching the current wallet main UTXO and passing it // here. This will allow running this test scenario multiple // times for the same wallet. - await SpvMaintainer.submitDepositSweepProof( + await maintenanceService.spv.submitDepositSweepProof( sweepUtxo.transactionHash, - // This is the first sweep of the given wallet so there is no main UTXO. { // The function expects an unprefixed hash. - transactionHash: BitcoinTransactionHash.from(constants.HashZero), + transactionHash: BitcoinTxHash.from(constants.HashZero), outputIndex: 0, value: BigNumber.from(0), - }, - maintainerBridgeHandle, - electrumClient + } ) console.log(` @@ -253,17 +298,17 @@ describe("System Test - Deposit and redemption", () => { }) it("should sweep the deposit on the bridge", async () => { - const { sweptAt } = await TBTC.getRevealedDeposit( - depositUtxo, - maintainerBridgeHandle + const { sweptAt } = await maintainerBridgeHandle.deposits( + depositUtxo.transactionHash, + depositUtxo.outputIndex ) expect(sweptAt).to.be.greaterThan(0) }) it("should increase depositor's balance in the bank", async () => { - const { treasuryFee } = await TBTC.getRevealedDeposit( - depositUtxo, - maintainerBridgeHandle + const { treasuryFee } = await maintainerBridgeHandle.deposits( + depositUtxo.transactionHash, + depositUtxo.outputIndex ) const expectedBalance = depositAmount @@ -294,7 +339,7 @@ describe("System Test - Deposit and redemption", () => { .approveBalance(bridgeAddress, requestedAmount) // Request redemption to depositor's address. - redeemerOutputScript = `0014${computeHash160( + redeemerOutputScript = `0014${BitcoinHashUtils.computeHash160( systemTestsContext.depositorBitcoinKeyPair.publicKey.compressed )}` @@ -302,18 +347,16 @@ describe("System Test - Deposit and redemption", () => { systemTestsContext.walletBitcoinKeyPair.publicKey.compressed, sweepUtxo, redeemerOutputScript, - requestedAmount, + requestedAmount ) console.log( `Requested redemption of amount ${requestedAmount} to script ${redeemerOutputScript} on the bridge` ) - redemptionRequest = await TBTC.getRedemptionRequest( + redemptionRequest = await maintainerBridgeHandle.pendingRedemptions( systemTestsContext.walletBitcoinKeyPair.publicKey.compressed, - redeemerOutputScript, - "pending", - maintainerBridgeHandle + redeemerOutputScript ) }) @@ -338,20 +381,19 @@ describe("System Test - Deposit and redemption", () => { context( "when redemption is made and redemption proof submitted", () => { - let redemptionTxHash: BitcoinTransactionHash + let redemptionTxHash: BitcoinTxHash before( "make the redemption and submit redemption proof", async () => { - ;({ transactionHash: redemptionTxHash, newMainUtxo: redemptionUtxo } = - await submitRedemptionTransaction( - electrumClient, - maintainerBridgeHandle, - systemTestsContext.walletBitcoinKeyPair.wif, - sweepUtxo, - [redemptionRequest.redeemerOutputScript], - true - )) + ;({ + transactionHash: redemptionTxHash, + newMainUtxo: redemptionUtxo, + } = await walletTx.redemption.submitTransaction( + systemTestsContext.walletBitcoinKeyPair.wif, + sweepUtxo, + [redemptionRequest.redeemerOutputScript] + )) console.log( "Redemption made on Bitcoin chain:\n" + @@ -366,12 +408,10 @@ describe("System Test - Deposit and redemption", () => { redemptionTxHash ) - await SpvMaintainer.submitRedemptionProof( + await maintenanceService.spv.submitRedemptionProof( redemptionTxHash, sweepUtxo, - systemTestsContext.walletBitcoinKeyPair.publicKey.compressed, - maintainerBridgeHandle, - electrumClient + systemTestsContext.walletBitcoinKeyPair.publicKey.compressed ) console.log("Redemption proved on the bridge") @@ -387,11 +427,9 @@ describe("System Test - Deposit and redemption", () => { it("should close the redemption request on the bridge", async () => { await expect( - TBTC.getRedemptionRequest( + maintainerBridgeHandle.pendingRedemptions( systemTestsContext.walletBitcoinKeyPair.publicKey.compressed, - redemptionRequest.redeemerOutputScript, - "pending", - maintainerBridgeHandle + redemptionRequest.redeemerOutputScript ) ).to.be.rejectedWith( "Provided redeemer output script and wallet public key do not identify a redemption request" @@ -411,33 +449,38 @@ describe("System Test - Deposit and redemption", () => { context("when deposit is made and revealed with a vault", () => { before("make and reveal deposit", async () => { - deposit = generateDeposit( + depositReceipt = createDepositReceipt( systemTestsContext.depositor.address, - depositAmount, - systemTestsContext.walletBitcoinKeyPair.publicKey.compressed, - vaultAddress, + systemTestsContext.walletBitcoinKeyPair.publicKey.compressed ) console.log(` Generated deposit data: - ${JSON.stringify(deposit)} + ${JSON.stringify(depositReceipt)} `) - const depositorBitcoinAddress = createAddressFromPublicKey( - Hex.from(systemTestsContext.depositorBitcoinKeyPair.publicKey.compressed), - BitcoinNetwork.Testnet, - ) - const depositorUtxos = await electrumClient.findAllUnspentTransactionOutputs( - depositorBitcoinAddress + const depositorBitcoinAddress = + BitcoinAddressConverter.publicKeyToAddress( + Hex.from( + systemTestsContext.depositorBitcoinKeyPair.publicKey.compressed + ), + BitcoinNetwork.Testnet + ) + const depositorUtxos = + await electrumClient.findAllUnspentTransactionOutputs( + depositorBitcoinAddress + ) + + const depositFunding = DepositFunding.fromScript( + DepositScript.fromReceipt(depositReceipt, true) ) - ;({ depositUtxo } = await submitDepositTransaction( - deposit, - systemTestsContext.depositorBitcoinKeyPair.wif, - electrumClient, - true, + ;({ depositUtxo } = await depositFunding.submitTransaction( + depositAmount, depositorUtxos, - depositTxFee + depositTxFee, + systemTestsContext.depositorBitcoinKeyPair.wif, + electrumClient )) console.log(` @@ -449,12 +492,17 @@ describe("System Test - Deposit and redemption", () => { // Since the reveal deposit logic does not perform SPV proof, we // can reveal the deposit transaction immediately without waiting // for confirmations. - await TBTC.revealDeposit( - depositUtxo, - deposit, - electrumClient, - depositorBridgeHandle, - deposit.vault, + const rawDepositTransaction = await electrumClient.getRawTransaction( + depositUtxo.transactionHash + ) + const depositRawTxVectors = extractBitcoinRawTxVectors( + rawDepositTransaction + ) + depositorBridgeHandle.revealDeposit( + depositRawTxVectors, + depositUtxo.outputIndex, + depositReceipt, + EthereumAddress.from(vaultAddress) ) console.log(` @@ -470,24 +518,23 @@ describe("System Test - Deposit and redemption", () => { }) it("should reveal the deposit to the bridge", async () => { - const { revealedAt } = await TBTC.getRevealedDeposit( - depositUtxo, - maintainerBridgeHandle + const { revealedAt } = await maintainerBridgeHandle.deposits( + depositUtxo.transactionHash, + depositUtxo.outputIndex ) expect(revealedAt).to.be.greaterThan(0) }) context("when deposit is swept and sweep proof submitted", () => { before("sweep the deposit and submit sweep proof", async () => { - ;({ newMainUtxo: sweepUtxo } = await submitDepositSweepTransaction( - electrumClient, - depositSweepTxFee, - systemTestsContext.walletBitcoinKeyPair.wif, - true, - [depositUtxo], - [deposit], - redemptionUtxo // The UTXO from the previous test became the new main UTXO. - )) + ;({ newMainUtxo: sweepUtxo } = + await walletTx.depositSweep.submitTransaction( + depositSweepTxFee, + systemTestsContext.walletBitcoinKeyPair.wif, + [depositUtxo], + [depositReceipt], + redemptionUtxo // The UTXO from the previous test became the new main UTXO. + )) console.log(` Deposit swept on Bitcoin chain: @@ -510,17 +557,15 @@ describe("System Test - Deposit and redemption", () => { // If the redemption transaction from the previous test created a new // main UTXO, use it. Otherwise call it with a zero-filled main UTXO. - const mainUtxo = redemptionUtxo ? redemptionUtxo : { - transactionHash: BitcoinTransactionHash.from(constants.HashZero), + const mainUtxo = redemptionUtxo || { + transactionHash: BitcoinTxHash.from(constants.HashZero), outputIndex: 0, value: BigNumber.from(0), - }; - await SpvMaintainer.submitDepositSweepProof( + } + await maintenanceService.spv.submitDepositSweepProof( sweepUtxo.transactionHash, mainUtxo, - maintainerBridgeHandle, - electrumClient, - deposit.vault, + EthereumAddress.from(vaultAddress) ) console.log(` @@ -536,34 +581,32 @@ describe("System Test - Deposit and redemption", () => { }) it("should sweep the deposit on the bridge", async () => { - const { sweptAt } = await TBTC.getRevealedDeposit( - depositUtxo, - maintainerBridgeHandle + const { sweptAt } = await maintainerBridgeHandle.deposits( + depositUtxo.transactionHash, + depositUtxo.outputIndex ) expect(sweptAt).to.be.greaterThan(0) }) it("should increase vault's balance in the bank", async () => { - const { treasuryFee } = await TBTC.getRevealedDeposit( - depositUtxo, - maintainerBridgeHandle + const { treasuryFee } = await maintainerBridgeHandle.deposits( + depositUtxo.transactionHash, + depositUtxo.outputIndex ) const expectedBalance = depositAmount .sub(treasuryFee) .sub(depositSweepTxFee) - const actualBalance = await bank.balanceOf( - vaultAddress - ) + const actualBalance = await bank.balanceOf(vaultAddress) expect(actualBalance).to.be.equal(expectedBalance) }) it("should mint TBTC tokens for the depositor", async () => { - const { treasuryFee } = await TBTC.getRevealedDeposit( - depositUtxo, - maintainerBridgeHandle + const { treasuryFee } = await maintainerBridgeHandle.deposits( + depositUtxo.transactionHash, + depositUtxo.outputIndex ) const balanceInSatoshis = depositAmount @@ -594,27 +637,24 @@ describe("System Test - Deposit and redemption", () => { requestedAmount = tbtcBalanceOfDepositor.div(SATOSHI_MULTIPLIER) // Request redemption to depositor's address. - redeemerOutputScript = `0014${computeHash160( + redeemerOutputScript = `0014${BitcoinHashUtils.computeHash160( systemTestsContext.depositorBitcoinKeyPair.publicKey.compressed )}` - await TBTC.requestRedemption( + await depositorBridgeHandle.requestRedemption( systemTestsContext.walletBitcoinKeyPair.publicKey.compressed, sweepUtxo, redeemerOutputScript, - tbtcBalanceOfDepositor, - tbtcTokenHandle, + tbtcBalanceOfDepositor ) console.log( `Requested redemption of ${tbtcBalanceOfDepositor} TBTC tokens to script ${redeemerOutputScript} on the bridge` ) - redemptionRequest = await TBTC.getRedemptionRequest( + redemptionRequest = await maintainerBridgeHandle.pendingRedemptions( systemTestsContext.walletBitcoinKeyPair.publicKey.compressed, - redeemerOutputScript, - "pending", - maintainerBridgeHandle + redeemerOutputScript ) }) @@ -645,20 +685,19 @@ describe("System Test - Deposit and redemption", () => { context( "when redemption is made and redemption proof submitted", () => { - let redemptionTxHash: BitcoinTransactionHash + let redemptionTxHash: BitcoinTxHash before( "make the redemption and submit redemption proof", async () => { - ;({ transactionHash: redemptionTxHash } = - await submitRedemptionTransaction( - electrumClient, - maintainerBridgeHandle, - systemTestsContext.walletBitcoinKeyPair.wif, - sweepUtxo, - [redemptionRequest.redeemerOutputScript], - true - )) + ;({ + transactionHash: redemptionTxHash, + newMainUtxo: redemptionUtxo, + } = await walletTx.redemption.submitTransaction( + systemTestsContext.walletBitcoinKeyPair.wif, + sweepUtxo, + [redemptionRequest.redeemerOutputScript] + )) console.log( "Redemption made on Bitcoin chain:\n" + @@ -673,12 +712,10 @@ describe("System Test - Deposit and redemption", () => { redemptionTxHash ) - await SpvMaintainer.submitRedemptionProof( + await maintenanceService.spv.submitRedemptionProof( redemptionTxHash, sweepUtxo, - systemTestsContext.walletBitcoinKeyPair.publicKey.compressed, - maintainerBridgeHandle, - electrumClient + systemTestsContext.walletBitcoinKeyPair.publicKey.compressed ) console.log("Redemption proved on the bridge") @@ -694,11 +731,9 @@ describe("System Test - Deposit and redemption", () => { it("should close the redemption request on the bridge", async () => { await expect( - TBTC.getRedemptionRequest( + maintainerBridgeHandle.pendingRedemptions( systemTestsContext.walletBitcoinKeyPair.publicKey.compressed, - redemptionRequest.redeemerOutputScript, - "pending", - maintainerBridgeHandle + redemptionRequest.redeemerOutputScript ) ).to.be.rejectedWith( "Provided redeemer output script and wallet public key do not identify a redemption request" diff --git a/system-tests/test/utils/bitcoin.ts b/system-tests/test/utils/bitcoin.ts index 5df7ded63..a81126fa9 100644 --- a/system-tests/test/utils/bitcoin.ts +++ b/system-tests/test/utils/bitcoin.ts @@ -2,11 +2,10 @@ // @ts-ignore import wifLib from "wif" import { ec as EllipticCurve } from "elliptic" -import { assembleTransactionProof } from "@keep-network/tbtc-v2.ts/dist/src/proof" +import { assembleBitcoinSpvProof } from "@keep-network/tbtc-v2.ts" +import type { BitcoinTxHash, ElectrumClient } from "@keep-network/tbtc-v2.ts" import type { Contract } from "ethers" -import type { Client as BitcoinClient } from "@keep-network/tbtc-v2.ts/dist/src/bitcoin" -import type { BitcoinTransactionHash } from "@keep-network/tbtc-v2.ts/dist/src" /** * Elliptic curve used by Bitcoin. @@ -74,8 +73,8 @@ export function keyPairFromWif(wif: string): KeyPair { * @returns Empty promise. */ export async function waitTransactionConfirmed( - bitcoinClient: BitcoinClient, - transactionHash: BitcoinTransactionHash, + bitcoinClient: ElectrumClient, + transactionHash: BitcoinTxHash, requiredConfirmations: number = defaultTxProofDifficultyFactor, sleep = 30000 ): Promise { @@ -94,7 +93,7 @@ export async function waitTransactionConfirmed( if (confirmations >= requiredConfirmations) { console.log(` - Transaction ${transactionHash} has enough confirmations. + Transaction ${transactionHash} has enough confirmations. `) return } @@ -125,11 +124,11 @@ export async function waitTransactionConfirmed( */ export async function fakeRelayDifficulty( relay: Contract, - bitcoinClient: BitcoinClient, - transactionHash: BitcoinTransactionHash, + bitcoinClient: ElectrumClient, + transactionHash: BitcoinTxHash, headerChainLength: number = defaultTxProofDifficultyFactor ): Promise { - const proof = await assembleTransactionProof( + const proof = await assembleBitcoinSpvProof( transactionHash, headerChainLength, bitcoinClient diff --git a/system-tests/test/utils/deposit.ts b/system-tests/test/utils/deposit.ts index 054fba9cc..5510e9524 100644 --- a/system-tests/test/utils/deposit.ts +++ b/system-tests/test/utils/deposit.ts @@ -1,12 +1,12 @@ import crypto from "crypto" -import { calculateDepositRefundLocktime } from "@keep-network/tbtc-v2.ts/dist/src/deposit" -import { EthereumAddress } from "@keep-network/tbtc-v2.ts/dist/src" -import { constants } from "ethers" -import { computeHash160 } from "@keep-network/tbtc-v2.ts/dist/src/bitcoin" +import { + BitcoinLocktimeUtils, + BitcoinHashUtils, + EthereumAddress, +} from "@keep-network/tbtc-v2.ts" -import type { BigNumber } from "ethers" -import type { Deposit } from "@keep-network/tbtc-v2.ts/dist/src/deposit" +import type { DepositReceipt } from "@keep-network/tbtc-v2.ts" /** * Default refund public key used for deposits. Their corresponding private key: @@ -16,47 +16,37 @@ export const DEFAULT_REFUND_PUBLIC_KEY = "03989d253b17a6a0f41838b84ff0d20e8898f9d7b1a98f2564da4cc29dcf8581d9" /** - * Generates a deposit object based on the given parameters. + * Creates a deposit receipt based on the given parameters. * @param depositorAddress Ethereum address of the depositor. - * @param amount Amount of the deposit in satoshi. * @param walletPublicKey Compressed ECDSA public key of the target wallet. - * @param vaultAddress Ethereum address of the Bank vault to which the deposit - * is routed to. Optional parameter, if not set the zero address is used. * @param refundPublicKey Compressed ECDSA public key that can be used for * refund. Optional parameter, default value is used if not set * @see {DEFAULT_REFUND_PUBLIC_KEY}. - * @returns Deposit object. + * @returns Deposit receipt. */ -export function generateDeposit( +export function createDepositReceipt( depositorAddress: string, - amount: BigNumber, walletPublicKey: string, - vaultAddress?: string, refundPublicKey?: string -): Deposit { - const blindingFactor = crypto.randomBytes(8).toString("hex") - - const walletPublicKeyHash = computeHash160(walletPublicKey) +): DepositReceipt { + const walletPublicKeyHash = BitcoinHashUtils.computeHash160(walletPublicKey) const resolvedRefundPublicKey = refundPublicKey || DEFAULT_REFUND_PUBLIC_KEY - const refundPublicKeyHash = computeHash160(resolvedRefundPublicKey) + const refundPublicKeyHash = BitcoinHashUtils.computeHash160( + resolvedRefundPublicKey + ) + + const blindingFactor = crypto.randomBytes(8).toString("hex") - const refundLocktime = calculateDepositRefundLocktime( + const refundLocktime = BitcoinLocktimeUtils.calculateLocktime( Math.floor(Date.now() / 1000), 2592000 // 30 days ) - - const resolvedVaultAddress = new EthereumAddress( - vaultAddress || constants.AddressZero - ) - return { - depositor: new EthereumAddress(depositorAddress), - amount, - blindingFactor, + depositor: EthereumAddress.from(depositorAddress), walletPublicKeyHash, refundPublicKeyHash, + blindingFactor, refundLocktime, - vault: resolvedVaultAddress, } } diff --git a/system-tests/yarn.lock b/system-tests/yarn.lock index 2b4efbd6b..070844d6e 100644 --- a/system-tests/yarn.lock +++ b/system-tests/yarn.lock @@ -1515,18 +1515,7 @@ resolved "https://registry.yarnpkg.com/@keep-network/bitcoin-spv-sol/-/bitcoin-spv-sol-3.4.0-solc-0.8.tgz#8b44c246ffab8ea993efe196f6bf385b1a3b84dc" integrity sha512-KlpY9BbasyLvYXSS7dsJktgRChu/yjdFLOX8ldGA/pltLicCm/l0F4oqxL8wSws9XD12vq9x0B5qzPygVLB2TQ== -"@keep-network/ecdsa@2.1.0-dev.15": - version "2.1.0-dev.15" - resolved "https://registry.yarnpkg.com/@keep-network/ecdsa/-/ecdsa-2.1.0-dev.15.tgz#ee631a42e165f30c75aae8c54aace765b77e272a" - integrity sha512-iUE3SwDSNc/k1oui7Z+fDGhhGyOzpe4/f/oKvDUMHqXx0BQG3QCrOz9KqWuPFXTXMav4LxLbt12WyDITAl/hjw== - dependencies: - "@keep-network/random-beacon" "2.1.0-dev.15" - "@keep-network/sortition-pools" "^2.0.0-pre.16" - "@openzeppelin/contracts" "^4.6.0" - "@openzeppelin/contracts-upgradeable" "^4.6.0" - "@threshold-network/solidity-contracts" "1.3.0-dev.6" - -"@keep-network/ecdsa@2.1.0-dev.16": +"@keep-network/ecdsa@2.1.0-dev.16", "@keep-network/ecdsa@development": version "2.1.0-dev.16" resolved "https://registry.yarnpkg.com/@keep-network/ecdsa/-/ecdsa-2.1.0-dev.16.tgz#bd9d084b4f2b7bdda0a85a8f69200832d7c24fde" integrity sha512-8k8XiPXneRtBN/4jVX+0qxqya8232cglRM/GrfYMv/niQkgvdolK2prXyd3LO7d4ot4WcyfLYU7uJfjjmc5rEw== @@ -1572,16 +1561,6 @@ version "0.0.1" resolved "https://codeload.github.com/keep-network/prettier-config-keep/tar.gz/a1a333e7ac49928a0f6ed39421906dd1e46ab0f3" -"@keep-network/random-beacon@2.1.0-dev.15": - version "2.1.0-dev.15" - resolved "https://registry.yarnpkg.com/@keep-network/random-beacon/-/random-beacon-2.1.0-dev.15.tgz#541620c469e3bc75a5d1f7649889540b0e032e9e" - integrity sha512-vxBICRtmqSmJtFU5hZMpwB0alhgKchyMbxk4DtLZ7T2zBjd5tjt3CqeKEk+ON09g7yL1mIxY07InP4okviUK4A== - dependencies: - "@keep-network/sortition-pools" "^2.0.0-pre.16" - "@openzeppelin/contracts" "4.7.3" - "@thesis/solidity-contracts" "github:thesis/solidity-contracts#4985bcf" - "@threshold-network/solidity-contracts" "1.3.0-dev.5" - "@keep-network/random-beacon@2.1.0-dev.16": version "2.1.0-dev.16" resolved "https://registry.yarnpkg.com/@keep-network/random-beacon/-/random-beacon-2.1.0-dev.16.tgz#9f2b5c19aa79f6ff1a5498ba7b55eb170463161d" @@ -1592,6 +1571,16 @@ "@thesis/solidity-contracts" "github:thesis/solidity-contracts#4985bcf" "@threshold-network/solidity-contracts" "1.3.0-dev.6" +"@keep-network/random-beacon@2.1.0-dev.17": + version "2.1.0-dev.17" + resolved "https://registry.yarnpkg.com/@keep-network/random-beacon/-/random-beacon-2.1.0-dev.17.tgz#5fb2621948aa2fe07ceb134ba76f737b7e6d85cd" + integrity sha512-alfd2sHdMrX15qKzM4zwkZ3l/CXboLoeos4l3WvChW978VJIwUPm2ZIXd8tNTaHlykQ57eSSX7esaLfIjeO3Kg== + dependencies: + "@keep-network/sortition-pools" "^2.0.0-pre.16" + "@openzeppelin/contracts" "4.7.3" + "@thesis/solidity-contracts" "github:thesis/solidity-contracts#4985bcf" + "@threshold-network/solidity-contracts" "1.3.0-dev.8" + "@keep-network/sortition-pools@1.2.0-dev.1": version "1.2.0-dev.1" resolved "https://registry.yarnpkg.com/@keep-network/sortition-pools/-/sortition-pools-1.2.0-dev.1.tgz#2ee371f1dd1ff71f6d05c9ddc2a83a4a93ff56b3" @@ -1607,13 +1596,13 @@ "@openzeppelin/contracts" "^4.3.2" "@thesis/solidity-contracts" "github:thesis/solidity-contracts#4985bcf" -"@keep-network/tbtc-v2.ts@2.0.0-dev.0": - version "2.0.0-dev.0" - resolved "https://registry.yarnpkg.com/@keep-network/tbtc-v2.ts/-/tbtc-v2.ts-2.0.0-dev.0.tgz#1e51d8b605a8c96c3bea9522595d3b3db4eb8214" - integrity sha512-kJJCiWTu0EoZW1Q2zQZ5fOM52PpXDPd7EHd/oTbETxKYIW7h/Qg/Fnqx9KNynIInMrQIdXwsIDTEjoxGmD1AiQ== +"@keep-network/tbtc-v2.ts@2.1.0-dev.0": + version "2.1.0-dev.0" + resolved "https://registry.yarnpkg.com/@keep-network/tbtc-v2.ts/-/tbtc-v2.ts-2.1.0-dev.0.tgz#6eadb36784fdb9b79e0fd360519af0bf18cf487c" + integrity sha512-VZmBX1SZaDD9KRfkIL3O9pGDqa7k89SM3fGdz3o7AkV/1rBWXOp3CJ4yfe57xV9XwHKac7BJWob9/ApTgD26HA== dependencies: - "@keep-network/ecdsa" "2.1.0-dev.16" - "@keep-network/tbtc-v2" "1.6.0-dev.0" + "@keep-network/ecdsa" development + "@keep-network/tbtc-v2" development bitcoinjs-lib "^6.1.5" bufio "^1.0.6" ecpair "^2.1.0" @@ -1623,14 +1612,14 @@ tiny-secp256k1 "^2.2.3" wif "2.0.6" -"@keep-network/tbtc-v2@1.6.0-dev.0": - version "1.6.0-dev.0" - resolved "https://registry.yarnpkg.com/@keep-network/tbtc-v2/-/tbtc-v2-1.6.0-dev.0.tgz#ba95805cef3f04bde7379d3c3b14e882a9cfa080" - integrity sha512-5N2dMdFSdS+Ljvqnqoscft5xnbIK/U/z8Dc2hNXWULkPhIy0Mx/E7i7I4CpBTV4LazIo1Hq6W4EJtj+lmrekgg== +"@keep-network/tbtc-v2@development": + version "1.6.0-dev.4" + resolved "https://registry.yarnpkg.com/@keep-network/tbtc-v2/-/tbtc-v2-1.6.0-dev.4.tgz#16861d4bccb9da3693170b4de3f747814179eb7f" + integrity sha512-fxbiK6PAsh1TlH/OYwbNdFMIyvjOXpSksOldrgBY3/xnppuoeMFi/nTNrCtRD9kGsnImc5D2TCRj8+vbCHg3gA== dependencies: "@keep-network/bitcoin-spv-sol" "3.4.0-solc-0.8" - "@keep-network/ecdsa" "2.1.0-dev.15" - "@keep-network/random-beacon" "2.1.0-dev.16" + "@keep-network/ecdsa" "2.1.0-dev.16" + "@keep-network/random-beacon" "2.1.0-dev.17" "@keep-network/tbtc" "1.1.2-dev.1" "@openzeppelin/contracts" "^4.8.1" "@openzeppelin/contracts-upgradeable" "^4.8.1" @@ -2289,7 +2278,6 @@ eslint-config-prettier "^8.3.0" eslint-plugin-import "^2.23.4" eslint-plugin-jsx-a11y "^6.4.1" - eslint-plugin-no-only-tests "^2.6.0" eslint-plugin-prettier "^4.0.0" eslint-plugin-react "^7.25.2" eslint-plugin-react-hooks "^4.2.0" @@ -2304,16 +2292,6 @@ dependencies: "@openzeppelin/contracts" "^4.1.0" -"@threshold-network/solidity-contracts@1.3.0-dev.5": - version "1.3.0-dev.5" - resolved "https://registry.yarnpkg.com/@threshold-network/solidity-contracts/-/solidity-contracts-1.3.0-dev.5.tgz#f7a2727d627a10218f0667bc0d33e19ed8f87fdc" - integrity sha512-AInTKQkJ0PKa32q2m8GnZFPYEArsnvOwhIFdBFaHdq9r4EGyqHMf4YY1WjffkheBZ7AQ0DNA8Lst30kBoQd0SA== - dependencies: - "@keep-network/keep-core" ">1.8.1-dev <1.8.1-goerli" - "@openzeppelin/contracts" "~4.5.0" - "@openzeppelin/contracts-upgradeable" "~4.5.2" - "@thesis/solidity-contracts" "github:thesis/solidity-contracts#4985bcf" - "@threshold-network/solidity-contracts@1.3.0-dev.6": version "1.3.0-dev.6" resolved "https://registry.yarnpkg.com/@threshold-network/solidity-contracts/-/solidity-contracts-1.3.0-dev.6.tgz#41e34a84f409f63635e59f9a6ce170df1472b8a1"