Skip to content

Commit 9aadfc9

Browse files
authored
fix: btc-address query now resolves mainnet addresses as well
1 parent 0975fde commit 9aadfc9

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

lib/address/bech32.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,18 @@ export function fromBech32Address(hash: string) {
5454
}
5555

5656
export function isBTCAddress(hash: string) {
57+
let pkscript = null;
58+
try {
59+
pkscript = bitcoin.address.toOutputScript(hash, bitcoin.networks.bitcoin).toString('hex');
60+
} catch (error) {
61+
try {
62+
pkscript = bitcoin.address.toOutputScript(hash, bitcoin.networks.testnet).toString('hex');
63+
} catch (error) {
64+
return false;
65+
}
66+
}
67+
5768
try {
58-
const pkscript = bitcoin.address.toOutputScript(hash, bitcoin.networks.testnet).toString('hex');
5969
const pkscriptBuf = Buffer.from(pkscript, 'hex');
6070
const pkscriptHash = keccak256(pkscriptBuf);
6171
pkscriptHash.slice(-40);
@@ -65,8 +75,18 @@ export function isBTCAddress(hash: string) {
6575
}
6676
}
6777
export function fromBTCAddress(hash: string) {
78+
let pkscript = null;
79+
try {
80+
pkscript = bitcoin.address.toOutputScript(hash, bitcoin.networks.bitcoin).toString('hex');
81+
} catch (error) {
82+
try {
83+
pkscript = bitcoin.address.toOutputScript(hash, bitcoin.networks.testnet).toString('hex');
84+
} catch (error) {
85+
return hash;
86+
}
87+
}
88+
6889
try {
69-
const pkscript = bitcoin.address.toOutputScript(hash, bitcoin.networks.testnet).toString('hex');
7090
const pkscriptBuf = Buffer.from(pkscript, 'hex');
7191
const pkscriptHash = keccak256(pkscriptBuf);
7292
const addr = pkscriptHash.slice(-40);

0 commit comments

Comments
 (0)