mirror of
https://github.com/sparrowwallet/sparrow.git
synced 2024-11-04 21:36:45 +00:00
support encrypted bip129 wallet imports
This commit is contained in:
parent
fc5d48de6f
commit
41636f7152
1 changed files with 61 additions and 57 deletions
|
@ -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();
|
||||
|
|
Loading…
Reference in a new issue