vechain / connex

The mono-repo contains libraries to help build dApps for VeChain.
https://docs.vechain.org/developer-resources/sdks-and-providers/connex
GNU Lesser General Public License v3.0
85 stars 3.6k forks source link

Add encryption service (encrypt/decrypt) to connex + Sync2 #125

Closed ifavo closed 1 year ago

ifavo commented 2 years ago

It would be great to handle string encryption using connex and Sync2.

The goal is to achieve something similar to sending private messages on the blockchain using public keys and decoding them on the recipient side. It can be implemented into a private chat system or for publishing private data without leaving the connex system.

Similar to this example:
https://github.com/pubkey/eth-crypto/blob/3c26820796d6613ce0e70c47732601283ee129ea/tutorials/encrypted-message.md

I suggest to either modify the signing service with a new option (encrypt|decrypt) or add a new encryption service:

connex.vendor.encryption('decrypt', {
    purpose: 'access to message reason',
    payload: {
        type: 'text',
        content: 'encrypted string'
    }
})
.request()
.then(decodedResult=>{
    console.log(decodedResult)
})

In the end the following snippet should work:

const encodedString = await connex.vendor.encryption('encrypt', {
    purpose: 'encrypt private message',
    payload: {
        publicKey: 'publicKey',
        message: 'a message'
    }
})
.request()

// user is presented with confirmation dialog to confirm encryption

const decodedString = await connex.vendor.encryption('decrypt', {
    purpose: 'decrypt private message',
    payload: {
        encrypted: encodedString
    }
})
.request()

// user is presented with confirmation dialog to confirm decryption

to support multiple payloads without calling encryption/decryption for each, payload should also support a list/array of objects:

const encodedStrings = await connex.vendor.encryption('encrypt', {
    purpose: 'encrypt private message',
    payload: [
   {
        publicKey: 'publicKey#1',
        message: 'Hello World'
    },
   {
        publicKey: 'publicKey#2',
        message: 'Hello VeChain'
    }]
})
.request()

// encodedStrings is a list with both results

const decodedStrings = await connex.vendor.encryption('encrypt', {
    purpose: 'encrypt private message',
    payload: encodedStrings.map(encrypted => ({ encrypted }))
})
.request()

// decodedStrings is a list of the original messages