Coder Social home page Coder Social logo

nearfoundation / near-js-encryption-box Goto Github PK

View Code? Open in Web Editor NEW
15.0 3.0 1.0 190 KB

An experimental library to encrypt and decrypt data using the NEAR account's ed25519 keypairs

License: MIT License

TypeScript 100.00%
ed25519 encryption-decryption near near-blockchain near-protocol nearprotocol secret-box

near-js-encryption-box's Introduction

near-js-encryption-box NEAR License

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).

โš ๏ธ This is an experimental library. We do not recommend to use it to store confidential information publically.

Installation

npm install @nearfoundation/near-js-encryption-box

Usage

You can find below an example where Alice encrypt data with her private key and Bob's public key. Then Bob can decrypt the message with his private key, Alice's public key and a nonce.

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

Encryption

  • Convert NEAR Ed25519 signing key pair into Curve25519 key pair suitable for Diffie-Hellman key; using ed2curve.js
    • "Note that there's currently no proof that this is safe to do. It is safer to share both Ed25519 and Curve25519 public keys (their concatenation is 64 bytes long)."
  • Then uses Curve25519-XSalsa20-Poly1305 implemented by TweetNaCl.js

Authors

License

MIT License

near-js-encryption-box's People

Contributors

sandoche avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

cyt1999

near-js-encryption-box's Issues

How secure is your library?

I use it in my project to encode private conversation, but wonder about notice "This is an experimental library. We do not recommend to use it to store confidential information publically."

How can I test it or maybe you have suggestions how secure is your library. Any plans for improvements?

I used your library for private mode in direct messages:
https://test.chatme.page/

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.