-
Notifications
You must be signed in to change notification settings - Fork 763
/
__init__.py
320 lines (265 loc) · 10.2 KB
/
__init__.py
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
###############################################################################
#
# The MIT License (MIT)
#
# Copyright (c) Crossbar.io Technologies GmbH
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
###############################################################################
try:
from mnemonic import Mnemonic
from autobahn.xbr._mnemonic import mnemonic_to_private_key
# monkey patch, see:
# https://github.com/ethereum/web3.py/issues/1201
# https://github.com/ethereum/eth-abi/pull/88
from eth_abi import abi
from autobahn.xbr._abi import XBR_TOKEN_ABI, XBR_NETWORK_ABI, XBR_MARKET_ABI, XBR_CATALOG_ABI, XBR_CHANNEL_ABI # noqa
from autobahn.xbr._abi import XBR_DEBUG_TOKEN_ADDR, XBR_DEBUG_NETWORK_ADDR, XBR_DEBUG_MARKET_ADDR, XBR_DEBUG_CATALOG_ADDR, XBR_DEBUG_CHANNEL_ADDR # noqa
from autobahn.xbr._abi import XBR_DEBUG_TOKEN_ADDR_SRC, XBR_DEBUG_NETWORK_ADDR_SRC, XBR_DEBUG_MARKET_ADDR_SRC, XBR_DEBUG_CATALOG_ADDR_SRC, XBR_DEBUG_CHANNEL_ADDR_SRC # noqa
from autobahn.xbr._interfaces import IMarketMaker, IProvider, IConsumer, ISeller, IBuyer # noqa
from autobahn.xbr._util import make_w3, pack_uint256, unpack_uint256 # noqa
from autobahn.xbr._eip712_member_register import sign_eip712_member_register, recover_eip712_member_register # noqa
from autobahn.xbr._eip712_member_login import sign_eip712_member_login, recover_eip712_member_login # noqa
from autobahn.xbr._eip712_market_create import sign_eip712_market_create, recover_eip712_market_create # noqa
from autobahn.xbr._eip712_market_join import sign_eip712_market_join, recover_eip712_market_join # noqa
from autobahn.xbr._eip712_catalog_create import sign_eip712_catalog_create, recover_eip712_catalog_create # noqa
from autobahn.xbr._eip712_api_publish import sign_eip712_api_publish, recover_eip712_api_publish # noqa
from autobahn.xbr._eip712_consent import sign_eip712_consent, recover_eip712_consent # noqa
from autobahn.xbr._eip712_channel_open import sign_eip712_channel_open, recover_eip712_channel_open # noqa
from autobahn.xbr._eip712_channel_close import sign_eip712_channel_close, recover_eip712_channel_close # noqa
from autobahn.xbr._blockchain import SimpleBlockchain # noqa
from autobahn.xbr._seller import SimpleSeller, KeySeries # noqa
from autobahn.xbr._buyer import SimpleBuyer # noqa
HAS_XBR = True
if not hasattr(abi, 'collapse_type'):
def collapse_type(base, sub, arrlist):
return base + sub + ''.join(map(repr, arrlist))
abi.collapse_type = collapse_type
if not hasattr(abi, 'process_type'):
from eth_abi.grammar import (
TupleType,
normalize,
parse,
)
def process_type(type_str):
normalized_type_str = normalize(type_str)
abi_type = parse(normalized_type_str)
type_str_repr = repr(type_str)
if type_str != normalized_type_str:
type_str_repr = '{} (normalized to {})'.format(
type_str_repr,
repr(normalized_type_str),
)
if isinstance(abi_type, TupleType):
raise ValueError("Cannot process type {}: tuple types not supported".format(type_str_repr, ))
abi_type.validate()
sub = abi_type.sub
if isinstance(sub, tuple):
sub = 'x'.join(map(str, sub))
elif isinstance(sub, int):
sub = str(sub)
else:
sub = ''
arrlist = abi_type.arrlist
if isinstance(arrlist, tuple):
arrlist = list(map(list, arrlist))
else:
arrlist = []
return abi_type.base, sub, arrlist
abi.process_type = process_type
xbrtoken = None
"""
Contract instance of XBRToken.
"""
xbrnetwork = None
"""
Contract instance of XBRNetwork.
"""
xbrmarket = None
"""
Contract instance of XBRMarket.
"""
xbrcatalog = None
"""
Contract instance of XBRMarket.
"""
xbrchannel = None
"""
Contract instance of XBRMarket.
"""
def setProvider(_w3):
"""
The XBR library must be initialized (once) first by setting the Web3 provider
using this function.
"""
global xbrtoken
global xbrnetwork
global xbrmarket
global xbrcatalog
global xbrchannel
# print('Provider set - xbrtoken={}'.format(XBR_DEBUG_TOKEN_ADDR))
xbrtoken = _w3.eth.contract(address=XBR_DEBUG_TOKEN_ADDR, abi=XBR_TOKEN_ABI)
# print('Provider set - xbrnetwork={}'.format(XBR_DEBUG_NETWORK_ADDR))
xbrnetwork = _w3.eth.contract(address=XBR_DEBUG_NETWORK_ADDR, abi=XBR_NETWORK_ABI)
# print('Provider set - xbrmarket={}'.format(XBR_DEBUG_MARKET_ADDR))
xbrmarket = _w3.eth.contract(address=XBR_DEBUG_MARKET_ADDR, abi=XBR_MARKET_ABI)
# print('Provider set - xbrcatalog={}'.format(XBR_DEBUG_CATALOG_ADDR))
xbrcatalog = _w3.eth.contract(address=XBR_DEBUG_CATALOG_ADDR, abi=XBR_CATALOG_ABI)
# print('Provider set - xbrchannel={}'.format(XBR_DEBUG_CHANNEL_ADDR))
xbrchannel = _w3.eth.contract(address=XBR_DEBUG_CHANNEL_ADDR, abi=XBR_CHANNEL_ABI)
class MemberLevel(object):
"""
XBR Network member levels.
"""
NONE = 0
ACTIVE = 1
VERIFIED = 2
RETIRED = 3
PENALTY = 4
BLOCKED = 5
class NodeType(object):
"""
XBR Cloud node types.
"""
NONE = 0
MASTER = 1
CORE = 2
EDGE = 3
class ChannelType(object):
"""
Type of a XBR off-chain channel: paying channel (for provider delegates selling data services) or payment channel (for consumer delegates buying data services).
"""
NONE = 0
"""
Unset
"""
PAYMENT = 1
"""
Payment channel: from buyer/consumer delegate to maker maker.
"""
PAYING = 2
"""
Paying channel: from market maker to seller/provider delegate.
"""
class ActorType(object):
"""
XBR Market Actor type.
"""
NONE = 0
"""
Unset
"""
PROVIDER = 1
"""
Actor is a XBR Provider.
"""
CONSUMER = 2
"""
Actor is a XBR Consumer.
"""
PROVIDER_CONSUMER = 3
"""
Actor is both a XBR Provider and XBR Consumer.
"""
def generate_seedphrase(strength=128, language='english'):
"""
Generate a new BIP-39 mnemonic seed phrase for use in Ethereum (Metamask, etc).
:param strength: Strength of seed phrase in bits, one of the following ``[128, 160, 192, 224, 256]``,
generating seed phrase of 12 - 24 words inlength.
:return: Newly generated seed phrase (in english).
:rtype: string
"""
return Mnemonic(language).generate(strength)
def check_seedphrase(seedphrase, language='english'):
return Mnemonic(language).check(seedphrase)
def account_from_seedphrase(seephrase, index=0):
from web3.auto import w3
derivation_path = "m/44'/60'/0'/0/{}".format(index)
key = mnemonic_to_private_key(seephrase, str_derivation_path=derivation_path)
account = w3.eth.account.privateKeyToAccount(key)
return account
ASCII_BOMB = r"""
_ ._ _ , _ ._
(_ ' ( ` )_ .__)
( ( ( ) `) ) _)
(__ (_ (_ . _) _) ,__)
`~~`\ ' . /`~~`
; ;
/ \
_____________/_ __ \_____________
"""
__all__ = (
'HAS_XBR',
'XBR_TOKEN_ABI',
'XBR_NETWORK_ABI',
'XBR_MARKET_ABI',
'XBR_CATALOG_ABI',
'XBR_CHANNEL_ABI',
'xbrtoken',
'xbrnetwork',
'xbrmarket',
'xbrcatalog',
'xbrchannel',
'setProvider',
'make_w3',
'pack_uint256',
'unpack_uint256',
'generate_seedphrase',
'check_seedphrase',
'account_from_seedphrase',
'ASCII_BOMB',
'sign_eip712_member_register',
'recover_eip712_member_register',
'sign_eip712_member_login',
'recover_eip712_member_login',
'sign_eip712_market_create',
'recover_eip712_market_create',
'sign_eip712_market_join',
'recover_eip712_market_join',
'sign_eip712_catalog_create',
'recover_eip712_catalog_create',
'sign_eip712_api_publish',
'recover_eip712_api_publish',
'sign_eip712_consent',
'recover_eip712_consent',
'sign_eip712_channel_open',
'recover_eip712_channel_open',
'sign_eip712_channel_close',
'recover_eip712_channel_close',
'MemberLevel',
'ActorType',
'ChannelType',
'NodeType',
'KeySeries',
'SimpleBlockchain',
'SimpleSeller',
'SimpleBuyer',
'IMarketMaker',
'IProvider',
'IConsumer',
'ISeller',
'IBuyer',
)
except (ImportError, FileNotFoundError) as e:
import sys
sys.stderr.write(str(e))
sys.stderr.flush()
HAS_XBR = False
__all__ = ('HAS_XBR',)