Guest User

Untitled

a guest
May 22nd, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.21 KB | None | 0 0
  1. class SawtoothService:
  2. def __init__(self, rest_api_url):
  3. context = create_context('secp256k1')
  4. private_key = context.new_random_private_key()
  5. signer = CryptoFactory(context).new_signer(private_key)
  6.  
  7. self._signer = signer
  8. self._rest_api_url = rest_api_url
  9.  
  10. def SingleTransaction(self, family, payload):
  11. txn = self.BuildTransaction(
  12. family,
  13. '1.0',
  14. payload,
  15. self.BuildAddress(family, payload['Session'])
  16. )
  17.  
  18. batchlist = self.BuildBatchList([txn])
  19.  
  20. responseContent = WebRequest.Post(
  21. self._rest_api_url + '/batches',
  22. batchlist.SerializeToString(),
  23. 'application/octet-stream'
  24. )
  25. return responseContent
  26.  
  27. def BuildTransaction(self, family_name, family_version, payload, address):
  28.  
  29. payload_bytes = cbor.dumps(payload)
  30.  
  31. txn_header_bytes = TransactionHeader(
  32. family_name=family_name,
  33. family_version=family_version,
  34. inputs=[address],
  35. outputs=[address],
  36. signer_public_key=self._signer.get_public_key().as_hex(),
  37. batcher_public_key=self._signer.get_public_key().as_hex(),
  38. dependencies=[],
  39. payload_sha512=sha512(payload_bytes).hexdigest()
  40. ).SerializeToString()
  41.  
  42. signature = self._signer.sign(txn_header_bytes)
  43.  
  44. return Transaction(
  45. header=txn_header_bytes,
  46. header_signature=signature,
  47. payload=payload_bytes,
  48. )
  49.  
  50. def BuildBatchList(self, txns):
  51. batch_header_bytes = BatchHeader(
  52. signer_public_key=self._signer.get_public_key().as_hex(),
  53. transaction_ids=[txn.header_signature for txn in txns],
  54. ).SerializeToString()
  55.  
  56. signature = self._signer.sign(batch_header_bytes)
  57.  
  58. batch = Batch(
  59. header=batch_header_bytes,
  60. header_signature=signature,
  61. transactions=txns,
  62. trace=True
  63. )
  64.  
  65. return BatchList(batches=[batch])
  66.  
  67. def BuildAddress(self, prefix, name):
  68. return sha512(prefix.encode('utf-8')).hexdigest()[0:6] + sha512(name.encode('utf-8')).hexdigest()[-64:]
Add Comment
Please, Sign In to add comment