Skip to content

Commit

Permalink
millionaires example updated
Browse files Browse the repository at this point in the history
  • Loading branch information
Davetbutler committed Jun 6, 2024
1 parent 621bc00 commit e2fb904
Show file tree
Hide file tree
Showing 5 changed files with 172 additions and 70 deletions.
Original file line number Diff line number Diff line change
@@ -1,50 +1,87 @@
import asyncio
import py_nillion_client as nillion
import os
import sys
import pytest

from dotenv import load_dotenv
from config import (
CONFIG_PARTY_1
)
from config import CONFIG_PARTY_1

from cosmpy.aerial.client import LedgerClient
from cosmpy.aerial.wallet import LocalWallet
from cosmpy.crypto.keypairs import PrivateKey

sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '../..')))
from helpers.nillion_client_helper import create_nillion_client
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "../..")))
from helpers.nillion_client_helper import (
create_nillion_client,
pay,
create_payments_config,
)
from helpers.nillion_keypath_helper import getUserKeyFromFile, getNodeKeyFromFile

load_dotenv()


# Alice stores the millionaires program in the network
async def main():
cluster_id = os.getenv("NILLION_CLUSTER_ID")
grpc_endpoint = os.getenv("NILLION_GRPC")
chain_id = os.getenv("NILLION_CHAIN_ID")
client_alice = create_nillion_client(
getUserKeyFromFile(CONFIG_PARTY_1["userkey_file"]), getNodeKeyFromFile(CONFIG_PARTY_1["nodekey_file"])
getUserKeyFromFile(CONFIG_PARTY_1["userkey_file"]),
getNodeKeyFromFile(CONFIG_PARTY_1["nodekey_file"]),
)

millionaires_program_name = "millionaires"

# Note: check out the code for the full millionaires program in the programs folder
program_mir_path = "millionaires.nada.bin"
program_mir_path = "../../programs-compiled/millionaires.nada.bin"

payments_config = create_payments_config(chain_id, grpc_endpoint)
payments_client = LedgerClient(payments_config)
payments_wallet = LocalWallet(
PrivateKey(bytes.fromhex(os.getenv("NILLION_WALLET_PRIVATE_KEY"))),
prefix="nillion",
)

# Pay to store the program
receipt_store_program = await pay(
client_alice,
nillion.Operation.store_program(),
payments_wallet,
payments_client,
cluster_id,
)

# Store millionaires program in the network
print(f"Storing program in the network: {millionaires_program_name}")
await client_alice.store_program(
cluster_id, millionaires_program_name, program_mir_path
program_id = await client_alice.store_program(
cluster_id, millionaires_program_name, program_mir_path, receipt_store_program
)

# Set permissions for the client to compute on the program
permissions = nillion.Permissions.default_for_user(client_alice.user_id)
permissions.add_compute_permissions({client_alice.user_id: {program_id}})

user_id_alice = client_alice.user_id
program_id = f"{user_id_alice}/{millionaires_program_name}"

print(f"Alice stores millionaires program at program_id: {program_id}")
print(f"Alice tells Bob and Charlie her user_id and the millionaires program_id")

print("\nπŸ“‹β¬‡οΈ Copy and run the following command to store Bob and Charlie's salaries in the network")
print(f"\npython3 02_store_secret_party_n.py --user_id_1 {user_id_alice} --program_id {program_id}")
print(
"\nπŸ“‹β¬‡οΈ Copy and run the following command to store Bob and Charlie's salaries in the network"
)
print(
f"\npython3 02_store_secret_party_n.py --user_id_1 {user_id_alice} --program_id {program_id}"
)
return [user_id_alice, program_id]


if __name__ == "__main__":
asyncio.run(main())


@pytest.mark.asyncio
async def test_main():
pass
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,25 @@
import pytest

from dotenv import load_dotenv
from config import (
CONFIG_N_PARTIES
)
from config import CONFIG_N_PARTIES

from cosmpy.aerial.client import LedgerClient
from cosmpy.aerial.wallet import LocalWallet
from cosmpy.crypto.keypairs import PrivateKey

sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '../..')))
from helpers.nillion_client_helper import create_nillion_client
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "../..")))
from helpers.nillion_client_helper import (
create_nillion_client,
pay,
create_payments_config,
)
from helpers.nillion_keypath_helper import getUserKeyFromFile, getNodeKeyFromFile

