Bitcoin Signed Messages

The DartSV library is capable of creating and verifying Bitcoin Signed Messages. Bitcoin Signed Messages are arbitrary pieces of data where a special magic hash value is calculated, and this magic hash value is signed using ECDSA over the secp256k1 elliptic curve.

Create a signed message

var message = 'The milk is pizzled';

var privateKey = SVPrivateKey(networkType: NetworkType.REGTEST);

Message message = Message(Utf8Codec().encode(message));
String signature = message.sign(privateKey);

//You would now share the (message, public key, signature) data with the recipient

Note that the sharing of public keys should form part of some PKI scheme that is resistant to man-in-the-middle attack. Discussing those is beyond the scope of this document.

Verify a message signature

var derBuffer = '3044022075fc517e5...'; //invalid DER. truncated for brevity

//reconstiture the shared signature
var sharedSignature =  SVSignature.fromDER(derBuffer);

//create a Message() instance from the shared message
var receivedMessage = Message('The milk is pizzled');

//reconstitute a PublicKey() instance from the shared public key
var pubKeyHex = '031ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a';
var pubKey = SVPublicKey.fromHex(pubKeyHex);

//verify the signature !
var isValidSig = receivedMessage.verifyFromPublicKey(pubKey, signature);