Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: CompleteAddress type #1524

Merged
merged 38 commits into from
Aug 15, 2023
Merged
Show file tree
Hide file tree
Changes from 32 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
9044f69
fix: url pointing to deleted branch
benesjan Aug 11, 2023
1592ff3
fea: CompleteAddress type
benesjan Aug 11, 2023
5d22d82
WIP
benesjan Aug 11, 2023
640f6ef
WIP
benesjan Aug 11, 2023
1934bcc
refactor: better naming
benesjan Aug 11, 2023
06bb0f9
test: fix
benesjan Aug 11, 2023
12494c1
fixes after rebase
benesjan Aug 11, 2023
55e0003
feat: validity check inside CompleteAddress
benesjan Aug 11, 2023
776fa44
style: broken formatting from master
benesjan Aug 11, 2023
821e249
fix
benesjan Aug 11, 2023
b2cd35a
better command names
benesjan Aug 11, 2023
839bdd3
fix: linter
benesjan Aug 11, 2023
759c3c8
fixes after rebase
benesjan Aug 14, 2023
93c8b08
fix: bug in addAccount
benesjan Aug 14, 2023
10b8bd5
refactor: fixed stale naming
benesjan Aug 14, 2023
84b683e
feat: CompleteAddress.equal
benesjan Aug 14, 2023
428576b
docs: updated sandbox example
benesjan Aug 14, 2023
0c48125
fix: adding accounts to RPC server which I've totally messed up
benesjan Aug 14, 2023
c899f1c
docs: updated test comments
benesjan Aug 14, 2023
f4c2555
refactor: improved names in AztecRPC
benesjan Aug 14, 2023
38c5a73
docs: fixed aztec-cli docs
benesjan Aug 14, 2023
2c483e0
fix: correctly setting a recipient
benesjan Aug 14, 2023
9577cc7
test: fixed archiver e2e test
benesjan Aug 14, 2023
42979ab
feat: CompleteAddress serialization
benesjan Aug 14, 2023
8fa994b
fix: incorrectly serialized public key
benesjan Aug 14, 2023
c11055a
fix
benesjan Aug 14, 2023
fb3f670
fix
benesjan Aug 15, 2023
3345859
fix
benesjan Aug 15, 2023
86adc12
docs: documenting e2e_aztec_js_browser test
benesjan Aug 15, 2023
e1effe8
test: getting rid of unexpected test dependency on previous test run
benesjan Aug 15, 2023
d15bfcf
final interface refactor
benesjan Aug 15, 2023
a16edea
final touches
benesjan Aug 15, 2023
fe8e941
test cleanup
benesjan Aug 15, 2023
4facdda
Merge branch 'master' into janb/complete-address-type
benesjan Aug 15, 2023
c7162b9
fix
benesjan Aug 15, 2023
e3723b8
refactor: better naming
benesjan Aug 15, 2023
07bc8a0
test: fix
benesjan Aug 15, 2023
5c2bfb4
test fix
benesjan Aug 15, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion circuits/cpp/src/aztec3/circuits/abis/c_bind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ WASM_EXPORT void abis__compute_contract_address_from_partial(uint8_t const* poin
* @param constructor_hash_buf the hash of the contract constructor's verification key
* @param output buffer that will contain the output. The serialized contract address.
* See the link bellow for more details:
* https://github.com/AztecProtocol/aztec-packages/blob/janb/rpc-interface-cleanup/docs/docs/concepts/foundation/accounts/keys.md#addresses-partial-addresses-and-public-keys
* https://github.com/AztecProtocol/aztec-packages/blob/master/docs/docs/concepts/foundation/accounts/keys.md#addresses-partial-addresses-and-public-keys
*/
WASM_EXPORT void abis__compute_partial_address(uint8_t const* contract_address_salt_buf,
uint8_t const* function_tree_root_buf,
Expand Down
78 changes: 27 additions & 51 deletions docs/src/code_examples/sandbox_example.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// docs:start:index
import { PrivateTokenContract } from "@aztec/noir-contracts/types";
import {
AztecRPC,
L2BlockL2Logs,
Expand All @@ -8,41 +7,39 @@ import {
createDebugLogger,
getSchnorrAccount,
mustSucceedFetch,
} from "@aztec/aztec.js";
} from '@aztec/aztec.js';
import { PrivateTokenContract } from '@aztec/noir-contracts/types';

////////////// CREATE THE CLIENT INTERFACE AND CONTACT THE SANDBOX //////////////
const logger = createDebugLogger("private-token");
const sandboxUrl = "http://localhost:8080";
const logger = createDebugLogger('private-token');
const sandboxUrl = 'http://localhost:8080';

const aztecRpc = createAztecRpcClient(sandboxUrl, mustSucceedFetch);

const nodeInfo = await aztecRpc.getNodeInfo();

logger("Aztec Sandbox Info ", nodeInfo);
logger('Aztec Sandbox Info ', nodeInfo);
// docs:end:index

// docs:start:Accounts
////////////// CREATE SOME ACCOUNTS WITH SCHNORR SIGNERS //////////////
// Creates new accounts using an account contract that verifies schnorr signatures
// Returns once the deployment transactions have settled
const createSchnorrAccounts = async (
numAccounts: number,
aztecRpc: AztecRPC
) => {
const createSchnorrAccounts = async (numAccounts: number, aztecRpc: AztecRPC) => {
const accountManagers = Array(numAccounts)
.fill(0)
.map((x) =>
.map(x =>
getSchnorrAccount(
aztecRpc,
PrivateKey.random(), // encryption private key
PrivateKey.random() // signing private key
)
PrivateKey.random(), // signing private key
),
);
return await Promise.all(
accountManagers.map(async (x) => {
accountManagers.map(async x => {
await x.waitDeploy({});
return x;
})
}),
);
};

Expand All @@ -52,17 +49,15 @@ const accounts = await createSchnorrAccounts(2, aztecRpc);

////////////// VERIFY THE ACCOUNTS WERE CREATED SUCCESSFULLY //////////////

const [alice, bob] = (
await Promise.all(accounts.map((x) => x.getCompleteAddress()))
).map((x) => x.address);
const [alice, bob] = (await Promise.all(accounts.map(x => x.getCompleteAddress()))).map(x => x.address);

// Verify that the accounts were deployed
const registeredAccounts = await aztecRpc.getAccounts();
const registeredAccounts = (await aztecRpc.getAccounts()).map(x => x.address);
for (const [account, name] of [
[alice, "Alice"],
[bob, "Bob"],
[alice, 'Alice'],
[bob, 'Bob'],
] as const) {
if (registeredAccounts.find((acc) => acc.equals(account))) {
if (registeredAccounts.find(acc => acc.equals(account))) {
logger(`Created ${name}'s account at ${account.toShortString()}`);
continue;
}
Expand All @@ -76,23 +71,19 @@ for (const [account, name] of [
// Deploy a private token contract, create a contract abstraction object and link it to the owner's wallet
// The contract's constructor takes 2 arguments, the initial supply and the owner of that initial supply
const initialSupply = 1_000_000;
logger(
`Deploying private token contract minting an initial ${initialSupply} tokens to Alice...`
);
logger(`Deploying private token contract minting an initial ${initialSupply} tokens to Alice...`);
const tokenContractTx = PrivateTokenContract.deploy(
aztecRpc,
initialSupply, // the initial supply
alice // the owner of the initial supply
alice, // the owner of the initial supply
).send();
// wait for the tx to settle
await tokenContractTx.isMined();
const receipt = await tokenContractTx.getReceipt();
logger(`Transaction status is ${receipt.status}`);
const contractInfo = await aztecRpc.getContractInfo(receipt.contractAddress!);
if (contractInfo) {
logger(
`Contract successfully deployed at address ${receipt.contractAddress!.toShortString()}`
);
const contractData = await aztecRpc.getContractData(receipt.contractAddress!);
if (contractData) {
logger(`Contract successfully deployed at address ${receipt.contractAddress!.toShortString()}`);
}
// docs:end:Deployment
// docs:start:Logs
Expand All @@ -105,7 +96,7 @@ const viewUnencryptedLogs = async () => {
logger(`Retrieving unencrypted logs for block ${lastBlock}`);
const logs = await aztecRpc.getUnencryptedLogs(lastBlock, 1);
const unrolledLogs = L2BlockL2Logs.unrollLogs(logs);
const asciiLogs = unrolledLogs.map((log) => log.toString("ascii"));
const asciiLogs = unrolledLogs.map(log => log.toString('ascii'));
logger(`Emitted logs: `, asciiLogs);
};
await viewUnencryptedLogs();
Expand All @@ -116,28 +107,16 @@ await viewUnencryptedLogs();
////////////// QUERYING THE TOKEN BALANCE FOR EACH ACCOUNT //////////////

// Create the contract abstraction and link to Alice's wallet for future signing
const tokenContractAlice = await PrivateTokenContract.create(
receipt.contractAddress!,
await accounts[0].getWallet()
);
const tokenContractAlice = await PrivateTokenContract.create(receipt.contractAddress!, await accounts[0].getWallet());

// Bob wants to mint some funds, the contract is already deployed, create an abstraction and link it his wallet
const tokenContractBob = await PrivateTokenContract.create(
receipt.contractAddress!,
await accounts[1].getWallet()
);
const tokenContractBob = await PrivateTokenContract.create(receipt.contractAddress!, await accounts[1].getWallet());

const checkBalances = async () => {
// Check Alice's balance
logger(
`Alice's balance ${await tokenContractAlice.methods
.getBalance(alice)
.view()}`
);
logger(`Alice's balance ${await tokenContractAlice.methods.getBalance(alice).view()}`);
// Check Bob's balance
logger(
`Bob's balance ${await tokenContractBob.methods.getBalance(bob).view()}`
);
logger(`Bob's balance ${await tokenContractBob.methods.getBalance(bob).view()}`);
};
// Check the initial balances
await checkBalances();
Expand All @@ -148,10 +127,7 @@ await checkBalances();
// We will now transfer tokens from ALice to Bob
const transferQuantity = 543;
logger(`Transferring ${transferQuantity} tokens from Alice to Bob...`);
await tokenContractAlice.methods
.transfer(transferQuantity, alice, bob)
.send()
.wait();
await tokenContractAlice.methods.transfer(transferQuantity, alice, bob).send().wait();

// See if any logs were emitted
await viewUnencryptedLogs();
Expand Down
8 changes: 4 additions & 4 deletions yarn-project/acir-simulator/src/client/db_oracle.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PartialAddress, PrivateKey, PublicKey } from '@aztec/circuits.js';
import { CompleteAddress, PrivateKey, PublicKey } from '@aztec/circuits.js';
import { FunctionAbi } from '@aztec/foundation/abi';
import { AztecAddress } from '@aztec/foundation/aztec-address';
import { EthAddress } from '@aztec/foundation/eth-address';
Expand Down Expand Up @@ -72,11 +72,11 @@ export interface CommitmentDataOracleInputs {
*/
export interface DBOracle extends CommitmentsDB {
/**
* Retrieve the public key associated to a given address.
* Retrieve the complete address associated to a given address.
* @param address - Address to fetch the pubkey for.
* @returns A public key and the corresponding partial address, such that the hash of the two resolves to the input address.
* @returns A complete address associated with the input address.
*/
getPublicKey(address: AztecAddress): Promise<[PublicKey, PartialAddress]>;
getCompleteAddress(address: AztecAddress): Promise<CompleteAddress>;

/**
* Retrieve the secret key associated with a specific public key.
Expand Down
88 changes: 35 additions & 53 deletions yarn-project/acir-simulator/src/client/private_execution.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
CallContext,
CircuitsWasm,
CompleteAddress,
ConstantHistoricBlockData,
ContractDeploymentData,
FieldsOf,
Expand All @@ -15,19 +16,18 @@ import {
import {
computeCallStackItemHash,
computeCommitmentNonce,
computeContractAddressFromPartial,
computeSecretMessageHash,
computeUniqueCommitment,
siloCommitment,
} from '@aztec/circuits.js/abis';
import { pedersenPlookupCommitInputs } from '@aztec/circuits.js/barretenberg';
import { makeAddressWithPreimagesFromPrivateKey, makeContractDeploymentData } from '@aztec/circuits.js/factories';
import { makeContractDeploymentData } from '@aztec/circuits.js/factories';
import { FunctionAbi, encodeArguments, generateFunctionSelector } from '@aztec/foundation/abi';
import { asyncMap } from '@aztec/foundation/async-map';
import { AztecAddress } from '@aztec/foundation/aztec-address';
import { toBufferBE } from '@aztec/foundation/bigint-buffer';
import { EthAddress } from '@aztec/foundation/eth-address';
import { Fr, Point } from '@aztec/foundation/fields';
import { Fr } from '@aztec/foundation/fields';
import { DebugLogger, createDebugLogger } from '@aztec/foundation/log';
import { AppendOnlyTree, Pedersen, StandardTree, newTree } from '@aztec/merkle-tree';
import {
Expand Down Expand Up @@ -193,24 +193,15 @@ describe('Private Execution test suite', () => {
};

beforeEach(async () => {
const {
address: ownerAddress,
partialAddress: ownerPartialAddress,
publicKey: ownerPubKey,
} = await makeAddressWithPreimagesFromPrivateKey(ownerPk);

const {
address: recipientAddress,
partialAddress: recipientPartialAddress,
publicKey: recipientPubKey,
} = await makeAddressWithPreimagesFromPrivateKey(recipientPk);

owner = ownerAddress;
recipient = recipientAddress;

oracle.getPublicKey.mockImplementation((address: AztecAddress) => {
if (address.equals(owner)) return Promise.resolve([ownerPubKey, ownerPartialAddress]);
if (address.equals(recipient)) return Promise.resolve([recipientPubKey, recipientPartialAddress]);
const ownerCompleteAddress = await CompleteAddress.fromPrivateKey(ownerPk);
const recipientCompleteAddress = await CompleteAddress.fromPrivateKey(recipientPk);

owner = ownerCompleteAddress.address;
recipient = recipientCompleteAddress.address;

oracle.getCompleteAddress.mockImplementation((address: AztecAddress) => {
if (address.equals(owner)) return Promise.resolve(ownerCompleteAddress);
if (address.equals(recipient)) return Promise.resolve(recipientCompleteAddress);
throw new Error(`Unknown address ${address}`);
});

Expand Down Expand Up @@ -432,24 +423,15 @@ describe('Private Execution test suite', () => {
};

beforeEach(async () => {
const {
address: ownerAddress,
partialAddress: ownerPartialAddress,
publicKey: ownerPubKey,
} = await makeAddressWithPreimagesFromPrivateKey(ownerPk);

const {
address: recipientAddress,
partialAddress: recipientPartialAddress,
publicKey: recipientPubKey,
} = await makeAddressWithPreimagesFromPrivateKey(recipientPk);

owner = ownerAddress;
recipient = recipientAddress;

oracle.getPublicKey.mockImplementation((address: AztecAddress) => {
if (address.equals(owner)) return Promise.resolve([ownerPubKey, ownerPartialAddress]);
if (address.equals(recipient)) return Promise.resolve([recipientPubKey, recipientPartialAddress]);
const ownerCompleteAddress = await CompleteAddress.fromPrivateKey(ownerPk);
const recipientCompleteAddress = await CompleteAddress.fromPrivateKey(recipientPk);

owner = ownerCompleteAddress.address;
recipient = recipientCompleteAddress.address;

oracle.getCompleteAddress.mockImplementation((address: AztecAddress) => {
if (address.equals(owner)) return Promise.resolve(ownerCompleteAddress);
if (address.equals(recipient)) return Promise.resolve(recipientCompleteAddress);
throw new Error(`Unknown address ${address}`);
});

Expand Down Expand Up @@ -648,10 +630,10 @@ describe('Private Execution test suite', () => {
let recipient: AztecAddress;

beforeEach(async () => {
const { address, partialAddress, publicKey } = await makeAddressWithPreimagesFromPrivateKey(recipientPk);
recipient = address;
oracle.getPublicKey.mockImplementation((address: AztecAddress) => {
if (address.equals(recipient)) return Promise.resolve([publicKey, partialAddress]);
const recipientCompleteAddress = await CompleteAddress.fromPrivateKey(recipientPk);
recipient = recipientCompleteAddress.address;
oracle.getCompleteAddress.mockImplementation((address: AztecAddress) => {
if (address.equals(recipient)) return Promise.resolve(recipientCompleteAddress);
throw new Error(`Unknown address ${address}`);
});
});
Expand Down Expand Up @@ -787,10 +769,12 @@ describe('Private Execution test suite', () => {
let owner: AztecAddress;

beforeEach(async () => {
const { address, partialAddress, publicKey } = await makeAddressWithPreimagesFromPrivateKey(ownerPk);
owner = address;
oracle.getPublicKey.mockImplementation((address: AztecAddress) => {
if (address.equals(owner)) return Promise.resolve([publicKey, partialAddress]);
const ownerCompleteAddress = await CompleteAddress.fromPrivateKey(ownerPk);

owner = ownerCompleteAddress.address;

oracle.getCompleteAddress.mockImplementation((address: AztecAddress) => {
if (address.equals(owner)) return Promise.resolve(ownerCompleteAddress);
throw new Error(`Unknown address ${address}`);
});
});
Expand Down Expand Up @@ -971,13 +955,11 @@ describe('Private Execution test suite', () => {
abi.returnTypes = [{ kind: 'field' }, { kind: 'field' }];

// Generate a partial address, pubkey, and resulting address
const partialAddress = Fr.random();
const pubKey = Point.random();
const wasm = await CircuitsWasm.get();
const address = computeContractAddressFromPartial(wasm, pubKey, partialAddress);
const args = [address];
const completeAddress = await CompleteAddress.random();
const args = [completeAddress.address];
const pubKey = completeAddress.publicKey;

oracle.getPublicKey.mockResolvedValue([pubKey, partialAddress]);
oracle.getCompleteAddress.mockResolvedValue(completeAddress);
const result = await runSimulator({ origin: AztecAddress.random(), abi, args });
expect(result.returnValues).toEqual([pubKey.x.value, pubKey.y.value]);
});
Expand Down
4 changes: 2 additions & 2 deletions yarn-project/acir-simulator/src/client/private_execution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ export class PrivateFunctionExecution {
getSecretKey: ([ownerX], [ownerY]) => this.context.getSecretKey(this.contractAddress, ownerX, ownerY),
getPublicKey: async ([acvmAddress]) => {
const address = frToAztecAddress(fromACVMField(acvmAddress));
const [pubKey, partialAddress] = await this.context.db.getPublicKey(address);
return [pubKey.x, pubKey.y, partialAddress].map(toACVMField);
const { publicKey, partialAddress } = await this.context.db.getCompleteAddress(address);
return [publicKey.x, publicKey.y, partialAddress].map(toACVMField);
},
getNotes: ([slot], sortBy, sortOrder, [limit], [offset], [returnSize]) =>
this.context.getNotes(this.contractAddress, slot, sortBy, sortOrder, +limit, +offset, +returnSize),
Expand Down
Loading