Search in sources :

Example 11 with PublicKey

use of com.icodici.crypto.PublicKey in project universa by UniversaBlockchain.

the class ExtendedSignatureTest method extractPublicKeyNull.

@Test
public void extractPublicKeyNull() throws Exception {
    byte[] data = "Hello world".getBytes();
    PrivateKey k = TestKeys.privateKey(3);
    byte[] signature = ExtendedSignature.sign(k, data);
    PublicKey pubKey = k.getPublicKey();
    ExtendedSignature es = ExtendedSignature.verify(pubKey, signature, data);
    assertNotNull(es);
    assertAlmostSame(es.getCreatedAt(), ZonedDateTime.now());
    assertEquals(ExtendedSignature.keyId(k), ExtendedSignature.keyId(pubKey));
    assertEquals(ExtendedSignature.keyId(k), ExtendedSignature.extractKeyId(signature));
    assertEquals(null, es.getPublicKey());
}
Also used : PrivateKey(com.icodici.crypto.PrivateKey) PublicKey(com.icodici.crypto.PublicKey) Test(org.junit.Test)

Example 12 with PublicKey

use of com.icodici.crypto.PublicKey in project universa by UniversaBlockchain.

the class ExtendedSignatureTest method verifyOldSignatureWithSha512Only.

@Test
public void verifyOldSignatureWithSha512Only() throws Exception {
    String oldSignatureHex = "17 23 73 69 67 6E C4 00 01 D3 2C 73 A2 33 D9 8B 4A 3E 73 02 53 E7 15 D9 C9 9E 8B 08 A3 6A A4 76 ED 5E E3 81 82 1F 47 A2 A1 2D 2B 3F CF 73 90 47 2C 4F 3C 88 76 59 BB 9D 67 02 F1 8F AE 5E 43 3F A3 7E 69 51 4F 7F 32 CC 73 A3 F4 86 EB 49 AA FC 96 97 5B 30 8B 3D F1 29 02 0B 1D 65 D4 DD D1 1F D6 AD 3E AC 1C 5E 0B FC F9 3C 63 11 FE BC 2D 8F CC 35 FD 3E 48 AF 6A B5 98 0A 79 E2 38 A4 46 70 F8 D4 F6 A7 43 3D F3 76 12 65 4D 80 4C 7E 09 6B D1 72 A8 1C E2 60 68 7A FD 9B 1A 9C 78 E3 62 F2 BF FF C0 24 27 5C AE 8E FA 3F B5 59 26 02 D1 0D 55 B6 4C FE 6A 99 58 50 45 9B AB 88 BC E1 1F 26 DA 0D 8D 87 B1 D9 D0 52 EA 20 09 46 0D D0 32 CA 31 53 DA 29 32 7D E4 8E D1 77 94 83 87 68 3A 5B 65 0D 94 E4 A3 8B 7A 14 E2 4B 7B CA F0 30 25 A9 87 51 01 F6 2F FC CD FF 57 BE F4 D5 14 6C 5A 3D 0F 25 38 1E 90 C3 9E D6 06 23 65 78 74 73 BC 82 1F 33 73 68 61 35 31 32 BC 40 B7 F7 83 BA ED 82 97 F0 DB 91 74 62 18 4F F4 F0 8E 69 C2 D5 E5 F7 9A 94 26 00 F9 72 5F 58 CE 1F 29 C1 81 39 BF 80 B0 6C 0F FF 2B DD 34 73 84 52 EC F4 0C 48 8C 22 A7 E3 D8 0C DF 6F 9C 1C 0D 47 53 63 72 65 61 74 65 64 5F 61 74 79 20 28 16 55 85 1B 6B 65 79 BC 21 07 7A 32 A6 07 72 37 6F E4 DA D9 43 55 34 19 45 14 1D 17 14 28 B6 F2 3D CE 11 1D 6F 52 4B DF 60 13";
    String oldZonedDateTimePackedHex = "0F 1B 6E 6F 77 79 20 28 16 55 85";
    byte[] data = "Hello world".getBytes();
    PrivateKey k = TestKeys.privateKey(3);
    byte[] signature = Bytes.fromHex(oldSignatureHex).getData();
    PublicKey pubKey = k.getPublicKey();
    ExtendedSignature es = ExtendedSignature.verify(pubKey, signature, data);
    assertNotNull(es);
    ZonedDateTime savedCreationTime = Boss.unpack(Bytes.fromHex(oldZonedDateTimePackedHex).getData()).getZonedDateTimeOrThrow("now");
    assertAlmostSame(es.getCreatedAt(), savedCreationTime);
    assertEquals(ExtendedSignature.keyId(k), ExtendedSignature.keyId(pubKey));
    assertEquals(ExtendedSignature.keyId(k), ExtendedSignature.extractKeyId(signature));
}
Also used : PrivateKey(com.icodici.crypto.PrivateKey) ZonedDateTime(java.time.ZonedDateTime) PublicKey(com.icodici.crypto.PublicKey) Test(org.junit.Test)

