Java Code Examples for net.bither.bitherj.crypto.hd.HDKeyDerivation#createMasterPrivateKey()

The following examples show how to use net.bither.bitherj.crypto.hd.HDKeyDerivation#createMasterPrivateKey() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HDMSingular.java    From bitherj with Apache License 2.0 6 votes vote down vote up
private DeterministicKey rootFromMnemonic(byte[] mnemonic) {
    try {
        byte[] hdSeed = HDMKeychain.seedFromMnemonic(mnemonic);
        DeterministicKey master = HDKeyDerivation.createMasterPrivateKey(hdSeed);
        DeterministicKey purpose = master.deriveHardened(44);
        DeterministicKey coinType = purpose.deriveHardened(0);
        DeterministicKey account = coinType.deriveHardened(0);
        DeterministicKey external = account.deriveSoftened(0);
        master.wipe();
        purpose.wipe();
        coinType.wipe();
        account.wipe();
        return external;
    } catch (MnemonicException.MnemonicLengthException e) {
        throw new RuntimeException(e);
    }
}
 
Example 2
Source File: AbstractHD.java    From bitherj with Apache License 2.0 5 votes vote down vote up
protected DeterministicKey masterKey(CharSequence password) throws MnemonicException
        .MnemonicLengthException {
    long begin = System.currentTimeMillis();
    decryptHDSeed(password);
    DeterministicKey master = HDKeyDerivation.createMasterPrivateKey(hdSeed);
    wipeHDSeed();
    log.info("hdm keychain decrypt time: {}", System.currentTimeMillis() - begin);
    return master;
}
 
Example 3
Source File: DesktopHDMKeychain.java    From bitherj with Apache License 2.0 5 votes vote down vote up
public static boolean checkPassword(String keysString, CharSequence password) throws
        MnemonicException.MnemonicLengthException {
    String[] passwordSeeds = QRCodeUtil.splitOfPasswordSeed(keysString);
    String address = Base58.hexToBase58WithAddress(passwordSeeds[0]);
    String encreyptString = Utils.joinString(new String[]{passwordSeeds[1], passwordSeeds[2],
            passwordSeeds[3]}, QRCodeUtil.QR_CODE_SPLIT);
    byte[] seed = new EncryptedData(encreyptString).decrypt(password);
    MnemonicCode mnemonic = MnemonicCode.instance();

    byte[] s = mnemonic.toSeed(mnemonic.toMnemonic(seed), "");

    DeterministicKey master = HDKeyDerivation.createMasterPrivateKey(s);

    DeterministicKey purpose = master.deriveHardened(44);

    DeterministicKey coinType = purpose.deriveHardened(0);

    DeterministicKey account = coinType.deriveHardened(0);

    DeterministicKey external = account.deriveSoftened(0);

    external.clearPrivateKey();

    DeterministicKey key = external.deriveSoftened(0);
    boolean result = Utils.compareString(address, Utils.toAddress(key.getPubKeyHash()));
    key.wipe();

    return result;
}
 
Example 4
Source File: HDAccount.java    From bitherj with Apache License 2.0 5 votes vote down vote up
public HDAccount(byte[] mnemonicSeed, CharSequence password, boolean isSyncedComplete) throws
        MnemonicException
        .MnemonicLengthException {
    super();
    this.mnemonicSeed = mnemonicSeed;
    hdSeed = seedFromMnemonic(mnemonicSeed);
    DeterministicKey master = HDKeyDerivation.createMasterPrivateKey(hdSeed);
    EncryptedData encryptedHDSeed = new EncryptedData(hdSeed, password, isFromXRandom);
    EncryptedData encryptedMnemonicSeed = new EncryptedData(mnemonicSeed, password,
            isFromXRandom);
    DeterministicKey account = getAccount(master);
    account.clearPrivateKey();
    initHDAccount(account, encryptedMnemonicSeed, encryptedHDSeed, isFromXRandom,
            isSyncedComplete, null);
}
 
Example 5
Source File: HDAccount.java    From bitherj with Apache License 2.0 5 votes vote down vote up
public HDAccount(SecureRandom random, CharSequence password, HDAccountGenerationDelegate generationDelegate) throws MnemonicException.MnemonicLengthException {
    isFromXRandom = random.getClass().getCanonicalName().indexOf("XRandom") >= 0;
    mnemonicSeed = new byte[16];
    random.nextBytes(mnemonicSeed);
    hdSeed = seedFromMnemonic(mnemonicSeed);
    EncryptedData encryptedHDSeed = new EncryptedData(hdSeed, password, isFromXRandom);
    EncryptedData encryptedMnemonicSeed = new EncryptedData(mnemonicSeed, password,
            isFromXRandom);
    DeterministicKey master = HDKeyDerivation.createMasterPrivateKey(hdSeed);
    DeterministicKey account = getAccount(master);
    account.clearPrivateKey();
    initHDAccount(account, encryptedMnemonicSeed, encryptedHDSeed, isFromXRandom, true,
            generationDelegate);
}
 
Example 6
Source File: HDAccount.java    From bitherj with Apache License 2.0 5 votes vote down vote up
public HDAccount(EncryptedData encryptedMnemonicSeed, CharSequence password, boolean
        isSyncedComplete)
        throws MnemonicException.MnemonicLengthException {
    mnemonicSeed = encryptedMnemonicSeed.decrypt(password);
    hdSeed = seedFromMnemonic(mnemonicSeed);
    isFromXRandom = encryptedMnemonicSeed.isXRandom();
    EncryptedData encryptedHDSeed = new EncryptedData(hdSeed, password, isFromXRandom);
    DeterministicKey master = HDKeyDerivation.createMasterPrivateKey(hdSeed);
    DeterministicKey account = getAccount(master);
    account.clearPrivateKey();
    initHDAccount(account, encryptedMnemonicSeed, encryptedHDSeed, isFromXRandom,
            isSyncedComplete, null);
}
 
