Skip to content

Commit

Permalink
Added a paragraph about metamask compatability
Browse files Browse the repository at this point in the history
  • Loading branch information
Cashmaney committed Feb 11, 2024
1 parent 9b220eb commit af03ab5
Showing 1 changed file with 52 additions and 2 deletions.
54 changes: 52 additions & 2 deletions docs/devdocs/Writing Smart Contracts/Returning-Data.md
Original file line number Diff line number Diff line change
Expand Up @@ -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": "<base64 bytes of a nonce used for encrypted>",
"ephemPublicKey": "<base64 bytes of the target public key>",
"ciphertext": "<base64 string of a big-endian number>"
}
```

### 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
Expand All @@ -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);
}
```

Expand All @@ -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)

0 comments on commit af03ab5

Please sign in to comment.