Java Code Examples for net.bither.bitherj.crypto.ECKey#getPubKey()

The following examples show how to use net.bither.bitherj.crypto.ECKey#getPubKey() . 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: KeyUtil.java    From bither-desktop-java with Apache License 2.0 6 votes vote down vote up
public static List<Address> addPrivateKeyByRandomWithPassphras(IUEntropy iuEntropy, CharSequence password, int count) {
    PeerUtil.stopPeer();
    List<Address> addressList = new ArrayList<Address>();
    for (int i = 0; i < count; i++) {
        XRandom xRandom = new XRandom(iuEntropy);
        ECKey ecKey = ECKey.generateECKey(xRandom);
        ecKey = PrivateKeyUtil.encrypt(ecKey, password);
        Address address = new Address(ecKey.toAddress(),
                ecKey.getPubKey(), PrivateKeyUtil.getEncryptedString(ecKey), ecKey.isFromXRandom());
        ecKey.clearPrivateKey();
        addressList.add(address);
        AddressManager.getInstance().addAddress(address);

    }
    PeerUtil.startPeer();
    if (UserPreference.getInstance().getAppMode() == BitherjSettings.AppMode.COLD) {
        BackupUtil.backupColdKey(false);
    } else {
        BackupUtil.backupHotKey();
    }

    return addressList;

}
 
Example 2
Source File: Tx.java    From bitherj with Apache License 2.0 6 votes vote down vote up
private byte[] getSignPubs(byte[] messageHash,
                           ECKey.ECDSASignature sig, List<byte[]> pubs) {

    for (int i = 0; i < 4; i++) {
        ECPoint point = ECKey.recoverECPointFromSignature(i, sig, messageHash);
        ECKey ecKeyCompress = new ECKey(null, point.getEncoded(true));
        ECKey ecKeyUnCompress = new ECKey(null, point.getEncoded(false));
        for (int j = 0; j < pubs.size(); j++) {
            if (Arrays.equals(ecKeyCompress.getPubKey(), pubs.get(j))) {
                return ecKeyCompress.getPubKey();

            }
            if (Arrays.equals(ecKeyUnCompress.getPubKey(), pubs.get(j))) {
                return ecKeyUnCompress.getPubKey();

            }
        }
    }
    return null;
}
 
Example 3
Source File: KeyUtil.java    From bither-desktop-java with Apache License 2.0 6 votes vote down vote up
public static List<Address> addPrivateKeyByRandomWithPassphras(IUEntropy iuEntropy, CharSequence password, int count) {
    PeerUtil.stopPeer();
    List<Address> addressList = new ArrayList<Address>();
    for (int i = 0; i < count; i++) {
        XRandom xRandom = new XRandom(iuEntropy);
        ECKey ecKey = ECKey.generateECKey(xRandom);
        ecKey = PrivateKeyUtil.encrypt(ecKey, password);
        Address address = new Address(ecKey.toAddress(),
                ecKey.getPubKey(), PrivateKeyUtil.getEncryptedString(ecKey), ecKey.isFromXRandom());
        ecKey.clearPrivateKey();
        addressList.add(address);
        AddressManager.getInstance().addAddress(address);

    }
    PeerUtil.startPeer();
    if (UserPreference.getInstance().getAppMode() == BitherjSettings.AppMode.COLD) {
        BackupUtil.backupColdKey(false);
    } else {
        BackupUtil.backupHotKey();
    }

    return addressList;

}
 
Example 4
Source File: KeyUtil.java    From bither-android with Apache License 2.0 5 votes vote down vote up
public static List<Address> addPrivateKeyByRandomWithPassphras(BlockchainService service, IUEntropy iuEntropy, CharSequence password, int count) {
    if (service != null) {
        service.stopAndUnregister();
    }
    List<Address> addressList = new ArrayList<Address>();
    for (int i = 0; i < count; i++) {
        XRandom xRandom = new XRandom(iuEntropy);
        ECKey ecKey = ECKey.generateECKey(xRandom);
        ecKey = PrivateKeyUtil.encrypt(ecKey, password);
        Address address = new Address(ecKey.toAddress(),
                ecKey.getPubKey(), PrivateKeyUtil.getEncryptedString(ecKey), true, ecKey.isFromXRandom());
        ecKey.clearPrivateKey();
        addressList.add(address);
        AddressManager.getInstance().addAddress(address);

    }
    if (AppSharedPreference.getInstance().getAppMode() == BitherjSettings.AppMode.COLD) {
        BackupUtil.backupColdKey(false);
    } else {
        BackupUtil.backupHotKey();
    }
    if (service != null) {
        service.startAndRegister();
    }
    return addressList;

}
 
