Search in sources :

Example 21 with PrivateKey

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

the class ContractTest method checkSealedBytes.

@Test
public void checkSealedBytes() throws Exception {
    PrivateKey martyPrivateKey = new PrivateKey(Do.read(rootPath + "keys/marty_mcfly.private.unikey"));
    PrivateKey stepaPrivateKey = new PrivateKey(Do.read(rootPath + "keys/stepan_mamontov.private.unikey"));
    PrivateKey manufacturePrivateKey = new PrivateKey(Do.read(rootPath + "_xer0yfe2nn1xthc.private.unikey"));
    Contract delorean = Contract.fromDslFile(rootPath + "DeLoreanOwnership.yml");
    delorean.addSignerKey(manufacturePrivateKey);
    delorean.seal();
    System.out.println("----");
    delorean = delorean.createRevision(manufacturePrivateKey);
    delorean.addSignerKey(manufacturePrivateKey);
    delorean.seal();
    byte[] firstSeal = delorean.getLastSealedBinary();
    System.out.println("--delorean 1--");
    System.out.println(delorean.getRevoking().size());
    TransactionPack tp_before = delorean.getTransactionPack();
    byte[] data = tp_before.pack();
    System.out.println("----");
    TransactionPack tp_after = TransactionPack.unpack(data);
    Contract delorean2 = tp_after.getContract();
    System.out.println("----");
    delorean2.addSignerKey(stepaPrivateKey);
    delorean2.seal();
    System.out.println("--delorean 2--");
    System.out.println(delorean2.getRevoking().size());
    byte[] secondSeal = delorean2.getLastSealedBinary();
    Binder data1 = Boss.unpack(firstSeal);
    byte[] contractBytes1 = data1.getBinaryOrThrow("data");
    for (Object signature : (List) data1.getOrThrow("signatures")) {
        byte[] s = ((Bytes) signature).toArray();
        System.out.println(ExtendedSignature.verify(manufacturePrivateKey.getPublicKey(), s, contractBytes1));
    }
    System.out.println("----");
    Binder data2 = Boss.unpack(secondSeal);
    byte[] contractBytes2 = data2.getBinaryOrThrow("data");
    for (Object signature : (List) data2.getOrThrow("signatures")) {
        byte[] s = ((Bytes) signature).toArray();
        System.out.println("m: " + ExtendedSignature.verify(manufacturePrivateKey.getPublicKey(), s, contractBytes2));
        System.out.println("s: " + ExtendedSignature.verify(stepaPrivateKey.getPublicKey(), s, contractBytes2));
    }
    System.out.println("----");
    for (Object signature : (List) data1.getOrThrow("signatures")) {
        byte[] s = ((Bytes) signature).toArray();
        System.out.println(ExtendedSignature.verify(manufacturePrivateKey.getPublicKey(), s, contractBytes2));
    }
}
Also used : Binder(net.sergeych.tools.Binder) Bytes(net.sergeych.utils.Bytes) PrivateKey(com.icodici.crypto.PrivateKey) Test(org.junit.Test)

Example 22 with PrivateKey

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

the class ContractsServiceTest method checkCreateParcelFotTestNet.

private void checkCreateParcelFotTestNet(String contract_file_payload, String contract_file_payment) throws Exception {
    final String ROOT_PATH = "./src/test_contracts/contractService/";
    PrivateKey privateKey = TestKeys.privateKey(3);
    Set<PrivateKey> privateKeys = new HashSet<>();
    privateKeys.add(privateKey);
    Set<PublicKey> publicKeys = new HashSet<>();
    publicKeys.add(privateKey.getPublicKey());
    Contract payload = Contract.fromDslFile(ROOT_PATH + contract_file_payload);
    payload.addSignerKey(privateKey);
    payload.seal();
    Contract payment = InnerContractsService.createFreshTU(100, publicKeys, true);
    Parcel parcel = ContractsService.createParcel(payload, payment, 20, privateKeys, true);
    assertEquals(parcel.getPayloadContract().getState().getBranchId(), payload.getState().getBranchId());
    assertEquals(parcel.getPaymentContract().getState().getBranchId(), payment.getState().getBranchId());
    assertEquals(parcel.getPayloadContract().getStateData(), payload.getStateData());
    assertEquals(parcel.getPaymentContract().getDefinition().getData(), payment.getDefinition().getData());
    assertEquals(100, parcel.getPaymentContract().getStateData().getIntOrThrow("transaction_units"));
    assertEquals(10000 - 20, parcel.getPaymentContract().getStateData().getIntOrThrow("test_transaction_units"));
    System.out.println("OK");
}
Also used : PrivateKey(com.icodici.crypto.PrivateKey) PublicKey(com.icodici.crypto.PublicKey) HashSet(java.util.HashSet)

