예제 #1
0
    def buy_ram(account_object,
                amount_kbytes,
                receiver=None,
                expiration_sec=30,
                skip_signature=0,
                dont_broadcast=0,
                forceUnique=0,
                max_cpu_usage=0,
                max_net_usage=0,
                ref_block=None):

        if manager.is_local_testnet():
            return

        if receiver is None:
            receiver = account_object

        buy_ram_kbytes = 1

        result = cleosys.BuyRam(account_object,
                                receiver,
                                amount_kbytes,
                                buy_ram_kbytes,
                                expiration_sec,
                                skip_signature,
                                dont_broadcast,
                                forceUnique,
                                max_cpu_usage,
                                max_net_usage,
                                ref_block,
                                is_verbose=0)

        logger.INFO('''
            * Transfered RAM from {} to {} kbytes: {}
            '''.format(result.payer, result.receiver, result.amount))
예제 #2
0
    def is_local(self):
        '''Check whether EOSFactory is connected to the local testnet.

        Returns: 
            bool: Whether EOSFactory is connected to the local testnet.
        '''        
        return manager.is_local_testnet()
예제 #3
0
    def delegate_bw(
            self, stake_net_quantity, stake_cpu_quantity,
            receiver=None,
            permission=None,
            transfer=False,
            expiration_sec=None, 
            skip_sign=0, dont_broadcast=0, force_unique=0,
            max_cpu_usage=0, max_net_usage=0,
            ref_block=None,
            is_verbose=1):

        stop_if_account_is_not_set(self)
        if manager.is_local_testnet():
            return

        if receiver is None:
            receiver = self

        result = cleos_sys.DelegateBw(
            self, receiver,
            stake_net_quantity, stake_cpu_quantity,
            permission,
            transfer,
            expiration_sec,
            skip_sign, dont_broadcast, force_unique,
            max_cpu_usage, max_net_usage,
            ref_block,
            is_verbose=0
            )

        logger.INFO('''
        * Delegated stake from {} to {} NET: {} CPU: {}
        '''.format(
            result.payer, result.receiver,
            result.stake_net_quantity, result.stake_cpu_quantity))
예제 #4
0
    def buy_ram(
            self, amount_kbytes, receiver=None,
            expiration_sec=None, 
            skip_sign=0, dont_broadcast=0, force_unique=0,
            max_cpu_usage=0, max_net_usage=0,
            ref_block=None):

        stop_if_account_is_not_set(self)
        if manager.is_local_testnet():
            return

        if receiver is None:
            receiver = self

        buy_ram_kbytes = 1
        
        result = cleos_sys.BuyRam(
            self, receiver, amount_kbytes,
            buy_ram_kbytes,
            expiration_sec,
            skip_sign, dont_broadcast, force_unique,
            max_cpu_usage, max_net_usage,
            ref_block,
            is_verbose=0
            )

        logger.INFO('''
            * Transfered RAM from {} to {} kbytes: {}
            '''.format(result.payer, result.receiver, result.amount))
예제 #5
0
    def __init__(self, name=None, password="", verbosity=None, file=False):

        cleos.set_local_nodeos_address_if_none()
        if name is None:
            name = setup.wallet_default_name
        else:
            name = setup.file_prefix() + name

        if not self.wallet is None:
            raise errors.Error('''
            It can be only one ``Wallet`` object in the script; there is one
            named ``{}``.
            '''.format(wallet.name))
            return

        self.wallet_dir = manager.wallet_dir()

        logger.INFO('''
                * Wallet name is ``{}``, wallet directory is
                    {}.
                '''.format(name, self.wallet_dir))

        if not password:  # look for password:
            passwords = wallet_json_read()
            if name in passwords:
                password = passwords[name]
                logger.INFO(
                    '''
                    The password is restored from the file:
                    {}
                    '''.format(
                        os.path.join(self.wallet_dir, setup.password_map)),
                    verbosity)

        cleos.WalletCreate.__init__(self, name, password, is_verbose=False)

        if self.is_created:  # new password
            logger.INFO(
                '''
                * Created wallet ``{}``.
                '''.format(self.name), verbosity)
            ###############################################################################
            # TO DO: detect live node!!!!!!!!!!
            if manager.is_local_testnet() or file or True:
                ###############################################################################
                password_map = wallet_json_read()
                password_map[name] = self.password
                wallet_json_write(password_map)
                logger.INFO(
                    '''
                    * Password is saved to the file ``{}`` in the wallet directory.
                    '''.format(setup.password_map), verbosity)
            else:
                logger.OUT(self.out_msg)
        else:
            logger.TRACE('''
                    Opened wallet ``{}``
                    '''.format(self.name))
