Skip to content

Commit

Permalink
Added address extraction from key pair
Browse files Browse the repository at this point in the history
  • Loading branch information
tomaszslabon committed Sep 25, 2023
1 parent 6a06c63 commit 6833051
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
1 change: 1 addition & 0 deletions typescript/src/bitcoin-network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export function toBcoinNetwork(bitcoinNetwork: BitcoinNetwork): string {
}
}

// TODO: Description
export function toBitcoinJsLibNetwork(
bitcoinNetwork: BitcoinNetwork
): networks.Network {
Expand Down
18 changes: 17 additions & 1 deletion typescript/src/bitcoin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Check failure on line 7 in typescript/src/bitcoin.ts

View workflow job for this annotation

GitHub Actions / typescript-format

'address' is defined but never used

Check failure on line 7 in typescript/src/bitcoin.ts

View workflow job for this annotation

GitHub Actions / typescript-format

'script' is defined but never used
import { ECPairInterface } from "ecpair"

/**
* Represents a transaction hash (or transaction ID) as an un-prefixed hex
Expand Down Expand Up @@ -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!;

Check failure on line 753 in typescript/src/bitcoin.ts

View workflow job for this annotation

GitHub Actions / typescript-format

Delete `;`
} else {
// P2PKH (Legacy)
return payments.p2pkh({ pubkey: keyPair.publicKey, network }).address!;

Check failure on line 756 in typescript/src/bitcoin.ts

View workflow job for this annotation

GitHub Actions / typescript-format

Delete `;`
}
}
8 changes: 2 additions & 6 deletions typescript/src/deposit-sweep.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
decomposeRawTransaction,
isCompressedPublicKey,
createKeyRing,
addressFromKeyPair,
TransactionHash,
computeHash160,
isP2PKH,
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 6833051

Please sign in to comment.