Skip to main content

Encrypt and Sign Outputs

Summary​

This how-to guide explains creating a signed and encrypted transaction to store arbitrary data on the Bitcoin blockchain using the mintBlue SDK. Only the Data Output and File Output support encryption and signing.

Prerequisites​

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

Set the "sign" and "encrypt" variables​

The code sample below shows a data transaction with the value attribute set to a customer order ID and their name.

const outputs = [
{
type: "data",
value: {
email: "john@doe.com",
order: "order#1234"
}
}
];

However, we don't want to store this data without encrypting and signing it. To do so, you can set the sign and encrypt variables to true. You can also choose to encrypt the data without signing it. Here's a code example with signing and encryption enabled.

const outputs = [
{
type: "data",
value: {
name: "John Doe",
email: "john@doe.com",
order: "order#1234",
},
sign: true,
encrypt: true
},
];

Now, let's send the encrypted data to mintBlue to handle the final Bitcoin blockchain storage. Make sure to replace the <YOUR-PROJECT-ID> variable with your project ID.

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

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.

[
{
"type": "data",
"value": {
"name": "John Doe",
"email": "john@doe.com",
"order": "order#1234"
},
"encrypt": true,
"sign": true
},
{
"type": "payment",
"to": "1Juc9Tjmb6wc6yCaWFuB9QdEEQ621992ks",
"satoshis": 24844
}
]

The mintBlue Console presents the data in an unencrypted format, utilizing your keys to decrypt the transaction contents.

Alternatively, the information is displayed in an encrypted and signed form when accessing a transaction through a blockchain explorer, such as WhatOnChain. The output is unreadable to humans.

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: {
name: "John Doe",
email: "john@doe.com",
order: "order#1234",
},
sign: true,
encrypt: true
}
];

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

main();