use of com.icodici.crypto.PrivateKey in project universa by UniversaBlockchain.
the class CLIMain method doRevoke.
private static void doRevoke() throws IOException {
List<String> sources = new ArrayList<String>((List) options.valuesOf("revoke"));
String tuSource = (String) options.valueOf("tu");
int tuAmount = (int) options.valueOf("amount");
boolean tutest = options.has("tutest");
List<String> nonOptions = new ArrayList<String>((List) options.nonOptionArguments());
for (String opt : nonOptions) {
sources.addAll(asList(opt.split(",")));
}
cleanNonOptionalArguments(sources);
report("doRevoke");
for (int s = 0; s < sources.size(); s++) {
String source = sources.get(s);
Contract contract = loadContract(source);
report("doRevoke " + contract);
Contract tu = null;
if (tuSource != null) {
tu = loadContract(tuSource, true);
report("load payment revision: " + tu.getState().getRevision() + " id: " + tu.getId());
}
Set<PrivateKey> tuKeys = new HashSet<>(keysMap().values());
report("tuKeys num: " + tuKeys.size());
if (contract != null) {
if (tu != null && tuKeys != null && tuKeys.size() > 0) {
report("registering the paid contract " + contract.getId() + " from " + source + " for " + tuAmount + " TU");
Parcel parcel = null;
try {
if (contract.check()) {
report("revoke contract from " + source);
parcel = revokeContract(contract, tu, tuAmount, tuKeys, tutest, keysMap().values().toArray(new PrivateKey[0]));
} else {
addErrors(contract.getErrors());
}
} catch (Quantiser.QuantiserException e) {
addError("QUANTIZER_COST_LIMIT", contract.toString(), e.getMessage());
}
if (parcel != null) {
report("save payment revision: " + parcel.getPaymentContract().getState().getRevision() + " id: " + parcel.getPaymentContract().getId());
CopyOption[] copyOptions = new CopyOption[] { StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.COPY_ATTRIBUTES };
Files.copy(Paths.get(tuSource), Paths.get(tuSource.replaceAll("(?i)\\.(unicon)$", "_rev" + tu.getRevision() + ".unicon")), copyOptions);
saveContract(parcel.getPaymentContract(), tuSource, true, false);
}
} else {
try {
if (contract.check()) {
report("revoke contract from " + source);
revokeContract(contract, keysMap().values().toArray(new PrivateKey[0]));
} else {
addErrors(contract.getErrors());
}
} catch (Quantiser.QuantiserException e) {
addError("QUANTIZER_COST_LIMIT", contract.toString(), e.getMessage());
}
}
}
}
finish();
}
use of com.icodici.crypto.PrivateKey in project universa by UniversaBlockchain.
the class CLIMain method doCreateAddress.
private static void doCreateAddress(String keyFilePath, boolean bShort) throws IOException {
report("Generate " + (bShort ? "short" : "long") + " address from key: " + keyFilePath);
PrivateKey key = new PrivateKey(Do.read(keyFilePath));
KeyAddress address = new KeyAddress(key.getPublicKey(), 0, !bShort);
report("Address: " + address.toString());
finish();
}
use of com.icodici.crypto.PrivateKey in project universa by UniversaBlockchain.
the class CLIMainTest method revokeContractVirtual.
@Test
public void revokeContractVirtual() throws Exception {
Contract c = Contract.fromDslFile(rootPath + "simple_root_contract.yml");
c.addSignerKeyFromFile(rootPath + "_xer0yfe2nn1xthc.private.unikey");
c.addSignerKeyFromFile(rootPath + "keys/tu_key.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();
System.out.println("---");
System.out.println("register contract");
System.out.println("---");
CLIMain.registerContract(c);
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);
PrivateKey issuer1 = TestKeys.privateKey(1);
Contract tc = ContractsService.createRevocation(c, issuer1, goodKey);
assertTrue(tc.check());
System.out.println("---");
System.out.println("register revoking contract");
System.out.println("---");
CLIMain.registerContract(tc);
Thread.sleep(2500);
System.out.println("---");
System.out.println("check revoking contract");
System.out.println("---");
callMain("--probe", tc.getId().toBase64String());
System.out.println(output);
assertTrue(output.indexOf(ItemState.APPROVED.name()) >= 1);
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.REVOKED.name()) >= 0);
}
use of com.icodici.crypto.PrivateKey in project universa by UniversaBlockchain.
the class CLIMainTest method swapManyContractsViaTransactionAllGood.
// @Test
public void swapManyContractsViaTransactionAllGood() throws Exception {
Set<PrivateKey> martyPrivateKeys = new HashSet<>();
Set<PublicKey> martyPublicKeys = new HashSet<>();
Set<PrivateKey> stepaPrivateKeys = new HashSet<>();
Set<PublicKey> stepaPublicKeys = new HashSet<>();
Contract delorean1 = Contract.fromDslFile(rootPath + "DeLoreanOwnership.yml");
Contract delorean2 = Contract.fromDslFile(rootPath + "DeLoreanOwnership.yml");
Contract delorean3 = Contract.fromDslFile(rootPath + "DeLoreanOwnership.yml");
List<Contract> deloreans = new ArrayList<>();
deloreans.add(delorean1);
deloreans.add(delorean2);
deloreans.add(delorean3);
Contract lamborghini1 = Contract.fromDslFile(rootPath + "LamborghiniOwnership.yml");
Contract lamborghini2 = Contract.fromDslFile(rootPath + "LamborghiniOwnership.yml");
List<Contract> lamborghinis = new ArrayList<>();
lamborghinis.add(lamborghini1);
lamborghinis.add(lamborghini2);
// ----- prepare contracts -----------
martyPrivateKeys.add(new PrivateKey(Do.read(rootPath + "keys/marty_mcfly.private.unikey")));
for (PrivateKey pk : martyPrivateKeys) {
martyPublicKeys.add(pk.getPublicKey());
}
stepaPrivateKeys.add(new PrivateKey(Do.read(rootPath + "keys/stepan_mamontov.private.unikey")));
for (PrivateKey pk : stepaPrivateKeys) {
stepaPublicKeys.add(pk.getPublicKey());
}
PrivateKey manufacturePrivateKey = new PrivateKey(Do.read(rootPath + "_xer0yfe2nn1xthc.private.unikey"));
int i = 0;
for (Contract d : deloreans) {
i++;
d.addSignerKey(manufacturePrivateKey);
d.seal();
CLIMain.saveContract(d, rootPath + "delorean" + i + ".unicon");
callMain("--register", rootPath + "delorean" + i + ".unicon", "-wait", "5000");
}
i = 0;
for (Contract l : lamborghinis) {
i++;
l.addSignerKey(manufacturePrivateKey);
l.seal();
CLIMain.saveContract(l, rootPath + "lamborghini" + i + ".unicon");
callMain("--register", rootPath + "lamborghini" + i + ".unicon", "-wait", "5000");
}
// register swapped contracts using ContractsService
System.out.println("--- register swapped contracts using ContractsService ---");
Contract swapContract;
// first Marty create transaction, add both contracts and swap owners, sign own new contract
swapContract = ContractsService.startSwap(deloreans, lamborghinis, martyPrivateKeys, stepaPublicKeys);
ContractsService.signPresentedSwap(swapContract, stepaPrivateKeys);
ContractsService.finishSwap(swapContract, martyPrivateKeys);
swapContract.check();
swapContract.traceErrors();
System.out.println("Transaction contract for swapping is valid: " + swapContract.isOk() + " num new contracts: " + swapContract.getNewItems().size());
CLIMain.saveContract(swapContract, rootPath + "swapContract.unicon", true, true);
callMain("--register", rootPath + "swapContract.unicon", "-wait", "5000");
i = 0;
for (Contract c : swapContract.getNew()) {
i++;
CLIMain.saveContract(c, rootPath + "new" + i + ".unicon");
callMain("-e", rootPath + "new" + i + ".unicon", "-pretty");
}
// checkSwapResultSuccess(swapContract, delorean, lamborghini, martyPublicKeys, stepaPublicKeys);
}
use of com.icodici.crypto.PrivateKey in project universa by UniversaBlockchain.
the class CLIMainTest method anonymizeAllRoles.
@Test
public void anonymizeAllRoles() throws Exception {
callMain2("-create", rootPath + "TokenDSLTemplate.yml", "-name", basePath + "forRoleAnonymizing.unicon", "-k", rootPath + "_xer0yfe2nn1xthc.private.unikey");
assertTrue(new File(basePath + "forRoleAnonymizing.unicon").exists());
callMain("-anonymize", basePath + "forRoleAnonymizing.unicon");
assertTrue(new File(basePath + "forRoleAnonymizing_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 contract = CLIMain.loadContract(basePath + "forRoleAnonymizing_anonymized.unicon", true);
assertFalse(contract.getOwner().getKeys().contains(ownerKey.getPublicKey()));
assertFalse(contract.getIssuer().getKeys().contains(key.getPublicKey()));
assertFalse(contract.getCreator().getKeys().contains(key.getPublicKey()));
Contract anonPublishedContract = new Contract(contract.getLastSealedBinary());
assertFalse(anonPublishedContract.getOwner().getKeys().contains(ownerKey.getPublicKey()));
assertFalse(anonPublishedContract.getIssuer().getKeys().contains(key.getPublicKey()));
assertFalse(anonPublishedContract.getCreator().getKeys().contains(key.getPublicKey()));
assertFalse(anonPublishedContract.getSealedByKeys().contains(key.getPublicKey()));
assertFalse(anonPublishedContract.getSealedByKeys().contains(ownerKey.getPublicKey()));
}
Aggregations