use of com.icodici.crypto.KeyAddress 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.KeyAddress in project universa by UniversaBlockchain.
the class CLIMainTest method testExportImportWithAddresses.
@Test
public void testExportImportWithAddresses() throws Exception {
callMain2("-create", rootPath + "simple_root_contract_v2.yml", "-name", basePath + "contractWithAddresses.unicon", "-k", rootPath + "_xer0yfe2nn1xthc.private.unikey");
Contract contract = CLIMain.loadContract(basePath + "contractWithAddresses.unicon", true);
Set<KeyAddress> keyAddresses = new HashSet<>();
keyAddresses.add(new KeyAddress(TestKeys.publicKey(0), 0, true));
SimpleRole sr1 = new SimpleRole("owner", keyAddresses);
contract.registerRole(sr1);
contract.addSignerKey(TestKeys.privateKey(0));
contract.seal();
CLIMain.saveContract(contract, basePath + "contractWithAddresses.unicon");
callMain("-e", basePath + "contractWithAddresses.unicon", "-name", basePath + "contractWithAddresses.json");
System.out.println(output);
assertTrue(output.indexOf("export as json ok") >= 0);
assertEquals(0, errors.size());
callMain("-i", basePath + "contractWithAddresses.json", "-name", basePath + "contractWithAddressesImported.unicon");
System.out.println(output);
assertTrue(output.indexOf("import from json ok") >= 0);
assertEquals(1, errors.size());
if (errors.size() > 0) {
assertEquals(Errors.NOT_SIGNED.name(), errors.get(0).get("code"));
}
Contract contractImported = CLIMain.loadContract(basePath + "contractWithAddressesImported.unicon", true);
assertTrue(contractImported.getOwner().getKeyAddresses().iterator().next().isMatchingKey(TestKeys.privateKey(0).getPublicKey()));
PrivateKey creatorPrivateKey = new PrivateKey(Do.read(rootPath + "_xer0yfe2nn1xthc.private.unikey"));
contractImported.addSignatureToSeal(creatorPrivateKey);
contractImported.addSignatureToSeal(TestKeys.privateKey(0));
assertTrue(contractImported.check());
Set<PrivateKey> signKeys = new HashSet<>();
signKeys.add(creatorPrivateKey);
signKeys.add(TestKeys.privateKey(0));
contractImported.setKeysToSignWith(signKeys);
byte[] sealedContract = contractImported.sealAsV2();
TransactionPack tp = new TransactionPack();
tp.addKeys(creatorPrivateKey.getPublicKey());
tp.addKeys(TestKeys.privateKey(0).getPublicKey());
Contract restoredContract = new Contract(sealedContract, tp);
assertTrue(restoredContract.check());
}
use of com.icodici.crypto.KeyAddress in project universa by UniversaBlockchain.
the class SimpleRole method initWithRecords.
private void initWithRecords(@NonNull Collection records) {
records.forEach(x -> {
KeyRecord kr = null;
AnonymousId anonId = null;
if (x instanceof KeyRecord)
kr = (KeyRecord) x;
else if (x instanceof PublicKey)
kr = new KeyRecord((PublicKey) x);
else if (x instanceof AnonymousId)
anonId = (AnonymousId) x;
else if (x instanceof PrivateKey)
kr = new KeyRecord(((PrivateKey) x).getPublicKey());
else if (x instanceof KeyAddress)
keyAddresses.add((KeyAddress) x);
else
throw new IllegalArgumentException("Cant create KeyRecord from " + x);
if (anonId != null)
anonymousIds.add(anonId);
else if (kr != null)
keyRecords.put(kr.getPublicKey(), kr);
});
}
use of com.icodici.crypto.KeyAddress in project universa by UniversaBlockchain.
the class SimpleRole method deserialize.
@Override
public void deserialize(Binder data, BiDeserializer deserializer) {
super.deserialize(data, deserializer);
// role can have keys - this should actually be refactored to let role
// hold other roles and so on.
List keyList = data.getList("keys", null);
keyRecords.clear();
if (keyList != null) {
keyList.forEach(kr -> {
addKeyRecord(deserializer.deserialize(kr));
});
}
List anonIdList = data.getList("anonIds", null);
anonymousIds.clear();
if (anonIdList != null) {
for (Object aid : anonIdList) {
AnonymousId anonymousId = deserializer.deserialize(aid);
anonymousIds.add(anonymousId);
}
}
List keyAddrList = data.getList("addresses", null);
keyAddresses.clear();
if (keyAddrList != null) {
for (Object keyAddr : keyAddrList) {
KeyAddress ka = deserializer.deserialize(keyAddr);
keyAddresses.add(ka);
}
}
}
use of com.icodici.crypto.KeyAddress in project universa by UniversaBlockchain.
the class BaseNetworkTest method changeOwnerWithAddress2.
@Test(timeout = 90000)
public void changeOwnerWithAddress2() throws Exception {
Set<PrivateKey> martyPrivateKeys = new HashSet<>();
Set<PublicKey> martyPublicKeys = new HashSet<>();
Set<PrivateKey> stepaPrivateKeys = new HashSet<>();
Set<PublicKey> stepaPublicKeys = new HashSet<>();
martyPrivateKeys.add(new PrivateKey(Do.read(ROOT_PATH + "keys/marty_mcfly.private.unikey")));
stepaPrivateKeys.add(new PrivateKey(Do.read(ROOT_PATH + "keys/stepan_mamontov.private.unikey")));
for (PrivateKey pk : stepaPrivateKeys) stepaPublicKeys.add(pk.getPublicKey());
for (PrivateKey pk : martyPrivateKeys) martyPublicKeys.add(pk.getPublicKey());
PrivateKey key = new PrivateKey(Do.read(ROOT_PATH + "_xer0yfe2nn1xthc.private.unikey"));
Contract c1 = Contract.fromDslFile(ROOT_PATH + "coin100.yml");
c1.addSignerKey(key);
c1.seal();
c1.check();
c1.traceErrors();
registerAndCheckApproved(c1);
//
KeyAddress stepaAddress = stepaPublicKeys.iterator().next().getLongAddress();
Contract anonOwnerContract = c1.createRevision(key);
anonOwnerContract.setOwnerKey(stepaAddress);
anonOwnerContract.seal();
anonOwnerContract.check();
anonOwnerContract.traceErrors();
registerAndCheckApproved(anonOwnerContract);
assertTrue(anonOwnerContract.getOwner().getKeyAddresses().iterator().next().equals(stepaAddress));
assertEquals(0, anonOwnerContract.getOwner().getKeys().size());
//
Contract anonSignedContract = anonOwnerContract.createRevision();
anonSignedContract.setOwnerKeys(martyPublicKeys);
anonSignedContract.setCreatorKeys(stepaAddress);
anonSignedContract.addSignerKey(stepaPrivateKeys.iterator().next());
anonSignedContract.seal();
anonSignedContract.check();
anonSignedContract.traceErrors();
Contract afterSend = imitateSendingTransactionToPartner(anonSignedContract);
registerAndCheckApproved(afterSend);
assertEquals(0, afterSend.getOwner().getKeyAddresses().size());
assertTrue(afterSend.getOwner().isAllowedForKeys(martyPublicKeys));
Contract anonPublishedContract = new Contract(anonSignedContract.getLastSealedBinary());
ItemResult itemResult = node.waitItem(anonPublishedContract.getId(), 8000);
assertEquals(ItemState.APPROVED, itemResult.state);
assertFalse(anonPublishedContract.getSealedByKeys().contains(stepaPublicKeys.iterator().next()));
}
Aggregations