Java Code Examples for net.bither.bitherj.crypto.hd.DeterministicKey

The following examples show how to use net.bither.bitherj.crypto.hd.DeterministicKey. These examples are extracted from open source projects. 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 Project: bitherj   Source File: HDMSingular.java    License: 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 Project: bitherj   Source File: HDMAddress.java    License: Apache License 2.0 6 votes vote down vote up
public ArrayList<TransactionSignature> signMyPart(List<byte[]> unsignedHashes,
                                                  CharSequence password) {
    if (isInRecovery()) {
        throw new AssertionError("recovery hdm address can not sign");
    }
    DeterministicKey key = keychain.getExternalKey(pubs.index, password);
    ArrayList<TransactionSignature> sigs = new ArrayList<TransactionSignature>();
    for (int i = 0;
         i < unsignedHashes.size();
         i++) {
        TransactionSignature transactionSignature = new TransactionSignature(key.sign
                (unsignedHashes.get(i)), TransactionSignature.SigHash.ALL, false);
        sigs.add(transactionSignature);
    }
    key.wipe();
    return sigs;
}
 
Example 3
Source Project: bitherj   Source File: DesktopHDMKeychain.java    License: Apache License 2.0 6 votes vote down vote up
private void initHDAccount(DeterministicKey master, EncryptedData encryptedMnemonicSeed,
                           EncryptedData encryptedHDSeed, boolean isSyncedComplete) {
    String firstAddress;
    ECKey k = new ECKey(mnemonicSeed, null);
    String address = k.toAddress();
    k.clearPrivateKey();
    DeterministicKey accountKey = getAccount(master);
    DeterministicKey internalKey = getChainRootKey(accountKey, AbstractHD.PathType.INTERNAL_ROOT_PATH);
    DeterministicKey externalKey = getChainRootKey(accountKey, AbstractHD.PathType.EXTERNAL_ROOT_PATH);
    DeterministicKey key = externalKey.deriveSoftened(0);
    firstAddress = key.toAddress();
    accountKey.wipe();
    master.wipe();

    wipeHDSeed();
    wipeMnemonicSeed();
    hdSeedId = AbstractDb.desktopAddressProvider.addHDKey(encryptedMnemonicSeed.toEncryptedString(),
            encryptedHDSeed.toEncryptedString(), firstAddress, isFromXRandom, address, externalKey.getPubKeyExtended(), internalKey
                    .getPubKeyExtended());
    internalKey.wipe();
    externalKey.wipe();


}
 
Example 4
Source Project: bitherj   Source File: DesktopHDMKeychain.java    License: Apache License 2.0 6 votes vote down vote up
public ArrayList<byte[]> signWithCold(List<byte[]> unsignedHashes,
                                      CharSequence password,
                                      List<PathTypeIndex> pathTypeIndexList) {


    ArrayList<byte[]> sigs = new ArrayList<byte[]>();
    for (int i = 0;
         i < unsignedHashes.size();
         i++) {
        PathTypeIndex pathTypeIndex = pathTypeIndexList.get(i);
        DeterministicKey key;
        if (pathTypeIndex.pathType == PathType.EXTERNAL_ROOT_PATH) {
            key = getExternalKey(pathTypeIndex.index, password);
            System.out.println("pub:" + Base58.encode(key.getPubKey()));
        } else {
            key = getInternalKey(pathTypeIndex.index, password);
        }
        ECKey.ECDSASignature signed = key.sign(unsignedHashes.get(i));
        sigs.add(signed.encodeToDER());
        key.wipe();
    }

    return sigs;
}
 
