Skip to content

Commit

Permalink
Used Hex for system test setup
Browse files Browse the repository at this point in the history
  • Loading branch information
tomaszslabon committed Nov 7, 2023
1 parent 14355ab commit a759c41
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 27 deletions.
54 changes: 37 additions & 17 deletions system-tests/test/deposit-redemption.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ describe("System Test - Deposit and redemption", () => {
let maintainerBridgeHandle: EthereumBridge
let depositorBridgeHandle: EthereumBridge
let walletRegistryHandle: EthereumWalletRegistry
let tbtcContracts: TBTCContracts

let walletTx: WalletTx
let maintenanceService: MaintenanceService
Expand Down Expand Up @@ -132,15 +131,14 @@ describe("System Test - Deposit and redemption", () => {
signerOrProvider: depositor,
})

tbtcContracts = {
const tbtcContracts: TBTCContracts = {
bridge: maintainerBridgeHandle,
tbtcToken: tbtcTokenHandle,
tbtcVault: vaultHandle,
walletRegistry: walletRegistryHandle,
}

walletTx = new WalletTx(tbtcContracts, electrumClient)

maintenanceService = new MaintenanceService(tbtcContracts, electrumClient)

const bankDeploymentInfo = deployedContracts.Bank
Expand Down Expand Up @@ -277,6 +275,7 @@ describe("System Test - Deposit and redemption", () => {
// times for the same wallet.
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: BitcoinTxHash.from(constants.HashZero),
Expand Down Expand Up @@ -340,13 +339,17 @@ describe("System Test - Deposit and redemption", () => {

// Request redemption to depositor's address.
redeemerOutputScript = `0014${BitcoinHashUtils.computeHash160(
systemTestsContext.depositorBitcoinKeyPair.publicKey.compressed
Hex.from(
systemTestsContext.depositorBitcoinKeyPair.publicKey.compressed
)
)}`

await depositorBridgeHandle.requestRedemption(
systemTestsContext.walletBitcoinKeyPair.publicKey.compressed,
Hex.from(
systemTestsContext.walletBitcoinKeyPair.publicKey.compressed
),
sweepUtxo,
redeemerOutputScript,
Hex.from(redeemerOutputScript),
requestedAmount
)

Expand All @@ -355,8 +358,10 @@ describe("System Test - Deposit and redemption", () => {
)

redemptionRequest = await maintainerBridgeHandle.pendingRedemptions(
systemTestsContext.walletBitcoinKeyPair.publicKey.compressed,
redeemerOutputScript
Hex.from(
systemTestsContext.walletBitcoinKeyPair.publicKey.compressed
),
Hex.from(redeemerOutputScript)
)
})

Expand Down Expand Up @@ -411,7 +416,9 @@ describe("System Test - Deposit and redemption", () => {
await maintenanceService.spv.submitRedemptionProof(
redemptionTxHash,
sweepUtxo,
systemTestsContext.walletBitcoinKeyPair.publicKey.compressed
Hex.from(
systemTestsContext.walletBitcoinKeyPair.publicKey.compressed
)
)

console.log("Redemption proved on the bridge")
Expand All @@ -428,7 +435,9 @@ describe("System Test - Deposit and redemption", () => {
it("should close the redemption request on the bridge", async () => {
await expect(
maintainerBridgeHandle.pendingRedemptions(
systemTestsContext.walletBitcoinKeyPair.publicKey.compressed,
Hex.from(
systemTestsContext.walletBitcoinKeyPair.publicKey.compressed
),
redemptionRequest.redeemerOutputScript
)
).to.be.rejectedWith(
Expand Down Expand Up @@ -466,6 +475,7 @@ describe("System Test - Deposit and redemption", () => {
),
BitcoinNetwork.Testnet
)

const depositorUtxos =
await electrumClient.findAllUnspentTransactionOutputs(
depositorBitcoinAddress
Expand Down Expand Up @@ -638,13 +648,17 @@ describe("System Test - Deposit and redemption", () => {

// Request redemption to depositor's address.
redeemerOutputScript = `0014${BitcoinHashUtils.computeHash160(
systemTestsContext.depositorBitcoinKeyPair.publicKey.compressed
Hex.from(
systemTestsContext.depositorBitcoinKeyPair.publicKey.compressed
)
)}`

await depositorBridgeHandle.requestRedemption(
systemTestsContext.walletBitcoinKeyPair.publicKey.compressed,
Hex.from(
systemTestsContext.walletBitcoinKeyPair.publicKey.compressed
),
sweepUtxo,
redeemerOutputScript,
Hex.from(redeemerOutputScript),
tbtcBalanceOfDepositor
)

Expand All @@ -653,8 +667,10 @@ describe("System Test - Deposit and redemption", () => {
)

redemptionRequest = await maintainerBridgeHandle.pendingRedemptions(
systemTestsContext.walletBitcoinKeyPair.publicKey.compressed,
redeemerOutputScript
Hex.from(
systemTestsContext.walletBitcoinKeyPair.publicKey.compressed
),
Hex.from(redeemerOutputScript)
)
})

Expand Down Expand Up @@ -715,7 +731,9 @@ describe("System Test - Deposit and redemption", () => {
await maintenanceService.spv.submitRedemptionProof(
redemptionTxHash,
sweepUtxo,
systemTestsContext.walletBitcoinKeyPair.publicKey.compressed
Hex.from(
systemTestsContext.walletBitcoinKeyPair.publicKey.compressed
)
)

console.log("Redemption proved on the bridge")
Expand All @@ -732,7 +750,9 @@ describe("System Test - Deposit and redemption", () => {
it("should close the redemption request on the bridge", async () => {
await expect(
maintainerBridgeHandle.pendingRedemptions(
systemTestsContext.walletBitcoinKeyPair.publicKey.compressed,
Hex.from(
systemTestsContext.walletBitcoinKeyPair.publicKey.compressed
),
redemptionRequest.redeemerOutputScript
)
).to.be.rejectedWith(
Expand Down
22 changes: 12 additions & 10 deletions system-tests/test/utils/bitcoin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// @ts-ignore
import wifLib from "wif"
import { ec as EllipticCurve } from "elliptic"
import { assembleBitcoinSpvProof } from "@keep-network/tbtc-v2.ts"
import { assembleBitcoinSpvProof, Hex } from "@keep-network/tbtc-v2.ts"

import type { BitcoinTxHash, ElectrumClient } from "@keep-network/tbtc-v2.ts"
import type { Contract } from "ethers"
Expand All @@ -26,21 +26,21 @@ export interface KeyPair {
*/
wif: string
/**
* Private key as an unprefixed hex string.
* Private key.
*/
privateKey: string
privateKey: Hex
/**
* Public key.
*/
publicKey: {
/**
* Compressed public key as a 33-byte hex string prefixed with 02 or 03.
* Compressed 33-byte-long public key prefixed with 02 or 03.
*/
compressed: string
compressed: Hex
/**
* Uncompressed public key as an unprefixed hex string.
* Uncompressed public key.
*/
uncompressed: string
uncompressed: Hex
}
}

Expand All @@ -54,11 +54,13 @@ export function keyPairFromWif(wif: string): KeyPair {
const keyPair = secp256k1.keyFromPrivate(privateKey)
return {
wif,
privateKey: keyPair.getPrivate("hex"),
privateKey: Hex.from(keyPair.getPrivate("hex")),
publicKey: {
compressed: keyPair.getPublic().encodeCompressed("hex"),
compressed: Hex.from(keyPair.getPublic().encodeCompressed("hex")),
// Trim the `04` prefix from the uncompressed key.
uncompressed: keyPair.getPublic().encode("hex", false).substring(2),
uncompressed: Hex.from(
keyPair.getPublic().encode("hex", false).substring(2)
),
},
}
}
Expand Down

0 comments on commit a759c41

Please sign in to comment.