-
Notifications
You must be signed in to change notification settings - Fork 304
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add deploy contract helper to aztec-nr (#4775)
Adds a helper function to the aztec-nr library to call the contract instance deployer from another contract. This required adding a new oracle call to get a full contract instance, as well as enshrining the deployer contract address as a constant. Built on #4497
- Loading branch information
1 parent
1cb6396
commit 6018fc6
Showing
25 changed files
with
340 additions
and
64 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
use crate::{ | ||
context::PrivateContext, | ||
oracle::get_contract_instance::get_contract_instance, | ||
}; | ||
|
||
use dep::protocol_types::{ | ||
address::AztecAddress, | ||
abis::function_selector::FunctionSelector, | ||
constants::DEPLOYER_CONTRACT_ADDRESS, | ||
}; | ||
|
||
// Calls `deploy` on the deployer contract to deploy a new instance. | ||
pub fn deploy_contract(context: &mut PrivateContext, target: AztecAddress) { | ||
let instance = get_contract_instance(target); | ||
|
||
let mut universal_deploy = false; | ||
if ! instance.deployer.is_zero() { | ||
assert(instance.deployer == context.this_address(), "Deployer address does not match current address"); | ||
universal_deploy = true; | ||
} | ||
|
||
// Adapted from noir-contracts/contracts/contract_instance_deployer_contract/src/interface/ContractInstanceDeployer.nr | ||
// That file was autogenerated running the following command from noir-projects/noir-contracts: | ||
// ../../yarn-project/node_modules/.bin/aztec-cli codegen target/contract_instance_deployer_contract-ContractInstanceDeployer.json --nr -o ./contracts/contract_instance_deployer_contract/src/interface | ||
let mut serialized_args = [0; 6]; | ||
serialized_args[0] = instance.salt; | ||
serialized_args[1] = instance.contract_class_id.to_field(); | ||
serialized_args[2] = instance.initialization_hash; | ||
serialized_args[3] = instance.portal_contract_address.to_field(); | ||
serialized_args[4] = instance.public_keys_hash.to_field(); | ||
serialized_args[5] = universal_deploy as Field; | ||
|
||
let _call_result = context.call_private_function( | ||
AztecAddress::from_field(DEPLOYER_CONTRACT_ADDRESS), | ||
FunctionSelector::from_field(0x883355ab), | ||
serialized_args | ||
); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
mod avm; | ||
mod context; | ||
mod deploy; | ||
mod hash; | ||
mod hasher; | ||
mod history; | ||
|
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
14 changes: 14 additions & 0 deletions
14
noir-projects/aztec-nr/aztec/src/oracle/get_contract_instance.nr
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
use dep::protocol_types::{address::AztecAddress, contract_instance::ContractInstance, constants::CONTRACT_INSTANCE_LENGTH}; | ||
|
||
#[oracle(getContractInstance)] | ||
fn get_contract_instance_oracle(_address: AztecAddress) -> [Field; CONTRACT_INSTANCE_LENGTH] {} | ||
|
||
unconstrained fn get_contract_instance_internal(address: AztecAddress) -> [Field; CONTRACT_INSTANCE_LENGTH] { | ||
get_contract_instance_oracle(address) | ||
} | ||
|
||
pub fn get_contract_instance(address: AztecAddress) -> ContractInstance { | ||
let instance = ContractInstance::deserialize(get_contract_instance_internal(address)); | ||
assert(instance.to_address().eq(address)); | ||
instance | ||
} |
65 changes: 65 additions & 0 deletions
65
...s/contracts/contract_instance_deployer_contract/src/interface/ContractInstanceDeployer.nr
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* Autogenerated file, do not edit! */ | ||
|
||
use dep::std; | ||
use dep::aztec::context::{PrivateContext, PublicContext}; | ||
use dep::aztec::protocol_types::{address::AztecAddress, abis::function_selector::FunctionSelector, constants::RETURN_VALUES_LENGTH}; | ||
|
||
struct ContractClassIdDeployStruct { | ||
inner: Field, | ||
} | ||
|
||
struct PortalContractAddressDeployStruct { | ||
inner: Field, | ||
} | ||
|
||
struct PublicKeysHashDeployStruct { | ||
inner: Field, | ||
} | ||
|
||
// Interface for calling ContractInstanceDeployer functions from a private context | ||
struct ContractInstanceDeployerPrivateContextInterface { | ||
address: AztecAddress, | ||
} | ||
|
||
impl ContractInstanceDeployerPrivateContextInterface { | ||
pub fn at(address: AztecAddress) -> Self { | ||
Self { address } | ||
} | ||
|
||
pub fn deploy( | ||
self, | ||
context: &mut PrivateContext, | ||
salt: Field, | ||
contract_class_id: ContractClassIdDeployStruct, | ||
initialization_hash: Field, | ||
portal_contract_address: PortalContractAddressDeployStruct, | ||
public_keys_hash: PublicKeysHashDeployStruct, | ||
universal_deploy: bool | ||
) -> [Field; RETURN_VALUES_LENGTH] { | ||
let mut serialized_args = [0; 6]; | ||
serialized_args[0] = salt; | ||
serialized_args[1] = contract_class_id.inner; | ||
serialized_args[2] = initialization_hash; | ||
serialized_args[3] = portal_contract_address.inner; | ||
serialized_args[4] = public_keys_hash.inner; | ||
serialized_args[5] = universal_deploy as Field; | ||
|
||
context.call_private_function( | ||
self.address, | ||
FunctionSelector::from_field(0x883355ab), | ||
serialized_args | ||
) | ||
} | ||
} | ||
|
||
// Interface for calling ContractInstanceDeployer functions from a public context | ||
struct ContractInstanceDeployerPublicContextInterface { | ||
address: AztecAddress, | ||
} | ||
|
||
impl ContractInstanceDeployerPublicContextInterface { | ||
pub fn at(address: AztecAddress) -> Self { | ||
Self { address } | ||
} | ||
} | ||
|
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
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
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
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
74 changes: 74 additions & 0 deletions
74
noir-projects/noir-protocol-circuits/src/crates/types/src/contract_instance.nr
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
use crate::{ | ||
address::{ | ||
aztec_address::AztecAddress, eth_address::EthAddress, partial_address::PartialAddress, | ||
public_keys_hash::PublicKeysHash | ||
}, | ||
contract_class_id::ContractClassId, | ||
constants::{GENERATOR_INDEX__CONTRACT_DEPLOYMENT_DATA, CONTRACT_INSTANCE_LENGTH}, | ||
traits::{Deserialize, Hash, Serialize} | ||
}; | ||
|
||
struct ContractInstance { | ||
salt : Field, | ||
deployer: AztecAddress, | ||
contract_class_id : ContractClassId, | ||
initialization_hash : Field, | ||
portal_contract_address : EthAddress, | ||
public_keys_hash : PublicKeysHash, | ||
} | ||
|
||
impl Eq for ContractInstance { | ||
fn eq(self, other: Self) -> bool { | ||
self.public_keys_hash.eq(other.public_keys_hash) & | ||
self.initialization_hash.eq(other.initialization_hash) & | ||
self.contract_class_id.eq(other.contract_class_id) & | ||
self.salt.eq(other.salt) & | ||
self.portal_contract_address.eq(other.portal_contract_address) | ||
} | ||
} | ||
|
||
impl Serialize<CONTRACT_INSTANCE_LENGTH> for ContractInstance { | ||
fn serialize(self) -> [Field; CONTRACT_INSTANCE_LENGTH] { | ||
[ | ||
self.salt, | ||
self.deployer.to_field(), | ||
self.contract_class_id.to_field(), | ||
self.initialization_hash, | ||
self.portal_contract_address.to_field(), | ||
self.public_keys_hash.to_field() | ||
] | ||
} | ||
} | ||
|
||
impl Deserialize<CONTRACT_INSTANCE_LENGTH> for ContractInstance { | ||
fn deserialize(serialized: [Field; CONTRACT_INSTANCE_LENGTH]) -> Self { | ||
Self { | ||
salt: serialized[0], | ||
deployer: AztecAddress::from_field(serialized[1]), | ||
contract_class_id: ContractClassId::from_field(serialized[2]), | ||
initialization_hash: serialized[3], | ||
portal_contract_address: EthAddress::from_field(serialized[4]), | ||
public_keys_hash: PublicKeysHash::from_field(serialized[5]), | ||
} | ||
} | ||
} | ||
|
||
impl Hash for ContractInstance { | ||
fn hash(self) -> Field { | ||
self.to_address().to_field() | ||
} | ||
} | ||
|
||
impl ContractInstance { | ||
fn to_address(self) -> AztecAddress { | ||
AztecAddress::compute( | ||
self.public_keys_hash, | ||
PartialAddress::compute( | ||
self.contract_class_id, | ||
self.salt, | ||
self.initialization_hash, | ||
self.portal_contract_address | ||
) | ||
) | ||
} | ||
} |
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
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
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
Oops, something went wrong.