Catenis File
Helper library used to prepare file contents to be stored/recovered onto/from the Bitcoin blockchain via Catenis Enterprise.
Installation
npm install catenis-file
Usage
Prepare file contents to be stored onto the blockchain
Send file contents directly
const ctnFile = require('catenis-file');
const fileInfo = {
fileName: 'mysamplefile.txt',
fileType: 'text/plain',
fileContents: Buffer.from('This is only a test.')
};
const modifiedFileContents = ctnFile.FileHeader.encode(fileInfo);
Break file contents into chunks before sending it
const ctnFile = require('catenis-file');
const fileInfo = {
fileName: 'mysamplefile.txt',
fileType: 'text/plain',
fileContents: Buffer.from('A very long text...')
};
const modifiedFileContents = ctnFile.FileHeader.encode(fileInfo);
const msgChunker = new ctnFile.MessageChunker(modifiedFileContents, 'base64', 1024);
let msgChunk;
while ((msgChunk = msgChunker.nextMessageChunk()) !== undefined) {
}
Note: in practice, the contents of a file only needs to be broken into chunks if it is very long, typically
above 10 MB.
Recover file contents stored on the blockchain
Receive the whole file contents directly
const ctnFile = require('catenis-file');
function receiveFile(fileContents) {
const fileInfo = ctnFile.FileHeader.decode(Buffer.from(fileContents, 'base64'));
}
Receive file contents in chunks
const ctnFile = require('catenis-file');
const msgChunker = new ctnFile.MessageChunker('base64');
let fileInfo;
function receiveFileChunk(fileChunk) {
if (msgChunker.getBytesCount() === 0) {
fileInfo = ctnFile.FileHeader.decode(Buffer.from(fileChunk, 'base64'));
fileChunk = fileInfo.fileContents.toString('base64');
}
msgChunker.newMessageChunk(fileChunk);
}
fileInfo.fileContents = Buffer.from(msgChunker.getMessage(), 'base64');
License
This Node.js module is released under the MIT License. Feel free to fork, and modify!
Copyright © 2020, Blockchain of Things Inc.