Пример #1
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)
Пример #2
0
def get_blocks():
    """
    Subscribe to blocks stream from the network
    :return:
    """
    query = iroha.blocks_query()
    IrohaCrypto.sign_query(query, admin_private_key)
    for block in net.send_blocks_stream_query(query):
        print('The next block arrived:', block)
Пример #3
0
def get_userone_details():
    """
    Get all the kv-storage entries for userone@domain
    """
    query = iroha.query('GetAccountDetail', account_id='userone@domain')
    IrohaCrypto.sign_query(query, admin_private_key)

    response = net.send_query(query)
    data = response.account_detail_response
    print('Account id = {}, details = {}'.format('userone@domain',
                                                 data.detail))
Пример #4
0
def add_coin_to_admin():
    """
    Add 1000.00 units of 'coin#domain' to 'admin@test'
    """
    tx = iroha.transaction([
        iroha.command('AddAssetQuantity',
                      asset_id='coin#domain',
                      amount='1000.00')
    ])
    IrohaCrypto.sign_transaction(tx, admin_private_key)
    send_transaction_and_print_status(tx)
Пример #5
0
def create_account_userone():
    """
    Create account 'userone@domain'
    """
    tx = iroha.transaction([
        iroha.command('CreateAccount',
                      account_name='userone',
                      domain_id='domain',
                      public_key=user_public_key)
    ])
    IrohaCrypto.sign_transaction(tx, admin_private_key)
    send_transaction_and_print_status(tx)
Пример #6
0
def get_coin_info():
    """
    Get asset info for coin#domain
    :return:
    """
    query = iroha.query('GetAssetInfo', asset_id='coin#domain')
    IrohaCrypto.sign_query(query, admin_private_key)

    response = net.send_query(query)
    data = response.asset_response.asset
    print('Asset id = {}, precision = {}'.format(data.asset_id,
                                                 data.precision))
Пример #7
0
def get_account_assets():
    """
    List all the assets of userone@domain
    """
    query = iroha.query('GetAccountAssets', account_id='userone@domain')
    IrohaCrypto.sign_query(query, admin_private_key)

    response = net.send_query(query)
    data = response.account_assets_response.account_assets
    for asset in data:
        print('Asset id = {}, balance = {}'.format(asset.asset_id,
                                                   asset.balance))
Пример #8
0
def set_age_to_userone():
    """
    Set age to userone@domain by admin@test
    """
    tx = iroha.transaction([
        iroha.command('SetAccountDetail',
                      account_id='userone@domain',
                      key='age',
                      value='18')
    ])
    IrohaCrypto.sign_transaction(tx, admin_private_key)
    send_transaction_and_print_status(tx)
Пример #9
0
def userone_grants_to_admin_set_account_detail_permission():
    """
    Make admin@test able to set detail to userone@domain
    """
    tx = iroha.transaction([
        iroha.command('GrantPermission',
                      account_id='admin@test',
                      permission=can_set_my_account_detail)
    ],
                           creator_account='userone@domain')
    IrohaCrypto.sign_transaction(tx, user_private_key)
    send_transaction_and_print_status(tx)
Пример #10
0
def bob_accepts_exchange_request():
    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':
            del tx.signatures[:]  # we need do this temporarily, otherwise accept will not reach MST engine
        else:
            ic.sign_transaction(tx, *bob_private_keys)
    send_batch_and_print_status(*pending_transactions.transactions_response.transactions)
Пример #11
0
def transfer_coin_from_admin_to_userone():
    """
    Transfer 2.00 'coin#domain' from 'admin@test' to 'userone@domain'
    """
    tx = iroha.transaction([
        iroha.command('TransferAsset',
                      src_account_id='admin@test',
                      dest_account_id='userone@domain',
                      asset_id='coin#domain',
                      description='init top up',
                      amount='2.00')
    ])
    IrohaCrypto.sign_transaction(tx, admin_private_key)
    send_transaction_and_print_status(tx)
Пример #12
0
        def send_tx(self):
            iroha = Iroha('admin@test')
            admin_private_key = 'f101537e319568c765b2cc89698325604991dca57b9716b58016b253506cab70'

            tx = iroha.transaction([
                iroha.command('TransferAsset',
                              src_account_id='admin@test',
                              dest_account_id='test@test',
                              asset_id='coin#test',
                              amount='0.01',
                              description=HOSTNAME)
            ])
            ic.sign_transaction(tx, admin_private_key)

            self.client.send_tx(tx)
Пример #13
0
def send_transaction_and_print_status(transaction):
    hex_hash = binascii.hexlify(IrohaCrypto.hash(transaction))
    print('Transaction hash = {}, creator = {}'.format(
        hex_hash, transaction.payload.reduced_payload.creator_account_id))
    net.send_tx(transaction)
    for status in net.tx_status_stream(transaction):
        print(status)
