Created
September 19, 2023 18:23
-
-
Save samtay/20676ee15c633ed0c89a6e2cae5eb3f8 to your computer and use it in GitHub Desktop.
Parasol - Network Key - Private Tx Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import "sunscreen/src/FHE.sol"; | |
contract PrivateTxExample { | |
// Encrypted balances | |
mapping(address => bytes) balances; | |
constructor() { | |
// Instantiate our FHE subcontract | |
fhe = new FHE(); | |
} | |
// Register a new user | |
function register() public { | |
// Don't overwrite user's balance | |
require(balances[msg.sender].length == 0); | |
// Register the new user with a balance of zero | |
// Here we encrypt under the network key! | |
balances[msg.sender] = fhe.encryptUint256(fhe.networkPublicKey(), 0); | |
} | |
// Execute a private transfer | |
// Note: would typically require a ZKP proving amount <= sender's balance | |
function transfer( | |
address receiver, | |
bytes memory amount, | |
) public { | |
balances[msg.sender] = fhe.subtractUint256EncEnc(fhe.networkPublicKey(), balances[msg.sender], amount); | |
balances[receiver] = fhe.addUint256EncEnc(fhe.networkPublicKey(), balances[receiver], amount); | |
} | |
// Retrieve user's balance, encrypted under their own key | |
function balance(bytes memory publicKey) public view returns (bytes memory) { | |
return fhe.reencryptUint256(publicKey, balances[msg.sender]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment