Java Code Examples for net.bither.bitherj.crypto.hd.DeterministicKey#wipe()

The following examples show how to use net.bither.bitherj.crypto.hd.DeterministicKey#wipe() . 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: HDMAddress.java    From bitherj with 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 2
Source File: DesktopHDMKeychain.java    From bitherj with 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 3
Source File: DesktopHDMKeychain.java    From bitherj with 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 4
Source File: DesktopHDMKeychain.java    From bitherj with 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 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 6
Source File: HDMKeychain.java    From bitherj with 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 7
Source File: HDAccountCold.java    From bitherj with 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 8
Source File: HDAccount.java    From bitherj with 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 9
Source File: HDAccount.java    From bitherj with 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 10
Source File: DesktopHDMAddress.java    From bitherj with 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 11
Source File: HDAccountCold.java    From bitherj with Apache License 2.0 5 votes vote down vote up
public byte[] accountPubExtended(CharSequence password) throws MnemonicException
        .MnemonicLengthException {
    DeterministicKey master = masterKey(password);
    DeterministicKey account = getAccount(master);
    byte[] extended = account.getPubKeyExtended();
    master.wipe();
    account.wipe();
    return extended;
}
 
Example 12
Source File: DesktopHDMKeychain.java    From bitherj with 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 13
Source File: DesktopHDMKeychain.java    From bitherj with 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 14
Source File: AbstractHD.java    From bitherj with 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 15
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();
}
 
Example 16
Source File: EnterpriseHDMSeed.java    From bitherj with 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;
}
 
Example 17
Source File: AbstractHD.java    From bitherj with Apache License 2.0 4 votes vote down vote up
protected String getFirstAddressFromSeed(CharSequence password) {
    DeterministicKey key = getExternalKey(0, password);
    String address = Utils.toAddress(key.getPubKeyHash());
    key.wipe();
    return address;
}
 
Example 18
Source File: HDAccount.java    From bitherj with Apache License 2.0 4 votes vote down vote up
protected String getFirstAddressFromSeed(CharSequence password) {
    DeterministicKey key = getExternalKey(0, password);
    String address = Utils.toAddress(key.getPubKeyHash());
    key.wipe();
    return address;
}
 
Example 19
Source File: HDMSingular.java    From bitherj with Apache License 2.0 4 votes vote down vote up
private void initColdFirst() {
    DeterministicKey coldEx = rootFromMnemonic(coldMnemonicSeed);
    coldRoot = coldEx.getPubKeyExtended();
    coldFirst = coldEx.deriveSoftened(0);
    coldEx.wipe();
}
 
Example 20
Source File: SignTxDialg.java    From bither-desktop-java with Apache License 2.0 4 votes vote down vote up
@Override
public void onPasswordEntered(final SecureCharSequence password) {
    dp = new DialogProgress();
    Thread thread = new Thread() {
        public void run() {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    dp.pack();
                    dp.setVisible(true);
                }
            });
            List<String> strings = new ArrayList<String>();
            if (qrCodeTransport.getHdmIndex() >= 0) {
                if (!AddressManager.getInstance().hasHDMKeychain()) {
                    SwingUtilities.invokeLater(new Runnable() {
                        @Override
                        public void run() {
                            dp.dispose();
                            new MessageDialog(LocaliserUtils.getString("hdm_send_with_cold_no_requested_seed"));

                        }
                    });
                    password.wipe();
                    SwingUtilities.invokeLater(new Runnable() {
                        @Override
                        public void run() {
                            dp.dispose();
                        }
                    });
                    return;
                }
                try {
                    DeterministicKey key = AddressManager.getInstance().getHdmKeychain()
                            .getExternalKey(qrCodeTransport.getHdmIndex(), password);

                    List<String> hashes = qrCodeTransport.getHashList();
                    strings = new ArrayList<String>();
                    for (String hash : hashes) {
                        ECKey.ECDSASignature signed = key.sign(Utils.hexStringToByteArray
                                (hash));
                        strings.add(Utils.bytesToHexString(signed.encodeToDER()));
                    }
                    key.wipe();
                } catch (Exception e) {
                    e.printStackTrace();

                    SwingUtilities.invokeLater(new Runnable() {
                        @Override
                        public void run() {
                            dp.dispose();
                            new MessageDialog(LocaliserUtils.getString("hdm_send_with_cold_no_requested_seed")).showMsg();
                        }
                    });
                    password.wipe();
                    return;
                }
            } else {
                Address address = WalletUtils.findPrivateKey(qrCodeTransport.getMyAddress());
                strings = address.signStrHashes(qrCodeTransport.getHashList(), password);
            }
            password.wipe();
            String result = "";
            for (int i = 0;
                 i < strings.size();
                 i++) {
                if (i < strings.size() - 1) {
                    result = result + strings.get(i) + QRCodeUtil.QR_CODE_SPLIT;
                } else {
                    result = result + strings.get(i);
                }
            }
            final String r = result;
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    dp.dispose();
                    dispose();
                    DisplayBitherQRCodePanel displayBitherQRCodePanle = new DisplayBitherQRCodePanel(r);
                    displayBitherQRCodePanle.showPanel();

                }
            });

        }

        ;
    };
    thread.start();

}