Java Code Examples for org.bitcoin.paymentchannel.Protos#ClientVersion

The following examples show how to use org.bitcoin.paymentchannel.Protos#ClientVersion . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PaymentChannelClientTest.java    From bcm-android with GNU General Public License v3.0 5 votes vote down vote up
private void assertClientVersion(long expectedTimeWindow) {
    final TwoWayChannelMessage response = clientVersionCapture.getValue();
    final TwoWayChannelMessage.MessageType type = response.getType();
    assertEquals("Wrong type " + type, CLIENT_VERSION, type);
    final Protos.ClientVersion clientVersion = response.getClientVersion();
    final int major = clientVersion.getMajor();
    final int requestedVersion = clientChannelProperties.versionSelector().getRequestedMajorVersion();
    assertEquals("Wrong major version " + major, requestedVersion, major);
    final long actualTimeWindow = clientVersion.getTimeWindowSecs();
    assertEquals("Wrong timeWindow " + actualTimeWindow, expectedTimeWindow, actualTimeWindow);
}
 
Example 2
Source File: PaymentChannelClientTest.java    From green_android with GNU General Public License v3.0 5 votes vote down vote up
private void assertClientVersion(long expectedTimeWindow) {
    final TwoWayChannelMessage response = clientVersionCapture.getValue();
    final TwoWayChannelMessage.MessageType type = response.getType();
    assertEquals("Wrong type " + type, CLIENT_VERSION, type);
    final Protos.ClientVersion clientVersion = response.getClientVersion();
    final int major = clientVersion.getMajor();
    final int requestedVersion = clientChannelProperties.versionSelector().getRequestedMajorVersion();
    assertEquals("Wrong major version " + major, requestedVersion, major);
    final long actualTimeWindow = clientVersion.getTimeWindowSecs();
    assertEquals("Wrong timeWindow " + actualTimeWindow, expectedTimeWindow, actualTimeWindow );
}
 
Example 3
Source File: PaymentChannelClientTest.java    From GreenBits with GNU General Public License v3.0 5 votes vote down vote up
private void assertClientVersion(long expectedTimeWindow) {
    final TwoWayChannelMessage response = clientVersionCapture.getValue();
    final TwoWayChannelMessage.MessageType type = response.getType();
    assertEquals("Wrong type " + type, CLIENT_VERSION, type);
    final Protos.ClientVersion clientVersion = response.getClientVersion();
    final int major = clientVersion.getMajor();
    final int requestedVersion = clientChannelProperties.versionSelector().getRequestedMajorVersion();
    assertEquals("Wrong major version " + major, requestedVersion, major);
    final long actualTimeWindow = clientVersion.getTimeWindowSecs();
    assertEquals("Wrong timeWindow " + actualTimeWindow, expectedTimeWindow, actualTimeWindow );
}
 
