From d021ddc33d40a28768021528180d8d0c088b8c02 Mon Sep 17 00:00:00 2001 From: Tomasz Slabon Date: Wed, 27 Sep 2023 11:01:32 +0200 Subject: [PATCH] Refactored address generating function --- typescript/src/bitcoin.ts | 37 +++++++++++++++++++-------------- typescript/src/deposit-sweep.ts | 9 ++++++-- 2 files changed, 28 insertions(+), 18 deletions(-) diff --git a/typescript/src/bitcoin.ts b/typescript/src/bitcoin.ts index 83f868ea5..4679c9bde 100644 --- a/typescript/src/bitcoin.ts +++ b/typescript/src/bitcoin.ts @@ -3,9 +3,12 @@ import wif from "wif" import bufio from "bufio" import { BigNumber, utils } from "ethers" import { Hex } from "./hex" -import { BitcoinNetwork, toBcoinNetwork } from "./bitcoin-network" -import { payments, networks } from "bitcoinjs-lib" -import { Signer } from "ecpair" +import { + BitcoinNetwork, + toBcoinNetwork, + toBitcoinJsLibNetwork, +} from "./bitcoin-network" +import { payments } from "bitcoinjs-lib" /** * Represents a transaction hash (or transaction ID) as an un-prefixed hex @@ -743,25 +746,27 @@ export function isP2WSHScript(script: Buffer): boolean { } /** - * Generates a Bitcoin address based on the provided key pair and network. - * Can produce either SegWit (P2WPKH) or Legacy (P2PKH) addresses. - * @param keyPair - The key pair used to derive the Bitcoin address. - * @param network - Specified Bitcoin network. - * @param witness - Boolean flag indicating if the address should be SegWit - * (P2WPKH) or not (P2PKH). - * @returns The generated Bitcoin address as a string. + * Generates a Bitcoin address from a public key. Supports SegWit (P2WPKH) and + * Legacy (P2PKH) formats. + * @param publicKey - Public key used to derive the Bitcoin address. + * @param bitcoinNetwork - Target Bitcoin network. + * @param witness - Flag to determine address format: true for SegWit (P2WPKH) + * and false for Legacy (P2PKH). Default is true. + * @returns The derived Bitcoin address. */ // TODO: Unit tests. -export function addressFromKeyPair( - keyPair: Signer, - network: networks.Network, - witness: boolean +export function publicKeyToAddress( + publicKey: Hex, + bitcoinNetwork: BitcoinNetwork, + witness: boolean = true ): string { + const network = toBitcoinJsLibNetwork(bitcoinNetwork) + if (witness) { // P2WPKH (SegWit) - return payments.p2wpkh({ pubkey: keyPair.publicKey, network }).address! + return payments.p2wpkh({ pubkey: publicKey.toBuffer(), network }).address! } else { // P2PKH (Legacy) - return payments.p2pkh({ pubkey: keyPair.publicKey, network }).address! + return payments.p2pkh({ pubkey: publicKey.toBuffer(), network }).address! } } diff --git a/typescript/src/deposit-sweep.ts b/typescript/src/deposit-sweep.ts index d2e6b452e..0e744c370 100644 --- a/typescript/src/deposit-sweep.ts +++ b/typescript/src/deposit-sweep.ts @@ -8,13 +8,14 @@ import { networks, } from "bitcoinjs-lib" import { BigNumber } from "ethers" +import { Hex } from "./hex" import { RawTransaction, UnspentTransactionOutput, Client as BitcoinClient, decomposeRawTransaction, isCompressedPublicKey, - addressFromKeyPair, + publicKeyToAddress, TransactionHash, computeHash160, isP2PKHScript, @@ -153,7 +154,11 @@ export async function assembleDepositSweepTransaction( const network = toBitcoinJsLibNetwork(bitcoinNetwork) // eslint-disable-next-line new-cap const keyPair = ECPairFactory(tinysecp).fromWIF(walletPrivateKey, network) - const walletAddress = addressFromKeyPair(keyPair, network, witness) + const walletAddress = publicKeyToAddress( + Hex.from(keyPair.publicKey), + bitcoinNetwork, + witness + ) // Calculate the value of transaction's output. Note that the value of fee // needs to be subtracted from the sum.