1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
=
# watch for updates to signature format
"""
A message compatible with EIP-191_ that is ready to be signed.
The properties are components of an EIP-191_ signable message. Other message formats
can be encoded into this format for easy signing. This data structure doesn't need to
know about the original message format. For example, you can think of
EIP-712 as compiling down to an EIP-191 message.
In typical usage, you should never need to create these by hand. Instead, use
one of the available encode_* methods in this module, like:
- :meth:`encode_structured_data`
- :meth:`encode_intended_validator`
- :meth:`encode_defunct`
.. _EIP-191: https://eips.ethereum.org/EIPS/eip-191
"""
: # must be length 1
: # aka "version specific data"
: # aka "data to sign"
=
= b + + +
return
# watch for updates to signature format
"""
Encode a message using the "intended validator" approach (ie~ version 0) defined in EIP-191_.
Supply the message as exactly one of these three arguments:
bytes as a primitive, a hex string, or a unicode string.
.. WARNING:: Note that this code has not gone through an external audit.
Also, watch for updates to the format, as the EIP is still in DRAFT.
:param validator_address: which on-chain contract is capable of validating this message,
provided as a checksummed address or in native bytes.
:param primitive: the binary message to be signed
:type primitive: bytes or int
:param str hexstr: the message encoded as hex
:param str text: the message as a series of unicode characters (a normal Py3 str)
:returns: The EIP-191 encoded message, ready for signing
.. _EIP-191: https://eips.ethereum.org/EIPS/eip-191
"""
# The validator_address is a str or Address (which is a subtype of bytes). Both of
# these are AnyStr, which includes str and bytes. Not sure why mypy complains here...
=
=
return
"""
Encode an EIP-712_ message.
EIP-712 is the "structured data" approach (ie~ version 1 of an EIP-191 message).
Supply the message as exactly one of the three arguments:
- primitive, as a dict that defines the structured data
- primitive, as bytes
- text, as a json-encoded string
- hexstr, as a hex-encoded (json-encoded) string
.. WARNING:: Note that this code has not gone through an external audit, and
the test cases are incomplete.
Also, watch for updates to the format, as the EIP is still in DRAFT.
:param primitive: the binary message to be signed
:type primitive: bytes or int or Mapping (eg~ dict )
:param hexstr: the message encoded as hex
:param text: the message as a series of unicode characters (a normal Py3 str)
:returns: The EIP-191 encoded message, ready for signing
.. _EIP-712: https://eips.ethereum.org/EIPS/eip-712
"""
=
=
=
return
r"""
Encode a message for signing, using an old, unrecommended approach.
Only use this method if you must have compatibility with
:meth:`w3.eth.sign() <web3.eth.Eth.sign>`.
EIP-191 defines this as "version ``E``".
.. NOTE: This standard includes the number of bytes in the message as a part of the header.
Awkwardly, the number of bytes in the message is encoded in decimal ascii.
So if the message is 'abcde', then the length is encoded as the ascii
character '5'. This is one of the reasons that this message format is not preferred.
There is ambiguity when the message '00' is encoded, for example.
Supply exactly one of the three arguments: bytes, a hex string, or a unicode string.
:param primitive: the binary message to be signed
:type primitive: bytes or int
:param str hexstr: the message encoded as hex
:param str text: the message as a series of unicode characters (a normal Py3 str)
:returns: The EIP-191 encoded message, ready for signing
.. doctest:: python
>>> from eth_account.messages import encode_defunct
>>> from eth_utils.curried import to_hex, to_bytes
>>> message_text = "I♥SF"
>>> encode_defunct(text=message_text)
SignableMessage(version=b'E', header=b'thereum Signed Message:\n6', body=b'I\xe2\x99\xa5SF')
These four also produce the same hash:
>>> encode_defunct(to_bytes(text=message_text))
SignableMessage(version=b'E', header=b'thereum Signed Message:\n6', body=b'I\xe2\x99\xa5SF')
>>> encode_defunct(bytes(message_text, encoding='utf-8'))
SignableMessage(version=b'E', header=b'thereum Signed Message:\n6', body=b'I\xe2\x99\xa5SF')
>>> to_hex(text=message_text)
'0x49e299a55346'
>>> encode_defunct(hexstr='0x49e299a55346')
SignableMessage(version=b'E', header=b'thereum Signed Message:\n6', body=b'I\xe2\x99\xa5SF')
>>> encode_defunct(0x49e299a55346)
SignableMessage(version=b'E', header=b'thereum Signed Message:\n6', body=b'I\xe2\x99\xa5SF')
"""
=
=
# Encoding version E defined by EIP-191
return
"""
Convert the provided message into a message hash, to be signed.
.. CAUTION:: Intented for use with the deprecated :meth:`eth_account.account.Account.signHash`.
This is for backwards compatibility only. All new implementations
should use :meth:`encode_defunct` instead.
:param primitive: the binary message to be signed
:type primitive: bytes or int
:param str hexstr: the message encoded as hex
:param str text: the message as a series of unicode characters (a normal Py3 str)
:returns: The hash of the message, after adding the prefix
"""
=
=
return