Search in sources :

Example 1 with RoleLink

use of com.icodici.universa.contract.roles.RoleLink in project universa by UniversaBlockchain.

the class MainTest method createNetConfigContract.

private Contract createNetConfigContract(List<NodeInfo> netConfig, PrivateKey issuerKey) throws IOException {
    Contract contract = new Contract();
    contract.setIssuerKeys(issuerKey.getPublicKey());
    contract.registerRole(new RoleLink("creator", "issuer"));
    ListRole listRole = new ListRole("owner");
    for (NodeInfo ni : netConfig) {
        SimpleRole role = new SimpleRole(ni.getName());
        contract.registerRole(role);
        role.addKeyRecord(new KeyRecord(ni.getPublicKey()));
        listRole.addRole(role);
    }
    listRole.setQuorum(netConfig.size() - 1);
    contract.registerRole(listRole);
    RoleLink ownerLink = new RoleLink("ownerlink", "owner");
    ChangeOwnerPermission changeOwnerPermission = new ChangeOwnerPermission(ownerLink);
    HashMap<String, Object> fieldsMap = new HashMap<>();
    fieldsMap.put("net_config", null);
    Binder modifyDataParams = Binder.of("fields", fieldsMap);
    ModifyDataPermission modifyDataPermission = new ModifyDataPermission(ownerLink, modifyDataParams);
    contract.addPermission(changeOwnerPermission);
    contract.addPermission(modifyDataPermission);
    contract.setExpiresAt(ZonedDateTime.now().plusYears(40));
    contract.getStateData().set("net_config", netConfig);
    contract.addSignerKey(issuerKey);
    contract.seal();
    return contract;
}
Also used : RoleLink(com.icodici.universa.contract.roles.RoleLink) ModifyDataPermission(com.icodici.universa.contract.permissions.ModifyDataPermission) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Binder(net.sergeych.tools.Binder) SimpleRole(com.icodici.universa.contract.roles.SimpleRole) ChangeOwnerPermission(com.icodici.universa.contract.permissions.ChangeOwnerPermission) ListRole(com.icodici.universa.contract.roles.ListRole)

Example 2 with RoleLink

use of com.icodici.universa.contract.roles.RoleLink in project universa by UniversaBlockchain.

the class MainTest method registerContractWithAnonymousId.

@Test
public void registerContractWithAnonymousId() throws Exception {
    TestSpace ts = prepareTestSpace();
    PrivateKey myPrivKey = TestKeys.privateKey(1);
    PublicKey myPubKey = myPrivKey.getPublicKey();
    byte[] myAnonId = myPrivKey.createAnonymousId();
    Contract contract = new Contract();
    contract.setExpiresAt(ZonedDateTime.now().plusDays(90));
    Role r = contract.setIssuerKeys(AnonymousId.fromBytes(myAnonId));
    contract.registerRole(new RoleLink("owner", "issuer"));
    contract.registerRole(new RoleLink("creator", "issuer"));
    contract.addPermission(new ChangeOwnerPermission(r));
    contract.addSignerKey(myPrivKey);
    contract.seal();
    System.out.println("contract.check(): " + contract.check());
    contract.traceErrors();
    // ItemResult itemResult = ts.client.register(contract.getPackedTransaction(), 5000);
    ItemResult itemResult0 = ts.node.node.registerItem(contract);
    // Thread.sleep(1000000000);
    ItemResult itemResult = ts.node.node.waitItem(contract.getId(), 100);
    assertEquals(ItemState.APPROVED, itemResult.state);
}
Also used : SimpleRole(com.icodici.universa.contract.roles.SimpleRole) Role(com.icodici.universa.contract.roles.Role) ListRole(com.icodici.universa.contract.roles.ListRole) RoleLink(com.icodici.universa.contract.roles.RoleLink) PrivateKey(com.icodici.crypto.PrivateKey) PublicKey(com.icodici.crypto.PublicKey) ChangeOwnerPermission(com.icodici.universa.contract.permissions.ChangeOwnerPermission) Test(org.junit.Test)

Example 3 with RoleLink

use of com.icodici.universa.contract.roles.RoleLink in project universa by UniversaBlockchain.

the class MainTest method registerContractWithAnonymousId_bak.

@Test
public void registerContractWithAnonymousId_bak() throws Exception {
    TestSpace ts = prepareTestSpace();
    PrivateKey myPrivKey = TestKeys.privateKey(1);
    PublicKey myPubKey = myPrivKey.getPublicKey();
    byte[] myAnonId = myPrivKey.createAnonymousId();
    Contract contract = new Contract();
    contract.setExpiresAt(ZonedDateTime.now().plusDays(90));
    Role r = contract.setIssuerKeys(myPubKey);
    contract.registerRole(new RoleLink("owner", "issuer"));
    contract.registerRole(new RoleLink("creator", "issuer"));
    contract.addPermission(new ChangeOwnerPermission(r));
    contract.addSignerKey(myPrivKey);
    contract.seal();
    ItemResult itemResult = ts.client.register(contract.getPackedTransaction(), 5000);
    assertEquals(ItemState.APPROVED, itemResult.state);
}
Also used : SimpleRole(com.icodici.universa.contract.roles.SimpleRole) Role(com.icodici.universa.contract.roles.Role) ListRole(com.icodici.universa.contract.roles.ListRole) RoleLink(com.icodici.universa.contract.roles.RoleLink) PrivateKey(com.icodici.crypto.PrivateKey) PublicKey(com.icodici.crypto.PublicKey) ChangeOwnerPermission(com.icodici.universa.contract.permissions.ChangeOwnerPermission) Test(org.junit.Test)