Example 7
Source File: HDAccount.java    From bitherj with Apache License 2.0 5 votes vote down vote up
protected DeterministicKey masterKey(CharSequence password) throws MnemonicException
        .MnemonicLengthException {
    long begin = System.currentTimeMillis();
    decryptHDSeed(password);
    DeterministicKey master = HDKeyDerivation.createMasterPrivateKey(hdSeed);
    wipeHDSeed();
    log.info("hdm keychain decrypt time: {}", System.currentTimeMillis() - begin);
    return master;
}
 
Example 8
Source File: HDMKeychain.java    From bitherj with Apache License 2.0 5 votes vote down vote up
public boolean checkSingularBackupWithPassword(CharSequence password) {
    if (isInRecovery()) {
        return true;
    }
    if (getAllCompletedAddresses().size() == 0) {
        return true;
    }
    String backup = AbstractDb.addressProvider.getSingularModeBackup(getHdSeedId());
    if (backup == null) {
        return true;
    }
    EncryptedData encrypted = new EncryptedData(backup);
    byte[] mnemonic = encrypted.decrypt(password);
    boolean result;
    try {
        byte[] seed = seedFromMnemonic(mnemonic);
        byte[] pub = getAllCompletedAddresses().get(0).getPubCold();
        DeterministicKey master = HDKeyDerivation.createMasterPrivateKey(seed);
        DeterministicKey purpose = master.deriveHardened(44);
        DeterministicKey coinType = purpose.deriveHardened(0);
        DeterministicKey account = coinType.deriveHardened(0);
        DeterministicKey external = account.deriveSoftened(0);
        DeterministicKey first = external.deriveSoftened(0);
        master.wipe();
        purpose.wipe();
        coinType.wipe();
        account.wipe();
        external.wipe();
        Utils.wipeBytes(seed);
        result = Arrays.equals(first.getPubKey(), pub);
        first.wipe();
    } catch (MnemonicException.MnemonicLengthException e) {
        e.printStackTrace();
        result = false;
    }
    Utils.wipeBytes(mnemonic);
    return result;
}
 
Example 9
Source File: HDMKeychain.java    From bitherj with Apache License 2.0 5 votes vote down vote up
public static boolean checkPassword(String keysString, CharSequence password) throws
        MnemonicException.MnemonicLengthException {
    String[] passwordSeeds = QRCodeUtil.splitOfPasswordSeed(keysString);
    String address = Base58.hexToBase58WithAddress(passwordSeeds[0]);
    String encreyptString = Utils.joinString(new String[]{passwordSeeds[1], passwordSeeds[2],
            passwordSeeds[3]}, QRCodeUtil.QR_CODE_SPLIT);
    byte[] seed = new EncryptedData(encreyptString).decrypt(password);
    MnemonicCode mnemonic = MnemonicCode.instance();

    byte[] s = mnemonic.toSeed(mnemonic.toMnemonic(seed), "");

    DeterministicKey master = HDKeyDerivation.createMasterPrivateKey(s);

    DeterministicKey purpose = master.deriveHardened(44);

    DeterministicKey coinType = purpose.deriveHardened(0);

    DeterministicKey account = coinType.deriveHardened(0);

    DeterministicKey external = account.deriveSoftened(0);

    external.clearPrivateKey();

    DeterministicKey key = external.deriveSoftened(0);
    boolean result = Utils.compareString(address, Utils.toAddress(key.getPubKeyHash()));
    key.wipe();

    return result;
}
 
Example 10
Source File: HDAccountCold.java    From bitherj with Apache License 2.0 5 votes vote down vote up
public HDAccountCold(byte[] mnemonicSeed, CharSequence password, boolean isFromXRandom)
        throws MnemonicException.MnemonicLengthException {
    this.mnemonicSeed = mnemonicSeed;
    hdSeed = seedFromMnemonic(mnemonicSeed);
    this.isFromXRandom = isFromXRandom;
    DeterministicKey master = HDKeyDerivation.createMasterPrivateKey(hdSeed);
    EncryptedData encryptedHDSeed = new EncryptedData(hdSeed, password, isFromXRandom);
    EncryptedData encryptedMnemonicSeed = new EncryptedData(mnemonicSeed, password,
            isFromXRandom);
    ECKey k = new ECKey(mnemonicSeed, null);
    String address = k.toAddress();
    k.clearPrivateKey();
    DeterministicKey accountKey = getAccount(master);
    DeterministicKey externalKey = getChainRootKey(accountKey, AbstractHD.PathType
            .EXTERNAL_ROOT_PATH);
    DeterministicKey internalKey = getChainRootKey(accountKey, PathType
            .INTERNAL_ROOT_PATH);
    DeterministicKey key = externalKey.deriveSoftened(0);
    String firstAddress = key.toAddress();
    accountKey.wipe();
    master.wipe();
    wipeHDSeed();
    wipeMnemonicSeed();
    hdSeedId = AbstractDb.hdAccountProvider.addHDAccount(encryptedMnemonicSeed
                    .toEncryptedString(), encryptedHDSeed.toEncryptedString(), firstAddress,
            isFromXRandom, address, externalKey.getPubKeyExtended(), internalKey
                    .getPubKeyExtended());
    externalKey.wipe();
}