Skip to content

Sign message

shigeyuki azuchi edited this page Jun 23, 2021 · 1 revision

Bitcoin::SignMessage class provides the ability to sign and verify Bitcoin messages.

Sign

You can generate a signature for any message with the following code:

message = 'Trust no one'
private_key = 'd97f5108f11cda6eeebaaa420fef0726b1f898060b98489fa3098463c0032866'
key = Bitcoin::Key.new(priv_key: private_key, key_type: Bitcoin::Key::TYPES[:compressed])

signature = Bitcoin::MessageSign.sign_message(key, message)
=> 'IPojfrX2dfPnH26UegfbGQQLrdK844DlHq5157/P6h57WyuS/Qsl+h/WSVGDF4MUi4rWSswW38oimDYfNNUBUOk='

A prefix will be assigned to the message when it is signed. The default value of the prefix is Bitcoin Signed Message:\n, but it can also be specified with the prefix: keyword argument of sign_message like:

signature = Bitcoin::MessageSign.sign_message(key, message, prefix: `Dogecoin Signed Message:\n`)

Verify

You can validate signature using the address and message.

Bitcoin::MessageSign.verify_message('15CRxFdyRpGZLW9w8HnHvVduizdL5jKNbs', signature, message)
=> true
Clone this wiki locally