use of com.icodici.crypto.PrivateKey in project universa by UniversaBlockchain.
the class CLIMainTest method anonymizeAllRolesForTwoContracts.
@Test
public void anonymizeAllRolesForTwoContracts() throws Exception {
callMain2("-create", rootPath + "TokenDSLTemplate.yml", "-name", basePath + "forRoleAnonymizing1.unicon", "-k", rootPath + "_xer0yfe2nn1xthc.private.unikey");
callMain2("-create", rootPath + "TokenDSLTemplate.yml", "-name", basePath + "forRoleAnonymizing2.unicon", "-k", rootPath + "_xer0yfe2nn1xthc.private.unikey");
assertTrue(new File(basePath + "forRoleAnonymizing1.unicon").exists());
assertTrue(new File(basePath + "forRoleAnonymizing2.unicon").exists());
callMain("-anonymize", basePath + "forRoleAnonymizing1.unicon", basePath + "forRoleAnonymizing2.unicon");
assertTrue(new File(basePath + "forRoleAnonymizing1_anonymized.unicon").exists());
assertTrue(new File(basePath + "forRoleAnonymizing2_anonymized.unicon").exists());
System.out.println(output);
PrivateKey key = new PrivateKey(Do.read(rootPath + "_xer0yfe2nn1xthc.private.unikey"));
PrivateKey ownerKey = new PrivateKey(Do.read(rootPath + "keys/stepan_mamontov.private.unikey"));
Contract contract1 = CLIMain.loadContract(basePath + "forRoleAnonymizing1_anonymized.unicon", true);
assertFalse(contract1.getOwner().getKeys().contains(ownerKey.getPublicKey()));
assertFalse(contract1.getIssuer().getKeys().contains(key.getPublicKey()));
assertFalse(contract1.getCreator().getKeys().contains(key.getPublicKey()));
Contract anonPublishedContract1 = new Contract(contract1.getLastSealedBinary());
assertFalse(anonPublishedContract1.getOwner().getKeys().contains(ownerKey.getPublicKey()));
assertFalse(anonPublishedContract1.getIssuer().getKeys().contains(key.getPublicKey()));
assertFalse(anonPublishedContract1.getCreator().getKeys().contains(key.getPublicKey()));
assertFalse(anonPublishedContract1.getSealedByKeys().contains(key.getPublicKey()));
assertFalse(anonPublishedContract1.getSealedByKeys().contains(ownerKey.getPublicKey()));
Contract contract2 = CLIMain.loadContract(basePath + "forRoleAnonymizing1_anonymized.unicon", true);
assertFalse(contract2.getOwner().getKeys().contains(ownerKey.getPublicKey()));
assertFalse(contract2.getIssuer().getKeys().contains(key.getPublicKey()));
assertFalse(contract2.getCreator().getKeys().contains(key.getPublicKey()));
Contract anonPublishedContract2 = new Contract(contract2.getLastSealedBinary());
assertFalse(anonPublishedContract2.getOwner().getKeys().contains(ownerKey.getPublicKey()));
assertFalse(anonPublishedContract2.getIssuer().getKeys().contains(key.getPublicKey()));
assertFalse(anonPublishedContract2.getCreator().getKeys().contains(key.getPublicKey()));
assertFalse(anonPublishedContract2.getSealedByKeys().contains(key.getPublicKey()));
assertFalse(anonPublishedContract2.getSealedByKeys().contains(ownerKey.getPublicKey()));
}
use of com.icodici.crypto.PrivateKey in project universa by UniversaBlockchain.
the class CLIMainTest method saveAndLoad.
@Test
public void saveAndLoad() throws Exception {
// Should register contracts and use -cost as key to print cost of processing it.
PrivateKey manufacturePrivateKey = new PrivateKey(Do.read(rootPath + "keys/tu_key.private.unikey"));
PrivateKey stepaPrivateKey = new PrivateKey(Do.read(rootPath + "keys/stepan_mamontov.private.unikey"));
Contract stepaTU = Contract.fromDslFile(rootPath + "StepaTU.yml");
stepaTU.addSignerKey(manufacturePrivateKey);
stepaTU.seal();
stepaTU.check();
// stepaTU.setIsTU(true);
stepaTU.traceErrors();
CLIMain.saveContract(stepaTU, basePath + "save_and_load.unicon");
callMain2("--register", basePath + "save_and_load.unicon", "--cost");
System.out.println("--- save --- " + stepaTU.getId());
Contract loaded = CLIMain.loadContract(basePath + "save_and_load.unicon", true);
System.out.println("--- load --- " + loaded.getId());
assertTrue(loaded.getId().equals(stepaTU.getId()));
Contract paymentDecreased = loaded.createRevision(stepaPrivateKey);
paymentDecreased.getStateData().set("transaction_units", 99);
paymentDecreased.seal();
CLIMain.saveContract(paymentDecreased, basePath + "save_and_load.unicon");
System.out.println("--- save 2 --- " + paymentDecreased.getId());
callMain("--register", basePath + "save_and_load.unicon", "--cost");
Contract loaded2 = CLIMain.loadContract(basePath + "save_and_load.unicon", true);
System.out.println("--- load 2 --- " + loaded2.getId());
assertTrue(loaded2.getId().equals(paymentDecreased.getId()));
}
use of com.icodici.crypto.PrivateKey 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.crypto.PrivateKey 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;
}
use of com.icodici.crypto.PrivateKey in project universa by UniversaBlockchain.
the class ContractsService method createTwoSignedContract.
/**
* Creates a contract with two signatures.
*<br><br>
* The service creates a contract which asks two signatures.
* It can not be registered without both parts of deal, so it is make sure both parts that they agreed with contract.
* Service creates a contract that should be send to partner,
* then partner should sign it and return back for final sign from calling part.
*<br><br>
* @param BaseContract is base contract
* @param fromKeys is own private keys
* @param toKeys is foreign public keys
* @param createNewRevision create new revision if true
* @return contract with two signatures that should be send from first part to partner.
*/
public static synchronized Contract createTwoSignedContract(Contract BaseContract, Set<PrivateKey> fromKeys, Set<PublicKey> toKeys, boolean createNewRevision) {
Contract twoSignContract = BaseContract;
if (createNewRevision) {
twoSignContract = BaseContract.createRevision(fromKeys);
twoSignContract.getKeysToSignWith().clear();
}
SimpleRole creatorFrom = new SimpleRole("creator");
for (PrivateKey k : fromKeys) {
KeyRecord kr = new KeyRecord(k.getPublicKey());
creatorFrom.addKeyRecord(kr);
}
SimpleRole ownerTo = new SimpleRole("owner");
for (PublicKey k : toKeys) {
KeyRecord kr = new KeyRecord(k);
ownerTo.addKeyRecord(kr);
}
twoSignContract.createTransactionalSection();
twoSignContract.getTransactional().setId(HashId.createRandom().toBase64String());
Reference reference = new Reference(twoSignContract);
reference.transactional_id = twoSignContract.getTransactional().getId();
reference.type = Reference.TYPE_TRANSACTIONAL;
reference.required = true;
reference.signed_by = new ArrayList<>();
reference.signed_by.add(creatorFrom);
reference.signed_by.add(ownerTo);
twoSignContract.getTransactional().addReference(reference);
twoSignContract.setOwnerKeys(toKeys);
twoSignContract.seal();
return twoSignContract;
}
Aggregations