diff --git a/docs/devdocs/Writing Smart Contracts/Returning-Data.md b/docs/devdocs/Writing Smart Contracts/Returning-Data.md index c73db9fe..6333c2bb 100644 --- a/docs/devdocs/Writing Smart Contracts/Returning-Data.md +++ b/docs/devdocs/Writing Smart Contracts/Returning-Data.md @@ -23,6 +23,35 @@ There are two ways to return encrypted data to the user: Alternatively, Fhenix supports standard decryption as well. If some data needs to be decrypted for public access, that can be done as well and a plaintext value is returned to the caller. This can be done using the `FHE.decrypt` function. +## Sealed Data Format + +If you are using `fhenixjs`, you don't have to worry about parsing the raw sealed data that is returned from `sealoutput` or `seal`. + +However, developers have the option to unseal this data manually. As we described above, the data is encrypted using [sealed box encryption](https://bitbeans.gitbooks.io/libsodium-net/content/public-key\_cryptography/sealed\_boxes.html). + +The data itself is encoded in the following format: + +```json +{ + "version": "x25519-xsalsa20-poly1305", + "nonce": "", + "ephemPublicKey": "", + "ciphertext": "" +} +``` + +### Metamask Compatability + +The encryption schema and structure matches the one used by Metamask's [`eth_decrypt`](https://docs.metamask.io/wallet/reference/eth_decrypt/) function. This means that we can consume sealed data +directly from metamask, which can provide a more engaging experience for the user of a dApp - You can fetch an address's public key using the `eth_getencryptionpublickey` method, +and seal data for that specific public key (either as a permit or by using the public key directly), and then use Metamask's `eth_decrypt` call to provided a +guided decryption experience. + +:::danger[Warning] +Metamask's `eth_getencryptionpublickey` and `eth_decrypt` methods are deprecated. We provide this compatability to demonstrate compatability with native wallet encryption/decryption. +We aim to maintain compatability as new standards for Encryption on Ethereum emerge +::: + ## Examples ### Sealed Box Encryption @@ -32,7 +61,7 @@ import {FHE} from "@fhenixprotocol/contracts"; function sealoutputExample(bytes32 pubkey) public pure returns (bytes memory reencrypted) { euint8 memory foo = asEuint8(100); - return FHE.sealoutput(foo, pubkey); + return foo.seal(pubkey); } ``` @@ -47,4 +76,25 @@ function sealoutputExample() public pure returns (uint8 decrypted) { } ``` -You can see full examples of how to use these functions in our [examples and dApps](../Examples%20and%20References/Examples-fheDapps.md) section. +### Metamask Unsealing + +```Javascript +async getPub() { + const provider = new BrowserProvider(window.ethereum); + const client = new FhenixClient({provider}); + const accounts = await window.ethereum.request({ method: 'eth_requestAccounts' }); + const keyResult = await provider.send('eth_getEncryptionPublicKey',[accounts[0]]); + const pk = `0x${this.base64ToHex(keyResult)}`; + this.showNotification(pk); +} +async unseal() { + const provider = new BrowserProvider(window.ethereum); + const client = new FhenixClient({provider}); + const accounts = await window.ethereum.request({ method: 'eth_requestAccounts' }); + const result = await provider.send('eth_decrypt', [this.sealedInput, accounts[0]]); + const plaintext = this.toString(result); + this.showNotification(`Unsealed result: ${plaintext}`); +} +``` + +Taken from the [encryption & unsealing tool](https://github.com/FhenixProtocol/fhenix-enc-tool/blob/master/src/App.vue)