net.bither.bitherj.api.SignatureHDMApi Java Examples

The following examples show how to use net.bither.bitherj.api.SignatureHDMApi. 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: SendHDMBitcoinPanel.java    From bither-desktop-java with Apache License 2.0 4 votes vote down vote up
@Override
public List<TransactionSignature> getOtherSignature(int addressIndex,
                                                    CharSequence password,
                                                    List<byte[]> unsignHash, Tx tx) {
    List<TransactionSignature> transactionSignatureList = new
            ArrayList<TransactionSignature>();
    try {
        HDMBId hdmbId = HDMBId.getHDMBidFromDb();
        byte[] decryptedPassword = hdmbId.decryptHDMBIdPassword(password);
        SignatureHDMApi signatureHDMApi = new SignatureHDMApi(HDMBId.getHDMBidFromDb()
                .getAddress(), addressIndex, decryptedPassword, unsignHash);
        signatureHDMApi.handleHttpPost();
        List<byte[]> bytesList = signatureHDMApi.getResult();
        for (byte[] bytes : bytesList) {
            TransactionSignature transactionSignature = new TransactionSignature(ECKey
                    .ECDSASignature.decodeFromDER(bytes), TransactionSignature.SigHash
                    .ALL, false);
            transactionSignatureList.add(transactionSignature);
        }
    } catch (Exception e) {
        if (e instanceof Http400Exception) {
            if (((Http400Exception) e).getErrorCode() == HttpSetting.PasswordWrong) {
                toChangePassword = false;
                final ReentrantLock lock = new ReentrantLock();
                final Condition changePasswordCondition = lock.newCondition();
                SwingUtilities.invokeLater(new Runnable() {
                    @Override
                    public void run() {
                        dp.dispose();
                        DialogConfirmTask dialogConfirmTask = new DialogConfirmTask(LocaliserUtils.getString("hdm_reset_server_password_password_wrong_confirm"),
                                new Runnable() {
                                    @Override
                                    public void run() {
                                        toChangePassword = true;
                                        try {
                                            lock.lock();
                                            changePasswordCondition.signal();
                                        } finally {
                                            lock.unlock();
                                        }
                                    }
                                }, new Runnable() {
                            @Override
                            public void run() {
                                toChangePassword = false;
                                try {
                                    lock.lock();
                                    changePasswordCondition.signal();
                                } finally {
                                    lock.unlock();
                                }
                            }
                        });
                        dialogConfirmTask.pack();
                        dialogConfirmTask.setVisible(true);
                    }
                });
                try {
                    lock.lock();
                    changePasswordCondition.awaitUninterruptibly();
                } finally {
                    lock.unlock();
                }
                if (!toChangePassword) {
                    throw new CompleteTransactionRunnable.HDMSignUserCancelExcetion();
                }
                resetServerPasswordUtil.setPassword(password);
                if (!resetServerPasswordUtil.changePassword()) {
                    throw new CompleteTransactionRunnable.HDMSignUserCancelExcetion();
                }
                return getOtherSignature(addressIndex, password, unsignHash, tx);
            } else {
                throw new CompleteTransactionRunnable.HDMServerSignException(LocaliserUtils.getString(
                        "hdm_address_sign_tx_server_error"));
            }
        } else if (e instanceof KeyCrypterException) {
            throw new PasswordException("hdm password decrypting error");
        } else {
            throw new RuntimeException(e);
        }
    }

    return transactionSignatureList;
}
 