Example 23 with PrivateKey

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

the class ContractsServiceTest method createTestTU.

@Test
public void createTestTU() throws Exception {
    PrivateKey privateKey = TestKeys.privateKey(3);
    Set<PublicKey> keys = new HashSet();
    keys.add(privateKey.getPublicKey());
    Contract tu = InnerContractsService.createFreshTU(100, keys, true);
    tu.check();
    tu.traceErrors();
    assertEquals(true, tu.getRole("owner").isAllowedForKeys(keys));
    assertEquals(100, tu.getStateData().getIntOrThrow("transaction_units"));
    assertEquals(10000, tu.getStateData().getIntOrThrow("test_transaction_units"));
    PrivateKey manufacturePrivateKey = new PrivateKey(Do.read("./src/test_contracts/keys/tu_key.private.unikey"));
    Set<PublicKey> badKeys = new HashSet();
    badKeys.add(manufacturePrivateKey.getPublicKey());
    assertEquals(false, tu.getRole("owner").isAllowedForKeys(badKeys));
}
Also used : PrivateKey(com.icodici.crypto.PrivateKey) PublicKey(com.icodici.crypto.PublicKey) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 24 with PrivateKey

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

the class ContractsServiceTest method goodToken.

@Test
public void goodToken() throws Exception {
    Set<PrivateKey> martyPrivateKeys = new HashSet<>();
    Set<PrivateKey> stepaPrivateKeys = new HashSet<>();
    Set<PublicKey> martyPublicKeys = new HashSet<>();
    Set<PublicKey> stepaPublicKeys = new HashSet<>();
    martyPrivateKeys.add(new PrivateKey(Do.read(rootPath + "keys/marty_mcfly.private.unikey")));
    stepaPrivateKeys.add(new PrivateKey(Do.read(rootPath + "keys/stepan_mamontov.private.unikey")));
    for (PrivateKey pk : stepaPrivateKeys) stepaPublicKeys.add(pk.getPublicKey());
    for (PrivateKey pk : martyPrivateKeys) martyPublicKeys.add(pk.getPublicKey());
    Contract notaryContract = ContractsService.createTokenContract(martyPrivateKeys, stepaPublicKeys, "100");
    notaryContract.check();
    notaryContract.traceErrors();
    assertTrue(notaryContract.isOk());
    assertTrue(notaryContract.getOwner().isAllowedForKeys(stepaPublicKeys));
    assertTrue(notaryContract.getIssuer().isAllowedForKeys(martyPrivateKeys));
    assertTrue(notaryContract.getCreator().isAllowedForKeys(martyPrivateKeys));
    assertFalse(notaryContract.getOwner().isAllowedForKeys(martyPrivateKeys));
    assertFalse(notaryContract.getIssuer().isAllowedForKeys(stepaPublicKeys));
    assertFalse(notaryContract.getCreator().isAllowedForKeys(stepaPublicKeys));
    assertTrue(notaryContract.getExpiresAt().isAfter(ZonedDateTime.now().plusMonths(3)));
    assertTrue(notaryContract.getCreatedAt().isBefore(ZonedDateTime.now()));
    assertEquals(InnerContractsService.getDecimalField(notaryContract, "amount"), new Decimal(100));
    assertEquals(notaryContract.getPermissions().get("split_join").size(), 1);
    Binder splitJoinParams = notaryContract.getPermissions().get("split_join").iterator().next().getParams();
    assertEquals(splitJoinParams.get("min_value"), 0.01);
    assertEquals(splitJoinParams.get("min_unit"), 0.01);
    assertEquals(splitJoinParams.get("field_name"), "amount");
    assertTrue(splitJoinParams.get("join_match_fields") instanceof List);
    assertEquals(((List) splitJoinParams.get("join_match_fields")).get(0), "state.origin");
    assertTrue(notaryContract.isPermitted("revoke", stepaPublicKeys));
    assertTrue(notaryContract.isPermitted("revoke", martyPublicKeys));
    assertTrue(notaryContract.isPermitted("change_owner", stepaPublicKeys));
    assertFalse(notaryContract.isPermitted("change_owner", martyPublicKeys));
    assertTrue(notaryContract.isPermitted("split_join", stepaPublicKeys));
    assertFalse(notaryContract.isPermitted("split_join", martyPublicKeys));
}
Also used : Binder(net.sergeych.tools.Binder) PrivateKey(com.icodici.crypto.PrivateKey) Decimal(com.icodici.universa.Decimal) PublicKey(com.icodici.crypto.PublicKey) List(java.util.List) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 25 with PrivateKey

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

