From 2e9f6887757b48b3732030672db3d1e91168fd8c Mon Sep 17 00:00:00 2001 From: Matthew Fosse Date: Fri, 12 Apr 2024 09:15:03 -0700 Subject: [PATCH] fixes for electrum signing --- cw_bitcoin/lib/electrum_wallet.dart | 66 ++++++------------- cw_nano/lib/nano_wallet.dart | 16 +---- cw_nano/pubspec.lock | 4 +- lib/view_model/dashboard/sign_view_model.dart | 1 + 4 files changed, 23 insertions(+), 64 deletions(-) diff --git a/cw_bitcoin/lib/electrum_wallet.dart b/cw_bitcoin/lib/electrum_wallet.dart index 8ea0d9110..1ac0c2f18 100644 --- a/cw_bitcoin/lib/electrum_wallet.dart +++ b/cw_bitcoin/lib/electrum_wallet.dart @@ -1231,14 +1231,11 @@ abstract class ElectrumWalletBase @override Future signMessage(String message, {String? address = null}) async { - // final index = address != null - // ? walletAddresses.allAddresses.firstWhere((element) => element.address == address).index - // : null; - // final HD = index == null ? hd : hd.derive(index); - // final HD = hd.derive(0); - // return base64Encode(HD.signMessage(message)); - // hd.privKey - final priv = ECPrivate.fromHex(hd.privKey!); + final index = address != null + ? walletAddresses.allAddresses.firstWhere((element) => element.address == address).index + : null; + final HD = index == null ? hd : hd.derive(index); + final priv = ECPrivate.fromHex(HD.privKey!); String messagePrefix = '\x18Bitcoin Signed Message:\n'; return priv.signMessage(utf8.encode(message), messagePrefix: messagePrefix); } @@ -1249,60 +1246,35 @@ abstract class ElectrumWalletBase return false; } - // final decode = List.unmodifiable(Base58Decoder.decode(address)); - - // /// Extract script bytes excluding version and checksum. - // final List scriptBytes = decode.sublist(1, decode.length - Base58Const.checksumByteLen); - - // scriptBytes == hash160 (public key) - String messagePrefix = '\x18Bitcoin Signed Message:\n'; - // ECDSASignature signature = ECDSASignature.fromBytes(ascii.encode(signature), generator) - // final btcSigner = BitcoinVerifier.fromKeyBytes([]); - // btcSigner.verifyKey.verify(signature, digest) - print("@@@@@@@@@111111111111"); final messageHash = QuickCrypto.sha256Hash( BitcoinSignerUtils.magicMessage(utf8.encode(message), messagePrefix)); final generator = Curves.generatorSecp256k1; final sigDecodedBytes = hex.decode(signature); - print(signature); final sig = ECDSASignature.fromBytes(sigDecodedBytes, generator); - print("######################"); - // final sigBytes = utf8.encode(signature); - // print(sigBytes[0]); final pubKey = sig.recoverPublicKey(messageHash, generator, sigDecodedBytes[0]); final recoveredPub = ECPublic.fromBytes(pubKey!.toBytes()); - print("recovered!: ${HEX.encode(pubKey.toBytes())} actual: ${hd.pubKey}"); - // final recoveredAddress = recoveredPub.toP2wpkhAddress().toAddress(network); - // final recoveredAddress = recoveredPub.toP2wshAddress().toAddress(network); - // final recoveredAddress = recoveredPub.toP2wpkhInP2sh().toAddress(network); - final recoveredAddress = recoveredPub.toP2wpkhAddress().toAddress(network); - print("ACTUAL: $address"); - print(recoveredPub.toHash160()); - print(recoveredPub.toP2wshInP2sh().toAddress(network)); - print(recoveredPub.toP2wpkhAddress().toAddress(network)); + // get the address type: + final baseAddress = addressTypeFromStr(address, network); + String? recoveredAddress; - // print("$address $recoveredAddress"); + if (baseAddress is P2pkAddress) { + recoveredAddress = recoveredPub.toP2pkAddress().toAddress(network); + } else if (baseAddress is P2pkhAddress) { + recoveredAddress = recoveredPub.toP2pkhAddress().toAddress(network); + } else if (baseAddress is P2wshAddress) { + recoveredAddress = recoveredPub.toP2wshAddress().toAddress(network); + } else if (baseAddress is P2wpkhAddress) { + recoveredAddress = recoveredPub.toP2wpkhAddress().toAddress(network); + } else { + return false; + } if (recoveredAddress == address) { return true; } - // ECPublic pub = ECPublic.fromBytes(pubKey!.toBytes()); - - // return pub.verify( - // utf8.encode(message), - // sigBytes, - // messagePrefix: messagePrefix, - // ); - - // final index = address != null - // ? walletAddresses.allAddresses.firstWhere((element) => element.address == address).index - // : null; - // final HD = index == null ? hd : hd.derive(index); - // final HD = hd.derive(0); - // return HD.verify(message: message, signature: base64Decode(signature)); return false; } diff --git a/cw_nano/lib/nano_wallet.dart b/cw_nano/lib/nano_wallet.dart index 45e52001c..111cccb15 100644 --- a/cw_nano/lib/nano_wallet.dart +++ b/cw_nano/lib/nano_wallet.dart @@ -27,7 +27,6 @@ import 'package:cw_nano/nano_wallet_addresses.dart'; import 'package:cw_core/wallet_base.dart'; import 'package:bip39/bip39.dart' as bip39; import 'package:nanoutil/nanoutil.dart'; -import 'package:nanodart/nanodart.dart' as nd; part 'nano_wallet.g.dart'; @@ -499,22 +498,9 @@ abstract class NanoWalletBase await Directory(currentDirPath).delete(recursive: true); } - String hash(String message) { - return nd.NanoHelpers.byteToHex( - nd.Blake2b.digest256([nd.NanoHelpers.stringToBytesUtf8(message)]), - ).toUpperCase(); - } - @override Future signMessage(String message, {String? address = null}) async { - // return NanoSignatures.sign(message, privateKey!); - // return nd.NanoHelpers.byteToHex( - // nd.Signature.detached( - // nd.NanoHelpers.hexToBytes(hash(message)), - // nd.NanoHelpers.hexToBytes(privateKey!), - // ), - // ); - return nd.NanoSignatures.signBlock(hash(message), privateKey!); + return NanoSignatures.sign(message, privateKey!); } @override diff --git a/cw_nano/pubspec.lock b/cw_nano/pubspec.lock index 37db3df28..619d39662 100644 --- a/cw_nano/pubspec.lock +++ b/cw_nano/pubspec.lock @@ -475,8 +475,8 @@ packages: dependency: "direct main" description: path: "." - ref: dd63c20a3d8956bba26732a36e14fda24ccef284 - resolved-ref: dd63c20a3d8956bba26732a36e14fda24ccef284 + ref: "59c021b08bdd3324607ebebd5c8e2c488aa80a37" + resolved-ref: "59c021b08bdd3324607ebebd5c8e2c488aa80a37" url: "https://github.com/perishllc/nanoutil.git" source: git version: "1.0.2" diff --git a/lib/view_model/dashboard/sign_view_model.dart b/lib/view_model/dashboard/sign_view_model.dart index bda9c3514..ab65883f4 100644 --- a/lib/view_model/dashboard/sign_view_model.dart +++ b/lib/view_model/dashboard/sign_view_model.dart @@ -21,6 +21,7 @@ abstract class SignViewModelBase with Store { bool get signIncludesAddress => [ WalletType.monero, + WalletType.bitcoin, WalletType.haven, ].contains(wallet.type);