3
3
import os
4
4
import sys
5
5
6
- sys .path .append (os .path .abspath (os .path .join (os .path .dirname (__file__ ), "../.. " )))
6
+ sys .path .append (os .path .abspath (os .path .join (os .path .dirname (__file__ ), ".." )))
7
7
8
8
import asyncio
9
9
10
10
import numpy as np
11
11
import py_nillion_client as nillion
12
+ from common .utils import compute , store_program , store_secret_array
13
+ from config import DIM
14
+ from cosmpy .aerial .client import LedgerClient
15
+ from cosmpy .aerial .wallet import LocalWallet
16
+ from cosmpy .crypto .keypairs import PrivateKey
12
17
from dotenv import load_dotenv
13
- from nillion_python_helpers import (create_nillion_client , getNodeKeyFromFile ,
14
- getUserKeyFromFile )
18
+ from nillion_python_helpers import (create_nillion_client ,
19
+ create_payments_config , get_quote ,
20
+ get_quote_and_pay , pay_with_quote )
21
+ from py_nillion_client import NodeKey , UserKey
15
22
16
23
import nada_numpy .client as na_client
17
- # Import helper functions for creating nillion client and getting keys
18
- from examples .broadcasting .config import DIM
19
- from examples .common .utils import compute , store_program , store_secret_array
20
24
21
- # Load environment variables from a .env file
22
- load_dotenv ()
25
+ home = os . getenv ( "HOME" )
26
+ load_dotenv (f"/workspaces/ai/.nillion-testnet.env" )
23
27
24
28
25
29
# Main asynchronous function to coordinate the process
26
30
async def main () -> None :
27
31
"""Main nada program"""
28
32
29
- print (f"USING: { DIM } dims" )
30
-
31
33
cluster_id = os .getenv ("NILLION_CLUSTER_ID" )
32
- userkey = getUserKeyFromFile (os .getenv ("NILLION_USERKEY_PATH_PARTY_1" ))
33
- nodekey = getNodeKeyFromFile (os .getenv ("NILLION_NODEKEY_PATH_PARTY_1" ))
34
+ grpc_endpoint = os .getenv ("NILLION_NILCHAIN_GRPC" )
35
+ chain_id = os .getenv ("NILLION_NILCHAIN_CHAIN_ID" )
36
+ seed = "my_seed"
37
+ userkey = UserKey .from_seed ((seed ))
38
+ nodekey = NodeKey .from_seed ((seed ))
34
39
client = create_nillion_client (userkey , nodekey )
35
40
party_id = client .party_id
36
41
user_id = client .user_id
42
+
37
43
party_names = na_client .parties (3 )
38
44
program_name = "broadcasting"
39
45
program_mir_path = f"target/{ program_name } .nada.bin"
40
46
41
- # Store the program
47
+ # Create payments config and set up Nillion wallet with a private key to pay for operations
48
+ payments_config = create_payments_config (chain_id , grpc_endpoint )
49
+ payments_client = LedgerClient (payments_config )
50
+ payments_wallet = LocalWallet (
51
+ PrivateKey (bytes .fromhex (os .getenv ("NILLION_NILCHAIN_PRIVATE_KEY_0" ))),
52
+ prefix = "nillion" ,
53
+ )
54
+
55
+ ##### STORE PROGRAM
56
+ print ("-----STORE PROGRAM" )
57
+
42
58
program_id = await store_program (
43
- client , user_id , cluster_id , program_name , program_mir_path
59
+ client ,
60
+ payments_wallet ,
61
+ payments_client ,
62
+ user_id ,
63
+ cluster_id ,
64
+ program_name ,
65
+ program_mir_path ,
44
66
)
45
67
46
- # Create and store secrets for two parties
68
+ ##### STORE SECRETS
69
+ print ("-----STORE SECRETS" )
47
70
A = np .ones ([DIM ])
48
71
C = np .ones ([DIM ])
49
- A_store_id = await store_secret_array (
72
+
73
+ # Create a permissions object to attach to the stored secret
74
+ permissions = nillion .Permissions .default_for_user (client .user_id )
75
+ permissions .add_compute_permissions ({client .user_id : {program_id }})
76
+
77
+ # Create a secret
78
+ store_id_A = await store_secret_array (
50
79
client ,
80
+ payments_wallet ,
81
+ payments_client ,
51
82
cluster_id ,
52
83
program_id ,
53
- party_id ,
54
- party_names [0 ],
55
84
A ,
56
85
"A" ,
57
86
nillion .SecretInteger ,
87
+ 1 ,
88
+ permissions ,
58
89
)
59
- C_store_id = await store_secret_array (
90
+
91
+ store_id_C = await store_secret_array (
60
92
client ,
93
+ payments_wallet ,
94
+ payments_client ,
61
95
cluster_id ,
62
96
program_id ,
63
- party_id ,
64
- party_names [0 ],
65
97
C ,
66
98
"C" ,
67
99
nillion .SecretInteger ,
100
+ 1 ,
101
+ permissions ,
68
102
)
69
103
104
+ # Create and store secrets for two parties
105
+
70
106
B = np .ones ([DIM ])
71
107
D = np .ones ([DIM ])
72
- B_store_id = await store_secret_array (
108
+
109
+ store_id_B = await store_secret_array (
73
110
client ,
111
+ payments_wallet ,
112
+ payments_client ,
74
113
cluster_id ,
75
114
program_id ,
76
- party_id ,
77
- party_names [1 ],
78
115
B ,
79
116
"B" ,
80
117
nillion .SecretInteger ,
118
+ 1 ,
119
+ permissions ,
81
120
)
82
- D_store_id = await store_secret_array (
121
+
122
+ store_id_D = await store_secret_array (
83
123
client ,
124
+ payments_wallet ,
125
+ payments_client ,
84
126
cluster_id ,
85
127
program_id ,
86
- party_id ,
87
- party_names [1 ],
88
128
D ,
89
129
"D" ,
90
130
nillion .SecretInteger ,
131
+ 1 ,
132
+ permissions ,
91
133
)
92
134
93
135
# Set up the compute bindings for the parties
@@ -99,19 +141,35 @@ async def main() -> None:
99
141
100
142
print (f"Computing using program { program_id } " )
101
143
print (
102
- f"Use secret store_id: { A_store_id } , { B_store_id } , { C_store_id } , { D_store_id } "
144
+ f"Use secret store_id: { store_id_A } , { store_id_B } , { store_id_C } , { store_id_D } "
103
145
)
104
146
105
- computation_time_secrets = nillion .Secrets ({"my_int2" : nillion .SecretInteger (10 )})
147
+ ##### COMPUTE
148
+ print ("-----COMPUTE" )
149
+
150
+ # Bind the parties in the computation to the client to set input and output parties
151
+ compute_bindings = nillion .ProgramBindings (program_id )
152
+ compute_bindings .add_input_party (party_names [0 ], party_id )
153
+ compute_bindings .add_input_party (party_names [1 ], party_id )
154
+ compute_bindings .add_output_party (party_names [2 ], party_id )
155
+
156
+ # Create a computation time secret to use
157
+ computation_time_secrets = nillion .NadaValues ({})
158
+
159
+ # Get cost quote, then pay for operation to compute
106
160
107
- # Perform the computation and return the result
108
161
result = await compute (
109
162
client ,
163
+ payments_wallet ,
164
+ payments_client ,
165
+ program_id ,
110
166
cluster_id ,
111
167
compute_bindings ,
112
- [A_store_id , B_store_id , C_store_id , D_store_id ],
168
+ [store_id_A , store_id_B , store_id_C , store_id_D ],
113
169
computation_time_secrets ,
170
+ verbose = 1 ,
114
171
)
172
+
115
173
return result
116
174
117
175
0 commit comments