Search in sources :

Example 46 with PublicKey

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

the class NodeInfo method loadYaml.

public static NodeInfo loadYaml(Path fileName) {
    try {
        Yaml yaml = new Yaml();
        Binder b = Binder.from(yaml.load(new FileInputStream(fileName.toString())));
        // System.out.println(fileName);
        int count = fileName.getNameCount();
        String n = fileName.getName(count - 1).toString();
        n = n.substring(0, n.length() - 5) + ".public.unikey";
        String keyPath = "/" + fileName.subpath(0, count - 2) + "/keys/" + n;
        System.out.println("expected key file path: <" + keyPath + ">");
        // System.out.println(keyPath);
        // System.out.println(b);
        PublicKey key = new PublicKey(Do.read(keyPath));
        return new NodeInfo(key, b.getIntOrThrow("node_number"), b.getStringOrThrow("node_name"), (String) b.getListOrThrow("ip").get(0), b.getStringOrThrow("public_host"), b.getIntOrThrow("udp_server_port"), b.getIntOrThrow("http_client_port"), b.getIntOrThrow("http_server_port"));
    } catch (Exception e) {
        System.err.println("failed to load node: " + fileName + ": " + e);
        e.printStackTrace();
    }
    return null;
}
Also used : Binder(net.sergeych.tools.Binder) PublicKey(com.icodici.crypto.PublicKey) Yaml(org.yaml.snakeyaml.Yaml) FileInputStream(java.io.FileInputStream) SQLException(java.sql.SQLException)

Example 47 with PublicKey

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

the class CLIMain method exportPublicKeys.

/**
 * Export public keys from specified contract.
 *
 * @param contract - contract to export.
 * @param roleName - from which role keys should be exported.
 * @param fileName - name of file to export to.
 */
private static void exportPublicKeys(Contract contract, String roleName, String fileName, boolean base64) throws IOException {
    if (fileName == null) {
        if (testMode && testRootPath != null) {
            fileName = testRootPath + "Universa_" + roleName + "_public_key";
        } else {
            fileName = "Universa_" + roleName + "_public_key.pub";
        }
    }
    Role role = contract.getRole(roleName);
    if (role != null) {
        Set<PublicKey> keys = role.getKeys();
        int index = 0;
        byte[] data;
        for (PublicKey key : keys) {
            index++;
            data = key.pack();
            String name = fileName.replaceAll("\\.(pub)$", "_key_" + roleName + "_" + index + ".public.unikey");
            if (base64) {
                name += ".txt";
            }
            try (FileOutputStream fs = new FileOutputStream(name)) {
                if (base64)
                    fs.write(Base64.encodeLines(data).getBytes());
                else
                    fs.write(data);
                fs.close();
            }
        }
        report(roleName + " export public keys ok");
    } else {
        report("export public keys error, role does not exist: " + roleName);
    }
}
Also used : Role(com.icodici.universa.contract.roles.Role) PublicKey(com.icodici.crypto.PublicKey)

Example 48 with PublicKey

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

the class CLIMainTest method showSwapResult.

// ////////////////////////////////////
// @Test
public void showSwapResult() throws Exception {
    Set<PrivateKey> martyPrivateKeys = new HashSet<>();
    Set<PublicKey> martyPublicKeys = new HashSet<>();
    Set<PrivateKey> stepaPrivateKeys = new HashSet<>();
    Set<PublicKey> stepaPublicKeys = new HashSet<>();
    PrivateKey martyPrivateKey = new PrivateKey(Do.read(rootPath + "keys/marty_mcfly.private.unikey"));
    martyPrivateKeys.add(martyPrivateKey);
    martyPublicKeys.add(martyPrivateKey.getPublicKey());
    PrivateKey stepaPrivateKey = new PrivateKey(Do.read(rootPath + "keys/stepan_mamontov.private.unikey"));
    stepaPrivateKeys.add(stepaPrivateKey);
    stepaPublicKeys.add(stepaPrivateKey.getPublicKey());
    PrivateKey manufacturePrivateKey = new PrivateKey(Do.read(rootPath + "_xer0yfe2nn1xthc.private.unikey"));
    Contract delorean = Contract.fromDslFile(rootPath + "DeLoreanOwnership.yml");
    Contract lamborghini = Contract.fromDslFile(rootPath + "LamborghiniOwnership.yml");
    delorean.addSignerKey(manufacturePrivateKey);
    delorean.seal();
    delorean.traceErrors();
    CLIMain.saveContract(delorean, rootPath + "delorean.unicon");
    lamborghini.addSignerKey(manufacturePrivateKey);
    lamborghini.seal();
    lamborghini.traceErrors();
    CLIMain.saveContract(lamborghini, rootPath + "lamborghini.unicon");
    callMain("--register", rootPath + "delorean.unicon", rootPath + "lamborghini.unicon", "-wait", "5000");
    Contract swapContract = ContractsService.startSwap(delorean, lamborghini, martyPrivateKeys, stepaPublicKeys);
    ContractsService.signPresentedSwap(swapContract, stepaPrivateKeys);
    ContractsService.finishSwap(swapContract, martyPrivateKeys);
    Contract newDelorean = null;
    Contract newLamborghini = null;
    for (Contract c : swapContract.getNew()) {
        if (c.getParent().equals(delorean.getId())) {
            newDelorean = c;
        }
        if (c.getParent().equals(lamborghini.getId())) {
            newLamborghini = c;
        }
    }
    CLIMain.saveContract(swapContract, rootPath + "swapContract.unicon", true, true);
    CLIMain.saveContract(newDelorean, rootPath + "newDelorean.unicon");
    CLIMain.saveContract(newLamborghini, rootPath + "newLamborghini.unicon");
    callMain("--register", rootPath + "swapContract.unicon", "-wait", "5000");
    System.out.println("delorean: " + delorean.check());
    System.out.println("lamborghini: " + lamborghini.check());
    System.out.println("newDelorean: " + newDelorean.check());
    System.out.println("newLamborghini: " + newLamborghini.check());
    System.out.println("swapContract: " + swapContract.check());
    callMain("-e", rootPath + "delorean.unicon", rootPath + "lamborghini.unicon", rootPath + "newDelorean.unicon", rootPath + "newLamborghini.unicon", rootPath + "swapContract.unicon", "-pretty");
}
Also used : PrivateKey(com.icodici.crypto.PrivateKey) PublicKey(com.icodici.crypto.PublicKey) Contract(com.icodici.universa.contract.Contract)

