-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathwallet.py
225 lines (126 loc) · 4.5 KB
/
wallet.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#!/usr/bin/env python
# coding: utf-8
# In[1]:
# import libraries
import subprocess
import json
import os
from constants import *
from dotenv import load_dotenv
from bipwallet import wallet
from web3 import Web3
from eth_account import Account
from bit import PrivateKeyTestnet
from bit.network import NetworkAPI
from web3.middleware import geth_poa_middleware
from web3.gas_strategies.time_based import medium_gas_price_strategy
load_dotenv()
# In[2]:
# import mnemonic from env
filepath="env.bat"
p = subprocess.Popen(filepath, shell=True, stdout = subprocess.PIPE)
stdout, stderr = p.communicate()
print (p.returncode) # is 0 if success
mnemonic = os.getenv('MNEMONIC')
print(mnemonic)
# In[3]:
# connect to local ETH/ geth
w3 = Web3(Web3.HTTPProvider('http://127.0.0.1:8545'))
w3.middleware_onion.inject(geth_poa_middleware, layer=0)
w3.eth.setGasPriceStrategy(medium_gas_price_strategy)
# In[4]:
# Define function to derive wallet
def derive_wallets(mnemonic, coin, numderive):
"""Use the subprocess library to call the php file script from Python"""
command = f'php ./hd-wallet-derive/hd-wallet-derive.php -g --mnemonic="{mnemonic}" --numderive="{numderive}" --coin="{coin}" --format=json'
p = subprocess.Popen(command, stdout=subprocess.PIPE, shell=True)
(output, err) = p.communicate()
keys = json.loads(output)
return keys
# In[5]:
# Test the function derive_wallets
derive_wallets(mnemonic, 'BTC', 3)
# In[6]:
#Setting dictionary of coins to be used in the wallet
coins = {"eth", "btc-test", "btc"}
numderive = 3
# In[7]:
#
keys = {}
for coin in coins:
keys[coin]= derive_wallets(mnemonic, coin, numderive=3)
# In[8]:
eth_PrivateKey = keys["eth"][0]['privkey']
btc_PrivateKey = keys['btc-test'][0]['privkey']
print(json.dumps(eth_PrivateKey, indent=4, sort_keys=True))
print(json.dumps(btc_PrivateKey, indent=4, sort_keys=True))
# In[9]:
print(json.dumps(keys, indent=4, sort_keys=True))
# In[9]:
# create a function that convert the privkey string in a child key to an account object.
def priv_key_to_account(coin,priv_key):
print(coin)
print(priv_key)
if coin == ETH:
return Account.privateKeyToAccount(priv_key)
elif coin == BTCTEST:
return PrivateKeyTestnet(priv_key)
# In[10]:
def create_tx(coin,account, recipient, amount):
if coin == ETH:
gasEstimate = w3.eth.estimateGas(
{"from":eth_acc.address, "to":recipient, "value": amount}
)
return {
"from": eth_acc.address,
"to": recipient,
"value": amount,
"gasPrice": w3.eth.gasPrice,
"gas": gasEstimate,
"nonce": w3.eth.getTransactionCount(eth_acc.address)
}
elif coin == BTCTEST:
return PrivateKeyTestnet.prepare_transaction(account.address, [(recipient, amount, BTC)])
# In[11]:
# create a function to hold Ethereum
eth_acc = priv_key_to_account(ETH, derive_wallets(mnemonic, ETH,5)[0]['privkey'])
# In[33]:
# create a function to send txn
def send_txn(coin,account,recipient, amount):
txn = create_tx(coin, account, recipient, amount)
if coin == ETH:
signed_txn = eth_acc.sign_transaction(txn)
result = w3.eth.sendRawTransaction(signed_txn.rawTransaction)
print(result.hex())
return result.hex()
elif coin == BTCTEST:
tx_btctest = create_tx(coin, account, recipient, amount)
signed_txn = account.sign_transaction(txn)
print(signed_txn)
return NetworkAPI.broadcast_tx_testnet(signed_txn)
# ## BTC-TEST Transactions
# In[15]:
btc_acc = priv_key_to_account(BTCTEST,btc_PrivateKey)
# In[23]:
# create BTC transaction
create_tx(BTCTEST,btc_acc,"n3D8vVvLyD7pPQmWoQgMMERZrDjmBheBpU", 0.1)
# In[34]:
# Send BTC transaction
send_txn(BTCTEST,btc_acc,"mtK73sNPY9CKuzVvpv4W1969AD1UmGGfsX", 0.1)
# ## ETH Transactions
# In[37]:
#connecting to HTTP with address pk
w3 = Web3(Web3.HTTPProvider("http://127.0.0.1:8545/0x30c2577db89760baa9ba4058b1033b9e103f287e5de1689e35833ee8e7a7c857"))
# In[38]:
# double check if I am connected to blockchain.
w3.isConnected()
# ## Check the Balance of the account with local mining blockchain
# In[39]:
w3.eth.getBalance("0x46BbdBf56ff911A93AdaF0164d0709F78B52765E")
# In[41]:
create_tx(ETH,eth_acc,"0x3dA0F09EF6F833b179768a63AdD29061e0702e47", 1000)
# In[42]:
send_txn(ETH, eth_acc,"0x3dA0F09EF6F833b179768a63AdD29061e0702e47", 1000)
# ## Confirmation of
# In[43]:
w3.eth.getBalance("0x3dA0F09EF6F833b179768a63AdD29061e0702e47")