Skip to content

Commit

Permalink
Updated unit tests for assembleDepositSweepTransaction
Browse files Browse the repository at this point in the history
  • Loading branch information
tomaszslabon committed Sep 25, 2023
1 parent e63197b commit e243f0f
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 47 deletions.
3 changes: 1 addition & 2 deletions typescript/src/deposit-sweep.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,14 +326,13 @@ export async function assembleDepositSweepTransactionBitcoinJsLib(
)
}

// TODO: Verify that output script is properly created from both testnet
// and mainnet addresses.
// Add transaction output.
const scriptPubKey = address.toOutputScript(walletAddress, network)
transaction.addOutput(scriptPubKey, totalInputValue.toNumber())

// UTXOs must be mapped to deposits, as `fund` may arrange inputs in any
// order
// TODO: Most likely remove.
const utxosWithDeposits: (UnspentTransactionOutput &
RawTransaction &
Deposit)[] = utxos.map((utxo, index) => ({
Expand Down
29 changes: 14 additions & 15 deletions typescript/test/data/deposit-sweep.ts
Original file line number Diff line number Diff line change
Expand Up @@ -350,24 +350,23 @@ export const depositSweepWithNonWitnessMainUtxoAndWitnessOutput: DepositSweepTes
witness: true,
expectedSweep: {
transactionHash: TransactionHash.from(
"7831d0dfde7e160f3b9bb66c433710f0d3110d73ea78b9db65e81c091a6718a0"
"1933781f01f27f086c3a31c4a53035ebc7c4688e1f4b316babefa8f6dab77dc2"
),
transaction: {
transactionHex:
"01000000000102173a201f597a2c8ccd7842303a6653bb87437fb08dae671731a0" +
"75403b32a2fd0000000000ffffffffe19612be756bf7e740b47bec0e24845089ac" +
"e48c78d473cb34949b3007c4a2c8000000006a47304402204382deb051f9f3e2b5" +
"39e4bac2d1a50faf8d66bc7a3a3f3d286dabd96d92b58b02207c74c6aaf48e25d0" +
"7e02bb4039606d77ecfd80c492c050ab2486af6027fc2d5a012103989d253b17a6" +
"a0f41838b84ff0d20e8898f9d7b1a98f2564da4cc29dcf8581d9ffffffff010884" +
"0000000000001600148db50eb52063ea9d98b3eac91489a90f738986f603483045" +
"022100c52bc876cdee80a3061ace3ffbce5e860942d444cd38e00e5f63fd8e818d" +
"7e7c022040a7017bb8213991697705e7092c481526c788a4731d06e582dc1c57be" +
"d7243b012103989d253b17a6a0f41838b84ff0d20e8898f9d7b1a98f2564da4cc2" +
"9dcf8581d95c14934b98637ca318a4d6e7ca6ffd1690b8e77df6377508f9f0c90d" +
"000395237576a9148db50eb52063ea9d98b3eac91489a90f738986f68763ac6776" +
"a914e257eccafbc07c381642ce6e7e55120fb077fbed880448f2b262b175ac6800" +
"00000000",
"01000000000102e19612be756bf7e740b47bec0e24845089ace48c78d473cb34949" +
"b3007c4a2c8000000006a473044022013787be70eda0620002fa55c92abfcd32257" +
"d64fa652dd32bac65d705162a95902203407fea1abc99a9273ead3179ce60f60a34" +
"33fb2e93f58569e4bee9f63c0d679012103989d253b17a6a0f41838b84ff0d20e88" +
"98f9d7b1a98f2564da4cc29dcf8581d9ffffffff173a201f597a2c8ccd7842303a6" +
"653bb87437fb08dae671731a075403b32a2fd0000000000ffffffff010884000000" +
"0000001600148db50eb52063ea9d98b3eac91489a90f738986f6000348304502210" +
"0804f0fa989d632cda99a24159e28b8d31d4033c2d5de47d8207ea2767273d10a02" +
"20278e82d0714867b31eb013762306e2b97c2c1cc74b8135bee78d565e72ee630e0" +
"12103989d253b17a6a0f41838b84ff0d20e8898f9d7b1a98f2564da4cc29dcf8581" +
"d95c14934b98637ca318a4d6e7ca6ffd1690b8e77df6377508f9f0c90d000395237" +
"576a9148db50eb52063ea9d98b3eac91489a90f738986f68763ac6776a914e257ec" +
"cafbc07c381642ce6e7e55120fb077fbed880448f2b262b175ac6800000000",
},
},
}
Expand Down
71 changes: 41 additions & 30 deletions typescript/test/deposit-sweep.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@ import chaiAsPromised from "chai-as-promised"
chai.use(chaiAsPromised)
import { expect } from "chai"
import {
assembleDepositSweepTransaction,
assembleDepositSweepTransactionBitcoinJsLib,
submitDepositSweepProof,
submitDepositSweepTransaction,
} from "../src/deposit-sweep"
import { BitcoinNetwork } from "../src/bitcoin-network"

describe("Sweep", () => {
const fee = BigNumber.from(1600)
Expand Down Expand Up @@ -378,7 +379,8 @@ describe("Sweep", () => {
transactionHash,
newMainUtxo,
rawTransaction: transaction,
} = await assembleDepositSweepTransaction(
} = await assembleDepositSweepTransactionBitcoinJsLib(
BitcoinNetwork.Testnet,
fee,
testnetWalletPrivateKey,
witness,
Expand Down Expand Up @@ -507,7 +509,8 @@ describe("Sweep", () => {
transactionHash,
newMainUtxo,
rawTransaction: transaction,
} = await assembleDepositSweepTransaction(
} = await assembleDepositSweepTransactionBitcoinJsLib(
BitcoinNetwork.Testnet,
fee,
testnetWalletPrivateKey,
witness,
Expand Down Expand Up @@ -657,7 +660,8 @@ describe("Sweep", () => {
transactionHash,
newMainUtxo,
rawTransaction: transaction,
} = await assembleDepositSweepTransaction(
} = await assembleDepositSweepTransactionBitcoinJsLib(
BitcoinNetwork.Testnet,
fee,
testnetWalletPrivateKey,
witness,
Expand Down Expand Up @@ -686,25 +690,7 @@ describe("Sweep", () => {
// Validate inputs.
expect(txJSON.inputs.length).to.be.equal(2)

const p2wshInput = txJSON.inputs[0]
expect(p2wshInput.prevout.hash).to.be.equal(
depositSweepWithNonWitnessMainUtxoAndWitnessOutput.deposits[0].utxo.transactionHash.toString()
)
expect(p2wshInput.prevout.index).to.be.equal(
depositSweepWithNonWitnessMainUtxoAndWitnessOutput.deposits[0]
.utxo.outputIndex
)
// Transaction should be signed. As it's a SegWit input, the `witness`
// field should be filled, while the `script` field should be empty.
expect(p2wshInput.witness.length).to.be.greaterThan(0)
expect(p2wshInput.script.length).to.be.equal(0)
// Input's address should be set to the address generated from deposit
// script hash
expect(p2wshInput.address).to.be.equal(
"tb1qk8urugnf08wfle6wslmdxq7mkz9z0gw8e6gkvspn7dx87tfpfntshdm7qr"
)

const p2pkhInput = txJSON.inputs[1] // main UTXO
const p2pkhInput = txJSON.inputs[0] // main UTXO
expect(p2pkhInput.prevout.hash).to.be.equal(
depositSweepWithNonWitnessMainUtxoAndWitnessOutput.mainUtxo.transactionHash.toString()
)
Expand All @@ -722,6 +708,24 @@ describe("Sweep", () => {
"mtSEUCE7G8om9zJttG9twtjoiSsUz7QnY9"
)

const p2wshInput = txJSON.inputs[1]
expect(p2wshInput.prevout.hash).to.be.equal(
depositSweepWithNonWitnessMainUtxoAndWitnessOutput.deposits[0].utxo.transactionHash.toString()
)
expect(p2wshInput.prevout.index).to.be.equal(
depositSweepWithNonWitnessMainUtxoAndWitnessOutput.deposits[0]
.utxo.outputIndex
)
// Transaction should be signed. As it's a SegWit input, the `witness`
// field should be filled, while the `script` field should be empty.
expect(p2wshInput.witness.length).to.be.greaterThan(0)
expect(p2wshInput.script.length).to.be.equal(0)
// Input's address should be set to the address generated from deposit
// script hash
expect(p2wshInput.address).to.be.equal(
"tb1qk8urugnf08wfle6wslmdxq7mkz9z0gw8e6gkvspn7dx87tfpfntshdm7qr"
)

// Validate outputs.
expect(txJSON.outputs.length).to.be.equal(1)

Expand Down Expand Up @@ -790,7 +794,8 @@ describe("Sweep", () => {
transactionHash,
newMainUtxo,
rawTransaction: transaction,
} = await assembleDepositSweepTransaction(
} = await assembleDepositSweepTransactionBitcoinJsLib(
BitcoinNetwork.Testnet,
fee,
testnetWalletPrivateKey,
witness,
Expand Down Expand Up @@ -877,7 +882,8 @@ describe("Sweep", () => {
context("when there are no UTXOs", () => {
it("should revert", async () => {
await expect(
assembleDepositSweepTransaction(
assembleDepositSweepTransactionBitcoinJsLib(
BitcoinNetwork.Testnet,
fee,
testnetWalletPrivateKey,
true,
Expand Down Expand Up @@ -905,7 +911,8 @@ describe("Sweep", () => {

it("should revert", async () => {
await expect(
assembleDepositSweepTransaction(
assembleDepositSweepTransactionBitcoinJsLib(
BitcoinNetwork.Testnet,
fee,
testnetWalletPrivateKey,
witness,
Expand All @@ -930,7 +937,8 @@ describe("Sweep", () => {

it("should revert", async () => {
await expect(
assembleDepositSweepTransaction(
assembleDepositSweepTransactionBitcoinJsLib(
BitcoinNetwork.Testnet,
fee,
testnetWalletPrivateKey,
true,
Expand Down Expand Up @@ -969,7 +977,8 @@ describe("Sweep", () => {

it("should revert", async () => {
await expect(
assembleDepositSweepTransaction(
assembleDepositSweepTransactionBitcoinJsLib(
BitcoinNetwork.Testnet,
fee,
testnetWalletPrivateKey,
true,
Expand All @@ -993,7 +1002,8 @@ describe("Sweep", () => {

it("should revert", async () => {
await expect(
assembleDepositSweepTransaction(
assembleDepositSweepTransactionBitcoinJsLib(
BitcoinNetwork.Testnet,
fee,
anotherPrivateKey,
true,
Expand Down Expand Up @@ -1026,7 +1036,8 @@ describe("Sweep", () => {

it("should revert", async () => {
await expect(
assembleDepositSweepTransaction(
assembleDepositSweepTransactionBitcoinJsLib(
BitcoinNetwork.Testnet,
fee,
testnetWalletPrivateKey,
true,
Expand Down

0 comments on commit e243f0f

Please sign in to comment.