Search in sources :

Example 1 with Policy

use of com.sparrowwallet.drongo.policy.Policy in project sparrow by sparrowwallet.

the class WalletMapper method map.

@Override
public Wallet map(ResultSet rs, StatementContext ctx) throws SQLException {
    Wallet wallet = new Wallet(rs.getString("wallet.name"));
    wallet.setId(rs.getLong("wallet.id"));
    wallet.setLabel(rs.getString("wallet.label"));
    wallet.setNetwork(Network.values()[rs.getInt("wallet.network")]);
    wallet.setPolicyType(PolicyType.values()[rs.getInt("wallet.policyType")]);
    wallet.setScriptType(ScriptType.values()[rs.getInt("wallet.scriptType")]);
    Policy policy = new Policy(rs.getString("policy.name"), new Miniscript(rs.getString("policy.script")));
    policy.setId(rs.getLong("policy.id"));
    wallet.setDefaultPolicy(policy);
    int storedBlockHeight = rs.getInt("wallet.storedBlockHeight");
    wallet.setStoredBlockHeight(rs.wasNull() ? null : storedBlockHeight);
    int gapLimit = rs.getInt("wallet.gapLimit");
    wallet.gapLimit(rs.wasNull() ? null : gapLimit);
    int watchLast = rs.getInt("wallet.watchLast");
    wallet.setWatchLast(rs.wasNull() ? null : watchLast);
    wallet.setBirthDate(rs.getTimestamp("wallet.birthDate"));
    return wallet;
}
Also used : Policy(com.sparrowwallet.drongo.policy.Policy) Wallet(com.sparrowwallet.drongo.wallet.Wallet) Miniscript(com.sparrowwallet.drongo.policy.Miniscript)

Example 2 with Policy

use of com.sparrowwallet.drongo.policy.Policy in project sparrow by sparrowwallet.

the class PolicyMapper method map.

@Override
public Policy map(ResultSet rs, StatementContext ctx) throws SQLException {
    Policy policy = new Policy(rs.getString("name"), new Miniscript(rs.getString("script")));
    policy.setId(rs.getLong("id"));
    return policy;
}
Also used : Policy(com.sparrowwallet.drongo.policy.Policy) Miniscript(com.sparrowwallet.drongo.policy.Miniscript)

Example 3 with Policy

use of com.sparrowwallet.drongo.policy.Policy in project sparrow by sparrowwallet.

the class ColdcardMultisig method importWallet.

