Developer SDK

Introduction

BTFS is currently compatible with the same JavaScript and Go SDK libraries as IPFS. This guide walks users through a simple example of integrating the BTFS network and TRON smart contracts via the SDK.

Example

Suppose your web-based DApp's main HTML page references the backend JS file:

<script type="text/javascript" src="backend.js?2019.06.03.v2"> </script>

To implement a function adding files to BTFS, you can first define the IP address and accessible port of your BTFS node.

const ipAddr = "api.btfs.trongrid.io";
const ipPort = "443";

Then define a variable with the host IP, port number, and protocol.

const btfs = window.IpfsApi({host:ipAddr, port:ipPort, protocol:'https'});

The add method is of the form .add(data, [options], [callback]), where the data may be:

  • a Readable Stream
  • a Buffer Instance
  • a File
  • a Pull Stream
  • an array of objects, each taking the form:
{
  path: '/tmp/myfile.txt', // The file path
  content: <data> // A Buffer, Readable Stream, Pull Stream or File with the contents of the file
}
// backend.js

const ipAddr = "api.btfs.trongrid.io";
const ipPort = "443";

async function add() {
    const btfs = window.IpfsApi({host:ipAddr, port:ipPort, protocol:'https'});
    const Buffer = window.IpfsApi().Buffer;
    const newReader = new FileReader();
    var movieHash, coverHash;
    newReader.onloadend = function () {
        const buf = Buffer.from(newReader.result);
        btfs.files.add(buf, (err, result) => {
            if(err) {
                console.error(err);
                return
            }
            let url = `https://yourURL./btfs/${result[0].hash}`;
            console.log(`Url --> ${url}`);
            document.getElementById("url").innerHTML= url;
            document.getElementById("url").href= url;
            document.getElementById("url").target = '_blank';
        });
    };
    const DApp = document.getElementById("DApp");
    newReader.readAsArrayBuffer(DApp.files[0]);
}

Resources