net.bither.bitherj.crypto.hd.HDKeyDerivation Java Examples
The following examples show how to use
net.bither.bitherj.crypto.hd.HDKeyDerivation.
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 |
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: HDAccount.java From bitherj with Apache License 2.0 | 5 votes |
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 #3
Source File: HDAccountCold.java From bitherj with Apache License 2.0 | 5 votes |
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 #4
Source File: HDMKeychain.java From bitherj with Apache License 2.0 | 5 votes |
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 #5
Source File: HDMKeychain.java From bitherj with Apache License 2.0 | 5 votes |
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 #6
Source File: HDAccount.java From bitherj with Apache License 2.0 | 5 votes |
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 #7
Source File: HDAccount.java From bitherj with Apache License 2.0 | 5 votes |
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 #8
Source File: HDAccount.java From bitherj with Apache License 2.0 | 5 votes |
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 #9
Source File: HDAccount.java From bitherj with Apache License 2.0 | 5 votes |
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 #10
Source File: HDAccount.java From bitherj with Apache License 2.0 | 5 votes |
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 #11
Source File: HDAccount.java From bitherj with Apache License 2.0 | 5 votes |
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 #12
Source File: DesktopHDMKeychain.java From bitherj with Apache License 2.0 | 5 votes |
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 #13
Source File: DesktopHDMKeychain.java From bitherj with Apache License 2.0 | 5 votes |
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 #14
Source File: DesktopHDMKeychain.java From bitherj with Apache License 2.0 | 5 votes |
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 #15
Source File: DesktopHDMKeychain.java From bitherj with Apache License 2.0 | 5 votes |
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 #16
Source File: AbstractHD.java From bitherj with Apache License 2.0 | 5 votes |
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 #17
Source File: EnterpriseHDMKeychainAddNewAddressActivity.java From bither-android with Apache License 2.0 | 4 votes |
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (AddPubCode == requestCode) { if (resultCode == RESULT_OK) { String result = data.getStringExtra(ScanActivity.INTENT_EXTRA_RESULT); if (Utils.isEmpty(result)) { return; } LogUtil.d("AddPub", "pub : " + result); byte[] pub = null; try { if (result.length() == 130) { pub = Utils.hexStringToByteArray(result); if (HDKeyDerivation.createMasterPubKeyFromExtendedBytes(Arrays.copyOf (pub, pub.length)) == null) { pub = null; } } } catch (Exception e) { pub = null; e.printStackTrace(); } if (pub == null) { DropdownMessage.showDropdownMessage(this, R.string .enterprise_hdm_keychain_collect_pub_error); return; } pubs.add(pub); for (int i = 0; i < pubs.size() && i < llPubs.getChildCount(); i++) { View v = llPubs.getChildAt(i); v.setBackgroundColor(getResources().getColor(R.color.blue_dark)); } if (pubs.size() < pubCount()) { ibtnAddPub.setVisibility(View.VISIBLE); btnCollectFinish.setEnabled(false); } else { ibtnAddPub.setVisibility(View.GONE); btnCollectFinish.setEnabled(true); } } return; } super.onActivityResult(requestCode, resultCode, data); }
Example #18
Source File: AddEnterpriseHDMKeychainActivity.java From bither-android with Apache License 2.0 | 4 votes |
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (AddPubCode == requestCode) { if (resultCode == RESULT_OK) { String result = data.getStringExtra(ScanActivity.INTENT_EXTRA_RESULT); if (Utils.isEmpty(result)) { return; } if (!result.startsWith(EnterpriseHDMSeed.XPubPrefix)) { DropdownMessage.showDropdownMessage(this, R.string .enterprise_hdm_keychain_collect_pub_error); return; } result = result.substring(EnterpriseHDMSeed.XPubPrefix.length()); LogUtil.d("AddPub", "pub : " + result); byte[] pub = null; try { if (result.length() == 130) { pub = Utils.hexStringToByteArray(result); if (HDKeyDerivation.createMasterPubKeyFromExtendedBytes(Arrays.copyOf (pub, pub.length)) == null) { pub = null; } } } catch (Exception e) { pub = null; e.printStackTrace(); } if (pub == null) { DropdownMessage.showDropdownMessage(this, R.string .enterprise_hdm_keychain_collect_pub_error); return; } pubs.add(pub); for (int i = 0; i < pubs.size() && i < llPubs.getChildCount(); i++) { View v = llPubs.getChildAt(i); v.setBackgroundColor(getResources().getColor(R.color.blue_dark)); } if (pubs.size() < pubCount()) { ibtnAddPub.setVisibility(View.VISIBLE); btnCollectFinish.setEnabled(false); } else { ibtnAddPub.setVisibility(View.GONE); btnCollectFinish.setEnabled(true); } } return; } super.onActivityResult(requestCode, resultCode, data); }
Example #19
Source File: HDAccountMonitoredSendActivity.java From bither-android with Apache License 2.0 | 4 votes |
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == BitherSetting.INTENT_REF.SIGN_TX_REQUEST_CODE) { if (resultCode == RESULT_OK) { btnSend.setEnabled(false); final String qr = data.getStringExtra(ScanActivity.INTENT_EXTRA_RESULT); if (!dp.isShowing()) { dp.show(); } new Thread() { @Override public void run() { String[] array = QRCodeUtil.splitString(qr); ArrayList<byte[]> sigs = new ArrayList<>(); for (String s : array) { sigs.add(Utils.hexStringToByteArray(s)); } if (tx.isSegwitAddress()) { HDAccount account = (HDAccount) address; List<HDAccount.HDAccountAddress> addresses = account.getSigningAddressesForInputs(tx .getIns()); List<byte[]> signatures = new ArrayList<>(); List<byte[]> witnesses = new ArrayList<>(); for (int i = 0; i < addresses.size(); i++) { HDAccount.HDAccountAddress hdAccountAddress = addresses.get(i); if (hdAccountAddress.getPathType().isSegwit()) { DeterministicKey root = HDKeyDerivation.createMasterPubKeyFromExtendedBytes(account.getExternalPub(hdAccountAddress.getPathType())); DeterministicKey key = root.deriveSoftened(hdAccountAddress.getIndex()); signatures.add(getRedeemScript(key.getPubKey())); witnesses.add(sigs.get(i)); } else { signatures.add(sigs.get(i)); byte[] witness = {0x00}; witnesses.add(witness); } } tx.signWithSignatures(signatures); tx.setWitnesses(witnesses); } else { tx.signWithSignatures(sigs); } tx.setIsSigned(true); if (tx.verifySignatures()) { runOnUiThread(new Runnable() { @Override public void run() { sendTx(); } }); } else { runOnUiThread(new Runnable() { @Override public void run() { if (dp.isShowing()) { dp.dismiss(); } DropdownMessage.showDropdownMessage (HDAccountMonitoredSendActivity.this, R.string .unsigned_transaction_sign_failed); btnSend.setEnabled(true); } }); } } }.start(); } else { btnSend.setEnabled(true); } return; } super.onActivityResult(requestCode, resultCode, data); }