Search in sources :

Example 6 with ExtendedKey

use of com.sparrowwallet.drongo.ExtendedKey in project sparrow by sparrowwallet.

the class KeystoneSinglesig method getKeystore.

@Override
public Keystore getKeystore(ScriptType scriptType, InputStream inputStream, String password) throws ImportException {
    try {
        String outputDescriptor = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8)).lines().collect(Collectors.joining("\n"));
        OutputDescriptor descriptor = OutputDescriptor.getOutputDescriptor(outputDescriptor);
        if (descriptor.isMultisig()) {
            throw new IllegalArgumentException("Output descriptor describes a multisig wallet");
        }
        if (descriptor.getScriptType() != scriptType) {
            throw new IllegalArgumentException("Output descriptor describes a " + descriptor.getScriptType().getDescription() + " wallet");
        }
        ExtendedKey xpub = descriptor.getSingletonExtendedPublicKey();
        KeyDerivation keyDerivation = descriptor.getKeyDerivation(xpub);
        Keystore keystore = new Keystore();
        keystore.setLabel(getName());
        keystore.setSource(KeystoreSource.HW_AIRGAPPED);
        keystore.setWalletModel(WalletModel.KEYSTONE);
        keystore.setKeyDerivation(keyDerivation);
        keystore.setExtendedPublicKey(xpub);
        return keystore;
    } catch (IllegalArgumentException e) {
        throw new ImportException("Error getting " + getName() + " keystore - not an output descriptor", e);
    } catch (Exception e) {
        throw new ImportException("Error getting " + getName() + " keystore", e);
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) OutputDescriptor(com.sparrowwallet.drongo.OutputDescriptor) KeyDerivation(com.sparrowwallet.drongo.KeyDerivation) BufferedReader(java.io.BufferedReader) ExtendedKey(com.sparrowwallet.drongo.ExtendedKey)

Example 7 with ExtendedKey

use of com.sparrowwallet.drongo.ExtendedKey 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 8 with ExtendedKey

use of com.sparrowwallet.drongo.ExtendedKey in project drongo by sparrowwallet.

the class Keystore method getExtendedPrivateKey.

public ExtendedKey getExtendedPrivateKey() throws MnemonicException {
    List<ChildNumber> derivation = getKeyDerivation().getDerivation();
    DeterministicKey derivedKey = getExtendedMasterPrivateKey().getKey(derivation);
    ExtendedKey xprv = new ExtendedKey(derivedKey, derivedKey.getParentFingerprint(), derivation.isEmpty() ? ChildNumber.ZERO : derivation.get(derivation.size() - 1));
    // Recreate from xprv string to reset path to single ChildNumber at the derived depth
    return ExtendedKey.fromDescriptor(xprv.toString());
}
Also used : ExtendedKey(com.sparrowwallet.drongo.ExtendedKey)

Example 9 with ExtendedKey

use of com.sparrowwallet.drongo.ExtendedKey in project drongo by sparrowwallet.

the class PSBTTest method validP2wpkh2InputsWithXpub.

@Test
public void validP2wpkh2InputsWithXpub() throws PSBTParseException {
    String psbt = "cHNidP8BAJ0BAAAAAnEOp2q0XFy2Q45gflnMA3YmmBgFrp4N/ZCJASq7C+U1AQAAAAD/////GQmU1qizyMgsy8+y+6QQaqBmObhyqNRHRlwNQliNbWcAAAAAAP////8CAOH1BQAAAAAZdqkUtrwsDuVlWoQ9ea/t0MzD991kNAmIrGBa9AUAAAAAFgAUEYjvjkzgRJ6qyPsUHL9aEXbmoIgAAAAATwEEiLIeA55TDKyAAAAAPbyKXJdp8DGxfnf+oVGGAyIaGP0Y8rmlTGyMGsdcvDUC8jBYSxVdHH8c1FEgplPEjWULQxtnxbLBPyfXFCA3wWkQJ1acUDEAAIAAAACAAAAAgAABAR8A4fUFAAAAABYAFDO5gvkbKPFgySC0q5XljOUN2jpKIgIDMJaA8zx9446mpHzU7NZvH1pJdHxv+4gI7QkDkkPjrVxHMEQCIC1wTO2DDFapCTRL10K2hS3M0QPpY7rpLTjnUlTSu0JFAiAthsQ3GV30bAztoITyopHD2i1kBw92v5uQsZXn7yj3cgEiBgMwloDzPH3jjqakfNTs1m8fWkl0fG/7iAjtCQOSQ+OtXBgnVpxQMQAAgAAAAIAAAACAAAAAAAEAAAAAAQEfAOH1BQAAAAAWABQ4j7lEMH63fvRRl9CwskXgefAR3iICAsd3Fh9z0LfHK57nveZQKT0T8JW8dlatH1Jdpf0uELEQRzBEAiBMsftfhpyULg4mEAV2ElQ5F5rojcqKncO6CPeVOYj6pgIgUh9JynkcJ9cOJzybFGFphZCTYeJb4nTqIA1+CIJ+UU0BIgYCx3cWH3PQt8crnue95lApPRPwlbx2Vq0fUl2l/S4QsRAYJ1acUDEAAIAAAACAAAAAgAAAAAAAAAAAAAAiAgLSDKUC7iiWhtIYFb1DqAY3sGmOH7zb5MrtRF9sGgqQ7xgnVpxQMQAAgAAAAIAAAACAAAAAAAQAAAAA";
    PSBT psbt1 = PSBT.fromString(psbt);
    ExtendedKey extendedPublicKey = psbt1.getExtendedPublicKeys().keySet().iterator().next();
    KeyDerivation keyDerivation = psbt1.getKeyDerivation(extendedPublicKey);
    Assert.assertEquals("27569c50", keyDerivation.getMasterFingerprint());
    Assert.assertEquals("m/49'/0'/0'", keyDerivation.getDerivationPath());
    Assert.assertEquals(2, psbt1.getPsbtInputs().size());
}
Also used : KeyDerivation(com.sparrowwallet.drongo.KeyDerivation) ExtendedKey(com.sparrowwallet.drongo.ExtendedKey) Test(org.junit.Test)

