def create_account(self, nick: str) -> User: user = User(nick, self.domain) tx = self.iroha.transaction( [ self.iroha.command( "CreateAccount", account_name=user.nick, domain_id=user.domain, public_key=user.public_key, ) ] ) IrohaCrypto.sign_transaction(tx, self._private_key) send_transaction_and_return_status(tx) return user
def sign_tx_callback(private_key): for i in tx_idxs: transaction = self._application.transactions[i] signed_transaction = IrohaCrypto.sign_transaction( transaction, private_key ) transaction.CopyFrom(signed_transaction)
def get_account_details(): params = integration_helpers.get_first_four_bytes_of_keccak( b"getAccountDetail()") no_of_param = 0 tx = iroha.transaction([ iroha.command("CallEngine", caller=ADMIN_ACCOUNT_ID, callee=address, input=params) ]) IrohaCrypto.sign_transaction(tx, ADMIN_PRIVATE_KEY) response = net.send_tx(tx) for status in net.tx_status_stream(tx): print(status) hex_hash = binascii.hexlify(IrohaCrypto.hash(tx)) return hex_hash
def create_new_account(self, account_name, domain, public_key): """ register new user """ tx = self.iroha.transaction( [ self.iroha.command( "CreateAccount", account_name=account_name, domain_id=domain, public_key=public_key, ) ] ) ic.sign_transaction(tx, self.user_private_key) self.submit_transaction(tx)
def debit_account(account_id, amount, account_private_key): """ Make admin@test able to set detail to {account_id}@test """ print(f'Debiting ${amount} from {account_id}') account_assets = get_account_assets(account_id) asset_id = account_assets[0].asset_id #assuming only one asset tx = iroha.transaction([ iroha.command( 'SubtractAssetQuantity', asset_id=asset_id, amount=amount) ], creator_account=account_id) IrohaCrypto.sign_transaction(tx, account_private_key) send_transaction_and_print_status(tx)
def create_domain(domain): commands = [ iroha.command('CreateDomain', domain_id=domain, default_role='user'), ] tx = ic.sign_transaction(iroha.transaction(commands), admin_private_key) send_transaction_and_print_status(tx) return
def satoshi_creates_exchange_batch(): """ Satoshi creates decentralized exchanged branch """ global iroha_satoshi global iroha_nakamoto ''' '100' amount of 'scoin' will be transferred from 'satoshi' account to 'nakamoto' account ''' satoshi_tx = iroha_satoshi.transaction([ iroha_satoshi.command('TransferAsset', src_account_id='satoshi@test', dest_account_id='nakamoto@test', asset_id='scoin#test', amount='100') ], creator_account='satoshi@test') ''' '200' amount of 'ncoin' will be transferred from 'nakamoto' account to 'satoshi' account ''' nakamoto_tx = iroha_nakamoto.transaction([ iroha_nakamoto.command('TransferAsset', src_account_id='nakamoto@test', dest_account_id='satoshi@test', asset_id='ncoin#test', amount='200') ], creator_account='nakamoto@test') ''' Creating the batch of transactions for sending several transactions This is atomic batch which means each and every transaction must need to pass all type of validations and after that all the transactions of this batch will commit transactional changes into leger. ''' iroha_satoshi.batch([satoshi_tx, nakamoto_tx], atomic=True) ''' Satoshi sign only his transaction with his private key ''' IrohaCrypto.sign_transaction(satoshi_tx, satoshi_private_key) ''' Finally send the atomic batch of transactions to Iroha Peer ''' send_batch_and_print_status([satoshi_tx, nakamoto_tx]) time.sleep(10)
def add_asset(name, domain, precision): commands = [iroha.command('CreateAsset', asset_name=name, domain_id=domain, precision=2) ] tx = IrohaCrypto.sign_transaction( iroha.transaction(commands), ADMIN_PRIVATE_KEY) return jsonify(send_transaction_and_print_status(tx))
def create_account(domain, name, public_key): """ Create a personal account. :param domain: (str) Name of the domain the user wants to join :param name: (str) Name of the node we are creating :param public_key: (str) public key of the node :return: null: """ # 1. Create account tx = iroha_config.IROHA_ADMIN.transaction([ iroha_config.IROHA_ADMIN.command('CreateAccount', account_name=name, domain_id=domain, public_key=public_key) ]) IrohaCrypto.sign_transaction(tx, iroha_config.ADMIN_PRIVATE_KEY) send_transaction_and_print_status(tx)
def revoke_set_account_detail_perms(self, user): """ Revoke creator account able to set detail to account """ tx = self.iroha.transaction( [ self.iroha.command( "RevokePermission", account_id= f"{self.creator_account_details.gov_id}@afyamkononi", permission=can_set_my_account_detail, ) ], creator_account=f"{user.gov_id}@afyamkononi", ) IrohaCrypto.sign_transaction(tx, user.private_key) return self.send_transaction_and_return_status(tx)
def __process_txs_signatures(self, block): """Recalculate transactions signatures and resign them""" txs = block.block_v1.payload.transactions i = 0 total_txs = len(txs) while i < total_txs: tx = txs[i] creator = tx.payload.reduced_payload.creator_account_id tx_keypairs = [] for signature in tx.signatures: kp = self.keystore.renew_key(signature.public_key, user=creator) tx_keypairs.append(kp) del tx.signatures[:] for kp in tx_keypairs: IrohaCrypto.sign_transaction(tx, kp.private_key) i += 1
def get_account_details(contract_address: str, user_account: str): iroha_user = Iroha(user_account) params = integration_helpers.get_first_four_bytes_of_keccak( b"getAccountDetail()") no_of_param = 0 tx = iroha_user.transaction([ iroha_user.command("CallEngine", caller=user_account, callee=contract_address, input=params) ]) IrohaCrypto.sign_transaction(tx, user_private_key) response = net.send_tx(tx) for status in net.tx_status_stream(tx): print(status) hex_hash = binascii.hexlify(IrohaCrypto.hash(tx)) return hex_hash
def init_game(): global iroha cmds = [ iroha.command('SetAccountDetail', account_id=GAME_ACCOUNT_ID, key='state', value='-,-,-,-,-,-,-,-,-') ] tx = iroha.transaction(cmds, creator_account=GAME_ACCOUNT_ID, quorum=1) ic.sign_transaction(tx, ADMIN_PRIVATE_KEY) send_transaction_and_print_status(tx) cmds = [ iroha.command('SetAccountQuorum', account_id=GAME_ACCOUNT_ID, quorum=2) ] tx = iroha.transaction(cmds) ic.sign_transaction(tx, ADMIN_PRIVATE_KEY) send_transaction_and_print_status(tx)
def transfer_coin_from_src_to_dest(src, dest, ass): """ Transfer 1 'organ' from 'donor' to 'patient' """ srcaccid = src + '@' + 'patient' destaccid = dest + '@' + 'patient' assetid = ass + '#' + 'organ' tx = iroha.transaction([ iroha.command('TransferAsset', src_account_id='admin@odwa', dest_account_id=destaccid, asset_id=assetid, description='Transferred', amount='1') ]) IrohaCrypto.sign_transaction(tx, ADMIN_PRIVATE_KEY) result = send_transaction_and_print_status(tx) return result
def CreateDomainAsset(self): commands = [ self.iroha.command('CreateDomain', domain_id='domain', default_role='user'), self.iroha.command('CreateAsset', asset_name='coin', domain_id='domain', precision=2) ] tx = IrohaCrypto.sign_transaction( self.iroha.transaction(commands), self.admin_private_key) self.SendTxAndPrintstatus(tx)
def create_domain(domain): """ register non existing/new domain on network """ commands = [ iroha.command('CreateDomain', domain_id=domain, default_role='user'), ] tx = ic.sign_transaction(iroha.transaction(commands), admin_private_key) send_transaction_and_print_status(tx)
def create_domain(domainID, defaultRole='user'): """ Creates domain @domainID """ commands = [ iroha.command('CreateDomain', domain_id=domainID, default_role=defaultRole), ] tx = IrohaCrypto.sign_transaction( iroha.transaction(commands), ADMIN_PRIVATE_KEY) send_transaction_and_print_status(tx)
def multi_signature_transaction(): global iroha_satoshi_1 global iroha_satoshi_2 multi_sign_tx = iroha_satoshi_1.transaction([ iroha_satoshi_1.command('TransferAsset', src_account_id='satoshi@test', dest_account_id='nakamoto@test', asset_id='scoin#test', amount='10') ], creator_account='satoshi@test', quorum=2) IrohaCrypto.sign_transaction(multi_sign_tx, satoshi_private_key_1) #Satoshi_2 can find pending transactions from peer if Satoshi_1 sent that on peer #or Satoshi_1 may pass that via a messaging channel IrohaCrypto.sign_transaction(multi_sign_tx, satoshi_private_key_2) send_transaction_and_print_status(multi_sign_tx)
def create_client(self, client_name): user_private_key = IrohaCrypto.private_key() user_public_key = IrohaCrypto.derive_public_key(user_private_key) commands = [ self.iroha.command('CreateAccount', account_name=client_name, domain_id="test", public_key=user_public_key), ] transaction = self.iroha.transaction(commands) IrohaCrypto.sign_transaction(transaction, self.admin_private_key) self.__send_transaction_and_print_status(transaction) tx = self.iroha.transaction([ self.iroha.command('GrantPermission', account_id='admin@test', permission=primitive_pb2.can_set_my_account_detail) ]) IrohaCrypto.sign_transaction(tx, user_private_key) if self.__send_transaction_and_print_status(transaction) == "COMMITTED": return "Your key: " + user_private_key, 201 else: return 'Internal Error', 500
def exchange_asset( self, asset_from: str, amount_from: int, asset_to: str, amount_to: str ): tx = self.iroha.transaction( [ self.iroha.command( "SubtractAssetQuantity", asset_id=f"{asset_from}#{self.domain}", amount=str(amount_from), ), self.iroha.command( "AddAssetQuantity", asset_id=f"{asset_to}#{self.domain}", amount=str(amount_to), ), ] ) IrohaCrypto.sign_transaction(tx, self._private_key) return str(send_transaction_and_return_status(tx))
def create_domain(self, domain): """ Creates a domain :Example: >>> public = Domain('public', 'default_role') >>> admin = Admin('123.456.789') >>> admin.create_domain(public) :param Domain domain: domain to be created """ tx = self.iroha.transaction([ self.iroha.command('CreateDomain', domain_id=domain.name, default_role=domain.default_role) ]) IrohaCrypto.sign_transaction(tx, self.private_key) send_transaction_and_print_status(tx, self.network)
def create_domain_and_asset(domainID, asset): """ Creates domain @domainID and asset $asset#domain """ commands = [ iroha.command('CreateDomain', domain_id=domainID, default_role='user'), iroha.command('CreateAsset', asset_name=asset, domain_id=domainID, precision=2) ] tx = IrohaCrypto.sign_transaction( iroha.transaction(commands), ADMIN_PRIVATE_KEY) send_transaction_and_print_status(tx)
def append_role(self, user): """ Appends a role to a user """ tx = self.iroha.transaction( [ self.iroha.command( "AppendRole", account_id=f"{user.gov_id}@afyamkononi", role_name=user.type, ) ], creator_account= f"{self.creator_account_details.gov_id}@afyamkononi", ) IrohaCrypto.sign_transaction(tx, self.creator_account_details.private_key) return self.send_transaction_and_return_status(tx)
def bob_declines_exchange_request(): print(""" IT IS EXPECTED HERE THAT THE BATCH WILL FAIL STATEFUL VALIDATION """) global net q = ic.sign_query( Iroha('bob@test').query('GetPendingTransactions'), bob_private_keys[0]) pending_transactions = net.send_query(q) for tx in pending_transactions.transactions_response.transactions: if tx.payload.reduced_payload.creator_account_id == 'alice@test': # we need do this temporarily, otherwise accept will not reach MST engine del tx.signatures[:] else: # intentionally alice keys were used to fail bob's txs ic.sign_transaction(tx, *alice_private_keys) # zeroes as private keys are also acceptable send_batch_and_print_status( pending_transactions.transactions_response.transactions)
def put_item(self, item, account, company, private_key): if not self.is_insurable_item(item=item, private_key=private_key): return 'Item is already insured', 409 item["account"] = account item["company"] = company commands = [ self.iroha.command('SetAccountDetail', account_id=self.admin_account, key=item['item_id'], value=base64.urlsafe_b64encode( json.dumps(item).encode()).decode()), ] transaction = self.iroha.transaction( commands=commands, creator_account=self.admin_account) IrohaCrypto.sign_transaction(transaction, private_key) if self.__send_transaction_and_print_status( transaction) == "COMMITTED": return str(item), 201 else: return 'Internal Error', 500
def init_operation(): global iroha_admin init_cmds = [ iroha_admin.command('CreateAsset', asset_name='scoin', domain_id='test', precision=2), iroha_admin.command('CreateAsset', asset_name='ncoin', domain_id='test', precision=2), iroha_admin.command('AddAssetQuantity', asset_id='scoin#test', amount='10000'), iroha_admin.command('AddAssetQuantity', asset_id='ncoin#test', amount='20000'), iroha_admin.command('CreateAccount', account_name='satoshi', domain_id='test', public_key=satoshi_public_key), iroha_admin.command('CreateAccount', account_name='nakamoto', domain_id='test', public_key=nakamoto_public_key), iroha_admin.command('TransferAsset', src_account_id='admin@test', dest_account_id='satoshi@test', asset_id='scoin#test', description='init top up', amount='10000'), iroha_admin.command('TransferAsset', src_account_id='admin@test', dest_account_id='nakamoto@test', asset_id='ncoin#test', description='init top up', amount='20000') ] init_tx = iroha_admin.transaction(init_cmds) IrohaCrypto.sign_transaction(init_tx, ADMIN_PRIVATE_KEY) send_transaction_and_print_status(init_tx)
def create_users(): global iroha init_cmds = [ iroha.command('CreateAsset', asset_name='bitcoin', domain_id='test', precision=2), iroha.command('CreateAsset', asset_name='dogecoin', domain_id='test', precision=2), iroha.command('AddAssetQuantity', asset_id='bitcoin#test', amount='100000'), iroha.command('AddAssetQuantity', asset_id='dogecoin#test', amount='20000'), iroha.command('CreateAccount', account_name='alice', domain_id='test', public_key=alice_public_keys[0]), iroha.command('CreateAccount', account_name='bob', domain_id='test', public_key=bob_public_keys[0]), iroha.command('TransferAsset', src_account_id='admin@test', dest_account_id='alice@test', asset_id='bitcoin#test', description='init top up', amount='100000'), iroha.command('TransferAsset', src_account_id='admin@test', dest_account_id='bob@test', asset_id='dogecoin#test', description='init doge', amount='20000') ] init_tx = iroha.transaction(init_cmds) ic.sign_transaction(init_tx, ADMIN_PRIVATE_KEY) send_transaction_and_print_status(init_tx)
def set_patient_record(self, patient, history_update): """ Set patient records """ history_update = (json.dumps(history_update)).replace('"', '\\"') tx = self.iroha.transaction([ self.iroha.command( "SetAccountDetail", account_id=f"{patient.gov_id}@afyamkononi", key="gov_id", value=f"{patient.gov_id}", ), self.iroha.command( "SetAccountDetail", account_id=f"{patient.gov_id}@afyamkononi", key="name", value=f"{patient.name}", ), self.iroha.command( "SetAccountDetail", account_id=f"{patient.gov_id}@afyamkononi", key="email", value=f"{patient.email}", ), self.iroha.command( "SetAccountDetail", account_id=f"{patient.gov_id}@afyamkononi", key="phone_number", value=f"{patient.phone_number}", ), self.iroha.command( "SetAccountDetail", account_id=f"{patient.gov_id}@afyamkononi", key="medical_data", value=history_update, ) ]) IrohaCrypto.sign_transaction(tx, self.creator_account_details.private_key) return self.send_transaction_and_return_status(tx)
def genesis_tx(): test_permissions = [primitive_pb2.can_get_domain_acc_ast_txs] genesis_commands = commons.genesis_block(admin, alice, test_permissions) genesis_commands.extend([ iroha.command('CreateAsset', asset_name='coin', domain_id='test', precision=2), iroha.command('AddAssetQuantity', asset_id='coin#test', amount='500.69'), iroha.command('TransferAsset', src_account_id=admin['id'], dest_account_id=alice['id'], asset_id='coin#test', description='top up', amount='10.00') ]) tx = iroha.transaction(genesis_commands) IrohaCrypto.sign_transaction(tx, admin['key']) return tx
def get_account(address): params = integration_helpers.get_first_four_bytes_of_keccak( b"getAccount(string)") no_of_param = 1 for x in range(no_of_param): params = params + integration_helpers.left_padded_address_of_param( x, no_of_param) params = params + integration_helpers.argument_encoding( "test@burrow") # account id tx = iroha.transaction([ iroha.command("CallEngine", caller=ADMIN_ACCOUNT_ID, callee=address, input=params) ]) IrohaCrypto.sign_transaction(tx, ADMIN_PRIVATE_KEY) response = net.send_tx(tx) for status in net.tx_status_stream(tx): print(status) hex_hash = binascii.hexlify(IrohaCrypto.hash(tx)) return hex_hash