Skip to main content

Data Output

Summary

This how-to guide explains how to create a data transaction to store arbitrary data on the Bitcoin blockchain using the mintBlue SDK.

Data outputs are published in so-called OP_RETURN scripts and use an envelope according to the Univrse spec.

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 data outputs. Here is its interface.

interface DataOutput {
type: 'data';
value: string | string[] | Buffer;
sign?: boolean;
encrypt?: boolean;
}

The data type indicates a data output. It can contain any value if it fits the JSON data structure. Here's an example of a customer order we provide to the value attribute.

const outputs = [
{
type: "data",
value: {
order_id: "order-123",
order_date: "2021-10-10T10:25:31.719Z",
order_items: [
{
item_id: "item-1",
item_price: 100,
}
],
},
},
];

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 data 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 output in the array is your data output.

[
{
"type": "data",
"value": {
"order_id": "order-123",
"order_date": "2021-10-10T10:25:31.719Z",
"order_items": [
{
"item_id": "item-1",
"item_name": "First item",
"item_price": 100,
"item_quantity": 1
}
]
},
"encrypt": false,
"sign": false
},
{
"type": "payment",
"to": "1MFbKnKAt4LMUmRvDohmhiYgYhFsAaSDux",
"satoshis": 24672
}
]

Code Check  ✅

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

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

const outputs = [
{
type: "data",
value: {
order_id: "order-123",
order_date: "2021-10-10T10:25:31.719Z",
order_items: [
{
item_id: "item-1",
item_price: 100,
},
],
},
},
];

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

main();