Created
May 17, 2018 15:03
-
-
Save danielcooperxyz/9ada1aac3700c76f4710eaa1084123ca to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SawtoothService: | |
def __init__(self, rest_api_url): | |
context = create_context('secp256k1') | |
private_key = context.new_random_private_key() | |
signer = CryptoFactory(context).new_signer(private_key) | |
self._signer = signer | |
self._rest_api_url = rest_api_url | |
def SingleTransaction(self, family, payload): | |
txn = self.BuildTransaction( | |
family, | |
'1.0', | |
payload, | |
self.BuildAddress(family, payload['Session']) | |
) | |
batchlist = self.BuildBatchList([txn]) | |
responseContent = WebRequest.Post( | |
self._rest_api_url + '/batches', | |
batchlist.SerializeToString(), | |
'application/octet-stream' | |
) | |
return responseContent | |
def BuildTransaction(self, family_name, family_version, payload, address): | |
payload_bytes = cbor.dumps(payload) | |
txn_header_bytes = TransactionHeader( | |
family_name=family_name, | |
family_version=family_version, | |
inputs=[address], | |
outputs=[address], | |
signer_public_key=self._signer.get_public_key().as_hex(), | |
batcher_public_key=self._signer.get_public_key().as_hex(), | |
dependencies=[], | |
payload_sha512=sha512(payload_bytes).hexdigest() | |
).SerializeToString() | |
signature = self._signer.sign(txn_header_bytes) | |
return Transaction( | |
header=txn_header_bytes, | |
header_signature=signature, | |
payload=payload_bytes, | |
) | |
def BuildBatchList(self, txns): | |
batch_header_bytes = BatchHeader( | |
signer_public_key=self._signer.get_public_key().as_hex(), | |
transaction_ids=[txn.header_signature for txn in txns], | |
).SerializeToString() | |
signature = self._signer.sign(batch_header_bytes) | |
batch = Batch( | |
header=batch_header_bytes, | |
header_signature=signature, | |
transactions=txns, | |
trace=True | |
) | |
return BatchList(batches=[batch]) | |
def BuildAddress(self, prefix, name): | |
return sha512(prefix.encode('utf-8')).hexdigest()[0:6] + sha512(name.encode('utf-8')).hexdigest()[-64:] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment