use of com.samourai.wallet.crypto.DecryptionException in project samourai-wallet-android by Samourai-Wallet.
the class PayNymHome method getPcodeSyncObservable.
private Observable<String> getPcodeSyncObservable(String pcode) {
return Observable.fromCallable(() -> {
try {
PaymentCode payment_code = new PaymentCode(pcode);
int idx = 0;
boolean loop = true;
ArrayList<String> addrs = new ArrayList<String>();
while (loop) {
addrs.clear();
for (int i = idx; i < (idx + 20); i++) {
// Log.i("BIP47Activity", "sync receive from " + i + ":" + BIP47Util.getInstance(BIP47Activity.this).getReceivePubKey(payment_code, i));
BIP47Meta.getInstance().getIdx4AddrLookup().put(BIP47Util.getInstance(this).getReceivePubKey(payment_code, i), i);
BIP47Meta.getInstance().getPCode4AddrLookup().put(BIP47Util.getInstance(this).getReceivePubKey(payment_code, i), payment_code.toString());
addrs.add(BIP47Util.getInstance(this).getReceivePubKey(payment_code, i));
// Log.i("BIP47Activity", "p2pkh " + i + ":" + BIP47Util.getInstance(BIP47Activity.this).getReceiveAddress(payment_code, i).getReceiveECKey().toAddress(SamouraiWallet.getInstance().getCurrentNetworkParams()).toString());
}
String[] s = addrs.toArray(new String[addrs.size()]);
int nb = APIFactory.getInstance(getApplicationContext()).syncBIP47Incoming(s);
// Log.i("BIP47Activity", "sync receive idx:" + idx + ", nb == " + nb);
if (nb == 0) {
loop = false;
}
idx += 20;
}
idx = 0;
loop = true;
BIP47Meta.getInstance().setOutgoingIdx(pcode, 0);
while (loop) {
addrs.clear();
for (int i = idx; i < (idx + 20); i++) {
PaymentAddress sendAddress = BIP47Util.getInstance(this).getSendAddress(payment_code, i);
// Log.i("BIP47Activity", "sync send to " + i + ":" + sendAddress.getSendECKey().toAddress(SamouraiWallet.getInstance().getCurrentNetworkParams()).toString());
// BIP47Meta.getInstance().setOutgoingIdx(payment_code.toString(), i);
BIP47Meta.getInstance().getIdx4AddrLookup().put(BIP47Util.getInstance(this).getSendPubKey(payment_code, i), i);
BIP47Meta.getInstance().getPCode4AddrLookup().put(BIP47Util.getInstance(this).getSendPubKey(payment_code, i), payment_code.toString());
addrs.add(BIP47Util.getInstance(this).getSendPubKey(payment_code, i));
}
String[] s = addrs.toArray(new String[addrs.size()]);
int nb = APIFactory.getInstance(getApplicationContext()).syncBIP47Outgoing(s);
// Log.i("BIP47Activity", "sync send idx:" + idx + ", nb == " + nb);
if (nb == 0) {
loop = false;
}
idx += 20;
}
BIP47Meta.getInstance().pruneIncoming();
PayloadUtil.getInstance(this).saveWalletToJSON(new CharSequenceX(AccessFactory.getInstance(this).getGUID() + AccessFactory.getInstance(this).getPIN()));
} catch (IOException ioe) {
;
} catch (JSONException je) {
;
} catch (DecryptionException de) {
;
} catch (NotSecp256k1Exception nse) {
;
} catch (InvalidKeySpecException ikse) {
;
} catch (InvalidKeyException ike) {
;
} catch (NoSuchAlgorithmException nsae) {
;
} catch (NoSuchProviderException nspe) {
;
} catch (MnemonicException.MnemonicLengthException mle) {
;
} catch (Exception ex) {
}
return pcode;
});
}
use of com.samourai.wallet.crypto.DecryptionException in project samourai-wallet-android by Samourai-Wallet.
the class APIFactory method parseXPUB.
private synchronized boolean parseXPUB(JSONObject jsonObject) throws JSONException {
if (jsonObject != null) {
HashMap<String, Integer> pubkeys = new HashMap<String, Integer>();
if (jsonObject.has("wallet")) {
JSONObject walletObj = (JSONObject) jsonObject.get("wallet");
if (walletObj.has("final_balance")) {
xpub_balance = walletObj.getLong("final_balance");
debug("APIFactory", "xpub_balance:" + xpub_balance);
}
}
if (jsonObject.has("info")) {
JSONObject infoObj = (JSONObject) jsonObject.get("info");
if (infoObj.has("latest_block")) {
JSONObject blockObj = (JSONObject) infoObj.get("latest_block");
if (blockObj.has("height")) {
latest_block_height = blockObj.getLong("height");
}
if (blockObj.has("hash")) {
latest_block_hash = blockObj.getString("hash");
}
}
}
if (jsonObject.has("addresses")) {
JSONArray addressesArray = (JSONArray) jsonObject.get("addresses");
JSONObject addrObj = null;
for (int i = 0; i < addressesArray.length(); i++) {
addrObj = (JSONObject) addressesArray.get(i);
if (addrObj != null && addrObj.has("final_balance") && addrObj.has("address")) {
if (FormatsUtil.getInstance().isValidXpub((String) addrObj.get("address"))) {
xpub_amounts.put((String) addrObj.get("address"), addrObj.getLong("final_balance"));
if (addrObj.getString("address").equals(BIP84Util.getInstance(context).getWallet().getAccount(0).xpubstr()) || addrObj.getString("address").equals(BIP84Util.getInstance(context).getWallet().getAccount(0).zpubstr())) {
AddressFactory.getInstance().setHighestBIP84ReceiveIdx(addrObj.has("account_index") ? addrObj.getInt("account_index") : 0);
AddressFactory.getInstance().setHighestBIP84ChangeIdx(addrObj.has("change_index") ? addrObj.getInt("change_index") : 0);
BIP84Util.getInstance(context).getWallet().getAccount(0).getChain(0).setAddrIdx(addrObj.has("account_index") ? addrObj.getInt("account_index") : 0);
BIP84Util.getInstance(context).getWallet().getAccount(0).getChain(1).setAddrIdx(addrObj.has("change_index") ? addrObj.getInt("change_index") : 0);
} else if (addrObj.getString("address").equals(BIP49Util.getInstance(context).getWallet().getAccount(0).xpubstr()) || addrObj.getString("address").equals(BIP49Util.getInstance(context).getWallet().getAccount(0).ypubstr())) {
AddressFactory.getInstance().setHighestBIP49ReceiveIdx(addrObj.has("account_index") ? addrObj.getInt("account_index") : 0);
AddressFactory.getInstance().setHighestBIP49ChangeIdx(addrObj.has("change_index") ? addrObj.getInt("change_index") : 0);
BIP49Util.getInstance(context).getWallet().getAccount(0).getChain(0).setAddrIdx(addrObj.has("account_index") ? addrObj.getInt("account_index") : 0);
BIP49Util.getInstance(context).getWallet().getAccount(0).getChain(1).setAddrIdx(addrObj.has("change_index") ? addrObj.getInt("change_index") : 0);
} else if (AddressFactory.getInstance().xpub2account().get((String) addrObj.get("address")) != null) {
AddressFactory.getInstance().setHighestTxReceiveIdx(AddressFactory.getInstance().xpub2account().get((String) addrObj.get("address")), addrObj.has("account_index") ? addrObj.getInt("account_index") : 0);
AddressFactory.getInstance().setHighestTxChangeIdx(AddressFactory.getInstance().xpub2account().get((String) addrObj.get("address")), addrObj.has("change_index") ? addrObj.getInt("change_index") : 0);
try {
HD_WalletFactory.getInstance(context).get().getAccount(0).getChain(0).setAddrIdx(addrObj.has("account_index") ? addrObj.getInt("account_index") : 0);
HD_WalletFactory.getInstance(context).get().getAccount(0).getChain(1).setAddrIdx(addrObj.has("change_index") ? addrObj.getInt("change_index") : 0);
} catch (IOException | MnemonicException.MnemonicLengthException e) {
;
}
} else {
;
}
} else {
long amount = 0L;
String addr = null;
addr = (String) addrObj.get("address");
amount = addrObj.getLong("final_balance");
String pcode = BIP47Meta.getInstance().getPCode4Addr(addr);
if (addrObj.has("pubkey")) {
bip47Lookahead(pcode, addrObj.getString("pubkey"));
}
if (addr != null && addr.length() > 0 && pcode != null && pcode.length() > 0 && BIP47Meta.getInstance().getIdx4Addr(addr) != null) {
int idx = BIP47Meta.getInstance().getIdx4Addr(addr);
if (amount > 0L) {
BIP47Meta.getInstance().addUnspent(pcode, idx);
if (idx > BIP47Meta.getInstance().getIncomingIdx(pcode)) {
BIP47Meta.getInstance().setIncomingIdx(pcode, idx);
}
} else {
if (addrObj.has("pubkey")) {
String pubkey = addrObj.getString("pubkey");
if (pubkeys.containsKey(pubkey)) {
int count = pubkeys.get(pubkey);
count++;
if (count == BIP47Meta.INCOMING_LOOKAHEAD) {
BIP47Meta.getInstance().removeUnspent(pcode, Integer.valueOf(idx));
} else {
pubkeys.put(pubkey, count + 1);
}
} else {
pubkeys.put(pubkey, 1);
}
} else {
BIP47Meta.getInstance().removeUnspent(pcode, Integer.valueOf(idx));
}
}
if (addr != null) {
bip47_amounts.put(addr, amount);
}
}
}
}
}
}
if (jsonObject.has("txs")) {
List<String> seenHashes = new ArrayList<String>();
JSONArray txArray = (JSONArray) jsonObject.get("txs");
JSONObject txObj = null;
for (int i = 0; i < txArray.length(); i++) {
txObj = (JSONObject) txArray.get(i);
long height = 0L;
long amount = 0L;
long ts = 0L;
String hash = null;
String addr = null;
String _addr = null;
if (txObj.has("block_height")) {
height = txObj.getLong("block_height");
} else {
// 0 confirmations
height = -1L;
}
if (txObj.has("hash")) {
hash = (String) txObj.get("hash");
}
if (txObj.has("result")) {
amount = txObj.getLong("result");
}
if (txObj.has("time")) {
ts = txObj.getLong("time");
}
if (!seenHashes.contains(hash)) {
seenHashes.add(hash);
}
if (txObj.has("inputs")) {
JSONArray inputArray = (JSONArray) txObj.get("inputs");
JSONObject inputObj = null;
for (int j = 0; j < inputArray.length(); j++) {
inputObj = (JSONObject) inputArray.get(j);
if (inputObj.has("prev_out")) {
JSONObject prevOutObj = (JSONObject) inputObj.get("prev_out");
if (prevOutObj.has("xpub")) {
JSONObject xpubObj = (JSONObject) prevOutObj.get("xpub");
addr = (String) xpubObj.get("m");
} else if (prevOutObj.has("addr") && BIP47Meta.getInstance().getPCode4Addr((String) prevOutObj.get("addr")) != null) {
_addr = (String) prevOutObj.get("addr");
} else {
_addr = (String) prevOutObj.get("addr");
}
}
}
}
if (txObj.has("out")) {
JSONArray outArray = (JSONArray) txObj.get("out");
JSONObject outObj = null;
for (int j = 0; j < outArray.length(); j++) {
outObj = (JSONObject) outArray.get(j);
if (outObj.has("xpub")) {
JSONObject xpubObj = (JSONObject) outObj.get("xpub");
addr = (String) xpubObj.get("m");
} else {
_addr = (String) outObj.get("addr");
}
}
}
if (addr != null || _addr != null) {
if (addr == null) {
addr = _addr;
}
Tx tx = new Tx(hash, addr, amount, ts, (latest_block_height > 0L && height > 0L) ? (latest_block_height - height) + 1 : 0);
if (SentToFromBIP47Util.getInstance().getByHash(hash) != null) {
tx.setPaymentCode(SentToFromBIP47Util.getInstance().getByHash(hash));
}
if (BIP47Meta.getInstance().getPCode4Addr(addr) != null) {
tx.setPaymentCode(BIP47Meta.getInstance().getPCode4Addr(addr));
}
if (!xpub_txs.containsKey(addr)) {
xpub_txs.put(addr, new ArrayList<Tx>());
}
if (FormatsUtil.getInstance().isValidXpub(addr)) {
xpub_txs.get(addr).add(tx);
} else {
xpub_txs.get(AddressFactory.getInstance().account2xpub().get(0)).add(tx);
}
if (height > 0L) {
RBFUtil.getInstance().remove(hash);
}
}
}
List<String> hashesSentToViaBIP47 = SentToFromBIP47Util.getInstance().getAllHashes();
if (hashesSentToViaBIP47.size() > 0) {
for (String s : hashesSentToViaBIP47) {
if (!seenHashes.contains(s)) {
SentToFromBIP47Util.getInstance().removeHash(s);
}
}
}
}
try {
PayloadUtil.getInstance(context).serializeMultiAddr(jsonObject);
} catch (IOException | DecryptionException e) {
;
}
return true;
}
return false;
}
use of com.samourai.wallet.crypto.DecryptionException in project samourai-wallet-android by Samourai-Wallet.
the class APIFactory method parseDynamicFees_bitcoind.
private synchronized boolean parseDynamicFees_bitcoind(JSONObject jsonObject) throws JSONException {
if (jsonObject != null) {
//
// bitcoind
//
List<SuggestedFee> suggestedFees = new ArrayList<SuggestedFee>();
if (jsonObject.has("2")) {
long fee = jsonObject.getInt("2");
SuggestedFee suggestedFee = new SuggestedFee();
suggestedFee.setDefaultPerKB(BigInteger.valueOf(fee * 1000L));
suggestedFee.setStressed(false);
suggestedFee.setOK(true);
suggestedFees.add(suggestedFee);
}
if (jsonObject.has("6")) {
long fee = jsonObject.getInt("6");
SuggestedFee suggestedFee = new SuggestedFee();
suggestedFee.setDefaultPerKB(BigInteger.valueOf(fee * 1000L));
suggestedFee.setStressed(false);
suggestedFee.setOK(true);
suggestedFees.add(suggestedFee);
}
if (jsonObject.has("24")) {
long fee = jsonObject.getInt("24");
SuggestedFee suggestedFee = new SuggestedFee();
suggestedFee.setDefaultPerKB(BigInteger.valueOf(fee * 1000L));
suggestedFee.setStressed(false);
suggestedFee.setOK(true);
suggestedFees.add(suggestedFee);
}
if (suggestedFees.size() > 0) {
FeeUtil.getInstance().setEstimatedFees(suggestedFees);
// debug("APIFactory", "high fee:" + FeeUtil.getInstance().getHighFee().getDefaultPerKB().toString());
// debug("APIFactory", "suggested fee:" + FeeUtil.getInstance().getSuggestedFee().getDefaultPerKB().toString());
// debug("APIFactory", "low fee:" + FeeUtil.getInstance().getLowFee().getDefaultPerKB().toString());
}
try {
PayloadUtil.getInstance(context).serializeFees(jsonObject);
} catch (IOException | DecryptionException e) {
;
}
return true;
}
return false;
}
use of com.samourai.wallet.crypto.DecryptionException in project samourai-wallet-android by Samourai-Wallet.
the class BalanceActivity method doBackup.
private void doBackup() {
try {
final String passphrase = HD_WalletFactory.getInstance(BalanceActivity.this).get().getPassphrase();
final String[] export_methods = new String[2];
export_methods[0] = getString(R.string.export_to_clipboard);
export_methods[1] = getString(R.string.export_to_email);
new AlertDialog.Builder(BalanceActivity.this).setTitle(R.string.options_export).setSingleChoiceItems(export_methods, 0, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
try {
PayloadUtil.getInstance(BalanceActivity.this).saveWalletToJSON(new CharSequenceX(AccessFactory.getInstance(BalanceActivity.this).getGUID() + AccessFactory.getInstance(BalanceActivity.this).getPIN()));
} catch (IOException ioe) {
;
} catch (JSONException je) {
;
} catch (DecryptionException de) {
;
} catch (MnemonicException.MnemonicLengthException mle) {
;
}
String encrypted = null;
try {
encrypted = AESUtil.encrypt(PayloadUtil.getInstance(BalanceActivity.this).getPayload().toString(), new CharSequenceX(passphrase), AESUtil.DefaultPBKDF2Iterations);
} catch (Exception e) {
Toast.makeText(BalanceActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
} finally {
if (encrypted == null) {
Toast.makeText(BalanceActivity.this, R.string.encryption_error, Toast.LENGTH_SHORT).show();
return;
}
}
JSONObject obj = PayloadUtil.getInstance(BalanceActivity.this).putPayload(encrypted, true);
if (which == 0) {
android.content.ClipboardManager clipboard = (android.content.ClipboardManager) getSystemService(android.content.Context.CLIPBOARD_SERVICE);
android.content.ClipData clip = null;
clip = android.content.ClipData.newPlainText("Wallet backup", obj.toString());
clipboard.setPrimaryClip(clip);
Toast.makeText(BalanceActivity.this, R.string.copied_to_clipboard, Toast.LENGTH_SHORT).show();
} else {
Intent email = new Intent(Intent.ACTION_SEND);
email.putExtra(Intent.EXTRA_SUBJECT, "Samourai Wallet backup");
email.putExtra(Intent.EXTRA_TEXT, obj.toString());
email.setType("message/rfc822");
startActivity(Intent.createChooser(email, BalanceActivity.this.getText(R.string.choose_email_client)));
}
dialog.dismiss();
}
}).show();
} catch (IOException ioe) {
ioe.printStackTrace();
Toast.makeText(BalanceActivity.this, "HD wallet error", Toast.LENGTH_SHORT).show();
} catch (MnemonicException.MnemonicLengthException mle) {
mle.printStackTrace();
Toast.makeText(BalanceActivity.this, "HD wallet error", Toast.LENGTH_SHORT).show();
}
}
use of com.samourai.wallet.crypto.DecryptionException in project samourai-wallet-android by Samourai-Wallet.
the class BalanceActivity method onBackPressed.
@Override
public void onBackPressed() {
if (account == 0) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(R.string.ask_you_sure_exit);
AlertDialog alert = builder.create();
alert.setButton(AlertDialog.BUTTON_POSITIVE, getString(R.string.yes), (dialog, id) -> {
try {
PayloadUtil.getInstance(BalanceActivity.this).saveWalletToJSON(new CharSequenceX(AccessFactory.getInstance(BalanceActivity.this).getGUID() + AccessFactory.getInstance(BalanceActivity.this).getPIN()));
} catch (MnemonicException.MnemonicLengthException mle) {
} catch (JSONException je) {
} catch (IOException ioe) {
} catch (DecryptionException de) {
}
// disconnect Whirlpool on app back key exit
WhirlpoolNotificationService.stopService(getApplicationContext());
if (TorManager.getInstance(getApplicationContext()).isRequired()) {
Intent startIntent = new Intent(getApplicationContext(), TorService.class);
startIntent.setAction(TorService.STOP_SERVICE);
startIntent.putExtra("KILL_TOR", true);
startService(startIntent);
}
TimeOutUtil.getInstance().reset();
finishAffinity();
finish();
super.onBackPressed();
});
alert.setButton(AlertDialog.BUTTON_NEGATIVE, getString(R.string.no), (dialog, id) -> dialog.dismiss());
alert.show();
} else {
super.onBackPressed();
}
}
Aggregations