Resolving a Transaction's Encrypted Outputs

This guide assumes you have already deployed the Counter.sol contract. If you haven't already, check out our guide for deploying a Basic Private Smart Contract.

After calling add, we can use our AES encryption key to decrypt the value of sum and check the result.

Setup

npm install @coti-io/coti-ethers

Code

import { Contract, CotiNetwork, getDefaultProvider, Wallet } from "@coti-io/coti-ethers"

const PRIVATE_KEY = "<EOA_PRIVATE_KEY>"
const AES_KEY = "<AES_KEY>"
const COUNTER_ADDRESS = "<COUNTER_ADDRESS>"
const COUNTER_ABI = [
    {
        "inputs": [],
        "stateMutability": "nonpayable",
        "type": "constructor"
    },
    {
        "inputs": [
            {
                "components": [
                    {
                        "internalType": "ctUint64",
                        "name": "ciphertext",
                        "type": "uint256"
                    },
                    {
                        "internalType": "bytes",
                        "name": "signature",
                        "type": "bytes"
                    }
                ],
                "internalType": "struct itUint64",
                "name": "value",
                "type": "tuple"
            }
        ],
        "name": "add",
        "outputs": [],
        "stateMutability": "nonpayable",
        "type": "function"
    },
    {
        "inputs": [],
        "name": "sum",
        "outputs": [
            {
                "internalType": "ctUint64",
                "name": "",
                "type": "uint256"
            }
        ],
        "stateMutability": "view",
        "type": "function"
    }
]

const provider = getDefaultProvider(CotiNetwork.Testnet)
const wallet = new Wallet(PRIVATE_KEY, provider)
wallet.setAesKey(AES_KEY)

const counter = new Contract(COUNTER_ADDRESS, COUNTER_ABI, wallet)

const ctSum = await counter.sum()

const clearSum = await wallet.decryptValue(ctSum)

console.log(clearSum)

Last updated