Example 5
Source Project: bitherj   Source File: DesktopHDMKeychain.java    License: Apache License 2.0 6 votes vote down vote up
public ArrayList<TransactionSignature> signMyPart(List<byte[]> unsignedHashes,
                                                  CharSequence password,
                                                  List<PathTypeIndex> pathTypeIndexList) {


    ArrayList<TransactionSignature> sigs = new ArrayList<TransactionSignature>();
    for (int i = 0;
         i < unsignedHashes.size();
         i++) {
        PathTypeIndex pathTypeIndex = pathTypeIndexList.get(i);
        DeterministicKey key;
        if (pathTypeIndex.pathType == PathType.EXTERNAL_ROOT_PATH) {
            key = getExternalKey(pathTypeIndex.index, password);
        } else {
            key = getInternalKey(pathTypeIndex.index, password);
        }
        TransactionSignature transactionSignature = new TransactionSignature(key.sign
                (unsignedHashes.get(i)), TransactionSignature.SigHash.ALL, false);
        sigs.add(transactionSignature);
        key.wipe();
    }

    return sigs;
}
 
Example 6
Source Project: bitherj   Source File: HDMSingular.java    License: Apache License 2.0 5 votes vote down vote up
private void initHotFirst() {
    DeterministicKey hotEx = rootFromMnemonic(hotMnemonicSeed);
    DeterministicKey hotFirst = hotEx.deriveSoftened(0);
    hotFirstAddress = hotFirst.toAddress();
    hotEx.wipe();
    hotFirst.wipe();
}
 
Example 7
Source Project: bitherj   Source File: AbstractHD.java    License: Apache License 2.0 5 votes vote down vote up
protected DeterministicKey getAccount(DeterministicKey master) {
    DeterministicKey purpose = master.deriveHardened(44);
    DeterministicKey coinType = purpose.deriveHardened(0);
    DeterministicKey account = coinType.deriveHardened(0);
    purpose.wipe();
    coinType.wipe();
    return account;
}
 
Example 8
Source Project: bitherj   Source File: AbstractHD.java    License: 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 9
Source Project: bitherj   Source File: DesktopHDMKeychain.java    License: Apache License 2.0 5 votes vote down vote up
public void addAccountKey(byte[] firstByte, byte[] secondByte) {
    if (new BigInteger(1, firstByte).compareTo(new BigInteger(1, secondByte)) > 0) {
        byte[] temp = firstByte;
        firstByte = secondByte;
        secondByte = temp;
    }

    DeterministicKey firstAccountKey = HDKeyDerivation.createMasterPubKeyFromExtendedBytes
            (firstByte);
    DeterministicKey secondAccountKey = HDKeyDerivation.createMasterPubKeyFromExtendedBytes
            (secondByte);

    DeterministicKey firestInternalKey = getChainRootKey(firstAccountKey, AbstractHD.PathType.INTERNAL_ROOT_PATH);
    DeterministicKey firestExternalKey = getChainRootKey(firstAccountKey, AbstractHD.PathType.EXTERNAL_ROOT_PATH);

    DeterministicKey secondInternalKey = getChainRootKey(secondAccountKey, AbstractHD.PathType.INTERNAL_ROOT_PATH);
    DeterministicKey secondExternalKey = getChainRootKey(secondAccountKey, AbstractHD.PathType.EXTERNAL_ROOT_PATH);
    List<byte[]> externalPubs = new ArrayList<byte[]>();
    List<byte[]> internalPubs = new ArrayList<byte[]>();
    externalPubs.add(firestExternalKey.getPubKeyExtended());
    externalPubs.add(secondExternalKey.getPubKeyExtended());
    internalPubs.add(firestInternalKey.getPubKeyExtended());
    internalPubs.add(secondInternalKey.getPubKeyExtended());
    AbstractDb.desktopAddressProvider.addHDMPub(externalPubs, internalPubs);
    addDesktopAddress(PathType.EXTERNAL_ROOT_PATH, LOOK_AHEAD_SIZE);
    addDesktopAddress(PathType.INTERNAL_ROOT_PATH, LOOK_AHEAD_SIZE);
}
 
