This repository has been archived by the owner on Dec 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 76
Added support for signer #33
Merged
Merged
Changes from 7 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
083cc74
Signer support
exp-table c2356ec
stringified all command items
exp-table de03e6e
loading pkeys from .env
exp-table 4719c5c
added accounts.json management
exp-table 69da9f0
updated readme'
exp-table 5f8925e
Added nile send
exp-table 10ab0af
fixed comments spacing
exp-table 33f51e5
passing tox linting
exp-table cb261c3
renamed the proxy commands and added examples
exp-table bb31eeb
import module instead of cli commands
exp-table dfc6e4e
fixed README typo
exp-table 6e6b56f
fixed another readme typo
exp-table fc280fd
fixed import path issues + functions arguments
exp-table 75beda4
shipping Account artifacts + fixed setup file
exp-table cdeecc6
try/catch starkware import + readme typo fixed
exp-table a1330bf
forgot to run tox format
exp-table 7835e43
passing network to signer
exp-table File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
click==8.0.1 | ||
python-dotenv==0.19.2 |
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,49 @@ | ||
"""nile common module.""" | ||
import os | ||
import json | ||
|
||
from nile.common import ACCOUNTS_FILENAME | ||
|
||
|
||
def register(pubkey, address, index, network): | ||
"""Register a new account.""" | ||
file = f"{network}.{ACCOUNTS_FILENAME}" | ||
|
||
if exists(pubkey, network): | ||
raise Exception(f"account-{index} already exists in {file}") | ||
|
||
with open(file, "r") as fp: | ||
accounts = json.load(fp) | ||
accounts[pubkey] = { | ||
"address":address, | ||
"index":index | ||
} | ||
with open(file, 'w') as file: | ||
json.dump(accounts, file) | ||
|
||
|
||
def exists(pubkey, network): | ||
"""Return whether an account exists or not.""" | ||
foo = next(load(pubkey, network), None) | ||
return foo is not None | ||
|
||
|
||
def load(pubkey, network): | ||
"""Load account that matches a pubkey.""" | ||
file = f"{network}.{ACCOUNTS_FILENAME}" | ||
|
||
if not os.path.exists(file): | ||
with open(file, 'w') as fp: | ||
json.dump({}, fp) | ||
|
||
with open(file) as fp: | ||
accounts = json.load(fp) | ||
if pubkey in accounts: | ||
yield accounts[pubkey] | ||
|
||
def current_index(network): | ||
file = f"{network}.{ACCOUNTS_FILENAME}" | ||
|
||
with open(file) as fp: | ||
accounts = json.load(fp) | ||
return len(accounts.keys()) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
"""Command to call or invoke StarkNet smart contracts.""" | ||
import os | ||
import subprocess | ||
import json | ||
from dotenv import load_dotenv | ||
load_dotenv() | ||
|
||
from nile import deployments, accounts | ||
from nile.common import GATEWAYS | ||
|
||
from nile.signer import Signer | ||
|
||
def proxy_setup_command(signer, network): | ||
"""Deploy an Account contract for the given private key.""" | ||
signer = Signer(int(os.environ[signer])) | ||
if accounts.exists(str(signer.public_key), network): | ||
signer_data = next(accounts.load(str(signer.public_key), network)) | ||
signer.account = signer_data["address"] | ||
signer.index = signer_data["index"] | ||
else: # doesn't exist, have to deploy | ||
signer.index = accounts.current_index(network) | ||
subprocess.run(f"nile deploy Account {signer.public_key} --alias account-{signer.index}", shell=True) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why not importing the module? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I didn't even think about it when prototyping initially. Good catch, fixed it! |
||
address, _ = next(deployments.load(f"account-{signer.index}", network)) | ||
# initialize account | ||
subprocess.run(f"nile invoke account-{signer.index} initialize {address}", shell=True) | ||
signer.account = address | ||
accounts.register(signer.public_key, address, signer.index, network) | ||
|
||
return signer | ||
|
||
|
||
def send_command(signer, contract, method, params, network): | ||
"""Sugared call to a contract passing by an Account contract.""" | ||
address, abi = next(deployments.load(contract, network)) | ||
return proxy_command(signer, [address, method] + list(params), network) | ||
|
||
def proxy_command(signer, params, network): | ||
"""Execute a tx going through an Account contract.""" | ||
# params are : to, selector_name, calldata | ||
signer = proxy_setup_command(signer, network) | ||
|
||
_, abi = next(deployments.load(f"account-{signer.index}", network)) | ||
|
||
command = [ | ||
"starknet", | ||
"invoke", | ||
"--address", | ||
signer.account, | ||
"--abi", | ||
abi, | ||
"--function", | ||
"execute", | ||
] | ||
|
||
if network == "mainnet": | ||
os.environ["STARKNET_NETWORK"] = "alpha" | ||
else: | ||
gateway_prefix = "feeder_gateway" if type == "call" else "gateway" | ||
command.append(f"--{gateway_prefix}_url={GATEWAYS.get(network)}") | ||
|
||
if len(params) > 0: | ||
command.append("--inputs") | ||
ingested_inputs = signer.get_inputs(params[0], params[1], params[2:]) | ||
command.extend([str(param) for param in ingested_inputs[0]]) | ||
command.append("--signature") | ||
command.extend([str(sig_part) for sig_part in ingested_inputs[1]]) | ||
|
||
subprocess.check_call(command) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
"""Utility for sending signed transactions to an Account on Starknet.""" | ||
import subprocess | ||
|
||
from starkware.crypto.signature.signature import private_to_stark_key, sign | ||
from starkware.starknet.public.abi import get_selector_from_name | ||
from starkware.cairo.common.hash_state import compute_hash_on_elements | ||
|
||
class Signer(): | ||
""" | ||
Utility for sending signed transactions to an Account on Starknet. | ||
|
||
Parameters | ||
---------- | ||
|
||
private_key : int | ||
|
||
Examples | ||
--------- | ||
Constructing a Singer object | ||
|
||
>>> signer = Signer(1234) | ||
|
||
Sending a transaction | ||
|
||
>>> await signer.send_transaction(account, | ||
account.contract_address, | ||
'set_public_key', | ||
[other.public_key] | ||
) | ||
|
||
""" | ||
|
||
def __init__(self, private_key): | ||
self.private_key = private_key | ||
self.public_key = private_to_stark_key(private_key) | ||
self.account = None | ||
self.index = 0 | ||
|
||
def sign(self, message_hash): | ||
return sign(msg_hash=message_hash, priv_key=self.private_key) | ||
|
||
def get_nonce(self): | ||
nonce = subprocess.check_output(f"nile call account-{self.index} get_nonce", shell=True, encoding='utf-8') | ||
return int(nonce) | ||
|
||
def get_inputs(self, to, selector_name, calldata): | ||
nonce = self.get_nonce() | ||
selector = get_selector_from_name(selector_name) | ||
ingested_calldata = [int(arg, 16) for arg in calldata] | ||
message_hash = hash_message(int(self.account,16), int(to,16), selector, ingested_calldata, nonce) | ||
sig_r, sig_s = self.sign(message_hash) | ||
return ((int(to,16), selector, len(ingested_calldata), *ingested_calldata, nonce), (sig_r, sig_s)) | ||
|
||
|
||
def hash_message(sender, to, selector, calldata, nonce): | ||
message = [ | ||
sender, | ||
to, | ||
selector, | ||
compute_hash_on_elements(calldata), | ||
nonce | ||
] | ||
return compute_hash_on_elements(message) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
there's no
nile set
command :)