[Library] Encrypt / decrypt messages with your NEAR account’s keypair

TL;DR

Near-js-encryption-box is an experimental library to encrypt and decrypt data using the NEAR account’s ed25519 keypairs; you can use it to store encrypted data on-chain, off-chain, or in any decentralized storage (IPFS, Arweave).

Be aware that storing any encrypted secrets publically (e.g. in a blockchain or IPFS) is risky since encryption could eventually be cracked in the future using new hardware or new algorithms (e.g. Quantum computing). Also consider that this repo has not been audited; use at your own risk

Motivation

We always talk about ownership of assets and data on chains, but I was wondering myself: how do you privately store data? Some projects would do it off-chain in a centralized database and this requires a centralized backend. So I wanted to explore ways to encrypt data to store it on-chain or in any decentralized storage. That is why I built near-js-encryption-box.

How to use

You can use it for sending a secret message from Alice to Bob or even from Alice to herself.

import { create, open } from 'near-js-encryption-box';
import { utils } from 'near-api-js';

// Randomly generating key pairs for the example
const keyPairAlice = utils.key_pair.KeyPairEd25519.fromRandom();
const keyPairBob = utils.key_pair.KeyPairEd25519.fromRandom();

// Encrypting a message
const message = 'Hello Bob';
const publicKeyBob = keyPairBob.getPublicKey().toString();
const privateKeyAlice = keyPairAlice.secretKey;
const { secret, nonce } = create(message, publicKeyBob, privateKeyAlice); // you can also pass your own custom nonce as a 4th parameter

// Decrypting the message
const publicKeyAlice = keyPairAlice.getPublicKey().toString();
const privateKeyBob = keyPairBob.secretKey;
const messageReceived = open(secret, publicKeyAlice, privateKeyBob, nonce);
console.log(messageReceived); // will return 'Hello Bob'

Find more examples in the near-js-encryption-box-test.ts

Repository

Feedback

I would love your feedback, feel free to open issues in the repository or just to answer in this thread.

6 Likes

Amazing.

Is it too late to create some Bounties for Metabuidl III to encourage more teams to explore and implement these tooling? :man_detective:

1 Like