public static byte calculateChecksum(byte[] initialEntropy) { int ent = initialEntropy.length * 8; byte mask = (byte) (0xff << 8 - ent / 32); byte[] bytes = sha256(initialEntropy); return (byte) (bytes[0] & mask); }
public static byte[] sha256hash160(byte[] input) { byte[] sha256 = sha256(input); RIPEMD160Digest digest = new RIPEMD160Digest(); digest.update(sha256, 0, sha256.length); byte[] out = new byte[20]; digest.doFinal(out, 0); return out; } }
public static Credentials loadBip39Credentials(String password, String mnemonic) { byte[] seed = MnemonicUtils.generateSeed(mnemonic, password); return Credentials.create(ECKeyPair.create(sha256(seed))); }
/** * Generates a BIP-39 compatible Ethereum wallet. The private key for the wallet can * be calculated using following algorithm: * <pre> * Key = SHA-256(BIP_39_SEED(mnemonic, password)) * </pre> * * @param password Will be used for both wallet encryption and passphrase for BIP-39 seed * @param destinationDirectory The directory containing the wallet * @return A BIP-39 compatible Ethereum wallet * @throws CipherException if the underlying cipher is not available * @throws IOException if the destination cannot be written to */ public static Bip39Wallet generateBip39Wallet(String password, File destinationDirectory) throws CipherException, IOException { byte[] initialEntropy = new byte[16]; secureRandom.nextBytes(initialEntropy); String mnemonic = MnemonicUtils.generateMnemonic(initialEntropy); byte[] seed = MnemonicUtils.generateSeed(mnemonic, password); ECKeyPair privateKey = ECKeyPair.create(sha256(seed)); String walletFile = generateWalletFile(password, privateKey, destinationDirectory, false); return new Bip39Wallet(walletFile, mnemonic); }
@Test public void testGenerateBip39Wallets() throws Exception { Bip39Wallet wallet = WalletUtils.generateBip39Wallet(PASSWORD, tempDir); byte[] seed = MnemonicUtils.generateSeed(wallet.getMnemonic(), PASSWORD); Credentials credentials = Credentials.create(ECKeyPair.create(sha256(seed))); assertEquals(credentials, WalletUtils.loadBip39Credentials(PASSWORD, wallet.getMnemonic())); }
public static byte calculateChecksum(byte[] initialEntropy) { int ent = initialEntropy.length * 8; byte mask = (byte) (0xff << 8 - ent / 32); byte[] bytes = sha256(initialEntropy); return (byte) (bytes[0] & mask); }