Skip to content

Commit 14008ec

Browse files
committed
remove tiny-secp256k1 from xpub-lib dependencies, add initEccLib function, require for taproot
1 parent 5e512c2 commit 14008ec

File tree

4 files changed

+28
-10
lines changed

4 files changed

+28
-10
lines changed

packages/xpub-cli/package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@swan-bitcoin/xpub-cli",
3-
"version": "0.2.0",
3+
"version": "0.2.1",
44
"description": "Command-line wrapper around @swan-bitcoin/xpub-lib",
55
"keywords": [
66
"bitcoin",
@@ -53,8 +53,9 @@
5353
"test": "echo \"Warning: no test specified\""
5454
},
5555
"dependencies": {
56-
"@swan-bitcoin/xpub-lib": "^0.2.0",
57-
"commander": "^6.1.0"
56+
"@swan-bitcoin/xpub-lib": "^0.2.1",
57+
"commander": "^6.1.0",
58+
"tiny-secp256k1": "^2.2.3"
5859
},
5960
"devDependencies": {
6061
"@babel/cli": "^7.11.6",

packages/xpub-cli/src/xpub.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22

33
const {
44
Network,
5+
initEccLib,
56
isValidAddress,
67
isValidExtPubKey,
78
addressFromExtPubKey,
89
addressesFromExtPubKey,
910
Purpose,
1011
} = require("@swan-bitcoin/xpub-lib")
12+
import * as ecc from 'tiny-secp256k1'
1113
const { version } = require("../package.json")
1214

1315
function parsePurpose(purpose) {
@@ -72,6 +74,8 @@ program
7274
cmdObj.help()
7375
}
7476

77+
initEccLib(ecc)
78+
7579
const network = cmdObj.testnet ? Network.TESTNET : Network.MAINNET
7680
if (!isValidExtPubKey(extPubKey, network)) {
7781
console.error(`error: invalid extended public key '${extPubKey}'`)

packages/xpub-lib/package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@swan-bitcoin/xpub-lib",
3-
"version": "0.2.0",
3+
"version": "0.2.1",
44
"description": "A JavaScript library for bitcoin address derivation from extended public keys, built upon bitcoinjs-lib and Unchained's bitcoin utilities. Supports P2PKH, P2SH, and P2WPKH (bech32) addresses as defined in BIP44, BIP49, and BIP84.",
55
"author": "Pablof7z <pablo@swan.com>",
66
"license": "MIT",
@@ -34,8 +34,7 @@
3434
],
3535
"dependencies": {
3636
"@caravan/bitcoin": "^0.3.1",
37-
"bitcoinjs-lib": "^6.1.7",
38-
"tiny-secp256k1": "^2.2.3"
37+
"bitcoinjs-lib": "^6.1.7"
3938
},
4039
"scripts": {
4140
"babel-version": "babel --version",

packages/xpub-lib/src/derivation.js

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
*/
66

77
import * as bitcoin from "bitcoinjs-lib"
8-
import * as ecc from 'tiny-secp256k1';
98
import { toXOnly } from 'bitcoinjs-lib/src/psbt/bip371';
109
import { deriveChildPublicKey, networkData, Network } from "@caravan/bitcoin"
1110
import { fullDerivationPath, partialKeyDerivationPath } from "./paths"
@@ -35,7 +34,20 @@ const DEFAULT_NETWORK = Network.TESTNET
3534
* */
3635
const DEFAULT_PURPOSE = Purpose.P2WPKH
3736

38-
bitcoin.initEccLib(ecc);
37+
/**
38+
* The secp256k1 interface to use for Taproot address derivation.
39+
*/
40+
let eccInstance = null;
41+
42+
/**
43+
* Initialize the ECC library for Taproot address derivation.
44+
*
45+
* @param {object} eccLib - The secp256k1 interface to use.
46+
*/
47+
function initEccLib(eccLib) {
48+
eccInstance = eccLib;
49+
bitcoin.initEccLib(eccInstance);
50+
}
3951

4052
/**
4153
* Derive a single address from a public key.
@@ -73,7 +85,9 @@ function deriveAddress({ purpose, pubkey, network }) {
7385
return bc1qAddress
7486
}
7587
case Purpose.P2TR: {
76-
// Context: https://bitcoinops.org/en/topics/x-only-public-keys/
88+
if (!eccInstance) {
89+
throw new Error("An instance of an ECC library implementing the secp256k1 curve must be initialized to generate taproot addresses. You must call initEccLib() first.");
90+
}
7791
const xOnlyPubkey = toXOnly(pubkey)
7892
const { address: bc1pAddress } = bitcoin.payments.p2tr({
7993
internalPubkey: xOnlyPubkey,
@@ -176,4 +190,4 @@ function addressesFromExtPubKey({
176190
return addresses
177191
}
178192

179-
export { addressFromExtPubKey, addressesFromExtPubKey }
193+
export { addressFromExtPubKey, addressesFromExtPubKey, initEccLib }

0 commit comments

Comments
 (0)