Skip to content

Commit

Permalink
feat: deploy Openzeppelin accounts script (#1396)
Browse files Browse the repository at this point in the history
Time spent on this PR: 0.5

## Pull request type

<!-- Please try to limit your pull request to one type,
submit multiple pull requests if needed. -->

Please check the type of change your PR introduces:

- [ ] Bugfix
- [x] Feature
- [ ] Code style update (formatting, renaming)
- [ ] Refactoring (no functional changes, no api changes)
- [ ] Build related changes
- [ ] Documentation content changes
- [ ] Other (please describe):

## What is the current behavior?

<!-- Please describe the current behavior that you are modifying,
or link to a relevant issue. -->

Resolves #<Issue number>

## What is the new behavior?

Script that allows the deployment of OpenZeppelin accounts, saving the
private key on AWS Secrets and exporting a JSON containing the addresses
of the deployed accounts and the secret ID to retrieve the private key
(file stored in `kakarot_scripts/data`).

**Parameters to configure in order to run the script:**

- **num_accounts** - The number of OZ accounts.
- **amount** - The amount to send to the address to deploy the accounts.
- **private_key** - The private key to use, which will also be saved on
AWS Secrets. Therefore, it only needs to be provided the first time; in
subsequent executions, you can use an empty string `""` to retrieve the
previous key. Otherwise, the secret will be updated.
- **class_hash** - The class hash of OpenZeppelinAccount.

To interact with AWS Secrets, boto3 requires the AWS configuration
variables to be set up. [Here is the
documentation](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/configure/index.html)

<!-- Reviewable:start -->
- - -
This change is [<img src="https://reviewable.io/review_button.svg"
height="34" align="absmiddle"
alt="Reviewable"/>](https://reviewable.io/reviews/kkrt-labs/kakarot/1396)
<!-- Reviewable:end -->
  • Loading branch information
eugypalu authored Sep 6, 2024
1 parent e181743 commit 35b4db5
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 3 deletions.
3 changes: 3 additions & 0 deletions kakarot_scripts/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class NetworkType(Enum):
"chain_id": StarknetChainId.MAINNET,
"check_interval": 1,
"max_wait": 10,
"class_hash": 0x061DAC032F228ABEF9C6626F995015233097AE253A7F72D68552DB02F2971B8F,
},
"sepolia": {
"name": "starknet-sepolia",
Expand All @@ -50,6 +51,7 @@ class NetworkType(Enum):
"chain_id": StarknetChainId.SEPOLIA,
"check_interval": 1,
"max_wait": 10,
"class_hash": 0x061DAC032F228ABEF9C6626F995015233097AE253A7F72D68552DB02F2971B8F,
},
"starknet-devnet": {
"name": "starknet-devnet",
Expand All @@ -68,6 +70,7 @@ class NetworkType(Enum):
"type": NetworkType.DEV,
"check_interval": 0.01,
"max_wait": 3,
"class_hash": 0x6153CCF69FD20F832C794DF36E19135F0070D0576144F0B47F75A226E4BE530,
"relayers": [
{
"address": 0xB3FF441A68610B30FD5E2ABBF3A1548EB6BA6F3559F2862BF2DC757E5828CA,
Expand Down
62 changes: 62 additions & 0 deletions kakarot_scripts/utils/deploy_oz.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# %% Imports
import asyncio
import json
import logging
import secrets

import boto3

from kakarot_scripts.utils.starknet import deploy_starknet_account

# Initialize AWS Secrets Manager client
client = boto3.client("secretsmanager")
SECRET_NAME = "relayers_private_key"

logging.basicConfig()
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)


# %% Get private key
def get_private_key():
"""Retrieve or generate a private key."""
try:
response = client.get_secret_value(SecretId=SECRET_NAME)
private_key = response["SecretString"]
secret_id = response["ARN"]
except client.exceptions.ResourceNotFoundException:
private_key = hex(secrets.randbits(256))
secret_id = client.create_secret(Name=SECRET_NAME, SecretString=private_key)[
"ARN"
]
return private_key, secret_id


# %% Deploy accounts
async def main():
"""Deploy 'num_accounts' accounts and store the private keys in AWS Secrets Manager."""
num_accounts = 30 # Number of accounts to create
amount = 0.1 # Initial funding amount for each relayer account
accounts_data = []
private_key, secret_id = get_private_key()

accounts_data = [
{
"secret_id": secret_id,
"address": (
await deploy_starknet_account(private_key=private_key, amount=amount)
)["address"],
}
for _ in range(num_accounts)
]
logger.info(f"{num_accounts} accounts deployed and saved to 'oz_accounts.json'")

# Save the account data to a JSON file
with open("kakarot_scripts/data/oz_accounts.json", "w") as json_file:
json.dump(accounts_data, json_file, indent=2)


# %% Run
if __name__ == "__main__":
# Deploy the accounts and store private keys in AWS Secrets Manager
asyncio.run(main())
4 changes: 3 additions & 1 deletion kakarot_scripts/utils/starknet.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,9 @@ async def deploy_starknet_account(class_hash=None, private_key=None, amount=1):
)
key_pair = KeyPair.from_private_key(int(private_key, 16))
constructor_calldata = [key_pair.public_key]
class_hash = class_hash or get_declarations()["OpenzeppelinAccount"]
class_hash = class_hash or NETWORK.get(
"class_hash", get_declarations().get("OpenzeppelinAccount")
)
address = compute_address(
salt=salt,
class_hash=class_hash,
Expand Down
71 changes: 69 additions & 2 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ async-lru = "^2.0.4"
toml = "^0.10.2"
scikit-learn = "^1.5.1"
seaborn = "^0.13.2"
boto3 = "^1.35.12"

[tool.poetry.group.dev.dependencies]
pytest = "^8.1.1"
Expand Down

0 comments on commit 35b4db5

Please sign in to comment.