Skip to content

Commit

Permalink
Refactored address generating function
Browse files Browse the repository at this point in the history
  • Loading branch information
tomaszslabon committed Sep 27, 2023
1 parent 3fee283 commit d021ddc
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 18 deletions.
37 changes: 21 additions & 16 deletions typescript/src/bitcoin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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!
}
}
9 changes: 7 additions & 2 deletions typescript/src/deposit-sweep.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit d021ddc

Please sign in to comment.