use of com.icodici.universa.contract.roles.SimpleRole in project universa by UniversaBlockchain.
the class CLIMainTest method registerManyContractsFromVariousNodes.
//
// @Test
// public void registerManyContracts() throws Exception {
//
// int numContracts = 100;
// List<Contract> contracts = new ArrayList<>();
//
// for (int i = 0; i < numContracts; i++) {
// 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();
//
// contracts.add(c);
// }
//
// Thread.sleep(500);
//
// for (int i = 0; i < numContracts; i++) {
//
// System.out.println("---");
// System.out.println("register contract " + i);
// System.out.println("---");
// final Contract contract = contracts.get(i);
// Thread thread = new Thread(() -> {
// try {
// System.out.println("register contract -> run thread");
// CLIMain.registerContract(contract);
// } catch (IOException e) {
// e.printStackTrace();
// }
// });
//
// thread.start();
// }
//
// Thread.sleep(30000);
//
// for (int i = 0; i < numContracts; i++) {
// System.out.println("---");
// System.out.println("check contract " + i);
// System.out.println("---");
//
// final Contract contract = contracts.get(i);
// Thread thread = new Thread(() -> {
// System.out.println("check contract -> run thread");
// try {
// callMain2("--probe", contract.getId().toBase64String());
// } catch (IOException e) {
// e.printStackTrace();
// } catch (Exception e) {
// e.printStackTrace();
// }
// });
//
// thread.start();
// }
//
// Thread.sleep(30000);
//
// System.out.println("---");
// System.out.println("check contracts in order");
// System.out.println("---");
// for (int i = 0; i < numContracts; i++) {
//
// final Contract contract = contracts.get(i);
// try {
// callMain2("--probe", contract.getId().toBase64String());
// } catch (IOException e) {
// e.printStackTrace();
// } catch (Exception e) {
// e.printStackTrace();
// }
// }
//
// assertEquals(0, CLIMain.getReporter().getErrors().size());
// }
@Test
public void registerManyContractsFromVariousNodes() throws Exception {
ClientNetwork clientNetwork1 = new ClientNetwork("http://localhost:8080", CLIMain.getPrivateKey(), null);
ClientNetwork clientNetwork2 = new ClientNetwork("http://localhost:6002", CLIMain.getPrivateKey(), null);
ClientNetwork clientNetwork3 = new ClientNetwork("http://localhost:6004", CLIMain.getPrivateKey(), null);
int numContracts = 10;
List<Contract> contracts = new ArrayList<>();
for (int i = 0; i < numContracts; i++) {
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();
contracts.add(c);
}
Thread.sleep(500);
for (int i = 0; i < numContracts; i++) {
// System.out.println("---");
// System.out.println("register contract " + i);
// System.out.println("---");
final Contract contract = contracts.get(i);
Thread thread1 = new Thread(() -> {
try {
// System.out.println("register contract on the client 1 -> run thread");
CLIMain.registerContract(contract);
ItemResult r1 = clientNetwork1.register(contract.getPackedTransaction(), 50);
// System.out.println("register contract on the client 1 -> result: " + r1.toString());
} catch (IOException e) {
if (e.getCause() instanceof SocketTimeoutException) {
System.err.println(">>>> ERROR 1: " + e.getMessage());
} else if (e.getCause() instanceof ConnectException) {
System.err.println(">>>> ERROR 1: " + e.getMessage());
} else if (e.getCause() instanceof IllegalStateException) {
System.err.println(">>>> ERROR 1: " + e.getMessage());
} else {
e.printStackTrace();
}
}
});
thread1.start();
Thread thread2 = new Thread(() -> {
try {
// System.out.println("register contracz on the client 2 -> run thread");
CLIMain.registerContract(contract);
ItemResult r2 = clientNetwork2.register(contract.getPackedTransaction(), 50);
// System.out.println("register contract on the client 2 -> result: " + r2.toString());
} catch (IOException e) {
if (e.getCause() instanceof SocketTimeoutException) {
System.err.println(">>>> ERROR 2: " + e.getMessage());
} else if (e.getCause() instanceof ConnectException) {
System.err.println(">>>> ERROR 2: " + e.getMessage());
} else if (e.getCause() instanceof IllegalStateException) {
System.err.println(">>>> ERROR 2: " + e.getMessage());
} else {
e.printStackTrace();
}
}
});
thread2.start();
Thread thread3 = new Thread(() -> {
try {
// System.out.println("register contract on the client 3 -> run thread");
CLIMain.registerContract(contract);
ItemResult r3 = clientNetwork3.register(contract.getPackedTransaction(), 50);
// System.out.println("register contract on the client 3 -> result: " + r3.toString());
} catch (IOException e) {
if (e.getCause() instanceof SocketTimeoutException) {
System.err.println(">>>> ERROR 3: " + e.getMessage());
} else if (e.getCause() instanceof ConnectException) {
System.err.println(">>>> ERROR 3: " + e.getMessage());
} else if (e.getCause() instanceof IllegalStateException) {
System.err.println(">>>> ERROR 3: " + e.getMessage());
} else {
e.printStackTrace();
}
}
});
thread3.start();
}
Thread.sleep(1000);
System.out.println("---");
System.out.println("check contracts in order");
System.out.println("---");
for (int i = 0; i < numContracts; i++) {
final Contract contract = contracts.get(i);
callMain2("--probe", contract.getId().toBase64String());
}
assertEquals(0, CLIMain.getReporter().getErrors().size());
}
use of com.icodici.universa.contract.roles.SimpleRole in project universa by UniversaBlockchain.
the class CLIMainTest method registerContractFromVariousNetworks.
@Test
public void registerContractFromVariousNetworks() throws Exception {
final 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.registerContract(c);
List<ClientNetwork> clientNetworks = new ArrayList<>();
int numConnections = 10;
for (int i = 0; i < numConnections; i++) {
clientNetworks.add(new ClientNetwork("http://localhost:8080", new PrivateKey(2048), null));
}
for (int i = 0; i < numConnections; i++) {
final int index = i;
try {
clientNetworks.get(index).ping();
// System.out.println("result (" + index + "): " + r1.toString());
} catch (IOException e) {
if (e.getCause() instanceof SocketTimeoutException) {
System.err.println(">>>> ERROR 1: " + e.getMessage());
} else if (e.getCause() instanceof ConnectException) {
System.err.println(">>>> ERROR 1: " + e.getMessage());
} else if (e.getCause() instanceof IllegalStateException) {
System.err.println(">>>> ERROR 1: " + e.getMessage());
} else {
e.printStackTrace();
}
}
}
for (int i = 0; i < numConnections; i++) {
final int index = i;
Thread thread1 = new Thread(() -> {
try {
ItemResult r1 = clientNetworks.get(index).register(c.getPackedTransaction());
System.out.println("result from thread (" + index + "): " + r1.toString());
} catch (IOException e) {
if (e.getCause() instanceof SocketTimeoutException) {
System.err.println(">>>> ERROR 1: " + e.getMessage());
} else if (e.getCause() instanceof ConnectException) {
System.err.println(">>>> ERROR 1: " + e.getMessage());
} else if (e.getCause() instanceof IllegalStateException) {
System.err.println(">>>> ERROR 1: " + e.getMessage());
} else {
e.printStackTrace();
}
}
});
thread1.start();
}
// Thread.sleep(10000);
}
use of com.icodici.universa.contract.roles.SimpleRole in project universa by UniversaBlockchain.
the class CLIMainTest method prepareRoot.
@BeforeClass
public static void prepareRoot() throws Exception {
createLocalNetwork();
ownerKey1 = TestKeys.privateKey(3);
ownerKey2 = TestKeys.privateKey(1);
ownerKey3 = TestKeys.privateKey(2);
// new File(rootPath + "/simple_root_contract.unicon").delete();
assertTrue(new File(rootPath + "/simple_root_contract.yml").exists());
assertTrue(new File(rootPath + "/simple_root_contract_v2.yml").exists());
CLIMain.setTestMode();
CLIMain.setTestRootPath(rootPath);
CLIMain.setNodeUrl("http://localhost:8080");
File file = new File(basePath);
if (!file.exists()) {
file.mkdir();
}
ZonedDateTime zdt = ZonedDateTime.now().plusHours(1);
String field = "definition.expires_at";
String value = "definition.expires_at:\n" + " seconds: " + zdt.toEpochSecond() + "\n" + " __type: unixtime";
callMain("-c", "-v", rootPath + "simple_root_contract_v2.yml", "-name", basePath + "contract1.unicon", "-k", rootPath + "_xer0yfe2nn1xthc.private.unikey", "-set", field, "-value", value);
callMain("-c", rootPath + "simple_root_contract_v2.yml", "-name", basePath + "contract2.unicon", "-k", rootPath + "_xer0yfe2nn1xthc.private.unikey", "-set", field, "-value", value);
callMain("-c", rootPath + "simple_root_contract_v2.yml", "-name", basePath + "contract3.unicon", "-k", rootPath + "_xer0yfe2nn1xthc.private.unikey", "-set", field, "-value", value);
callMain("-c", rootPath + "simple_root_contract_v2.yml", "-name", basePath + "contract_to_export.unicon", "-set", field, "-value", value);
Contract c1 = Contract.fromDslFile(rootPath + "simple_root_contract.yml");
c1.addSignerKeyFromFile(rootPath + "_xer0yfe2nn1xthc.private.unikey");
PrivateKey goodKey1 = c1.getKeysToSignWith().iterator().next();
// let's make this key among owners
((SimpleRole) c1.getRole("owner")).addKeyRecord(new KeyRecord(goodKey1.getPublicKey()));
c1.seal();
CLIMain.saveContract(c1, basePath + "contract_for_revoke1.unicon");
Contract c2 = Contract.fromDslFile(rootPath + "another_root_contract_v2.yml");
c2.addSignerKeyFromFile(rootPath + "_xer0yfe2nn1xthc.private.unikey");
PrivateKey goodKey2 = c2.getKeysToSignWith().iterator().next();
// let's make this key among owners
((SimpleRole) c2.getRole("owner")).addKeyRecord(new KeyRecord(goodKey2.getPublicKey()));
c2.seal();
CLIMain.saveContract(c2, basePath + "contract_for_revoke2.unicon");
Contract c3 = Contract.fromDslFile(rootPath + "simple_root_contract_v2.yml");
c3.addSignerKeyFromFile(rootPath + "_xer0yfe2nn1xthc.private.unikey");
PrivateKey goodKey3 = c3.getKeysToSignWith().iterator().next();
// let's make this key among owners
((SimpleRole) c3.getRole("owner")).addKeyRecord(new KeyRecord(goodKey3.getPublicKey()));
c3.seal();
CLIMain.saveContract(c3, basePath + "contract_for_revoke3.unicon");
callMain("-e", basePath + "contract1.unicon", "-name", basePath + "contract_to_import.json");
callMain("-e", basePath + "contract1.unicon", "-name", basePath + "contract_to_import.xml");
callMain("-e", basePath + "contract1.unicon", "-name", basePath + "contract_to_import.XML");
callMain("-e", basePath + "contract1.unicon", "-name", basePath + "contract_to_import.yaml");
callMain("-i", basePath + "contract_to_import.json", "-name", basePath + "not_signed_contract.unicon");
Path path = Paths.get(rootPath + "packedContract.unicon");
byte[] data = Files.readAllBytes(path);
Set<PrivateKey> keys = new HashSet<>();
keys.add(new PrivateKey(Do.read(PRIVATE_KEY_PATH)));
Contract contract = createCoin100apiv3();
contract.addSignerKey(keys.iterator().next());
contract.seal();
CLIMain.saveContract(contract, basePath + "packedContract.unicon");
callMain("--register", basePath + "packedContract.unicon", "--wait", "5000");
Contract packedContract = ContractsService.createSplit(contract, 1, FIELD_NAME, keys);
packedContract.addSignerKey(keys.iterator().next());
packedContract.seal();
CLIMain.saveContract(packedContract, basePath + "packedContract.unicon", true, true);
// try (FileOutputStream fs = new FileOutputStream(basePath + "packedContract.unicon")) {
// fs.write(data);
// fs.close();
// }
path = Paths.get(rootPath + "packedContract.unicon");
data = Files.readAllBytes(path);
try (FileOutputStream fs = new FileOutputStream(basePath + "packedContract2.unicon")) {
fs.write(data);
fs.close();
}
path = Paths.get(rootPath + "packedContract_new_item.unicon");
data = Files.readAllBytes(path);
try (FileOutputStream fs = new FileOutputStream(basePath + "packedContract_new_item.unicon")) {
fs.write(data);
fs.close();
}
path = Paths.get(rootPath + "packedContract_revoke.unicon");
data = Files.readAllBytes(path);
try (FileOutputStream fs = new FileOutputStream(basePath + "packedContract_revoke.unicon")) {
fs.write(data);
fs.close();
}
}
use of com.icodici.universa.contract.roles.SimpleRole in project universa by UniversaBlockchain.
the class ContractsService method startSwap.
/**
* First step of swap procedure. Calls from swapper1 part.
*<br><br>
* Get lists of contracts.
*<br><br>
* Service create new revisions of existing contracts, change owners,
* added transactional sections with references to each other with asks two signs of swappers
* and sign contract that was own for calling part.
*<br><br>
* Swap procedure consist from three steps:<br>
* (1) prepare contracts with creating transactional section on the first swapper site, sign only one contract;<br>
* (2) sign contracts on the second swapper site;<br>
* (3) sign lost contracts on the first swapper site and finishing swap.
*<br><br>
* @param contracts1 is list of own for calling part (swapper1 owned), existing or new revision of contract
* @param contracts2 is list of foreign for calling part (swapper2 owned), existing or new revision contract
* @param fromKeys is own for calling part (swapper1 keys) private keys
* @param toKeys is foreign for calling part (swapper2 keys) public keys
* @param createNewRevision if true - create new revision of given contracts. If false - use them as new revisions.
* @return swap contract including new revisions of old contracts swapping between;
* should be send to partner (swapper2) and he should go to step (2) of the swap procedure.
*/
public static synchronized Contract startSwap(List<Contract> contracts1, List<Contract> contracts2, Set<PrivateKey> fromKeys, Set<PublicKey> toKeys, boolean createNewRevision) {
Set<PublicKey> fromPublicKeys = new HashSet<>();
for (PrivateKey pk : fromKeys) {
fromPublicKeys.add(pk.getPublicKey());
}
// first of all we creating main swap contract which will include new revisions of contract for swap
// you can think about this contract as about transaction
Contract swapContract = new Contract();
Contract.Definition cd = swapContract.getDefinition();
// by default, transactions expire in 30 days
cd.setExpiresAt(swapContract.getCreatedAt().plusDays(30));
SimpleRole issuerRole = new SimpleRole("issuer");
for (PrivateKey k : fromKeys) {
KeyRecord kr = new KeyRecord(k.getPublicKey());
issuerRole.addKeyRecord(kr);
}
swapContract.registerRole(issuerRole);
swapContract.createRole("owner", issuerRole);
swapContract.createRole("creator", issuerRole);
// now we will prepare new revisions of contracts
// create new revisions of contracts and create transactional sections in it
List<Contract> newContracts1 = new ArrayList<>();
for (Contract c : contracts1) {
Contract nc;
if (createNewRevision) {
nc = c.createRevision(fromKeys);
} else {
nc = c;
}
nc.createTransactionalSection();
nc.getTransactional().setId(HashId.createRandom().toBase64String());
newContracts1.add(nc);
}
List<Contract> newContracts2 = new ArrayList<>();
for (Contract c : contracts2) {
Contract nc;
if (createNewRevision) {
nc = c.createRevision();
} else {
nc = c;
}
nc.createTransactionalSection();
nc.getTransactional().setId(HashId.createRandom().toBase64String());
newContracts2.add(nc);
}
// prepare roles for references
// it should new owners and old creators in new revisions of contracts
SimpleRole ownerFrom = new SimpleRole("owner");
SimpleRole creatorFrom = new SimpleRole("creator");
for (PrivateKey k : fromKeys) {
KeyRecord kr = new KeyRecord(k.getPublicKey());
ownerFrom.addKeyRecord(kr);
creatorFrom.addKeyRecord(kr);
}
SimpleRole ownerTo = new SimpleRole("owner");
SimpleRole creatorTo = new SimpleRole("creator");
for (PublicKey k : toKeys) {
KeyRecord kr = new KeyRecord(k);
ownerTo.addKeyRecord(kr);
creatorTo.addKeyRecord(kr);
}
for (Contract nc1 : newContracts1) {
for (Contract nc2 : newContracts2) {
Reference reference = new Reference(nc1);
reference.transactional_id = nc2.getTransactional().getId();
reference.type = Reference.TYPE_TRANSACTIONAL;
reference.required = true;
reference.signed_by = new ArrayList<>();
reference.signed_by.add(ownerFrom);
reference.signed_by.add(creatorTo);
nc1.getTransactional().addReference(reference);
}
}
for (Contract nc2 : newContracts2) {
for (Contract nc1 : newContracts1) {
Reference reference = new Reference(nc2);
reference.transactional_id = nc1.getTransactional().getId();
reference.type = Reference.TYPE_TRANSACTIONAL;
reference.required = true;
reference.signed_by = new ArrayList<>();
reference.signed_by.add(ownerTo);
reference.signed_by.add(creatorFrom);
nc2.getTransactional().addReference(reference);
}
}
// swap owners in this contracts
for (Contract nc : newContracts1) {
nc.setOwnerKeys(toKeys);
nc.seal();
}
for (Contract nc : newContracts2) {
nc.setOwnerKeys(fromPublicKeys);
nc.seal();
}
// finally on this step add created new revisions to main swap contract
for (Contract nc : newContracts1) {
swapContract.addNewItems(nc);
}
for (Contract nc : newContracts2) {
swapContract.addNewItems(nc);
}
swapContract.seal();
return swapContract;
}
use of com.icodici.universa.contract.roles.SimpleRole in project universa by UniversaBlockchain.
the class ContractsService method createNotaryContract.
/**
* Creates a simple notary contract for given keys.
*<br><br>
* The service creates a notary contract with issuer, creator and owner roles;
* with change_owner permission for owner and revoke permissions for owner and issuer.
* By default expires at time is set to 60 months from now.
*<br><br>
* @param issuerKeys is issuer private keys.
* @param ownerKeys is owner public keys.
* @return signed and sealed contract, ready for register.
*/
public static synchronized Contract createNotaryContract(Set<PrivateKey> issuerKeys, Set<PublicKey> ownerKeys) {
Contract notaryContract = new Contract();
notaryContract.setApiLevel(3);
Contract.Definition cd = notaryContract.getDefinition();
cd.setExpiresAt(ZonedDateTime.now().plusMonths(60));
Binder data = new Binder();
data.set("name", "Default notary");
data.set("description", "Default notary description.");
cd.setData(data);
SimpleRole issuerRole = new SimpleRole("issuer");
for (PrivateKey k : issuerKeys) {
KeyRecord kr = new KeyRecord(k.getPublicKey());
issuerRole.addKeyRecord(kr);
}
SimpleRole ownerRole = new SimpleRole("owner");
for (PublicKey k : ownerKeys) {
KeyRecord kr = new KeyRecord(k);
ownerRole.addKeyRecord(kr);
}
notaryContract.registerRole(issuerRole);
notaryContract.createRole("issuer", issuerRole);
notaryContract.createRole("creator", issuerRole);
notaryContract.registerRole(ownerRole);
notaryContract.createRole("owner", ownerRole);
ChangeOwnerPermission changeOwnerPerm = new ChangeOwnerPermission(ownerRole);
notaryContract.addPermission(changeOwnerPerm);
RevokePermission revokePerm1 = new RevokePermission(ownerRole);
notaryContract.addPermission(revokePerm1);
RevokePermission revokePerm2 = new RevokePermission(issuerRole);
notaryContract.addPermission(revokePerm2);
notaryContract.seal();
notaryContract.addSignatureToSeal(issuerKeys);
return notaryContract;
}
Aggregations