wallet <-> output descriptor

This commit is contained in:
Craig Raw 2020-09-07 17:22:37 +02:00
parent 0516426dc0
commit 70fdecf919
2 changed files with 35 additions and 1 deletions

View file

@ -4,8 +4,14 @@ import com.sparrowwallet.drongo.address.*;
import com.sparrowwallet.drongo.crypto.ChildNumber; import com.sparrowwallet.drongo.crypto.ChildNumber;
import com.sparrowwallet.drongo.crypto.DeterministicKey; import com.sparrowwallet.drongo.crypto.DeterministicKey;
import com.sparrowwallet.drongo.crypto.ECKey; import com.sparrowwallet.drongo.crypto.ECKey;
import com.sparrowwallet.drongo.policy.Policy;
import com.sparrowwallet.drongo.policy.PolicyType;
import com.sparrowwallet.drongo.protocol.Script; import com.sparrowwallet.drongo.protocol.Script;
import com.sparrowwallet.drongo.protocol.ScriptType; import com.sparrowwallet.drongo.protocol.ScriptType;
import com.sparrowwallet.drongo.wallet.Keystore;
import com.sparrowwallet.drongo.wallet.KeystoreSource;
import com.sparrowwallet.drongo.wallet.Wallet;
import com.sparrowwallet.drongo.wallet.WalletModel;
import java.util.*; import java.util.*;
import java.util.regex.Matcher; import java.util.regex.Matcher;
@ -212,6 +218,34 @@ public class OutputDescriptor {
return keyPath; return keyPath;
} }
public Wallet toWallet() {
Wallet wallet = new Wallet();
wallet.setPolicyType(isMultisig() ? PolicyType.MULTI : PolicyType.SINGLE);
wallet.setScriptType(scriptType);
for(Map.Entry<ExtendedKey,KeyDerivation> extKeyEntry : extendedPublicKeys.entrySet()) {
Keystore keystore = new Keystore();
keystore.setSource(KeystoreSource.SW_WATCH);
keystore.setWalletModel(WalletModel.SPARROW);
keystore.setKeyDerivation(extKeyEntry.getValue());
keystore.setExtendedPublicKey(extKeyEntry.getKey());
wallet.makeLabelsUnique(keystore);
wallet.getKeystores().add(keystore);
}
wallet.setDefaultPolicy(Policy.getPolicy(wallet.getPolicyType(), wallet.getScriptType(), wallet.getKeystores(), getMultisigThreshold()));
return wallet;
}
public static OutputDescriptor getOutputDescriptor(Wallet wallet) {
Map<ExtendedKey, KeyDerivation> extendedKeyDerivationMap = new LinkedHashMap<>();
for(Keystore keystore : wallet.getKeystores()) {
extendedKeyDerivationMap.put(keystore.getExtendedPublicKey(), keystore.getKeyDerivation());
}
return new OutputDescriptor(wallet.getScriptType(), wallet.getDefaultPolicy().getNumSignaturesRequired(), extendedKeyDerivationMap);
}
// See https://github.com/bitcoin/bitcoin/blob/master/doc/descriptors.md // See https://github.com/bitcoin/bitcoin/blob/master/doc/descriptors.md
public static OutputDescriptor getOutputDescriptor(String descriptor) { public static OutputDescriptor getOutputDescriptor(String descriptor) {
ScriptType scriptType = ScriptType.fromDescriptor(descriptor); ScriptType scriptType = ScriptType.fromDescriptor(descriptor);

View file

@ -1,7 +1,7 @@
package com.sparrowwallet.drongo.wallet; package com.sparrowwallet.drongo.wallet;
public enum WalletModel { public enum WalletModel {
SEED, SPARROW, BITCOIN_CORE, ELECTRUM, TREZOR_1, TREZOR_T, COLDCARD, LEDGER_NANO_S, LEDGER_NANO_X, DIGITALBITBOX_01, KEEPKEY; SEED, SPARROW, BITCOIN_CORE, ELECTRUM, TREZOR_1, TREZOR_T, COLDCARD, LEDGER_NANO_S, LEDGER_NANO_X, DIGITALBITBOX_01, KEEPKEY, SPECTER;
public static WalletModel getModel(String model) { public static WalletModel getModel(String model) {
return valueOf(model.toUpperCase()); return valueOf(model.toUpperCase());