@Override
public Wallet importWallet(InputStream inputStream, String password) throws ImportException {
    Wallet wallet = new Wallet();
    wallet.setPolicyType(PolicyType.MULTI);
    int threshold = 2;
    ScriptType scriptType = ScriptType.P2SH;
    String derivation = null;
    try {
        List<String> lines = CharStreams.readLines(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
        for (String line : lines) {
            line = line.trim();
            if (line.isEmpty()) {
                continue;
            }
            String[] keyValue = line.split(":");
            if (keyValue.length == 2) {
                String key = keyValue[0].trim();
                String value = keyValue[1].trim();
                switch(key) {
                    case "Name":
                        wallet.setName(value.trim());
                        break;
                    case "Policy":
                        threshold = Integer.parseInt(value.split(" ")[0]);
                        break;
                    case "Derivation":
                    case "# derivation":
                        derivation = value;
                        break;
                    case "Format":
                        scriptType = ScriptType.valueOf(value.replace("P2WSH-P2SH", "P2SH_P2WSH"));
                        break;
                    default:
                        if (key.length() == 8 && Utils.isHex(key)) {
                            Keystore keystore = new Keystore("Coldcard");
                            keystore.setSource(KeystoreSource.HW_AIRGAPPED);
                            keystore.setWalletModel(WalletModel.COLDCARD);
                            keystore.setKeyDerivation(new KeyDerivation(key, derivation));
                            keystore.setExtendedPublicKey(ExtendedKey.fromDescriptor(value));
                            wallet.makeLabelsUnique(keystore);
                            wallet.getKeystores().add(keystore);
                        }
                }
            }
        }
        Policy policy = Policy.getPolicy(PolicyType.MULTI, scriptType, wallet.getKeystores(), threshold);
        wallet.setDefaultPolicy(policy);
        wallet.setScriptType(scriptType);
        if (!wallet.isValid()) {
            throw new IllegalStateException("This file does not describe a valid wallet. " + getKeystoreImportDescription());
        }
        return wallet;
    } catch (Exception e) {
        throw new ImportException("Error importing " + getName() + " wallet", e);
    }
}
Also used : Policy(com.sparrowwallet.drongo.policy.Policy) ScriptType(com.sparrowwallet.drongo.protocol.ScriptType) Wallet(com.sparrowwallet.drongo.wallet.Wallet) KeyDerivation(com.sparrowwallet.drongo.KeyDerivation) Keystore(com.sparrowwallet.drongo.wallet.Keystore)

Example 4 with Policy

use of com.sparrowwallet.drongo.policy.Policy in project drongo by sparrowwallet.

the class PolicyTest method testMiniscriptParsing.

@Test
public void testMiniscriptParsing() {
    Keystore keystore1 = new Keystore("Keystore 1");
    Keystore keystore2 = new Keystore("Keystore 2");
    Keystore keystore3 = new Keystore("Keystore 3");
    Policy policy = Policy.getPolicy(PolicyType.SINGLE, ScriptType.P2PKH, List.of(keystore1), 1);
    Assert.assertEquals("pkh(keystore1)", policy.getMiniscript().toString().toLowerCase());
    Assert.assertEquals(1, policy.getNumSignaturesRequired());
    Policy policy2 = Policy.getPolicy(PolicyType.SINGLE, ScriptType.P2SH_P2WPKH, List.of(keystore1), 1);
    Assert.assertEquals("sh(wpkh(keystore1))", policy2.getMiniscript().toString().toLowerCase());
    Assert.assertEquals(1, policy2.getNumSignaturesRequired());
    Policy policy3 = Policy.getPolicy(PolicyType.SINGLE, ScriptType.P2WPKH, List.of(keystore1), 1);
    Assert.assertEquals("wpkh(keystore1)", policy3.getMiniscript().toString().toLowerCase());
    Assert.assertEquals(1, policy3.getNumSignaturesRequired());
    Policy policy4 = Policy.getPolicy(PolicyType.MULTI, ScriptType.P2SH, List.of(keystore1, keystore2, keystore3), 2);
    Assert.assertEquals("sh(sortedmulti(2,keystore1,keystore2,keystore3))", policy4.getMiniscript().toString().toLowerCase());
    Assert.assertEquals(2, policy4.getNumSignaturesRequired());
    Policy policy5 = Policy.getPolicy(PolicyType.MULTI, ScriptType.P2SH_P2WSH, List.of(keystore1, keystore2, keystore3), 2);
    Assert.assertEquals("sh(wsh(sortedmulti(2,keystore1,keystore2,keystore3)))", policy5.getMiniscript().toString().toLowerCase());
    Assert.assertEquals(2, policy5.getNumSignaturesRequired());
    Policy policy6 = Policy.getPolicy(PolicyType.MULTI, ScriptType.P2WSH, List.of(keystore1, keystore2, keystore3), 2);
    Assert.assertEquals("wsh(sortedmulti(2,keystore1,keystore2,keystore3))", policy6.getMiniscript().toString().toLowerCase());
    Assert.assertEquals(2, policy6.getNumSignaturesRequired());
}
Also used : Policy(com.sparrowwallet.drongo.policy.Policy) Test(org.junit.Test)

Aggregations

Policy (com.sparrowwallet.drongo.policy.Policy)4 Miniscript (com.sparrowwallet.drongo.policy.Miniscript)2 Wallet (com.sparrowwallet.drongo.wallet.Wallet)2 KeyDerivation (com.sparrowwallet.drongo.KeyDerivation)1 ScriptType (com.sparrowwallet.drongo.protocol.ScriptType)1 Keystore (com.sparrowwallet.drongo.wallet.Keystore)1 Test (org.junit.Test)1