public bool Verify(SignedMessage message) { var r = new byte[32]; var s = new byte[32]; Buffer.BlockCopy(message.SignatureBytes, 1, r, 0, 32); Buffer.BlockCopy(message.SignatureBytes, 33, s, 0, 32); var recId = message.SignatureBytes[0] - 27; var magicData = VariableLengthEncoding.GetVariableLengthStringBytes("Bitcoin Signed Message:\n"); var messageData = VariableLengthEncoding.GetVariableLengthStringBytes(message.Message); var data = new byte[magicData.Length + messageData.Length]; Buffer.BlockCopy(magicData, 0, data, 0, magicData.Length); Buffer.BlockCopy(messageData, 0, data, magicData.Length, messageData.Length); var hash = SHA256.DoubleHash(data); var point = signer.RecoverFromSignature(hash, r.ToBigIntegerUnsigned(true), s.ToBigIntegerUnsigned(true), recId); var pubKeyHash = Hash160.Hash(point.EncodePoint(false)); var addressBytes = new byte[pubKeyHash.Length + 1]; Buffer.BlockCopy(pubKeyHash, 0, addressBytes, 1, pubKeyHash.Length); var address = Base58.EncodeWithCheckSum(addressBytes); if (address == message.Address) { return(true); } return(false); }
public static string GetBitcoinAddress(this ECPoint publicKey, bool compressed = true) { var pubKeyHash = Hash160.Hash(publicKey.EncodePoint(compressed)); byte[] addressBytes = new byte[pubKeyHash.Length + 1]; Buffer.BlockCopy(pubKeyHash, 0, addressBytes, 1, pubKeyHash.Length); return(Base58.EncodeWithCheckSum(addressBytes)); }