use of com.icodici.universa.contract.roles.Role in project universa by UniversaBlockchain.
the class PermissionsTest method decrementWithReference.
@Test
public void decrementWithReference() throws Exception {
Set<PrivateKey> stepaPrivateKeys = new HashSet<>();
stepaPrivateKeys.add(new PrivateKey(Do.read(rootPath + "keys/stepan_mamontov.private.unikey")));
Set<PublicKey> stepaPublicKeys = new HashSet<>();
for (PrivateKey pk : stepaPrivateKeys) {
stepaPublicKeys.add(pk.getPublicKey());
}
Set<String> references = new HashSet<>();
references.add("certification_contract");
Contract c = Contract.fromDslFile(rootPath + "AbonementWithReferenceDSLTemplate.yml");
c.addSignerKeyFromFile(PRIVATE_KEY_PATH);
Role r = c.getPermissions().getFirst("decrement_permission").getRole();
assertThat(r, is(instanceOf(ListRole.class)));
assertFalse(r.isAllowedFor(stepaPublicKeys, null));
assertTrue(r.isAllowedFor(stepaPublicKeys, references));
System.out.println("decrement permission :" + c.getPermissions().get("decrement_permission"));
c.seal();
c.check();
c.traceErrors();
assertTrue(c.isOk());
assertEquals(c, (c.getPermissions().getFirst("decrement_permission").getRole()).getContract());
// Bad contract change: owner has no right to change owner ;)
Set<PrivateKey> badPrivateKeys = new HashSet<>();
badPrivateKeys.add(TestKeys.privateKey(0));
Contract c1 = c.createRevision(TestKeys.privateKey(0));
c1.getStateData().set("units", c.getStateData().getIntOrThrow("units") - 1);
c1.seal();
c1.check();
c1.traceErrors();
// assertEquals(1, c1.getErrors().size());
// ErrorRecord error = c1.getErrors().get(0);
// assertEquals(Errors.FORBIDDEN, error.getError());
assertFalse(c1.isOk());
// bad contract change: good key but no reference
Contract c2 = c.createRevision(stepaPrivateKeys);
c2.getStateData().set("units", c.getStateData().getIntOrThrow("units") - 1);
assertEquals(c2, c2.getPermissions().getFirst("decrement_permission").getRole().getContract());
System.out.println("-------------");
c1.seal();
c1.check();
c1.traceErrors();
// assertEquals(1, c1.getErrors().size());
// ErrorRecord error = c1.getErrors().get(0);
// assertEquals(Errors.FORBIDDEN, error.getError());
assertFalse(c1.isOk());
// good contract change: creator is an owner
Contract c3 = c.createRevision(stepaPrivateKeys);
c3.getStateData().set("units", c.getStateData().getIntOrThrow("units") - 1);
Reference ref = new Reference();
ref.name = "certification_contract";
ref.type = Reference.TYPE_EXISTING;
ref.addMatchingItem(new Contract());
c3.getReferences().put(ref.name, ref);
assertEquals(c3, c3.getPermissions().getFirst("decrement_permission").getRole().getContract());
System.out.println("-------------");
sealCheckTrace(c3, true);
}
use of com.icodici.universa.contract.roles.Role in project universa by UniversaBlockchain.
the class ContractDelta method checkOwnerChanged.
private void checkOwnerChanged() throws Quantiser.QuantiserException {
ChangedItem<Role, Role> oc = (ChangedItem<Role, Role>) stateChanges.get("owner");
if (oc != null) {
stateChanges.remove("owner");
Role creator = changed.getRole("creator");
if (!existing.isPermitted("change_owner", creator))
addError(FORBIDDEN, "state.owner", "creator has no right to change");
}
}
use of com.icodici.universa.contract.roles.Role in project universa by UniversaBlockchain.
the class Contract method checkRootContract.
/**
* Create new root contract to be created. It may have parent, but does not have origin, as it is an origin itself.
*/
private void checkRootContract() throws Quantiser.QuantiserException {
// root contract must be issued ny the issuer
Role issuer = getRole("issuer");
if (issuer == null || !issuer.isValid()) {
addError(BAD_VALUE, "definition.issuer", "missing issuer");
return;
}
// the bad case - no issuer - should be processed normally without exceptions:
Role createdBy = getRole("creator");
if (createdBy == null || !createdBy.isValid()) {
addError(BAD_VALUE, "state.created_by", "invalid creator");
return;
}
if (issuer != null && !issuer.equalKeys(createdBy))
addError(ISSUER_MUST_CREATE, "state.created_by");
if (state.revision != 1)
addError(BAD_VALUE, "state.revision", "must be 1 in a root contract");
if (state.createdAt == null)
state.createdAt = definition.createdAt;
else if (!state.createdAt.equals(definition.createdAt))
addError(BAD_VALUE, "state.created_at", "invalid");
if (state.origin != null)
addError(BAD_VALUE, "state.origin", "must be empty in a root contract");
checkRootDependencies();
}
use of com.icodici.universa.contract.roles.Role in project universa by UniversaBlockchain.
the class Reference method toString.
@Override
public String toString() {
String res = "{";
res += "name:" + name;
res += ", type:" + type;
if (transactional_id.length() > 8)
res += ", transactional_id:" + transactional_id.substring(0, 8) + "...";
else
res += ", transactional_id:" + transactional_id;
res += ", contract_id:" + contract_id;
res += ", required:" + required;
res += ", origin:" + origin;
res += ", signed_by:[";
for (int i = 0; i < signed_by.size(); ++i) {
if (i > 0)
res += ", ";
Role r = signed_by.get(i);
res += r.getName() + ":" + Bytes.toHex(r.getKeys().iterator().next().fingerprint()).substring(0, 8) + "...";
}
res += "]";
res += "}";
return res;
}
use of com.icodici.universa.contract.roles.Role in project universa by UniversaBlockchain.
the class CLIMain method exportPublicKeys.
/**
* Export public keys from specified contract.
*
* @param contract - contract to export.
* @param roleName - from which role keys should be exported.
* @param fileName - name of file to export to.
*/
private static void exportPublicKeys(Contract contract, String roleName, String fileName, boolean base64) throws IOException {
if (fileName == null) {
if (testMode && testRootPath != null) {
fileName = testRootPath + "Universa_" + roleName + "_public_key";
} else {
fileName = "Universa_" + roleName + "_public_key.pub";
}
}
Role role = contract.getRole(roleName);
if (role != null) {
Set<PublicKey> keys = role.getKeys();
int index = 0;
byte[] data;
for (PublicKey key : keys) {
index++;
data = key.pack();
String name = fileName.replaceAll("\\.(pub)$", "_key_" + roleName + "_" + index + ".public.unikey");
if (base64) {
name += ".txt";
}
try (FileOutputStream fs = new FileOutputStream(name)) {
if (base64)
fs.write(Base64.encodeLines(data).getBytes());
else
fs.write(data);
fs.close();
}
}
report(roleName + " export public keys ok");
} else {
report("export public keys error, role does not exist: " + roleName);
}
}
Aggregations