the class ContractsServiceTest method goodNotary.

@Test
public void goodNotary() throws Exception {
    Set<PrivateKey> martyPrivateKeys = new HashSet<>();
    Set<PrivateKey> stepaPrivateKeys = new HashSet<>();
    Set<PublicKey> martyPublicKeys = new HashSet<>();
    Set<PublicKey> stepaPublicKeys = new HashSet<>();
    martyPrivateKeys.add(new PrivateKey(Do.read(rootPath + "keys/marty_mcfly.private.unikey")));
    stepaPrivateKeys.add(new PrivateKey(Do.read(rootPath + "keys/stepan_mamontov.private.unikey")));
    for (PrivateKey pk : stepaPrivateKeys) stepaPublicKeys.add(pk.getPublicKey());
    for (PrivateKey pk : martyPrivateKeys) martyPublicKeys.add(pk.getPublicKey());
    Contract notaryContract = ContractsService.createNotaryContract(martyPrivateKeys, stepaPublicKeys);
    notaryContract.check();
    notaryContract.traceErrors();
    assertTrue(notaryContract.isOk());
    assertTrue(notaryContract.getOwner().isAllowedForKeys(stepaPublicKeys));
    assertTrue(notaryContract.getIssuer().isAllowedForKeys(martyPrivateKeys));
    assertTrue(notaryContract.getCreator().isAllowedForKeys(martyPrivateKeys));
    assertFalse(notaryContract.getOwner().isAllowedForKeys(martyPrivateKeys));
    assertFalse(notaryContract.getIssuer().isAllowedForKeys(stepaPublicKeys));
    assertFalse(notaryContract.getCreator().isAllowedForKeys(stepaPublicKeys));
    assertTrue(notaryContract.getExpiresAt().isAfter(ZonedDateTime.now().plusMonths(3)));
    assertTrue(notaryContract.getCreatedAt().isBefore(ZonedDateTime.now()));
    assertTrue(notaryContract.isPermitted("revoke", stepaPublicKeys));
    assertTrue(notaryContract.isPermitted("revoke", martyPublicKeys));
    assertTrue(notaryContract.isPermitted("change_owner", stepaPublicKeys));
    assertFalse(notaryContract.isPermitted("change_owner", martyPublicKeys));
}
Also used : PrivateKey(com.icodici.crypto.PrivateKey) PublicKey(com.icodici.crypto.PublicKey) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

PrivateKey (com.icodici.crypto.PrivateKey)168 Test (org.junit.Test)122 PublicKey (com.icodici.crypto.PublicKey)76 SimpleRole (com.icodici.universa.contract.roles.SimpleRole)29 Contract (com.icodici.universa.contract.Contract)24 Binder (net.sergeych.tools.Binder)22 IOException (java.io.IOException)16 HashSet (java.util.HashSet)14 ListRole (com.icodici.universa.contract.roles.ListRole)12 SQLException (java.sql.SQLException)11 Ignore (org.junit.Ignore)9 File (java.io.File)8 KeyAddress (com.icodici.crypto.KeyAddress)7 KeyRecord (com.icodici.universa.contract.KeyRecord)7 Role (com.icodici.universa.contract.roles.Role)7 Semaphore (java.util.concurrent.Semaphore)7 RoleLink (com.icodici.universa.contract.roles.RoleLink)5 TimeoutException (java.util.concurrent.TimeoutException)5 Decimal (com.icodici.universa.Decimal)4 HashId (com.icodici.universa.HashId)4