def _create_role_txn(public_key, signing_key, role_name, policy_name): role = Role(name=role_name, policy_name=policy_name) payload = IdentityPayload(type=IdentityPayload.ROLE, data=role.SerializeToString()) policy_address = _policy_to_address(policy_name) role_address = _role_to_address(role_name) header = TransactionHeader( signer_public_key=public_key, family_name='sawtooth_identity', family_version='1.0', inputs=[_REQUIRED_INPUT, policy_address, role_address], outputs=[role_address], dependencies=[], payload_sha512=hashlib.sha512(payload.SerializeToString()).hexdigest(), batcher_public_key=public_key, nonce=time.time().hex().encode()) header_bytes = header.SerializeToString() signature = signing.sign(header_bytes, signing_key) transaction = Transaction(header=header_bytes, payload=payload.SerializeToString(), header_signature=signature) return transaction
def _create_policy_txn(signer, policy_name, rules): entries = [] for rule in rules: rule = rule.split(" ") if rule[0] == "PERMIT_KEY": entry = Policy.Entry(type=Policy.PERMIT_KEY, key=rule[1]) entries.append(entry) elif rule[0] == "DENY_KEY": entry = Policy.Entry(type=Policy.DENY_KEY, key=rule[1]) entries.append(entry) policy = Policy(name=policy_name, entries=entries) payload = IdentityPayload(type=IdentityPayload.POLICY, data=policy.SerializeToString()) policy_address = _policy_to_address(policy_name) header = TransactionHeader( signer_public_key=signer.get_public_key().as_hex(), family_name='sawtooth_identity', family_version='1.0', inputs=[_REQUIRED_INPUT, policy_address], outputs=[policy_address], dependencies=[], payload_sha512=hashlib.sha512(payload.SerializeToString()).hexdigest(), batcher_public_key=signer.get_public_key().as_hex(), nonce=hex(random.randint(0, 2**64))) header_bytes = header.SerializeToString() transaction = Transaction(header=header_bytes, payload=payload.SerializeToString(), header_signature=signer.sign(header_bytes)) return transaction
def _create_config_txn(pubkey, signing_key, setting_key_value): """Creates an individual sawtooth_config transaction for the given key and value. """ setting_key = setting_key_value[0] setting_value = setting_key_value[1] nonce = str(datetime.datetime.utcnow().timestamp()) proposal = ConfigProposal(setting=setting_key, value=setting_value, nonce=nonce) payload = ConfigPayload(data=proposal.SerializeToString(), action=ConfigPayload.PROPOSE).SerializeToString() header = TransactionHeader( signer_pubkey=pubkey, family_name='sawtooth_config', family_version='1.0', inputs=_config_inputs(setting_key), outputs=_config_outputs(setting_key), dependencies=[], payload_encoding='application/protobuf', payload_sha512=hashlib.sha512(payload).hexdigest(), batcher_pubkey=pubkey).SerializeToString() signature = signing.sign(header, signing_key) return Transaction(header=header, header_signature=signature, payload=payload)
def _check_required_settings(batches): """Ensure that all settings required at genesis are set.""" required_settings = REQUIRED_SETTINGS.copy() for batch in batches: for txn in batch.transactions: txn_header = TransactionHeader() txn_header.ParseFromString(txn.header) if txn_header.family_name == 'sawtooth_settings': settings_payload = SettingsPayload() settings_payload.ParseFromString(txn.payload) if settings_payload.action == SettingsPayload.PROPOSE: proposal = SettingProposal() proposal.ParseFromString(settings_payload.data) if proposal.setting in required_settings: required_settings.remove(proposal.setting) if required_settings: raise CliException( 'The following setting(s) are required at genesis, but were not ' 'included in the genesis batches: {}'.format(required_settings))
def _create_policy_txn(pubkey, signing_key, policy_name, rules): entries = [] for rule in rules: rule = rule.split(" ") if rule[0] == "PERMIT_KEY": entry = Policy.Entry(type=Policy.PERMIT_KEY, key=rule[1]) entries.append(entry) elif rule[0] == "DENY_KEY": entry = Policy.Entry(type=Policy.DENY_KEY, key=rule[1]) entries.append(entry) policy = Policy(name=policy_name, entries=entries) payload = IdentityPayload(type=IdentityPayload.POLICY, data=policy.SerializeToString()) policy_address = _policy_to_address(policy_name) header = TransactionHeader( signer_pubkey=pubkey, family_name='sawtooth_identity', family_version='1.0', inputs=[policy_address], outputs=[policy_address], dependencies=[], payload_encoding="application/protobuf", payload_sha512=hashlib.sha512( payload.SerializeToString()).hexdigest(), batcher_pubkey=pubkey, nonce=time.time().hex().encode()) header_bytes = header.SerializeToString() signature = signing.sign(header_bytes, signing_key) transaction = Transaction( header=header_bytes, payload=payload.SerializeToString(), header_signature=signature) return transaction
def _validate_depedencies(batches): """Validates the transaction dependencies for the transactions contained within the sequence of batches. Given that all the batches are expected to to be executed for the genesis blocks, it is assumed that any dependent transaction will proceed the depending transaction. """ transaction_ids = set() for batch in batches: for txn in batch.transactions: txn_header = TransactionHeader() txn_header.ParseFromString(txn.header) if len(txn_header.dependencies) > 0: unsatisfied_deps = [id for id in txn_header.dependencies if id not in transaction_ids] if len(unsatisfied_deps) != 0: raise CliException( 'Unsatisfied dependency in given transactions:' ' {}'.format(unsatisfied_deps)) transaction_ids.add(txn.header_signature)
def transaction(txn_sig, dependencies): header = TransactionHeader( signer_public_key='test_public_key', family_name='test_family', family_version='1.0', inputs=[], outputs=[], dependencies=dependencies, payload_sha512='some_sha512', batcher_public_key='test_public_key').SerializeToString() return Transaction(header=header, header_signature=txn_sig, payload=b'')
def _make_txn(signer, setting_key, payload): """Creates and signs a bgx_settings transaction with with a payload. """ serialized_payload = payload.SerializeToString() header = TransactionHeader( signer_public_key=signer.get_public_key().as_hex(), family_name='bgx_settings', # 'sawtooth_settings' family_version='1.0', inputs=_config_inputs(setting_key), outputs=_config_outputs(setting_key), dependencies=[], payload_sha512=hashlib.sha512(serialized_payload).hexdigest(), batcher_public_key=signer.get_public_key().as_hex()).SerializeToString( ) return Transaction(header=header, header_signature=signer.sign(header), payload=serialized_payload)
def _make_txn(pubkey, signing_key, setting_key, payload): """Creates and signs a sawtooth_config transaction with with a payload. """ serialized_payload = payload.SerializeToString() header = TransactionHeader( signer_pubkey=pubkey, family_name='sawtooth_config', family_version='1.0', inputs=_config_inputs(setting_key), outputs=_config_outputs(setting_key), dependencies=[], payload_encoding='application/protobuf', payload_sha512=hashlib.sha512(serialized_payload).hexdigest(), batcher_pubkey=pubkey).SerializeToString() signature = signing.sign(header, signing_key) return Transaction(header=header, header_signature=signature, payload=serialized_payload)