예제 #6
0
    def delegate_bw(account_object,
                    stake_net_quantity,
                    stake_cpu_quantity,
                    receiver=None,
                    permission=None,
                    transfer=False,
                    expiration_sec=30,
                    skip_signature=0,
                    dont_broadcast=0,
                    forceUnique=0,
                    max_cpu_usage=0,
                    max_net_usage=0,
                    ref_block=None,
                    is_verbose=1):

        if manager.is_local_testnet():
            return

        if receiver is None:
            receiver = account_object

        result = cleosys.DelegateBw(account_object,
                                    receiver,
                                    stake_net_quantity,
                                    stake_cpu_quantity,
                                    permission,
                                    transfer,
                                    expiration_sec,
                                    skip_signature,
                                    dont_broadcast,
                                    forceUnique,
                                    max_cpu_usage,
                                    max_net_usage,
                                    ref_block,
                                    is_verbose=0)

        logger.INFO('''
        * Delegated stake from {} to {} NET: {} CPU: {}
        '''.format(result.payer, result.receiver, result.stake_net_quantity,
                   result.stake_cpu_quantity))
예제 #7
0
def create_account(account_object_name,
                   creator,
                   account_name="",
                   owner_key="",
                   active_key="",
                   stake_net=3,
                   stake_cpu=3,
                   permission=None,
                   expiration_sec=None,
                   skip_sign=0,
                   dont_broadcast=0,
                   force_unique=0,
                   max_cpu_usage=0,
                   max_net_usage=0,
                   ref_block=None,
                   delay_sec=0,
                   buy_ram_kbytes=8,
                   buy_ram="",
                   transfer=False,
                   restore=False):
    '''Create account object in caller's global namespace.
    
    Args:
        account_object_name (str): The name of the account object returned.
        creator (str or .core.interface.Account): The account creating the new 
            account
        account_name (str): The name of an valid EOSIO account. If not set, it 
            is random.
        owner_key (.core.interface.Key): The *owner* key pair. If not set, it
            is random.
        active_key (.core.interface.Key): The *active* key pair. If not set, 
            and the *owner_key* is set, it is substituted with the *owner_key*.
            Otherwise, it is random.
        stake_net (int): The amount of EOS delegated for net bandwidth.
        stake_cpu (int): The amount of EOS delegated for CPU bandwidth.
        buy_ram_kbytes (int): The amount of RAM bytes to purchase.
        transfer (bool): Transfer voting power and right to unstake EOS to 
            receiver.

    See definitions of the remaining parameters: \
    :func:`.cleos.common_parameters`.
    '''
    globals = inspect.stack()[1][0].f_globals
    '''
    Check the conditions:
    * a *Wallet* object is defined;
    * the account object name is not in use, already.
    '''
    if not is_wallet_defined(logger):
        return None

    if is_in_globals(account_object_name, globals):
        logger.INFO('''
            ######## Account object ``{}`` restored from the blockchain.
            '''.format(account_object_name))
        return globals[account_object_name]

    logger.INFO('''
        ######### Create an account object ``{}``.
        '''.format(account_object_name))
    '''
    Create an account object.
    '''
    account_object = None

    if restore:
        if creator:
            account_name = creator
        logger.INFO('''
                    ... for an existing blockchain account ``{}`` 
                        mapped as ``{}``.
                    '''.format(account_name, account_object_name),
                    translate=False)
        account_object = account.RestoreAccount(account_name)
        account_object.account_object_name = account_object_name
    else:
        if not account_name:
            account_name = cleos.account_name()
        if owner_key:
            if not active_key:
                active_key = owner_key
        else:
            owner_key = cleos.CreateKey(is_verbose=False)
            active_key = cleos.CreateKey(is_verbose=False)

        if stake_net and not manager.is_local_testnet():
            logger.INFO('''
                        ... delegating stake to a new blockchain account ``{}`` mapped as ``{}``.
                        '''.format(account_name, account_object_name))

            try:
                account_object = account.SystemNewaccount(
                    creator, account_name, owner_key, active_key, stake_net,
                    stake_cpu, permission, buy_ram_kbytes, buy_ram, transfer,
                    expiration_sec, skip_sign, dont_broadcast, force_unique,
                    max_cpu_usage, max_net_usage, ref_block, delay_sec)
            except errors.LowRamError as e:
                logger.TRACE('''
                * RAM needed is {}.kByte, buying RAM {}.kByte.
                '''.format(e.needs_kbyte, e.deficiency_kbyte))

                buy_ram_kbytes = str(e.deficiency_kbyte + 1)
                account_object = account.SystemNewaccount(
                    creator, account_name, owner_key, active_key, stake_net,
                    stake_cpu, permission, buy_ram_kbytes, buy_ram, transfer,
                    expiration_sec, skip_sign, dont_broadcast, force_unique,
                    max_cpu_usage, max_net_usage, ref_block, delay_sec)
        else:
            logger.INFO('''
                        ... for a new blockchain account ``{}``.
                        '''.format(account_name))
            account_object = account.CreateAccount(
                creator, account_name, owner_key, active_key, permission,
                expiration_sec, skip_sign, dont_broadcast, force_unique,
                max_cpu_usage, max_net_usage, ref_block, delay_sec)

        account_object.account_object_name = account_object_name
        account_object.owner_key = owner_key
        account_object.active_key = active_key

    logger.TRACE('''
        * The account object is created.
        ''')
    Account.add_methods_and_finalize(account_object_name, account_object)
    return account_object
