Decryption of Provably Encrypted Data

With mishtiwasm

Install.

npm i @holonym-foundation/mishtiwasm

Import, fetch parameters, and decrypt.

import wasm from '@holonym-foundation/mishtiwasm'

// -------- Fetch parameters --------
// To decrypt, Mishti needs to verify the user's signature of the
// access conditions contract address. We retrieve the ciphertext
// as well as the signature and conditions contract from the observer.

const user = '0x123'
// This example uses Holonym's default observer to store ciphertext and assist in decryption
const observerUrl = 'https://observer.holonym.io'

const resp = await fetch(`${observerUrl}/observations?user_address=${user}`)
const data = await resp.json()
const { 
  user_address,
  signature,
  access_contract,
  zkp_public_values,
} = data[0]

// C1 and C2 are the two points in the ElGamal Ciphertext: the ephemeral public key and the masked plaintext, respectively
const ciphertext = {
  c1: {
    x: BigInt('0x' + zkp_public_values[7]).toString(),
    y: BigInt('0x' + zkp_public_values[8]).toString()
  },
  c2: {
    x: BigInt('0x' + zkp_public_values[9]).toString(),
    y: BigInt('0x' + zkp_public_values[10]).toString()
  }
}

// -------- Decrypt --------

// privateKey should be the private key to your wallet with Mishti credits.
// It is used to sign the request to Mishti Network.
const privateKey = '0x123'

const result = await wasm.decrypt(
  privateKey,
  JSON.stringify(ciphertext),
  access_contract,
  signature
);

// If the decrypted value is a UTF-8 encoded string, we need to decode
// by doing something like this:
const decoded = Buffer.from(BigInt(result).toString(16), 'hex').toString()

Last updated