How to use encryption
Methods such as eth_estimateGas, eth_call, eth_sendTransaction should have encrypted data field. To do it, you can use https://www.npmjs.com/package/@swisstronik/utils
Example #1. Send encrypted transaction
Let's take a look on sample function that accepts ethers signer, destination address (address of contract or EOA), raw data field and value to be sent within transaction. This function will request node public key to perform ECDH and derive shared encryption key and then will encrypt provided data using DEOXYS-II algorithm
const { encryptDataField } = require('@swisstronik/utils')
// Encrypts data field and sends encrypted transaction
// * signer β ethers signer with connected provider
// * destination - destination address
// * data β raw (unencrypted) transaction `data` field
// * value β amount of aswtr to be sent within transaction
const sendShieldedTransaction = async (signer, destination, data, value) => {
// Encrypt transaction data. `encryptDataField` function also returns
// used encryption key to be able decrypt node response. It will be useful
// in case if you're sending some query (for example, request PERC20 balance)
const [encryptedData] = await encryptDataField(
signer.provider.connection.url,
data,
// encryptionKey β also you can provide encryption key by yourself
// if you want to be able to decrypt transaction payload in the future.
// Otherwise, it will generate random encryption key using `tweetnacl`
// library
)
// Construct and sign transaction with encrypted data. This transaction will
// be sent as a regular Ethereum transaction
return await signer.sendTransaction({
from: signer.address,
to: destination,
data: encryptedData,
value,
})
}Example #2. Send encrypted balanceOf request
Let's take a look on two functions which are used to obtain PERC20 token balance
Function getTokenBalance creates a signature from owner and calls sendSignedShieldedQuery to encrypt function data, sign the query, send the call and decrypt the response
Last updated