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;
}
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;
}
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);
}
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()));
}
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");
}
Aggregations