-
Notifications
You must be signed in to change notification settings - Fork 234
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(docs): Aztecnr notes docs (#7168)
Closes AztecProtocol/dev-rel#196
- Loading branch information
Showing
13 changed files
with
182 additions
and
8 deletions.
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
docs/docs/guides/smart_contracts/writing_contracts/notes/address_note.md
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,42 @@ | ||
--- | ||
title: Using Address Note in Aztec.nr | ||
--- | ||
|
||
Address notes hold one main property of the type `AztecAddress`. It also holds `npk_m_hash` and `randomness`, similar to other note types. | ||
|
||
## AddressNote | ||
|
||
This is the AddressNote struct: | ||
|
||
#include_code address_note_struct noir-projects/aztec-nr/address-note/src/address_note.nr rust | ||
|
||
## Importing AddressNote | ||
|
||
### In Nargo.toml | ||
|
||
```toml | ||
address_note = { git="https://github.com/AztecProtocol/aztec-packages/", tag="#include_aztec_version", directory="noir-projects/aztec-nr/address-note" } | ||
``` | ||
|
||
### In your contract | ||
|
||
#include_code addressnote_import noir-projects/noir-contracts/contracts/escrow_contract/src/main.nr rust | ||
|
||
## Working with AddressNote | ||
|
||
### Creating a new note | ||
|
||
Creating a new `AddressNote` takes the following args: | ||
|
||
- `address` (`AztecAddress`): the address to store in the AddressNote | ||
- `npk_m_hash` (`Field`): the master nullifier public key hash of the user | ||
|
||
#include_code addressnote_new noir-projects/noir-contracts/contracts/escrow_contract/src/main.nr rust | ||
|
||
In this example, `owner` is the `address` and the `npk_m_hash` of the donor was computed earlier. | ||
|
||
## Learn more | ||
|
||
- [Keys, including nullifier keys and outgoing viewer](../../../../aztec/concepts/accounts/keys.md) | ||
- [How to write a custom note](./custom_note.md) | ||
- [AddressNote reference](https://docs.aztec.network/reference/smart_contract_reference/aztec-nr/address-note/address_note) |
45 changes: 45 additions & 0 deletions
45
docs/docs/guides/smart_contracts/writing_contracts/notes/custom_note.md
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,45 @@ | ||
--- | ||
title: Using custom note types in Aztec.nr | ||
--- | ||
|
||
It may be useful to write a custom note type if you want to use a specific type of private data or struct that does not have a default implementation in Aztec.nr. If you create a note that uses a custom note type, you are able to nullify that note with one nullifier. This is more secure and less expensive than using multiple separate notes. | ||
|
||
As an example, if you are developing a card game, you may want to store multiple pieces of data in each card. Rather than storing each of these pieces of data in their own note, you can use a custom note to define the card, and then nullify (or exchange ownership of) the card when it has been used. | ||
|
||
If you want to work with values or addresses, you can check out [ValueNote](./value_note.md) or [AddressNote](./address_note.md). | ||
|
||
## Define a custom note type | ||
|
||
You will likely want to define your note in a new file and import it into your contract. | ||
|
||
A custom note type can be defined with the macro `#[aztec(note)]` used on a struct: | ||
|
||
#include_code state_vars-CardNote noir-projects/noir-contracts/contracts/docs_example_contract/src/types/card_note.nr rust | ||
|
||
In this example, we are implementing a card note that holds a number of points as `u8`. | ||
|
||
## Implement NoteInterface | ||
|
||
You will need to implement a note interface for your note. Most of this is automatically generated by the `#[aztec(note)]` macro but can be overwritten. For your reference, this is what a note interface looks like: | ||
|
||
#include_code note_interface noir-projects/aztec-nr/aztec/src/note/note_interface.nr rust | ||
|
||
You will need to implement the functions `compute_note_hash_and_nullifier()` and `compute_note_hash_and_nullifier_without_context()` which tells Aztec how to compute nullifiers for your note. | ||
|
||
#include_code note_interface noir-projects/noir-contracts/contracts/docs_example_contract/src/types/card_note.nr rust | ||
|
||
In this example, these functions compute the note hash by using `compute_note_hash_for_consumption` and then generate a nullifier by hashing this note hash together with a secret. | ||
|
||
### Methods | ||
|
||
You will likely want to implement methods in order to use your note easily within contracts. For example, this may be what a `new` method can look like, for creating a new note: | ||
|
||
#include_code cardnote_impl noir-projects/noir-contracts/contracts/docs_example_contract/src/types/card_note.nr rust | ||
|
||
If you are also planning to be able to access the data with a note in public state, you will need to implement a method for serializing the note. This might look something like this: | ||
|
||
#include_code serialize noir-projects/noir-contracts/contracts/docs_example_contract/src/types/card_note.nr rust | ||
|
||
## Further reading | ||
|
||
- [Macros reference](../../../../reference/smart_contract_reference/macros.md) |
6 changes: 6 additions & 0 deletions
6
docs/docs/guides/smart_contracts/writing_contracts/notes/index.md
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,6 @@ | ||
--- | ||
title: Notes | ||
sidebar_position: 6 | ||
--- | ||
|
||
Notes are the fundamental data structure in Aztec when working with private state. In this section there are guides about how to work with `AddressNote`, `ValueNote`, and custom notes in Aztec.nr. You can learn more about notes in the [concepts section](../../../../aztec/concepts/state_model/index.md). |
64 changes: 64 additions & 0 deletions
64
docs/docs/guides/smart_contracts/writing_contracts/notes/value_note.md
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,64 @@ | ||
--- | ||
title: Using Value Notes in Aztec.nr | ||
--- | ||
|
||
ValueNotes hold one main property - a `value` - and have utils useful for manipulating this value, such as incrementing and decrementing it similarly to an integer. | ||
|
||
## ValueNote | ||
|
||
This is the ValueNote struct: | ||
|
||
#include_code value-note-def noir-projects/aztec-nr/value-note/src/value_note.nr rust | ||
|
||
## Importing ValueNote | ||
|
||
### In Nargo.toml | ||
|
||
```toml | ||
value_note = { git="https://github.com/AztecProtocol/aztec-packages/", tag="#include_aztec_version", directory="noir-projects/aztec-nr/value-note" } | ||
``` | ||
|
||
### In your contract | ||
|
||
#include_code import_valuenote noir-projects/noir-contracts/contracts/crowdfunding_contract/src/main.nr rust | ||
|
||
## Working with ValueNote | ||
|
||
### Creating a new note | ||
|
||
Creating a new `ValueNote` takes the following args: | ||
|
||
- `value` (`Field`): the value of the ValueNote | ||
- `npk_m_hash` (`Field`): the master nullifier public key hash of the user | ||
|
||
#include_code valuenote_new noir-projects/noir-contracts/contracts/crowdfunding_contract/src/main.nr rust | ||
|
||
In this example, `amount` is the `value` and the `npk_m_hash` of the donor was computed earlier. | ||
|
||
### Getting a balance | ||
|
||
A user may have multiple notes in a set that all refer to the same content (e.g. a set of notes representing a single token balance). By using the `ValueNote` type to represent token balances, you do not have to manually add each of these notes and can instead use a helper function `get_balance()`. | ||
|
||
It takes one argument - the set of notes. | ||
|
||
#include_code get_balance noir-projects/noir-contracts/contracts/stateful_test_contract/src/main.nr rust | ||
|
||
This can only be used in an unconstrained function. | ||
|
||
### Incrementing and decrementing | ||
|
||
Both `increment` and `decrement` functions take the same args: | ||
|
||
#include_code increment_args noir-projects/aztec-nr/value-note/src/utils.nr rust | ||
|
||
Note that this will create a new note in the set of notes passed as the first argument. | ||
For example: | ||
#include_code increment_valuenote noir-projects/noir-contracts/contracts/benchmarking_contract/src/main.nr rust | ||
|
||
The `decrement` function works similarly except the `amount` is the number that the value will be decremented by, and it will fail if the sum of the selected notes is less than the amount. | ||
|
||
## Learn more | ||
|
||
- [Keys, including nullifier keys and outgoing viewer](../../../../aztec/concepts/accounts/keys.md) | ||
- [How to write a custom note](./custom_note.md) | ||
- [ValueNote reference](https://docs.aztec.network/reference/smart_contract_reference/aztec-nr/value-note/value_note) |
2 changes: 1 addition & 1 deletion
2
docs/docs/guides/smart_contracts/writing_contracts/portals/index.md
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,6 +1,6 @@ | ||
--- | ||
title: Portals | ||
sidebar_position: 6 | ||
sidebar_position: 7 | ||
--- | ||
|
||
A portal is a point of contact between L1 and a contract on Aztec. For applications such as token bridges, this is the point where the tokens are held on L1 while used in L2. Note, that a portal doesn't actually need to be a contract, it could be any address on L1. |
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
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