Exemplo n.º 1
0
def create_account_user(iroha, network, name, public_key, domain_id, asset_qty,
                        asset_id):
    """
    Create a personal account. This function works in three steps
        1. Create an account with a name, in a domain and a public key
        2. The admin create credit (assets) for the account (credit is created only if the user
           buy it)
        3. The admin transfer the credit to the user
    :param iroha: (Iroha('name@domain')) Address for connecting to a domain
    :param network: (IrohaGrpc('IP address')) Physical address of one node running the BSMD
    :param name: (str) Name of the node we are creating
    :param public_key: (str) public key of the node
    :param domain_id: (str) Name of the domain the user wants to join
    :param asset_qty: (float) Quantity of assets the node buy
    :param asset_id: (name#domain) Name of asset the node buy
    :return: null:

    Usage example:
    create_account_user(Iroha('david@federated'), IrohaGrpc('127.0.0.1'), 'Tommy', 'key', 'federated', '5', fedcoin#federated)
    """
    # 1. Create account
    tx = iroha.transaction([
        iroha.command('CreateAccount',
                      account_name=name,
                      domain_id=domain_id,
                      public_key=public_key)
    ])
    IrohaCrypto.sign_transaction(tx, admin_private_key)
    send_transaction_and_print_status(tx, network)
Exemplo n.º 2
0
def get_all_details(iroha, network, account_id, private_key):
    """
    Consult all details of the node
    :param iroha: (Iroha(name@domain)) Address for connecting to a domain
    :param network: (IrohaGrpc(IP address)) Physical address of one node running the BSMD
    :param account_id: (name@domain) Id of the user in the domain
    :param private_key: (str) Private key of the user
    :return: data: (json) solicited details of the user

    Usage example:
    get_detail_from_generator(Iroha('david@federated'),IrohaGrpc('127.0.0.1'), 'david@federated', 'key')

    Return example:
    {
        "nodeA@domain":{
            "Age":"35",
            "Name":"Quetzacoatl"
        },
        "nodeB@domain":{
            "Location":"35.3333535,-45.2141556464",
            "Status":"valid"
        },
        "nodeA@domainB":{
            "FederatingParam":"35.242553",
            "Loop":"3"
        }
    }
    """
    query = iroha.query('GetAccountDetail', account_id=account_id)
    IrohaCrypto.sign_query(query, private_key)

    response = network.send_query(query)
    data = response.account_detail_response
    # print('Account id = {}, details = {}'.format(account_id, data.detail))
    return data.detail
Exemplo n.º 3
0
def get_balance(iroha, network, account_id, private_key):
    """
    Get the balance of the account
    :param iroha: (Iroha('name@domain')) Address for connecting to a domain
    :param network: (IrohaGrpc('IP address')) Physical address of one node running the BSMD
    :param account_id: (name@domain) Id of the user in the domain
    :param private_key: (str) Private key of the user
    :return: data: (array) asset id and assets quantity

    Usage example:
    get_balance(Iroha('david@federated'), IrohaGrpc('127.0.0.1'), 'david@federated', 'key')

    Return example:
    [asset_id: "fedcoin#federated"
    account_id: "generator@federated"
    balance: "1000"
    ]
    """
    query = iroha.query('GetAccountAssets', account_id=account_id)
    IrohaCrypto.sign_query(query, private_key)

    response = network.send_query(query)
    data = response.account_assets_response.account_assets
    for asset in data:
        print('Asset id = {}, balance = {}'.format(asset.asset_id,
                                                   asset.balance))
    return data
Exemplo n.º 4
0
def get_all_details_from_generator(iroha, network, account_id, private_key,
                                   generator_id):
    """
    Consult all the details generated by some node
    :param iroha: (Iroha(name@domain)) Address for connecting to a domain
    :param network: (IrohaGrpc(IP address)) Physical address of one node running the BSMD
    :param account_id: (name@domain) Id of the user in the domain
    :param private_key: (str) Private key of the user
    :param generator_id: (name@domain) Id of the user who create de detail
    :return: data: (json) solicited details of the user

    Usage example:
    get_detail_from_generator(Iroha('david@federated'),IrohaGrpc('127.0.0.1'), 'david@federated', 'key',
                              'david@federated')

    Return example:
    {
       "nodeA@domain":{
            "Age":"35",
            "Name":"Quetzacolatl"
        }
    }
    """

    query = iroha.query('GetAccountDetail',
                        account_id=account_id,
                        writer=generator_id)
    IrohaCrypto.sign_query(query, private_key)

    response = network.send_query(query)
    data = response.account_detail_response
    # print('Account id = {}, details = {}'.format(account_id, data.detail))
    return data.detail
