scan qr to bitcoin uri

This commit is contained in:
Craig Raw
2020-08-20 15:26:59 +02:00
parent 4c8f7bb711
commit ca64a1d307
5 changed files with 78 additions and 5 deletions
@@ -2,9 +2,11 @@ package com.sparrowwallet.sparrow.control;
import com.github.sarxos.webcam.WebcamResolution;
import com.sparrowwallet.drongo.Utils;
import com.sparrowwallet.drongo.address.Address;
import com.sparrowwallet.drongo.protocol.Base43;
import com.sparrowwallet.drongo.protocol.Transaction;
import com.sparrowwallet.drongo.psbt.PSBT;
import com.sparrowwallet.drongo.uri.BitcoinURI;
import com.sparrowwallet.sparrow.ur.ResultType;
import com.sparrowwallet.sparrow.ur.UR;
import com.sparrowwallet.sparrow.ur.URDecoder;
@@ -99,6 +101,24 @@ public class QRScanDialog extends Dialog<QRScanDialog.Result> {
} else {
PSBT psbt;
Transaction transaction;
BitcoinURI bitcoinURI;
Address address;
try {
bitcoinURI = new BitcoinURI(qrtext);
result = new Result(bitcoinURI);
return;
} catch(Exception e) {
//Ignore, not an BIP 21 URI
}
try {
address = Address.fromString(qrtext);
result = new Result(address);
return;
} catch(Exception e) {
//Ignore, not an address
}
try {
psbt = PSBT.fromString(qrtext);
result = new Result(psbt);
@@ -132,9 +152,8 @@ public class QRScanDialog extends Dialog<QRScanDialog.Result> {
}
//Try Base43 used by Electrum
byte[] base43 = Base43.decode(qrResult.getText());
try {
psbt = new PSBT(base43);
psbt = new PSBT(Base43.decode(qrResult.getText()));
result = new Result(psbt);
return;
} catch(Exception e) {
@@ -142,14 +161,14 @@ public class QRScanDialog extends Dialog<QRScanDialog.Result> {
}
try {
transaction = new Transaction(base43);
transaction = new Transaction(Base43.decode(qrResult.getText()));
result = new Result(transaction);
return;
} catch(Exception e) {
//Ignore, not parseable as base43 decoded bytes
}
result = new Result("Cannot parse QR code into a PSBT or transaction");
result = new Result("Cannot parse QR code into a PSBT, transaction or address");
}
}
}
@@ -157,12 +176,14 @@ public class QRScanDialog extends Dialog<QRScanDialog.Result> {
public static class Result {
public final Transaction transaction;
public final PSBT psbt;
public final BitcoinURI uri;
public final String error;
public final Throwable exception;
public Result(Transaction transaction) {
this.transaction = transaction;
this.psbt = null;
this.uri = null;
this.error = null;
this.exception = null;
}
@@ -170,6 +191,23 @@ public class QRScanDialog extends Dialog<QRScanDialog.Result> {
public Result(PSBT psbt) {
this.transaction = null;
this.psbt = psbt;
this.uri = null;
this.error = null;
this.exception = null;
}
public Result(BitcoinURI uri) {
this.transaction = null;
this.psbt = null;
this.uri = uri;
this.error = null;
this.exception = null;
}
public Result(Address address) {
this.transaction = null;
this.psbt = null;
this.uri = BitcoinURI.fromAddress(address);
this.error = null;
this.exception = null;
}
@@ -177,6 +215,7 @@ public class QRScanDialog extends Dialog<QRScanDialog.Result> {
public Result(String error) {
this.transaction = null;
this.psbt = null;
this.uri = null;
this.error = error;
this.exception = null;
}
@@ -184,6 +223,7 @@ public class QRScanDialog extends Dialog<QRScanDialog.Result> {
public Result(Throwable exception) {
this.transaction = null;
this.psbt = null;
this.uri = null;
this.error = null;
this.exception = exception;
}
@@ -496,6 +496,25 @@ public class SendController extends WalletFormController implements Initializabl
return address.getScriptType().getDustThreshold(txOutput, getFeeRate());
}
public void scanQrAddress(ActionEvent event) {
QRScanDialog qrScanDialog = new QRScanDialog();
Optional<QRScanDialog.Result> optionalResult = qrScanDialog.showAndWait();
if(optionalResult.isPresent()) {
QRScanDialog.Result result = optionalResult.get();
if(result.uri != null) {
if(result.uri.getAddress() != null) {
address.setText(result.uri.getAddress().toString());
}
if(result.uri.getLabel() != null) {
label.setText(result.uri.getLabel());
}
if(result.uri.getAmount() != null) {
setRecipientValueSats(result.uri.getAmount());
}
}
}
}
public void clear(ActionEvent event) {
address.setText("");
label.setText("");