diff --git a/typescript/src/bitcoin-network.ts b/typescript/src/bitcoin-network.ts index 1f6bf50a9..c2805c9f7 100644 --- a/typescript/src/bitcoin-network.ts +++ b/typescript/src/bitcoin-network.ts @@ -66,6 +66,7 @@ export function toBcoinNetwork(bitcoinNetwork: BitcoinNetwork): string { } } +// TODO: Description export function toBitcoinJsLibNetwork( bitcoinNetwork: BitcoinNetwork ): networks.Network { diff --git a/typescript/src/bitcoin.ts b/typescript/src/bitcoin.ts index 2e4e0fb08..6bfbad24e 100644 --- a/typescript/src/bitcoin.ts +++ b/typescript/src/bitcoin.ts @@ -4,7 +4,8 @@ import bufio from "bufio" import { BigNumber, utils } from "ethers" import { Hex } from "./hex" import { BitcoinNetwork, toBcoinNetwork } from "./bitcoin-network" -import { payments } from "bitcoinjs-lib" +import { payments, address, script, networks } from "bitcoinjs-lib" +import { ECPairInterface } from "ecpair" /** * Represents a transaction hash (or transaction ID) as an un-prefixed hex @@ -740,3 +741,18 @@ export function isP2WSH(script: Buffer): boolean { return false } } + +// TODO: Description and unit tests. +export function addressFromKeyPair( + keyPair: ECPairInterface, + network: networks.Network, + witness: boolean +): string { + if (witness) { + // P2WPKH (SegWit) + return payments.p2wpkh({ pubkey: keyPair.publicKey, network }).address!; + } else { + // P2PKH (Legacy) + return payments.p2pkh({ pubkey: keyPair.publicKey, network }).address!; + } +} diff --git a/typescript/src/deposit-sweep.ts b/typescript/src/deposit-sweep.ts index 7a646ab82..85ad6e02b 100644 --- a/typescript/src/deposit-sweep.ts +++ b/typescript/src/deposit-sweep.ts @@ -15,6 +15,7 @@ import { decomposeRawTransaction, isCompressedPublicKey, createKeyRing, + addressFromKeyPair, TransactionHash, computeHash160, isP2PKH, @@ -293,13 +294,8 @@ export async function assembleDepositSweepTransactionBitcoinJsLib( } 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, network) + const walletAddress = addressFromKeyPair(keyPair, network, witness) // Calculate the value of transaction's output. Note that the value of fee // needs to be subtracted from the sum.