A bitcoin user (bob1) would like to receive funds on his off-chain Lightning wallet. In order to do that the user will generate a Lightning invoice and show it the to Swap Provider (alice1). The Swap Provider will prompt the user to pay an on-chain address (the P2WSH swap smart contract). Once the swap has been paid and confirmed, the Swap Provider is able to redeem the funds to his wallet and can pay the Lightning invoice of the user.
To learn more about how submarine swaps works you can read this nice article from Boltz exchange. For more info regarding the swap smart contract can be found in Alex Bosworth’s submarineswaps repository.
-
NodeJS >= 10.x
-
JQ (JSON parser)
-
Bitcoin Core >= 18.x
You need a work environment with Bitcoin Core on regtest and two LND nodes connected to it. One LND is the Swap Provider, the other is the Bitcoin user. For simplicity we create a direct channel between them.
Install dependencies
$ npm install
Generate a bunch of testing wallets and import the private keys in your running Bitcoin Core
$ npm run wallets
$ lncli-user addinvoice 1000 [PREIMAGE] PAYMENT_REQUEST
node swap_p2wsh.js on2off PAYMENT_HASH TIMELOCK [PREIMAGE]
|
Note
|
User pays 200 satoshis more to compensate for the mining fees that the Swap Provider will have to pay to redeem the funds.bitcoin-cli gettransaction or getrawtransaction to get the output index (TX_VOUT) for step 7.
|
sendtoaddress P2WSH_ADDRESS 0.000012
6. Swap Provider must pay the invoice in order to get the preimage that allows him to redeem the on-chain funds
lncli-sp payinvoice PAYMENT_REQUEST
|
Note
|
Get TX_ID and TX_VOUT from step 5 |
node spending_swap_tx.js claim on2off TX_ID TX_VOUT WITNESS_SCRIPT TIMELOCK PREIMAGE sendrawtransaction TX_HEX getrawtransaction TX_ID
node spending_swap_tx.js refund on2off TX_ID TX_VOUT WITNESS_SCRIPT TIMELOCK sendrawtransaction TX_HEX getrawtransaction TX_ID
|
Note
|
Swap Provider redeem address is alice1 p2wpkh address |
bitcoin-cli scantxoutset start '["addr(bcrt1qlwyzpu67l7s9gwv4gzuv4psypkxa4fx4ggs05g)"]'
lncli-sp addinvoice 1200 [PREIMAGE] PAYMENT_REQUEST
node swap_p2wsh.js off2on PAYMENT_HASH TIMELOCK [PREIMAGE]
|
Note
|
bitcoin-cli gettransaction or getrawtransaction to get the output index (TX_VOUT) for step 6
|
sendtoaddress P2WSH_ADDRESS 0.000012
5. User must pay the invoice in order to get the preimage that allows him to redeem the on-chain funds
lncli-user payinvoice PAYMENT_REQUEST
|
Note
|
Input: 1200 sats → Output: 1000 sats |
node spending_swap_tx.js claim off2on TX_ID TX_VOUT WITNESS_SCRIPT TIMELOCK PREIMAGE sendrawtransaction TX_HEX getrawtransaction TX_ID
|
Note
|
Input: 1200 sats → Output: 1000 sats |
node spending_swap_tx.js refund off2on TX_ID TX_VOUT WITNESS_SCRIPT TIMELOCK sendrawtransaction TX_HEX getrawtransaction TX_ID
|
Note
|
User redeem address is bob1 p2wpkh address |
bitcoin-cli scantxoutset start '["addr(bcrt1qlwyzpu67l7s9gwv4gzuv4psypkxa4fx4ggs05g)"]'