-
Notifications
You must be signed in to change notification settings - Fork 0
/
accounts.py
143 lines (114 loc) · 4.35 KB
/
accounts.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
from dydx3 import private_key_to_public_key_pair_hex
from dydx3.constants import *
from dydx3.eth_signing.eth_prive_action import SignEthPrivateAction
from dydx3.eth_signing.onboarding_action import SignOnboardingAction
from dydx3.helpers.request_helpers import generate_now_iso
from dydx3.helpers.requests import request
from eulith_web3.eulith_web3 import EulithWeb3
from eulith_web3.signer import Signature
from eulith_web3.signing import construct_signing_middleware
from credentials import *
from private_request import private_request
from utils import get_dydx_host, get_exchange_contract, get_signer
def normalize_signature(signature: Signature) -> str:
"""
Normalize the signature to a string, for instance for serialization for an RPC method.
"""
compatible_v = signature.v + 27
last_two_chars = hex(compatible_v)[-2:]
return str(signature)[:-2] + last_two_chars
def create_user(parser_args):
network_id = int(parser_args.network_id)
signer = get_signer()
host = get_dydx_host(network_id)
public_x, public_y = private_key_to_public_key_pair_hex(STARK_PRIVATE_KEY)
sign_offchain_action = SignOnboardingAction(signer, NETWORK_ID_MAINNET)
message = {
"action": OFF_CHAIN_ONBOARDING_ACTION,
}
message_hash = sign_offchain_action.get_hash(**message)
signature = signer.sign_msg_hash(message_hash)
ns = (
normalize_signature(signature) + "00"
) # // 00 comes from SIGNATURE_TYPE_NO_PREPEND in constants
request_path = "/".join(["/v3", "onboarding"])
response = request(
host + request_path,
"post",
{
"DYDX-SIGNATURE": ns,
"DYDX-ETHEREUM-ADDRESS": signer.address,
},
{
"starkKey": public_x,
"starkKeyYCoordinate": public_y,
},
10,
).data
print(f"Created user, response:")
print(response)
print()
print("YOU SHOULD WRITE DOWN THESE API CREDENTIALS")
def get_account(parser_args):
network_id = int(parser_args.network_id)
print(_get_account_internal(network_id))
def _get_account_internal(network_id: int) -> dict:
host = get_dydx_host(network_id)
response = private_request(host, "get", "accounts", {})
return response.get("accounts", {})[
0
] # dydx appears to only supports 1 account per eth key
def get_registration_signature(network_id: int) -> dict:
host = get_dydx_host(network_id)
response = private_request(host, "get", "registration", {})
return response.get("signature")
def create_new_api_key_with_signer(args):
network_id = int(args.network_id)
host = get_dydx_host(network_id)
signer = get_signer()
sign_offchain_action = SignEthPrivateAction(signer, NETWORK_ID_MAINNET)
request_path = "/".join(["/v3", "api-keys"])
timestamp = generate_now_iso()
message = {
"method": "POST",
"request_path": request_path,
"body": "{}",
"timestamp": timestamp,
}
message_hash = sign_offchain_action.get_hash(**message)
signature = signer.sign_msg_hash(message_hash)
ns = (
normalize_signature(signature) + "00"
) # // 00 comes from SIGNATURE_TYPE_NO_PREPEND in constants
return request(
host + request_path,
"post",
{
"DYDX-SIGNATURE": ns,
"DYDX-TIMESTAMP": timestamp,
"DYDX-ETHEREUM-ADDRESS": signer.address,
},
{},
10,
).data
def register_user(parser_args):
network_id = int(parser_args.network_id)
if network_id != 1:
raise ValueError("only mainnet is supported for now")
public_x, public_y = private_key_to_public_key_pair_hex(STARK_PRIVATE_KEY)
reg_signature = get_registration_signature(network_id)
signer = get_signer()
with EulithWeb3(
"https://eth-main.eulithrpc.com/v0",
eulith_token=EULITH_TOKEN,
signing_middle_ware=construct_signing_middleware(signer),
) as ew3:
contract = get_exchange_contract(network_id, ew3)
tx_params = contract.functions.registerUser(
signer.address, int(public_x, 16), reg_signature
).build_transaction({"from": signer.address, "gas": 100000})
tx_hash = ew3.eth.send_transaction(tx_params)
print(f"Registration tx hash: {tx_hash.hex()}")
def show_wallet(_args):
signer = get_signer()
print(f"wallet address: {signer.address}")