mirror of
https://github.com/sparrowwallet/sparrow.git
synced 2024-12-25 13:16:44 +00:00
write and parse both multipath and single descriptors in wallet output descriptor export
This commit is contained in:
parent
ff0c381437
commit
b15d6308bd
1 changed files with 56 additions and 7 deletions
|
@ -7,7 +7,8 @@ import com.sparrowwallet.drongo.wallet.WalletModel;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.stream.Collectors;
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
public class Descriptor implements WalletImport, WalletExport {
|
public class Descriptor implements WalletImport, WalletExport {
|
||||||
|
|
||||||
|
@ -24,10 +25,29 @@ public class Descriptor implements WalletImport, WalletExport {
|
||||||
@Override
|
@Override
|
||||||
public void exportWallet(Wallet wallet, OutputStream outputStream) throws ExportException {
|
public void exportWallet(Wallet wallet, OutputStream outputStream) throws ExportException {
|
||||||
try {
|
try {
|
||||||
|
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream));
|
||||||
|
bufferedWriter.write("# Receive and change descriptor (BIP389):");
|
||||||
|
bufferedWriter.newLine();
|
||||||
|
|
||||||
OutputDescriptor outputDescriptor = OutputDescriptor.getOutputDescriptor(wallet, KeyPurpose.DEFAULT_PURPOSES, null);
|
OutputDescriptor outputDescriptor = OutputDescriptor.getOutputDescriptor(wallet, KeyPurpose.DEFAULT_PURPOSES, null);
|
||||||
String outputDescriptorString = outputDescriptor.toString(true);
|
bufferedWriter.write(outputDescriptor.toString(true));
|
||||||
outputStream.write(outputDescriptorString.getBytes(StandardCharsets.UTF_8));
|
bufferedWriter.newLine();
|
||||||
outputStream.flush();
|
bufferedWriter.newLine();
|
||||||
|
bufferedWriter.newLine();
|
||||||
|
|
||||||
|
bufferedWriter.write("# Receive descriptor (Bitcoin Core):");
|
||||||
|
bufferedWriter.newLine();
|
||||||
|
OutputDescriptor receiveDescriptor = OutputDescriptor.getOutputDescriptor(wallet, KeyPurpose.RECEIVE, null);
|
||||||
|
bufferedWriter.write(receiveDescriptor.toString(true));
|
||||||
|
bufferedWriter.newLine();
|
||||||
|
bufferedWriter.newLine();
|
||||||
|
bufferedWriter.write("# Change descriptor (Bitcoin Core):");
|
||||||
|
bufferedWriter.newLine();
|
||||||
|
OutputDescriptor changeDescriptor = OutputDescriptor.getOutputDescriptor(wallet, KeyPurpose.CHANGE, null);
|
||||||
|
bufferedWriter.write(changeDescriptor.toString(true));
|
||||||
|
bufferedWriter.newLine();
|
||||||
|
|
||||||
|
bufferedWriter.flush();
|
||||||
} catch(Exception e) {
|
} catch(Exception e) {
|
||||||
throw new ExportException("Error writing output descriptor", e);
|
throw new ExportException("Error writing output descriptor", e);
|
||||||
}
|
}
|
||||||
|
@ -77,14 +97,43 @@ public class Descriptor implements WalletImport, WalletExport {
|
||||||
//ignore
|
//ignore
|
||||||
}
|
}
|
||||||
|
|
||||||
String outputDescriptor = new BufferedReader(new InputStreamReader(secondClone, StandardCharsets.UTF_8)).lines().collect(Collectors.joining("\n"));
|
List<String> paragraphs = getParagraphs(secondClone);
|
||||||
OutputDescriptor descriptor = OutputDescriptor.getOutputDescriptor(outputDescriptor.trim());
|
for(String paragraph : paragraphs) {
|
||||||
return descriptor.toWallet();
|
OutputDescriptor descriptor = OutputDescriptor.getOutputDescriptor(paragraph);
|
||||||
|
return descriptor.toWallet();
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new ImportException("Could not find an output descriptor in the file");
|
||||||
} catch(Exception e) {
|
} catch(Exception e) {
|
||||||
throw new ImportException("Error importing output descriptor", e);
|
throw new ImportException("Error importing output descriptor", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static List<String> getParagraphs(InputStream inputStream) {
|
||||||
|
List<String> paragraphs = new ArrayList<>();
|
||||||
|
StringBuilder paragraph = new StringBuilder();
|
||||||
|
|
||||||
|
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
|
||||||
|
for(String line : reader.lines().map(String::trim).toArray(String[]::new)) {
|
||||||
|
if(line.isEmpty()) {
|
||||||
|
if(paragraph.length() > 0) {
|
||||||
|
paragraphs.add(paragraph.toString());
|
||||||
|
paragraph.setLength(0);
|
||||||
|
}
|
||||||
|
} else if(line.startsWith("#")) {
|
||||||
|
continue;
|
||||||
|
} else {
|
||||||
|
paragraph.append(line);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(paragraph.length() > 0) {
|
||||||
|
paragraphs.add(paragraph.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
return paragraphs;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isWalletImportScannable() {
|
public boolean isWalletImportScannable() {
|
||||||
return false;
|
return false;
|
||||||
|
|
Loading…
Reference in a new issue