wallet copying

This commit is contained in:
Craig Raw 2020-04-19 19:07:06 +02:00
parent 97cdd62173
commit 813781902b
8 changed files with 63 additions and 4 deletions

View file

@ -111,6 +111,11 @@ public class ExtendedPublicKey {
return true;
}
public ExtendedPublicKey copy() {
//DeterministicKey is effectively final
return new ExtendedPublicKey(pubKey, Arrays.copyOf(parentFingerprint, parentFingerprint.length), pubKeyChildNumber);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;

View file

@ -9,7 +9,7 @@ import java.util.List;
public class KeyDerivation {
private final String masterFingerprint;
private final String derivationPath;
private final List<ChildNumber> derivation;
private final transient List<ChildNumber> derivation;
public KeyDerivation(String masterFingerprint, String derivationPath) {
this.masterFingerprint = masterFingerprint;
@ -73,6 +73,10 @@ public class KeyDerivation {
return true;
}
public KeyDerivation copy() {
return new KeyDerivation(masterFingerprint, derivationPath);
}
public String toString() {
return masterFingerprint + (derivationPath != null ? derivationPath.replace("m", "") : "");
}

View file

@ -6,13 +6,14 @@ import com.sparrowwallet.drongo.protocol.Base58;
import com.sparrowwallet.drongo.protocol.Sha256Hash;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class DeterministicKey extends ECKey {
private final DeterministicKey parent;
private final List<ChildNumber> childNumberPath;
private final int depth;
private byte[] parentFingerprint; // 0 if this key is root node of key hierarchy
private final byte[] parentFingerprint; // 0 if this key is root node of key hierarchy
/** 32 bytes */
private final byte[] chainCode;
@ -80,7 +81,7 @@ public class DeterministicKey extends ECKey {
* the first child of that node.
*/
public List<ChildNumber> getPath() {
return childNumberPath;
return Collections.unmodifiableList(childNumberPath);
}
public DeterministicKey getParent() {

View file

@ -35,4 +35,8 @@ public class Miniscript {
throw new IllegalArgumentException("Could not find multisig threshold in " + this);
}
}
public Miniscript copy() {
return new Miniscript(script);
}
}

View file

@ -55,4 +55,8 @@ public class Policy {
throw new PolicyException("No standard policy for " + policyType + " policy with script type " + scriptType);
}
public Policy copy() {
return new Policy(name, miniscript.copy());
}
}

View file

@ -4,10 +4,16 @@ import com.sparrowwallet.drongo.ExtendedPublicKey;
import com.sparrowwallet.drongo.KeyDerivation;
public class Keystore {
public static final String DEFAULT_LABEL = "Keystore 1";
private String label;
private KeyDerivation keyDerivation;
private ExtendedPublicKey extendedPublicKey;
public Keystore() {
this(DEFAULT_LABEL);
}
public Keystore(String label) {
this.label = label;
}
@ -39,4 +45,15 @@ public class Keystore {
public void setExtendedPublicKey(ExtendedPublicKey extendedPublicKey) {
this.extendedPublicKey = extendedPublicKey;
}
public Keystore copy() {
Keystore copy = new Keystore(label);
if(keyDerivation != null) {
copy.setKeyDerivation(keyDerivation.copy());
}
if(extendedPublicKey != null) {
copy.setExtendedPublicKey(extendedPublicKey.copy());
}
return copy;
}
}

View file

@ -5,14 +5,27 @@ import com.sparrowwallet.drongo.policy.PolicyType;
import com.sparrowwallet.drongo.protocol.ScriptType;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Wallet {
private PolicyType policyType;
private ScriptType scriptType;
private Policy defaultPolicy;
private List<Keystore> keystores = new ArrayList<>();
public Wallet() {
}
public Wallet(PolicyType policyType, ScriptType scriptType) {
this.policyType = policyType;
this.scriptType = scriptType;
this.keystores = Collections.singletonList(new Keystore());
this.defaultPolicy = Policy.getPolicy(policyType, scriptType, keystores, null);
}
public PolicyType getPolicyType() {
return policyType;
}
@ -44,4 +57,15 @@ public class Wallet {
public void setKeystores(List<Keystore> keystores) {
this.keystores = keystores;
}
public Wallet copy() {
Wallet copy = new Wallet();
copy.setPolicyType(policyType);
copy.setScriptType(scriptType);
copy.setDefaultPolicy(defaultPolicy.copy());
for(Keystore keystore : keystores) {
copy.getKeystores().add(keystore.copy());
}
return copy;
}
}

View file

@ -1,4 +1,4 @@
module com.sparrowwallet.drongo {
open module com.sparrowwallet.drongo {
requires org.bouncycastle.provider;
requires slf4j.api;
exports com.sparrowwallet.drongo;