예제 #8
0
def create_master_account(account_object_name,
                          account_name=None,
                          owner_key=None,
                          active_key=None):
    '''Create master account object in caller's global namespace.

    Start a singleton :class:`.shell.wallet.Wallet` object if there is no one 
    in the global namespace already.

    If a local testnet is running, create an account object representing 
    the *eosio* account. Put the account into the wallet. Put the account
    object into the global namespace of the caller, and return.

    Otherwise, an outer testnet has to be defined with the function
    :func:`.core.setup.set_nodeos_address`.

    If the *account_name* argument is set, check the testnet for presence of the 
    account. If present, create a corresponding object and put the account 
    into the wallet, and put the account object into the global namespace of 
    the caller, and return. 
    
    Otherwise start a registration procedure:    
    
        - if the argument *account_name* is not set, make it random
        - print registration data, namely:
            - account name
            - owner public key
            - active public key
            - owner private key
            - active private key
        - wait for the user to register the master account
        - . . . . 
        - detect the named account on the remote testnet
        - put the account into the wallet
        - put the account object into the global namespace of the caller and \
            return

    Note: Name conflict:
        If a new account object, named as an existing one, is going to be added to 
        the wallet, an error is reported. Then an offer is given to edith the 
        mapping file in order to resolve the conflict. When the conflict is 
        resolved, the procedure finishes successfully.   

    Args:
        account_object_name (str): The name of the account object returned.
        account_name (str or .core.testnet.Testnet): The name of an valid EOSIO
            account. Must be set if the testnode is not local.
        owner_key (str or .core.interface.Key): The owner public key. Must 
            be set if the testnode is not local.
        active_key (str or .core.interface.Key): The active public key. Must 
            be set if the testnode is not local.
    '''

    globals = inspect.stack()[1][0].f_globals

    if isinstance(account_name, testnet.Testnet):
        owner_key = account_name.owner_key
        active_key = account_name.active_key
        account_name = account_name.account_name

    if account_object_name:  # account_object_name==None in register_testnet
        '''
        Check the conditions:
        * a *Wallet* object is defined.
        * the account object name is not in use, already.    
        '''
        if not is_wallet_defined(logger, globals):
            return None

        if is_in_globals(account_object_name, globals):
            logger.INFO('''
                ######## Account object ``{}`` restored from the blockchain.
                '''.format(account_object_name))
            return globals[account_object_name]

    logger.INFO('''
        ######### Create a master account object ``{}``.
        '''.format(account_object_name))
    '''
    If the local testnet is running, create an account object representing the 
    *eosio* account. Put the account into the wallet. Put the account object into 
    the global namespace of the caller, and **return**.
    '''
    if setup.is_local_address:
        account_object = account.Eosio(account_object_name)
        put_account_to_wallet_and_on_stack(account_object_name, account_object,
                                           logger)
        return account_object
    '''
    Otherwise, an outer testnet has to be defined with 
    *setup.set_nodeos_address(<url>)*.
    '''

    if manager.is_local_testnet():
        if teos.is_local_node_process_running():
            raise errors.Error('''
    There is an local testnode process running, but its 'eosio` account is not like 
    expected.
            ''')
            sys.exit()

            raise errors.Error('''
    If the local testnet is not running, an outer testnet has to be defined with 
    `setup.set_nodeos_address(<url>)`: use 'setup.set_nodeos_address(<URL>)'
        ''')
    '''
    If the *account_name* argument is not set, it is randomized. Check the 
    testnet for presence of the account. If present, create the corresponding 
    object and see whether it is in the wallets. If so, put the account object into 
    the global namespace of the caller. and **return**. 
    '''
    first_while = True
    while True:
        account_object = account.GetAccount(account_object_name, account_name,
                                            owner_key, active_key)

        if first_while and account_name and owner_key and active_key \
                        and not account_object.exists:
            raise errors.Error('''
            There is no account named ``{}`` in the blockchain.
            '''.format(account_name))

        first_while = False

        if account_object.exists:
            if account_object.has_keys:  # it is your account
                logger.TRACE('''
                    * Checking whether the wallet has keys to the account ``{}``
                    '''.format(account_object.name))

                logger.TRACE('''
                    * The account object is created.
                    ''')

                if account_object_name:
                    if Account.add_methods_and_finalize(
                            account_object_name, account_object):
                        logger.TRACE('''
                            * The account ``{}`` is in the wallet.
                            '''.format(account_object.name))
                        return account_object
                else:
                    return account_object

            else:  # the name is taken by somebody else
                logger.TRACE('''
                ###
                You can try another name. Do you wish to do this?
                ''')
                decision = input("y/n <<< ")
                if decision == "y":
                    account_name = input(
                        "enter the account name or nothing to make the name random <<< "
                    )
                else:
                    return None
        else:
            owner_key_new = cleos.CreateKey(is_verbose=False)
            active_key_new = cleos.CreateKey(is_verbose=False)

            logger.OUT('''
            Use the following data to register a new account on a public testnet:
            Account Name: {}
            Owner Public Key: {}
            Active Public Key: {}

            Owner Private Key: {}
            Active Private Key: {}
            '''.format(account_object.name, owner_key_new.key_public,
                       active_key_new.key_public, owner_key_new.key_private,
                       active_key_new.key_private))

            while True:
                is_ready = input("enter 'go' when ready or 'q' to quit <<< ")
                if is_ready == "q":
                    return None
                else:
                    if is_ready == "go":
                        break

            account_name = account_object.name
            owner_key = owner_key_new
            active_key = active_key_new
