Search in sources :

Example 16 with KeyRecord

use of com.icodici.universa.contract.KeyRecord in project universa by UniversaBlockchain.

the class SimpleRole method initWithRecords.

private void initWithRecords(@NonNull Collection records) {
    records.forEach(x -> {
        KeyRecord kr = null;
        AnonymousId anonId = null;
        if (x instanceof KeyRecord)
            kr = (KeyRecord) x;
        else if (x instanceof PublicKey)
            kr = new KeyRecord((PublicKey) x);
        else if (x instanceof AnonymousId)
            anonId = (AnonymousId) x;
        else if (x instanceof PrivateKey)
            kr = new KeyRecord(((PrivateKey) x).getPublicKey());
        else if (x instanceof KeyAddress)
            keyAddresses.add((KeyAddress) x);
        else
            throw new IllegalArgumentException("Cant create KeyRecord from " + x);
        if (anonId != null)
            anonymousIds.add(anonId);
        else if (kr != null)
            keyRecords.put(kr.getPublicKey(), kr);
    });
}
Also used : KeyRecord(com.icodici.universa.contract.KeyRecord) PrivateKey(com.icodici.crypto.PrivateKey) PublicKey(com.icodici.crypto.PublicKey) KeyAddress(com.icodici.crypto.KeyAddress) AnonymousId(com.icodici.universa.contract.AnonymousId)

Example 17 with KeyRecord

use of com.icodici.universa.contract.KeyRecord in project universa by UniversaBlockchain.

the class CLIMainTest method checkSessionReusing.

@Test
public void checkSessionReusing() throws Exception {
    Contract c = Contract.fromDslFile(rootPath + "simple_root_contract.yml");
    c.addSignerKeyFromFile(rootPath + "_xer0yfe2nn1xthc.private.unikey");
    PrivateKey goodKey = c.getKeysToSignWith().iterator().next();
    // let's make this key among owners
    ((SimpleRole) c.getRole("owner")).addKeyRecord(new KeyRecord(goodKey.getPublicKey()));
    c.seal();
    CLIMain.setVerboseMode(true);
    Thread.sleep(1000);
    CLIMain.clearSession();
    System.out.println("---session cleared---");
    CLIMain.registerContract(c);
    Thread.sleep(1000);
    CLIMain.setNodeUrl("http://localhost:8080");
    System.out.println("---session should be reused from variable---");
    CLIMain.registerContract(c);
    CLIMain.saveSession();
    Thread.sleep(1000);
    CLIMain.clearSession(false);
    CLIMain.setNodeUrl("http://localhost:8080");
    System.out.println("---session should be reused from file---");
    CLIMain.registerContract(c);
    CLIMain.saveSession();
    Thread.sleep(1000);
    // CLIMain.clearSession(false);
    // 
    // CLIMain.setNodeUrl(null);
    // 
    // System.out.println("---session should be created for remote network---");
    // 
    // CLIMain.registerContract(c);
    // 
    // CLIMain.saveSession();
    CLIMain.breakSession(-1);
    Thread.sleep(2000);
    CLIMain.clearSession(false);
    CLIMain.setNodeUrl("http://localhost:8080");
    System.out.println("---broken session should be recreated---");
    CLIMain.registerContract(c);
}
Also used : KeyRecord(com.icodici.universa.contract.KeyRecord) PrivateKey(com.icodici.crypto.PrivateKey) SimpleRole(com.icodici.universa.contract.roles.SimpleRole) Contract(com.icodici.universa.contract.Contract) Test(org.junit.Test)

Example 18 with KeyRecord

use of com.icodici.universa.contract.KeyRecord in project universa by UniversaBlockchain.

the class CLIMainTest method revokeCreatedContractWithRole.

