Search in sources :

Example 71 with Binder

use of net.sergeych.tools.Binder in project universa by UniversaBlockchain.

the class UDPAdapter method sendHello.

protected void sendHello(Session session) throws InterruptedException {
    report(getLabel(), () -> concatReportMessage("send hello to ", session.remoteNodeId), VerboseLevel.BASE);
    session.state = Session.HELLO;
    Binder binder = Binder.fromKeysValues("data", myNodeInfo.getNumber());
    Block block = new Block(myNodeInfo.getNumber(), session.remoteNodeId, new Random().nextInt(Integer.MAX_VALUE), PacketTypes.HELLO, session.address, session.port, Boss.pack(binder));
    sendBlock(block, session);
}
Also used : Binder(net.sergeych.tools.Binder)

Example 72 with Binder

use of net.sergeych.tools.Binder in project universa by UniversaBlockchain.

the class UDPAdapter method sendKeyRequest.

protected synchronized void sendKeyRequest(Session session) throws InterruptedException {
    report(getLabel(), () -> concatReportMessage("send key request to ", session.remoteNodeId), VerboseLevel.BASE);
    session.state = Session.KEY_REQ;
    List data = asList(session.localNonce, session.remoteNonce);
    try {
        byte[] packed = Boss.pack(data);
        byte[] signed = ownPrivateKey.sign(packed, HashType.SHA512);
        Binder binder = Binder.fromKeysValues("data", packed, "signature", signed);
        Block block = new Block(myNodeInfo.getNumber(), session.remoteNodeId, new Random().nextInt(Integer.MAX_VALUE), PacketTypes.KEY_REQ, session.address, session.port, Boss.pack(binder));
        sendBlock(block, session);
    } catch (EncryptionError encryptionError) {
        callErrorCallbacks("[sendKeyRequest] EncryptionError in node " + myNodeInfo.getNumber() + ": " + encryptionError.getMessage());
        // encryptionError.printStackTrace();
        if (sessionsById.containsKey(session.remoteNodeId)) {
            sessionsById.remove(session.remoteNodeId);
        }
        Session newSession = getOrCreateSession(session.remoteNodeId, session.address, session.port);
        newSession.publicKey = session.publicKey;
        newSession.remoteNodeId = session.remoteNodeId;
        for (Block b : session.waitingBlocksQueue) {
            newSession.addBlockToWaitingQueue(b);
        }
        sendHello(newSession);
    }
}
Also used : Binder(net.sergeych.tools.Binder) Arrays.asList(java.util.Arrays.asList)

Example 73 with Binder

use of net.sergeych.tools.Binder in project universa by UniversaBlockchain.

the class UDPAdapter method sendSessionKey.

protected synchronized void sendSessionKey(Session session) throws InterruptedException {
    report(getLabel(), () -> concatReportMessage("send session key to ", session.remoteNodeId), VerboseLevel.BASE);
    report(getLabel(), () -> concatReportMessage("sessionKey is ", session.sessionKey.hashCode(), " for ", session.remoteNodeId));
    List data = asList(session.sessionKey.getKey(), session.remoteNonce);
    try {
        byte[] packed = Boss.pack(data);
        PublicKey sessionPublicKey = new PublicKey(session.publicKey.pack());
        byte[] encrypted = sessionPublicKey.encrypt(packed);
        byte[] signed = ownPrivateKey.sign(encrypted, HashType.SHA512);
        Binder binder = Binder.fromKeysValues("data", encrypted, "signature", signed);
        Block block = new Block(myNodeInfo.getNumber(), session.remoteNodeId, new Random().nextInt(Integer.MAX_VALUE), PacketTypes.SESSION, session.address, session.port, Boss.pack(binder));
        sendBlock(block, session);
        session.state = Session.SESSION;
    } catch (EncryptionError encryptionError) {
        callErrorCallbacks("[sendSessionKey] EncryptionError in node " + myNodeInfo.getNumber() + ": " + encryptionError.getMessage());
        // encryptionError.printStackTrace();
        if (sessionsById.containsKey(session.remoteNodeId)) {
            sessionsById.remove(session.remoteNodeId);
        }
        Session newSession = getOrCreateSession(session.remoteNodeId, session.address, session.port);
        newSession.publicKey = session.publicKey;
        newSession.remoteNodeId = session.remoteNodeId;
        for (Block b : session.waitingBlocksQueue) {
            newSession.addBlockToWaitingQueue(b);
        }
        sendHello(newSession);
    }
}
Also used : Binder(net.sergeych.tools.Binder) Arrays.asList(java.util.Arrays.asList)

Example 74 with Binder

use of net.sergeych.tools.Binder in project universa by UniversaBlockchain.

the class ListRole method initWithDsl.

@Override
public void initWithDsl(Binder serializedRole) {
    List<Object> roleBinders = serializedRole.getListOrThrow("roles");
    mode = Mode.valueOf(serializedRole.getStringOrThrow("mode").toUpperCase());
    if (mode == Mode.QUORUM)
        quorumSize = serializedRole.getIntOrThrow("quorumSize");
    roleBinders.stream().forEach(x -> {
        if (x instanceof String) {
            roles.add(new RoleLink(x + "link" + Instant.now().toEpochMilli(), (String) x));
        } else {
            Binder binder = Binder.of(x);
            if (binder.size() == 1) {
                String name = binder.keySet().iterator().next();
                roles.add(Role.fromDslBinder(name, binder.getBinderOrThrow(name)));
            } else {
                roles.add(Role.fromDslBinder(null, binder));
            }
        }
    });
}
Also used : Binder(net.sergeych.tools.Binder)

Example 75 with Binder

use of net.sergeych.tools.Binder 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)

Aggregations

Binder (net.sergeych.tools.Binder)136 Test (org.junit.Test)67 PrivateKey (com.icodici.crypto.PrivateKey)21 Contract (com.icodici.universa.contract.Contract)14 PublicKey (com.icodici.crypto.PublicKey)13 IOException (java.io.IOException)11 KeyRecord (com.icodici.universa.contract.KeyRecord)10 SimpleRole (com.icodici.universa.contract.roles.SimpleRole)10 Yaml (org.yaml.snakeyaml.Yaml)9 Bytes (net.sergeych.utils.Bytes)8 Decimal (com.icodici.universa.Decimal)7 ListRole (com.icodici.universa.contract.roles.ListRole)6 NonNull (org.checkerframework.checker.nullness.qual.NonNull)5 Gson (com.google.gson.Gson)4 GsonBuilder (com.google.gson.GsonBuilder)4 XStream (com.thoughtworks.xstream.XStream)4 DomDriver (com.thoughtworks.xstream.io.xml.DomDriver)4 List (java.util.List)4 RoleLink (com.icodici.universa.contract.roles.RoleLink)3 ItemResult (com.icodici.universa.node.ItemResult)3