Example 10
Source Project: bitherj   Source File: DesktopHDMKeychain.java    License: Apache License 2.0 5 votes vote down vote up
private void supplyNewInternalKey(int count, boolean isSyncedComplete) {
    List<DesktopHDMAddress> desktopHDMAddresses = new ArrayList<DesktopHDMAddress>();
    List<byte[]> internalPubs = AbstractDb.desktopAddressProvider.getInternalPubs();
    DeterministicKey internalKey1 = HDKeyDerivation.createMasterPubKeyFromExtendedBytes
            (internalPubs.get(0));
    DeterministicKey internalKey2 = HDKeyDerivation.createMasterPubKeyFromExtendedBytes
            (internalPubs.get(1));
    DeterministicKey internalKey3 = HDKeyDerivation.createMasterPubKeyFromExtendedBytes
            (internalPubs.get(2));
    int firstIndex = allGeneratedInternalAddressCount();
    for (int i = firstIndex;
         i < count + firstIndex;
         i++) {
        byte[] subInternalPub1 = internalKey1.deriveSoftened(i).getPubKey();
        byte[] subInternalPub2 = internalKey2.deriveSoftened(i).getPubKey();
        byte[] subInternalPub3 = internalKey3.deriveSoftened(i).getPubKey();
        HDMAddress.Pubs pubs = new HDMAddress.Pubs();
        pubs.hot = subInternalPub1;
        pubs.cold = subInternalPub2;
        pubs.remote = subInternalPub3;
        pubs.index = i;
        DesktopHDMAddress desktopHDMAddress = new DesktopHDMAddress(pubs, PathType.INTERNAL_ROOT_PATH, DesktopHDMKeychain.this, isSyncedComplete);
        desktopHDMAddresses.add(desktopHDMAddress);

    }
    AbstractDb.desktopTxProvider.addAddress(desktopHDMAddresses);

}
 
Example 11
Source Project: bitherj   Source File: DesktopHDMKeychain.java    License: Apache License 2.0 5 votes vote down vote up
private void supplyNewExternalKey(int count, boolean isSyncedComplete) {
    List<byte[]> externalPubs = AbstractDb.desktopAddressProvider.getExternalPubs();
    DeterministicKey externalKey1 = HDKeyDerivation.createMasterPubKeyFromExtendedBytes
            (externalPubs.get(0));
    DeterministicKey externalKey2 = HDKeyDerivation.createMasterPubKeyFromExtendedBytes
            (externalPubs.get(1));
    DeterministicKey externalKey3 = HDKeyDerivation.createMasterPubKeyFromExtendedBytes
            (externalPubs.get(2));
    List<DesktopHDMAddress> desktopHDMAddresses = new ArrayList<DesktopHDMAddress>();
    int firstIndex = allGeneratedExternalAddressCount();
    for (int i = firstIndex;
         i < count + firstIndex;
         i++) {
        byte[] subExternalPub1 = externalKey1.deriveSoftened(i).getPubKey();
        byte[] subExternalPub2 = externalKey2.deriveSoftened(i).getPubKey();
        byte[] subExternalPub3 = externalKey3.deriveSoftened(i).getPubKey();
        HDMAddress.Pubs pubs = new HDMAddress.Pubs();
        pubs.hot = subExternalPub1;
        pubs.cold = subExternalPub2;
        pubs.remote = subExternalPub3;
        pubs.index = i;
        DesktopHDMAddress desktopHDMAddress = new DesktopHDMAddress(pubs, PathType.EXTERNAL_ROOT_PATH, DesktopHDMKeychain.this, isSyncedComplete);
        desktopHDMAddresses.add(desktopHDMAddress);

    }
    AbstractDb.desktopTxProvider.addAddress(desktopHDMAddresses);
    log.info("HD supplied {} internal addresses", desktopHDMAddresses.size());
}
 
