swap-bch-js
swap-bch-js is a JavaScript Library for building transactions for the Signal, Watch, and Pay (SWaP) Protocol. The library is a fork of bitcoinfilesjs and also supports the Bitcoin Files Protocol (BFP). Methods for uploading and downloading files are provided per the BFP specification. For convenience, SLP-SDK (Bitbox) functionality has been built into the library.
SWaP Protocol Reference
This library is reference code for the Signal, Watch, and Pay (SWaP) Protocol. The end-to-end examples in this repository contain the full code necessary for the SWaP Signal and Payment message classes and types described in the protocol specification. Please reference these examples when building or testing your own applications.
Installation
For node.js
npm install swap-bch-js
Example Signal Downloads
const SLPSDK = const bitbox = restURL: 'https://rest.bitcoin.com/v2/' const swap = const Swp = swapswp;const swp = bitbox;fs = ; let metadata;{ try // Exchange let exchangeMetadata = await swpbitdb; console; let exchangeByTokenMetadata = await swpbitdb; console; // Escrow let escrowMetadata = await swpbitdb; console; let escrowByOracleMetadata = await swpbitdb; console; // Crowdfund let crowdfundMetadata = await swpbitdb; console; console catch e console };
Example Exchange Offer Signal Upload
Full exchange end-to-end example in examples directory
const SLPSDK = const bitbox = restURL: 'https://rest.bitcoin.com/v2/' const swap = const Swp = swapswp;const swp = bitbox;const network = swpnetwork; { try // 1 - Set the funding address and WIF let fundingWif = 'L3TS24MB1RhG3tcod2se7ik59WQRsUFkDiG8S2QNaPsapby7DmjR' let fundingEcpair = bitboxECPair let fundingAddress = bitboxECPair let msgType = 1 // exchange // 2 - estimate upload cost for funding the transaction const fileSize = 0 // No file included in this tx let config = tokenId: 'c4b0d62156b3fa5c8f3436079b5394f7edc1bef5dc1cd2f9d0c4d46f82cca479' buyOrSell: 'BUY' rate: 280000 reserve: false exactUtxoTxId: '9a1a5be139ee12e487ea34e5ff62d90995946f876227be056d5d088a79a302ee' // txid of the UTXO to spend exactUtxoIndex: 1 // index (vout) of the UTXO you want to spend in the exchange minSatsToExchange: 140000 let uploadCost = Swp; console; // 3 - Make sure address above is funded with the amount equal to the uploadCost let fundingUtxos = await network; let availableSats = fundingUtxos console console ifavailableSats < uploadCost throw 'Insufficient funds in '+fundingAddress+' to send offer signal. Needed: '+uploadCost // wait for network to resolve... // 4 - upload the file let fileId = await swp; console; } catch e console }; // wait for upload to complete resolve... Done.
Example Escrow Offer Signal Upload
Full escrow end-to-end example in examples directory
const BITBOXSDK = const bitbox = restURL: 'https://rest.bitcoin.com/v2/' ;const swap = const Swp = swapswp;const swp = bitbox;const network = swpnetwork; { try // 1 - Set the funding address and WIF let fundingWif = 'L3TS24MB1RhG3tcod2se7ik59WQRsUFkDiG8S2QNaPsapby7DmjR' let fundingEcpair = bitboxECPair let fundingAddress = bitboxECPair let msgType = 2 // escrow // 2 - estimate upload cost for funding the transaction const fileSize = 0 // No file included in this tx let config = oracleBfp: 'ee10994d61ae1318d6298975283509e44eeace497d30765cf1c7bc48a7410d2f' contractTermsIndex: 0 contractPartyIndex: 1 compilerId: 'jeton' compilerVersion: 'e01' pubKey: '025d8a5fb65c9171946a17b791279862d85e7fe88ea194f85ff95de15388c7e666' exactUtxoTxId: '3bebd6590b0870e13a65fbb6a59e891ec06cd2f1c162f2b3c034d8b1e1ae88b9' exactUtxoIndex: 1 appendedScriptPubKey: '76a91410c1db6f3076e020974ef540199e7ae4b76fbafa88ac' appendedSats: 2000 let uploadCost = Swp; console; // 3 - Make sure address above is funded with the amount equal to the uploadCost let fundingUtxos = await network; let availableSats = fundingUtxos console console ifavailableSats < uploadCost throw 'Insufficient funds in '+fundingAddress+' to send offer signal. Needed: '+uploadCost // wait for network to resolve... // 4 - upload the file let fileId = await swp; console; catch e console }; // wait for upload to complete resolve... Done.
Example Crowdfund Offer Signal Upload
Full threshold crowdfunding end-to-end example in examples directory
const SLPSDK = const bitbox = restURL: 'https://rest.bitcoin.com/v2/' const swap = const Swp = swapswp;const swp = bitbox;const network = swpnetwork;const utils = swaputils { let bufArray = } { try // 1 - Set the funding address and WIF let fundingWif = 'L3TS24MB1RhG3tcod2se7ik59WQRsUFkDiG8S2QNaPsapby7DmjR' let fundingEcpair = bitboxECPair let fundingAddress = bitboxECPair let msgType = 3 // crowdfund let outputArray= script: Buffer value: 100000 script: Buffer value: 150000 let outputsBuf = utils // 2 - estimate upload cost for funding the transaction const fileSize = 0 // No file included in this tx let config = msgClass: 1 msgType: msgType campaignUri: 'https://swapcrowdfund.com/somecampaign' outputs: outputsBuf let uploadCost = Swp; console; // 3 - Make sure address above is funded with the amount equal to the uploadCost let fundingUtxos = await network; let availableSats = fundingUtxos console console ifavailableSats < uploadCost throw 'Insufficient funds in '+fundingAddress+' to send offer signal. Needed: '+uploadCost // wait for network to resolve... // 4 - upload the file let fileId = await swp; console; catch e console }; // wait for upload to complete resolve... Done.