You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Migrating to Ten is a straight-forward process that immediately unlocks private state.
7
-
There are three main types of changes you need to make:
6
+
Migrating to Ten is a straightforward process that immediately unlocks "Programmable Encryption".
8
7
9
-
1. Change your hardhat deployment script so that you can use --network ten
8
+
There are a couple of changes you need to make:
9
+
10
+
1. Change your hardhat deployment script so that you can use `--network ten`.
10
11
2. Add logic to your view functions to protect data (if needed).
11
-
3. Add a widget to your javascript UI to onboard Ten users.
12
+
3. Configure event log visibility (if needed).
13
+
4. Add a widget to your javascript UI to onboard Ten users.
12
14
13
15
## 1. Configuring Hardhat
14
16
15
-
To begin building on Ten, start by setting up a Hardhat project as usual.
17
+
To begin building on Ten, you can start by setting up a Hardhat project as usual.
16
18
17
19
### 1.1 Installing the Ten Hardhat Plugin
18
20
@@ -22,9 +24,9 @@ To integrate the Ten Network into your Hardhat project, install the ten-hardhat-
22
24
npm install ten-hardhat-plugin
23
25
```
24
26
25
-
You can extend the functionality of Hardhat by installing plugins. Install them using npm or Yarn & configure it in the next step.
27
+
Note: Plugins can be installed using `npm` or `yarn`.
26
28
27
-
### 1.2 Configuring `hardhat.config.js`
29
+
### 1.2 Configuring `hardhat.config.js` for the Ten Testnet
28
30
29
31
Open `hardhat.config.js` in your project's root directory and configure it in the following way:
30
32
@@ -49,19 +51,20 @@ module.exports = {
49
51
50
52
exportdefaultconfig;
51
53
```
52
-
Now, start writing the smart contracts for your project.
54
+
Now, you can start writing or migrating the smart contracts.
53
55
54
56
# 2. Writing Smart Contracts
55
57
56
58
Ten performs bytecode execution in the EVM identically to Ethereum, allowing developers to leverage their existing codebase and tools.
57
59
58
-
The main difference and advantage of Ten is that on Ten, during execution, private variables and the internal state of the contract are hidden from everyone, including node operators and the sequencer.
60
+
The main difference is that, during execution, private variables and the internal state of the contract are hidden from everyone, including node operators and the sequencer.
61
+
This is a major advantage that represents "Programmable Privacy".
59
62
60
63
:::info
61
64
In Ten, the internal node database is encrypted, and the execution itself is also encrypted inside the TEE.
62
65
:::
63
66
64
-
The calls to [getStorageAt](https://docs.alchemy.com/reference/eth-getstorageat) are disabled, so all data access will be performed through view functions which are under the control of the smart contract developer. Public variables are accessible to everyone because Solidity automatically generates a getter function for them.
67
+
The calls to [getStorageAt](https://docs.alchemy.com/reference/eth-getstorageat) are disabled by default, so all data access will be performed through view functions which are under the control of the smart contract developer. Note that public variables are accessible to everyone because Solidity automatically generates a getter function for them.
65
68
66
69
We'll illustrate how this works by creating a simple data storage example. In this dApp, users can store a number and retrieve it later.
67
70
@@ -114,7 +117,7 @@ In Ethereum, the `_storedValues` variable can also be accessed directly using th
114
117
115
118
## Step 3: Implementing Data Access Control
116
119
117
-
In this step, our aim is to restrict users to only access their own value. This feature can only be implemented in Ten because as mentioned above, `_storedValues` is not hidden in Ethereum.
120
+
In this step, we aim to restrict users to only access their own value. This feature can only be implemented in Ten because as mentioned above, `_storedValues` is not hidden in Ethereum.
118
121
119
122
### Code:
120
123
@@ -141,9 +144,9 @@ Since `getValue` is the only function which exposes the values, we can add a che
141
144
In Ethereum, since all data is accessible anyway, there is no need to sign calls to view functions, so `tx.origin` can be spoofed.
142
145
:::
143
146
144
-
In Ten, the platform ensures that calls to view functions are authenticated. Which means that behind the scenes, there is a signature of the `tx.origin` address.
147
+
In Ten, the platform ensures that calls to view functions are authenticated, which means that behind the scenes, there is a "Viewing Key" signature of the `tx.origin` address.
145
148
146
-
## Step 4: Emitting Events
149
+
## Step 4: Emitting Events - Default visibility
147
150
148
151
Events in Ethereum are crucial for UIs to react to smart contract state changes. In this step, we'll emit an event when a user stores a value. We'll also gauge the popularity of our contract by emitting an event when certain milestones are reached.
149
152
@@ -175,15 +178,105 @@ contract StorageExample {
175
178
176
179
### Explanation:
177
180
178
-
On Ethereum, events are visible to anyone. For example, you can subscribe to the `DataChanged` event and receive notifications in real-time about the data of everyone else. In Ten, we wanted to do better than that.
181
+
On Ethereum, events are visible to anyone. For example, you can subscribe to the `DataChanged` event and receive notifications in real time about the data of everyone else.
182
+
183
+
The programmable encryption of Ten allows you full control over visibility but also has sensible defaults.
184
+
Event logs can be queried using `eth_getLogs` or subscribed to using the `logs` endpoint. Both these calls are authenticated, and the platform makes sure to return only visible logs.
185
+
186
+
In our case, the requirements are very simple and common sense:
179
187
180
188
- The `DataChanged` event is specific to an account, so it should only be received by that user.
181
189
-`MilestoneReached`, on the other hand, is intended for everyone, as we want to show how popular our contract is.
182
190
183
-
The behavior you desire is to restrict the visibility of `DataChanged`, but not that of `MilestoneReached`. **This is exactly how it works by default!**
191
+
The behaviour you desire is to restrict the visibility of `DataChanged`, but not that of `MilestoneReached`. **Which is exactly how it works by default!**
192
+
193
+
Default behaviour:
184
194
185
-
How it works:
186
-
-`DataChanged` - has an address as a topic (an indexed field), which makes it relevant to that address.
187
-
-`MilestoneReached` - has no address topic, so it is visible to everyone.
195
+
-`DataChanged` - has an address as a topic (an indexed field), which instructs the platform that the event log is only visible to that address.
196
+
-`MilestoneReached` - has no address topic which by default means it is visible to everyone.
188
197
189
198
All you have to do is emit events as usual, and the platform applies common-sense visibility rules.
0 commit comments