Skip to content

Commit

Permalink
feat: allow scanning elect-rs using get_tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
rafael-xmr committed Feb 23, 2024
1 parent fd820ec commit d72cc2e
Show file tree
Hide file tree
Showing 46 changed files with 414 additions and 463 deletions.
21 changes: 20 additions & 1 deletion cw_bitcoin/lib/bitcoin_receive_page_option.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,28 @@ class BitcoinReceivePageOption implements ReceivePageOption {
BitcoinReceivePageOption.p2sh,
BitcoinReceivePageOption.p2tr,
BitcoinReceivePageOption.p2wsh,
BitcoinReceivePageOption.p2pkh
BitcoinReceivePageOption.p2pkh,
BitcoinReceivePageOption.silent_payments,
];

BitcoinAddressType toType() {
switch (this) {
case BitcoinReceivePageOption.p2tr:
return SegwitAddresType.p2tr;
case BitcoinReceivePageOption.p2wsh:
return SegwitAddresType.p2wsh;
case BitcoinReceivePageOption.p2pkh:
return P2pkhAddressType.p2pkh;
case BitcoinReceivePageOption.p2sh:
return P2shAddressType.p2wpkhInP2sh;
case BitcoinReceivePageOption.silent_payments:
return SilentPaymentsAddresType.p2sp;
case BitcoinReceivePageOption.p2wpkh:
default:
return SegwitAddresType.p2wpkh;
}
}

factory BitcoinReceivePageOption.fromType(BitcoinAddressType type) {
switch (type) {
case SegwitAddresType.p2tr:
Expand Down
26 changes: 22 additions & 4 deletions cw_bitcoin/lib/bitcoin_wallet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ abstract class BitcoinWalletBase extends ElectrumWallet with Store {
Map<String, int>? initialChangeAddressIndex,
int initialSilentAddressIndex = 0,
}) async {
final seedBytes = await mnemonicToSeedBytes(mnemonic);
return BitcoinWallet(
mnemonic: mnemonic,
password: password,
Expand All @@ -86,10 +87,18 @@ abstract class BitcoinWalletBase extends ElectrumWallet with Store {
initialAddresses: initialAddresses,
initialSilentAddresses: initialSilentAddresses,
initialSilentAddressIndex: initialSilentAddressIndex,
silentAddress: await SilentPaymentOwner.fromMnemonic(mnemonic,
silentAddress: await SilentPaymentOwner.fromPrivateKeys(
scanPrivkey: ECPrivate.fromHex(bitcoin.HDWallet.fromSeed(
seedBytes,
network: network == BitcoinNetwork.testnet ? bitcoin.testnet : bitcoin.bitcoin,
).derivePath(SCAN_PATH).privKey!),
spendPrivkey: ECPrivate.fromHex(bitcoin.HDWallet.fromSeed(
seedBytes,
network: network == BitcoinNetwork.testnet ? bitcoin.testnet : bitcoin.bitcoin,
).derivePath(SPEND_PATH).privKey!),
hrp: network == BitcoinNetwork.testnet ? 'tsp' : 'sp'),
initialBalance: initialBalance,
seedBytes: await mnemonicToSeedBytes(mnemonic),
seedBytes: seedBytes,
initialRegularAddressIndex: initialRegularAddressIndex,
initialChangeAddressIndex: initialChangeAddressIndex,
addressPageType: addressPageType,
Expand All @@ -106,6 +115,7 @@ abstract class BitcoinWalletBase extends ElectrumWallet with Store {
final snp = await ElectrumWalletSnapshot.load(name, walletInfo.type, password,
walletInfo.network != null ? BasedUtxoNetwork.fromName(walletInfo.network!) : null);

final seedBytes = await mnemonicToSeedBytes(snp.mnemonic);
return BitcoinWallet(
mnemonic: snp.mnemonic,
password: password,
Expand All @@ -114,10 +124,18 @@ abstract class BitcoinWalletBase extends ElectrumWallet with Store {
initialAddresses: snp.addresses,
initialSilentAddresses: snp.silentAddresses,
initialSilentAddressIndex: snp.silentAddressIndex,
silentAddress: await SilentPaymentOwner.fromMnemonic(snp.mnemonic,
silentAddress: await SilentPaymentOwner.fromPrivateKeys(
scanPrivkey: ECPrivate.fromHex(bitcoin.HDWallet.fromSeed(
seedBytes,
network: snp.network == BitcoinNetwork.testnet ? bitcoin.testnet : bitcoin.bitcoin,
).derivePath(SCAN_PATH).privKey!),
spendPrivkey: ECPrivate.fromHex(bitcoin.HDWallet.fromSeed(
seedBytes,
network: snp.network == BitcoinNetwork.testnet ? bitcoin.testnet : bitcoin.bitcoin,
).derivePath(SPEND_PATH).privKey!),
hrp: snp.network == BitcoinNetwork.testnet ? 'tsp' : 'sp'),
initialBalance: snp.balance,
seedBytes: await mnemonicToSeedBytes(snp.mnemonic),
seedBytes: seedBytes,
initialRegularAddressIndex: snp.regularAddressIndex,
initialChangeAddressIndex: snp.changeAddressIndex,
addressPageType: snp.addressPageType,
Expand Down
13 changes: 10 additions & 3 deletions cw_bitcoin/lib/electrum.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,19 @@ class ElectrumClient {
Timer? _aliveTimer;
String unterminatedString;

Future<void> connectToUri(Uri uri) async => await connect(host: uri.host, port: uri.port);
Uri? uri;

Future<void> connectToUri(Uri uri) async {
this.uri = uri;
await connect(host: uri.host, port: uri.port);
}

Future<void> connect({required String host, required int port}) async {
try {
await socket?.close();
} catch (_) {}

socket = await SecureSocket.connect(host, port,
timeout: connectionTimeout, onBadCertificate: (_) => true);
socket = await Socket.connect(host, port, timeout: connectionTimeout);
_setIsConnected(true);

socket!.listen((Uint8List event) {
Expand Down Expand Up @@ -275,6 +279,9 @@ class ElectrumClient {
Future<Map<String, dynamic>> getHeader({required int height}) async =>
await call(method: 'blockchain.block.get_header', params: [height]) as Map<String, dynamic>;

Future<Map<String, dynamic>> getTweaks({required int height}) async =>
await call(method: 'blockchain.block.tweaks', params: [height]) as Map<String, dynamic>;

Future<double> estimatefee({required int p}) =>
call(method: 'blockchain.estimatefee', params: [p]).then((dynamic result) {
if (result is double) {
Expand Down
Loading

0 comments on commit d72cc2e

Please sign in to comment.