Example 4
Source File: PaymentChannelServer.java    From bcm-android with GNU General Public License v3.0 4 votes vote down vote up
@GuardedBy("lock")
private void receiveVersionMessage(Protos.TwoWayChannelMessage msg) throws VerificationException {
    checkState(step == InitStep.WAITING_ON_CLIENT_VERSION && msg.hasClientVersion());
    final Protos.ClientVersion clientVersion = msg.getClientVersion();
    majorVersion = clientVersion.getMajor();
    if (!SERVER_VERSIONS.containsKey(majorVersion)) {
        error("This server needs one of protocol versions " + SERVER_VERSIONS.keySet() + " , client offered " + majorVersion,
                Protos.Error.ErrorCode.NO_ACCEPTABLE_VERSION, CloseReason.NO_ACCEPTABLE_VERSION);
        return;
    }

    Protos.ServerVersion.Builder versionNegotiationBuilder = Protos.ServerVersion.newBuilder()
            .setMajor(majorVersion).setMinor(SERVER_VERSIONS.get(majorVersion));
    conn.sendToClient(Protos.TwoWayChannelMessage.newBuilder()
            .setType(Protos.TwoWayChannelMessage.MessageType.SERVER_VERSION)
            .setServerVersion(versionNegotiationBuilder)
            .build());
    ByteString reopenChannelContractHash = clientVersion.getPreviousChannelContractHash();
    if (reopenChannelContractHash != null && reopenChannelContractHash.size() == 32) {
        Sha256Hash contractHash = Sha256Hash.wrap(reopenChannelContractHash.toByteArray());
        log.info("New client that wants to resume {}", contractHash);
        StoredPaymentChannelServerStates channels = (StoredPaymentChannelServerStates)
                wallet.getExtensions().get(StoredPaymentChannelServerStates.EXTENSION_ID);
        if (channels != null) {
            StoredServerChannel storedServerChannel = channels.getChannel(contractHash);
            if (storedServerChannel != null) {
                final PaymentChannelServer existingHandler = storedServerChannel.setConnectedHandler(this, false);
                if (existingHandler != this) {
                    log.warn("  ... and that channel is already in use, disconnecting other user.");
                    existingHandler.close();
                    storedServerChannel.setConnectedHandler(this, true);
                }

                log.info("Got resume version message, responding with VERSIONS and CHANNEL_OPEN");
                state = storedServerChannel.getOrCreateState(wallet, broadcaster);
                step = InitStep.CHANNEL_OPEN;
                conn.sendToClient(Protos.TwoWayChannelMessage.newBuilder()
                        .setType(Protos.TwoWayChannelMessage.MessageType.CHANNEL_OPEN)
                        .build());
                conn.channelOpen(contractHash);
                return;
            } else {
                log.error(" ... but we do not have any record of that contract! Resume failed.");
            }
        } else {
            log.error(" ... but we do not have any stored channels! Resume failed.");
        }
    }
    log.info("Got initial version message, responding with VERSIONS and INITIATE: min value={}",
            minAcceptedChannelSize.value);

    myKey = new ECKey();
    wallet.freshReceiveKey();

    expireTime = Utils.currentTimeSeconds() + truncateTimeWindow(clientVersion.getTimeWindowSecs());
    switch (majorVersion) {
        case 1:
            step = InitStep.WAITING_ON_UNSIGNED_REFUND;
            break;
        case 2:
            step = InitStep.WAITING_ON_CONTRACT;
            break;
        default:
            error("Protocol version " + majorVersion + " not supported", Protos.Error.ErrorCode.NO_ACCEPTABLE_VERSION, CloseReason.NO_ACCEPTABLE_VERSION);
            break;
    }

    Protos.Initiate.Builder initiateBuilder = Protos.Initiate.newBuilder()
            .setMultisigKey(ByteString.copyFrom(myKey.getPubKey()))
            .setExpireTimeSecs(expireTime)
            .setMinAcceptedChannelSize(minAcceptedChannelSize.value)
            .setMinPayment(minPayment.value);

    conn.sendToClient(Protos.TwoWayChannelMessage.newBuilder()
            .setInitiate(initiateBuilder)
            .setType(Protos.TwoWayChannelMessage.MessageType.INITIATE)
            .build());
}
 