Example #2
Source File: HdmSendActivity.java    From bither-android with Apache License 2.0 4 votes vote down vote up
@Override
public List<TransactionSignature> getOtherSignature(int addressIndex,
                                                    CharSequence password,
                                                    List<byte[]> unsignHash, Tx tx) {
    List<TransactionSignature> transactionSignatureList = new
            ArrayList<TransactionSignature>();
    try {
        HDMBId hdmbId = HDMBId.getHDMBidFromDb();
        byte[] decryptedPassword = hdmbId.decryptHDMBIdPassword(password);
        SignatureHDMApi signatureHDMApi = new SignatureHDMApi(HDMBId.getHDMBidFromDb()
                .getAddress(), addressIndex, decryptedPassword, unsignHash);
        signatureHDMApi.handleHttpPost();
        List<byte[]> bytesList = signatureHDMApi.getResult();
        for (byte[] bytes : bytesList) {
            TransactionSignature transactionSignature = new TransactionSignature(ECKey
                    .ECDSASignature.decodeFromDER(bytes), TransactionSignature.SigHash
                    .ALL, false);
            transactionSignatureList.add(transactionSignature);
        }
    } catch (Exception e) {
        if (e instanceof Http400Exception) {
            if (((Http400Exception) e).getErrorCode() == HttpSetting.PasswordWrong) {
                toChangePassword = false;
                final ReentrantLock lock = new ReentrantLock();
                final Condition changePasswordCondition = lock.newCondition();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (dp.isShowing()) {
                            dp.dismiss();
                        }
                        new DialogConfirmTask(HdmSendActivity.this, getString(R.string.hdm_reset_server_password_password_wrong_confirm),
                                new Runnable() {
                                    @Override
                                    public void run() {
                                        toChangePassword = true;
                                        try {
                                            lock.lock();
                                            changePasswordCondition.signal();
                                        } finally {
                                            lock.unlock();
                                        }
                                    }
                                }, new Runnable() {
                            @Override
                            public void run() {
                                toChangePassword = false;
                                try {
                                    lock.lock();
                                    changePasswordCondition.signal();
                                } finally {
                                    lock.unlock();
                                }
                            }
                        }).show();
                    }
                });
                try {
                    lock.lock();
                    changePasswordCondition.awaitUninterruptibly();
                } finally {
                    lock.unlock();
                }
                if (!toChangePassword) {
                    throw new CompleteTransactionRunnable.HDMSignUserCancelExcetion();
                }
                resetServerPasswordUtil.setPassword(password);
                if (!resetServerPasswordUtil.changePassword()) {
                    throw new CompleteTransactionRunnable.HDMSignUserCancelExcetion();
                }
                return getOtherSignature(addressIndex, password, unsignHash, tx);
            } else {
                throw new CompleteTransactionRunnable.HDMServerSignException(R.string
                        .hdm_address_sign_tx_server_error);
            }
        } else if (e instanceof KeyCrypterException) {
            throw new PasswordException("hdm password decrypting error");
        } else {
            throw new RuntimeException(e);
        }
    }

    return transactionSignatureList;
}
 
Example #3
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 #4
Source File: SendHDMBitcoinPanel.java    From bither-desktop-java with Apache License 2.0 4 votes vote down vote up
@Override
public List<TransactionSignature> getOtherSignature(int addressIndex,
                                                    CharSequence password,
                                                    List<byte[]> unsignHash, Tx tx) {
    List<TransactionSignature> transactionSignatureList = new
            ArrayList<TransactionSignature>();
    try {
        HDMBId hdmbId = HDMBId.getHDMBidFromDb();
        byte[] decryptedPassword = hdmbId.decryptHDMBIdPassword(password);
        SignatureHDMApi signatureHDMApi = new SignatureHDMApi(HDMBId.getHDMBidFromDb()
                .getAddress(), addressIndex, decryptedPassword, unsignHash);
        signatureHDMApi.handleHttpPost();
        List<byte[]> bytesList = signatureHDMApi.getResult();
        for (byte[] bytes : bytesList) {
            TransactionSignature transactionSignature = new TransactionSignature(ECKey
                    .ECDSASignature.decodeFromDER(bytes), TransactionSignature.SigHash
                    .ALL, false);
            transactionSignatureList.add(transactionSignature);
        }
    } catch (Exception e) {
        if (e instanceof Http400Exception) {
            if (((Http400Exception) e).getErrorCode() == HttpSetting.PasswordWrong) {
                toChangePassword = false;
                final ReentrantLock lock = new ReentrantLock();
                final Condition changePasswordCondition = lock.newCondition();
                SwingUtilities.invokeLater(new Runnable() {
                    @Override
                    public void run() {
                        dp.dispose();
                        DialogConfirmTask dialogConfirmTask = new DialogConfirmTask(LocaliserUtils.getString("hdm_reset_server_password_password_wrong_confirm"),
                                new Runnable() {
                                    @Override
                                    public void run() {
                                        toChangePassword = true;
                                        try {
                                            lock.lock();
                                            changePasswordCondition.signal();
                                        } finally {
                                            lock.unlock();
                                        }
                                    }
                                }, new Runnable() {
                            @Override
                            public void run() {
                                toChangePassword = false;
                                try {
                                    lock.lock();
                                    changePasswordCondition.signal();
                                } finally {
                                    lock.unlock();
                                }
                            }
                        });
                        dialogConfirmTask.pack();
                        dialogConfirmTask.setVisible(true);
                    }
                });
                try {
                    lock.lock();
                    changePasswordCondition.awaitUninterruptibly();
                } finally {
                    lock.unlock();
                }
                if (!toChangePassword) {
                    throw new CompleteTransactionRunnable.HDMSignUserCancelExcetion();
                }
                resetServerPasswordUtil.setPassword(password);
                if (!resetServerPasswordUtil.changePassword()) {
                    throw new CompleteTransactionRunnable.HDMSignUserCancelExcetion();
                }
                return getOtherSignature(addressIndex, password, unsignHash, tx);
            } else {
                throw new CompleteTransactionRunnable.HDMServerSignException(LocaliserUtils.getString(
                        "hdm_address_sign_tx_server_error"));
            }
        } else if (e instanceof KeyCrypterException) {
            throw new PasswordException("hdm password decrypting error");
        } else {
            throw new RuntimeException(e);
        }
    }

    return transactionSignatureList;
}