Search in sources :

Example 1 with KeyAddress

use of com.icodici.crypto.KeyAddress in project universa by UniversaBlockchain.

the class BaseNetworkTest method changeOwnerWithAddress.

@Test(timeout = 90000)
public void changeOwnerWithAddress() 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().getShortAddress();
    Contract anonOwnerContract = c1.createRevisionWithAddress(Arrays.asList(key));
    anonOwnerContract.addSignerKey(key);
    anonOwnerContract.setOwnerKey(stepaAddress);
    anonOwnerContract.seal();
    anonOwnerContract.check();
    anonOwnerContract.traceErrors();
    Contract anonAfterSend = imitateSendingTransactionToPartner(anonOwnerContract);
    registerAndCheckApproved(anonAfterSend);
    assertTrue(anonAfterSend.getOwner().getKeyAddresses().iterator().next().equals(stepaAddress));
    assertEquals(0, anonAfterSend.getOwner().getKeys().size());
    // 
    Contract anonSignedContract = anonAfterSend.createRevisionWithAddress(stepaPrivateKeys);
    anonSignedContract.addSignerKeys(stepaPrivateKeys);
    anonSignedContract.setOwnerKeys(martyPublicKeys);
    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)

Example 2 with KeyAddress

use of com.icodici.crypto.KeyAddress in project universa by UniversaBlockchain.

the class SimpleRoleTest method testAddressRole.

@Test
public void testAddressRole() throws Exception {
    Set<KeyAddress> keyAddresses = new HashSet<>();
    keyAddresses.add(new KeyAddress(keys.get(0).getPublicKey(), 0, true));
    SimpleRole sr = new SimpleRole("tr1", keyAddresses);
    Binder serialized = DefaultBiMapper.serialize(sr);
    Role r1 = DefaultBiMapper.deserialize(serialized);
    Set<PublicKey> pubKeys = new HashSet<>();
    pubKeys.add(keys.get(0).getPublicKey());
    assertTrue(sr.isAllowedForKeys(pubKeys));
    assertTrue(r1.isAllowedForKeys(pubKeys));
    assertEquals(sr, r1);
}
Also used : Binder(net.sergeych.tools.Binder) PublicKey(com.icodici.crypto.PublicKey) KeyAddress(com.icodici.crypto.KeyAddress) Test(org.junit.Test)

Example 3 with KeyAddress

use of com.icodici.crypto.KeyAddress in project universa by UniversaBlockchain.

the class SimpleRoleTest method testCloneAsAddressRole.

@Test
public void testCloneAsAddressRole() throws Exception {
    Set<KeyAddress> keyAddresses = new HashSet<>();
    keyAddresses.add(new KeyAddress(keys.get(0).getPublicKey(), 0, true));
    SimpleRole sr = new SimpleRole("tr1", keyAddresses);
    SimpleRole r1 = sr.cloneAs("tr2");
    SimpleRole r2 = r1.cloneAs("tr1");
    assertEquals(sr, r2);
}
Also used : KeyAddress(com.icodici.crypto.KeyAddress) Test(org.junit.Test)

Example 4 with KeyAddress

use of com.icodici.crypto.KeyAddress in project universa by UniversaBlockchain.

the class CLIMain method doSelectKeyInFolder.

private static void doSelectKeyInFolder(String keysPath, String address) throws IOException {
    File folder = new File(keysPath);
    KeyAddress keyAddress;
    if (!keysPath.endsWith("/"))
        keysPath = keysPath.concat("/");
    try {
        keyAddress = new KeyAddress(address);
    } catch (Exception e) {
        report("Invalid address.");
        finish();
        return;
    }
    if (folder.exists()) {
        for (File file : folder.listFiles()) {
            if (!file.isDirectory()) {
                boolean bResult = false;
                try {
                    PrivateKey key = new PrivateKey(Do.read(keysPath + file.getName()));
                    bResult = keyAddress.isMatchingKey(key.getPublicKey());
                } catch (Exception e) {
                    bResult = false;
                }
                if (bResult) {
                    report("Filekey: " + file.getName());
                    finish();
                    return;
                }
            }
        }
        report("File not found.");
    } else
        report("There is no such directory.");
    finish();
}
Also used : PrivateKey(com.icodici.crypto.PrivateKey) KeyAddress(com.icodici.crypto.KeyAddress) BackingStoreException(java.util.prefs.BackingStoreException) OptionException(joptsimple.OptionException)

Example 5 with KeyAddress

use of com.icodici.crypto.KeyAddress in project universa by UniversaBlockchain.

the class CLIMain method doAddressMatch.

private static void doAddressMatch(String address, String keyFilePath) throws IOException {
    boolean bResult = false;
    try {
        PrivateKey key = new PrivateKey(Do.read(keyFilePath));
        KeyAddress keyAddress = new KeyAddress(address);
        bResult = keyAddress.isMatchingKey(key.getPublicKey());
    } catch (Exception e) {
    }
    ;
    report("Matching address: " + address + " with key: " + keyFilePath);
    report("Matching result: " + bResult);
    finish();
}
Also used : PrivateKey(com.icodici.crypto.PrivateKey) KeyAddress(com.icodici.crypto.KeyAddress) BackingStoreException(java.util.prefs.BackingStoreException) OptionException(joptsimple.OptionException)

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