From 70fdecf919ea9dcae46cc35df23e0a20ba90683f Mon Sep 17 00:00:00 2001 From: Craig Raw Date: Mon, 7 Sep 2020 17:22:37 +0200 Subject: [PATCH] wallet <-> output descriptor --- .../drongo/OutputDescriptor.java | 34 +++++++++++++++++++ .../drongo/wallet/WalletModel.java | 2 +- 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/sparrowwallet/drongo/OutputDescriptor.java b/src/main/java/com/sparrowwallet/drongo/OutputDescriptor.java index 4f22b80..b572d64 100644 --- a/src/main/java/com/sparrowwallet/drongo/OutputDescriptor.java +++ b/src/main/java/com/sparrowwallet/drongo/OutputDescriptor.java @@ -4,8 +4,14 @@ import com.sparrowwallet.drongo.address.*; import com.sparrowwallet.drongo.crypto.ChildNumber; import com.sparrowwallet.drongo.crypto.DeterministicKey; 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.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.regex.Matcher; @@ -212,6 +218,34 @@ public class OutputDescriptor { return keyPath; } + public Wallet toWallet() { + Wallet wallet = new Wallet(); + wallet.setPolicyType(isMultisig() ? PolicyType.MULTI : PolicyType.SINGLE); + wallet.setScriptType(scriptType); + + for(Map.Entry 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 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 public static OutputDescriptor getOutputDescriptor(String descriptor) { ScriptType scriptType = ScriptType.fromDescriptor(descriptor); diff --git a/src/main/java/com/sparrowwallet/drongo/wallet/WalletModel.java b/src/main/java/com/sparrowwallet/drongo/wallet/WalletModel.java index 2ab15af..7be2c94 100644 --- a/src/main/java/com/sparrowwallet/drongo/wallet/WalletModel.java +++ b/src/main/java/com/sparrowwallet/drongo/wallet/WalletModel.java @@ -1,7 +1,7 @@ package com.sparrowwallet.drongo.wallet; 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) { return valueOf(model.toUpperCase());