Esempio n. 1
0
    def __init__(self,
                 account_object_name,
                 name=None,
                 owner_key=None,
                 active_key=None,
                 verbosity=None):

        self.account_object_name = account_object_name
        if name is None:
            self.name = cleos.account_name()
        else:
            self.name = name

        if active_key is None:
            active_key = owner_key

        self.exists = False
        self.in_wallet_on_stack = False
        self.has_keys = not owner_key is None

        try:
            cleos.GetAccount.__init__(self,
                                      self.name,
                                      is_info=False,
                                      is_verbose=False)
        except errors.AccountDoesNotExistError:
            return

        self.exists = True
        if owner_key is None:
            self.owner_key = cleos.CreateKey(
                self.json["permissions"][1]["required_auth"]["keys"] \
                [0]["key"],
                is_verbose=0)
        else:  # an orphan account, private key is restored from cache
            self.owner_key = cleos.CreateKey(
                self.json["permissions"][1]["required_auth"]["keys"] \
                [0]["key"], interface.key_arg(
                    owner_key, is_owner_key=True, is_private_key=True),
                is_verbose=0)

        if active_key is None:
            self.owner_key = cleos.CreateKey(
                self.json["permissions"][0]["required_auth"]["keys"] \
                [0]["key"],
                is_verbose=0)
        else:  # an orphan account, private key is restored from cache
            self.active_key = cleos.CreateKey(
                self.json["permissions"][0]["required_auth"]["keys"] \
                [0]["key"], interface.key_arg(
                    active_key, is_owner_key=False, is_private_key=True),
                is_verbose=0)

        logger.TRACE('''
            * Account *{}* exists in the blockchain.
            '''.format(self.name))
Esempio n. 2
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)
Esempio n. 3
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
Esempio n. 4
0
    def __init__(self,
                 creator,
                 name,
                 owner_key,
                 active_key,
                 stake_net,
                 stake_cpu,
                 permission=None,
                 buy_ram_kbytes=0,
                 buy_ram="",
                 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,
                 delay_sec=0,
                 is_verbose=1):

        stake_net = "{} EOS".format(stake_net)
        stake_cpu = "{} EOS".format(stake_cpu)

        if name is None:
            name = cleos.account_name()
        interface.Account.__init__(self, name)

        self.owner_key = None  # private keys
        self.active_key = None

        if active_key is None:
            active_key = owner_key

        args = [
            interface.account_arg(creator), self.name,
            interface.key_arg(owner_key,
                              is_owner_key=True,
                              is_private_key=False),
            interface.key_arg(active_key,
                              is_owner_key=False,
                              is_private_key=False)
        ]

        args.append("--json")
        args.extend(["--stake-net", stake_net, "--stake-cpu", stake_cpu])
        if buy_ram_kbytes:
            args.extend(["--buy-ram-kbytes", str(buy_ram_kbytes)])
        if buy_ram:
            args.extend(["--buy-ram", str(buy_ram)])
        if transfer:
            args.extend(["--transfer"])
        if not permission is None:
            p = interface.permission_arg(permission)
            for perm in p:
                args.extend(["--permission", perm])
        if expiration_sec:
            args.extend(["--expiration", str(expiration_sec)])
        if skip_sign:
            args.append("--skip-sign")
        if dont_broadcast:
            args.append("--dont-broadcast")
        if force_unique:
            args.append("--force-unique")
        if max_cpu_usage:
            args.extend(["--max-cpu-usage-ms", str(max_cpu_usage)])
        if max_net_usage:
            args.extend(["--max-net-usage", str(max_net_usage)])
        if not ref_block is None:
            args.extend(["--ref-block", ref_block])
        if delay_sec:
            args.extend(["--delay-sec", delay_sec])

        cleos.Cleos.__init__(self, args, "system", "newaccount", is_verbose)

        self.json = cleos.GetAccount(self.name, is_verbose=0,
                                     is_info=False).json

        if self.is_verbose:
            print(self.__str__())