Example 10 with ExtendedKey

use of com.sparrowwallet.drongo.ExtendedKey in project drongo by sparrowwallet.

the class WalletTest method testHighDerivationPath.

@Test
public void testHighDerivationPath() {
    Wallet wallet = new Wallet();
    wallet.setPolicyType(PolicyType.SINGLE);
    wallet.setScriptType(ScriptType.P2WPKH);
    Keystore keystore = new Keystore();
    keystore.setKeyDerivation(new KeyDerivation("ffffffff", "m/84'/0'/2147483646'"));
    ExtendedKey extendedKey = ExtendedKey.fromDescriptor("ypub6WxQGZTrBdeYSD6ZnSxopCGnuS7dhbqc72S3sbjdFjxf8eBR3EJDB3iDMhny2tKogZnpaJcjoHC6zF5Cz1jSMrFFR1wrqfA1MFsWP3ACotd");
    keystore.setExtendedPublicKey(extendedKey);
    wallet.getKeystores().add(keystore);
    List<ChildNumber> derivation = List.of(keystore.getExtendedPublicKey().getKeyChildNumber(), new ChildNumber(0));
    Assert.assertEquals("027ecc656f4b91b92881b6f07cf876cd2e42b20df7acc4df54fc3315fbb2d13e1c", Utils.bytesToHex(extendedKey.getKey(derivation).getPubKey()));
    WalletNode receive0 = new WalletNode(wallet, KeyPurpose.RECEIVE, 0);
    Assert.assertEquals("bc1qarzeu6ncapyvjzdeayjq8vnzp6uvcn4eaeuuqq", receive0.getAddress().toString());
}
Also used : KeyDerivation(com.sparrowwallet.drongo.KeyDerivation) ChildNumber(com.sparrowwallet.drongo.crypto.ChildNumber) ExtendedKey(com.sparrowwallet.drongo.ExtendedKey) Test(org.junit.Test)

Aggregations

ExtendedKey (com.sparrowwallet.drongo.ExtendedKey)10 KeyDerivation (com.sparrowwallet.drongo.KeyDerivation)6 KeyPurpose (com.sparrowwallet.drongo.KeyPurpose)2 PaymentAddress (com.sparrowwallet.drongo.bip47.PaymentAddress)2 PolicyType (com.sparrowwallet.drongo.policy.PolicyType)2 ArrayList (java.util.ArrayList)2 Test (org.junit.Test)2 OutputDescriptor (com.sparrowwallet.drongo.OutputDescriptor)1 Utils (com.sparrowwallet.drongo.Utils)1 Address (com.sparrowwallet.drongo.address.Address)1 PaymentCode (com.sparrowwallet.drongo.bip47.PaymentCode)1 com.sparrowwallet.drongo.crypto (com.sparrowwallet.drongo.crypto)1 ChildNumber (com.sparrowwallet.drongo.crypto.ChildNumber)1 ScriptType (com.sparrowwallet.drongo.protocol.ScriptType)1 PSBTEntry (com.sparrowwallet.drongo.psbt.PSBTEntry)1 BufferedReader (java.io.BufferedReader)1 InputStreamReader (java.io.InputStreamReader)1 Type (java.lang.reflect.Type)1 List (java.util.List)1 InflaterInputStream (java.util.zip.InflaterInputStream)1