Example 4 with RoleLink

use of com.icodici.universa.contract.roles.RoleLink in project universa by UniversaBlockchain.

the class ParcelTest method setUp.

@Before
public void setUp() throws Exception {
    TransactionPack payloadTpFromFile = new TransactionPack();
    TransactionPack paymentTpFromFile = new TransactionPack();
    TransactionPack payload_tp = new TransactionPack();
    TransactionPack payment_tp = new TransactionPack();
    // fill in with the values
    Contract payload = new Contract();
    payload.setIssuerKeys(TestKeys.publicKey(3));
    payload.addSignerKey(TestKeys.privateKey(3));
    payload.registerRole(new RoleLink("owner", "issuer"));
    payload.registerRole(new RoleLink("creator", "issuer"));
    payload.setExpiresAt(ZonedDateTime.now().plusDays(2));
    payload.seal();
    Contract payment = new Contract();
    payment.setIssuerKeys(TestKeys.publicKey(3));
    payment.addSignerKey(TestKeys.privateKey(3));
    payment.registerRole(new RoleLink("owner", "issuer"));
    payment.registerRole(new RoleLink("creator", "issuer"));
    payment.setExpiresAt(ZonedDateTime.now().plusDays(2));
    payment.seal();
    payload_tp.setContract(payload);
    payment_tp.setContract(payment);
    parcel = new Parcel(payload_tp, payment_tp);
    PrivateKey privateKey = TestKeys.privateKey(3);
    Contract payloadFromFile = Contract.fromDslFile(ROOT_CONTRACT);
    payloadFromFile.addSignerKey(privateKey);
    payloadFromFile.seal();
    Contract paymentFromFile = Contract.fromDslFile(ROOT_CONTRACT);
    paymentFromFile.addSignerKey(privateKey);
    paymentFromFile.seal();
    payloadTpFromFile.setContract(payloadFromFile);
    paymentTpFromFile.setContract(paymentFromFile);
    parcelFromFile = new Parcel(payloadTpFromFile, paymentTpFromFile);
}
Also used : RoleLink(com.icodici.universa.contract.roles.RoleLink) PrivateKey(com.icodici.crypto.PrivateKey) Before(org.junit.Before)

Example 5 with RoleLink

use of com.icodici.universa.contract.roles.RoleLink in project universa by UniversaBlockchain.

the class Node method checkIfContractContainsNetConfig.

private boolean checkIfContractContainsNetConfig(Contract contract) {
    if (!contract.getStateData().containsKey("net_config")) {
        return false;
    }
    // check if owner is list role
    if (!(contract.getOwner() instanceof ListRole)) {
        return false;
    }
    // TODO: network council criteria here
    // check if quorum matches network concil criteria
    ListRole owner = (ListRole) contract.getOwner();
    if (owner.getQuorum() == 0 || owner.getQuorum() < owner.getRoles().size() - 1) {
        return false;
    }
    // check if owner keys set equals to nodes key set
    Object obj = DefaultBiMapper.getInstance().deserializeObject(contract.getStateData().get("net_config"));
    if (!(obj instanceof List)) {
        return false;
    }
    List contractNodes = (List) obj;
    Set<PublicKey> ownerKeys = contract.getOwner().getKeys();
    if (contractNodes.size() != ownerKeys.size() || !contractNodes.stream().allMatch(nodeInfo -> nodeInfo instanceof NodeInfo && ownerKeys.contains(((NodeInfo) nodeInfo).getPublicKey()))) {
        return false;
    }
    for (Permission permission : contract.getPermissions().values()) {
        if (permission instanceof ChangeOwnerPermission) {
            if (!(permission.getRole() instanceof RoleLink) || ((RoleLink) permission.getRole()).getRole() != contract.getOwner())
                return false;
        }
        if (permission instanceof ModifyDataPermission) {
            if (((ModifyDataPermission) permission).getFields().containsKey("net_config")) {
                if (!(permission.getRole() instanceof RoleLink) || ((RoleLink) permission.getRole()).getRole() != contract.getOwner())
                    return false;
            }
        }
    }
    return true;
}
Also used : RoleLink(com.icodici.universa.contract.roles.RoleLink) ModifyDataPermission(com.icodici.universa.contract.permissions.ModifyDataPermission) PublicKey(com.icodici.crypto.PublicKey) ModifyDataPermission(com.icodici.universa.contract.permissions.ModifyDataPermission) ChangeOwnerPermission(com.icodici.universa.contract.permissions.ChangeOwnerPermission) Permission(com.icodici.universa.contract.permissions.Permission) ChangeOwnerPermission(com.icodici.universa.contract.permissions.ChangeOwnerPermission) ListRole(com.icodici.universa.contract.roles.ListRole)

Aggregations

RoleLink (com.icodici.universa.contract.roles.RoleLink)7 PrivateKey (com.icodici.crypto.PrivateKey)4 PublicKey (com.icodici.crypto.PublicKey)4 ChangeOwnerPermission (com.icodici.universa.contract.permissions.ChangeOwnerPermission)4 ListRole (com.icodici.universa.contract.roles.ListRole)4 Test (org.junit.Test)4 SimpleRole (com.icodici.universa.contract.roles.SimpleRole)3 ModifyDataPermission (com.icodici.universa.contract.permissions.ModifyDataPermission)2 Role (com.icodici.universa.contract.roles.Role)2 Binder (net.sergeych.tools.Binder)2 Permission (com.icodici.universa.contract.permissions.Permission)1 File (java.io.File)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 BufferedLogger (net.sergeych.tools.BufferedLogger)1 Before (org.junit.Before)1