mirror of
https://github.com/cake-tech/cake_wallet.git
synced 2025-01-19 01:04:43 +00:00
Generate MWEB addresses
This commit is contained in:
parent
dbc005dcf3
commit
29238effdf
11 changed files with 91 additions and 28 deletions
|
@ -7,6 +7,7 @@ class BitcoinReceivePageOption implements ReceivePageOption {
|
||||||
static const p2tr = BitcoinReceivePageOption._('Taproot (P2TR)');
|
static const p2tr = BitcoinReceivePageOption._('Taproot (P2TR)');
|
||||||
static const p2wsh = BitcoinReceivePageOption._('Segwit (P2WSH)');
|
static const p2wsh = BitcoinReceivePageOption._('Segwit (P2WSH)');
|
||||||
static const p2pkh = BitcoinReceivePageOption._('Legacy (P2PKH)');
|
static const p2pkh = BitcoinReceivePageOption._('Legacy (P2PKH)');
|
||||||
|
static const mweb = BitcoinReceivePageOption._('MWEB');
|
||||||
|
|
||||||
const BitcoinReceivePageOption._(this.value);
|
const BitcoinReceivePageOption._(this.value);
|
||||||
|
|
||||||
|
@ -24,12 +25,19 @@ class BitcoinReceivePageOption implements ReceivePageOption {
|
||||||
BitcoinReceivePageOption.p2pkh
|
BitcoinReceivePageOption.p2pkh
|
||||||
];
|
];
|
||||||
|
|
||||||
|
static const allLitecoin = [
|
||||||
|
BitcoinReceivePageOption.p2wpkh,
|
||||||
|
BitcoinReceivePageOption.mweb
|
||||||
|
];
|
||||||
|
|
||||||
factory BitcoinReceivePageOption.fromType(BitcoinAddressType type) {
|
factory BitcoinReceivePageOption.fromType(BitcoinAddressType type) {
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case SegwitAddresType.p2tr:
|
case SegwitAddresType.p2tr:
|
||||||
return BitcoinReceivePageOption.p2tr;
|
return BitcoinReceivePageOption.p2tr;
|
||||||
case SegwitAddresType.p2wsh:
|
case SegwitAddresType.p2wsh:
|
||||||
return BitcoinReceivePageOption.p2wsh;
|
return BitcoinReceivePageOption.p2wsh;
|
||||||
|
case SegwitAddresType.mweb:
|
||||||
|
return BitcoinReceivePageOption.mweb;
|
||||||
case P2pkhAddressType.p2pkh:
|
case P2pkhAddressType.p2pkh:
|
||||||
return BitcoinReceivePageOption.p2pkh;
|
return BitcoinReceivePageOption.p2pkh;
|
||||||
case P2shAddressType.p2wpkhInP2sh:
|
case P2shAddressType.p2wpkhInP2sh:
|
||||||
|
|
|
@ -1337,6 +1337,8 @@ BitcoinBaseAddress addressTypeFromStr(String address, BasedUtxoNetwork network)
|
||||||
return P2wshAddress.fromAddress(address: address, network: network);
|
return P2wshAddress.fromAddress(address: address, network: network);
|
||||||
} else if (P2trAddress.regex.hasMatch(address)) {
|
} else if (P2trAddress.regex.hasMatch(address)) {
|
||||||
return P2trAddress.fromAddress(address: address, network: network);
|
return P2trAddress.fromAddress(address: address, network: network);
|
||||||
|
} else if (MwebAddress.regex.hasMatch(address)) {
|
||||||
|
return MwebAddress.fromAddress(address: address, network: network);
|
||||||
} else {
|
} else {
|
||||||
return P2wpkhAddress.fromAddress(address: address, network: network);
|
return P2wpkhAddress.fromAddress(address: address, network: network);
|
||||||
}
|
}
|
||||||
|
@ -1351,6 +1353,8 @@ BitcoinAddressType _getScriptType(BitcoinBaseAddress type) {
|
||||||
return SegwitAddresType.p2wsh;
|
return SegwitAddresType.p2wsh;
|
||||||
} else if (type is P2trAddress) {
|
} else if (type is P2trAddress) {
|
||||||
return SegwitAddresType.p2tr;
|
return SegwitAddresType.p2tr;
|
||||||
|
} else if (type is MwebAddress) {
|
||||||
|
return SegwitAddresType.mweb;
|
||||||
} else {
|
} else {
|
||||||
return SegwitAddresType.p2wpkh;
|
return SegwitAddresType.p2wpkh;
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,6 +16,7 @@ const List<BitcoinAddressType> ADDRESS_TYPES = [
|
||||||
P2pkhAddressType.p2pkh,
|
P2pkhAddressType.p2pkh,
|
||||||
SegwitAddresType.p2tr,
|
SegwitAddresType.p2tr,
|
||||||
SegwitAddresType.p2wsh,
|
SegwitAddresType.p2wsh,
|
||||||
|
SegwitAddresType.mweb,
|
||||||
P2shAddressType.p2wpkhInP2sh,
|
P2shAddressType.p2wpkhInP2sh,
|
||||||
];
|
];
|
||||||
|
|
||||||
|
@ -154,6 +155,7 @@ abstract class ElectrumWalletAddressesBase extends WalletAddresses with Store {
|
||||||
await _generateInitialAddresses(type: P2pkhAddressType.p2pkh);
|
await _generateInitialAddresses(type: P2pkhAddressType.p2pkh);
|
||||||
} else if (walletInfo.type == WalletType.litecoin) {
|
} else if (walletInfo.type == WalletType.litecoin) {
|
||||||
await _generateInitialAddresses();
|
await _generateInitialAddresses();
|
||||||
|
await _generateInitialAddresses(type: SegwitAddresType.mweb);
|
||||||
} else if (walletInfo.type == WalletType.bitcoin) {
|
} else if (walletInfo.type == WalletType.bitcoin) {
|
||||||
await _generateInitialAddresses();
|
await _generateInitialAddresses();
|
||||||
await _generateInitialAddresses(type: P2pkhAddressType.p2pkh);
|
await _generateInitialAddresses(type: P2pkhAddressType.p2pkh);
|
||||||
|
@ -217,6 +219,10 @@ abstract class ElectrumWalletAddressesBase extends WalletAddresses with Store {
|
||||||
{required int index, required bitcoin.HDWallet hd, BitcoinAddressType? addressType}) =>
|
{required int index, required bitcoin.HDWallet hd, BitcoinAddressType? addressType}) =>
|
||||||
'';
|
'';
|
||||||
|
|
||||||
|
Future<String> getAddressAsync(
|
||||||
|
{required int index, required bitcoin.HDWallet hd, BitcoinAddressType? addressType}) async =>
|
||||||
|
getAddress(index: index, hd: hd, addressType: addressType);
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<void> updateAddressesInBox() async {
|
Future<void> updateAddressesInBox() async {
|
||||||
try {
|
try {
|
||||||
|
@ -328,7 +334,7 @@ abstract class ElectrumWalletAddressesBase extends WalletAddresses with Store {
|
||||||
|
|
||||||
for (var i = startIndex; i < count + startIndex; i++) {
|
for (var i = startIndex; i < count + startIndex; i++) {
|
||||||
final address = BitcoinAddressRecord(
|
final address = BitcoinAddressRecord(
|
||||||
getAddress(index: i, hd: _getHd(isHidden), addressType: type ?? addressPageType),
|
await getAddressAsync(index: i, hd: _getHd(isHidden), addressType: type ?? addressPageType),
|
||||||
index: i,
|
index: i,
|
||||||
isHidden: isHidden,
|
isHidden: isHidden,
|
||||||
type: type ?? addressPageType,
|
type: type ?? addressPageType,
|
||||||
|
|
|
@ -1,8 +1,11 @@
|
||||||
|
import 'package:convert/convert.dart';
|
||||||
import 'package:bitcoin_base/bitcoin_base.dart';
|
import 'package:bitcoin_base/bitcoin_base.dart';
|
||||||
import 'package:bitcoin_flutter/bitcoin_flutter.dart' as bitcoin;
|
import 'package:bitcoin_flutter/bitcoin_flutter.dart';
|
||||||
import 'package:cw_bitcoin/utils.dart';
|
import 'package:cw_bitcoin/utils.dart';
|
||||||
import 'package:cw_bitcoin/electrum_wallet_addresses.dart';
|
import 'package:cw_bitcoin/electrum_wallet_addresses.dart';
|
||||||
import 'package:cw_core/wallet_info.dart';
|
import 'package:cw_core/wallet_info.dart';
|
||||||
|
import 'package:cw_mweb/cw_mweb.dart';
|
||||||
|
import 'package:cw_mweb/mwebd.pb.dart';
|
||||||
import 'package:mobx/mobx.dart';
|
import 'package:mobx/mobx.dart';
|
||||||
|
|
||||||
part 'litecoin_wallet_addresses.g.dart';
|
part 'litecoin_wallet_addresses.g.dart';
|
||||||
|
@ -21,8 +24,40 @@ abstract class LitecoinWalletAddressesBase extends ElectrumWalletAddresses with
|
||||||
super.initialChangeAddressIndex,
|
super.initialChangeAddressIndex,
|
||||||
}) : super(walletInfo);
|
}) : super(walletInfo);
|
||||||
|
|
||||||
|
List<String> mweb_addrs = [];
|
||||||
|
|
||||||
|
Future<void> topUpMweb(int index) async {
|
||||||
|
while (mweb_addrs.length - index < 1000) {
|
||||||
|
final length = mweb_addrs.length;
|
||||||
|
final scanSecret = mainHd.derive(0).privKey!;
|
||||||
|
final spendPubkey = mainHd.derive(1).pubKey!;
|
||||||
|
final stub = await CwMweb.stub();
|
||||||
|
final resp = await stub.addresses(AddressRequest(
|
||||||
|
fromIndex: length,
|
||||||
|
toIndex: index + 1000,
|
||||||
|
scanSecret: hex.decode(scanSecret),
|
||||||
|
spendPubkey: hex.decode(spendPubkey),
|
||||||
|
));
|
||||||
|
if (mweb_addrs.length == length) {
|
||||||
|
mweb_addrs.addAll(resp.address);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
String getAddress(
|
String getAddress({required int index, required HDWallet hd, BitcoinAddressType? addressType}) {
|
||||||
{required int index, required bitcoin.HDWallet hd, BitcoinAddressType? addressType}) =>
|
if (addressType == SegwitAddresType.mweb) {
|
||||||
generateP2WPKHAddress(hd: hd, index: index, network: network);
|
topUpMweb(index);
|
||||||
|
return hd == sideHd ? mweb_addrs[0] : mweb_addrs[index+1];
|
||||||
|
}
|
||||||
|
return generateP2WPKHAddress(hd: hd, index: index, network: network);
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<String> getAddressAsync({required int index, required HDWallet hd, BitcoinAddressType? addressType}) async {
|
||||||
|
if (addressType == SegwitAddresType.mweb) {
|
||||||
|
await topUpMweb(index);
|
||||||
|
}
|
||||||
|
return getAddress(index: index, hd: hd, addressType: addressType);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -86,11 +86,9 @@ packages:
|
||||||
bitcoin_base:
|
bitcoin_base:
|
||||||
dependency: "direct main"
|
dependency: "direct main"
|
||||||
description:
|
description:
|
||||||
path: "."
|
path: "../../bitcoin_base"
|
||||||
ref: cake-update-v2
|
relative: true
|
||||||
resolved-ref: "01d844a5f5a520a31df5254e34169af4664aa769"
|
source: path
|
||||||
url: "https://github.com/cake-tech/bitcoin_base.git"
|
|
||||||
source: git
|
|
||||||
version: "4.2.0"
|
version: "4.2.0"
|
||||||
bitcoin_flutter:
|
bitcoin_flutter:
|
||||||
dependency: "direct main"
|
dependency: "direct main"
|
||||||
|
|
|
@ -31,9 +31,7 @@ dependencies:
|
||||||
unorm_dart: ^0.2.0
|
unorm_dart: ^0.2.0
|
||||||
cryptography: ^2.0.5
|
cryptography: ^2.0.5
|
||||||
bitcoin_base:
|
bitcoin_base:
|
||||||
git:
|
path: ../../bitcoin_base
|
||||||
url: https://github.com/cake-tech/bitcoin_base.git
|
|
||||||
ref: cake-update-v2
|
|
||||||
blockchain_utils: ^2.1.1
|
blockchain_utils: ^2.1.1
|
||||||
cw_mweb:
|
cw_mweb:
|
||||||
path: ../cw_mweb
|
path: ../cw_mweb
|
||||||
|
|
|
@ -30,9 +30,7 @@ dependencies:
|
||||||
url: https://github.com/cake-tech/bitbox-flutter.git
|
url: https://github.com/cake-tech/bitbox-flutter.git
|
||||||
ref: Add-Support-For-OP-Return-data
|
ref: Add-Support-For-OP-Return-data
|
||||||
bitcoin_base:
|
bitcoin_base:
|
||||||
git:
|
path: ../../bitcoin_base
|
||||||
url: https://github.com/cake-tech/bitcoin_base.git
|
|
||||||
ref: cake-update-v2
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -242,6 +242,8 @@ class CWBitcoin extends Bitcoin {
|
||||||
return SegwitAddresType.p2tr;
|
return SegwitAddresType.p2tr;
|
||||||
case BitcoinReceivePageOption.p2wsh:
|
case BitcoinReceivePageOption.p2wsh:
|
||||||
return SegwitAddresType.p2wsh;
|
return SegwitAddresType.p2wsh;
|
||||||
|
case BitcoinReceivePageOption.mweb:
|
||||||
|
return SegwitAddresType.mweb;
|
||||||
case BitcoinReceivePageOption.p2wpkh:
|
case BitcoinReceivePageOption.p2wpkh:
|
||||||
default:
|
default:
|
||||||
return SegwitAddresType.p2wpkh;
|
return SegwitAddresType.p2wpkh;
|
||||||
|
|
|
@ -219,7 +219,8 @@ class AddressPage extends BasePage {
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
if (addressListViewModel.type == WalletType.bitcoin) {
|
if (addressListViewModel.type == WalletType.bitcoin ||
|
||||||
|
addressListViewModel.type == WalletType.litecoin) {
|
||||||
addressListViewModel.setAddressType(bitcoin!.getBitcoinAddressType(option));
|
addressListViewModel.setAddressType(bitcoin!.getBitcoinAddressType(option));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
import 'package:cake_wallet/bitcoin/bitcoin.dart';
|
import 'package:cake_wallet/bitcoin/bitcoin.dart';
|
||||||
|
import 'package:cw_bitcoin/bitcoin_receive_page_option.dart';
|
||||||
import 'package:cw_core/receive_page_option.dart';
|
import 'package:cw_core/receive_page_option.dart';
|
||||||
import 'package:cw_core/wallet_base.dart';
|
import 'package:cw_core/wallet_base.dart';
|
||||||
import 'package:cw_core/wallet_type.dart';
|
import 'package:cw_core/wallet_type.dart';
|
||||||
|
@ -11,19 +12,30 @@ class ReceiveOptionViewModel = ReceiveOptionViewModelBase with _$ReceiveOptionVi
|
||||||
abstract class ReceiveOptionViewModelBase with Store {
|
abstract class ReceiveOptionViewModelBase with Store {
|
||||||
ReceiveOptionViewModelBase(this._wallet, this.initialPageOption)
|
ReceiveOptionViewModelBase(this._wallet, this.initialPageOption)
|
||||||
: selectedReceiveOption = initialPageOption ??
|
: selectedReceiveOption = initialPageOption ??
|
||||||
(_wallet.type == WalletType.bitcoin
|
(_wallet.type == WalletType.bitcoin ||
|
||||||
|
_wallet.type == WalletType.litecoin
|
||||||
? bitcoin!.getSelectedAddressType(_wallet)
|
? bitcoin!.getSelectedAddressType(_wallet)
|
||||||
: ReceivePageOption.mainnet),
|
: ReceivePageOption.mainnet),
|
||||||
_options = [] {
|
_options = [] {
|
||||||
final walletType = _wallet.type;
|
switch (_wallet.type) {
|
||||||
_options = walletType == WalletType.haven
|
case WalletType.bitcoin:
|
||||||
? [ReceivePageOption.mainnet]
|
_options = [
|
||||||
: walletType == WalletType.bitcoin
|
...bitcoin!.getBitcoinReceivePageOptions(),
|
||||||
? [
|
...ReceivePageOptions.where((element) => element != ReceivePageOption.mainnet)
|
||||||
...bitcoin!.getBitcoinReceivePageOptions(),
|
];
|
||||||
...ReceivePageOptions.where((element) => element != ReceivePageOption.mainnet)
|
break;
|
||||||
]
|
case WalletType.litecoin:
|
||||||
: ReceivePageOptions;
|
_options = [
|
||||||
|
...BitcoinReceivePageOption.allLitecoin,
|
||||||
|
...ReceivePageOptions.where((element) => element != ReceivePageOption.mainnet)
|
||||||
|
];
|
||||||
|
break;
|
||||||
|
case WalletType.haven:
|
||||||
|
_options = [ReceivePageOption.mainnet];
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
_options = ReceivePageOptions;
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
final WalletBase _wallet;
|
final WalletBase _wallet;
|
||||||
|
|
|
@ -404,7 +404,8 @@ abstract class WalletAddressListViewModelBase extends WalletChangeListenerViewMo
|
||||||
|
|
||||||
@action
|
@action
|
||||||
Future<void> setAddressType(dynamic option) async {
|
Future<void> setAddressType(dynamic option) async {
|
||||||
if (wallet.type == WalletType.bitcoin) {
|
if (wallet.type == WalletType.bitcoin ||
|
||||||
|
wallet.type == WalletType.litecoin) {
|
||||||
await bitcoin!.setAddressType(wallet, option);
|
await bitcoin!.setAddressType(wallet, option);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue