Skip to content

Commit

Permalink
Added Bitcoin network argument
Browse files Browse the repository at this point in the history
  • Loading branch information
tomaszslabon committed Sep 25, 2023
1 parent 8416229 commit 6a06c63
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
17 changes: 17 additions & 0 deletions typescript/src/bitcoin-network.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Hex } from "./hex"
import { networks } from "bitcoinjs-lib"

/**
* Bitcoin networks.
Expand Down Expand Up @@ -64,3 +65,19 @@ export function toBcoinNetwork(bitcoinNetwork: BitcoinNetwork): string {
}
}
}

export function toBitcoinJsLibNetwork(
bitcoinNetwork: BitcoinNetwork
): networks.Network {
switch (bitcoinNetwork) {
case BitcoinNetwork.Mainnet: {
return networks.bitcoin
}
case BitcoinNetwork.Testnet: {
return networks.testnet
}
default: {
throw new Error(`network not supported`)
}
}
}
12 changes: 6 additions & 6 deletions typescript/src/deposit-sweep.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
payments,
address,
script,
networks,
} from "bitcoinjs-lib"
import { BigNumber } from "ethers"
import {
Expand All @@ -28,6 +27,7 @@ import { Bridge, Identifier } from "./chain"
import { assembleTransactionProof } from "./proof"
import { ECPairFactory as ecFactory } from "ecpair"
import * as tinysecp from "tiny-secp256k1"
import { BitcoinNetwork, toBitcoinJsLibNetwork } from "./bitcoin-network"

/**
* Submits a deposit sweep by combining all the provided P2(W)SH UTXOs and
Expand Down Expand Up @@ -272,6 +272,7 @@ export async function assembleDepositSweepTransaction(
*/
// TODO: Rename once it's finished.
export async function assembleDepositSweepTransactionBitcoinJsLib(
bitcoinNetwork: BitcoinNetwork,
fee: BigNumber,
walletPrivateKey: string,
witness: boolean,
Expand All @@ -291,15 +292,14 @@ export async function assembleDepositSweepTransactionBitcoinJsLib(
throw new Error("Number of UTXOs must equal the number of deposit elements")
}

const network = toBitcoinJsLibNetwork(bitcoinNetwork)

// TODO: Replace keyring with bitcoinjs-lib functionalities for managing
// keys (ecpair).
const walletKeyRing = createKeyRing(walletPrivateKey, witness)
const walletAddress = walletKeyRing.getAddress("string")

const keyPair = ecFactory(tinysecp).fromWIF(
walletPrivateKey,
networks.testnet
)
const keyPair = ecFactory(tinysecp).fromWIF(walletPrivateKey, network)

// Calculate the value of transaction's output. Note that the value of fee
// needs to be subtracted from the sum.
Expand Down Expand Up @@ -332,7 +332,7 @@ 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)
const scriptPubKey = address.toOutputScript(walletAddress, network)
transaction.addOutput(scriptPubKey, totalInputValue.toNumber())

// UTXOs must be mapped to deposits, as `fund` may arrange inputs in any
Expand Down

0 comments on commit 6a06c63

Please sign in to comment.