@ -1,6 +1,8 @@
package com.sparrowwallet.sparrow.control ;
package com.sparrowwallet.sparrow.control ;
import com.sparrowwallet.drongo.wallet.Bip39MnemonicCode ;
import com.sparrowwallet.drongo.wallet.Bip39MnemonicCode ;
import com.sparrowwallet.drongo.wallet.DeterministicSeed ;
import com.sparrowwallet.sparrow.AppServices ;
import com.sparrowwallet.sparrow.glyphfont.FontAwesome5 ;
import com.sparrowwallet.sparrow.glyphfont.FontAwesome5 ;
import javafx.application.Platform ;
import javafx.application.Platform ;
import javafx.beans.property.IntegerProperty ;
import javafx.beans.property.IntegerProperty ;
@ -26,13 +28,16 @@ import org.controlsfx.validation.ValidationResult;
import org.controlsfx.validation.ValidationSupport ;
import org.controlsfx.validation.ValidationSupport ;
import org.controlsfx.validation.Validator ;
import org.controlsfx.validation.Validator ;
import org.controlsfx.validation.decoration.StyleClassValidationDecoration ;
import org.controlsfx.validation.decoration.StyleClassValidationDecoration ;
import org.slf4j.Logger ;
import org.slf4j.LoggerFactory ;
import java.util.ArrayList ;
import java.util.* ;
import java.util.Collection ;
import java.util.Collections ;
import static com.sparrowwallet.sparrow.AppServices.showErrorDialog ;
import java.util.List ;
public class MnemonicKeystorePane extends TitledDescriptionPane {
public class MnemonicKeystorePane extends TitledDescriptionPane {
private static final Logger log = LoggerFactory . getLogger ( MnemonicKeystorePane . class ) ;
protected SplitMenuButton enterMnemonicButton ;
protected SplitMenuButton enterMnemonicButton ;
protected TilePane wordsPane ;
protected TilePane wordsPane ;
protected Label validLabel ;
protected Label validLabel ;
@ -73,9 +78,44 @@ public class MnemonicKeystorePane extends TitledDescriptionPane {
} ) ;
} ) ;
enterMnemonicButton . getItems ( ) . add ( item ) ;
enterMnemonicButton . getItems ( ) . add ( item ) ;
}
}
enterMnemonicButton . getItems ( ) . add ( new SeparatorMenuItem ( ) ) ;
MenuItem scanItem = new MenuItem ( "Scan QR..." ) ;
scanItem . setOnAction ( event - > {
scanQR ( ) ;
} ) ;
enterMnemonicButton . getItems ( ) . add ( scanItem ) ;
enterMnemonicButton . managedProperty ( ) . bind ( enterMnemonicButton . visibleProperty ( ) ) ;
enterMnemonicButton . managedProperty ( ) . bind ( enterMnemonicButton . visibleProperty ( ) ) ;
}
}
protected void scanQR ( ) {
QRScanDialog qrScanDialog = new QRScanDialog ( ) ;
Optional < QRScanDialog . Result > optionalResult = qrScanDialog . showAndWait ( ) ;
if ( optionalResult . isPresent ( ) ) {
QRScanDialog . Result result = optionalResult . get ( ) ;
if ( result . seed ! = null ) {
showWordList ( result . seed ) ;
Platform . runLater ( ( ) - > validLabel . requestFocus ( ) ) ;
} else if ( result . exception ! = null ) {
log . error ( "Error scanning QR" , result . exception ) ;
showErrorDialog ( "Error scanning QR" , result . exception . getMessage ( ) ) ;
} else {
AppServices . showErrorDialog ( "Invalid QR Code" , "Cannot parse QR code into a seed." ) ;
}
}
}
protected void showWordList ( DeterministicSeed seed ) {
List < String > words = seed . getMnemonicCode ( ) ;
setContent ( getMnemonicWordsEntry ( words . size ( ) ) ) ;
setExpanded ( true ) ;
for ( int i = 0 ; i < wordsPane . getChildren ( ) . size ( ) ; i + + ) {
WordEntry wordEntry = ( WordEntry ) wordsPane . getChildren ( ) . get ( i ) ;
wordEntry . getEditor ( ) . setText ( words . get ( i ) ) ;
wordEntry . getEditor ( ) . setEditable ( false ) ;
}
}
protected void enterMnemonic ( int numWords ) {
protected void enterMnemonic ( int numWords ) {
setDescription ( "Generate new or enter existing" ) ;
setDescription ( "Generate new or enter existing" ) ;
showHideLink . setVisible ( false ) ;
showHideLink . setVisible ( false ) ;