from sawtooth_sdk.protobuf.transaction_pb2 import Transaction signature = signer.sign(txn_header_bytes) txn = Transaction(header=txn_header_bytes, header_signature=signature, payload=payload_bytes) # Encoding transaction from sawtooth_sdk.protobuf.transaction_pb2 import TransactionList txn_list_bytes = TransactionList(transactions=[txn]).SerializeToString() txn_bytes = txn.SerializeToString() # Create a batch header from sawtooth_sdk.protobuf.batch_pb2 import BatchHeader txns = [txn] batch_header_bytes = BatchHeader( signer_public_key=signer.get_public_key().as_hex(), transaction_ids=[txn.header_signature for txn in txns], ).SerializeToString() #creating the batch from sawtooth_sdk.protobuf.batch_pb2 import Batch
def create(self, request): print(request.data) # encoding payload payload = { "action":"create", "residuo":request.data } payload_bytes = cbor.dumps(payload) ## BUILD TRANSACTION # Build transaction header txn_header_bytes = TransactionHeader( family_name=FAMILY, family_version=VERSION, inputs=[PREFIX], outputs=[PREFIX], signer_public_key=signer.get_public_key().as_hex(), # In this example, we're signing the batch with the same private key, # but the batch can be signed by another party, in which case, the # public key will need to be associated with that key. batcher_public_key=signer.get_public_key().as_hex(), dependencies=[], payload_sha512=sha512(payload_bytes).hexdigest() ).SerializeToString() # Create transaction signature = signer.sign(txn_header_bytes) txn = Transaction( header=txn_header_bytes, header_signature=signature, payload= payload_bytes ) # Encode the Transaction txn_list_bytes = TransactionList( transactions=[txn] ).SerializeToString() txn_bytes = txn.SerializeToString() ## BUILD BATCH # create batch header txns = [txn] batch_header_bytes = BatchHeader( signer_public_key=signer.get_public_key().as_hex(), transaction_ids=[txn.header_signature for txn in txns], ).SerializeToString() # create Batch signature = signer.sign(batch_header_bytes) batch = Batch( header=batch_header_bytes, header_signature=signature, transactions=txns ) # BATCHLIST batch_list_bytes = BatchList(batches=[batch]).SerializeToString() response = None # ENVIAR A SERVIDOR try: request = urllib.request.Request( API_URL+'/batches', ## URL SERVIDOR batch_list_bytes, method='POST', headers={'Content-Type': 'application/octet-stream'}) response = urllib.request.urlopen(request) except HTTPError as e: response = e.file response = json.loads(response.read().decode('utf-8')) link = response["link"].split('=')[1] requests.get(API_URL+'/batch_statuses',params={'id':link, 'wait':1}).json() return self.retrieve(None,payload['residuo']['code'])