Skip to content

Commit

Permalink
Add unit tests for the DepositService.initiateDepositWithProxy func…
Browse files Browse the repository at this point in the history
…tion
  • Loading branch information
lukasz-zimnoch committed Jan 19, 2024
1 parent bd9756b commit 40a1a9c
Show file tree
Hide file tree
Showing 2 changed files with 175 additions and 1 deletion.
174 changes: 174 additions & 0 deletions typescript/test/services/deposits.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1793,6 +1793,180 @@ describe("Deposits", () => {
})
})
})

describe("initiateDepositWithProxy", () => {
const bitcoinClient = new MockBitcoinClient()
const tbtcContracts = new MockTBTCContracts()
const depositorProxy = new MockDepositorProxy()
let depositService: DepositsService

beforeEach(async () => {
depositService = new DepositsService(tbtcContracts, bitcoinClient)
})

context("when active wallet is not set", () => {
it("should throw", async () => {
await expect(
depositService.initiateDepositWithProxy(
"mjc2zGWypwpNyDi4ZxGbBNnUA84bfgiwYc",
depositorProxy
)
).to.be.rejectedWith("Could not get active wallet public key")
})
})

context("when active wallet is set", () => {
beforeEach(async () => {
tbtcContracts.bridge.setActiveWalletPublicKey(
Hex.from(
"03989d253b17a6a0f41838b84ff0d20e8898f9d7b1a98f2564da4cc29dcf8581d9"
)
)
})

context("when recovery address is incorrect", () => {
it("should throw", async () => {
await expect(
depositService.initiateDepositWithProxy(
"2N5WZpig3vgpSdjSherS2Lv7GnPuxCvkQjT", // p2sh address
depositorProxy
)
).to.be.rejectedWith(
"Bitcoin recovery address must be P2PKH or P2WPKH"
)
})
})

context("when recovery address is correct", () => {
const assertCommonDepositProperties = (receipt: DepositReceipt) => {
expect(receipt.depositor).to.be.deep.equal(
depositorProxy.getChainIdentifier()
)

expect(receipt.walletPublicKeyHash).to.be.deep.equal(
Hex.from("8db50eb52063ea9d98b3eac91489a90f738986f6")
)

// Expect the refund locktime to be in the future.
const receiptTimestamp = BigNumber.from(
receipt.refundLocktime.reverse().toPrefixedString()
).toNumber()
const currentTimestamp = Math.floor(new Date().getTime() / 1000)
expect(receiptTimestamp).to.be.greaterThan(currentTimestamp)

// Expect blinding factor to be set and 8-byte long.
expect(receipt.blindingFactor).not.to.be.undefined
expect(receipt.blindingFactor.toBuffer().length).to.be.equal(8)
}

context("when optional extra data is not provided", () => {
context("when recovery address is P2PKH", () => {
let deposit: Deposit

beforeEach(async () => {
deposit = await depositService.initiateDepositWithProxy(
"mjc2zGWypwpNyDi4ZxGbBNnUA84bfgiwYc",
depositorProxy
)
})

it("should initiate deposit correctly", async () => {
// Inspect the deposit object by looking at its receipt.
const receipt = deposit.getReceipt()

assertCommonDepositProperties(receipt)

expect(receipt.refundPublicKeyHash).to.be.deep.equal(
Hex.from("2cd680318747b720d67bf4246eb7403b476adb34")
)
expect(receipt.extraData).to.be.undefined
})
})

context("when recovery address is P2WPKH", () => {
let deposit: Deposit

beforeEach(async () => {
deposit = await depositService.initiateDepositWithProxy(
"tb1qumuaw3exkxdhtut0u85latkqfz4ylgwstkdzsx",
depositorProxy
)
})

it("should initiate deposit correctly", async () => {
// Inspect the deposit object by looking at its receipt.
const receipt = deposit.getReceipt()

assertCommonDepositProperties(receipt)

expect(receipt.refundPublicKeyHash).to.be.deep.equal(
Hex.from("e6f9d74726b19b75f16fe1e9feaec048aa4fa1d0")
)
expect(receipt.extraData).to.be.undefined
})
})
})

context("when optional extra data is provided", () => {
context("when extra data is not 32-byte", () => {
it("should throw", async () => {
await expect(
depositService.initiateDepositWithProxy(
"tb1qumuaw3exkxdhtut0u85latkqfz4ylgwstkdzsx",
depositorProxy,
Hex.from("11")
)
).to.be.rejectedWith("Extra data is not 32-byte")
})
})

context("when extra data is 32-byte but all-zero", () => {
it("should throw", async () => {
await expect(
depositService.initiateDepositWithProxy(
"tb1qumuaw3exkxdhtut0u85latkqfz4ylgwstkdzsx",
depositorProxy,
Hex.from(
"0000000000000000000000000000000000000000000000000000000000000000"
)
)
).to.be.rejectedWith("Extra data contains only zero bytes")
})
})

context("when extra data is 32-byte and non-zero", () => {
let deposit: Deposit

beforeEach(async () => {
deposit = await depositService.initiateDepositWithProxy(
"tb1qumuaw3exkxdhtut0u85latkqfz4ylgwstkdzsx",
depositorProxy,
Hex.from(
"1111111111111111222222222222222211111111111111112222222222222222"
)
)
})

it("should initiate deposit correctly", async () => {
// Inspect the deposit object by looking at its receipt.
const receipt = deposit.getReceipt()

assertCommonDepositProperties(receipt)

expect(receipt.refundPublicKeyHash).to.be.deep.equal(
Hex.from("e6f9d74726b19b75f16fe1e9feaec048aa4fa1d0")
)
expect(receipt.extraData).to.be.eql(
Hex.from(
"1111111111111111222222222222222211111111111111112222222222222222"
)
)
})
})
})
})
})
})
})

describe("DepositRefund", () => {
Expand Down
2 changes: 1 addition & 1 deletion typescript/test/utils/mock-depositor-proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export class MockDepositorProxy implements DepositorProxy {
}

getChainIdentifier(): ChainIdentifier {
return EthereumAddress.from("0x774cfd89700040163727828AE20B52099C58F02C")
return EthereumAddress.from("0xEdA7bE2D82566ce2546b150447b5cb0E4320a1B2")
}

revealDeposit(
Expand Down

0 comments on commit 40a1a9c

Please sign in to comment.