Search in sources :

Example 6 with KeyAddress

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();
}
Also used : PrivateKey(com.icodici.crypto.PrivateKey) KeyAddress(com.icodici.crypto.KeyAddress)

Example 7 with KeyAddress

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());
}
Also used : PrivateKey(com.icodici.crypto.PrivateKey) SimpleRole(com.icodici.universa.contract.roles.SimpleRole) KeyAddress(com.icodici.crypto.KeyAddress) Contract(com.icodici.universa.contract.Contract) TransactionPack(com.icodici.universa.contract.TransactionPack) Test(org.junit.Test)

Example 8 with KeyAddress

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);
    });
}
Also used : KeyRecord(com.icodici.universa.contract.KeyRecord) PrivateKey(com.icodici.crypto.PrivateKey) PublicKey(com.icodici.crypto.PublicKey) KeyAddress(com.icodici.crypto.KeyAddress) AnonymousId(com.icodici.universa.contract.AnonymousId)

Example 9 with KeyAddress

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);
        }
    }
}
Also used : KeyAddress(com.icodici.crypto.KeyAddress) AnonymousId(com.icodici.universa.contract.AnonymousId)

Example 10 with KeyAddress

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()));
}
Also used : PrivateKey(com.icodici.crypto.PrivateKey) PublicKey(com.icodici.crypto.PublicKey) KeyAddress(com.icodici.crypto.KeyAddress) Test(org.junit.Test)

Aggregations

KeyAddress (com.icodici.crypto.KeyAddress)13 PrivateKey (com.icodici.crypto.PrivateKey)7 Test (org.junit.Test)5 PublicKey (com.icodici.crypto.PublicKey)4 AnonymousId (com.icodici.universa.contract.AnonymousId)3 KeyRecord (com.icodici.universa.contract.KeyRecord)2 BackingStoreException (java.util.prefs.BackingStoreException)2 OptionException (joptsimple.OptionException)2 Binder (net.sergeych.tools.Binder)2 Contract (com.icodici.universa.contract.Contract)1 TransactionPack (com.icodici.universa.contract.TransactionPack)1 Role (com.icodici.universa.contract.roles.Role)1 SimpleRole (com.icodici.universa.contract.roles.SimpleRole)1