Skip to main content

File Output

Summary​

This how-to guide explains how to create a file transaction to store a file's contents on the Bitcoin blockchain using the mintBlue SDK.

Prerequisites​

We recommend you complete the Getting Started section to learn how to interact with mintBlue through the mintBlue SDK.

Prepare data outputs​

First, we need to prepare file outputs. Here is the interface for the file output.

interface FileOutput {
type: 'file';
value: {
contentType: string;
content: Buffer;
};
sign?: boolean;
encrypt?: boolean;
}

The file type indicates a file output. We need to define the content type, which is a MIME type, and the content, which is a Buffer.

Here's an example of a plain text file we want to store for a customer order.

const outputs = [
{
type: 'file',
value: {
contentType: 'text/plain',
content: Buffer.from("Order #1234 [...]"),
}
}
];

However, we can also create a file output containing an image. Let's imagine we have an image stored in a directory called /images. Here's a code snippet that reads the image as a Buffer.

const imageBuffer = fs.readFileSync(path.join(__dirname, 'images', 'image.png'), 'base64');
const outputs = [
{
type: 'file',
value: {
contentType: 'image/png',
content: imageBuffer,
},
sign: false,
encrypt: false,
}
];

In short, any file type is supported. You need to make sure you set the correct contentType and load the file as a Buffer or string.

Send the transaction to mintBlue​

Next, let's send the customer order to mintBlue to handle the final Bitcoin blockchain storage. Here's how you can do this using the mintBlue SDK. Make sure to replace the <YOUR-PROJECT-ID> with your project ID.

const { txid, rawtx } = await client.createTransaction({
project_id: "<YOUR-PROJECT-ID>",
outputs,
});

Once the file transaction has been created, the createTransaction function returns a transaction ID and the raw transaction data. To look up the transaction on a blockchain explorer, let's print the txid variable.

console.log(`https://whatsonchain.com/tx/${txid}`);

Look up the transaction output​

Let's look up the transaction output via the mintBlue console. Make sure to access the correct project. Here's what your transaction output should look like. The first object in the array is your file output. Here, the data is represented in its Buffer format.

[
{
"type": "file",
"value": {
"content": {
"type": "Buffer",
"data": [
79,
114,
100,
101,
114,
32,
35,
49,
50,
51,
52,
32,
91,
46,
46,
46,
93
]
},
"contentType": "text/plain"
},
"encrypt": false,
"sign": false
},
{
"type": "payment",
"to": "1MFbKnKAt4LMUmRvDohmhiYgYhFsAaSDux",
"satoshis": 24701
}
]

Code Check  ✅​

const fs = require("fs");
const path = require("path");
const { Mintblue } = require("@mintblue/sdk");

async function main() {
const token = "<YOUR-SDK-TOKEN>";
const client = await Mintblue.create({ token });

const imageBuffer = fs.readFileSync(path.join(__dirname, 'images', 'image.png'), 'base64');
const outputs = [
{
type: 'file',
value: {
contentType: 'image/png',
content: imageBuffer,
},
sign: false,
encrypt: false,
}
];

const { txid, rawtx } = await client.createTransaction({
project_id: "<YOUR-PROJECT-ID>",
outputs,
});
console.log(`https://whatsonchain.com/tx/${txid}`);
}

main();