use of com.samourai.wallet.crypto.DecryptionException in project samourai-wallet-android by Samourai-Wallet.
the class PayNymDetailsActivity method onDestroy.
@Override
protected void onDestroy() {
disposables.dispose();
try {
PayloadUtil.getInstance(getApplicationContext()).saveWalletToJSON(new CharSequenceX(AccessFactory.getInstance(getApplicationContext()).getGUID() + AccessFactory.getInstance(getApplicationContext()).getPIN()));
} catch (MnemonicException.MnemonicLengthException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
} catch (DecryptionException e) {
e.printStackTrace();
}
super.onDestroy();
}
use of com.samourai.wallet.crypto.DecryptionException in project samourai-wallet-android by Samourai-Wallet.
the class PayNymDetailsActivity method updatePaynym.
private void updatePaynym(String label, String pcode) {
if (pcode == null || pcode.length() < 1 || !FormatsUtil.getInstance().isValidPaymentCode(pcode)) {
Snackbar.make(userAvatar.getRootView(), R.string.invalid_payment_code, Snackbar.LENGTH_SHORT).show();
} else if (label == null || label.length() < 1) {
Snackbar.make(userAvatar.getRootView(), R.string.bip47_no_label_error, Snackbar.LENGTH_SHORT).show();
} else {
BIP47Meta.getInstance().setLabel(pcode, label);
new Thread(() -> {
Looper.prepare();
try {
PayloadUtil.getInstance(PayNymDetailsActivity.this).saveWalletToJSON(new CharSequenceX(AccessFactory.getInstance(PayNymDetailsActivity.this).getGUID() + AccessFactory.getInstance().getPIN()));
} catch (MnemonicException.MnemonicLengthException mle) {
mle.printStackTrace();
Toast.makeText(PayNymDetailsActivity.this, R.string.decryption_error, Toast.LENGTH_SHORT).show();
} catch (DecoderException de) {
de.printStackTrace();
Toast.makeText(PayNymDetailsActivity.this, R.string.decryption_error, Toast.LENGTH_SHORT).show();
} catch (JSONException je) {
je.printStackTrace();
Toast.makeText(PayNymDetailsActivity.this, R.string.decryption_error, Toast.LENGTH_SHORT).show();
} catch (IOException ioe) {
ioe.printStackTrace();
Toast.makeText(PayNymDetailsActivity.this, R.string.decryption_error, Toast.LENGTH_SHORT).show();
} catch (NullPointerException npe) {
npe.printStackTrace();
Toast.makeText(PayNymDetailsActivity.this, R.string.decryption_error, Toast.LENGTH_SHORT).show();
} catch (DecryptionException de) {
de.printStackTrace();
Toast.makeText(PayNymDetailsActivity.this, R.string.decryption_error, Toast.LENGTH_SHORT).show();
} finally {
;
}
Looper.loop();
doUpdatePayNymInfo(pcode);
}).start();
}
}
use of com.samourai.wallet.crypto.DecryptionException in project samourai-wallet-android by Samourai-Wallet.
the class BalanceViewModel method parseXPUB.
private Observable<Boolean> parseXPUB(JSONObject jsonObject) throws JSONException {
return Observable.fromCallable(() -> {
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");
Log.d("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(getApplication()).getWallet().getAccount(0).xpubstr()) || addrObj.getString("address").equals(BIP84Util.getInstance(getApplication()).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(getApplication()).getWallet().getAccount(0).getChain(0).setAddrIdx(addrObj.has("account_index") ? addrObj.getInt("account_index") : 0);
BIP84Util.getInstance(getApplication()).getWallet().getAccount(0).getChain(1).setAddrIdx(addrObj.has("change_index") ? addrObj.getInt("change_index") : 0);
} else if (addrObj.getString("address").equals(BIP49Util.getInstance(getApplication()).getWallet().getAccount(0).xpubstr()) || addrObj.getString("address").equals(BIP49Util.getInstance(getApplication()).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(getApplication()).getWallet().getAccount(0).getChain(0).setAddrIdx(addrObj.has("account_index") ? addrObj.getInt("account_index") : 0);
BIP49Util.getInstance(getApplication()).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(getApplication()).get().getAccount(0).getChain(0).setAddrIdx(addrObj.has("account_index") ? addrObj.getInt("account_index") : 0);
HD_WalletFactory.getInstance(getApplication()).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 (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 == 3) {
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(getApplication()).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 parseMixXPUB.
private synchronized boolean parseMixXPUB(JSONObject jsonObject) throws JSONException {
final int PRE_MIX = 0;
final int POST_MIX = 1;
final int BAD_BANK = 2;
int account_type = 0;
if (jsonObject != null) {
long xpub_mix_balance = 0;
if (jsonObject.has("wallet")) {
JSONObject walletObj = (JSONObject) jsonObject.get("wallet");
if (walletObj.has("final_balance")) {
xpub_mix_balance = walletObj.getLong("final_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().getAccountAt(WhirlpoolMeta.getInstance(context).getWhirlpoolPostmix()).xpubstr()) || addrObj.getString("address").equals(BIP84Util.getInstance(context).getWallet().getAccountAt(WhirlpoolMeta.getInstance(context).getWhirlpoolPostmix()).zpubstr())) {
xpub_postmix_balance = xpub_mix_balance;
account_type = POST_MIX;
AddressFactory.getInstance().setHighestPostReceiveIdx(addrObj.has("account_index") ? addrObj.getInt("account_index") : 0);
AddressFactory.getInstance().setHighestPostChangeIdx(addrObj.has("change_index") ? addrObj.getInt("change_index") : 0);
BIP84Util.getInstance(context).getWallet().getAccountAt(WhirlpoolMeta.getInstance(context).getWhirlpoolPostmix()).getChain(0).setAddrIdx(addrObj.has("account_index") ? addrObj.getInt("account_index") : 0);
BIP84Util.getInstance(context).getWallet().getAccountAt(WhirlpoolMeta.getInstance(context).getWhirlpoolPostmix()).getChain(1).setAddrIdx(addrObj.has("change_index") ? addrObj.getInt("change_index") : 0);
} else if (addrObj.getString("address").equals(BIP84Util.getInstance(context).getWallet().getAccountAt(WhirlpoolMeta.getInstance(context).getWhirlpoolPremixAccount()).xpubstr()) || addrObj.getString("address").equals(BIP84Util.getInstance(context).getWallet().getAccountAt(WhirlpoolMeta.getInstance(context).getWhirlpoolPremixAccount()).zpubstr())) {
xpub_premix_balance = xpub_mix_balance;
account_type = PRE_MIX;
AddressFactory.getInstance().setHighestPreReceiveIdx(addrObj.has("account_index") ? addrObj.getInt("account_index") : 0);
AddressFactory.getInstance().setHighestPreChangeIdx(addrObj.has("change_index") ? addrObj.getInt("change_index") : 0);
BIP84Util.getInstance(context).getWallet().getAccountAt(WhirlpoolMeta.getInstance(context).getWhirlpoolPremixAccount()).getChain(0).setAddrIdx(addrObj.has("account_index") ? addrObj.getInt("account_index") : 0);
BIP84Util.getInstance(context).getWallet().getAccountAt(WhirlpoolMeta.getInstance(context).getWhirlpoolPremixAccount()).getChain(1).setAddrIdx(addrObj.has("change_index") ? addrObj.getInt("change_index") : 0);
} else if (addrObj.getString("address").equals(BIP84Util.getInstance(context).getWallet().getAccountAt(WhirlpoolMeta.getInstance(context).getWhirlpoolBadBank()).xpubstr()) || addrObj.getString("address").equals(BIP84Util.getInstance(context).getWallet().getAccountAt(WhirlpoolMeta.getInstance(context).getWhirlpoolBadBank()).zpubstr())) {
xpub_badbank_balance = xpub_mix_balance;
account_type = BAD_BANK;
AddressFactory.getInstance().setHighestBadBankReceiveIdx(addrObj.has("account_index") ? addrObj.getInt("account_index") : 0);
AddressFactory.getInstance().setHighestBadBankChangeIdx(addrObj.has("change_index") ? addrObj.getInt("change_index") : 0);
BIP84Util.getInstance(context).getWallet().getAccountAt(WhirlpoolMeta.getInstance(context).getWhirlpoolBadBank()).getChain(0).setAddrIdx(addrObj.has("account_index") ? addrObj.getInt("account_index") : 0);
BIP84Util.getInstance(context).getWallet().getAccountAt(WhirlpoolMeta.getInstance(context).getWhirlpoolBadBank()).getChain(1).setAddrIdx(addrObj.has("change_index") ? addrObj.getInt("change_index") : 0);
} else {
;
}
}
}
}
}
if (jsonObject.has("txs")) {
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 (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) {
Tx tx = new Tx(hash, addr, amount, ts, (latest_block_height > 0L && height > 0L) ? (latest_block_height - height) + 1 : 0);
if (addr == null) {
addr = _addr;
}
if (addr.equals(BIP84Util.getInstance(context).getWallet().getAccountAt(WhirlpoolMeta.getInstance(context).getWhirlpoolPostmix()).xpubstr()) || addr.equals(BIP84Util.getInstance(context).getWallet().getAccountAt(WhirlpoolMeta.getInstance(context).getWhirlpoolPostmix()).zpubstr())) {
if (!postmix_txs.containsKey(addr)) {
postmix_txs.put(addr, new ArrayList<Tx>());
}
if (FormatsUtil.getInstance().isValidXpub(addr)) {
postmix_txs.get(addr).add(tx);
} else {
postmix_txs.get(AddressFactory.getInstance().account2xpub().get(0)).add(tx);
}
} else if (addr.equals(BIP84Util.getInstance(context).getWallet().getAccountAt(WhirlpoolMeta.getInstance(context).getWhirlpoolPremixAccount()).xpubstr()) || addr.equals(BIP84Util.getInstance(context).getWallet().getAccountAt(WhirlpoolMeta.getInstance(context).getWhirlpoolPremixAccount()).zpubstr())) {
if (!premix_txs.containsKey(addr)) {
premix_txs.put(addr, new ArrayList<Tx>());
}
if (FormatsUtil.getInstance().isValidXpub(addr)) {
premix_txs.get(addr).add(tx);
} else {
premix_txs.get(AddressFactory.getInstance().account2xpub().get(0)).add(tx);
}
} else if (addr.equals(BIP84Util.getInstance(context).getWallet().getAccountAt(WhirlpoolMeta.getInstance(context).getWhirlpoolBadBank()).xpubstr()) || addr.equals(BIP84Util.getInstance(context).getWallet().getAccountAt(WhirlpoolMeta.getInstance(context).getWhirlpoolBadBank()).zpubstr())) {
if (!badbank_txs.containsKey(addr)) {
badbank_txs.put(addr, new ArrayList<Tx>());
}
if (FormatsUtil.getInstance().isValidXpub(addr)) {
badbank_txs.get(addr).add(tx);
} else {
badbank_txs.get(AddressFactory.getInstance().account2xpub().get(0)).add(tx);
}
}
}
}
}
try {
if (account_type == POST_MIX) {
PayloadUtil.getInstance(context).serializeMultiAddrPost(jsonObject);
} else if (account_type == PRE_MIX) {
PayloadUtil.getInstance(context).serializeMultiAddrPre(jsonObject);
} else {
PayloadUtil.getInstance(context).serializeMultiAddrBadBank(jsonObject);
}
} catch (IOException | DecryptionException e) {
;
}
return true;
}
return false;
}
Aggregations