public static List<TransactionOutput> getMyOutputs(Transaction tx, DeterministicKey key) { List<TransactionOutput> mines = new ArrayList<>(); for (TransactionOutput curr : tx.getOutputs()) { boolean isMine = false; String to = null; Address add = curr.getAddressFromP2PKHScript(BitcoinNetwork.getInstance().get().getParams()); if (add != null) to = add.toString(); else { add = curr.getAddressFromP2SH(BitcoinNetwork.getInstance().get().getParams()); if (add != null) to = add.toString(); } if (to != null) { // VERIFICATION BY ADDRESS isMine = to.equals(keyToStringAddress(key)); } else { // VERIFICATION BY PUBKEY final byte[] pubKeyCurr = curr.getScriptPubKey().getPubKey(); isMine = Arrays.equals(key.getPubKey(), pubKeyCurr); } if (isMine) { mines.add(curr); break; } } return mines; }
public static boolean isValidAddress(String address) { if (address == null) return false; if (!basicValidation(address)) return false; try { new Address(BitcoinNetwork.getInstance().get().getParams(), address); return true; } catch (AddressFormatException e) { return false; } }
public static String keyToStringAddress(ECKey key) { return key.toAddress(BitcoinNetwork.getInstance().get().getParams()).toString(); }