Example 5
Source File: PrivateKeyUtil.java    From bitherj with Apache License 2.0 5 votes vote down vote up
public static List<Address> getECKeysFromBackupString(String str, CharSequence password) {
    String[] strs = QRCodeUtil.splitOfPasswordSeed(str);
    if (strs.length % 3 != 0) {
        log.error("Backup: PrivateKeyFromString format error");
        return null;
    }
    ArrayList<Address> list = new ArrayList<Address>();
    for (int i = 0;
         i < strs.length;
         i += 3) {
        if (strs[i].indexOf(QRCodeUtil.HDM_QR_CODE_FLAG) == 0) {
            continue;
        }
        if (strs[i].indexOf(QRCodeUtil.HD_QR_CODE_FLAG) == 0){
            continue;
        }
        String encryptedString = strs[i] + QRCodeUtil.QR_CODE_SPLIT + strs[i + 1]
                + QRCodeUtil.QR_CODE_SPLIT + strs[i + 2];
        ECKey key = getECKeyFromSingleString(encryptedString, password);

        if (key == null) {
            return null;
        } else {
            Address address = new Address(key.toAddress(), key.getPubKey(), encryptedString,
                    false, key.isFromXRandom());
            key.clearPrivateKey();
            list.add(address);
        }
    }
    return list;
}
 
Example 6
Source File: ImportPrivateKey.java    From bitherj with Apache License 2.0 5 votes vote down vote up
private Address addECKey(ECKey ecKey) {
    String encryptedPrivateString;
    if (importPrivateKeyType == ImportPrivateKeyType.BitherQrcode) {
        encryptedPrivateString = QRCodeUtil.getNewVersionEncryptPrivKey(content);
    } else {
        ecKey = PrivateKeyUtil.encrypt(ecKey, password);
        encryptedPrivateString = PrivateKeyUtil.getEncryptedString(ecKey);
    }
    Address address = new Address(ecKey.toAddress(), ecKey.getPubKey(), encryptedPrivateString
            , false, ecKey.isFromXRandom());
    if (AddressManager.getInstance().getWatchOnlyAddresses().contains(address)) {
        password.wipe();
        importError(CAN_NOT_IMPORT_BITHER_COLD_PRIVATE_KEY);
        return null;
    } else if (AddressManager.getInstance().getPrivKeyAddresses().contains(address)) {
        password.wipe();
        importError(PRIVATE_KEY_ALREADY_EXISTS);
        return null;

    } else {
        if (importPrivateKeyType == ImportPrivateKeyType.BitherQrcode) {
            PasswordSeed passwordSeed = PasswordSeed.getPasswordSeed();
            if (passwordSeed != null && !passwordSeed.checkPassword(password)) {
                password.wipe();
                importError(PASSWORD_IS_DIFFEREND_LOCAL);
                return null;
            }
        } else {
            password.wipe();
        }
        return address;


    }

}
 
Example 7
Source File: PrivateKeyUEntropyDialog.java    From bither-desktop-java with Apache License 2.0 4 votes vote down vote up
@Override
public void run() {
    startGeneratingTime = System.currentTimeMillis();
    onProgress(startProgress);
    SecureCharSequence password = passwordGetter.getPassword();
    boolean success = false;
    final ArrayList<String> addressStrs = new ArrayList<String>();
    double progress = startProgress;
    double itemProgress = (1.0 - startProgress - saveProgress) / (double) targetCount;

    try {
        entropyCollector.start();
        PeerUtil.stopPeer();

        java.util.List<Address> addressList = new ArrayList<Address>();
        for (int i = 0;
             i < targetCount;
             i++) {
            if (cancelRunnable != null) {
                finishGenerate();
                SwingUtilities.invokeLater(cancelRunnable);
                return;
            }

            XRandom xRandom = new XRandom(entropyCollector);
            ECKey ecKey = ECKey.generateECKey(xRandom);
            ecKey.setFromXRandom(true);
            progress += itemProgress * progressKeyRate;
            onProgress(progress);
            if (cancelRunnable != null) {
                finishGenerate();
                SwingUtilities.invokeLater(cancelRunnable);

                return;
            }
            // start encrypt
            ecKey = PrivateKeyUtil.encrypt(ecKey, password);
            Address address = new Address(ecKey.toAddress(), ecKey.getPubKey(),
                    PrivateKeyUtil.getEncryptedString(ecKey), ecKey.isFromXRandom());
            ecKey.clearPrivateKey();
            addressList.add(address);
            addressStrs.add(address.getAddress());

            progress += itemProgress * progressEntryptRate;
            onProgress(progress);
        }
        entropyCollector.stop();
        passwordGetter.wipe();

        if (cancelRunnable != null) {
            finishGenerate();
            SwingUtilities.invokeLater(cancelRunnable);
            return;
        }
        KeyUtil.addAddressListByDesc(addressList);
        success = true;
    } catch (Exception e) {
        e.printStackTrace();
    }

    finishGenerate();
    if (success) {
        while (System.currentTimeMillis() - startGeneratingTime < MinGeneratingTime) {

        }
        onProgress(1);
        didSuccess(addressStrs);
    } else {
        onFailed();
    }
}
 