Example 13 with PublicKey

use of com.icodici.crypto.PublicKey in project universa by UniversaBlockchain.

the class PermissionsTest method revokeWithReference.

@Test
public void revokeWithReference() 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 + "NotaryWithReferenceDSLTemplate.yml");
    c.addSignerKeyFromFile(PRIVATE_KEY_PATH);
    Role r = c.getPermissions().getFirst("revoke").getRole();
    assertThat(r, is(instanceOf(ListRole.class)));
    assertFalse(r.isAllowedFor(stepaPublicKeys, null));
    assertTrue(r.isAllowedFor(stepaPublicKeys, references));
    System.out.println("revoke permission :" + c.getPermissions().get("revoke"));
    c.seal();
    c.check();
    c.traceErrors();
    assertTrue(c.isOk());
    assertEquals(c, (c.getPermissions().getFirst("revoke").getRole()).getContract());
    // Bad contract change: owner has no right to change owner ;)
    Contract c1 = c.createRevision(TestKeys.privateKey(0));
    c1.setOwnerKey(ownerKey2);
    assertNotEquals(c.getOwner(), c1.getOwner());
    c1.seal();
    c1.check();
    c1.traceErrors();
    assertEquals(1, c1.getErrors().size());
    ErrorRecord error = c1.getErrors().get(0);
    assertEquals(Errors.FORBIDDEN, error.getError());
    // bad contract change: good key but bad reference
    Contract c2 = ContractsService.createRevocation(c, stepaPrivateKeys.iterator().next());
    assertEquals(c2.getRevoking().get(0), c2.getRevoking().get(0).getPermissions().getFirst("revoke").getRole().getContract());
    c2.seal();
    c2.check();
    c2.traceErrors();
    assertEquals(1, c2.getErrors().size());
    error = c2.getErrors().get(0);
    assertEquals(Errors.FORBIDDEN, error.getError());
    // good contract change: creator is an owner
    Contract c3 = ContractsService.createRevocation(c, stepaPrivateKeys.iterator().next());
    Reference ref = new Reference();
    ref.name = "certification_contract";
    ref.type = Reference.TYPE_EXISTING;
    ref.addMatchingItem(new Contract());
    c3.getRevoking().get(0).getReferences().put(ref.name, ref);
    // assertEquals(c3.getRevoking().get(0), c3.getPermissions().getFirst("revoke").getRole().getContract());
    System.out.println("-------------");
    sealCheckTrace(c3, true);
}
Also used : ListRole(com.icodici.universa.contract.roles.ListRole) SimpleRole(com.icodici.universa.contract.roles.SimpleRole) Role(com.icodici.universa.contract.roles.Role) PrivateKey(com.icodici.crypto.PrivateKey) PublicKey(com.icodici.crypto.PublicKey) HashSet(java.util.HashSet) ErrorRecord(com.icodici.universa.ErrorRecord) Test(org.junit.Test)

Example 14 with PublicKey

use of com.icodici.crypto.PublicKey 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);
}
Also used : ListRole(com.icodici.universa.contract.roles.ListRole) SimpleRole(com.icodici.universa.contract.roles.SimpleRole) Role(com.icodici.universa.contract.roles.Role) PrivateKey(com.icodici.crypto.PrivateKey) PublicKey(com.icodici.crypto.PublicKey) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 15 with PublicKey

use of com.icodici.crypto.PublicKey in project universa by UniversaBlockchain.

the class BaseNetworkTest method registerGoodParcel.

