Search in sources :

Example 16 with ScriptType

use of com.sparrowwallet.drongo.protocol.ScriptType in project drongo by sparrowwallet.

the class Keystore method rederiveKeystoreFromMaster.

private static void rederiveKeystoreFromMaster(Keystore keystore, List<ChildNumber> derivation) throws MnemonicException {
    ExtendedKey xprv = keystore.getExtendedMasterPrivateKey();
    String masterFingerprint = Utils.bytesToHex(xprv.getKey().getFingerprint());
    DeterministicKey derivedKey = xprv.getKey(derivation);
    DeterministicKey derivedKeyPublicOnly = derivedKey.dropPrivateBytes().dropParent();
    ExtendedKey xpub = new ExtendedKey(derivedKeyPublicOnly, derivedKey.getParentFingerprint(), derivation.isEmpty() ? ChildNumber.ZERO : derivation.get(derivation.size() - 1));
    keystore.setSource(KeystoreSource.SW_SEED);
    keystore.setWalletModel(WalletModel.SPARROW);
    keystore.setKeyDerivation(new KeyDerivation(masterFingerprint, KeyDerivation.writePath(derivation)));
    keystore.setExtendedPublicKey(ExtendedKey.fromDescriptor(xpub.toString()));
    int account = ScriptType.getScriptTypesForPolicyType(PolicyType.SINGLE).stream().mapToInt(scriptType -> scriptType.getAccount(keystore.getKeyDerivation().getDerivationPath())).filter(idx -> idx > -1).findFirst().orElse(0);
    List<ChildNumber> bip47Derivation = KeyDerivation.getBip47Derivation(account);
    DeterministicKey bip47Key = xprv.getKey(bip47Derivation);
    ExtendedKey bip47ExtendedPrivateKey = new ExtendedKey(bip47Key, bip47Key.getParentFingerprint(), bip47Derivation.get(bip47Derivation.size() - 1));
    keystore.setBip47ExtendedPrivateKey(ExtendedKey.fromDescriptor(bip47ExtendedPrivateKey.toString()));
}
Also used : Logger(org.slf4j.Logger) ExtendedKey(com.sparrowwallet.drongo.ExtendedKey) PaymentAddress(com.sparrowwallet.drongo.bip47.PaymentAddress) LoggerFactory(org.slf4j.LoggerFactory) com.sparrowwallet.drongo.crypto(com.sparrowwallet.drongo.crypto) PolicyType(com.sparrowwallet.drongo.policy.PolicyType) ArrayList(java.util.ArrayList) List(java.util.List) KeyPurpose(com.sparrowwallet.drongo.KeyPurpose) Utils(com.sparrowwallet.drongo.Utils) KeyDerivation(com.sparrowwallet.drongo.KeyDerivation) ScriptType(com.sparrowwallet.drongo.protocol.ScriptType) PaymentCode(com.sparrowwallet.drongo.bip47.PaymentCode) KeyDerivation(com.sparrowwallet.drongo.KeyDerivation) ExtendedKey(com.sparrowwallet.drongo.ExtendedKey)

Example 17 with ScriptType

use of com.sparrowwallet.drongo.protocol.ScriptType in project drongo by sparrowwallet.

the class OutputDescriptor method getOutputDescriptor.

// See https://github.com/bitcoin/bitcoin/blob/master/doc/descriptors.md
public static OutputDescriptor getOutputDescriptor(String descriptor) {
    ScriptType scriptType = ScriptType.fromDescriptor(descriptor);
    if (scriptType == null) {
        ExtendedKey.Header header = ExtendedKey.Header.fromExtendedKey(descriptor);
        scriptType = header.getDefaultScriptType();
    }
    if (scriptType == null) {
        throw new IllegalArgumentException("Cannot determine script type from descriptor: " + descriptor);
    }
    int threshold = getMultisigThreshold(descriptor);
    return getOutputDescriptorImpl(scriptType, threshold, descriptor);
}
Also used : ScriptType(com.sparrowwallet.drongo.protocol.ScriptType)

Example 18 with ScriptType

use of com.sparrowwallet.drongo.protocol.ScriptType in project drongo by sparrowwallet.

the class KeystoreTest method testFromSeed.

@Test
public void testFromSeed() throws MnemonicException {
    ScriptType p2pkh = ScriptType.P2PKH;
    DeterministicSeed seed = new DeterministicSeed("absent essay fox snake vast pumpkin height crouch silent bulb excuse razor", "", 0, DeterministicSeed.Type.BIP39);
    Keystore keystore = Keystore.fromSeed(seed, p2pkh.getDefaultDerivation());
    Assert.assertEquals("xpub6D9jqMkBdgTqrzTxXVo2w8yZCa7HvzJTybFevJ2StHSxBRhs8dzsVEke9TQ9QjZCKbWZvzbc8iSScBbsCiA11wT28hZmCv3YmjSFEqCLmMn", keystore.getExtendedPublicKey().toString());
}
Also used : ScriptType(com.sparrowwallet.drongo.protocol.ScriptType) Test(org.junit.Test)

Aggregations

ScriptType (com.sparrowwallet.drongo.protocol.ScriptType)18 Wallet (com.sparrowwallet.drongo.wallet.Wallet)6 Keystore (com.sparrowwallet.drongo.wallet.Keystore)5 KeyDerivation (com.sparrowwallet.drongo.KeyDerivation)4 ImportException (com.sparrowwallet.sparrow.io.ImportException)4 PolicyType (com.sparrowwallet.drongo.policy.PolicyType)3 WalletImportEvent (com.sparrowwallet.sparrow.event.WalletImportEvent)3 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 SecureString (com.sparrowwallet.drongo.SecureString)2 Address (com.sparrowwallet.drongo.address.Address)2 InvalidAddressException (com.sparrowwallet.drongo.address.InvalidAddressException)2 ECKey (com.sparrowwallet.drongo.crypto.ECKey)2 InputStreamReader (java.io.InputStreamReader)2 List (java.util.List)2 Subscribe (com.google.common.eventbus.Subscribe)1 HostAndPort (com.google.common.net.HostAndPort)1 Gson (com.google.gson.Gson)1 JsonElement (com.google.gson.JsonElement)1 JsonParseException (com.google.gson.JsonParseException)1