コード例 #1
0
ファイル: wallet.py プロジェクト: fregatte/minter-sdk
    def create(cls, mnemonic=None):
        """
        Create Minter wallet
            @param mnemonic|string: Mnemonic phrase
            @return: dict 
        """

        # Create mnemonic phrase if None
        if not mnemonic:
            _mnemonic = Mnemonic(language='english')
            mnemonic = _mnemonic.generate(cls.BIP44_ENTROPY_BITS)

        if len(mnemonic.split(' ')) != 12:
            raise Exception('Mnemonic phrase should have 12 words.')

        # Mnemonic to seed (bytes)
        seed = Mnemonic.to_seed(mnemonic, '')

        # Generate master key from master seed
        I = hmac.new(cls.MASTER_SEED, seed, hashlib.sha512).hexdigest()
        IL = I[:64]
        IR = I[64:]

        master_key = HDPrivateKey(key=int.from_bytes(binascii.unhexlify(IL),
                                                     'big'),
                                  chain_code=binascii.unhexlify(IR),
                                  index=0,
                                  depth=0)

        # Get child keys from master key by path
        keys = HDKey.from_path(master_key, cls.BIP44_SEED_ADDRESS_PATH)

        # Get private key
        private_key = binascii.hexlify(bytes(keys[-1]._key))

        # Get public key
        public_key = cls.get_public_from_private(private_key)

        # Get address
        address = cls.get_address_from_public_key(public_key)

        return {
            'address': address,
            'private_key': private_key.decode(),
            'mnemonic': mnemonic,
            'seed': binascii.hexlify(seed).decode()
        }
コード例 #2
0
ファイル: keystore.py プロジェクト: piotr-roslaniec/nucypher
    def generate(
        cls,
        password: str,
        keystore_dir: Optional[Path] = None,
        interactive: bool = True,
    ) -> Union['Keystore', Tuple['Keystore', str]]:
        """Generate a new nucypher keystore for use with characters"""
        mnemonic = Mnemonic(_MNEMONIC_LANGUAGE)
        __words = mnemonic.generate(strength=_ENTROPY_BITS)
        if interactive:
            cls._confirm_generate(__words)
        __secret = bytes(mnemonic.to_entropy(__words))
        path = Keystore.__save(secret=__secret,
                               password=password,
                               keystore_dir=keystore_dir)
        keystore = cls(keystore_path=path)

        if interactive:
            return keystore

        return keystore, __words
コード例 #3
0
    def create(cls, mnemonic=None):
        """
        Create Minter wallet
        Args:
            mnemonic (str): Mnemonic phrase
        Returns:
            dict
        """

        # Create mnemonic phrase if None
        if not mnemonic:
            _mnemonic = Mnemonic(language='english')
            mnemonic = _mnemonic.generate(cls.entropy_bits)

        if len(mnemonic.split(' ')) != 12:
            raise Exception('Mnemonic phrase should have 12 words.')

        # Mnemonic to seed (bytes)
        seed = Mnemonic.to_seed(mnemonic, '')

        # Generate master key (key, hmac_key) from master seed
        _I = hmac.new(cls.master_seed, seed, hashlib.sha512).hexdigest()
        master_key = (int(_I[:64], 16), bytes.fromhex(_I[64:]))

        # Get child keys from master key by path
        keys = cls.from_path(root_key=master_key, path=cls.seed_address_path)

        # Get private key
        private_key = format(keys[-1][0], '0x')
        # Get public key from private
        public_key = cls.get_public_from_private(private_key)
        # Get address from public key
        address = cls.get_address_from_public_key(public_key)

        return {
            'address': address,
            'private_key': private_key,
            'mnemonic': mnemonic,
            'seed': seed.hex()
        }