Search in sources :

Example 76 with PrivateKey

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

the class InnerContractsService method createFreshTU.

/**
 * Creates fresh contract in the first revision with transaction units.
 *<br><br>
 * This contract should be registered and then should be used as payment for other contract's processing.
 * TU contracts signs with special Universa keys and set as owner public keys from params.
 *<br><br>
 * @param amount is initial number of TU that will be have an owner
 * @param ownerKeys is public keys that will became an owner of TU
 * @param withTestTU if true TU will be created with test transaction units
 * @return sealed TU contract; should be registered in the Universa by simplified procedure.
 * @throws IOException with exceptions while contract preparing
 */
public static synchronized Contract createFreshTU(int amount, Set<PublicKey> ownerKeys, boolean withTestTU) throws IOException {
    PrivateKey manufacturePrivateKey = new PrivateKey(Do.read(Config.tuKeyPath));
    Contract tu;
    if (withTestTU) {
        tu = Contract.fromDslFile(Config.testTUTemplatePath);
    } else {
        tu = Contract.fromDslFile(Config.tuTemplatePath);
    }
    SimpleRole ownerRole = new SimpleRole("owner");
    for (PublicKey k : ownerKeys) {
        KeyRecord kr = new KeyRecord(k);
        ownerRole.addKeyRecord(kr);
    }
    tu.registerRole(ownerRole);
    tu.createRole("owner", ownerRole);
    tu.getStateData().set("transaction_units", amount);
    if (withTestTU) {
        tu.getStateData().set("test_transaction_units", amount * 100);
    }
    tu.addSignerKey(manufacturePrivateKey);
    tu.seal();
    return tu;
}
Also used : PrivateKey(com.icodici.crypto.PrivateKey) SimpleRole(com.icodici.universa.contract.roles.SimpleRole) PublicKey(com.icodici.crypto.PublicKey)

Example 77 with PrivateKey

use of com.icodici.crypto.PrivateKey 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 78 with PrivateKey

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

the class CLIMain method doRegister.

private static void doRegister() throws IOException {
    List<String> sources = new ArrayList<String>((List) options.valuesOf("register"));
    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);
    for (int s = 0; s < sources.size(); s++) {
        String source = sources.get(s);
        Contract contract = loadContract(source);
        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());
        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 = registerContract(contract, tu, tuAmount, tuKeys, tutest, (int) options.valueOf("wait"));
                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 {
                report("registering the contract " + contract.getId().toBase64String() + " from " + source);
                registerContract(contract, (int) options.valueOf("wait"));
            }
        }
    }
    // print cost of processing if asked
    if (options.has("cost")) {
        doCost();
    } else {
        finish();
    }
}
Also used : PrivateKey(com.icodici.crypto.PrivateKey) Parcel(com.icodici.universa.contract.Parcel) Contract(com.icodici.universa.contract.Contract)

Example 79 with PrivateKey

use of com.icodici.crypto.PrivateKey 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)

Example 80 with PrivateKey

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

the class CLIMain method keysMap.

public static synchronized Map<String, PrivateKey> keysMap() throws IOException {
    if (keyFiles == null) {
        keyFiles = new HashMap<>();
        for (String fileName : keyFileNames) {
            // keyFiles.put(fileName, pk);
            try {
                PrivateKey pk = PrivateKey.fromPath(Paths.get(fileName));
                keyFiles.put(fileName, pk);
            } catch (IOException e) {
                addError(Errors.NOT_FOUND.name(), fileName.toString(), "failed to load key file: " + e.getMessage());
            }
        }
    }
    return keyFiles;
}
Also used : PrivateKey(com.icodici.crypto.PrivateKey)

Aggregations

PrivateKey (com.icodici.crypto.PrivateKey)168 Test (org.junit.Test)122 PublicKey (com.icodici.crypto.PublicKey)76 SimpleRole (com.icodici.universa.contract.roles.SimpleRole)29 Contract (com.icodici.universa.contract.Contract)24 Binder (net.sergeych.tools.Binder)22 IOException (java.io.IOException)16 HashSet (java.util.HashSet)14 ListRole (com.icodici.universa.contract.roles.ListRole)12 SQLException (java.sql.SQLException)11 Ignore (org.junit.Ignore)9 File (java.io.File)8 KeyAddress (com.icodici.crypto.KeyAddress)7 KeyRecord (com.icodici.universa.contract.KeyRecord)7 Role (com.icodici.universa.contract.roles.Role)7 Semaphore (java.util.concurrent.Semaphore)7 RoleLink (com.icodici.universa.contract.roles.RoleLink)5 TimeoutException (java.util.concurrent.TimeoutException)5 Decimal (com.icodici.universa.Decimal)4 HashId (com.icodici.universa.HashId)4