예제 #9
0
def create_account(account_object_name,
                   creator,
                   account_name="",
                   owner_key="",
                   active_key="",
                   stake_net=3,
                   stake_cpu=3,
                   permission=None,
                   buy_ram_kbytes=8,
                   buy_ram="",
                   transfer=False,
                   expiration_sec=30,
                   skip_signature=0,
                   dont_broadcast=0,
                   forceUnique=0,
                   max_cpu_usage=0,
                   max_net_usage=0,
                   ref_block=None,
                   restore=False,
                   verbosity=None):

    global wallet_singleton
    globals = inspect.stack()[1][0].f_globals
    '''
    Check the conditions:
    * a ``Wallet`` object is defined;
    * the account object name is not in use, already.
    '''
    is_wallet_defined(logger)
    if wallet_singleton is None:
        return
    #wallet_singleton.is_name_taken(account_object_name, account_name)

    if account_object_name in globals:

        if not isinstance(globals[account_object_name], interface.Account):
            raise errors.Error('''
            The global variable ``{}`` type is not ``Account``.
            '''.format(account_object_name))
            return

        logger.INFO('''
            ######## Account object ``{}`` restored from the blockchain.
            '''.format(account_object_name))
        return

    logger.INFO('''
        ######### Create an account object ``{}``.
        '''.format(account_object_name))
    '''
    Create an account object.
    '''
    account_object = None

    if restore:
        if creator:
            account_name = creator
        logger.INFO('''
                    ... for an existing blockchain account ``{}`` mapped as ``{}``.
                    '''.format(account_name, account_object_name),
                    translate=False)
        account_object = RestoreAccount(account_name, verbosity)
        account_object.account_object_name = account_object_name
    else:
        if not account_name:
            account_name = cleos.account_name()
        if owner_key:
            if not active_key:
                active_key = owner_key
        else:
            owner_key = cleos.CreateKey("owner", is_verbose=False)
            active_key = cleos.CreateKey("active", is_verbose=False)

        if stake_net and not manager.is_local_testnet():
            logger.INFO('''
                        ... delegating stake to a new blockchain account ``{}`` mapped as ``{}``.
                        '''.format(account_name, account_object_name))

            try:
                account_object = SystemNewaccount(
                    creator, account_name, owner_key, active_key, stake_net,
                    stake_cpu, permission, buy_ram_kbytes, buy_ram, transfer,
                    expiration_sec, skip_signature, dont_broadcast,
                    forceUnique, max_cpu_usage, max_net_usage, ref_block,
                    verbosity)
            except errors.LowRamError as e:
                logger.TRACE('''
                * RAM needed is {}.kByte, buying RAM {}.kByte.
                '''.format(e.needs_kbyte, e.deficiency_kbyte))

                buy_ram_kbytes = str(e.deficiency_kbyte + 1)
                account_object = SystemNewaccount(
                    creator, account_name, owner_key, active_key, stake_net,
                    stake_cpu, permission, buy_ram_kbytes, buy_ram, transfer,
                    expiration_sec, skip_signature, dont_broadcast,
                    forceUnique, max_cpu_usage, max_net_usage, ref_block,
                    verbosity)
        else:
            logger.INFO('''
                        ... for a new blockchain account ``{}``.
                        '''.format(account_name))
            account_object = CreateAccount(creator, account_name, owner_key,
                                           active_key, permission,
                                           expiration_sec, skip_signature,
                                           dont_broadcast, forceUnique,
                                           max_cpu_usage, max_net_usage,
                                           ref_block, verbosity)

        account_object.account_object_name = account_object_name
        account_object.owner_key = owner_key
        account_object.active_key = active_key

    logger.TRACE('''
        * The account object is created.
        ''')
    append_account_methods_and_finish(account_object_name, account_object)