Example 5
Source File: PaymentChannelServer.java    From green_android with GNU General Public License v3.0 4 votes vote down vote up
@GuardedBy("lock")
private void receiveVersionMessage(Protos.TwoWayChannelMessage msg) throws VerificationException {
    checkState(step == InitStep.WAITING_ON_CLIENT_VERSION && msg.hasClientVersion());
    final Protos.ClientVersion clientVersion = msg.getClientVersion();
    majorVersion = clientVersion.getMajor();
    if (!SERVER_VERSIONS.containsKey(majorVersion)) {
        error("This server needs one of protocol versions " + SERVER_VERSIONS.keySet() + " , client offered " + majorVersion,
                Protos.Error.ErrorCode.NO_ACCEPTABLE_VERSION, CloseReason.NO_ACCEPTABLE_VERSION);
        return;
    }

    Protos.ServerVersion.Builder versionNegotiationBuilder = Protos.ServerVersion.newBuilder()
            .setMajor(majorVersion).setMinor(SERVER_VERSIONS.get(majorVersion));
    conn.sendToClient(Protos.TwoWayChannelMessage.newBuilder()
            .setType(Protos.TwoWayChannelMessage.MessageType.SERVER_VERSION)
            .setServerVersion(versionNegotiationBuilder)
            .build());
    ByteString reopenChannelContractHash = clientVersion.getPreviousChannelContractHash();
    if (reopenChannelContractHash != null && reopenChannelContractHash.size() == 32) {
        Sha256Hash contractHash = Sha256Hash.wrap(reopenChannelContractHash.toByteArray());
        log.info("New client that wants to resume {}", contractHash);
        StoredPaymentChannelServerStates channels = (StoredPaymentChannelServerStates)
                wallet.getExtensions().get(StoredPaymentChannelServerStates.EXTENSION_ID);
        if (channels != null) {
            StoredServerChannel storedServerChannel = channels.getChannel(contractHash);
            if (storedServerChannel != null) {
                final PaymentChannelServer existingHandler = storedServerChannel.setConnectedHandler(this, false);
                if (existingHandler != this) {
                    log.warn("  ... and that channel is already in use, disconnecting other user.");
                    existingHandler.close();
                    storedServerChannel.setConnectedHandler(this, true);
                }

                log.info("Got resume version message, responding with VERSIONS and CHANNEL_OPEN");
                state = storedServerChannel.getOrCreateState(wallet, broadcaster);
                step = InitStep.CHANNEL_OPEN;
                conn.sendToClient(Protos.TwoWayChannelMessage.newBuilder()
                        .setType(Protos.TwoWayChannelMessage.MessageType.CHANNEL_OPEN)
                        .build());
                conn.channelOpen(contractHash);
                return;
            } else {
                log.error(" ... but we do not have any record of that contract! Resume failed.");
            }
        } else {
            log.error(" ... but we do not have any stored channels! Resume failed.");
        }
    }
    log.info("Got initial version message, responding with VERSIONS and INITIATE: min value={}",
            minAcceptedChannelSize.value);

    myKey = new ECKey();
    wallet.freshReceiveKey();

    expireTime = Utils.currentTimeSeconds() + truncateTimeWindow(clientVersion.getTimeWindowSecs());
    switch (majorVersion) {
        case 1:
            step = InitStep.WAITING_ON_UNSIGNED_REFUND;
            break;
        case 2:
            step = InitStep.WAITING_ON_CONTRACT;
            break;
        default:
            error("Protocol version " + majorVersion + " not supported", Protos.Error.ErrorCode.NO_ACCEPTABLE_VERSION, CloseReason.NO_ACCEPTABLE_VERSION);
            break;
    }

    Protos.Initiate.Builder initiateBuilder = Protos.Initiate.newBuilder()
            .setMultisigKey(ByteString.copyFrom(myKey.getPubKey()))
            .setExpireTimeSecs(expireTime)
            .setMinAcceptedChannelSize(minAcceptedChannelSize.value)
            .setMinPayment(minPayment.value);

    conn.sendToClient(Protos.TwoWayChannelMessage.newBuilder()
            .setInitiate(initiateBuilder)
            .setType(Protos.TwoWayChannelMessage.MessageType.INITIATE)
            .build());
}
 
