Skip to content

Instantly share code, notes, and snippets.

@svanschalkwyk
Created May 11, 2018 22:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save svanschalkwyk/06896f33044e4f3e254e4ec526658198 to your computer and use it in GitHub Desktop.
Save svanschalkwyk/06896f33044e4f3e254e4ec526658198 to your computer and use it in GitHub Desktop.
from sawtooth_signing import create_context
from sawtooth_signing import CryptoFactory
from hashlib import sha512
from sawtooth_sdk.protobuf.transaction_pb2 import TransactionHeader
import cbor
from sawtooth_sdk.protobuf.transaction_pb2 import Transaction
from sawtooth_sdk.protobuf.transaction_pb2 import TransactionList
from sawtooth_sdk.protobuf.batch_pb2 import BatchHeader
from sawtooth_sdk.protobuf.batch_pb2 import Batch
from sawtooth_sdk.protobuf.batch_pb2 import BatchList
import urllib.request
from urllib.error import HTTPError
context = create_context('secp256k1')
private_key = context.new_random_private_key()
signer = CryptoFactory(context).new_signer(private_key)
payload = {
'Verb': 'set',
'Name': 'foo',
'Value': 42}
payload_bytes = cbor.dumps(payload)
txn_header_bytes = TransactionHeader(
family_name='intkey',
family_version='1.0',
inputs=['1cf1266e282c41be5e4254d8820772c5518a2c5a8c0c7f7eda19594a7eb539453e1ed8'],
outputs=['1cf1266e282c41be5e4254d8820772c5518a2c5a8c0c7f7eda19594a7eb539453e1ed8'],
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(),
# In this example, there are no dependencies. This list should include
# an previous transaction header signatures that must be applied for
# this transaction to successfully commit.
# For example,
# dependencies=['540a6803971d1880ec73a96cb97815a95d374cbad5d865925e5aa0432fcf1931539afe10310c122c5eaae15df61236079abbf4f258889359c4d175516934484a'],
dependencies=[],
payload_sha512=sha512(payload_bytes).hexdigest()
).SerializeToString()
signature = signer.sign(txn_header_bytes)
txn = Transaction(
header=txn_header_bytes,
header_signature=signature,
payload=payload_bytes
)
txn_list_bytes = TransactionList(
transactions=[txn]
).SerializeToString()
txn_bytes = txn.SerializeToString()
txns = [txn]
batch_header_bytes = BatchHeader(
signer_public_key=signer.get_public_key().as_hex(),
# transaction_ids=[txn.header_signature for txn in txns],
transaction_ids=[txn.header_signature]
).SerializeToString()
signature = signer.sign(batch_header_bytes)
batch = Batch(
header=batch_header_bytes,
header_signature=signature,
transactions=txns
)
batch_list_bytes = BatchList(batches=[batch]).SerializeToString()
try:
request = urllib.request.Request(
'http://192.168.5.113:8024/batches',
batch_list_bytes,
method='POST',
headers={'Content-Type': 'application/octet-stream'})
response = urllib.request.urlopen(request)
html=response.read()
print(html)
except HTTPError as e:
response = e.file
print(response)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment