1. What is payment channel
Traditional service providers always provide their service for fee, as they make earns by accumulated users in other fields. Free service bring some problems:
- Quality of service is not guaranteed。
- Frequency limit。
- Risk of DDOS。
In this scenario, we introduce payment channel to solve these problems. We take advantage of natural monetary character of bitcoin to implement payment channel. User pay a slight of bitcoin to use service.
Service providers are willing to provide better non-free service, meanwhile, DDOS is keep in the way — for it takes money to use the service.
2. What paymentchanneldo
There are always two ways for service provider to settle the money for their service：
- Pay money for every requests of service.
- Pay money after several times of service.
The down side of the first way is that, it costs too much transaction fee, while the down side of the second way is that, we can not stop user from leaving without settlement.
To solve the above problems, we combine these two ways, as well as the malleability of bitcoin,and design a kind of work flow for payment channel as demonstrated below:
The purple one represents the user，we can call him Alice; and the yellow one represents the service provider，we can call him Bob. D0，D1…Dn represents the datas，Tx1，Tx2…Txn represent the fee that Alice pay for the service.
Below is the structure of TXn:
What behind the structure of TXn is that：
- Total amount of vouts is great than the amount of vin, so this tx wil not be accepted by P2P network.
- There is a ANYONECANPAY flag for SigHash of vin0, which means that others can append a vin to the tx and broadcast the tx to the P2P network.
- There are 3 ways to unlock the locking script for vout0, the first one force Bob to unlock vout0 only if he provide the data that alice wants, we can replace it by P2PKH for Bob if Bob is trusted.
- vout1 represents charge for Alice
- vout2 is a large amount of money to prevent the tx being broadcast to the P2P network by others.
So, everytime Alice request for service, he takes TXn alone with the request, and Txn represents the fee for the service, which should increases everytime to meet demand the accumulated service until a settlement. After that, the fee should be calculated from 0. Under this scenario, Bob can choose a suitable chance to settle the service, without worrying Alice leave unexpectedly;for Alice, he already enjoy the service, and will lose nothing. For this procedur, one settlement for many rounds of service, only very slight of fee is needed.
Below this service that based on payment channel:
- Tx broadcast service
3.1 Tx broadcast service
Some apps may manipulate a large amount of txs, which will cost much even if BSV charge at low fee rate.So, mempool, as a mining pool operator, provides a service to help putting low fee txs to blockchain.
Let’s call app developer as Alice，and Mempool as Bob:
- Alice invoke the service to obtain a pubkey, and make it the id of the payment channel.
- Alice construct TX1 with the pubkey for Bob, as demonstrated in chapter 2.
- Alice sends TX1 to Bob, as long as the txs he wishes to broadcast.
- Bob validates Tx1, for instance, vin0 should be unspent, and vout0 should meet the service fee.
- After that, Bob puts the txs to mempool and informs Alice.
- Alice receives the reponse, and send another request with amount for vout0 enlarged.
What should be take into account is that:
- No concurrent request is permitted in one single payment channel, which means one should send the next request only if he receive the response for previous request.
3.1.2 Api to fetch a channel
AuthCode is the identifier for Alice, which might be replace by token after login.
3.1.3 Api to broadcast txs
Rawtx is the tx to pay the service fee in hex format(see chapter 2)，payload are the txs to be put on the blockchain in hex format, sig is the signature for the combination of rawtx and payload, which can prevent payload being modified by middle man.
There are 3 kinds of responses:
- Success and no settlement, and there will be a signature for payload by Bob.
- Success and with settlement, and there will be a signature for payload by Bob, as well as a field ‘settled’ to indicate that the payment channel has been settled, so Alice need to recalculate the fee from 0.
- Failed and the reason.
” a signature for payload by Bob” is a way to force Bob to put the payload to blockchain.If not,Alice can sue Bob and take the signature as a evidence that Bob promised to do so.Below is the format of the response:
settled_tx:” 987f0b7354fb9124868b926752cc948902b0fa4d05b90dec3d66fa2edce986f4”, (optional)