Skip to content

Commit

Permalink
WalletUtils, PaymentIntent: derive address from script in a more gene…
Browse files Browse the repository at this point in the history
…ric way
  • Loading branch information
Andreas Schildbach committed Oct 7, 2021
1 parent 24410d1 commit c9aa3e2
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 26 deletions.
24 changes: 12 additions & 12 deletions wallet/src/de/schildbach/wallet/data/PaymentIntent.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import de.schildbach.wallet.Constants;
import de.schildbach.wallet.util.Bluetooth;
import de.schildbach.wallet.util.GenericUtils;
import de.schildbach.wallet.util.WalletUtils;
import org.bitcoinj.core.Address;
import org.bitcoinj.core.AddressFormatException;
import org.bitcoinj.core.Coin;
Expand Down Expand Up @@ -83,13 +84,13 @@ public String toString() {
builder.append('[');
builder.append(hasAmount() ? amount.toPlainString() : "null");
builder.append(',');
if (ScriptPattern.isP2PKH(script) || ScriptPattern.isP2SH(script)
|| ScriptPattern.isP2WH(script))
builder.append(script.getToAddress(Constants.NETWORK_PARAMETERS));
else if (ScriptPattern.isP2PK(script))
final Address toAddress = WalletUtils.getToAddress(script);
if (ScriptPattern.isP2PK(script))
builder.append(Constants.HEX.encode(ScriptPattern.extractKeyFromP2PK(script)));
else if (ScriptPattern.isSentToMultisig(script))
builder.append("multisig");
else if (toAddress != null)
builder.append(toAddress);
else
builder.append("unknown");
builder.append(']');
Expand Down Expand Up @@ -269,20 +270,19 @@ public boolean hasOutputs() {
}

public boolean hasAddress() {
if (outputs == null || outputs.length != 1)
return false;

final Script script = outputs[0].script;
return ScriptPattern.isP2PKH(script) || ScriptPattern.isP2SH(script)
|| ScriptPattern.isP2PK(script) || ScriptPattern.isP2WH(script);
return getAddress() != null;
}

public Address getAddress() {
if (!hasAddress())
if (outputs == null || outputs.length != 1)
throw new IllegalStateException();

final Script script = outputs[0].script;
return script.getToAddress(Constants.NETWORK_PARAMETERS, true);
final Address address = WalletUtils.getToAddress(script);
if (address == null)
throw new IllegalStateException();

return address;
}

public boolean mayEditAddress() {
Expand Down
33 changes: 19 additions & 14 deletions wallet/src/de/schildbach/wallet/util/WalletUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,16 +117,23 @@ public static Spanned formatHash(@Nullable final String prefix, final String has
return SpannedString.valueOf(builder);
}

@Nullable
public static Address getToAddress(final Script script) {
try {
return script.getToAddress(Constants.NETWORK_PARAMETERS, true);
} catch (final ScriptException x) {
return null;
}
}

@Nullable
public static Address getToAddressOfSent(final Transaction tx, final Wallet wallet) {
for (final TransactionOutput output : tx.getOutputs()) {
try {
if (!output.isMine(wallet)) {
final Script script = output.getScriptPubKey();
return script.getToAddress(Constants.NETWORK_PARAMETERS, true);
}
} catch (final ScriptException x) {
// swallow
if (!output.isMine(wallet)) {
final Script script = output.getScriptPubKey();
final Address address = getToAddress(script);
if (address != null)
return address;
}
}

Expand All @@ -136,13 +143,11 @@ public static Address getToAddressOfSent(final Transaction tx, final Wallet wall
@Nullable
public static Address getWalletAddressOfReceived(final Transaction tx, final Wallet wallet) {
for (final TransactionOutput output : tx.getOutputs()) {
try {
if (output.isMine(wallet)) {
final Script script = output.getScriptPubKey();
return script.getToAddress(Constants.NETWORK_PARAMETERS, true);
}
} catch (final ScriptException x) {
// swallow
if (output.isMine(wallet)) {
final Script script = output.getScriptPubKey();
final Address address = getToAddress(script);
if (address != null)
return address;
}
}

Expand Down

0 comments on commit c9aa3e2

Please sign in to comment.