fixes for electrum signing

This commit is contained in:
Matthew Fosse 2024-04-12 09:15:03 -07:00
parent 827c1097f3
commit 2e9f688775
4 changed files with 23 additions and 64 deletions

View file

@ -1231,14 +1231,11 @@ abstract class ElectrumWalletBase
@override @override
Future<String> signMessage(String message, {String? address = null}) async { Future<String> signMessage(String message, {String? address = null}) async {
// final index = address != null final index = address != null
// ? walletAddresses.allAddresses.firstWhere((element) => element.address == address).index ? walletAddresses.allAddresses.firstWhere((element) => element.address == address).index
// : null; : null;
// final HD = index == null ? hd : hd.derive(index); final HD = index == null ? hd : hd.derive(index);
// final HD = hd.derive(0); final priv = ECPrivate.fromHex(HD.privKey!);
// return base64Encode(HD.signMessage(message));
// hd.privKey
final priv = ECPrivate.fromHex(hd.privKey!);
String messagePrefix = '\x18Bitcoin Signed Message:\n'; String messagePrefix = '\x18Bitcoin Signed Message:\n';
return priv.signMessage(utf8.encode(message), messagePrefix: messagePrefix); return priv.signMessage(utf8.encode(message), messagePrefix: messagePrefix);
} }
@ -1249,60 +1246,35 @@ abstract class ElectrumWalletBase
return false; return false;
} }
// final decode = List<int>.unmodifiable(Base58Decoder.decode(address));
// /// Extract script bytes excluding version and checksum.
// final List<int> scriptBytes = decode.sublist(1, decode.length - Base58Const.checksumByteLen);
// scriptBytes == hash160 (public key)
String messagePrefix = '\x18Bitcoin Signed Message:\n'; 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( final messageHash = QuickCrypto.sha256Hash(
BitcoinSignerUtils.magicMessage(utf8.encode(message), messagePrefix)); BitcoinSignerUtils.magicMessage(utf8.encode(message), messagePrefix));
final generator = Curves.generatorSecp256k1; final generator = Curves.generatorSecp256k1;
final sigDecodedBytes = hex.decode(signature); final sigDecodedBytes = hex.decode(signature);
print(signature);
final sig = ECDSASignature.fromBytes(sigDecodedBytes, generator); final sig = ECDSASignature.fromBytes(sigDecodedBytes, generator);
print("######################");
// final sigBytes = utf8.encode(signature);
// print(sigBytes[0]);
final pubKey = sig.recoverPublicKey(messageHash, generator, sigDecodedBytes[0]); final pubKey = sig.recoverPublicKey(messageHash, generator, sigDecodedBytes[0]);
final recoveredPub = ECPublic.fromBytes(pubKey!.toBytes()); 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); // get the address type:
print("ACTUAL: $address"); final baseAddress = addressTypeFromStr(address, network);
print(recoveredPub.toHash160()); String? recoveredAddress;
print(recoveredPub.toP2wshInP2sh().toAddress(network));
print(recoveredPub.toP2wpkhAddress().toAddress(network));
// 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) { if (recoveredAddress == address) {
return true; 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; return false;
} }

View file

@ -27,7 +27,6 @@ import 'package:cw_nano/nano_wallet_addresses.dart';
import 'package:cw_core/wallet_base.dart'; import 'package:cw_core/wallet_base.dart';
import 'package:bip39/bip39.dart' as bip39; import 'package:bip39/bip39.dart' as bip39;
import 'package:nanoutil/nanoutil.dart'; import 'package:nanoutil/nanoutil.dart';
import 'package:nanodart/nanodart.dart' as nd;
part 'nano_wallet.g.dart'; part 'nano_wallet.g.dart';
@ -499,22 +498,9 @@ abstract class NanoWalletBase
await Directory(currentDirPath).delete(recursive: true); await Directory(currentDirPath).delete(recursive: true);
} }
String hash(String message) {
return nd.NanoHelpers.byteToHex(
nd.Blake2b.digest256([nd.NanoHelpers.stringToBytesUtf8(message)]),
).toUpperCase();
}
@override @override
Future<String> signMessage(String message, {String? address = null}) async { Future<String> signMessage(String message, {String? address = null}) async {
// return NanoSignatures.sign(message, privateKey!); 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!);
} }
@override @override

View file

@ -475,8 +475,8 @@ packages:
dependency: "direct main" dependency: "direct main"
description: description:
path: "." path: "."
ref: dd63c20a3d8956bba26732a36e14fda24ccef284 ref: "59c021b08bdd3324607ebebd5c8e2c488aa80a37"
resolved-ref: dd63c20a3d8956bba26732a36e14fda24ccef284 resolved-ref: "59c021b08bdd3324607ebebd5c8e2c488aa80a37"
url: "https://github.com/perishllc/nanoutil.git" url: "https://github.com/perishllc/nanoutil.git"
source: git source: git
version: "1.0.2" version: "1.0.2"

View file

@ -21,6 +21,7 @@ abstract class SignViewModelBase with Store {
bool get signIncludesAddress => [ bool get signIncludesAddress => [
WalletType.monero, WalletType.monero,
WalletType.bitcoin,
WalletType.haven, WalletType.haven,
].contains(wallet.type); ].contains(wallet.type);