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
drongo

@ -1 +1 @@
Subproject commit 9663629e344ad19f5eb550f6d87bd27048f3704e
Subproject commit 5d456a10df1375b41e6159efc540b3d90cfbfc4d

View file

@ -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;
}

View file

@ -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("");

View file

@ -20,6 +20,7 @@
<?import com.sparrowwallet.sparrow.control.TransactionDiagram?>
<?import com.sparrowwallet.drongo.BitcoinUnit?>
<?import com.sparrowwallet.sparrow.control.FiatLabel?>
<?import org.controlsfx.glyphfont.Glyph?>
<BorderPane stylesheets="@send.css, @wallet.css, @../script.css, @../general.css" styleClass="wallet-pane" xmlns="http://javafx.com/javafx/10.0.2-internal" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.sparrowwallet.sparrow.wallet.SendController">
<center>
@ -61,6 +62,15 @@
</Field>
</Fieldset>
</Form>
<Form GridPane.columnIndex="2" GridPane.rowIndex="0">
<Fieldset inputGrow="SOMETIMES" text="">
<Button text="Scan QR" onAction="#scanQrAddress">
<graphic>
<Glyph fontFamily="Font Awesome 5 Free Solid" fontSize="12" icon="CAMERA" />
</graphic>
</Button>
</Fieldset>
</Form>
<Form GridPane.columnIndex="0" GridPane.rowIndex="1">
<Fieldset inputGrow="SOMETIMES" text="Fee">
<Field text="Block target:">

View file

@ -1,6 +1,10 @@
<configuration>
<statusListener class="ch.qos.logback.core.status.NopStatusListener" />
<logger name="com.github.sarxos.webcam.Webcam" level="OFF"/>
<logger name="com.github.sarxos.webcam.ds.cgt.WebcamOpenTask" level="OFF"/>
<logger name="com.github.sarxos.webcam.ds.cgt.WebcamCloseTask" level="OFF"/>
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>${user.home}/.sparrow/sparrow.log</file>
<encoder>