Skip to content

Commit 5a5f07c

Browse files
authored
chore: bump bitcoinjs-lib to latest (fixes warnings related to taproot/segwit_v1 addresses) (#1517)
* chore: bump to bitcoinjs-lib to latest (fixes warnings related to taproot/segwitV2 addresses) * chore: bitcoinjs-lib now supports taproot addresses without custom handling
1 parent 734d5a5 commit 5a5f07c

File tree

6 files changed

+39
-33
lines changed

6 files changed

+39
-33
lines changed

package-lock.json

Lines changed: 15 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@
137137
"@types/ws": "7.4.7",
138138
"ajv": "6.12.6",
139139
"bignumber.js": "9.0.2",
140-
"bitcoinjs-lib": "6.0.2",
140+
"bitcoinjs-lib": "6.1.0",
141141
"bluebird": "3.7.2",
142142
"c32check": "1.1.3",
143143
"chokidar": "3.5.3",

src/btc-faucet.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { RPCClient } from 'rpc-bitcoin';
22
import * as btc from 'bitcoinjs-lib';
3+
import * as ecc from 'tiny-secp256k1';
34
import * as Bluebird from 'bluebird';
45
import { parsePort, time, logger, logError } from './helpers';
56
import * as coinselect from 'coinselect';
@@ -70,6 +71,7 @@ const MIN_TX_CONFIRMATIONS = 1;
7071

7172
function isValidBtcAddress(network: btc.Network, address: string): boolean {
7273
try {
74+
btc.initEccLib(ecc);
7375
btc.address.toOutputScript(address, network);
7476
return true;
7577
} catch (error) {

src/ec-helpers.ts

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -198,24 +198,15 @@ function p2wshAddressFromKey(args: KeyInputArgs): KeyOutput {
198198
function p2trAddressFromKey(args: KeyInputArgs): KeyOutput {
199199
const network = BITCOIN_NETWORKS[args.network];
200200
const ecPair = ecPairFromKeyInputArgs(args, true);
201-
202-
// x-only pubkey (remove 1 byte y parity)
203-
const myXOnlyPubkey = ecPair.publicKey.slice(1, 33);
204-
const commitHash = bitcoin.crypto.taggedHash('TapTweak', myXOnlyPubkey);
205-
const tweakResult = ecc.xOnlyPointAddTweak(myXOnlyPubkey, commitHash);
206-
if (tweakResult === null) {
207-
throw new Error('Invalid Tweak');
201+
bitcoin.initEccLib(ecc);
202+
const pmnt = bitcoin.payments.p2tr({
203+
internalPubkey: ecPair.publicKey.slice(1, 33),
204+
network: network,
205+
});
206+
if (!pmnt.address) {
207+
throw new Error(`Could not create p2tr address from key`);
208208
}
209-
const { xOnlyPubkey: tweaked } = tweakResult;
210-
const scriptPubkey = Buffer.concat([
211-
// witness v1, PUSH_DATA 32 bytes
212-
Buffer.from([0x51, 0x20]),
213-
// x-only tweaked pubkey
214-
tweaked,
215-
]);
216-
217-
const address = bitcoin.address.fromOutputScript(scriptPubkey, network);
218-
return { ecPair, address };
209+
return { ecPair, address: pmnt.address };
219210
}
220211

221212
export interface VerboseKeyOutput {

src/helpers.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import * as stream from 'stream';
77
import * as http from 'http';
88
import * as winston from 'winston';
99
import { isValidStacksAddress, stacksToBitcoinAddress } from 'stacks-encoding-native-js';
10+
import * as ecc from 'tiny-secp256k1';
1011
import * as btc from 'bitcoinjs-lib';
1112
import {
1213
BufferCV,
@@ -243,6 +244,7 @@ export function microStxToStx(microStx: bigint | BigNumber): string {
243244
* @param address - A bitcoin address.
244245
*/
245246
export function isValidBitcoinAddress(address: string): boolean {
247+
btc.initEccLib(ecc);
246248
try {
247249
btc.address.toOutputScript(address, btc.networks.bitcoin);
248250
return true;

src/tests/helpers-tests.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,17 @@ test('bitcoin<->stacks address', () => {
114114
'tb1qwvwagx5f24farha0fzfmxr48lgr7sly7t5tsyh',
115115
'tb1qqruv3zxqtmaxqa8uxaychtm0szfazeay63j9yu',
116116
],
117+
// bech32m / segwit_V1 / p2tr / taproot
118+
p2tr_mainnet: [
119+
'bc1p5d7rjq7g6rdk2yhzks9smlaqtedr4dekq08ge8ztwac72sfr9rusxg3297',
120+
'bc1p2wsldez5mud2yam29q22wgfh9439spgduvct83k3pm50fcxa5dps59h4z5',
121+
'bc1pmfr3p9j00pfxjh0zmgp99y8zftmd3s5pmedqhyptwy6lm87hf5sspknck9',
122+
],
123+
// bech32m / segwit_V1 / p2tr / taproot
124+
p2tr_testnet: [
125+
'tb1p6h5fuzmnvpdthf5shf0qqjzwy7wsqc5rhmgq2ks9xrak4ry6mtrscsqvzp',
126+
'tb1p8dlmzllfah294ntwatr8j5uuvcj7yg0dete94ck2krrk0ka2c9qqex96hv',
127+
],
117128
bip141_p2wpkh_mainnet: [
118129
'bc1q86agjesjeu33mq7uwxsfgdxpe5uxwd0z9ttke9',
119130
'bc1qlq3xlzgun9x92hd4hrfqkqs6uh78tjleqsc2u2',

0 commit comments

Comments
 (0)