org.bitcoinj.protocols.channels.IPaymentChannelClient.ClientChannelProperties Java Examples
The following examples show how to use
org.bitcoinj.protocols.channels.IPaymentChannelClient.ClientChannelProperties.
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: PaymentChannelClientConnection.java From bcm-android with GNU General Public License v3.0 | 4 votes |
/** * Attempts to open a new connection to and open a payment channel with the given host and port, blocking until the * connection is open. The server is requested to keep the channel open for {@code timeoutSeconds} * seconds. If the server proposes a longer time the channel will be closed. * * @param server The host/port pair where the server is listening. * @param timeoutSeconds The connection timeout and read timeout during initialization. This should be large enough * to accommodate ECDSA signature operations and network latency. * @param wallet The wallet which will be paid from, and where completed transactions will be committed. * Can be encrypted if user key is supplied when needed. Must already have a * {@link StoredPaymentChannelClientStates} object in its extensions set. * @param myKey A freshly generated keypair used for the multisig contract and refund output. * @param maxValue The maximum value this channel is allowed to request * @param serverId A unique ID which is used to attempt reopening of an existing channel. * This must be unique to the server, and, if your application is exposing payment channels to some * API, this should also probably encompass some caller UID to avoid applications opening channels * which were created by others. * @param userKeySetup Key derived from a user password, used to decrypt myKey, if it is encrypted, during setup. * @param clientChannelProperties Modifier to change the channel's configuration. * @throws IOException if there's an issue using the network. * @throws ValueOutOfRangeException if the balance of wallet is lower than maxValue. */ public PaymentChannelClientConnection(InetSocketAddress server, int timeoutSeconds, Wallet wallet, ECKey myKey, Coin maxValue, String serverId, @Nullable KeyParameter userKeySetup, final ClientChannelProperties clientChannelProperties) throws IOException, ValueOutOfRangeException { // Glue the object which vends/ingests protobuf messages in order to manage state to the network object which // reads/writes them to the wire in length prefixed form. channelClient = new PaymentChannelClient(wallet, myKey, maxValue, Sha256Hash.of(serverId.getBytes()), userKeySetup, clientChannelProperties, new PaymentChannelClient.ClientConnection() { @Override public void sendToServer(Protos.TwoWayChannelMessage msg) { wireParser.write(msg); } @Override public void destroyConnection(PaymentChannelCloseException.CloseReason reason) { channelOpenFuture.setException(new PaymentChannelCloseException("Payment channel client requested that the connection be closed: " + reason, reason)); wireParser.closeConnection(); } @Override public boolean acceptExpireTime(long expireTime) { return expireTime <= (clientChannelProperties.timeWindow() + Utils.currentTimeSeconds() + 60); // One extra minute to compensate for time skew and latency } @Override public void channelOpen(boolean wasInitiated) { wireParser.setSocketTimeout(0); // Inform the API user that we're done and ready to roll. channelOpenFuture.set(PaymentChannelClientConnection.this); } }); // And glue back in the opposite direction - network to the channelClient. wireParser = new ProtobufConnection<Protos.TwoWayChannelMessage>(new ProtobufConnection.Listener<Protos.TwoWayChannelMessage>() { @Override public void messageReceived(ProtobufConnection<Protos.TwoWayChannelMessage> handler, Protos.TwoWayChannelMessage msg) { try { channelClient.receiveMessage(msg); } catch (InsufficientMoneyException e) { // We should only get this exception during INITIATE, so channelOpen wasn't called yet. channelOpenFuture.setException(e); } } @Override public void connectionOpen(ProtobufConnection<Protos.TwoWayChannelMessage> handler) { channelClient.connectionOpen(); } @Override public void connectionClosed(ProtobufConnection<Protos.TwoWayChannelMessage> handler) { channelClient.connectionClosed(); channelOpenFuture.setException(new PaymentChannelCloseException("The TCP socket died", PaymentChannelCloseException.CloseReason.CONNECTION_CLOSED)); } }, Protos.TwoWayChannelMessage.getDefaultInstance(), Short.MAX_VALUE, timeoutSeconds * 1000); // Initiate the outbound network connection. We don't need to keep this around. The wireParser object will handle // things from here on out. new NioClient(server, wireParser, timeoutSeconds * 1000); }
Example #2
Source File: PaymentChannelV2ClientState.java From bcm-android with GNU General Public License v3.0 | 4 votes |
@Override public synchronized void initiate(@Nullable KeyParameter userKey, ClientChannelProperties clientChannelProperties) throws ValueOutOfRangeException, InsufficientMoneyException { final NetworkParameters params = wallet.getParams(); Transaction template = new Transaction(params); // There is also probably a change output, but we don't bother shuffling them as it's obvious from the // format which one is the change. If we start obfuscating the change output better in future this may // be worth revisiting. Script redeemScript = ScriptBuilder.createCLTVPaymentChannelOutput(BigInteger.valueOf(expiryTime), myKey, serverKey); TransactionOutput transactionOutput = template.addOutput(totalValue, ScriptBuilder.createP2SHOutputScript(redeemScript)); if (transactionOutput.isDust()) throw new ValueOutOfRangeException("totalValue too small to use"); SendRequest req = SendRequest.forTx(template); req.coinSelector = AllowUnconfirmedCoinSelector.get(); req.shuffleOutputs = false; // TODO: Fix things so shuffling is usable. req = clientChannelProperties.modifyContractSendRequest(req); if (userKey != null) req.aesKey = userKey; wallet.completeTx(req); Coin multisigFee = req.tx.getFee(); contract = req.tx; // Build a refund transaction that protects us in the case of a bad server that's just trying to cause havoc // by locking up peoples money (perhaps as a precursor to a ransom attempt). We time lock it because the // CheckLockTimeVerify opcode requires a lock time to be specified and the input to have a non-final sequence // number (so that the lock time is not disabled). refundTx = new Transaction(params); // by using this sequence value, we avoid extra full replace-by-fee and relative lock time processing. refundTx.addInput(contract.getOutput(0)).setSequenceNumber(TransactionInput.NO_SEQUENCE - 1L); refundTx.setLockTime(expiryTime); if (Context.get().isEnsureMinRequiredFee()) { // Must pay min fee. final Coin valueAfterFee = totalValue.subtract(Transaction.REFERENCE_DEFAULT_MIN_TX_FEE); if (Transaction.MIN_NONDUST_OUTPUT.compareTo(valueAfterFee) > 0) throw new ValueOutOfRangeException("totalValue too small to use"); refundTx.addOutput(valueAfterFee, LegacyAddress.fromKey(params, myKey)); refundFees = multisigFee.add(Transaction.REFERENCE_DEFAULT_MIN_TX_FEE); } else { refundTx.addOutput(totalValue, LegacyAddress.fromKey(params, myKey)); refundFees = multisigFee; } TransactionSignature refundSignature = refundTx.calculateSignature(0, myKey.maybeDecrypt(userKey), getSignedScript(), Transaction.SigHash.ALL, false); refundTx.getInput(0).setScriptSig(ScriptBuilder.createCLTVPaymentChannelP2SHRefund(refundSignature, redeemScript)); refundTx.getConfidence().setSource(TransactionConfidence.Source.SELF); log.info("initiated channel with contract {}", contract.getHashAsString()); stateMachine.transition(State.SAVE_STATE_IN_WALLET); // Client should now call getIncompleteRefundTransaction() and send it to the server. }
Example #3
Source File: PaymentChannelV1ClientState.java From bcm-android with GNU General Public License v3.0 | 4 votes |
/** * Creates the initial multisig contract and incomplete refund transaction which can be requested at the appropriate * time using {@link PaymentChannelV1ClientState#getIncompleteRefundTransaction} and * {@link PaymentChannelV1ClientState#getContract()}. * By default unconfirmed coins are allowed to be used, as for micropayments the risk should be relatively low. * * @param userKey Key derived from a user password, needed for any signing when the wallet is encrypted. * The wallet KeyCrypter is assumed. * @param clientChannelProperties Modify the channel's configuration. * @throws ValueOutOfRangeException if the value being used is too small to be accepted by the network * @throws InsufficientMoneyException if the wallet doesn't contain enough balance to initiate */ @Override public synchronized void initiate(@Nullable KeyParameter userKey, ClientChannelProperties clientChannelProperties) throws ValueOutOfRangeException, InsufficientMoneyException { final NetworkParameters params = wallet.getParams(); Transaction template = new Transaction(params); // We always place the client key before the server key because, if either side wants some privacy, they can // use a fresh key for the the multisig contract and nowhere else List<ECKey> keys = Lists.newArrayList(myKey, serverKey); // There is also probably a change output, but we don't bother shuffling them as it's obvious from the // format which one is the change. If we start obfuscating the change output better in future this may // be worth revisiting. TransactionOutput multisigOutput = template.addOutput(totalValue, ScriptBuilder.createMultiSigOutputScript(2, keys)); if (multisigOutput.isDust()) throw new ValueOutOfRangeException("totalValue too small to use"); SendRequest req = SendRequest.forTx(template); req.coinSelector = AllowUnconfirmedCoinSelector.get(); req.shuffleOutputs = false; // TODO: Fix things so shuffling is usable. req = clientChannelProperties.modifyContractSendRequest(req); if (userKey != null) req.aesKey = userKey; wallet.completeTx(req); Coin multisigFee = req.tx.getFee(); multisigContract = req.tx; // Build a refund transaction that protects us in the case of a bad server that's just trying to cause havoc // by locking up peoples money (perhaps as a precursor to a ransom attempt). We time lock it so the server // has an assurance that we cannot take back our money by claiming a refund before the channel closes - this // relies on the fact that since Bitcoin 0.8 time locked transactions are non-final. This will need to change // in future as it breaks the intended design of timelocking/tx replacement, but for now it simplifies this // specific protocol somewhat. refundTx = new Transaction(params); // don't disable lock time. the sequence will be included in the server's signature and thus won't be changeable. // by using this sequence value, we avoid extra full replace-by-fee and relative lock time processing. refundTx.addInput(multisigOutput).setSequenceNumber(TransactionInput.NO_SEQUENCE - 1L); refundTx.setLockTime(expiryTime); if (Context.get().isEnsureMinRequiredFee()) { // Must pay min fee. final Coin valueAfterFee = totalValue.subtract(Transaction.REFERENCE_DEFAULT_MIN_TX_FEE); if (Transaction.MIN_NONDUST_OUTPUT.compareTo(valueAfterFee) > 0) throw new ValueOutOfRangeException("totalValue too small to use"); refundTx.addOutput(valueAfterFee, LegacyAddress.fromKey(params, myKey)); refundFees = multisigFee.add(Transaction.REFERENCE_DEFAULT_MIN_TX_FEE); } else { refundTx.addOutput(totalValue, LegacyAddress.fromKey(params, myKey)); refundFees = multisigFee; } refundTx.getConfidence().setSource(TransactionConfidence.Source.SELF); log.info("initiated channel with multi-sig contract {}, refund {}", multisigContract.getHashAsString(), refundTx.getHashAsString()); stateMachine.transition(State.INITIATED); // Client should now call getIncompleteRefundTransaction() and send it to the server. }
Example #4
Source File: PaymentChannelClientConnection.java From green_android with GNU General Public License v3.0 | 4 votes |
/** * Attempts to open a new connection to and open a payment channel with the given host and port, blocking until the * connection is open. The server is requested to keep the channel open for {@param timeWindow} * seconds. If the server proposes a longer time the channel will be closed. * * @param server The host/port pair where the server is listening. * @param timeoutSeconds The connection timeout and read timeout during initialization. This should be large enough * to accommodate ECDSA signature operations and network latency. * @param wallet The wallet which will be paid from, and where completed transactions will be committed. * Can be encrypted if user key is supplied when needed. Must already have a * {@link StoredPaymentChannelClientStates} object in its extensions set. * @param myKey A freshly generated keypair used for the multisig contract and refund output. * @param maxValue The maximum value this channel is allowed to request * @param serverId A unique ID which is used to attempt reopening of an existing channel. * This must be unique to the server, and, if your application is exposing payment channels to some * API, this should also probably encompass some caller UID to avoid applications opening channels * which were created by others. * @param userKeySetup Key derived from a user password, used to decrypt myKey, if it is encrypted, during setup. * @param clientChannelProperties Modifier to change the channel's configuration. * * @throws IOException if there's an issue using the network. * @throws ValueOutOfRangeException if the balance of wallet is lower than maxValue. */ public PaymentChannelClientConnection(InetSocketAddress server, int timeoutSeconds, Wallet wallet, ECKey myKey, Coin maxValue, String serverId, @Nullable KeyParameter userKeySetup, final ClientChannelProperties clientChannelProperties) throws IOException, ValueOutOfRangeException { // Glue the object which vends/ingests protobuf messages in order to manage state to the network object which // reads/writes them to the wire in length prefixed form. channelClient = new PaymentChannelClient(wallet, myKey, maxValue, Sha256Hash.of(serverId.getBytes()), userKeySetup, clientChannelProperties, new PaymentChannelClient.ClientConnection() { @Override public void sendToServer(Protos.TwoWayChannelMessage msg) { wireParser.write(msg); } @Override public void destroyConnection(PaymentChannelCloseException.CloseReason reason) { channelOpenFuture.setException(new PaymentChannelCloseException("Payment channel client requested that the connection be closed: " + reason, reason)); wireParser.closeConnection(); } @Override public boolean acceptExpireTime(long expireTime) { return expireTime <= (clientChannelProperties.timeWindow() + Utils.currentTimeSeconds() + 60); // One extra minute to compensate for time skew and latency } @Override public void channelOpen(boolean wasInitiated) { wireParser.setSocketTimeout(0); // Inform the API user that we're done and ready to roll. channelOpenFuture.set(PaymentChannelClientConnection.this); } }); // And glue back in the opposite direction - network to the channelClient. wireParser = new ProtobufConnection<Protos.TwoWayChannelMessage>(new ProtobufConnection.Listener<Protos.TwoWayChannelMessage>() { @Override public void messageReceived(ProtobufConnection<Protos.TwoWayChannelMessage> handler, Protos.TwoWayChannelMessage msg) { try { channelClient.receiveMessage(msg); } catch (InsufficientMoneyException e) { // We should only get this exception during INITIATE, so channelOpen wasn't called yet. channelOpenFuture.setException(e); } } @Override public void connectionOpen(ProtobufConnection<Protos.TwoWayChannelMessage> handler) { channelClient.connectionOpen(); } @Override public void connectionClosed(ProtobufConnection<Protos.TwoWayChannelMessage> handler) { channelClient.connectionClosed(); channelOpenFuture.setException(new PaymentChannelCloseException("The TCP socket died", PaymentChannelCloseException.CloseReason.CONNECTION_CLOSED)); } }, Protos.TwoWayChannelMessage.getDefaultInstance(), Short.MAX_VALUE, timeoutSeconds*1000); // Initiate the outbound network connection. We don't need to keep this around. The wireParser object will handle // things from here on out. new NioClient(server, wireParser, timeoutSeconds * 1000); }
Example #5
Source File: PaymentChannelV2ClientState.java From green_android with GNU General Public License v3.0 | 4 votes |
@Override public synchronized void initiate(@Nullable KeyParameter userKey, ClientChannelProperties clientChannelProperties) throws ValueOutOfRangeException, InsufficientMoneyException { final NetworkParameters params = wallet.getParams(); Transaction template = new Transaction(params); // There is also probably a change output, but we don't bother shuffling them as it's obvious from the // format which one is the change. If we start obfuscating the change output better in future this may // be worth revisiting. Script redeemScript = ScriptBuilder.createCLTVPaymentChannelOutput(BigInteger.valueOf(expiryTime), myKey, serverKey); TransactionOutput transactionOutput = template.addOutput(totalValue, ScriptBuilder.createP2SHOutputScript(redeemScript)); if (transactionOutput.isDust()) throw new ValueOutOfRangeException("totalValue too small to use"); SendRequest req = SendRequest.forTx(template); req.coinSelector = AllowUnconfirmedCoinSelector.get(); req.shuffleOutputs = false; // TODO: Fix things so shuffling is usable. req = clientChannelProperties.modifyContractSendRequest(req); if (userKey != null) req.aesKey = userKey; wallet.completeTx(req); Coin multisigFee = req.tx.getFee(); contract = req.tx; // Build a refund transaction that protects us in the case of a bad server that's just trying to cause havoc // by locking up peoples money (perhaps as a precursor to a ransom attempt). We time lock it because the // CheckLockTimeVerify opcode requires a lock time to be specified and the input to have a non-final sequence // number (so that the lock time is not disabled). refundTx = new Transaction(params); // by using this sequence value, we avoid extra full replace-by-fee and relative lock time processing. refundTx.addInput(contract.getOutput(0)).setSequenceNumber(TransactionInput.NO_SEQUENCE - 1L); refundTx.setLockTime(expiryTime); if (Context.get().isEnsureMinRequiredFee()) { // Must pay min fee. final Coin valueAfterFee = totalValue.subtract(Transaction.REFERENCE_DEFAULT_MIN_TX_FEE); if (Transaction.MIN_NONDUST_OUTPUT.compareTo(valueAfterFee) > 0) throw new ValueOutOfRangeException("totalValue too small to use"); refundTx.addOutput(valueAfterFee, myKey.toAddress(params)); refundFees = multisigFee.add(Transaction.REFERENCE_DEFAULT_MIN_TX_FEE); } else { refundTx.addOutput(totalValue, myKey.toAddress(params)); refundFees = multisigFee; } TransactionSignature refundSignature = refundTx.calculateSignature(0, myKey.maybeDecrypt(userKey), getSignedScript(), Transaction.SigHash.ALL, false); refundTx.getInput(0).setScriptSig(ScriptBuilder.createCLTVPaymentChannelP2SHRefund(refundSignature, redeemScript)); refundTx.getConfidence().setSource(TransactionConfidence.Source.SELF); log.info("initiated channel with contract {}", contract.getHashAsString()); stateMachine.transition(State.SAVE_STATE_IN_WALLET); // Client should now call getIncompleteRefundTransaction() and send it to the server. }
Example #6
Source File: PaymentChannelV1ClientState.java From green_android with GNU General Public License v3.0 | 4 votes |
/** * Creates the initial multisig contract and incomplete refund transaction which can be requested at the appropriate * time using {@link PaymentChannelV1ClientState#getIncompleteRefundTransaction} and * {@link PaymentChannelV1ClientState#getContract()}. * By default unconfirmed coins are allowed to be used, as for micropayments the risk should be relatively low. * @param userKey Key derived from a user password, needed for any signing when the wallet is encrypted. * The wallet KeyCrypter is assumed. * @param clientChannelProperties Modify the channel's configuration. * * @throws ValueOutOfRangeException if the value being used is too small to be accepted by the network * @throws InsufficientMoneyException if the wallet doesn't contain enough balance to initiate */ @Override public synchronized void initiate(@Nullable KeyParameter userKey, ClientChannelProperties clientChannelProperties) throws ValueOutOfRangeException, InsufficientMoneyException { final NetworkParameters params = wallet.getParams(); Transaction template = new Transaction(params); // We always place the client key before the server key because, if either side wants some privacy, they can // use a fresh key for the the multisig contract and nowhere else List<ECKey> keys = Lists.newArrayList(myKey, serverKey); // There is also probably a change output, but we don't bother shuffling them as it's obvious from the // format which one is the change. If we start obfuscating the change output better in future this may // be worth revisiting. TransactionOutput multisigOutput = template.addOutput(totalValue, ScriptBuilder.createMultiSigOutputScript(2, keys)); if (multisigOutput.isDust()) throw new ValueOutOfRangeException("totalValue too small to use"); SendRequest req = SendRequest.forTx(template); req.coinSelector = AllowUnconfirmedCoinSelector.get(); req.shuffleOutputs = false; // TODO: Fix things so shuffling is usable. req = clientChannelProperties.modifyContractSendRequest(req); if (userKey != null) req.aesKey = userKey; wallet.completeTx(req); Coin multisigFee = req.tx.getFee(); multisigContract = req.tx; // Build a refund transaction that protects us in the case of a bad server that's just trying to cause havoc // by locking up peoples money (perhaps as a precursor to a ransom attempt). We time lock it so the server // has an assurance that we cannot take back our money by claiming a refund before the channel closes - this // relies on the fact that since Bitcoin 0.8 time locked transactions are non-final. This will need to change // in future as it breaks the intended design of timelocking/tx replacement, but for now it simplifies this // specific protocol somewhat. refundTx = new Transaction(params); // don't disable lock time. the sequence will be included in the server's signature and thus won't be changeable. // by using this sequence value, we avoid extra full replace-by-fee and relative lock time processing. refundTx.addInput(multisigOutput).setSequenceNumber(TransactionInput.NO_SEQUENCE - 1L); refundTx.setLockTime(expiryTime); if (Context.get().isEnsureMinRequiredFee()) { // Must pay min fee. final Coin valueAfterFee = totalValue.subtract(Transaction.REFERENCE_DEFAULT_MIN_TX_FEE); if (Transaction.MIN_NONDUST_OUTPUT.compareTo(valueAfterFee) > 0) throw new ValueOutOfRangeException("totalValue too small to use"); refundTx.addOutput(valueAfterFee, myKey.toAddress(params)); refundFees = multisigFee.add(Transaction.REFERENCE_DEFAULT_MIN_TX_FEE); } else { refundTx.addOutput(totalValue, myKey.toAddress(params)); refundFees = multisigFee; } refundTx.getConfidence().setSource(TransactionConfidence.Source.SELF); log.info("initiated channel with multi-sig contract {}, refund {}", multisigContract.getHashAsString(), refundTx.getHashAsString()); stateMachine.transition(State.INITIATED); // Client should now call getIncompleteRefundTransaction() and send it to the server. }
Example #7
Source File: PaymentChannelClientConnection.java From GreenBits with GNU General Public License v3.0 | 4 votes |
/** * Attempts to open a new connection to and open a payment channel with the given host and port, blocking until the * connection is open. The server is requested to keep the channel open for {@param timeWindow} * seconds. If the server proposes a longer time the channel will be closed. * * @param server The host/port pair where the server is listening. * @param timeoutSeconds The connection timeout and read timeout during initialization. This should be large enough * to accommodate ECDSA signature operations and network latency. * @param wallet The wallet which will be paid from, and where completed transactions will be committed. * Can be encrypted if user key is supplied when needed. Must already have a * {@link StoredPaymentChannelClientStates} object in its extensions set. * @param myKey A freshly generated keypair used for the multisig contract and refund output. * @param maxValue The maximum value this channel is allowed to request * @param serverId A unique ID which is used to attempt reopening of an existing channel. * This must be unique to the server, and, if your application is exposing payment channels to some * API, this should also probably encompass some caller UID to avoid applications opening channels * which were created by others. * @param userKeySetup Key derived from a user password, used to decrypt myKey, if it is encrypted, during setup. * @param clientChannelProperties Modifier to change the channel's configuration. * * @throws IOException if there's an issue using the network. * @throws ValueOutOfRangeException if the balance of wallet is lower than maxValue. */ public PaymentChannelClientConnection(InetSocketAddress server, int timeoutSeconds, Wallet wallet, ECKey myKey, Coin maxValue, String serverId, @Nullable KeyParameter userKeySetup, final ClientChannelProperties clientChannelProperties) throws IOException, ValueOutOfRangeException { // Glue the object which vends/ingests protobuf messages in order to manage state to the network object which // reads/writes them to the wire in length prefixed form. channelClient = new PaymentChannelClient(wallet, myKey, maxValue, Sha256Hash.of(serverId.getBytes()), userKeySetup, clientChannelProperties, new PaymentChannelClient.ClientConnection() { @Override public void sendToServer(Protos.TwoWayChannelMessage msg) { wireParser.write(msg); } @Override public void destroyConnection(PaymentChannelCloseException.CloseReason reason) { channelOpenFuture.setException(new PaymentChannelCloseException("Payment channel client requested that the connection be closed: " + reason, reason)); wireParser.closeConnection(); } @Override public boolean acceptExpireTime(long expireTime) { return expireTime <= (clientChannelProperties.timeWindow() + Utils.currentTimeSeconds() + 60); // One extra minute to compensate for time skew and latency } @Override public void channelOpen(boolean wasInitiated) { wireParser.setSocketTimeout(0); // Inform the API user that we're done and ready to roll. channelOpenFuture.set(PaymentChannelClientConnection.this); } }); // And glue back in the opposite direction - network to the channelClient. wireParser = new ProtobufConnection<Protos.TwoWayChannelMessage>(new ProtobufConnection.Listener<Protos.TwoWayChannelMessage>() { @Override public void messageReceived(ProtobufConnection<Protos.TwoWayChannelMessage> handler, Protos.TwoWayChannelMessage msg) { try { channelClient.receiveMessage(msg); } catch (InsufficientMoneyException e) { // We should only get this exception during INITIATE, so channelOpen wasn't called yet. channelOpenFuture.setException(e); } } @Override public void connectionOpen(ProtobufConnection<Protos.TwoWayChannelMessage> handler) { channelClient.connectionOpen(); } @Override public void connectionClosed(ProtobufConnection<Protos.TwoWayChannelMessage> handler) { channelClient.connectionClosed(); channelOpenFuture.setException(new PaymentChannelCloseException("The TCP socket died", PaymentChannelCloseException.CloseReason.CONNECTION_CLOSED)); } }, Protos.TwoWayChannelMessage.getDefaultInstance(), Short.MAX_VALUE, timeoutSeconds*1000); // Initiate the outbound network connection. We don't need to keep this around. The wireParser object will handle // things from here on out. new NioClient(server, wireParser, timeoutSeconds * 1000); }
Example #8
Source File: PaymentChannelV2ClientState.java From GreenBits with GNU General Public License v3.0 | 4 votes |
@Override public synchronized void initiate(@Nullable KeyParameter userKey, ClientChannelProperties clientChannelProperties) throws ValueOutOfRangeException, InsufficientMoneyException { final NetworkParameters params = wallet.getParams(); Transaction template = new Transaction(params); // There is also probably a change output, but we don't bother shuffling them as it's obvious from the // format which one is the change. If we start obfuscating the change output better in future this may // be worth revisiting. Script redeemScript = ScriptBuilder.createCLTVPaymentChannelOutput(BigInteger.valueOf(expiryTime), myKey, serverKey); TransactionOutput transactionOutput = template.addOutput(totalValue, ScriptBuilder.createP2SHOutputScript(redeemScript)); if (transactionOutput.isDust()) throw new ValueOutOfRangeException("totalValue too small to use"); SendRequest req = SendRequest.forTx(template); req.coinSelector = AllowUnconfirmedCoinSelector.get(); req.shuffleOutputs = false; // TODO: Fix things so shuffling is usable. req = clientChannelProperties.modifyContractSendRequest(req); if (userKey != null) req.aesKey = userKey; wallet.completeTx(req); Coin multisigFee = req.tx.getFee(); contract = req.tx; // Build a refund transaction that protects us in the case of a bad server that's just trying to cause havoc // by locking up peoples money (perhaps as a precursor to a ransom attempt). We time lock it because the // CheckLockTimeVerify opcode requires a lock time to be specified and the input to have a non-final sequence // number (so that the lock time is not disabled). refundTx = new Transaction(params); // by using this sequence value, we avoid extra full replace-by-fee and relative lock time processing. refundTx.addInput(contract.getOutput(0)).setSequenceNumber(TransactionInput.NO_SEQUENCE - 1L); refundTx.setLockTime(expiryTime); if (Context.get().isEnsureMinRequiredFee()) { // Must pay min fee. final Coin valueAfterFee = totalValue.subtract(Transaction.REFERENCE_DEFAULT_MIN_TX_FEE); if (Transaction.MIN_NONDUST_OUTPUT.compareTo(valueAfterFee) > 0) throw new ValueOutOfRangeException("totalValue too small to use"); refundTx.addOutput(valueAfterFee, myKey.toAddress(params)); refundFees = multisigFee.add(Transaction.REFERENCE_DEFAULT_MIN_TX_FEE); } else { refundTx.addOutput(totalValue, myKey.toAddress(params)); refundFees = multisigFee; } TransactionSignature refundSignature = refundTx.calculateSignature(0, myKey.maybeDecrypt(userKey), getSignedScript(), Transaction.SigHash.ALL, false); refundTx.getInput(0).setScriptSig(ScriptBuilder.createCLTVPaymentChannelP2SHRefund(refundSignature, redeemScript)); refundTx.getConfidence().setSource(TransactionConfidence.Source.SELF); log.info("initiated channel with contract {}", contract.getHashAsString()); stateMachine.transition(State.SAVE_STATE_IN_WALLET); // Client should now call getIncompleteRefundTransaction() and send it to the server. }
Example #9
Source File: PaymentChannelV1ClientState.java From GreenBits with GNU General Public License v3.0 | 4 votes |
/** * Creates the initial multisig contract and incomplete refund transaction which can be requested at the appropriate * time using {@link PaymentChannelV1ClientState#getIncompleteRefundTransaction} and * {@link PaymentChannelV1ClientState#getContract()}. * By default unconfirmed coins are allowed to be used, as for micropayments the risk should be relatively low. * @param userKey Key derived from a user password, needed for any signing when the wallet is encrypted. * The wallet KeyCrypter is assumed. * @param clientChannelProperties Modify the channel's configuration. * * @throws ValueOutOfRangeException if the value being used is too small to be accepted by the network * @throws InsufficientMoneyException if the wallet doesn't contain enough balance to initiate */ @Override public synchronized void initiate(@Nullable KeyParameter userKey, ClientChannelProperties clientChannelProperties) throws ValueOutOfRangeException, InsufficientMoneyException { final NetworkParameters params = wallet.getParams(); Transaction template = new Transaction(params); // We always place the client key before the server key because, if either side wants some privacy, they can // use a fresh key for the the multisig contract and nowhere else List<ECKey> keys = Lists.newArrayList(myKey, serverKey); // There is also probably a change output, but we don't bother shuffling them as it's obvious from the // format which one is the change. If we start obfuscating the change output better in future this may // be worth revisiting. TransactionOutput multisigOutput = template.addOutput(totalValue, ScriptBuilder.createMultiSigOutputScript(2, keys)); if (multisigOutput.isDust()) throw new ValueOutOfRangeException("totalValue too small to use"); SendRequest req = SendRequest.forTx(template); req.coinSelector = AllowUnconfirmedCoinSelector.get(); req.shuffleOutputs = false; // TODO: Fix things so shuffling is usable. req = clientChannelProperties.modifyContractSendRequest(req); if (userKey != null) req.aesKey = userKey; wallet.completeTx(req); Coin multisigFee = req.tx.getFee(); multisigContract = req.tx; // Build a refund transaction that protects us in the case of a bad server that's just trying to cause havoc // by locking up peoples money (perhaps as a precursor to a ransom attempt). We time lock it so the server // has an assurance that we cannot take back our money by claiming a refund before the channel closes - this // relies on the fact that since Bitcoin 0.8 time locked transactions are non-final. This will need to change // in future as it breaks the intended design of timelocking/tx replacement, but for now it simplifies this // specific protocol somewhat. refundTx = new Transaction(params); // don't disable lock time. the sequence will be included in the server's signature and thus won't be changeable. // by using this sequence value, we avoid extra full replace-by-fee and relative lock time processing. refundTx.addInput(multisigOutput).setSequenceNumber(TransactionInput.NO_SEQUENCE - 1L); refundTx.setLockTime(expiryTime); if (Context.get().isEnsureMinRequiredFee()) { // Must pay min fee. final Coin valueAfterFee = totalValue.subtract(Transaction.REFERENCE_DEFAULT_MIN_TX_FEE); if (Transaction.MIN_NONDUST_OUTPUT.compareTo(valueAfterFee) > 0) throw new ValueOutOfRangeException("totalValue too small to use"); refundTx.addOutput(valueAfterFee, myKey.toAddress(params)); refundFees = multisigFee.add(Transaction.REFERENCE_DEFAULT_MIN_TX_FEE); } else { refundTx.addOutput(totalValue, myKey.toAddress(params)); refundFees = multisigFee; } refundTx.getConfidence().setSource(TransactionConfidence.Source.SELF); log.info("initiated channel with multi-sig contract {}, refund {}", multisigContract.getHashAsString(), refundTx.getHashAsString()); stateMachine.transition(State.INITIATED); // Client should now call getIncompleteRefundTransaction() and send it to the server. }
Example #10
Source File: PaymentChannelClientState.java From bcm-android with GNU General Public License v3.0 | 2 votes |
/** * Creates the initial multisig contract and incomplete refund transaction which can be requested at the appropriate * time using {@link PaymentChannelV1ClientState#getIncompleteRefundTransaction} and * {@link PaymentChannelClientState#getContract()}. * By default unconfirmed coins are allowed to be used, as for micropayments the risk should be relatively low. * * @param userKey Key derived from a user password, needed for any signing when the wallet is encrypted. * The wallet KeyCrypter is assumed. * @param clientChannelProperties Modify the channel's configuration. * @throws ValueOutOfRangeException if the value being used is too small to be accepted by the network * @throws InsufficientMoneyException if the wallet doesn't contain enough balance to initiate */ public abstract void initiate(@Nullable KeyParameter userKey, ClientChannelProperties clientChannelProperties) throws ValueOutOfRangeException, InsufficientMoneyException;
Example #11
Source File: PaymentChannelClientState.java From green_android with GNU General Public License v3.0 | 2 votes |
/** * Creates the initial multisig contract and incomplete refund transaction which can be requested at the appropriate * time using {@link PaymentChannelV1ClientState#getIncompleteRefundTransaction} and * {@link PaymentChannelClientState#getContract()}. * By default unconfirmed coins are allowed to be used, as for micropayments the risk should be relatively low. * @param userKey Key derived from a user password, needed for any signing when the wallet is encrypted. * The wallet KeyCrypter is assumed. * @param clientChannelProperties Modify the channel's configuration. * * @throws ValueOutOfRangeException if the value being used is too small to be accepted by the network * @throws InsufficientMoneyException if the wallet doesn't contain enough balance to initiate */ public abstract void initiate(@Nullable KeyParameter userKey, ClientChannelProperties clientChannelProperties) throws ValueOutOfRangeException, InsufficientMoneyException;
Example #12
Source File: PaymentChannelClientState.java From GreenBits with GNU General Public License v3.0 | 2 votes |
/** * Creates the initial multisig contract and incomplete refund transaction which can be requested at the appropriate * time using {@link PaymentChannelV1ClientState#getIncompleteRefundTransaction} and * {@link PaymentChannelClientState#getContract()}. * By default unconfirmed coins are allowed to be used, as for micropayments the risk should be relatively low. * @param userKey Key derived from a user password, needed for any signing when the wallet is encrypted. * The wallet KeyCrypter is assumed. * @param clientChannelProperties Modify the channel's configuration. * * @throws ValueOutOfRangeException if the value being used is too small to be accepted by the network * @throws InsufficientMoneyException if the wallet doesn't contain enough balance to initiate */ public abstract void initiate(@Nullable KeyParameter userKey, ClientChannelProperties clientChannelProperties) throws ValueOutOfRangeException, InsufficientMoneyException;