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

The following examples show how to use net.bither.bitherj.crypto.ECKey#toAddress() . 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: 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 3
Source File: HDMBId.java    From bitherj with Apache License 2.0 6 votes vote down vote up
public String setSignatureAndGetAddressOfAddressOfSp(byte[] signed, CharSequence password, String firstHotAddress) throws Exception {
    String addressOfSP = null;
    String message = getBitidString();
    byte[] hash = Utils.getPreSignMessage(message);
    ECKey key = ECKey.signedMessageToKey(hash, signed);
    if (Utils.compareString(address, key.toAddress())) {
        throw new SignatureException();

    }
    String hotAddress = firstHotAddress != null ? firstHotAddress : AddressManager.getInstance().getHdmKeychain().getFirstAddressFromDb();
    UploadHDMBidApi uploadHDMBidApi = new UploadHDMBidApi(address, hotAddress, signed, decryptedPassword);
    uploadHDMBidApi.handleHttpPost();
    boolean result = uploadHDMBidApi.getResult();
    if (result) {
        encryptedBitherPassword = new EncryptedData(decryptedPassword, password);
        ECKey k = new ECKey(decryptedPassword, null);
        addressOfSP = k.toAddress();
        k.clearPrivateKey();
        if (firstHotAddress == null) {
            save(addressOfSP);
        }
    } else {
        throw new HttpException("UploadHDMBidApi error");
    }
    return addressOfSP;
}
 
Example 4
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 5
Source File: ImportPrivateKeyAndroid.java    From bither-android with Apache License 2.0 5 votes vote down vote up
public void importPrivateKey() {
    if (importPrivateKeyType != ImportPrivateKeyType.Text) {
        Address address = initPrivateKey();
        importPrivateKey(address);
        return;
    }

    boolean isCompressed = getIsCompressed();
    final ECKey compressKey = initEcKey(true);
    if (compressKey == null) {
        return;
    }
    final ECKey uncompressedKey = initEcKey(false);
    if (uncompressedKey == null) {
        return;
    }
    DialogImportPrivateKeyAddressValidation dialogImportPrivateKeyAddressValidation = new DialogImportPrivateKeyAddressValidation(activity, compressKey.toAddress(), uncompressedKey.toAddress(), isCompressed, new Runnable() {
        @Override
        public void run() {
            Address compressAddress = initPrivateKey(true);
            if (compressAddress != null) {
                importPrivateKey(compressAddress);
            }
        }
    }, new Runnable() {
        @Override
        public void run() {
            Address uncompressedAddress = initPrivateKey(false);
            if (uncompressedAddress != null) {
                importPrivateKey(uncompressedAddress);
            }
        }
    });
    dialogImportPrivateKeyAddressValidation.show();
}
 
Example 6
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 7
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 8
Source File: PrivateKeyUtil.java    From bitherj with Apache License 2.0 5 votes vote down vote up
public static boolean verifyMessage(String address, String messageText, String signatureText) {
    // Strip CRLF from signature text
    try {
        signatureText = signatureText.replaceAll("\n", "").replaceAll("\r", "");

        ECKey key = ECKey.signedMessageToKey(messageText, signatureText);
        String signAddress = key.toAddress();
        return Utils.compareString(address, signAddress);
    } catch (SignatureException e) {
        e.printStackTrace();
        return false;
    }

}
 