@Test
public void revokeCreatedContractWithRole() throws Exception {
    Contract c = Contract.fromDslFile(rootPath + "simple_root_contract.yml");
    c.addSignerKeyFromFile(PRIVATE_KEY_PATH);
    PrivateKey goodKey = c.getKeysToSignWith().iterator().next();
    // let's make this key among owners
    ((SimpleRole) c.getRole("owner")).addKeyRecord(new KeyRecord(goodKey.getPublicKey()));
    c.seal();
    String contractFileName = basePath + "with_role_for_revoke.unicon";
    CLIMain.saveContract(c, contractFileName);
    System.out.println("---");
    System.out.println("register contract");
    System.out.println("---");
    String tuContract = getApprovedTUContract();
    // CLIMain.registerContract(c);
    callMain2("--register", contractFileName, "--verbose", "--tu", tuContract, "--k", rootPath + "keys/stepan_mamontov.private.unikey");
    Thread.sleep(1500);
    System.out.println("---");
    System.out.println("check contract");
    System.out.println("---");
    callMain("--probe", c.getId().toBase64String());
    System.out.println(output);
    assertTrue(output.indexOf(ItemState.APPROVED.name()) >= 0);
    tuContract = getApprovedTUContract();
    callMain2("-revoke", contractFileName, "-k", PRIVATE_KEY_PATH, "-v", "--tu", tuContract, "--k", rootPath + "keys/stepan_mamontov.private.unikey");
    Thread.sleep(1500);
    System.out.println("---");
    System.out.println("check contract after revoke");
    System.out.println("---");
    callMain("--probe", c.getId().toBase64String());
    System.out.println(output);
    assertTrue(output.indexOf(ItemState.REVOKED.name()) >= 0);
}
Also used : KeyRecord(com.icodici.universa.contract.KeyRecord) PrivateKey(com.icodici.crypto.PrivateKey) SimpleRole(com.icodici.universa.contract.roles.SimpleRole) Contract(com.icodici.universa.contract.Contract) Test(org.junit.Test)

Example 19 with KeyRecord

use of com.icodici.universa.contract.KeyRecord in project universa by UniversaBlockchain.

the class ListRoleTest method shouldNotPerformRoleWithQuorumMode.

@Test
public void shouldNotPerformRoleWithQuorumMode() {
    Contract c = new Contract();
    SimpleRole s1 = new SimpleRole("owner");
    SimpleRole s2 = new SimpleRole("owner2");
    s1.addKeyRecord(new KeyRecord(keys.get(0).getPublicKey()));
    s2.addKeyRecord(new KeyRecord(keys.get(1).getPublicKey()));
    c.registerRole(s1);
    c.registerRole(s2);
    ListRole roleList = new ListRole("listQuorumMode");
    roleList.setQuorum(2);
    roleList.addAll(Do.listOf(s1, s2));
    c.registerRole(roleList);
    HashSet<AbstractKey> keys = new HashSet<>(Do.listOf(ListRoleTest.keys.get(1).getPublicKey()));
    assertFalse(roleList.isAllowedForKeys(keys));
}
Also used : KeyRecord(com.icodici.universa.contract.KeyRecord) AbstractKey(com.icodici.crypto.AbstractKey) Contract(com.icodici.universa.contract.Contract) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 20 with KeyRecord

use of com.icodici.universa.contract.KeyRecord in project universa by UniversaBlockchain.

the class ListRoleTest method shouldPerformRoleWithAllMode.

@Test
public void shouldPerformRoleWithAllMode() {
    Contract c = new Contract();
    SimpleRole s1 = new SimpleRole("owner");
    SimpleRole s2 = new SimpleRole("owner2");
    s1.addKeyRecord(new KeyRecord(keys.get(0).getPublicKey()));
    s2.addKeyRecord(new KeyRecord(keys.get(1).getPublicKey()));
    c.registerRole(s1);
    c.registerRole(s2);
    ListRole roleList = new ListRole("listAllMode");
    roleList.setMode(ListRole.Mode.ALL);
    roleList.addRole(s1).addRole(s2);
    HashSet<AbstractKey> keys = new HashSet<>(Do.listOf(ListRoleTest.keys.get(0).getPublicKey(), ListRoleTest.keys.get(1).getPublicKey()));
    assertTrue(roleList.isAllowedForKeys(keys));
}
Also used : KeyRecord(com.icodici.universa.contract.KeyRecord) AbstractKey(com.icodici.crypto.AbstractKey) Contract(com.icodici.universa.contract.Contract) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

KeyRecord (com.icodici.universa.contract.KeyRecord)24 Test (org.junit.Test)21 Contract (com.icodici.universa.contract.Contract)13 Binder (net.sergeych.tools.Binder)10 PrivateKey (com.icodici.crypto.PrivateKey)7 HashSet (java.util.HashSet)7 SimpleRole (com.icodici.universa.contract.roles.SimpleRole)6 AbstractKey (com.icodici.crypto.AbstractKey)4 PublicKey (com.icodici.crypto.PublicKey)3 KeyAddress (com.icodici.crypto.KeyAddress)2 AnonymousId (com.icodici.universa.contract.AnonymousId)2 ItemResult (com.icodici.universa.node.ItemResult)2 IOException (java.io.IOException)2 ConnectException (java.net.ConnectException)2 SocketTimeoutException (java.net.SocketTimeoutException)2 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 Path (java.nio.file.Path)1 ZonedDateTime (java.time.ZonedDateTime)1 BeforeClass (org.junit.BeforeClass)1