fix: derive LTC as P2WPKH (native segwit ltc1q) not legacy P2PKH

This commit is contained in:
M1 2026-03-18 23:24:17 +04:00
parent 8b2dcf0d56
commit 4cdd4b5343
1 changed files with 10 additions and 11 deletions

View File

@ -83,17 +83,16 @@ export function deriveAddressSafe(coin: string, index: number): string {
return addr.replace(/^bitcoincash:/, "ecash:"); return addr.replace(/^bitcoincash:/, "ecash:");
} }
if (coin === "ltc") { if (coin === "ltc") {
// zpub uses BIP84 version bytes — convert to Ltub so bitcore-lib-ltc can parse it // All zpub/Ltub/Mtub/xpub variants share the same key data — only version bytes differ.
let key = xpub; // Remap to the standard xpub version (0x0488b21e) so bitcore-lib-ltc can parse it,
if (xpub.startsWith("zpub") || xpub.startsWith("Zpub") || xpub.startsWith("Ltub") || xpub.startsWith("Mtub")) { // then derive a native segwit P2WPKH (bech32 ltc1q...) address.
// bitcore-lib-ltc only understands standard xpub version bytes (0x0488b21e).
// Convert zpub/Ltub/Mtub → xpub by swapping the 4-byte version prefix.
const decoded = Buffer.from(bs58check.decode(xpub)); const decoded = Buffer.from(bs58check.decode(xpub));
decoded[0] = 0x04; decoded[1] = 0x88; decoded[2] = 0xb2; decoded[3] = 0x1e; decoded[0] = 0x04; decoded[1] = 0x88; decoded[2] = 0xb2; decoded[3] = 0x1e;
key = bs58check.encode(decoded); const normalized = bs58check.encode(decoded);
} const hd = new bitcoreLtc.HDPublicKey(normalized);
const hd = new bitcoreLtc.HDPublicKey(key); const pubkey = hd.deriveChild(0).deriveChild(index).publicKey;
return hd.deriveChild(0).deriveChild(index).publicKey.toAddress().toString(); // Derive as P2WPKH (native segwit, bech32 ltc1q...)
return new bitcoreLtc.Address(pubkey, bitcoreLtc.Networks.mainnet, bitcoreLtc.Address.PayToWitnessPublicKeyHash).toString();
} }
if (coin === "doge") { if (coin === "doge") {
const hd = new bitcoreDoge.HDPublicKey(xpub); const hd = new bitcoreDoge.HDPublicKey(xpub);