Example 8
Source File: PrivateKeyUEntropyActivity.java    From bither-android with Apache License 2.0 4 votes vote down vote up
@Override
public void runWithService(BlockchainService service) {
    boolean success = false;
    final ArrayList<String> addressStrs = new ArrayList<String>();
    double progress = startProgress;
    double itemProgress = (1.0 - startProgress - saveProgress) / (double) targetCount;

    try {
        entropyCollector.start();
        if (service != null) {
            service.stopAndUnregister();
        }

        XRandom xRandom = new XRandom(entropyCollector);

        List<Address> addressList = new ArrayList<Address>();
        for (int i = 0;
             i < targetCount;
             i++) {
            if (cancelRunnable != null) {
                finishGenerate(service);
                runOnUiThread(cancelRunnable);
                return;
            }

            ECKey ecKey = ECKey.generateECKey(xRandom);
            ecKey.setFromXRandom(true);

            progress += itemProgress * progressKeyRate;
            onProgress(progress);
            if (cancelRunnable != null) {
                finishGenerate(service);
                runOnUiThread(cancelRunnable);
                return;
            }


            // start encrypt
            ecKey = PrivateKeyUtil.encrypt(ecKey, password);
            Address address = new Address(ecKey.toAddress(), ecKey.getPubKey(),
                    PrivateKeyUtil.getEncryptedString(ecKey), true, ecKey.isFromXRandom());
            ecKey.clearPrivateKey();
            addressList.add(address);
            addressStrs.add(address.getAddress());

            progress += itemProgress * progressEntryptRate;
            onProgress(progress);
        }

        entropyCollector.stop();
        password.wipe();
        password = null;

        if (cancelRunnable != null) {
            finishGenerate(service);
            runOnUiThread(cancelRunnable);
            return;
        }

        KeyUtil.addAddressListByDesc(service, addressList);
        success = true;
    } catch (Exception e) {
        e.printStackTrace();
    }

    finishGenerate(service);
    if (success) {
        while (System.currentTimeMillis() - startGeneratingTime < MinGeneratingTime) {

        }
        onProgress(1);
        onSuccess(addressStrs);
    } else {
        onFailed();
    }
}
 
Example 9
Source File: ScriptBuilder.java    From bitherj with Apache License 2.0 4 votes vote down vote up
/**
 * Creates a scriptSig that can redeem a pay-to-address output.
 */
public static Script createInputScript(TransactionSignature signature, ECKey pubKey) {
    byte[] pubkeyBytes = pubKey.getPubKey();
    return new ScriptBuilder().data(signature.encodeToBitcoin()).data(pubkeyBytes).build();
}
 
