Skip to content

Commit

Permalink
Fix types, support ltc address parsing (gh-112)
Browse files Browse the repository at this point in the history
  • Loading branch information
paulmillr committed Sep 18, 2024
1 parent f1acec6 commit ea27eab
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/payment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -719,14 +719,14 @@ export function Address(network = NETWORK) {
decode(address: string): P.UnwrapCoder<OutScriptType> {
if (address.length < 14 || address.length > 74) throw new Error('Invalid address length');
// Bech32
if (network.bech32 && address.toLowerCase().startsWith(network.bech32)) {
if (network.bech32 && address.toLowerCase().startsWith(`${network.bech32}1`)) {
let res;
try {
res = bech32.decode(address);
res = bech32.decode(address as `${string}1${string}`);
if (res.words[0] !== 0) throw new Error(`bech32: wrong version=${res.words[0]}`);
} catch (_) {
// Starting from version 1 it is decoded as bech32m
res = bech32m.decode(address);
res = bech32m.decode(address as `${string}1${string}`);
if (res.words[0] === 0) throw new Error(`bech32m: wrong version=${res.words[0]}`);
}
if (res.prefix !== network.bech32) throw new Error(`wrong bech32 prefix=${res.prefix}`);
Expand Down
13 changes: 13 additions & 0 deletions test/basic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,19 @@ should('BTC: P2PKH addresses', () => {
deepStrictEqual(btc.p2pkh(pub).address, ADDR_1);
});

should('LTC address parsing (GH-112)', () => {
const ltc = {
bech32: 'ltc',
pubKeyHash: 0x30,
scriptHash: 0x32,
wif: 0xb0,
};
deepStrictEqual(btc.Address(ltc).decode('LTCHodBXqzzfDvkL1kA3dHRVsx4SCL3Y13'), {
hash: hex.decode('57707ca1471cab4b8cbd32a0e92f538a661e4c53'),
type: 'pkh',
});
});

// Same as above
const TX_TEST_OUTPUTS = [
['1cMh228HTCiwS8ZsaakH8A8wze1JR5ZsP', 10n],
Expand Down

0 comments on commit ea27eab

Please sign in to comment.