Exemplo n.º 5
0
def alice_creates_exchange_batch():
    alice_tx = iroha.transaction([
        iroha.command('TransferAsset',
                      src_account_id='alice@test',
                      dest_account_id='bob@test',
                      asset_id='bitcoin#test',
                      amount='1')
    ],
                                 creator_account='alice@test',
                                 quorum=2)
    bob_tx = iroha.transaction(
        [
            iroha.command('TransferAsset',
                          src_account_id='bob@test',
                          dest_account_id='alice@test',
                          asset_id='dogecoin#test',
                          amount='2')
        ],
        creator_account='bob@test'
        # we intentionally omit here bob's quorum, since alice is the originator of the exchange and in general case
        # alice does not know bob's quorum.
        # bob knowing own quorum in case of accept should sign the tx using all the number of missing keys at once
    )
    iroha.batch(alice_tx, bob_tx, atomic=True)
    # sign transactions only after batch meta creation
    ic.sign_transaction(alice_tx, *alice_private_keys)
    send_batch_and_print_status(alice_tx, bob_tx)
Exemplo n.º 6
0
def remove_signatory(address):
    params = integration_helpers.get_first_four_bytes_of_keccak(
        b"removeSignatory(string,string)"
    )
    no_of_param = 2
    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(
        ADMIN_ACCOUNT_ID
    )  # account id
    params = params + integration_helpers.argument_encoding(
        test_public_key
    )  # public key
    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
Exemplo n.º 7
0
def transfer_assets(iroha, network, account_id, private_key,
                    destination_account, asset_id, quantity, description):
    """
    Transfer assets from one account to another
    :param iroha: (Iroha(name@domain)) Address for connecting to a domain
    :param network: (IrohaGrpc(IP address)) Physical address of one node running the BSMD
    :param account_id: (name@domain) Id of the user in the domain
    :param private_key: (str) Private key of the user
    :param destination_account: (name@domain) Id of the destination account
    :param asset_id: (name#domain) Id of the asset we want to transfer
    :param quantity: (float) Number of assets we want to transfer
    :param description: (str) Small message to the receiver of assets
    :return: null:

    Usage example:
    transfer_assets(Iroha('david@federated'),IrohaGrpc('127.0.0.1'), 'david@federated', 'key',
                    'toro@federated', 'fedcoin#federated', '2', 'Shut up and take my money')
    """
    tx = iroha.transaction([
        iroha.command('TransferAsset',
                      src_account_id=account_id,
                      dest_account_id=destination_account,
                      asset_id=asset_id,
                      description=description,
                      amount=quantity)
    ])
    IrohaCrypto.sign_transaction(tx, private_key)
    send_transaction_and_print_status(tx, network)
Exemplo n.º 8
0
    def init_ledger(self):
        print('init ledger...')
        tx = self.iroha.transaction([
            self.iroha.command('CreateAccount',
                               account_name=sawmill.account_name,
                               domain_id=sawmill.domain,
                               public_key=sawmill.public_key)
            for sawmill in self.sawmills
        ])
        IrohaCrypto.sign_transaction(tx, self.admin_private_key)
        print(self.send_transaction_and_log_status(tx))

        print("=" * 20)
        tx_commands = []
        for i in self.sawmills:
            for j in self.woods:
                tx_commands.append(
                    self.iroha.command(
                        'TransferAsset',
                        src_account_id='admin@test',
                        dest_account_id=f'{i.account_name}@{self.domain_name}',
                        asset_id=f'{j}#{self.domain_name}',
                        amount=str(randint(1, 100))))
        tx = self.iroha.transaction(tx_commands)
        IrohaCrypto.sign_transaction(tx, self.admin_private_key)
        print(self.send_transaction_and_log_status(tx))
        self.get_admin_details()
        print(self.get_accounts_info())
        print("=" * 20)
