From 6a06c63ccbd23a7f98a9c7e8b706b57feca9a81c Mon Sep 17 00:00:00 2001 From: Tomasz Slabon Date: Mon, 25 Sep 2023 14:17:46 +0200 Subject: [PATCH] Added Bitcoin network argument --- typescript/src/bitcoin-network.ts | 17 +++++++++++++++++ typescript/src/deposit-sweep.ts | 12 ++++++------ 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/typescript/src/bitcoin-network.ts b/typescript/src/bitcoin-network.ts index f14dd5ed4..1f6bf50a9 100644 --- a/typescript/src/bitcoin-network.ts +++ b/typescript/src/bitcoin-network.ts @@ -1,4 +1,5 @@ import { Hex } from "./hex" +import { networks } from "bitcoinjs-lib" /** * Bitcoin networks. @@ -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`) + } + } +} diff --git a/typescript/src/deposit-sweep.ts b/typescript/src/deposit-sweep.ts index 0340e646b..7a646ab82 100644 --- a/typescript/src/deposit-sweep.ts +++ b/typescript/src/deposit-sweep.ts @@ -6,7 +6,6 @@ import { payments, address, script, - networks, } from "bitcoinjs-lib" import { BigNumber } from "ethers" import { @@ -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 @@ -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, @@ -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. @@ -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