Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

_frealm.py 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. ###############################################################################
  2. #
  3. # The MIT License (MIT)
  4. #
  5. # Copyright (c) typedef int GmbH
  6. #
  7. # Permission is hereby granted, free of charge, to any person obtaining a copy
  8. # of this software and associated documentation files (the "Software"), to deal
  9. # in the Software without restriction, including without limitation the rights
  10. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. # copies of the Software, and to permit persons to whom the Software is
  12. # furnished to do so, subject to the following conditions:
  13. #
  14. # The above copyright notice and this permission notice shall be included in
  15. # all copies or substantial portions of the Software.
  16. #
  17. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. # THE SOFTWARE.
  24. #
  25. ###############################################################################
  26. import os
  27. from binascii import b2a_hex
  28. from typing import Optional, Dict, Any, List
  29. import web3
  30. from web3.contract import Contract
  31. from ens import ENS
  32. from twisted.internet.defer import Deferred, inlineCallbacks
  33. from twisted.internet.threads import deferToThread
  34. from autobahn.wamp.interfaces import ICryptosignKey, IEthereumKey
  35. from autobahn.wamp.message import identify_realm_name_category
  36. from autobahn.xbr import make_w3, EIP712AuthorityCertificate
  37. class Seeder(object):
  38. """
  39. """
  40. __slots__ = (
  41. '_frealm',
  42. '_operator',
  43. '_label',
  44. '_country',
  45. '_legal',
  46. '_endpoint',
  47. '_bandwidth_requested',
  48. '_bandwidth_offered',
  49. )
  50. def __init__(self,
  51. frealm: 'FederatedRealm',
  52. operator: Optional[str] = None,
  53. label: Optional[str] = None,
  54. country: Optional[str] = None,
  55. legal: Optional[str] = None,
  56. endpoint: Optional[str] = None,
  57. bandwidth_requested: Optional[int] = None,
  58. bandwidth_offered: Optional[int] = None,
  59. ):
  60. """
  61. :param frealm:
  62. :param operator:
  63. :param label:
  64. :param country:
  65. :param legal:
  66. :param endpoint:
  67. :param bandwidth_requested:
  68. :param bandwidth_offered:
  69. """
  70. self._frealm: FederatedRealm = frealm
  71. self._operator: Optional[str] = operator
  72. self._label: Optional[str] = label
  73. self._country: Optional[str] = country
  74. self._legal: Optional[str] = legal
  75. self._endpoint: Optional[str] = endpoint
  76. self._bandwidth_requested: Optional[str] = bandwidth_requested
  77. self._bandwidth_offered: Optional[str] = bandwidth_offered
  78. @staticmethod
  79. def _create_eip712_connect(chain_id: int,
  80. verifying_contract: bytes,
  81. channel_binding: str,
  82. channel_id: bytes,
  83. block_no: int,
  84. challenge: bytes,
  85. pubkey: bytes,
  86. realm: bytes,
  87. delegate: bytes,
  88. seeder: bytes,
  89. bandwidth: int):
  90. channel_binding = channel_binding or ''
  91. channel_id = channel_id or b''
  92. assert chain_id
  93. assert verifying_contract
  94. assert channel_binding is not None
  95. assert channel_id is not None
  96. assert block_no
  97. assert challenge
  98. assert pubkey
  99. assert realm
  100. assert delegate
  101. assert bandwidth
  102. data = {
  103. 'types': {
  104. 'EIP712Domain': [
  105. {
  106. 'name': 'name',
  107. 'type': 'string'
  108. },
  109. {
  110. 'name': 'version',
  111. 'type': 'string'
  112. },
  113. ],
  114. 'EIP712SeederConnect': [
  115. {
  116. 'name': 'chainId',
  117. 'type': 'uint256'
  118. },
  119. {
  120. 'name': 'verifyingContract',
  121. 'type': 'address'
  122. },
  123. {
  124. 'name': 'channel_binding',
  125. 'type': 'string'
  126. },
  127. {
  128. 'name': 'channel_id',
  129. 'type': 'bytes32'
  130. },
  131. {
  132. 'name': 'block_no',
  133. 'type': 'uint256'
  134. },
  135. {
  136. 'name': 'challenge',
  137. 'type': 'bytes32'
  138. },
  139. {
  140. 'name': 'pubkey',
  141. 'type': 'bytes32'
  142. },
  143. {
  144. 'name': 'realm',
  145. 'type': 'address'
  146. },
  147. {
  148. 'name': 'delegate',
  149. 'type': 'address'
  150. },
  151. {
  152. 'name': 'seeder',
  153. 'type': 'address'
  154. },
  155. {
  156. 'name': 'bandwidth',
  157. 'type': 'uint32'
  158. },
  159. ]
  160. },
  161. 'primaryType': 'EIP712SeederConnect',
  162. 'domain': {
  163. 'name': 'XBR',
  164. 'version': '1',
  165. },
  166. 'message': {
  167. 'chainId': chain_id,
  168. 'verifyingContract': verifying_contract,
  169. 'channel_binding': channel_binding,
  170. 'channel_id': channel_id,
  171. 'block_no': block_no,
  172. 'challenge': challenge,
  173. 'pubkey': pubkey,
  174. 'realm': realm,
  175. 'delegate': delegate,
  176. 'seeder': seeder,
  177. 'bandwidth': bandwidth,
  178. }
  179. }
  180. return data
  181. @inlineCallbacks
  182. def create_authextra(self,
  183. client_key: ICryptosignKey,
  184. delegate_key: IEthereumKey,
  185. bandwidth_requested: int,
  186. channel_id: Optional[bytes] = None,
  187. channel_binding: Optional[str] = None) -> Deferred:
  188. """
  189. :param client_key:
  190. :param delegate_key:
  191. :param bandwidth_requested:
  192. :param channel_id:
  193. :param channel_binding:
  194. :return:
  195. """
  196. chain_id = 1
  197. # FIXME
  198. verifying_contract = b'\x01' * 20
  199. block_no = 1
  200. challenge = os.urandom(32)
  201. eip712_data = Seeder._create_eip712_connect(chain_id=chain_id,
  202. verifying_contract=verifying_contract,
  203. channel_binding=channel_binding,
  204. channel_id=channel_id,
  205. block_no=block_no,
  206. challenge=challenge,
  207. pubkey=client_key.public_key(binary=True),
  208. # FIXME
  209. # realm=self._frealm.address(binary=True),
  210. realm=b'\x02' * 20,
  211. delegate=delegate_key.address(binary=False),
  212. # FIXME
  213. # seeder=self._operator,
  214. seeder=b'\x03' * 20,
  215. bandwidth=bandwidth_requested)
  216. signature = yield delegate_key.sign_typed_data(eip712_data)
  217. authextra = {
  218. # string
  219. 'pubkey': client_key.public_key(binary=False),
  220. # string
  221. 'challenge': challenge,
  222. # string
  223. 'channel_binding': channel_binding,
  224. # string
  225. 'channel_id': channel_id,
  226. # address
  227. # FIXME
  228. 'realm': '7f' * 20,
  229. # int
  230. 'chain_id': chain_id,
  231. # int
  232. 'block_no': block_no,
  233. # address
  234. 'delegate': delegate_key.address(binary=False),
  235. # address
  236. 'seeder': self._operator,
  237. # int: requested bandwidth in kbps
  238. 'bandwidth': bandwidth_requested,
  239. # string: Eth signature by delegate_key over EIP712 typed data as above
  240. 'signature': b2a_hex(signature).decode(),
  241. }
  242. return authextra
  243. @property
  244. def frealm(self) -> 'FederatedRealm':
  245. """
  246. :return:
  247. """
  248. return self._frealm
  249. @property
  250. def operator(self) -> Optional[str]:
  251. """
  252. Operator address, e.g. ``"0xe59C7418403CF1D973485B36660728a5f4A8fF9c"``.
  253. :return: The Ethereum address of the endpoint operator.
  254. """
  255. return self._operator
  256. @property
  257. def label(self) -> Optional[str]:
  258. """
  259. Operator endpoint label.
  260. :return: A human readable label for the operator or specific operator endpoint.
  261. """
  262. return self._label
  263. @property
  264. def country(self) -> Optional[str]:
  265. """
  266. Operator country (ISO 3166-1 alpha-2), e.g. ``"US"``.
  267. :return: ISO 3166-1 alpha-2 country code.
  268. """
  269. return self._country
  270. @property
  271. def legal(self) -> Optional[str]:
  272. """
  273. :return:
  274. """
  275. return self._legal
  276. @property
  277. def endpoint(self) -> Optional[str]:
  278. """
  279. Public WAMP endpoint of seeder. Secure WebSocket URL resolving to a public IPv4
  280. or IPv6 listening url accepting incoming WAMP-WebSocket connections,
  281. e.g. ``wss://service1.example.com/ws``.
  282. :return: The endpoint URL.
  283. """
  284. return self._endpoint
  285. @property
  286. def bandwidth_requested(self) -> Optional[int]:
  287. """
  288. :return:
  289. """
  290. return self._bandwidth_requested
  291. @property
  292. def bandwidth_offered(self) -> Optional[int]:
  293. """
  294. :return:
  295. """
  296. return self._bandwidth_offered
  297. class FederatedRealm(object):
  298. """
  299. A federated realm is a WAMP application realm with a trust anchor rooted in Ethereum, and
  300. which can be shared between multiple parties.
  301. A federated realm is globally identified on an Ethereum chain (e.g. on Mainnet or Rinkeby)
  302. by an Ethereum address associated to a federated realm owner by an on-chain record stored
  303. in the WAMP Network contract. The federated realm address thus only needs to exist as an
  304. identifier of the federated realm-owner record.
  305. """
  306. __slots__ = (
  307. '_name_or_address',
  308. '_gateway_config',
  309. '_status',
  310. '_name_category',
  311. '_w3',
  312. '_ens',
  313. '_address',
  314. '_contract',
  315. '_seeders',
  316. '_root_ca',
  317. '_catalog',
  318. '_meta',
  319. )
  320. # FIXME
  321. CONTRACT_ADDRESS = web3.Web3.toChecksumAddress('0xF7acf1C4CB4a9550B8969576573C2688B48988C2')
  322. CONTRACT_ABI: str = ''
  323. def __init__(self, name_or_address: str, gateway_config: Optional[Dict[str, Any]] = None):
  324. """
  325. Instantiate a federated realm from a federated realm ENS name (which is resolved to an Ethereum
  326. address) or Ethereum address.
  327. :param name_or_address: Ethereum ENS name or address.
  328. :param gateway_config: If provided, use this Ethereum gateway. If not provided,
  329. connect via Infura to Ethereum Mainnet, which requires an environment variable
  330. ``WEB3_INFURA_PROJECT_ID`` with your Infura project ID.
  331. """
  332. self._name_or_address = name_or_address
  333. self._gateway_config = gateway_config
  334. # status, will change to 'RUNNING' after initialize() has completed
  335. self._status = 'STOPPED'
  336. self._name_category: Optional[str] = identify_realm_name_category(self._name_or_address)
  337. if self._name_category not in ['eth', 'ens', 'reverse_ens']:
  338. raise ValueError('name_or_address "{}" not an Ethereum address or ENS name'.format(self._name_or_address))
  339. # will be filled once initialize()'ed
  340. self._w3 = None
  341. self._ens = None
  342. # address identifying the federated realm
  343. self._address: Optional[str] = None
  344. # will be initialized with a FederatedRealm contract instance
  345. self._contract: Optional[Contract] = None
  346. # cache of federated realm seeders, filled once in status running
  347. self._seeders: List[Seeder] = []
  348. self._root_ca = None
  349. @property
  350. def status(self) -> str:
  351. return self._status
  352. @property
  353. def name_or_address(self) -> str:
  354. return self._name_or_address
  355. @property
  356. def gateway_config(self) -> Optional[Dict[str, Any]]:
  357. return self._gateway_config
  358. @property
  359. def name_category(self) -> Optional[str]:
  360. return self._name_category
  361. @property
  362. def address(self):
  363. return self._address
  364. def root_ca(self) -> EIP712AuthorityCertificate:
  365. assert self._status == 'RUNNING'
  366. return self._root_ca
  367. @property
  368. def seeders(self) -> List[Seeder]:
  369. return self._seeders
  370. def initialize(self):
  371. """
  372. :return:
  373. """
  374. if self._status != 'STOPPED':
  375. raise RuntimeError('cannot start in status "{}"'.format(self._status))
  376. d = deferToThread(self._initialize_background)
  377. return d
  378. def _initialize_background(self):
  379. self._status = 'STARTING'
  380. if self._gateway_config:
  381. self._w3 = make_w3(self._gateway_config)
  382. else:
  383. raise RuntimeError('cannot auto-configure ethereum connection (was removed from web3.py in v6)')
  384. # https://github.com/ethereum/web3.py/issues/1416
  385. # https://github.com/ethereum/web3.py/pull/2706
  386. # from web3.auto.infura import w3
  387. # self._w3 = w3
  388. self._ens = ENS.from_web3(self._w3)
  389. if self._name_category in ['ens', 'reverse_ens']:
  390. if self._name_category == 'reverse_ens':
  391. name = ''.join(reversed(self._name_or_address.split('.')))
  392. else:
  393. name = self._name_or_address
  394. self._address = self._ens.address(name)
  395. elif self._name_category == 'eth':
  396. self._address = self._w3.toChecksumAddress(self._name_or_address)
  397. else:
  398. assert False, 'should not arrive here'
  399. # https://web3py.readthedocs.io/en/stable/contracts.html#web3.contract.Contract
  400. # https://web3py.readthedocs.io/en/stable/web3.eth.html#web3.eth.Eth.contract
  401. # self._contract = self._w3.eth.contract(address=self.CONTRACT_ADDRESS, abi=self.CONTRACT_ABI)
  402. # FIXME: get IPFS hash, download file, unzip seeders
  403. self._status = 'RUNNING'