Exemplo n.º 1
0
def create_keyfile(private_key, password, filename):
    """Creates a wallet keyfile.
    See https://github.com/ethereum/go-ethereum/wiki/Passphrase-protected-key-store-spec

    :param str private_key: private key

    :param str password: keyfile password

    :param str filename: keyfile path

    :raises: NotImplementedError: when using Python 3
    """
    if sys.version_info.major >= 3:
        raise NotImplementedError('keyfile creation is only supported in python2')
    from ethereum.tools import keys
    keyfile_json = keys.make_keystore_json(private_key.encode(), password, kdf='scrypt')
    try:
        oldumask = os.umask(0)
        fd = os.open(filename, os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o600)
        with os.fdopen(fd, "w") as f:
            json.dump(keyfile_json, f)
    except IOError as e:
        logger.exception(e)
    finally:
        os.umask(oldumask)
Exemplo n.º 2
0
def create_keyfile(private_key, password, filename):
    """Creates a wallet keyfile.
    See https://github.com/ethereum/go-ethereum/wiki/Passphrase-protected-key-store-spec

    :param str private_key: private key

    :param str password: keyfile password

    :param str filename: keyfile path

    :raises: NotImplementedError: when using Python 3
    """
    if sys.version_info.major >= 3:
        raise NotImplementedError('keyfile creation is only supported in python2')
    from ethereum.tools import keys
    keyfile_json = keys.make_keystore_json(private_key.encode(), password, kdf='scrypt')
    try:
        oldumask = os.umask(0)
        fd = os.open(filename, os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o600)
        with os.fdopen(fd, "w") as f:
            json.dump(keyfile_json, f)
    except IOError as e:
        logger.exception(e)
    finally:
        os.umask(oldumask)
Exemplo n.º 3
0
def make_keystore_json_patched(private_key, password):
    # Fix py3 bytes/string incompatibility in `make_keystore_json()`
    # See: https://github.com/ethereum/pyethereum/issues/758
    _encode_hex = keys.encode_hex
    setattr(keys, 'encode_hex', lambda *args: _encode_hex(*args).encode('ASCII'))
    try:
        return make_keystore_json(private_key, password)
    finally:
        setattr(keys, 'encode_hex', _encode_hex)
def make_keystore_json_patched(private_key, password):
    # Fix py3 bytes/string incompatibility in `make_keystore_json()`
    # See: https://github.com/ethereum/pyethereum/issues/758
    _encode_hex = keys.encode_hex
    setattr(keys, 'encode_hex', lambda *args: _encode_hex(*args).encode('ASCII'))
    try:
        return make_keystore_json(private_key, password)
    finally:
        setattr(keys, 'encode_hex', _encode_hex)
Exemplo n.º 5
0
def mock_create_keyfile(password, file_name=None):
    """创建钱包keyfile
    """
    account = Account.create()
    private_key = account.privateKey
    address = account.address
    keyfile_json = keys.make_keystore_json(private_key, password, kdf='scrypt')
    keyfile_json['id'] = str(keyfile_json['id'], encoding='utf-8')
    keyfile_json['address'] = address
    return address, private_key
Exemplo n.º 6
0
def create_keyfile(private_key, password, filename):
    """Creates a wallet keyfile.

    :param str private_key: private key
    :param password: keyfile password
    :param filename: keyfile path
    """
    import sys
    if sys.version_info.major >= 3:
        raise NotImplementedError('keyfile is only supported in python2')
    from ethereum.tools import keys as ekeys
    keyfile_json = ekeys.make_keystore_json(private_key.encode(),
                                            password,
                                            kdf='scrypt')
    with open(filename, 'w+') as f:
        json.dump(keyfile_json, f)
Exemplo n.º 7
0
    def new(cls, password, key=None, uuid=None, path=None):
        """Create a new account.

        Note that this creates the account in memory and does not store it on disk.

        :param password: the password used to encrypt the private key
        :param key: the private key, or `None` to generate a random one
        :param uuid: an optional id
        """
        if key is None:
            key = mk_random_privkey()

        # [NOTE]: key and password should be bytes
        if not is_string(key):
            key = to_string(key)
        if not is_string(password):
            password = to_string(password)

        keystore = keys.make_keystore_json(key, password)
        keystore['id'] = uuid
        return Account(keystore, password, path)
Exemplo n.º 8
0
def store_private_key(key, password, output_file):
    bkey = eth.normalize_key(key)

    keystore_json = ethkeys.make_keystore_json(bkey,
                                               password,
                                               kdf='pbkdf2',
                                               cipher='aes-128-ctr')
    keystore_json['id'] = keystore_json['id'].decode('utf8')
    logging.debug(keystore_json)
    if os.path.isdir(os.path.dirname(output_file)):
        if not os.path.isfile(output_file):
            with open(output_file, 'w') as f:
                json.dump(keystore_json, f)
                logging.info("Private key succesfully stored on file "
                             "'{}'.".format(output_file))
        else:
            logging.error('Keystore file already present! Aborting.')
            sys.exit(1)
    else:
        logging.error("Parent directory of keystore file doesn't exist. "
                      "Aborting")
        sys.exit(1)
Exemplo n.º 9
0
def create_keyfile(password, filename=None):
    """创建钱包keyfile
    """
    account = Account.create("")
    private_key = account.privateKey
    address = account.address
    keyfile_json = keys.make_keystore_json(private_key, password, kdf='scrypt')
    keyfile_json['id'] = str(keyfile_json['id'], encoding='utf-8')
    keyfile_json['address'] = address
    if not filename:
        isotime = dt.datetime.now().isoformat()
        filename = f'UTC--{isotime}--{address}'
    try:
        oldumask = os.umask(0)
        fd = os.open(filename, os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o600)
        with os.fdopen(fd, "w") as f:
            json.dump(keyfile_json, f)
    except IOError as e:
        print(e)
    else:
        return address, private_key
    finally:
        os.umask(oldumask)