예제 #10
0
def create_master_account(account_object_name,
                          account_name=None,
                          owner_key=None,
                          active_key=None,
                          verbosity=None):
    '''Create account object in caller's global namespace.

    - **parameters**::

        account_object_name:: the name of the account object
        account_name: the name of the account; random, if not set
        verbosity: argument to the internal logger

    ### Preconditions

    Check the following conditions:
    * precisely one ``Wallet`` object is defined;
    
    ### Local testnet

    If the local testnet is running, create an account object representing 
    the ``eosio`` account. Put the account into the wallet. Put the account
    object into the global namespace of the caller, and **return**.

    ### Remote testnet

    Otherwise, an outer testnet has to be defined with 
    ``setup.set_nodeos_address(<url>)``.

    ### Existing account

    If the ``account_name`` argument is set, check the testnet for presence of the 
    account. If present, create the corresponding object and put the account 
    into the wallet, and put the account object into the global namespace of 
    the caller. and **return**. Otherwise start a  registration procedure, 
    described in the next paragraph.

    ### Registration to a remote testnet

    If the ``account_name`` argument is not set or it does not address any existing
    account, see the previous paragraph, start a registration procedure.

    * if the ``account_name`` argument is not set, make it random
    * print registration data, namely:
        * account name
        * owner public key
        * active public key
        * owner private key
        * active private key
    * wait for the user to register the master account
    * . . . . 
    * detect the named account on the remote testnet
    * put the account into the wallet
    * put the account object into the global namespace of the caller
    
    ### Name conflict between account objects

    If the new account object is going to be added to the wallet, an error
    is reported. Then an offer is given to edith the mapping file in order
    to resolve the conflict. When the conflict is resolved, the procedure
    finishes successfully.
    '''

    globals = inspect.stack()[1][0].f_globals

    if account_object_name:  # account_object_name==None in register_testnet
        '''
        Check the conditions:
        * a ``Wallet`` object is defined.
        * the account object name is not in use, already.    
        '''
        is_wallet_defined(logger, globals)

        global wallet_singleton
        if wallet_singleton is None:
            return

        if account_object_name in globals:

            if not isinstance(globals[account_object_name], interface.Account):
                raise errors.Error('''
                The global variable {} type is not ``Account``.
                '''.format(account_object_name))
                return

            logger.INFO('''
                ######## Account object ``{}`` restored from the blockchain.
                '''.format(account_object_name))
            return

            #wallet_singleton.is_name_taken(account_object_name, account_name)

    if isinstance(account_name, testnet.Testnet):
        owner_key = account_name.owner_key
        active_key = account_name.active_key
        account_name = account_name.account_name

    logger.INFO('''
        ######### Create a master account object ``{}``.
        '''.format(account_object_name))
    '''
    If the local testnet is running, create an account object representing the 
    ``eosio`` account. Put the account into the wallet. Put the account object into 
    the global namespace of the caller, and **return**.
    '''
    account_object = Eosio(account_object_name)

    if is_local_testnet_running(account_object):
        put_account_to_wallet_and_on_stack(account_object_name, account_object,
                                           logger)
        return
    '''
    Otherwise, an outer testnet has to be defined with 
    ``setup.set_nodeos_address(<url>)``.
    '''

    if manager.is_local_testnet():
        if teos.is_local_node_process_running():
            raise errors.Error('''
    There is an local testnode process running, but its 'eosio` account is not like 
    expected.
            ''')

        raise errors.Error('''
    If the local testnet is not running, an outer testnet has to be defined with 
    `setup.set_nodeos_address(<url>)`: use 'setup.set_nodeos_address(<URL>)'
        ''')
    '''
    If the ``account_name`` argument is not set, it is randomized. Check the 
    testnet for presence of the account. If present, create the corresponding 
    object and see whether it is in the wallets. If so, put the account object into 
    the global namespace of the caller. and **return**. 
    '''
    first_while = True
    while True:
        account_object = GetAccount(account_object_name, account_name,
                                    owner_key, active_key, verbosity)

        if first_while and account_name and owner_key and active_key \
                        and not account_object.exists:
            raise errors.Error('''
            There is no account named ``{}`` in the blockchain.
            '''.format(account_name))
            return
        first_while = False

        if account_object.exists:
            if account_object.has_keys:  # it is your account
                logger.TRACE('''
                    * Checking whether the wallet has keys to the account ``{}``
                    '''.format(account_object.name))

                logger.TRACE('''
                    * The account object is created.
                    ''')

                if account_object_name:
                    if append_account_methods_and_finish(
                            account_object_name, account_object):
                        logger.TRACE('''
                            * The account ``{}`` is in the wallet.
                            '''.format(account_object.name))
                        return
                else:
                    return account_object

            else:  # the name is taken by somebody else
                logger.TRACE('''
                ###
                You can try another name. Do you wish to do this?
                ''')
                decision = input("y/n <<< ")
                if decision == "y":
                    account_name = input(
                        "enter the account name or nothing to make the name random <<< "
                    )
                else:
                    return
        else:
            owner_key_new = cleos.CreateKey("owner", is_verbose=False)
            active_key_new = cleos.CreateKey("active", is_verbose=False)

            logger.OUT('''
            Use the following data to register a new account on a public testnet:
            Account Name: {}
            Owner Public Key: {}
            Active Public Key: {}

            Owner Private Key: {}
            Active Private Key: {}
            '''.format(account_object.name, owner_key_new.key_public,
                       active_key_new.key_public, owner_key_new.key_private,
                       active_key_new.key_private))

            while True:
                is_ready = input("enter 'go' when ready or 'q' to quit <<< ")
                if is_ready == "q":
                    return
                else:
                    if is_ready == "go":
                        break
            account_name = account_object.name
            owner_key = owner_key_new
            active_key = active_key_new
예제 #11
0
 def is_local(self):
     return manager.is_local_testnet()