Skip to content

Instantly share code, notes, and snippets.

@samtay
Created September 19, 2023 18:23
Show Gist options
  • Save samtay/20676ee15c633ed0c89a6e2cae5eb3f8 to your computer and use it in GitHub Desktop.
Save samtay/20676ee15c633ed0c89a6e2cae5eb3f8 to your computer and use it in GitHub Desktop.
Parasol - Network Key - Private Tx Example
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