Giter VIP home page Giter VIP logo

bitcoinfilesjs's Introduction

bitcoinfilesjs

bitcoinfilesjs is a JavaScript Library for building transactions for Bitcoin Files Protocol (BFP). Methods for uploading and downloading files are provided per the BFP specification. For convenience, BITBOX network functionality has been built into the library.

Other tools using the Bitcoin Files Protocol include:

NPM

Installation

For node.js

npm install bitcoinfiles

For browser

<script src='https://unpkg.com/bitcoinfiles'></script>

Example File Download

const Bfp = require('bitcoinfiles').bfp;
const bfp = new Bfp();

// 1 - download file using URI
let result;
(async function(){
    result = await bfp.downloadFile('bitcoinfile:7e4600323c934926369c136562f5483e3df79baf087c8dd2b0ed1aea69d5ee49');
    console.log("download complete.");
})();

// Wait for download to complete -- Mario.png TAKES ABOUT 6 SEC TO DOWNLOAD!

// 2 - result includes a boolean check telling you if the file's sha256 matches the file's metadata```
if(result.passesHashCheck){
    console.log("Success: downloaded file sha256 matches file's metadata");
}

// 3 - do something with the file...
let fileBuffer = result.fileBuf;

Example File Upload

Below is a simple example. For a more complete React.js file upload example visit SimpleToken.cash website

const Bfp = require('bitcoinfiles').bfp;
const Network = require('bitcoinfiles').network;
const BITBOX = require('bitcoinfiles').bitbox;

const bfp = new Bfp();
const network = new Network();

// 1 - get a file and file metadata somehow 
const someFileBuffer = new Buffer.from('aabbccddeeff', 'hex');
const fileName = 'a test file';
const fileExt = 'txt';
const fileSize = someFileBuffer.length
const fileSha256Hex = BITBOX.Crypto.sha256(someFileBuffer).toString('hex');

// 2 - estimate upload cost for funding the transaction
let config = {
    msgType: 1,
    chunkCount: 1,
    fileName: fileName,
    fileExt: fileExt,
    fileSize: fileSize,
    fileSha256Hex: fileSha256Hex,
    prevFileSha256Hex: null,
    fileUri: null,
    chunkData: null  // chunk not needed for cost estimate stage
};
let uploadCost = Bfp.calculateFileUploadCost(fileSize, config);
console.log('upload cost: ', uploadCost);

// 3 - create a funding transaction
let fundingAddress = 'bitcoincash:qqgvrkm0xpmwqgyhfm65qxv70tjtwma6lgk07ffv9u'
let fundingWif = 'KzcuA9xnDRrb9cPh29N7EQbBhQQLMWtcrDwKbEMoahmwBNACNRfa'

// 4 - Make sure address above is funded with the amount equal to the uploadCost
let fundingUtxo;

(async function(){
    let txo = await network.getLastUtxoWithRetry(fundingAddress);
    
    console.log('got funding Utxo.')
})();

// wait for network to resolve...

// 5 - upload the file
let fileId;
(async function(){
 fileId = await bfp.uploadFile(fundingUtxo, fundingAddress, fundingWif, someFileBuffer, fileName, fileExt);
 console.log('fileId: ', fileId);
})(); 

// wait for upload to complete resolve... Done.

Get File Metadata

const bfp = require('bitcoinfiles');

let metadata;
(async function(){
    metadata = await bfp.bitdb.getFileMetadata("dc76c5bd116fd61713c5b454b393212e33a1b2a8c926dcc40261f955d59b8e90","qrg3fvfue463rc5genp2kyrj4mg6g2lpxst0y4wamw");
    console.log('metadata: ', metadata);
})

// metadata : 
// { filename: 'tes158',
//        fileext: '.json',
//        size: '017a',
//        sha256: '018321383bf2672befe28629d1e159af812260268a8aa77bbd4ec27489d65b58',
//        prev_sha256: '',
//        ext_uri: '' }

bitcoinfilesjs's People

Contributors

adrianbarwicki avatar jcramer avatar nodemaster115 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.