Exemplo n.º 9
0
    def __init__(self, sawmills, l):
        self.domain_name = "trade"
        self.admin_private_key = ADMIN_PRIVATE_KEY
        self.iroha = Iroha(ADMIN_ACCOUNT_ID)
        self.net = IrohaGrpc('{}:{}'.format(IROHA_HOST_ADDR, IROHA_PORT))
        self.sawmills = sawmills

        self.woods = list(map(config.to_lower_case_only_letters,
                              config.woods))  # uses as assets
        self.commands = [
            self.iroha.command('CreateDomain',
                               domain_id=self.domain_name,
                               default_role='user'),
            *[
                self.iroha.command('CreateAsset',
                                   asset_name=wood,
                                   domain_id=self.domain_name,
                                   precision=0) for wood in self.woods
            ]
        ]
        tx = IrohaCrypto.sign_transaction(
            self.iroha.transaction(self.commands), self.admin_private_key)
        print(self.send_transaction_and_log_status(tx))
        self.get_admin_details()
        tx = self.iroha.transaction([
            self.iroha.command('AddAssetQuantity',
                               asset_id=f'{wood}#{self.domain_name}',
                               amount=str(10000 // l)) for wood in self.woods
        ])
        IrohaCrypto.sign_transaction(tx, self.admin_private_key)
        print(self.send_transaction_and_log_status(tx))
        self.get_admin_details()
Exemplo n.º 10
0
def get_block(blockNum):
    # create Query
    get_block_query = iroha.query('GetBlock', height=blockNum)
    # sign Query
    IrohaCrypto.sign_query(get_block_query, admin_priv_key)
    # send Query
    response = net.send_query(get_block_query)
    return response
Exemplo n.º 11
0
def add_asset_tx():
    tx = iroha.transaction([
        iroha.command(
            'AddAssetQuantity', asset_id='coin#test', amount='5000.99')
    ],
                           creator_account=alice['id'])
    IrohaCrypto.sign_transaction(tx, alice['key'])
    return tx
Exemplo n.º 12
0
 def __init__(self, account_name, domain, ledger):
     self.account_name = account_name
     self.domain = domain
     self.full_name = f"{self.account_name}@{self.domain}"
     self.__private_key = IrohaCrypto.private_key()
     self.public_key = IrohaCrypto.derive_public_key(self.__private_key)
     self.iroha = Iroha(self.full_name)
     self.ledger = ledger
Exemplo n.º 13
0
def generate_new_node_keys(node_id):
    private_key = ic.private_key()
    public_key = ic.derive_public_key(private_key)
    private_key_file = node_id + ".priv"
    public_key_file = node_id + ".pub"
    open(private_key_file, "wb+").write(private_key)
    open(public_key_file, "wb+").write(public_key)
    return public_key
 def grant_account_tx_history_permission(self, account_id):
     tx = self.iroha.transaction([
         self.iroha.command('GrantPermission',
                            account_id=account_id,
                            permission=can_get_my_acc_txs)
     ])
     ic.sign_transaction(tx, self.user_private_key)
     self.send_transaction_print_status_and_return_result(tx)
Exemplo n.º 15
0
def grant_account_read_permission(account_id):
    tx = iroha.transaction([
        iroha.command('GrantPermission',
                      account_id=account_id,
                      permission=can_get_my_acc_detail)
    ])
    ic.sign_transaction(tx, user_private_key)
    send_transaction_print_status_and_return_result(tx)
Exemplo n.º 16
0
def get_tx_info(txid):
    '''
    Gets transaction info from Iroha for a transaction ID hash.
    '''
    qry = iroha.query('GetTransactions', tx_hashes=[get_txid_bytes(txid)])
    IrohaCrypto.sign_query(qry, admin_private_key)
    status = net.send_query()
    return status
Exemplo n.º 17
0
def get_user_details(name, domain):

    query = iroha.query('GetAccountDetail', account_id=name+'@'+domain)
    IrohaCrypto.sign_query(query, ADMIN_PRIVATE_KEY)

    response = net.send_query(query)
    data = response.account_detail_response
    return jsonify('Account id = {}, details = {}'.format(name+'@'+domain, data.detail))
Exemplo n.º 18
0
def add_account(name, domain):

    tx = iroha.transaction([
        iroha.command('CreateAccount', account_name=name, domain_id=domain,
                      public_key=user_public_key)
    ])
    IrohaCrypto.sign_transaction(tx, ADMIN_PRIVATE_KEY)
    return send_transaction_and_print_status(tx)
def write_winner_to_iroha(winner):
    global iroha
    cmds = [
        iroha.command('SetAccountDetail', account_id=ADMIN_ACCOUNT_ID, key='winner', value=winner)
    ]
    tx = iroha.transaction(cmds)
    ic.sign_transaction(tx, ADMIN_PRIVATE_KEY)
    send_transaction_and_print_status(tx)
Exemplo n.º 20
0
def make_move(account_id, privateKey, new_state):
    iroha = Iroha(account_id)
    cmds = [
        iroha.command('SetAccountDetail', account_id=GAME_ACCOUNT_ID, key='state', value=new_state)
    ]
    tx = iroha.transaction(cmds, creator_account=GAME_ACCOUNT_ID, quorum=2)
    ic.sign_transaction(tx, privateKey)
    send_transaction_and_print_status(tx)
Exemplo n.º 21
0
def add_vote_to_admin(name, domain, amount):

    tx = iroha.transaction([
        iroha.command('AddAssetQuantity',
                      asset_id=name+'#'+domain, amount=amount)
    ])
    IrohaCrypto.sign_transaction(tx, ADMIN_PRIVATE_KEY)
    return send_transaction_and_print_status(tx)
Exemplo n.º 22
0
def set_account_detail(key, value):
    """
    Set account detail key value
    """
    tx = iroha.transaction(
        [iroha.command('SetAccountDetail', key=key, value=value)])
    ic.sign_transaction(tx, admin_private_key)
    send_transaction_and_print_status(tx)
Exemplo n.º 23
0
def detach_role_tx():
    tx = iroha.transaction([
        iroha.command(
            'DetachRole', account_id=admin['id'], role_name='test_role')
    ],
                           creator_account=alice['id'])
    IrohaCrypto.sign_transaction(tx, alice['key'])
    return tx
Exemplo n.º 24
0
 def AddAdminCoin(self):
     tx = self.iroha.transaction([
         self.iroha.command('AddAssetQuantity',
                            asset_id='coin#domain',
                            amount='1000000000.00')
     ])
     IrohaCrypto.sign_transaction(tx, self.admin_private_key)
     self.SendTxAndPrintstatus(tx)
def account_asset_transactions_query():
    query = iroha.query('GetAccountAssetTransactions',
                        account_id=admin['id'],
                        asset_id='coin#test',
                        creator_account=alice['id'],
                        page_size=10)
    IrohaCrypto.sign_query(query, alice['key'])
    return query
Exemplo n.º 26
0
def create_asset_tx():
    tx = iroha.transaction([
        iroha.command(
            'CreateAsset', asset_name='coin', domain_id='test', precision=2)
    ],
                           creator_account=alice['id'])
    IrohaCrypto.sign_transaction(tx, alice['key'])
    return tx
Exemplo n.º 27
0
def add_asset_qty(asset_id, qty):
    """
    Add asset supply
    """
    tx = iroha.transaction(
        [iroha.command('AddAssetQuantity', asset_id=asset_id, amount=qty)])
    ic.sign_transaction(tx, user_private_key)
    send_transaction_print_status_and_return_result(tx)
Exemplo n.º 28
0
def append_role_tx():
    # Note that you can append only that role that has
    # lesser or the same set of permissions as transaction creator.
    tx = iroha.transaction([
        iroha.command('AppendRole', account_id=bob['id'], role_name='second_role')
    ], creator_account=alice['id'])
    IrohaCrypto.sign_transaction(tx, alice['key'])
    return tx
def generate_new_node_keys(node_id):
    private_key = ic.private_key()
    public_key = ic.derive_public_key(private_key)
    private_key_file = node_id + '.priv'
    public_key_file = node_id + '.pub'
    open(private_key_file, 'wb+').write(private_key)
    open(public_key_file, 'wb+').write(public_key)
    return public_key
Exemplo n.º 30
0
def transactions_query():
    hashes = [
        binascii.hexlify(alice_tx1_hash),
        binascii.hexlify(alice_tx2_hash)
    ]
    query = iroha.query('GetTransactions', creator_account=alice['id'], tx_hashes=hashes)
    IrohaCrypto.sign_query(query, alice['key'])
    return query