Example 10
Source File: HDMIdTest.java    From bitherj with Apache License 2.0 4 votes vote down vote up
@Test
public void testCreateHDAddress() {
    try {
        HttpsTest.trust();
        ECKey ecKey = new DumpedPrivateKey("L4rK1yDtCWekvXuE6oXD9jCYfFNV2cWRpVuPLBcCU2z8TrisoyY1").getKey();
        String address = ecKey.toAddress();
        GetHDMBIdRandomApi getHDMBIdRandomApi = new GetHDMBIdRandomApi(address);
        getHDMBIdRandomApi.handleHttpGet();
        long randomKey = getHDMBIdRandomApi.getResult();
        byte[] decryptedPassword = new byte[32];
        for (int i = 0; i < decryptedPassword.length; i++) {
            decryptedPassword[i] = 0;
        }

        String message = Utils.format(HDMBId.BITID_STRING, address, Utils.bytesToHexString(decryptedPassword).toLowerCase(Locale.US), randomKey);
        byte[] hash = Utils.getPreSignMessage(message);
        byte[] signBytes = ecKey.signHash(hash, null);
        UploadHDMBidApi uploadHDMBidApi = new UploadHDMBidApi(address, address, signBytes, decryptedPassword);
        uploadHDMBidApi.handleHttpPost();
        boolean str = uploadHDMBidApi.getResult();
        HDMAddress.Pubs pubs = new HDMAddress.Pubs(ecKey.getPubKey(), ecKey.getPubKey(), null, 0);
        List<HDMAddress.Pubs> pubsList = new ArrayList<HDMAddress.Pubs>();
        pubsList.add(pubs);

        CreateHDMAddressApi createHDMAddressApi = new CreateHDMAddressApi(address, pubsList, decryptedPassword);
        createHDMAddressApi.handleHttpPost();


        List<byte[]> remotePubs = createHDMAddressApi.getResult();
        for (int i = 0;
             i < pubsList.size();
             i++) {
            HDMAddress.Pubs pubss = pubsList.get(i);
            pubss.remote = remotePubs.get(i);
            System.out.println("hot:" + Utils.bytesToHexString(pubss.hot));
            System.out.println("cold:" + Utils.bytesToHexString(pubss.cold));
            System.out.println("remote:" + Utils.bytesToHexString(pubss.remote));
            System.out.println("create,Address:" + pubss.getAddress());
        }

        List<byte[]> unsigns = new ArrayList<byte[]>();
        unsigns.add(Utils.doubleDigest(decryptedPassword));
        SignatureHDMApi signatureHDMApi = new SignatureHDMApi(address, 0, decryptedPassword, unsigns);
        signatureHDMApi.handleHttpPost();
        List<byte[]> bytesList = signatureHDMApi.getResult();

    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 11
Source File: PrivateKeyUEntropyDialog.java    From bither-desktop-java with Apache License 2.0 4 votes vote down vote up
@Override
public void run() {
    startGeneratingTime = System.currentTimeMillis();
    onProgress(startProgress);
    SecureCharSequence password = passwordGetter.getPassword();
    boolean success = false;
    final ArrayList<String> addressStrs = new ArrayList<String>();
    double progress = startProgress;
    double itemProgress = (1.0 - startProgress - saveProgress) / (double) targetCount;

    try {
        entropyCollector.start();
        PeerUtil.stopPeer();

        java.util.List<Address> addressList = new ArrayList<Address>();
        for (int i = 0;
             i < targetCount;
             i++) {
            if (cancelRunnable != null) {
                finishGenerate();
                SwingUtilities.invokeLater(cancelRunnable);
                return;
            }

            XRandom xRandom = new XRandom(entropyCollector);
            ECKey ecKey = ECKey.generateECKey(xRandom);
            ecKey.setFromXRandom(true);
            progress += itemProgress * progressKeyRate;
            onProgress(progress);
            if (cancelRunnable != null) {
                finishGenerate();
                SwingUtilities.invokeLater(cancelRunnable);

                return;
            }
            // start encrypt
            ecKey = PrivateKeyUtil.encrypt(ecKey, password);
            Address address = new Address(ecKey.toAddress(), ecKey.getPubKey(),
                    PrivateKeyUtil.getEncryptedString(ecKey), ecKey.isFromXRandom());
            ecKey.clearPrivateKey();
            addressList.add(address);
            addressStrs.add(address.getAddress());

            progress += itemProgress * progressEntryptRate;
            onProgress(progress);
        }
        entropyCollector.stop();
        passwordGetter.wipe();

        if (cancelRunnable != null) {
            finishGenerate();
            SwingUtilities.invokeLater(cancelRunnable);
            return;
        }
        KeyUtil.addAddressListByDesc(addressList);
        success = true;
    } catch (Exception e) {
        e.printStackTrace();
    }

    finishGenerate();
    if (success) {
        while (System.currentTimeMillis() - startGeneratingTime < MinGeneratingTime) {

        }
        onProgress(1);
        didSuccess(addressStrs);
    } else {
        onFailed();
    }
}