Example 6
Source File: PaymentChannelServer.java    From GreenBits with GNU General Public License v3.0 4 votes vote down vote up
@GuardedBy("lock")
private void receiveVersionMessage(Protos.TwoWayChannelMessage msg) throws VerificationException {
    checkState(step == InitStep.WAITING_ON_CLIENT_VERSION && msg.hasClientVersion());
    final Protos.ClientVersion clientVersion = msg.getClientVersion();
    majorVersion = clientVersion.getMajor();
    if (!SERVER_VERSIONS.containsKey(majorVersion)) {
        error("This server needs one of protocol versions " + SERVER_VERSIONS.keySet() + " , client offered " + majorVersion,
                Protos.Error.ErrorCode.NO_ACCEPTABLE_VERSION, CloseReason.NO_ACCEPTABLE_VERSION);
        return;
    }

    Protos.ServerVersion.Builder versionNegotiationBuilder = Protos.ServerVersion.newBuilder()
            .setMajor(majorVersion).setMinor(SERVER_VERSIONS.get(majorVersion));
    conn.sendToClient(Protos.TwoWayChannelMessage.newBuilder()
            .setType(Protos.TwoWayChannelMessage.MessageType.SERVER_VERSION)
            .setServerVersion(versionNegotiationBuilder)
            .build());
    ByteString reopenChannelContractHash = clientVersion.getPreviousChannelContractHash();
    if (reopenChannelContractHash != null && reopenChannelContractHash.size() == 32) {
        Sha256Hash contractHash = Sha256Hash.wrap(reopenChannelContractHash.toByteArray());
        log.info("New client that wants to resume {}", contractHash);
        StoredPaymentChannelServerStates channels = (StoredPaymentChannelServerStates)
                wallet.getExtensions().get(StoredPaymentChannelServerStates.EXTENSION_ID);
        if (channels != null) {
            StoredServerChannel storedServerChannel = channels.getChannel(contractHash);
            if (storedServerChannel != null) {
                final PaymentChannelServer existingHandler = storedServerChannel.setConnectedHandler(this, false);
                if (existingHandler != this) {
                    log.warn("  ... and that channel is already in use, disconnecting other user.");
                    existingHandler.close();
                    storedServerChannel.setConnectedHandler(this, true);
                }

                log.info("Got resume version message, responding with VERSIONS and CHANNEL_OPEN");
                state = storedServerChannel.getOrCreateState(wallet, broadcaster);
                step = InitStep.CHANNEL_OPEN;
                conn.sendToClient(Protos.TwoWayChannelMessage.newBuilder()
                        .setType(Protos.TwoWayChannelMessage.MessageType.CHANNEL_OPEN)
                        .build());
                conn.channelOpen(contractHash);
                return;
            } else {
                log.error(" ... but we do not have any record of that contract! Resume failed.");
            }
        } else {
            log.error(" ... but we do not have any stored channels! Resume failed.");
        }
    }
    log.info("Got initial version message, responding with VERSIONS and INITIATE: min value={}",
            minAcceptedChannelSize.value);

    myKey = new ECKey();
    wallet.freshReceiveKey();

    expireTime = Utils.currentTimeSeconds() + truncateTimeWindow(clientVersion.getTimeWindowSecs());
    switch (majorVersion) {
        case 1:
            step = InitStep.WAITING_ON_UNSIGNED_REFUND;
            break;
        case 2:
            step = InitStep.WAITING_ON_CONTRACT;
            break;
        default:
            error("Protocol version " + majorVersion + " not supported", Protos.Error.ErrorCode.NO_ACCEPTABLE_VERSION, CloseReason.NO_ACCEPTABLE_VERSION);
            break;
    }

    Protos.Initiate.Builder initiateBuilder = Protos.Initiate.newBuilder()
            .setMultisigKey(ByteString.copyFrom(myKey.getPubKey()))
            .setExpireTimeSecs(expireTime)
            .setMinAcceptedChannelSize(minAcceptedChannelSize.value)
            .setMinPayment(minPayment.value);

    conn.sendToClient(Protos.TwoWayChannelMessage.newBuilder()
            .setInitiate(initiateBuilder)
            .setType(Protos.TwoWayChannelMessage.MessageType.INITIATE)
            .build());
}