@Test(timeout = 900000)
public void registerGoodParcel() throws Exception {
    if (node == null) {
        System.out.println("network not inited");
        return;
    }
    Set<PrivateKey> stepaPrivateKeys = new HashSet<>();
    Set<PublicKey> stepaPublicKeys = new HashSet<>();
    stepaPrivateKeys.add(new PrivateKey(Do.read(ROOT_PATH + "keys/stepan_mamontov.private.unikey")));
    for (PrivateKey pk : stepaPrivateKeys) {
        stepaPublicKeys.add(pk.getPublicKey());
    }
    Thread.sleep(500);
    // LogPrinter.showDebug(true);
    int N = 100;
    for (int k = 0; k < 1; k++) {
        for (int i = 0; i < N; i++) {
            Contract stepaCoins = Contract.fromDslFile(ROOT_PATH + "stepaCoins.yml");
            stepaCoins.addSignerKey(stepaPrivateKeys.iterator().next());
            stepaCoins.seal();
            stepaCoins.check();
            stepaCoins.traceErrors();
            Parcel parcel = createParcelWithClassTU(stepaCoins, stepaPrivateKeys);
            // System.out.println("-------------- register parcel " + parcel.getId() + " (iteration " + i + ") ------------");
            node.registerParcel(parcel);
            synchronized (tuContractLock) {
                tuContract = parcel.getPaymentContract();
            }
            for (Node n : nodesMap.values()) {
                try {
                    // System.out.println("-------------- wait parcel " + parcel.getId() + " on the node " + n + " (iteration " + i + ") ------------");
                    n.waitParcel(parcel.getId(), 25000);
                    ItemResult r = n.waitItem(parcel.getPayloadContract().getId(), 8000);
                    int numIterations = 0;
                    while (!r.state.isConsensusFound()) {
                        System.out.println("wait for consensus receiving on the node " + n + " state is " + r.state);
                        Thread.sleep(500);
                        n.waitParcel(parcel.getId(), 25000);
                        r = n.waitItem(parcel.getPayloadContract().getId(), 8000);
                        numIterations++;
                        if (numIterations > 20)
                            break;
                    }
                    assertEquals("In node " + n + " parcel " + parcel.getId(), ItemState.APPROVED, r.state);
                } catch (TimeoutException e) {
                    // System.out.println(n.ping());
                    // //                        System.out.println(n.traceTasksPool());
                    // System.out.println(n.traceParcelProcessors());
                    // System.out.println(n.traceItemProcessors());
                    fail("timeout, node " + n + " parcel " + parcel.getId() + " parcel " + parcel.getId() + " (iteration " + i + ")");
                }
            }
            node.waitParcel(parcel.getId(), 25000);
            ItemResult r = node.waitItem(parcel.getPayloadContract().getId(), 8000);
            assertEquals("after: In node " + node + " item " + parcel.getId(), ItemState.APPROVED, r.state);
            System.out.println("-------------- parcel " + parcel.getId() + " registered (iteration " + i + ")------------");
        // Thread.sleep(5000);
        // System.out.println("-------------- parcel " + parcel.getId() + " wait finished (iteration " + i + ")------------");
        }
    }
}
Also used : PrivateKey(com.icodici.crypto.PrivateKey) PublicKey(com.icodici.crypto.PublicKey) TimeoutException(java.util.concurrent.TimeoutException) Test(org.junit.Test)

Aggregations

PublicKey (com.icodici.crypto.PublicKey)84 PrivateKey (com.icodici.crypto.PrivateKey)75 Test (org.junit.Test)57 SimpleRole (com.icodici.universa.contract.roles.SimpleRole)17 HashSet (java.util.HashSet)14 Binder (net.sergeych.tools.Binder)13 ListRole (com.icodici.universa.contract.roles.ListRole)11 Role (com.icodici.universa.contract.roles.Role)7 Contract (com.icodici.universa.contract.Contract)6 KeyAddress (com.icodici.crypto.KeyAddress)4 RoleLink (com.icodici.universa.contract.roles.RoleLink)4 KeyRecord (com.icodici.universa.contract.KeyRecord)3 ChangeOwnerPermission (com.icodici.universa.contract.permissions.ChangeOwnerPermission)3 Permission (com.icodici.universa.contract.permissions.Permission)3 TimeoutException (java.util.concurrent.TimeoutException)3 Decimal (com.icodici.universa.Decimal)2 ErrorRecord (com.icodici.universa.ErrorRecord)2 HashId (com.icodici.universa.HashId)2 Quantiser (com.icodici.universa.node2.Quantiser)2 List (java.util.List)2