Example 49 with PublicKey

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

the class BasicHttpClient method restart.

public void restart() throws IOException {
    synchronized (this) {
        System.err.println("RESTART");
        PrivateKey privateKey = session.getPrivateKey();
        PublicKey nodePublicKey = session.getNodePublicKey();
        session = null;
        start(privateKey, nodePublicKey, null);
    }
}
Also used : PrivateKey(com.icodici.crypto.PrivateKey) PublicKey(com.icodici.crypto.PublicKey)

Example 50 with PublicKey

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

the class Node method checkIfContractContainsNetConfig.

private boolean checkIfContractContainsNetConfig(Contract contract) {
    if (!contract.getStateData().containsKey("net_config")) {
        return false;
    }
    // check if owner is list role
    if (!(contract.getOwner() instanceof ListRole)) {
        return false;
    }
    // TODO: network council criteria here
    // check if quorum matches network concil criteria
    ListRole owner = (ListRole) contract.getOwner();
    if (owner.getQuorum() == 0 || owner.getQuorum() < owner.getRoles().size() - 1) {
        return false;
    }
    // check if owner keys set equals to nodes key set
    Object obj = DefaultBiMapper.getInstance().deserializeObject(contract.getStateData().get("net_config"));
    if (!(obj instanceof List)) {
        return false;
    }
    List contractNodes = (List) obj;
    Set<PublicKey> ownerKeys = contract.getOwner().getKeys();
    if (contractNodes.size() != ownerKeys.size() || !contractNodes.stream().allMatch(nodeInfo -> nodeInfo instanceof NodeInfo && ownerKeys.contains(((NodeInfo) nodeInfo).getPublicKey()))) {
        return false;
    }
    for (Permission permission : contract.getPermissions().values()) {
        if (permission instanceof ChangeOwnerPermission) {
            if (!(permission.getRole() instanceof RoleLink) || ((RoleLink) permission.getRole()).getRole() != contract.getOwner())
                return false;
        }
        if (permission instanceof ModifyDataPermission) {
            if (((ModifyDataPermission) permission).getFields().containsKey("net_config")) {
                if (!(permission.getRole() instanceof RoleLink) || ((RoleLink) permission.getRole()).getRole() != contract.getOwner())
                    return false;
            }
        }
    }
    return true;
}
Also used : RoleLink(com.icodici.universa.contract.roles.RoleLink) ModifyDataPermission(com.icodici.universa.contract.permissions.ModifyDataPermission) PublicKey(com.icodici.crypto.PublicKey) ModifyDataPermission(com.icodici.universa.contract.permissions.ModifyDataPermission) ChangeOwnerPermission(com.icodici.universa.contract.permissions.ChangeOwnerPermission) Permission(com.icodici.universa.contract.permissions.Permission) ChangeOwnerPermission(com.icodici.universa.contract.permissions.ChangeOwnerPermission) ListRole(com.icodici.universa.contract.roles.ListRole)

Aggregations

PublicKey (com.icodici.crypto.PublicKey)84 PrivateKey (com.icodici.crypto.PrivateKey)75 Test (org.junit.Test)57 SimpleRole (com.icodici.universa.contract.roles.SimpleRole)17 HashSet (java.util.HashSet)14 Binder (net.sergeych.tools.Binder)13 ListRole (com.icodici.universa.contract.roles.ListRole)11 Role (com.icodici.universa.contract.roles.Role)7 Contract (com.icodici.universa.contract.Contract)6 KeyAddress (com.icodici.crypto.KeyAddress)4 RoleLink (com.icodici.universa.contract.roles.RoleLink)4 KeyRecord (com.icodici.universa.contract.KeyRecord)3 ChangeOwnerPermission (com.icodici.universa.contract.permissions.ChangeOwnerPermission)3 Permission (com.icodici.universa.contract.permissions.Permission)3 TimeoutException (java.util.concurrent.TimeoutException)3 Decimal (com.icodici.universa.Decimal)2 ErrorRecord (com.icodici.universa.ErrorRecord)2 HashId (com.icodici.universa.HashId)2 Quantiser (com.icodici.universa.node2.Quantiser)2 List (java.util.List)2