Example 9
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 10
Source File: DesktopHDMKeychain.java    From bitherj with Apache License 2.0 5 votes vote down vote up
public DesktopHDMKeychain(EncryptedData encryptedMnemonicSeed, CharSequence password) throws
        HDMBitherIdNotMatchException, MnemonicException.MnemonicLengthException {
    mnemonicSeed = encryptedMnemonicSeed.decrypt(password);
    hdSeed = seedFromMnemonic(mnemonicSeed);
    isFromXRandom = encryptedMnemonicSeed.isXRandom();
    EncryptedData encryptedHDSeed = new EncryptedData(hdSeed, password, isFromXRandom);
    ArrayList<DesktopHDMAddress> as = new ArrayList<DesktopHDMAddress>();
    ArrayList<HDMAddress.Pubs> uncompPubs = new ArrayList<HDMAddress.Pubs>();

    ECKey k = new ECKey(mnemonicSeed, null);
    String address = k.toAddress();
    k.clearPrivateKey();
    String firstAddress = getFirstAddressFromSeed(password);
    wipeMnemonicSeed();
    wipeHDSeed();

    this.hdSeedId = AbstractDb.desktopAddressProvider.addHDKey(encryptedMnemonicSeed
                    .toEncryptedString(), encryptedHDSeed.toEncryptedString(), firstAddress,
            isFromXRandom, address, null, null);
    if (as.size() > 0) {
        //   EnDesktopAddressProvider.getInstance().completeHDMAddresses(getHdSeedId(), as);

        if (uncompPubs.size() > 0) {
            //  EnDesktopAddressProvider.getInstance().prepareHDMAddresses(getHdSeedId(), uncompPubs);
            for (HDMAddress.Pubs p : uncompPubs) {
                AbstractDb.addressProvider.setHDMPubsRemote(getHdSeedId(), p.index, p.remote);
            }
        }
    }
}
 
Example 11
Source File: HDMKeychain.java    From bitherj with Apache License 2.0 5 votes vote down vote up
public PasswordSeed createPasswordSeed(CharSequence password) {
    if (isInRecovery()) {
        throw new AssertionError("HDM in recovery can not create passwordSeed");
    }
    String encrypted = AbstractDb.addressProvider.getEncryptMnemonicSeed(hdSeedId);
    byte[] priv = new EncryptedData(encrypted).decrypt(password);
    ECKey k = new ECKey(priv, null);
    String address = k.toAddress();
    Utils.wipeBytes(priv);
    k.clearPrivateKey();
    return new PasswordSeed(address, encrypted);
}
 
Example 12
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 13
Source File: AddressTest.java    From bitherj with Apache License 2.0 5 votes vote down vote up
@Test
public void testAddress() {
    try {
        DumpedPrivateKey dumpedPrivateKey = new DumpedPrivateKey("L4rK1yDtCWekvXuE6oXD9jCYfFNV2cWRpVuPLBcCU2z8TrisoyY1");
        ECKey ecKey = dumpedPrivateKey.getKey();
        String addressStr = ecKey.toAddress();
        assertEquals(ecKey.toAddress(), "1F3sAm6ZtwLAUnj7d38pGFxtP3RVEvtsbV");
    } catch (AddressFormatException e) {
        e.printStackTrace();
    }
}
 
Example 14
Source File: HDMIdTest.java    From bitherj with Apache License 2.0 5 votes vote down vote up
@Test
public void testRecoveryHDM() {
    try {
        HttpsTest.trust();
        ECKey ecKey = new DumpedPrivateKey("L4rK1yDtCWekvXuE6oXD9jCYfFNV2cWRpVuPLBcCU2z8TrisoyY1").getKey();
        String address = ecKey.toAddress();
        System.out.println("eckey:" + address);
        byte[] decryptedPassword = new byte[32];
        for (int i = 0; i < decryptedPassword.length; i++) {
            decryptedPassword[i] = 0;
        }
        GetHDMBIdRandomApi getHDMBIdRandomApi = new GetHDMBIdRandomApi(address);
        getHDMBIdRandomApi.handleHttpGet();
        long randomKey = getHDMBIdRandomApi.getResult();
        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);
        RecoveryHDMApi recoveryHDMApi = new RecoveryHDMApi(address, signBytes, decryptedPassword);
        recoveryHDMApi.handleHttpPost();
        List<HDMAddress.Pubs> pubses = recoveryHDMApi.getResult();
        for (HDMAddress.Pubs pubs : pubses) {
            System.out.println("hot:" + Utils.bytesToHexString(pubs.hot));
            System.out.println("cold:" + Utils.bytesToHexString(pubs.cold));
            System.out.println("remote:" + Utils.bytesToHexString(pubs.remote));
            System.out.println("address:" + pubs.getAddress());
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

}
 
Example 15
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 16
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 17
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 18
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();
    }
}