class Client(object): def __init__(self, rest_endpoint): self.priv_key = signer.generate_privkey() self.pub_key = signer.generate_pubkey(self.priv_key) self._namespace = hashlib.sha512('intkey'.encode()).hexdigest()[:6] self._factory = MessageFactory('intkey', '1.0', self._namespace) self._rest = RestClient(rest_endpoint) def send(self): name = uuid4().hex[:20] txns = [ self._factory.create_transaction( cbor.dumps({ 'Name': name, 'Verb': 'set', 'Value': 1000 }), inputs=[ self._namespace + self._factory.sha512(name.encode())[-64:] ], outputs=[ self._namespace + self._factory.sha512(name.encode())[-64:] ], deps=[]) ] self._rest.send_batches(self._factory.create_batch(txns)) def block_list(self): return [(item['header']['block_num'], item['header_signature'], item['header']['signer_pubkey']) for item in self._rest.block_list()['data']]
class Client(object): def __init__(self, rest_endpoint): context = create_context('secp256k1') private_key = context.new_random_private_key() self.priv_key = private_key.as_hex() self.pub_key = context.get_public_key(private_key).as_hex() self.signer = CryptoFactory(context).new_signer(private_key) self._namespace = hashlib.sha512('intkey'.encode()).hexdigest()[:6] self._factory = MessageFactory( 'intkey', '1.0', self._namespace, signer=self.signer) self._rest = RestClient(rest_endpoint) def send(self): name = uuid4().hex[:20] txns = [self._factory.create_transaction( cbor.dumps({'Name': name, 'Verb': 'set', 'Value': 1000}), inputs=[self._namespace + self._factory.sha512(name.encode())[-64:]], outputs=[self._namespace + self._factory.sha512(name.encode())[-64:]], deps=[])] self._rest.send_batches(self._factory.create_batch(txns)) def block_list(self): return [(item['header']['block_num'], item['header_signature'], item['header']['signer_public_key']) for item in self._rest.block_list()['data']]
def __init__(self, rest_endpoint): self.priv_key = signer.generate_private_key() self.pub_key = signer.generate_public_key(self.priv_key) self._namespace = hashlib.sha512('intkey'.encode()).hexdigest()[:6] self._factory = MessageFactory( 'intkey', '1.0', self._namespace) self._rest = RestClient(rest_endpoint)
def __init__(self, rest_endpoint): context = create_context('secp256k1') private_key = context.new_random_private_key() self.priv_key = private_key.as_hex() self.pub_key = context.get_public_key(private_key).as_hex() self.signer = CryptoFactory(context).new_signer(private_key) self._namespace = hashlib.sha512('intkey'.encode()).hexdigest()[:6] self._factory = MessageFactory('intkey', '1.0', self._namespace, signer=self.signer) self._rest = RestClient(rest_endpoint)
class Client: def __init__(self, rest_endpoint): context = create_context('secp256k1') private_key = context.new_random_private_key() self.priv_key = private_key.as_hex() self.pub_key = context.get_public_key(private_key).as_hex() self.signer = CryptoFactory(context).new_signer(private_key) self._namespace = hashlib.sha512('intkey'.encode()).hexdigest()[:6] self._factory = MessageFactory( 'intkey', '1.0', self._namespace, signer=self.signer) self._rest = RestClient(rest_endpoint) def send(self): name = uuid4().hex[:20] txns = [ self._factory.create_transaction( cbor.dumps({ 'Name': name, 'Verb': 'set', 'Value': 1000 }), inputs=[ self._namespace + self._factory.sha512(name.encode())[-64:] ], outputs=[ self._namespace + self._factory.sha512(name.encode())[-64:] ], deps=[]) ] self._rest.send_batches(self._factory.create_batch(txns)) def block_list(self): return [(item['header']['block_num'], item['header_signature'], item['header']['signer_public_key']) for item in self._rest.block_list()['data']]
def __init__(self, name, rest_endpoint): """ Args: name (str): An identifier for this Transactor rest_endpoint (str): The rest api that this Transactor will communicate with. """ self.name = name self._rest_endpoint = rest_endpoint \ if rest_endpoint.startswith("http://") \ else "http://{}".format(rest_endpoint) with open('/root/.sawtooth/keys/{}.priv'.format(name)) as priv_file: private_key = Secp256k1PrivateKey.from_hex( priv_file.read().strip('\n')) self._signer = CryptoFactory(create_context('secp256k1')) \ .new_signer(private_key) self._factories = {} self._client = RestClient(url=self._rest_endpoint) self._add_transaction_family_factory(Families.INTKEY) self._add_transaction_family_factory(Families.XO)
def __init__(self, name, rest_endpoint): """ Args: name (str): An identifier for this Transactor rest_endpoint (str): The rest api that this Transactor will communicate with. """ self.name = name self._rest_endpoint = rest_endpoint \ if rest_endpoint.startswith("http://") \ else "http://{}".format(rest_endpoint) with open('/root/.sawtooth/keys/{}.priv'.format(name)) as priv_file: private_key = priv_file.read().strip('\n') self._private_key = private_key with open('/root/.sawtooth/keys/{}.pub'.format(name)) as pub_file: public_key = pub_file.read().strip('\n') self._public_key = public_key self._factories = {} self._client = RestClient(url=self._rest_endpoint) self._add_transaction_family_factory(Families.INTKEY) self._add_transaction_family_factory(Families.XO)
def __init__(self, rest_endpoint): context = create_context('secp256k1') private_key = context.new_random_private_key() self.priv_key = private_key.as_hex() self.pub_key = context.get_public_key(private_key).as_hex() self.signer = CryptoFactory(context).new_signer(private_key) self._namespace = hashlib.sha512('intkey'.encode()).hexdigest()[:6] self._factory = MessageFactory( 'intkey', '1.0', self._namespace, signer=self.signer) self._rest = RestClient(rest_endpoint)
class Transactor: def __init__(self, name, rest_endpoint): """ Args: name (str): An identifier for this Transactor rest_endpoint (str): The rest api that this Transactor will communicate with. """ self.name = name self._rest_endpoint = rest_endpoint \ if rest_endpoint.startswith("http://") \ else "http://{}".format(rest_endpoint) with open('/root/.sawtooth/keys/{}.priv'.format(name)) as priv_file: private_key = Secp256k1PrivateKey.from_hex( priv_file.read().strip('\n')) self._signer = CryptoFactory(create_context('secp256k1')) \ .new_signer(private_key) self._factories = {} self._client = RestClient(url=self._rest_endpoint) self._add_transaction_family_factory(Families.INTKEY) self._add_transaction_family_factory(Families.XO) @property def public_key(self): return self._signer.get_public_key().as_hex() def _add_transaction_family_factory(self, family_name): """Add a MessageFactory for the specified family. Args: family_name (Families): One of the Enum values representing transaction families. """ family_config = FAMILY_CONFIG[family_name] self._factories[family_name] = MessageFactory( family_name=family_config['family_name'], family_version=family_config['family_version'], namespace=family_config['namespace'], signer=self._signer) def create_txn(self, family_name, batcher=None): unique_value = uuid4().hex[:20] encoder = TRANSACTION_ENCODER[family_name]['encoder'] payload = encoder( TRANSACTION_ENCODER[family_name]['payload_func'](unique_value)) address = TRANSACTION_ENCODER[family_name]['address_func']( unique_value) return self._factories[family_name].create_transaction( payload=payload, inputs=[address], outputs=[address], deps=[], batcher=batcher) def create_batch(self, family_name, count=1): transactions = [self.create_txn(family_name) for _ in range(count)] return self.batch_transactions(family_name, transactions=transactions) def batch_transactions(self, family_name, transactions): return self._factories[family_name].create_batch( transactions=transactions) def send(self, family_name, transactions=None): if not transactions: batch_list = self.create_batch(family_name) else: batch_list = self.batch_transactions(family_name=family_name, transactions=transactions) self._client.send_batches(batch_list=batch_list) def set_public_key_for_role(self, policy, role, permit_keys, deny_keys): permits = ["PERMIT_KEY {}".format(key) for key in permit_keys] denies = ["DENY_KEY {}".format(key) for key in deny_keys] self._run_identity_commands(policy, role, denies + permits) def _run_identity_commands(self, policy, role, rules): subprocess.run([ 'sawtooth', 'identity', 'policy', 'create', '-k', '/root/.sawtooth/keys/{}.priv'.format(self.name), '--wait', '15', '--url', self._rest_endpoint, policy, *rules ], check=True) subprocess.run([ 'sawtooth', 'identity', 'role', 'create', '-k', '/root/.sawtooth/keys/{}.priv'.format(self.name), '--wait', '15', '--url', self._rest_endpoint, role, policy ], check=True)
class Transactor(object): def __init__(self, name, rest_endpoint): """ Args: name (str): An identifier for this Transactor rest_endpoint (str): The rest api that this Transactor will communicate with. """ self.name = name self._rest_endpoint = rest_endpoint \ if rest_endpoint.startswith("http://") \ else "http://{}".format(rest_endpoint) with open('/root/.sawtooth/keys/{}.priv'.format(name)) as priv_file: private_key = Secp256k1PrivateKey.from_hex( priv_file.read().strip('\n')) self._signer = CryptoFactory(create_context('secp256k1')) \ .new_signer(private_key) self._factories = {} self._client = RestClient(url=self._rest_endpoint) self._add_transaction_family_factory(Families.INTKEY) self._add_transaction_family_factory(Families.XO) @property def public_key(self): return self._signer.get_public_key().as_hex() def _add_transaction_family_factory(self, family_name): """Add a MessageFactory for the specified family. Args: family_name (Families): One of the Enum values representing transaction families. """ family_config = FAMILY_CONFIG[family_name] self._factories[family_name] = MessageFactory( family_name=family_config['family_name'], family_version=family_config['family_version'], namespace=family_config['namespace'], signer=self._signer) def create_txn(self, family_name, batcher=None): unique_value = uuid4().hex[:20] encoder = TRANSACTION_ENCODER[family_name]['encoder'] payload = encoder( TRANSACTION_ENCODER[family_name]['payload_func'](unique_value)) address = TRANSACTION_ENCODER[family_name]['address_func']( unique_value) return self._factories[family_name].create_transaction( payload=payload, inputs=[address], outputs=[address], deps=[], batcher=batcher) def create_batch(self, family_name, count=1): transactions = [self.create_txn(family_name) for _ in range(count)] return self.batch_transactions(family_name, transactions=transactions) def batch_transactions(self, family_name, transactions): return self._factories[family_name].create_batch( transactions=transactions) def send(self, family_name, transactions=None): if not transactions: batch_list = self.create_batch(family_name) else: batch_list = self.batch_transactions( family_name=family_name, transactions=transactions) self._client.send_batches(batch_list=batch_list) def set_public_key_for_role(self, policy, role, permit_keys, deny_keys): permits = ["PERMIT_KEY {}".format(key) for key in permit_keys] denies = ["DENY_KEY {}".format(key) for key in deny_keys] self._run_identity_commands(policy, role, denies + permits) def _run_identity_commands(self, policy, role, rules): subprocess.run( ['sawtooth', 'identity', 'policy', 'create', '-k', '/root/.sawtooth/keys/{}.priv'.format(self.name), '--wait', '15', '--url', self._rest_endpoint, policy, *rules], check=True) subprocess.run( ['sawtooth', 'identity', 'role', 'create', '-k', '/root/.sawtooth/keys/{}.priv'.format(self.name), '--wait', '15', '--url', self._rest_endpoint, role, policy], check=True)