Пример #14
0
def check_no_pending_txs():
    print(' ~~~ No pending txs expected:')
    print(
        net.send_query(
            ic.sign_query(
                iroha.query('GetPendingTransactions',
                            creator_account='bob@test'), bob_private_keys[0])))
    print(' ~~~')
Пример #15
0
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':
            del tx.signatures[:]  # we need do this temporarily, otherwise accept will not reach MST engine
        else:
            ic.sign_transaction(tx, *alice_private_keys)  # intentionally alice keys were used to fail bob's txs
            # zeroes as private keys are also acceptable
    send_batch_and_print_status(*pending_transactions.transactions_response.transactions)
Пример #16
0
def send_batch_and_print_status(*transactions):
    global net
    net.send_txs(*transactions)
    for tx in transactions:
        hex_hash = binascii.hexlify(ic.hash(tx))
        print('\t' + '-' * 20)
        print('Transaction hash = {}, creator = {}'.format(
            hex_hash, tx.payload.reduced_payload.creator_account_id))
        for status in net.tx_status_stream(tx):
            print(status)
Пример #17
0
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=ic.hex_key_to_bytes(alice_public_keys[0])),
        iroha.command('CreateAccount', account_name='bob', domain_id='test',
                      public_key=ic.hex_key_to_bytes(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)
Пример #18
0
def create_domain_and_asset():
    """
    Creates domain 'domain' and asset 'coin#domain' with precision 2
    """
    commands = [
        iroha.command('CreateDomain', domain_id='domain', default_role='user'),
        iroha.command('CreateAsset',
                      asset_name='coin',
                      domain_id='domain',
                      precision=2)
    ]
    tx = IrohaCrypto.sign_transaction(iroha.transaction(commands),
                                      admin_private_key)
    send_transaction_and_print_status(tx)
Пример #19
0
def add_keys_and_set_quorum():
    alice_iroha = Iroha('alice@test')
    alice_cmds = [
        alice_iroha.command('AddSignatory',
                            account_id='alice@test',
                            public_key=alice_public_keys[1]),
        alice_iroha.command('SetAccountQuorum',
                            account_id='alice@test',
                            quorum=2)
    ]
    alice_tx = alice_iroha.transaction(alice_cmds)
    ic.sign_transaction(alice_tx, alice_private_keys[0])
    send_transaction_and_print_status(alice_tx)

    bob_iroha = Iroha('bob@test')
    bob_cmds = [
        bob_iroha.command('AddSignatory',
                          account_id='bob@test',
                          public_key=bob_public_keys[1]),
        bob_iroha.command('SetAccountQuorum', account_id='bob@test', quorum=2)
    ]
    bob_tx = bob_iroha.transaction(bob_cmds)
    ic.sign_transaction(bob_tx, bob_private_keys[0])
    send_transaction_and_print_status(bob_tx)
Пример #20
0
# Copyright Soramitsu Co., Ltd. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
#

import sys

if sys.version_info[0] < 3:
    raise Exception('Python 3 or a more recent version is required.')

from primitive_pb2 import can_set_my_account_detail
from irohalib import Iroha, IrohaGrpc
from irohalib import IrohaCrypto
import binascii

admin_private_key = open('../[email protected]').read()
user_private_key = IrohaCrypto.private_key()
user_public_key = IrohaCrypto.derive_public_key(user_private_key)
iroha = Iroha('admin@test')
net = IrohaGrpc()


def trace(func):
    """
    A decorator for tracing methods' begin/end execution points
    """
    def tracer(*args, **kwargs):
        name = func.__name__
        print('\tEntering "{}"'.format(name))
        result = func(*args, **kwargs)
        print('\tLeaving "{}"'.format(name))
        return result
Пример #21
0
from irohalib import Iroha, IrohaGrpc
from irohalib import IrohaCrypto as ic

import binascii

iroha = Iroha('admin@test')
net = IrohaGrpc()

admin_private_key = open('../[email protected]').read()

alice_private_keys = [
    b'f101537e319568c765b2cc89698325604991dca57b9716b58016b253506caba1',
    b'f101537e319568c765b2cc89698325604991dca57b9716b58016b253506caba2'
]
alice_public_keys = [ic.derive_public_key(x) for x in alice_private_keys]

bob_private_keys = [
    b'f101537e319568c765b2cc89698325604991dca57b9716b58016b253506caba3',
    b'f101537e319568c765b2cc89698325604991dca57b9716b58016b253506caba4'
]
bob_public_keys = [ic.derive_public_key(x) for x in bob_private_keys]


def trace(func):
    """
    A decorator for tracing methods' begin/end execution points
    """
    def tracer(*args, **kwargs):
        name = func.__name__
        print('\tEntering "{}"'.format(name))