bitcoin_message/message.rs
1crate::ix!();
2
3//-------------------------------------------[.cpp/bitcoin/src/util/message.h]
4
5lazy_static!{
6 /*
7 extern const std::string MESSAGE_MAGIC;
8 */
9}
10
11/**
12 | The result of a signed message verification.
13 |
14 | Message verification takes as an input:
15 |
16 | - address (with whose private key the
17 | message is supposed to have been signed)
18 |
19 | - signature
20 |
21 | - message
22 |
23 */
24pub enum MessageVerificationResult {
25
26 /**
27 | The provided address is invalid.
28 |
29 */
30 ERR_INVALID_ADDRESS,
31
32 /**
33 | The provided address is valid but does
34 | not refer to a public key.
35 |
36 */
37 ERR_ADDRESS_NO_KEY,
38
39 /**
40 | The provided signature couldn't be
41 | parsed (maybe invalid base64).
42 |
43 */
44 ERR_MALFORMED_SIGNATURE,
45
46 /**
47 | A public key could not be recovered from
48 | the provided signature and message.
49 |
50 */
51 ERR_PUBKEY_NOT_RECOVERED,
52
53 /**
54 | The message was not signed with the private
55 | key of the provided address.
56 |
57 */
58 ERR_NOT_SIGNED,
59
60 /**
61 | The message verification was successful.
62 |
63 */
64 OK
65}
66
67//-------------------------------------------[.cpp/bitcoin/src/util/message.cpp]
68
69/**
70 | Text used to signify that a signed message
71 | follows and to prevent inadvertently
72 | signing a transaction.
73 |
74 */
75pub const MESSAGE_MAGIC: &'static str = "Bitcoin Signed Message:\n";
76
77/**
78 | Verify a signed message.
79 |
80 | -----------
81 | @param[in] address
82 |
83 | Signer's bitcoin address, it must refer
84 | to a public key.
85 | ----------
86 | @param[in] signature
87 |
88 | The signature in base64 format.
89 | ----------
90 | @param[in] message
91 |
92 | The message that was signed.
93 |
94 | -----------
95 | @return
96 |
97 | result code
98 |
99 */
100pub fn message_verify(
101 address: &String,
102 signature: &String,
103 message: &String) -> MessageVerificationResult {
104
105 todo!();
106 /*
107 TxDestination destination = DecodeDestination(address);
108 if (!IsValidDestination(destination)) {
109 return MessageVerificationResult::ERR_INVALID_ADDRESS;
110 }
111
112 if (std::get_if<PKHash>(&destination) == nullptr) {
113 return MessageVerificationResult::ERR_ADDRESS_NO_KEY;
114 }
115
116 bool invalid = false;
117 std::vector<unsigned char> signature_bytes = DecodeBase64(signature.c_str(), &invalid);
118 if (invalid) {
119 return MessageVerificationResult::ERR_MALFORMED_SIGNATURE;
120 }
121
122 CPubKey pubkey;
123 if (!pubkey.RecoverCompact(MessageHash(message), signature_bytes)) {
124 return MessageVerificationResult::ERR_PUBKEY_NOT_RECOVERED;
125 }
126
127 if (!(TxDestination(PKHash(pubkey)) == destination)) {
128 return MessageVerificationResult::ERR_NOT_SIGNED;
129 }
130
131 return MessageVerificationResult::OK;
132 */
133}
134
135/**
136 | Sign a message.
137 |
138 | -----------
139 | @param[in] privkey
140 |
141 | Private key to sign with.
142 | ----------
143 | @param[in] message
144 |
145 | The message to sign.
146 | ----------
147 | @param[out] signature
148 |
149 | Signature, base64 encoded, only set
150 | if true is returned.
151 |
152 | -----------
153 | @return
154 |
155 | true if signing was successful.
156 |
157 */
158pub fn message_sign(
159 privkey: &Key,
160 message: &String,
161 signature: &mut String) -> bool {
162
163 todo!();
164 /*
165 std::vector<unsigned char> signature_bytes;
166
167 if (!privkey.SignCompact(MessageHash(message), signature_bytes)) {
168 return false;
169 }
170
171 signature = EncodeBase64(signature_bytes);
172
173 return true;
174 */
175}
176
177/**
178 | Hashes a message for signing and verification
179 | in a manner that prevents inadvertently
180 | signing a transaction.
181 |
182 */
183pub fn message_hash(message: &String) -> u256 {
184
185 todo!();
186 /*
187 CHashWriter hasher(SER_GETHASH, 0);
188 hasher << MESSAGE_MAGIC << message;
189
190 return hasher.GetHash();
191 */
192}
193