Payment Channels
Any transaction that has both an nLocktime in the future or an nSequence that is less than 0xFFFFFFFF is in a payment channel. Typically transactions can be updated once, or many times between peers, each time looking either for nLocktime to expire, or for all until either nSequence is finalised or nLocktime runs out and one of the parties transmits the transaction to the blockchain. Payment channels are useful for streaming data, operating a sequence of events or operating with a live dataset in applications such as gaming and more. Payment channels have been unavailable since early versions of Bitcoin and are a new field with huge potential in many areas.
Properties
- Payment channels can be opened and closed arbitrarily
- It is possible to open a payment channel however with or without an on-chain action
- Payment channels can be private or public
- You can have many peers in a channel
- You can add and remove peers from a channel
- Payment channels are data conduits
- Channels are typically closed with an on-chain payment however an on-chain payment can be made outside the channel which causes it to update
Connecting and using payment channels
- User peer connects to provider peer via known endpoint
- Provider gives peer new endpoint (can be CGA) and Tx template
- User peer completes TX by adding funding inputs
- Provider supplies data and information to access next item (e.g. movie frames)
- Peers keep track of channel state using Sequence
Example - Streaming Movie
The following goes through the sequence of opening, using and closing a payment channel for the purposes of serving streamed content.
STEP 1
User browses catalog for titles to watch. Content can be on-chain or off-chain, but should be provably linked to a hash of the overall data. This hash can be the merkle root of all of the chunks that will be served to the user. The user sends this hash to the service provider to select the content.
Step 2
User selects the content. At this point, there can be a few ways to manage the channel:
- In public via mining network
- In private with pre-funded UTXO per channel
- In private with separate channels for content purchase and service delivery per user
Step 3
In this example, peers will use a timelocked UTXO in a public channel managed by miners to serve the content. For the sake of this example we will assume a single UTXO is being used. This UTXO goes into a double spend monitoring pool.
The viewer sends a transaction with the following output script to the provider to initiate the channel:
Where:
- Svn is the Viewer's nth iteration of the channel signature
- Pv is the Viewer's pubkey
- Hv is the Viewer's PKH
- Spn is the service provider's nth iteration of the channel signature
- Pp is the service provider's pubkey
- Hp is the service provider's PKH
- Hc0 is the hash of the merkle root of the content being selected
- Hcn is the hash of each content frame with Hc1 being the hash of the first frame
- Cn is the content frame with C1 being the first frame data
- Hfm is the hash of a message the user can trigger to prematurely end or pause the stream
- Fm is a message that the provider uses to end the stream
The sequence no. for the input being spent is still 1
The transaction iterates between the two scripts as as follows:
Iteration 1
Script No. | Purpose | Script | Amount |
---|---|---|---|
1 | Iteration 1: Content Request | Hc0 DROP DUP HASH160 Hp EQUALVERIFY CHECKSIGVERIFY EQUALVERIFY DUP HASH160 Hv EQUALVERIFY CHECKSIG | Price of first frame |
2 | Change | Viewer's change script | Change amount |
The Viewer also supplies the following input script:
- Sv0 Pv
which would allow the service provider to countersign the tx and settle on the network, guaranteeing payment for the first frame.
The transaction pays out the price of the first frame to the service provider and the remainder is returned to the viewer as change. If the viewer cancels their session without watching, the transaction can still be processed on the network
Step 4
The provider responds with a new version of the transaction that modifies the output as shown:
Script No. | Purpose | Script | Amount |
---|---|---|---|
1 | Iteration 2: First frame | SHA256 Hc1 EQUALVERIFY DUP HASH160 Hp EQUALVERIFY CHECKSIGVERIFY EQUALVERIFY DUP HASH160 Hv EQUALVERIFY CHECKSIG | Price of second frame |
2 | Change | Viewer's change script | Change amount |
In this iteration, they sign to indicate they have received the first frame and provide the streaming service with payment for the second frame. This can be optimized for each service provider's fee model.
They also provide a new signature Sp0 for this output, allowing the user to countersign should they need to close the channel.
Step 5
The viewer completes the transaction as shown:
Script No. | Purpose | Script | Amount |
---|---|---|---|
1 | Iteration 2: First frame | SHA256 Hc2 EQUALVERIFY DUP HASH160 Hp EQUALVERIFY CHECKSIGVERIFY EQUALVERIFY DUP HASH160 Hv EQUALVERIFY CHECKSIG | Price of second frame |
2 | Change | Viewer's change script | Change amount |
As in the previous iteration, they sign to indicate they have received the frame and provide the streaming service with payment for the next frame. This can be optimized for each service provider's fee model.
They also provide a new signature for this output, Sv1, allowing the service provider to countersign should they need to close the channel. In this example, the keypairs being used don't need to change for each iteration, but they could.
Step N
The user can request that the provider close the channel. Streams can also be paused, or take place at any framerate or even use larger transactions to move chunks at a time. In this case the user is deciding to close it.
Script No. | Purpose | Script | Amount |
---|---|---|---|
1 | Iteration N: Final frame | SHA256 Hfm EQUALVERIFY DUP HASH160 Hp EQUALVERIFY CHECKSIGVERIFY EQUALVERIFY DUP HASH160 Hv EQUALVERIFY CHECKSIG | Price of second frame |
2 | Change | Viewer's change script | Change amount |
The user puts the finish message hash at the front of the output. The streaming provider sees this and knows the customer wants to close the channel. The user provide a new signature for this output, Svn. This allows the streaming service provider to finalise the channel.
Step N+1
The user can request that the provider close the channel. Streams can also be paused, or take place at any framerate or even use larger transactions to move chunks at a time. In this case the user is deciding to close it.
The provider signs the transaction and solves the puzzle with the following script:
Svn Pv Spn Pp Fm
In this way rather than closing the transaction with a potentially large blob of content data at the front, just a short code need be used.