FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
python-bitshares/bitshares/message.py at develop_sschiessl · blockchainprojects/python-bitshares · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
blockchainprojects
/
python-bitshares
Public
forked from
bitshares/python-bitshares
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
python-bitshares
/
bitshares
/
message.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
140 lines (113 loc) · 4.18 KB
Breadcrumbs
python-bitshares
/
bitshares
/
message.py
Copy path
File metadata and controls
140 lines (113 loc) · 4.18 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
import
re
import
logging
from
binascii
import
hexlify
,
unhexlify
from
graphenebase
.
ecdsa
import
verify_message
,
sign_message
from
bitsharesbase
.
account
import
PublicKey
from
.
instance
import
BlockchainInstance
from
.
account
import
Account
from
.
exceptions
import
InvalidMessageSignature
from
.
storage
import
configStorage
as
config
log
=
logging
.
getLogger
(
__name__
)
MESSAGE_SPLIT
=
(
"-----BEGIN BITSHARES SIGNED MESSAGE-----"
,
"-----BEGIN META-----"
,
"-----BEGIN SIGNATURE-----"
,
"-----END BITSHARES SIGNED MESSAGE-----"
)
SIGNED_MESSAGE_META
=
"""{message}
account={meta[account]}
memokey={meta[memokey]}
block={meta[block]}
timestamp={meta[timestamp]}"""
SIGNED_MESSAGE_ENCAPSULATED
=
"""
{MESSAGE_SPLIT[0]}
{message}
{MESSAGE_SPLIT[1]}
account={meta[account]}
memokey={meta[memokey]}
block={meta[block]}
timestamp={meta[timestamp]}
{MESSAGE_SPLIT[2]}
{signature}
{MESSAGE_SPLIT[3]}
"""
class
Message
():
def
__init__
(
self
,
message
,
**
kwargs
):
BlockchainInstance
.
__init__
(
self
,
**
kwargs
)
self
.
message
=
message
def
sign
(
self
,
account
=
None
,
**
kwargs
):
""" Sign a message with an account's memo key
:param str account: (optional) the account that owns the bet
(defaults to ``default_account``)
:returns: the signed message encapsulated in a known format
"""
if
not
account
:
if
"default_account"
in
config
:
account
=
config
[
"default_account"
]
if
not
account
:
raise
ValueError
(
"You need to provide an account"
)
# Data for message
account
=
Account
(
account
,
blockchain_instance
=
self
.
blockchain
)
info
=
self
.
blockchain
.
info
()
meta
=
dict
(
timestamp
=
info
[
"time"
],
block
=
info
[
"head_block_number"
],
memokey
=
account
[
"options"
][
"memo_key"
],
account
=
account
[
"name"
])
# wif key
wif
=
self
.
blockchain
.
wallet
.
getPrivateKeyForPublicKey
(
account
[
"options"
][
"memo_key"
]
)
# signature
message
=
self
.
message
.
strip
()
signature
=
hexlify
(
sign_message
(
SIGNED_MESSAGE_META
.
format
(
**
locals
()),
wif
)).
decode
(
"ascii"
)
message
=
self
.
message
return
SIGNED_MESSAGE_ENCAPSULATED
.
format
(
MESSAGE_SPLIT
=
MESSAGE_SPLIT
,
**
locals
()
)
def
verify
(
self
,
**
kwargs
):
""" Verify a message with an account's memo key
:param str account: (optional) the account that owns the bet
(defaults to ``default_account``)
:returns: True if the message is verified successfully
:raises InvalidMessageSignature if the signature is not ok
"""
# Split message into its parts
parts
=
re
.
split
(
"|"
.
join
(
MESSAGE_SPLIT
),
self
.
message
)
parts
=
[
x
for
x
in
parts
if
x
.
strip
()]
assert
len
(
parts
)
>
2
,
"Incorrect number of message parts"
message
=
parts
[
0
].
strip
()
signature
=
parts
[
2
].
strip
()
# Parse the meta data
meta
=
dict
(
re
.
findall
(
r'(\S+)=(.*)'
,
parts
[
1
]))
# Ensure we have all the data in meta
assert
"account"
in
meta
assert
"memokey"
in
meta
assert
"block"
in
meta
assert
"timestamp"
in
meta
# Load account from blockchain
account
=
Account
(
meta
.
get
(
"account"
),
blockchain_instance
=
self
.
blockchain
)
# Test if memo key is the same as on the blockchain
if
not
account
[
"options"
][
"memo_key"
]
==
meta
[
"memokey"
]:
log
.
error
(
"Memo Key of account {} on the Blockchain"
.
format
(
account
[
"name"
])
+
"differs from memo key in the message: {} != {}"
.
format
(
account
[
"options"
][
"memo_key"
],
meta
[
"memokey"
]
)
)
# Reformat message
message
=
SIGNED_MESSAGE_META
.
format
(
**
locals
())
# Verify Signature
pubkey
=
verify_message
(
message
,
unhexlify
(
signature
))
# Verify pubky
pk
=
PublicKey
(
hexlify
(
pubkey
).
decode
(
"ascii"
))
if
format
(
pk
,
self
.
blockchain
.
prefix
)
!=
meta
[
"memokey"
]:
raise
InvalidMessageSignature
return
True
Back
|
FazBrowse Home
|
New Git URL