mirror of
https://github.com/cake-tech/cake_wallet.git
synced 2024-12-23 12:09:43 +00:00
electrum messages working!
This commit is contained in:
parent
f5df81af8c
commit
70f2ac4386
1 changed files with 38 additions and 25 deletions
|
@ -1236,7 +1236,7 @@ abstract class ElectrumWalletBase
|
|||
: null;
|
||||
final HD = index == null ? hd : hd.derive(index);
|
||||
final priv = ECPrivate.fromHex(HD.privKey!);
|
||||
print(priv.prive.publicKey.pubKey.toHex());
|
||||
print(priv.getPublic().toHex());
|
||||
String messagePrefix = '\x18Bitcoin Signed Message:\n';
|
||||
return priv.signMessage(utf8.encode(message), messagePrefix: messagePrefix);
|
||||
}
|
||||
|
@ -1247,20 +1247,36 @@ abstract class ElectrumWalletBase
|
|||
return false;
|
||||
}
|
||||
|
||||
final sigDecodedBytes = hex.decode(signature);
|
||||
|
||||
if (sigDecodedBytes.length != 64 && sigDecodedBytes.length != 65) {
|
||||
throw ArgumentException(
|
||||
"bitcoin signature must be 64 bytes without recover-id or 65 bytes with recover-id");
|
||||
}
|
||||
|
||||
String messagePrefix = '\x18Bitcoin Signed Message:\n';
|
||||
final messageHash = QuickCrypto.sha256Hash(
|
||||
BitcoinSignerUtils.magicMessage(utf8.encode(message), messagePrefix));
|
||||
final generator = Curves.generatorSecp256k1;
|
||||
final sigDecodedBytes = hex.decode(signature);
|
||||
final sig = ECDSASignature.fromBytes(sigDecodedBytes, generator);
|
||||
final pubKey = sig.recoverPublicKey(messageHash, generator, sigDecodedBytes[0]);
|
||||
|
||||
List<int> correctSignature =
|
||||
sigDecodedBytes.length == 65 ? sigDecodedBytes.sublist(1) : List.from(sigDecodedBytes);
|
||||
List<int> rBytes = correctSignature.sublist(0, 32);
|
||||
List<int> sBytes = correctSignature.sublist(32);
|
||||
final sig = ECDSASignature(BigintUtils.fromBytes(rBytes), BigintUtils.fromBytes(sBytes));
|
||||
|
||||
List<int> possibleRecoverIds = [0, 1, 2, 3];
|
||||
if (sigDecodedBytes.length == 65) {
|
||||
possibleRecoverIds = [sigDecodedBytes[0]];
|
||||
}
|
||||
|
||||
final baseAddress = addressTypeFromStr(address, network);
|
||||
|
||||
for (int recoveryId in possibleRecoverIds) {
|
||||
final pubKey = sig.recoverPublicKey(messageHash, Curves.generatorSecp256k1, recoveryId);
|
||||
final recoveredPub = ECPublic.fromBytes(pubKey!.toBytes());
|
||||
|
||||
// get the address type:
|
||||
final baseAddress = addressTypeFromStr(address, network);
|
||||
String? recoveredAddress;
|
||||
|
||||
|
||||
if (baseAddress is P2pkAddress) {
|
||||
recoveredAddress = recoveredPub.toP2pkAddress().toAddress(network);
|
||||
} else if (baseAddress is P2pkhAddress) {
|
||||
|
@ -1271,13 +1287,10 @@ abstract class ElectrumWalletBase
|
|||
recoveredAddress = recoveredPub.toP2wpkhAddress().toAddress(network);
|
||||
}
|
||||
|
||||
print("@@@@@@@@@@@@@@@@@@@@@@@@");
|
||||
print("address: $address recoveredAddress: $recoveredAddress");
|
||||
print(recoveredPub.publicKey.toHex());
|
||||
|
||||
if (recoveredAddress == address) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue