Search in sources :

Example 76 with Binder

use of net.sergeych.tools.Binder in project universa by UniversaBlockchain.

the class Contract method findSignatureInSeal.

public boolean findSignatureInSeal(PublicKey publicKey) throws Quantiser.QuantiserException {
    if (sealedBinary == null)
        throw new IllegalStateException("failed to create revision");
    Binder data = Boss.unpack(sealedBinary);
    byte[] contractBytes = data.getBinaryOrThrow("data");
    List<Bytes> signatures = data.getListOrThrow("signatures");
    for (Bytes s : signatures) {
        verifySignatureQuantized(publicKey);
        if (ExtendedSignature.verify(publicKey, s.getData(), contractBytes) != null)
            return true;
    }
    return false;
}
Also used : Binder(net.sergeych.tools.Binder) Bytes(net.sergeych.utils.Bytes)

Example 77 with Binder

use of net.sergeych.tools.Binder in project universa by UniversaBlockchain.

the class Contract method sealAsV2.

public byte[] sealAsV2() {
    byte[] theContract = Boss.pack(BossBiMapper.serialize(Binder.of("contract", this, "revoking", revokingItems.stream().map(i -> i.getLastSealedBinary()).collect(Collectors.toList()), "new", newItems.stream().map(i -> i.seal()).collect(Collectors.toList()))));
    // redundand code. already executed here newItems.stream().map(i -> i.seal())
    // newItems.forEach(c -> c.seal());
    Binder result = Binder.of("type", "unicapsule", "version", 2, "data", theContract);
    List<byte[]> signatures = new ArrayList<>();
    keysToSignWith.forEach(key -> {
        signatures.add(ExtendedSignature.sign(key, theContract));
    });
    result.put("data", theContract);
    result.put("signatures", signatures);
    setOwnBinary(result);
    return sealedBinary;
}
Also used : Binder(net.sergeych.tools.Binder)

Example 78 with Binder

use of net.sergeych.tools.Binder in project universa by UniversaBlockchain.

the class CLIMain method checkSj.

private static void checkSj(Contract contract, Permission sj) {
    Binder params = sj.getParams();
    String fieldName = "state.data." + params.getStringOrThrow("field_name");
    reporter.verbose("splitjoins permission fond on field '" + fieldName + "'");
    StringBuilder outcome = new StringBuilder();
    List<Decimal> values = new ArrayList<>();
    contract.getRevoking().forEach(c -> {
        Decimal x;
        if (c.get(fieldName) != null) {
            x = new Decimal(c.get(fieldName).toString());
            values.add(x);
            if (outcome.length() > 0)
                outcome.append(" + ");
            outcome.append(x.toString());
        }
    });
    List<Contract> news = Do.listOf(contract);
    news.addAll(contract.getNew());
    outcome.append(" -> ");
    news.forEach(c -> {
        Decimal x;
        if (c.get(fieldName) != null) {
            if (c != contract)
                outcome.append(" + ");
            x = new Decimal(c.get(fieldName).toString());
            outcome.append(x.toString());
            values.add(x.negate());
        }
    });
    reporter.verbose("operation is: " + outcome.toString());
    Decimal saldo = values.stream().reduce(Decimal.ZERO, (a, b) -> a.add(b));
    if (saldo.compareTo(Decimal.ZERO) == 0)
        reporter.verbose("Saldo looks good (zero)");
    else
        reporter.warning("Saldo is not zero: " + saldo);
}
Also used : Binder(net.sergeych.tools.Binder) Contract(com.icodici.universa.contract.Contract)

Example 79 with Binder

use of net.sergeych.tools.Binder in project universa by UniversaBlockchain.

the class CLIMain method updateFields.

/**
 * Update fields for specified contract.
 *
 * @param contract - contract for update.
 * @param fields   - map of field names and values.
 */
private static void updateFields(Contract contract, HashMap<String, String> fields) throws IOException {
    for (String fieldName : fields.keySet()) {
        report("update field: " + fieldName + " -> " + fields.get(fieldName));
        Binder binder = new Binder();
        Binder data = null;
        try {
            XStream xstream = new XStream(new DomDriver());
            xstream.registerConverter(new MapEntryConverter());
            xstream.alias(fieldName, Binder.class);
            data = Binder.convertAllMapsToBinders(xstream.fromXML(fields.get(fieldName)));
        } catch (Exception xmlEx) {
            // xmlEx.printStackTrace();
            try {
                Gson gson = new GsonBuilder().create();
                binder = Binder.convertAllMapsToBinders(gson.fromJson(fields.get(fieldName), Binder.class));
                data = (Binder) data.get(fieldName);
            } catch (Exception jsonEx) {
                // jsonEx.printStackTrace();
                try {
                    Yaml yaml = new Yaml();
                    data = Binder.convertAllMapsToBinders(yaml.load(fields.get(fieldName)));
                    data = (Binder) data.get(fieldName);
                } catch (Exception yamlEx) {
                    yamlEx.printStackTrace();
                }
            }
        }
        if (data != null) {
            BiDeserializer bm = DefaultBiMapper.getInstance().newDeserializer();
            binder.put("data", bm.deserialize(data));
            contract.set(fieldName, binder);
            report("update field " + fieldName + " ok");
        } else {
            report("update field " + fieldName + " error: no valid data");
        }
    }
    report("contract expires at " + DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(contract.getExpiresAt()));
}
Also used : Binder(net.sergeych.tools.Binder) DomDriver(com.thoughtworks.xstream.io.xml.DomDriver) GsonBuilder(com.google.gson.GsonBuilder) XStream(com.thoughtworks.xstream.XStream) Gson(com.google.gson.Gson) BiDeserializer(net.sergeych.biserializer.BiDeserializer) BackingStoreException(java.util.prefs.BackingStoreException) OptionException(joptsimple.OptionException) Yaml(org.yaml.snakeyaml.Yaml)

Example 80 with Binder

use of net.sergeych.tools.Binder in project universa by UniversaBlockchain.

the class CLIMain method exportContract.

/**
 * Export contract to specified xml or json file.
 *
 * @param contract   - contract to export.
 * @param fileName   - name of file to export to.
 * @param format     - format of file to export to. Can be xml, yaml or json.
 * @param jsonPretty - if true, json will be pretty formated.
 */
public static void exportContract(Contract contract, String fileName, String format, Boolean jsonPretty) throws IOException {
    format = format.toLowerCase();
    report("export format: " + format);
    if (fileName == null) {
        if (testMode && testRootPath != null) {
            fileName = testRootPath + "Universa_" + DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(contract.getCreatedAt());
        } else {
            fileName = "Universa_" + DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(contract.getCreatedAt());
        }
    }
    Binder binder = contract.serialize(DefaultBiMapper.getInstance().newSerializer());
    byte[] data;
    if ("xml".equals(format)) {
        XStream xstream = new XStream(new DomDriver());
        xstream.registerConverter(new MapEntryConverter());
        xstream.alias("contract", Binder.class);
        data = xstream.toXML(binder).getBytes();
    } else if ("yaml".equals(format) || "yml".equals(format)) {
        Yaml yaml = new Yaml();
        data = yaml.dumpAsMap(binder).getBytes();
    } else {
        Gson gson;
        if (jsonPretty) {
            gson = new GsonBuilder().setPrettyPrinting().create();
        } else {
            gson = new GsonBuilder().create();
        }
        String jsonString = gson.toJson(binder);
        data = jsonString.getBytes();
    }
    try (FileOutputStream fs = new FileOutputStream(fileName)) {
        fs.write(data);
        fs.close();
    }
    report(fileName + " export as " + format + " ok");
}
Also used : Binder(net.sergeych.tools.Binder) DomDriver(com.thoughtworks.xstream.io.xml.DomDriver) GsonBuilder(com.google.gson.GsonBuilder) XStream(com.thoughtworks.xstream.XStream) Gson(com.google.gson.Gson) Yaml(org.yaml.snakeyaml.Yaml)

Aggregations

Binder (net.sergeych.tools.Binder)136 Test (org.junit.Test)67 PrivateKey (com.icodici.crypto.PrivateKey)21 Contract (com.icodici.universa.contract.Contract)14 PublicKey (com.icodici.crypto.PublicKey)13 IOException (java.io.IOException)11 KeyRecord (com.icodici.universa.contract.KeyRecord)10 SimpleRole (com.icodici.universa.contract.roles.SimpleRole)10 Yaml (org.yaml.snakeyaml.Yaml)9 Bytes (net.sergeych.utils.Bytes)8 Decimal (com.icodici.universa.Decimal)7 ListRole (com.icodici.universa.contract.roles.ListRole)6 NonNull (org.checkerframework.checker.nullness.qual.NonNull)5 Gson (com.google.gson.Gson)4 GsonBuilder (com.google.gson.GsonBuilder)4 XStream (com.thoughtworks.xstream.XStream)4 DomDriver (com.thoughtworks.xstream.io.xml.DomDriver)4 List (java.util.List)4 RoleLink (com.icodici.universa.contract.roles.RoleLink)3 ItemResult (com.icodici.universa.node.ItemResult)3