Skip to content

Commit

Permalink
feat: add function to retrieve bitcoin address from extended public key
Browse files Browse the repository at this point in the history
  • Loading branch information
Polybius93 committed Sep 18, 2024
1 parent 3fdcbb6 commit 626b821
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"type": "module",
"name": "dlc-btc-lib",
"version": "2.2.6",
"version": "2.2.7",
"description": "This library provides a comprehensive set of interfaces and functions for minting dlcBTC tokens on supported blockchains.",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
22 changes: 22 additions & 0 deletions src/functions/bitcoin/bitcoin-functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -481,3 +481,25 @@ export function ecdsaPublicKeyToSchnorr(publicKey: Buffer): Buffer {
throw new Error('Invalid Public Key Length');
return publicKey.subarray(1);
}

export function getBitcoinAddressFromExtendedPublicKey(
bitcoinExtendedPublicKey: string,
bitcoinNetwork: Network,
bitcoinAddressIndex: number,
paymentType: 'wpkh' | 'tr'
): string {
const derivedPublicKey = deriveUnhardenedPublicKey(
bitcoinExtendedPublicKey,
bitcoinNetwork,
bitcoinAddressIndex
);

const bitcoinAddress =
paymentType === 'wpkh'
? p2wpkh(derivedPublicKey, bitcoinNetwork).address
: p2tr(ecdsaPublicKeyToSchnorr(derivedPublicKey), undefined, bitcoinNetwork).address;

if (!bitcoinAddress) throw new Error('Could not create Bitcoin Address');

return bitcoinAddress;
}
2 changes: 2 additions & 0 deletions src/functions/bitcoin/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
finalizeUserInputs,
getBitcoinAddressFromExtendedPublicKey,
getFeeAmount,
getFeeRecipientAddressFromPublicKey,
getInputIndicesByScript,
Expand Down Expand Up @@ -28,4 +29,5 @@ export {
getBalance,
getFeeRecipientAddressFromPublicKey,
getInputIndicesByScript,
getBitcoinAddressFromExtendedPublicKey,
};

0 comments on commit 626b821

Please sign in to comment.