Files
hash-wallet/cw_core/lib/utils/zpub.dart
T
77812016ac LCW-1144 cupcake litecoin (#2557)
* Add psbt rpcs

* Only build arm64

* Rebuild protos

* Update proto

* Add psbt create rpc

* Restore wallet stuff

* more stuff

* more stuff

* more stuff

* Fix open wallet

* Fix mweb enabled

* Start of buildPsbt

* Update proto

* Psbt add recipients

* fixes

* slightly better

* more fixes

* decode ur

* add more commit code

* Update proto

* use new proto

* Don't eat psbt rpc errors

* Use unix domain socket for mwebd

* add gitignore

* Undo build only arm64

* fix pure mweb txns

* fix mweb txids

* fix ci

* fix blank pegin address

* chore: update cw_mweb dependencies
chore: remove cw_core as dependency from cw_mweb

* fix: Ltub restore

* fix: address review crashes, fix non-mweb view only wallets

* minor fixes

---------

Co-authored-by: Hector Chu <hectorchu@gmail.com>
Co-authored-by: OmarHatem <omarh.ismail1@gmail.com>
2025-11-24 17:58:12 +01:00

86 lines
2.2 KiB
Dart

import 'dart:typed_data';
import 'package:blockchain_utils/blockchain_utils.dart';
String convertAnyToXpub(String any) {
if (any.toLowerCase().startsWith("zpub")) {
return convertZpubToXpub(any);
} else if (any.toLowerCase().startsWith("ltub")) {
return convertLtubToXpub(any);
}
return any;
}
String convertZpubToXpub(String zpub) {
try {
final decoded = Base58Decoder.checkDecode(zpub);
if (decoded.length < 4) {
throw ArgumentError('Invalid extended public key length');
}
final versionBytes = decoded.sublist(0, 4);
final zpubVersionBytes = [0x04, 0xb2, 0x47, 0x46]; // zpub mainnet version
final zpubTestnetVersionBytes = [0x04, 0x5f, 0x1c, 0xf6]; // vpub testnet version
bool isZpub = listEquals(versionBytes, zpubVersionBytes);
bool isVpub = listEquals(versionBytes, zpubTestnetVersionBytes);
if (!isZpub && !isVpub) {
return zpub;
}
final xpubVersionBytes = isZpub ?
[0x04, 0x88, 0xb2, 0x1e] : // xpub mainnet
[0x04, 0x35, 0x87, 0xcf]; // tpub testnet
final newExtendedKey = Uint8List.fromList([
...xpubVersionBytes,
...decoded.sublist(4),
]);
return Base58Encoder.checkEncode(newExtendedKey);
} catch (e) {
throw ArgumentError('Failed to convert zpub to xpub: $e');
}
}
String convertLtubToXpub(String ltub) {
try {
final decoded = Base58Decoder.checkDecode(ltub);
if (decoded.length < 4) {
throw ArgumentError('Invalid extended public key length');
}
final versionBytes = decoded.sublist(0, 4);
final ltubVersionBytes = [0x01, 0x9D, 0xA4, 0x62]; // Ltub mainnet version
bool isLtub = listEquals(versionBytes, ltubVersionBytes);
if (!isLtub) {
return ltub;
}
final xpubVersionBytes = [0x04, 0x88, 0xb2, 0x1e];
final newExtendedKey = Uint8List.fromList([
...xpubVersionBytes,
...decoded.sublist(4),
]);
return Base58Encoder.checkEncode(newExtendedKey);
} catch (e) {
throw ArgumentError('Failed to convert ltub to xpub: $e');
}
}
bool listEquals<T>(List<T> a, List<T> b) {
if (a.length != b.length) return false;
for (int i = 0; i < a.length; i++) {
if (a[i] != b[i]) return false;
}
return true;
}