Merge pull request #1444 from cake-tech/sign-message-fixes

[wip] fixing
This commit is contained in:
Matthew Fosse 2024-05-10 12:17:05 -07:00 committed by GitHub
commit 3a5bf42069
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 56 additions and 46 deletions

View file

@ -2,6 +2,7 @@ import 'package:bitcoin_base/bitcoin_base.dart';
import 'package:convert/convert.dart'; import 'package:convert/convert.dart';
import 'package:cw_bitcoin/bitcoin_mnemonic.dart'; import 'package:cw_bitcoin/bitcoin_mnemonic.dart';
import 'package:cw_bitcoin/electrum_derivations.dart';
import 'package:cw_bitcoin/psbt_transaction_builder.dart'; import 'package:cw_bitcoin/psbt_transaction_builder.dart';
import 'package:cw_core/crypto_currency.dart'; import 'package:cw_core/crypto_currency.dart';
import 'package:cw_core/unspent_coins_info.dart'; import 'package:cw_core/unspent_coins_info.dart';
@ -39,26 +40,21 @@ abstract class BitcoinWalletBase extends ElectrumWallet with Store {
Map<String, int>? initialChangeAddressIndex, Map<String, int>? initialChangeAddressIndex,
String? passphrase, String? passphrase,
}) : super( }) : super(
mnemonic: mnemonic, mnemonic: mnemonic,
passphrase: passphrase, passphrase: passphrase,
xpub: xpub, xpub: xpub,
password: password, password: password,
walletInfo: walletInfo, walletInfo: walletInfo,
unspentCoinsInfo: unspentCoinsInfo, unspentCoinsInfo: unspentCoinsInfo,
networkType: networkParam == null networkType: networkParam == null
? bitcoin.bitcoin ? bitcoin.bitcoin
: networkParam == BitcoinNetwork.mainnet : networkParam == BitcoinNetwork.mainnet
? bitcoin.bitcoin ? bitcoin.bitcoin
: bitcoin.testnet, : bitcoin.testnet,
initialAddresses: initialAddresses, initialAddresses: initialAddresses,
initialBalance: initialBalance, initialBalance: initialBalance,
seedBytes: seedBytes, seedBytes: seedBytes,
currency: CryptoCurrency.btc) { currency: CryptoCurrency.btc) {
// in a standard BIP44 wallet, mainHd derivation path = m/84'/0'/0'/0 (account 0, index unspecified here)
// the sideHd derivation path = m/84'/0'/0'/1 (account 1, index unspecified here)
// String derivationPath = walletInfo.derivationInfo!.derivationPath!;
// String sideDerivationPath = derivationPath.substring(0, derivationPath.length - 1) + "1";
// final hd = bitcoin.HDWallet.fromSeed(seedBytes, network: networkType);
walletAddresses = BitcoinWalletAddresses( walletAddresses = BitcoinWalletAddresses(
walletInfo, walletInfo,
electrumClient: electrumClient, electrumClient: electrumClient,
@ -134,7 +130,8 @@ abstract class BitcoinWalletBase extends ElectrumWallet with Store {
); );
// set the default if not present: // set the default if not present:
walletInfo.derivationInfo!.derivationPath = snp.derivationPath ?? "m/0'/0"; final electrumPath = electrum_derivations[DerivationType.electrum]!.first.derivationPath!;
walletInfo.derivationInfo!.derivationPath = snp.derivationPath ?? electrumPath;
walletInfo.derivationInfo!.derivationType = snp.derivationType ?? DerivationType.electrum; walletInfo.derivationInfo!.derivationType = snp.derivationType ?? DerivationType.electrum;
Uint8List? seedBytes = null; Uint8List? seedBytes = null;
@ -154,6 +151,10 @@ abstract class BitcoinWalletBase extends ElectrumWallet with Store {
} }
} }
print(walletInfo.derivationInfo!.derivationType);
print(walletInfo.derivationInfo!.derivationPath);
print("@@@@@@@@@@@@@@@");
return BitcoinWallet( return BitcoinWallet(
mnemonic: snp.mnemonic, mnemonic: snp.mnemonic,
xpub: snp.xpub, xpub: snp.xpub,
@ -178,7 +179,8 @@ abstract class BitcoinWalletBase extends ElectrumWallet with Store {
void setLedger(Ledger setLedger, LedgerDevice setLedgerDevice) { void setLedger(Ledger setLedger, LedgerDevice setLedgerDevice) {
_ledger = setLedger; _ledger = setLedger;
_ledgerDevice = setLedgerDevice; _ledgerDevice = setLedgerDevice;
_bitcoinLedgerApp = BitcoinLedgerApp(_ledger!, derivationPath: walletInfo.derivationInfo!.derivationPath!); _bitcoinLedgerApp =
BitcoinLedgerApp(_ledger!, derivationPath: walletInfo.derivationInfo!.derivationPath!);
} }
@override @override
@ -201,16 +203,17 @@ abstract class BitcoinWalletBase extends ElectrumWallet with Store {
final publicKeyAndDerivationPath = publicKeys[utxo.ownerDetails.address.pubKeyHash()]!; final publicKeyAndDerivationPath = publicKeys[utxo.ownerDetails.address.pubKeyHash()]!;
psbtReadyInputs.add(PSBTReadyUtxoWithAddress( psbtReadyInputs.add(PSBTReadyUtxoWithAddress(
utxo: utxo.utxo, utxo: utxo.utxo,
rawTx: rawTx, rawTx: rawTx,
ownerDetails: utxo.ownerDetails, ownerDetails: utxo.ownerDetails,
ownerDerivationPath: publicKeyAndDerivationPath.derivationPath, ownerDerivationPath: publicKeyAndDerivationPath.derivationPath,
ownerMasterFingerprint: masterFingerprint, ownerMasterFingerprint: masterFingerprint,
ownerPublicKey: publicKeyAndDerivationPath.publicKey, ownerPublicKey: publicKeyAndDerivationPath.publicKey,
)); ));
} }
final psbt = PSBTTransactionBuild(inputs: psbtReadyInputs, outputs: outputs, enableRBF: enableRBF); final psbt =
PSBTTransactionBuild(inputs: psbtReadyInputs, outputs: outputs, enableRBF: enableRBF);
final rawHex = await _bitcoinLedgerApp!.signPsbt(_ledgerDevice!, psbt: psbt.psbt); final rawHex = await _bitcoinLedgerApp!.signPsbt(_ledgerDevice!, psbt: psbt.psbt);
return BtcTransaction.fromRaw(hex.encode(rawHex)); return BtcTransaction.fromRaw(hex.encode(rawHex));

View file

@ -17,6 +17,7 @@ import 'package:cw_bitcoin/bitcoin_unspent.dart';
import 'package:cw_bitcoin/bitcoin_wallet_keys.dart'; import 'package:cw_bitcoin/bitcoin_wallet_keys.dart';
import 'package:cw_bitcoin/electrum.dart'; import 'package:cw_bitcoin/electrum.dart';
import 'package:cw_bitcoin/electrum_balance.dart'; import 'package:cw_bitcoin/electrum_balance.dart';
import 'package:cw_bitcoin/electrum_derivations.dart';
import 'package:cw_bitcoin/electrum_transaction_history.dart'; import 'package:cw_bitcoin/electrum_transaction_history.dart';
import 'package:cw_bitcoin/electrum_transaction_info.dart'; import 'package:cw_bitcoin/electrum_transaction_info.dart';
import 'package:cw_bitcoin/electrum_wallet_addresses.dart'; import 'package:cw_bitcoin/electrum_wallet_addresses.dart';
@ -101,10 +102,11 @@ abstract class ElectrumWalletBase
} }
if (seedBytes != null) { if (seedBytes != null) {
final electrumPath = electrum_derivations[DerivationType.electrum]!.first.derivationPath!;
return currency == CryptoCurrency.bch return currency == CryptoCurrency.bch
? bitcoinCashHDWallet(seedBytes) ? bitcoinCashHDWallet(seedBytes)
: bitcoin.HDWallet.fromSeed(seedBytes, network: networkType) : bitcoin.HDWallet.fromSeed(seedBytes, network: networkType)
.derivePath(_hardenedDerivationPath(derivationInfo?.derivationPath ?? "m/0'")); .derivePath(_hardenedDerivationPath(derivationInfo?.derivationPath ?? electrumPath));
} }
return bitcoin.HDWallet.fromBase58(xpub!); return bitcoin.HDWallet.fromBase58(xpub!);
@ -120,6 +122,7 @@ abstract class ElectrumWalletBase
final String? _mnemonic; final String? _mnemonic;
bitcoin.HDWallet get hd => accountHD.derive(0); bitcoin.HDWallet get hd => accountHD.derive(0);
bitcoin.HDWallet get sideHd => accountHD.derive(1);
final String? passphrase; final String? passphrase;
@override @override
@ -247,8 +250,9 @@ abstract class ElectrumWalletBase
final address = addressTypeFromStr(utx.address, network); final address = addressTypeFromStr(utx.address, network);
final hd = final hd =
utx.bitcoinAddressRecord.isHidden ? walletAddresses.sideHd : walletAddresses.mainHd; utx.bitcoinAddressRecord.isHidden ? walletAddresses.sideHd : walletAddresses.mainHd;
final electrumPath = electrum_derivations[DerivationType.electrum]!.first.derivationPath!;
final derivationPath = final derivationPath =
"${_hardenedDerivationPath(walletInfo.derivationInfo?.derivationPath ?? "m/0'")}" "${_hardenedDerivationPath(walletInfo.derivationInfo?.derivationPath ?? electrumPath)}"
"/${utx.bitcoinAddressRecord.isHidden ? "1" : "0"}" "/${utx.bitcoinAddressRecord.isHidden ? "1" : "0"}"
"/${utx.bitcoinAddressRecord.index}"; "/${utx.bitcoinAddressRecord.index}";
final pubKeyHex = hd.derive(utx.bitcoinAddressRecord.index).pubKey!; final pubKeyHex = hd.derive(utx.bitcoinAddressRecord.index).pubKey!;
@ -376,8 +380,9 @@ abstract class ElectrumWalletBase
final hd = final hd =
utx.bitcoinAddressRecord.isHidden ? walletAddresses.sideHd : walletAddresses.mainHd; utx.bitcoinAddressRecord.isHidden ? walletAddresses.sideHd : walletAddresses.mainHd;
final electrumPath = electrum_derivations[DerivationType.electrum]!.first.derivationPath!;
final derivationPath = final derivationPath =
"${_hardenedDerivationPath(walletInfo.derivationInfo?.derivationPath ?? "m/0'")}" "${_hardenedDerivationPath(walletInfo.derivationInfo?.derivationPath ?? electrumPath)}"
"/${utx.bitcoinAddressRecord.isHidden ? "1" : "0"}" "/${utx.bitcoinAddressRecord.isHidden ? "1" : "0"}"
"/${utx.bitcoinAddressRecord.index}"; "/${utx.bitcoinAddressRecord.index}";
final pubKeyHex = hd.derive(utx.bitcoinAddressRecord.index).pubKey!; final pubKeyHex = hd.derive(utx.bitcoinAddressRecord.index).pubKey!;
@ -1376,7 +1381,7 @@ abstract class ElectrumWalletBase
} }
List<int> sigDecodedBytes = []; List<int> sigDecodedBytes = [];
if (signature.endsWith('=')) { if (signature.endsWith('=')) {
sigDecodedBytes = base64.decode(signature); sigDecodedBytes = base64.decode(signature);
} else { } else {

View file

@ -2,6 +2,8 @@ import 'dart:convert';
import 'package:bitcoin_base/bitcoin_base.dart'; import 'package:bitcoin_base/bitcoin_base.dart';
import 'package:cw_bitcoin/bitcoin_address_record.dart'; import 'package:cw_bitcoin/bitcoin_address_record.dart';
import 'package:cw_bitcoin/electrum_balance.dart'; import 'package:cw_bitcoin/electrum_balance.dart';
import 'package:cw_bitcoin/electrum_derivations.dart';
import 'package:cw_bitcoin/electrum_wallet.dart';
import 'package:cw_core/pathForWallet.dart'; import 'package:cw_core/pathForWallet.dart';
import 'package:cw_core/wallet_info.dart'; import 'package:cw_core/wallet_info.dart';
import 'package:cw_core/utils/file.dart'; import 'package:cw_core/utils/file.dart';
@ -57,9 +59,10 @@ class ElectrumWalletSnapshot {
var regularAddressIndexByType = {SegwitAddresType.p2wpkh.toString(): 0}; var regularAddressIndexByType = {SegwitAddresType.p2wpkh.toString(): 0};
var changeAddressIndexByType = {SegwitAddresType.p2wpkh.toString(): 0}; var changeAddressIndexByType = {SegwitAddresType.p2wpkh.toString(): 0};
final derivationType = final electrumPath = electrum_derivations[DerivationType.electrum]!.first.derivationPath!;
DerivationType.values[(data['derivationTypeIndex'] as int?) ?? DerivationType.electrum.index]; final derivationType = DerivationType
final derivationPath = data['derivationPath'] as String? ?? "m/0'/0"; .values[(data['derivationTypeIndex'] as int?) ?? DerivationType.electrum.index];
final derivationPath = data['derivationPath'] as String? ?? electrumPath;
try { try {
regularAddressIndexByType = { regularAddressIndexByType = {

View file

@ -298,12 +298,7 @@ class CWBitcoin extends Bitcoin {
List<DerivationType> types = await compareDerivationMethods(mnemonic: mnemonic, node: node); List<DerivationType> types = await compareDerivationMethods(mnemonic: mnemonic, node: node);
if (types.length == 1 && types.first == DerivationType.electrum) { if (types.length == 1 && types.first == DerivationType.electrum) {
return [ return [
DerivationInfo( electrum_derivations[DerivationType.electrum]!.first
derivationType: DerivationType.electrum,
derivationPath: "m/0'",
description: "Electrum",
scriptType: "p2wpkh",
)
]; ];
} }
@ -395,6 +390,11 @@ class CWBitcoin extends Bitcoin {
return list; return list;
} }
@override
Map<DerivationType, List<DerivationInfo>> getElectrumDerivations() {
return electrum_derivations;
}
@override @override
bool hasTaprootInput(PendingTransaction pendingTransaction) { bool hasTaprootInput(PendingTransaction pendingTransaction) {
return (pendingTransaction as PendingBitcoinTransaction).hasTaprootInputs; return (pendingTransaction as PendingBitcoinTransaction).hasTaprootInputs;

View file

@ -1,3 +1,4 @@
import 'package:cake_wallet/bitcoin/bitcoin.dart';
import 'package:cake_wallet/core/wallet_creation_service.dart'; import 'package:cake_wallet/core/wallet_creation_service.dart';
import 'package:cake_wallet/di.dart'; import 'package:cake_wallet/di.dart';
import 'package:cake_wallet/entities/background_tasks.dart'; import 'package:cake_wallet/entities/background_tasks.dart';
@ -98,10 +99,7 @@ abstract class WalletCreationVMBase with Store {
); );
case WalletType.bitcoin: case WalletType.bitcoin:
case WalletType.litecoin: case WalletType.litecoin:
return DerivationInfo( return bitcoin!.getElectrumDerivations()[DerivationType.electrum]!.first;
derivationType: DerivationType.electrum,
derivationPath: "m/0'",
);
default: default:
return null; return null;
} }

View file

@ -186,6 +186,7 @@ abstract class Bitcoin {
{required String mnemonic, required Node node}); {required String mnemonic, required Node node});
Future<List<DerivationInfo>> getDerivationsFromMnemonic( Future<List<DerivationInfo>> getDerivationsFromMnemonic(
{required String mnemonic, required Node node, String? passphrase}); {required String mnemonic, required Node node, String? passphrase});
Map<DerivationType, List<DerivationInfo>> getElectrumDerivations();
Future<void> setAddressType(Object wallet, dynamic option); Future<void> setAddressType(Object wallet, dynamic option);
ReceivePageOption getSelectedAddressType(Object wallet); ReceivePageOption getSelectedAddressType(Object wallet);
List<ReceivePageOption> getBitcoinReceivePageOptions(); List<ReceivePageOption> getBitcoinReceivePageOptions();