Example 12
Source Project: bitherj   Source File: DesktopHDMKeychain.java    License: Apache License 2.0 5 votes vote down vote up
private DeterministicKey externalChainRoot(CharSequence password) throws MnemonicException.MnemonicLengthException {
    DeterministicKey master = masterKey(password);
    DeterministicKey accountKey = getAccount(master);
    DeterministicKey externalKey = getChainRootKey(accountKey, PathType.EXTERNAL_ROOT_PATH);
    master.wipe();
    accountKey.wipe();
    return externalKey;
}
 
Example 13
Source Project: bitherj   Source File: DesktopHDMKeychain.java    License: Apache License 2.0 5 votes vote down vote up
public byte[] getExternalChainRootPubExtended(CharSequence password) throws MnemonicException
        .MnemonicLengthException {
    DeterministicKey ex = externalChainRoot(password);
    byte[] pub = ex.getPubKeyExtended();
    ex.wipe();
    return pub;
}
 
Example 14
Source Project: bitherj   Source File: DesktopHDMKeychain.java    License: 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 15
Source Project: bitherj   Source File: DesktopHDMAddress.java    License: Apache License 2.0 5 votes vote down vote up
public ArrayList<TransactionSignature> signMyPart(List<byte[]> unsignedHashes,
                                                  CharSequence password) {
    DeterministicKey key = keychain.getExternalKey(pubs.index, password);
    ArrayList<TransactionSignature> sigs = new ArrayList<TransactionSignature>();
    for (int i = 0;
         i < unsignedHashes.size();
         i++) {
        TransactionSignature transactionSignature = new TransactionSignature(key.sign
                (unsignedHashes.get(i)), TransactionSignature.SigHash.ALL, false);
        sigs.add(transactionSignature);
    }
    key.wipe();
    return sigs;
}
 
Example 16
Source Project: bitherj   Source File: HDAccount.java    License: 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 17
Source Project: bitherj   Source File: HDAccount.java    License: 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 18
Source Project: bitherj   Source File: HDAccount.java    License: 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 19
Source Project: bitherj   Source File: HDAccount.java    License: Apache License 2.0 5 votes vote down vote up
public HDAccount(byte[] accountExtentedPub, boolean isFromXRandom, boolean isSyncedComplete,
                 HDAccount.HDAccountGenerationDelegate generationDelegate) throws
        MnemonicException.MnemonicLengthException {
    super();
    this.isFromXRandom = isFromXRandom;
    DeterministicKey account = HDKeyDerivation.createMasterPubKeyFromExtendedBytes
            (accountExtentedPub);
    initHDAccount(account, null, null, isFromXRandom, isSyncedComplete, generationDelegate);
}
 
Example 20
Source Project: bitherj   Source File: HDAccount.java    License: Apache License 2.0 5 votes vote down vote up
private void supplyNewInternalKey(int count, boolean isSyncedComplete) {
    DeterministicKey root = HDKeyDerivation.createMasterPubKeyFromExtendedBytes
            (getInternalPub());
    int firstIndex = allGeneratedInternalAddressCount();
    ArrayList<HDAccountAddress> as = new ArrayList<HDAccountAddress>();
    for (int i = firstIndex;
         i < firstIndex + count;
         i++) {
        as.add(new HDAccountAddress(root.deriveSoftened(i).getPubKey(), AbstractHD.PathType
                .INTERNAL_ROOT_PATH, i, isSyncedComplete, hdSeedId));
    }
    AbstractDb.hdAccountAddressProvider.addAddress(as);
    log.info("HD supplied {} internal addresses", as.size());
}
 
Example 21
Source Project: bitherj   Source File: HDAccount.java    License: Apache License 2.0 5 votes vote down vote up
private void supplyNewExternalKey(int count, boolean isSyncedComplete) {
    DeterministicKey root = HDKeyDerivation.createMasterPubKeyFromExtendedBytes
            (getExternalPub());
    int firstIndex = allGeneratedExternalAddressCount();
    ArrayList<HDAccountAddress> as = new ArrayList<HDAccountAddress>();
    for (int i = firstIndex;
         i < firstIndex + count;
         i++) {
        as.add(new HDAccountAddress(root.deriveSoftened(i).getPubKey(), AbstractHD.PathType
                .EXTERNAL_ROOT_PATH, i, isSyncedComplete, hdSeedId));
    }
    AbstractDb.hdAccountAddressProvider.addAddress(as);
    log.info("HD supplied {} external addresses", as.size());
}
 