load_dotenv()


# Bob and Charlie store their salaries in the network
async def main(args = None):
async def main(args=None):
parser = argparse.ArgumentParser(
description="Create a secret on the Nillion network with set read/retrieve permissions"
)
Expand All @@ -37,33 +44,36 @@ async def main(args = None):
args = parser.parse_args(args)

cluster_id = os.getenv("NILLION_CLUSTER_ID")

grpc_endpoint = os.getenv("NILLION_GRPC")
chain_id = os.getenv("NILLION_CHAIN_ID")

# start a list of store ids to keep track of stored secrets
store_ids = []
party_ids = []

for party_info in CONFIG_N_PARTIES:
client_n = create_nillion_client(
getUserKeyFromFile(party_info["userkey_file"]),
getNodeKeyFromFile(party_info["nodekey_file"])
getUserKeyFromFile(party_info["userkey_file"]),
getNodeKeyFromFile(party_info["nodekey_file"]),
)
party_id_n = client_n.party_id
user_id_n = client_n.user_id

payments_config_n = create_payments_config(chain_id, grpc_endpoint)
payments_client_n = LedgerClient(payments_config_n)
payments_wallet_n = LocalWallet(
PrivateKey(bytes.fromhex(os.getenv("NILLION_WALLET_PRIVATE_KEY"))),
prefix="nillion",
)

party_name = party_info["party_name"]
secret_name = party_info["secret_name"]
secret_value = party_info["secret_value"]

# Create a secret for the current party
stored_secret = nillion.Secrets({
secret_name: nillion.SecretInteger(secret_value)
})

# Create input bindings for the specific millionaires program by program id
secret_bindings = nillion.ProgramBindings(args.program_id)

# Add the respective input party to say who will provide the input to the program
secret_bindings.add_input_party(party_name, party_id_n)
print(f"\nπŸ”— {party_name} sets bindings so that the secret can be input to a specific program (program_id: {args.program_id}) by a specific party (party_id: {party_id_n})")
stored_secret = nillion.Secrets(
{secret_name: nillion.SecretInteger(secret_value)}
)

# Create permissions object with default permissions for the current user
permissions = nillion.Permissions.default_for_user(user_id_n)
Expand All @@ -73,28 +83,46 @@ async def main(args = None):
args.user_id_1: {args.program_id},
}
permissions.add_compute_permissions(compute_permissions)
print(f"\nπŸ‘ {party_name} gives compute permissions on their secret to Alice's user_id: {args.user_id_1}")
print(
f"\nπŸ‘ {party_name} gives compute permissions on their secret to Alice's user_id: {args.user_id_1}"
)

receipt_store = await pay(
client_n,
nillion.Operation.store_secrets(stored_secret),
payments_wallet_n,
payments_client_n,
cluster_id,
)
# Store the permissioned secret
store_id = await client_n.store_secrets(
cluster_id, secret_bindings, stored_secret, permissions
cluster_id, stored_secret, permissions, receipt_store
)

store_ids.append(store_id)
party_ids.append(party_id_n)

print(f"\nπŸŽ‰ {party_name} stored {secret_name}: {secret_value} at store id: {store_id}")


party_ids_to_store_ids = ' '.join([f'{party_id}:{store_id}' for party_id, store_id in zip(party_ids, store_ids)])
print(
f"\nπŸŽ‰ {party_name} stored {secret_name}: {secret_value} at store id: {store_id}"
)

party_ids_to_store_ids = " ".join(
[f"{party_id}:{store_id}" for party_id, store_id in zip(party_ids, store_ids)]
)

print("\nπŸ“‹β¬‡οΈ Copy and run the following command to run multi party computation using the secrets")
print(f"\npython3 03_multi_party_compute.py --program_id {args.program_id} --party_ids_to_store_ids {party_ids_to_store_ids}")
print(
"\nπŸ“‹β¬‡οΈ Copy and run the following command to run multi party computation using the secrets"
)
print(
f"\npython3 03_multi_party_compute.py --program_id {args.program_id} --party_ids_to_store_ids {party_ids_to_store_ids}"
)
return [args.program_id, party_ids_to_store_ids]


if __name__ == "__main__":
asyncio.run(main())


@pytest.mark.asyncio
async def test_main():
pass
Loading

0 comments on commit e2fb904

Please sign in to comment.