support encrypted bip129 wallet imports

This commit is contained in:
Craig Raw 2023-02-23 12:17:41 +02:00
parent fc5d48de6f
commit 41636f7152

View file

@ -9,15 +9,13 @@ import com.sparrowwallet.drongo.protocol.ScriptType;
import com.sparrowwallet.drongo.protocol.Sha256Hash;
import com.sparrowwallet.drongo.wallet.*;
import javax.crypto.Cipher;
import javax.crypto.Mac;
import javax.crypto.*;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.*;
import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.security.Key;
import java.security.SignatureException;
import java.security.*;
import java.util.Arrays;
import java.util.List;
@ -97,9 +95,28 @@ public class Bip129 implements KeystoreFileExport, KeystoreFileImport, WalletExp
@Override
public Keystore getKeystore(ScriptType scriptType, InputStream inputStream, String password) throws ImportException {
try {
try(BufferedReader streamReader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8))) {
BufferedReader reader = streamReader;
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
if(password != null) {
reader = decryptImport(password, reader);
}
String header = reader.readLine();
String token = reader.readLine();
String descriptor = reader.readLine();
String label = reader.readLine();
String signature = reader.readLine();
return getKeystore(header, token, descriptor, label, signature);
} catch(MnemonicException.MnemonicWordException e) {
throw new ImportException("Error importing BSMS: Invalid mnemonic word " + e.badWord, e);
} catch(MnemonicException.MnemonicChecksumException e) {
throw new ImportException("Error importing BSMS: Invalid mnemonic checksum", e);
} catch(Exception e) {
throw new ImportException("Error importing BSMS", e);
}
}
private BufferedReader decryptImport(String password, BufferedReader streamReader) throws Exception {
byte[] token;
if((password.length() == 16 || password.length() == 32) && Utils.isHex(password)) {
token = Utils.hexToBytes(password);
@ -140,24 +157,7 @@ public class Bip129 implements KeystoreFileExport, KeystoreFileImport, WalletExp
throw new ImportException("Message digest authentication failed.");
}
reader = new BufferedReader(new StringReader(plaintextString));
}
String header = reader.readLine();
String token = reader.readLine();
String descriptor = reader.readLine();
String label = reader.readLine();
String signature = reader.readLine();
return getKeystore(header, token, descriptor, label, signature);
}
} catch(MnemonicException.MnemonicWordException e) {
throw new ImportException("Error importing BSMS: Invalid mnemonic word " + e.badWord, e);
} catch(MnemonicException.MnemonicChecksumException e) {
throw new ImportException("Error importing BSMS: Invalid mnemonic checksum", e);
} catch(Exception e) {
throw new ImportException("Error importing BSMS", e);
}
return new BufferedReader(new StringReader(plaintextString));
}
private Keystore getKeystore(String header, String token, String descriptor, String label, String signature) throws ImportException {
@ -230,6 +230,10 @@ public class Bip129 implements KeystoreFileExport, KeystoreFileImport, WalletExp
public Wallet importWallet(InputStream inputStream, String password) throws ImportException {
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
if(password != null) {
reader = decryptImport(password, reader);
}
String header = reader.readLine();
String descriptor = reader.readLine();
String paths = reader.readLine();