Example 22
Source Project: bitherj   Source File: HDAccount.java    License: Apache License 2.0 5 votes vote down vote up
protected DeterministicKey getAccount(DeterministicKey master) {
    DeterministicKey purpose = master.deriveHardened(44);
    DeterministicKey coinType = purpose.deriveHardened(0);
    DeterministicKey account = coinType.deriveHardened(0);
    purpose.wipe();
    coinType.wipe();
    return account;
}
 
Example 23
Source Project: bitherj   Source File: HDAccount.java    License: 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 24
Source Project: bitherj   Source File: HDAccount.java    License: Apache License 2.0 5 votes vote down vote up
public String xPubB58(CharSequence password) throws MnemonicException
        .MnemonicLengthException {
    DeterministicKey master = masterKey(password);
    DeterministicKey purpose = master.deriveHardened(44);
    DeterministicKey coinType = purpose.deriveHardened(0);
    DeterministicKey account = coinType.deriveHardened(0);
    String xpub = account.serializePubB58();
    master.wipe();
    purpose.wipe();
    coinType.wipe();
    account.wipe();
    return xpub;
}
 
Example 25
Source Project: bitherj   Source File: HDMKeychain.java    License: Apache License 2.0 5 votes vote down vote up
private DeterministicKey externalChainRoot(CharSequence password) throws MnemonicException.MnemonicLengthException {
    DeterministicKey master = masterKey(password);
    DeterministicKey accountKey = getAccount(master);
    DeterministicKey externalKey = getChainRootKey(accountKey, PathType.EXTERNAL_ROOT_PATH);
    master.wipe();
    accountKey.wipe();
    return externalKey;
}
 
Example 26
Source Project: bitherj   Source File: HDMKeychain.java    License: Apache License 2.0 5 votes vote down vote up
public byte[] getExternalChainRootPubExtended(CharSequence password) throws MnemonicException
        .MnemonicLengthException {
    DeterministicKey ex = externalChainRoot(password);
    byte[] pub = ex.getPubKeyExtended();
    ex.wipe();
    return pub;
}
 
Example 27
Source Project: bitherj   Source File: HDMKeychain.java    License: 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 28
Source Project: bitherj   Source File: HDMKeychain.java    License: Apache License 2.0 5 votes vote down vote up
public String signHDMBId(String messageHash, SecureCharSequence password) {
    DeterministicKey key = getExternalKey(0, password);
    log.info("messageHash:" + messageHash);
    if (key == null) {
        log.info("key:null");
    }

    byte[] signData = key.signHash(Utils.hexStringToByteArray(messageHash), null);

    return Utils.bytesToHexString(signData).toUpperCase();
}
 
Example 29
Source Project: bitherj   Source File: HDMKeychain.java    License: 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 30
Source Project: bitherj   Source File: EnterpriseHDMSeed.java    License: Apache License 2.0 5 votes vote down vote up
public byte[] getExternalRootPubExtended(CharSequence password) throws MnemonicException
        .MnemonicLengthException {
    DeterministicKey master = masterKey(password);
    DeterministicKey accountKey = getAccount(master);
    DeterministicKey externalChainRoot = getChainRootKey(accountKey, PathType
            .EXTERNAL_ROOT_PATH);
    master.wipe();
    accountKey.wipe();
    byte[] ext = externalChainRoot.getPubKeyExtended();
    externalChainRoot.clearPrivateKey();
    externalChainRoot.clearChainCode();
    return ext;
}