AutoPEP8 Python scripts
This commit is contained in:
@@ -22,30 +22,36 @@ from rlp.sedes import big_endian_int, binary, Binary
|
||||
from rlp import Serializable
|
||||
|
||||
try:
|
||||
from Crypto.Hash import keccak
|
||||
sha3_256 = lambda x: keccak.new(digest_bits=256, data=x).digest()
|
||||
from Crypto.Hash import keccak
|
||||
|
||||
def sha3_256(x): return keccak.new(digest_bits=256, data=x).digest()
|
||||
except:
|
||||
import sha3 as _sha3
|
||||
sha3_256 = lambda x: _sha3.sha3_256(x).digest()
|
||||
import sha3 as _sha3
|
||||
|
||||
def sha3_256(x): return _sha3.sha3_256(x).digest()
|
||||
address = Binary.fixed_length(20, allow_empty=True)
|
||||
|
||||
|
||||
def sha3(seed):
|
||||
return sha3_256(str(seed))
|
||||
return sha3_256(str(seed))
|
||||
|
||||
|
||||
class Transaction(Serializable):
|
||||
fields = [
|
||||
('nonce', big_endian_int),
|
||||
('gasprice', big_endian_int),
|
||||
('startgas', big_endian_int),
|
||||
('to', address),
|
||||
('value', big_endian_int),
|
||||
('data', binary),
|
||||
('v', big_endian_int),
|
||||
('r', big_endian_int),
|
||||
('s', big_endian_int),
|
||||
]
|
||||
fields = [
|
||||
('nonce', big_endian_int),
|
||||
('gasprice', big_endian_int),
|
||||
('startgas', big_endian_int),
|
||||
('to', address),
|
||||
('value', big_endian_int),
|
||||
('data', binary),
|
||||
('v', big_endian_int),
|
||||
('r', big_endian_int),
|
||||
('s', big_endian_int),
|
||||
]
|
||||
|
||||
def __init__(self, nonce, gasprice, startgas, to, value, data, v=0, r=0, s=0):
|
||||
super(Transaction, self).__init__(
|
||||
nonce, gasprice, startgas, to, value, data, v, r, s)
|
||||
|
||||
def __init__(self, nonce, gasprice, startgas, to, value, data, v=0, r=0, s=0):
|
||||
super(Transaction, self).__init__(nonce, gasprice, startgas, to, value, data, v, r, s)
|
||||
|
||||
UnsignedTransaction = Transaction.exclude(['v', 'r', 's'])
|
||||
|
||||
@@ -22,33 +22,36 @@ from ledgerblue.commException import CommException
|
||||
import argparse
|
||||
import struct
|
||||
|
||||
|
||||
def parse_bip32_path(path):
|
||||
if len(path) == 0:
|
||||
return ""
|
||||
result = ""
|
||||
elements = path.split('/')
|
||||
for pathElement in elements:
|
||||
element = pathElement.split('\'')
|
||||
if len(element) == 1:
|
||||
result = result + struct.pack(">I", int(element[0]))
|
||||
else:
|
||||
result = result + struct.pack(">I", 0x80000000 | int(element[0]))
|
||||
return result
|
||||
if len(path) == 0:
|
||||
return ""
|
||||
result = ""
|
||||
elements = path.split('/')
|
||||
for pathElement in elements:
|
||||
element = pathElement.split('\'')
|
||||
if len(element) == 1:
|
||||
result = result + struct.pack(">I", int(element[0]))
|
||||
else:
|
||||
result = result + struct.pack(">I", 0x80000000 | int(element[0]))
|
||||
return result
|
||||
|
||||
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument('--path', help="BIP 32 path to retrieve")
|
||||
args = parser.parse_args()
|
||||
|
||||
if args.path == None:
|
||||
args.path = "44'/60'/0'/0/0"
|
||||
args.path = "44'/60'/0'/0/0"
|
||||
|
||||
donglePath = parse_bip32_path(args.path)
|
||||
apdu = "e0020100".decode('hex') + chr(len(donglePath) + 1) + chr(len(donglePath) / 4) + donglePath
|
||||
apdu = "e0020100".decode('hex') + chr(len(donglePath) + 1) + \
|
||||
chr(len(donglePath) / 4) + donglePath
|
||||
|
||||
dongle = getDongle(True)
|
||||
result = dongle.exchange(bytes(apdu))
|
||||
offset = 1 + result[0]
|
||||
address = result[offset + 1 : offset + 1 + result[offset]]
|
||||
address = result[offset + 1: offset + 1 + result[offset]]
|
||||
|
||||
print "Public key " + str(result[1 : 1 + result[0]]).encode('hex')
|
||||
print "Public key " + str(result[1: 1 + result[0]]).encode('hex')
|
||||
print "Address 0x" + str(address)
|
||||
|
||||
@@ -22,28 +22,31 @@ from ledgerblue.commException import CommException
|
||||
import argparse
|
||||
import struct
|
||||
|
||||
|
||||
def parse_bip32_path(path):
|
||||
if len(path) == 0:
|
||||
return ""
|
||||
result = ""
|
||||
elements = path.split('/')
|
||||
for pathElement in elements:
|
||||
element = pathElement.split('\'')
|
||||
if len(element) == 1:
|
||||
result = result + struct.pack(">I", int(element[0]))
|
||||
else:
|
||||
result = result + struct.pack(">I", 0x80000000 | int(element[0]))
|
||||
return result
|
||||
if len(path) == 0:
|
||||
return ""
|
||||
result = ""
|
||||
elements = path.split('/')
|
||||
for pathElement in elements:
|
||||
element = pathElement.split('\'')
|
||||
if len(element) == 1:
|
||||
result = result + struct.pack(">I", int(element[0]))
|
||||
else:
|
||||
result = result + struct.pack(">I", 0x80000000 | int(element[0]))
|
||||
return result
|
||||
|
||||
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument('--path', help="BIP 32 path to retrieve")
|
||||
args = parser.parse_args()
|
||||
|
||||
if args.path == None:
|
||||
args.path = "44'/60'/0'/0/0"
|
||||
args.path = "44'/60'/0'/0/0"
|
||||
|
||||
donglePath = parse_bip32_path(args.path)
|
||||
apdu = "e0060000".decode('hex') + chr(len(donglePath) + 1) + chr(len(donglePath) / 4) + donglePath
|
||||
apdu = "e0060000".decode('hex') + chr(len(donglePath) + 1) + \
|
||||
chr(len(donglePath) / 4) + donglePath
|
||||
|
||||
dongle = getDongle(True)
|
||||
dongle.exchange(bytes(apdu))
|
||||
|
||||
@@ -26,18 +26,20 @@ from ethBase import Transaction, UnsignedTransaction
|
||||
from rlp import encode
|
||||
from rlp.utils import decode_hex, encode_hex, str_to_bytes
|
||||
|
||||
|
||||
def parse_bip32_path(path):
|
||||
if len(path) == 0:
|
||||
return ""
|
||||
result = ""
|
||||
elements = path.split('/')
|
||||
for pathElement in elements:
|
||||
element = pathElement.split('\'')
|
||||
if len(element) == 1:
|
||||
result = result + struct.pack(">I", int(element[0]))
|
||||
else:
|
||||
result = result + struct.pack(">I", 0x80000000 | int(element[0]))
|
||||
return result
|
||||
if len(path) == 0:
|
||||
return ""
|
||||
result = ""
|
||||
elements = path.split('/')
|
||||
for pathElement in elements:
|
||||
element = pathElement.split('\'')
|
||||
if len(element) == 1:
|
||||
result = result + struct.pack(">I", int(element[0]))
|
||||
else:
|
||||
result = result + struct.pack(">I", 0x80000000 | int(element[0]))
|
||||
return result
|
||||
|
||||
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument('--nonce', help="Nonce associated to the account")
|
||||
@@ -50,12 +52,12 @@ parser.add_argument('--data', help="Data to add, hex encoded")
|
||||
args = parser.parse_args()
|
||||
|
||||
if args.path == None:
|
||||
args.path = "44'/60'/0'/0/0"
|
||||
args.path = "44'/60'/0'/0/0"
|
||||
|
||||
if args.data == None:
|
||||
args.data = ""
|
||||
args.data = ""
|
||||
else:
|
||||
args.data = decode_hex(args.data[2:])
|
||||
args.data = decode_hex(args.data[2:])
|
||||
|
||||
amount = Decimal(args.amount) * 10**18
|
||||
|
||||
@@ -71,7 +73,8 @@ tx = Transaction(
|
||||
encodedTx = encode(tx, UnsignedTransaction)
|
||||
|
||||
donglePath = parse_bip32_path(args.path)
|
||||
apdu = "e0040000".decode('hex') + chr(len(donglePath) + 1 + len(encodedTx)) + chr(len(donglePath) / 4) + donglePath + encodedTx
|
||||
apdu = "e0040000".decode('hex') + chr(len(donglePath) + 1 +
|
||||
len(encodedTx)) + chr(len(donglePath) / 4) + donglePath + encodedTx
|
||||
|
||||
dongle = getDongle(True)
|
||||
result = dongle.exchange(bytes(apdu))
|
||||
@@ -80,6 +83,7 @@ v = result[0]
|
||||
r = int(str(result[1:1 + 32]).encode('hex'), 16)
|
||||
s = int(str(result[1 + 32: 1 + 32 + 32]).encode('hex'), 16)
|
||||
|
||||
tx = Transaction(tx.nonce, tx.gasprice, tx.startgas, tx.to, tx.value, tx.data, v, r, s)
|
||||
tx = Transaction(tx.nonce, tx.gasprice, tx.startgas,
|
||||
tx.to, tx.value, tx.data, v, r, s)
|
||||
|
||||
print "Signed transaction " + encode_hex(encode(tx))
|
||||
|
||||
@@ -32,109 +32,131 @@ from ethBase import Transaction, UnsignedTransaction, sha3
|
||||
SPLIT_CONTRACT_FUNCTION = decode_hex("9c709343")
|
||||
SPLIT_CONTRACT_ADDRESS = "5dc8108fc79018113a58328f5283b376b83922ef"
|
||||
|
||||
|
||||
def rpc_call(http, url, methodDebug):
|
||||
req = http.get(url)
|
||||
if req.status_code == 200:
|
||||
result = json.loads(req.text)
|
||||
if 'error' in result:
|
||||
raise Exception("Server error - " + methodDebug + " - " + result['error']['message'])
|
||||
return result
|
||||
else:
|
||||
raise Exception("Server error - " + methodDebug + " got status " + req.status)
|
||||
req = http.get(url)
|
||||
if req.status_code == 200:
|
||||
result = json.loads(req.text)
|
||||
if 'error' in result:
|
||||
raise Exception("Server error - " + methodDebug +
|
||||
" - " + result['error']['message'])
|
||||
return result
|
||||
else:
|
||||
raise Exception("Server error - " + methodDebug +
|
||||
" got status " + req.status)
|
||||
|
||||
|
||||
def parse_bip32_path(path):
|
||||
if len(path) == 0:
|
||||
return ""
|
||||
result = ""
|
||||
elements = path.split('/')
|
||||
for pathElement in elements:
|
||||
element = pathElement.split('\'')
|
||||
if len(element) == 1:
|
||||
result = result + struct.pack(">I", int(element[0]))
|
||||
else:
|
||||
result = result + struct.pack(">I", 0x80000000 | int(element[0]))
|
||||
return result
|
||||
if len(path) == 0:
|
||||
return ""
|
||||
result = ""
|
||||
elements = path.split('/')
|
||||
for pathElement in elements:
|
||||
element = pathElement.split('\'')
|
||||
if len(element) == 1:
|
||||
result = result + struct.pack(">I", int(element[0]))
|
||||
else:
|
||||
result = result + struct.pack(">I", 0x80000000 | int(element[0]))
|
||||
return result
|
||||
|
||||
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument('--nonce', help="Nonce associated to the account (default : query account)")
|
||||
parser.add_argument('--gasprice', help="Network gas price (default : query network)")
|
||||
parser.add_argument(
|
||||
'--nonce', help="Nonce associated to the account (default : query account)")
|
||||
parser.add_argument(
|
||||
'--gasprice', help="Network gas price (default : query network)")
|
||||
parser.add_argument('--startgas', help="startgas", default='80000')
|
||||
parser.add_argument('--startgas-delta', help="difference applied to startgas if gasprice is automatically fetched", default='1000')
|
||||
parser.add_argument('--amount', help="Amount to send in ether (default : query amount, use maximum)")
|
||||
parser.add_argument('--to', help="BIP 32 destination path (default : default ETC path)")
|
||||
parser.add_argument('--split-to-eth', help="Split to the ETH chain (default : spit to ETC chain)", action='store_true')
|
||||
parser.add_argument('--path', help="BIP 32 path to sign with (default : default ETH path)")
|
||||
parser.add_argument('--broadcast', help="Broadcast generated transaction (default : false)", action='store_true')
|
||||
parser.add_argument('--startgas-delta',
|
||||
help="difference applied to startgas if gasprice is automatically fetched", default='1000')
|
||||
parser.add_argument(
|
||||
'--amount', help="Amount to send in ether (default : query amount, use maximum)")
|
||||
parser.add_argument(
|
||||
'--to', help="BIP 32 destination path (default : default ETC path)")
|
||||
parser.add_argument(
|
||||
'--split-to-eth', help="Split to the ETH chain (default : spit to ETC chain)", action='store_true')
|
||||
parser.add_argument(
|
||||
'--path', help="BIP 32 path to sign with (default : default ETH path)")
|
||||
parser.add_argument(
|
||||
'--broadcast', help="Broadcast generated transaction (default : false)", action='store_true')
|
||||
args = parser.parse_args()
|
||||
|
||||
if args.path == None:
|
||||
if args.split_to_eth: #sign from ETC
|
||||
#args.path = "44'/60'/160720'/0'/0"
|
||||
args.path = "44'/60'/0'/0"
|
||||
else: #sign from ETH
|
||||
args.path = "44'/60'/0'/0"
|
||||
if args.split_to_eth: # sign from ETC
|
||||
#args.path = "44'/60'/160720'/0'/0"
|
||||
args.path = "44'/60'/0'/0"
|
||||
else: # sign from ETH
|
||||
args.path = "44'/60'/0'/0"
|
||||
|
||||
if args.to == None:
|
||||
if args.split_to_eth: #target ETH
|
||||
args.to = "44'/60'/0'/0"
|
||||
else: #target ETC transitional
|
||||
args.to = "44'/60'/160720'/0'/0"
|
||||
if args.split_to_eth: # target ETH
|
||||
args.to = "44'/60'/0'/0"
|
||||
else: # target ETC transitional
|
||||
args.to = "44'/60'/160720'/0'/0"
|
||||
|
||||
dongle = getDongle(True)
|
||||
|
||||
donglePath = parse_bip32_path(args.to)
|
||||
apdu = "e0060000".decode('hex') + chr(len(donglePath) + 1) + chr(len(donglePath) / 4) + donglePath
|
||||
apdu = "e0060000".decode('hex') + chr(len(donglePath) + 1) + \
|
||||
chr(len(donglePath) / 4) + donglePath
|
||||
dongle.exchange(bytes(apdu))
|
||||
|
||||
apdu = "e0020000".decode('hex') + chr(len(donglePath) + 1) + chr(len(donglePath) / 4) + donglePath
|
||||
apdu = "e0020000".decode('hex') + chr(len(donglePath) + 1) + \
|
||||
chr(len(donglePath) / 4) + donglePath
|
||||
result = dongle.exchange(bytes(apdu))
|
||||
publicKey = str(result[1 : 1 + result[0]])
|
||||
publicKey = str(result[1: 1 + result[0]])
|
||||
encodedPublicKey = sha3(publicKey[1:])[12:]
|
||||
|
||||
if (args.nonce == None) or (args.amount == None):
|
||||
donglePathFrom = parse_bip32_path(args.path)
|
||||
apdu = "e0020000".decode('hex') + chr(len(donglePathFrom) + 1) + chr(len(donglePathFrom) / 4) + donglePathFrom
|
||||
result = dongle.exchange(bytes(apdu))
|
||||
publicKeyFrom = str(result[1 : 1 + result[0]])
|
||||
encodedPublicKeyFrom = sha3(publicKeyFrom[1:])[12:]
|
||||
donglePathFrom = parse_bip32_path(args.path)
|
||||
apdu = "e0020000".decode('hex') + chr(len(donglePathFrom) + 1) + \
|
||||
chr(len(donglePathFrom) / 4) + donglePathFrom
|
||||
result = dongle.exchange(bytes(apdu))
|
||||
publicKeyFrom = str(result[1: 1 + result[0]])
|
||||
encodedPublicKeyFrom = sha3(publicKeyFrom[1:])[12:]
|
||||
|
||||
|
||||
http = None
|
||||
if (args.gasprice == None) or (args.nonce == None) or (args.amount == None) or (args.broadcast):
|
||||
http = requests.session()
|
||||
http = requests.session()
|
||||
|
||||
if args.gasprice == None:
|
||||
print "Fetching gas price"
|
||||
result = rpc_call(http, "https://api.etherscan.io/api?module=proxy&action=eth_gasPrice", "gasPrice")
|
||||
args.gasprice = int(result['result'], 16)
|
||||
print "Gas price " + str(args.gasprice)
|
||||
print "Fetching gas price"
|
||||
result = rpc_call(
|
||||
http, "https://api.etherscan.io/api?module=proxy&action=eth_gasPrice", "gasPrice")
|
||||
args.gasprice = int(result['result'], 16)
|
||||
print "Gas price " + str(args.gasprice)
|
||||
|
||||
if args.nonce == None:
|
||||
print "Fetching nonce"
|
||||
result = rpc_call(http, "https://api.etherscan.io/api?module=proxy&action=eth_getTransactionCount&address=0x" + encodedPublicKeyFrom.encode('hex'), "getTransactionCount")
|
||||
args.nonce = int(result['result'], 16)
|
||||
print "Nonce for 0x" + encodedPublicKeyFrom.encode('hex') + " " + str(args.nonce)
|
||||
print "Fetching nonce"
|
||||
result = rpc_call(http, "https://api.etherscan.io/api?module=proxy&action=eth_getTransactionCount&address=0x" +
|
||||
encodedPublicKeyFrom.encode('hex'), "getTransactionCount")
|
||||
args.nonce = int(result['result'], 16)
|
||||
print "Nonce for 0x" + \
|
||||
encodedPublicKeyFrom.encode('hex') + " " + str(args.nonce)
|
||||
|
||||
if args.amount == None:
|
||||
print "Fetching balance"
|
||||
result = rpc_call(http, "https://api.etherscan.io/api?module=account&action=balance&address=0x" + encodedPublicKeyFrom.encode('hex'), "getBalance")
|
||||
amount = int(result['result'])
|
||||
print "Balance for " + encodedPublicKeyFrom.encode('hex') + " " + str(amount)
|
||||
amount -= (int(args.startgas) - int(args.startgas_delta)) * int(args.gasprice)
|
||||
if amount < 0:
|
||||
raise Exception("Remaining amount too small to pay for contract fees")
|
||||
print "Fetching balance"
|
||||
result = rpc_call(http, "https://api.etherscan.io/api?module=account&action=balance&address=0x" +
|
||||
encodedPublicKeyFrom.encode('hex'), "getBalance")
|
||||
amount = int(result['result'])
|
||||
print "Balance for " + \
|
||||
encodedPublicKeyFrom.encode('hex') + " " + str(amount)
|
||||
amount -= (int(args.startgas) - int(args.startgas_delta)) * \
|
||||
int(args.gasprice)
|
||||
if amount < 0:
|
||||
raise Exception("Remaining amount too small to pay for contract fees")
|
||||
else:
|
||||
amount = Decimal(args.amount) * 10**18
|
||||
amount = Decimal(args.amount) * 10**18
|
||||
|
||||
print "Amount transferred " + str((Decimal(amount) / 10 ** 18)) + " to " + encodedPublicKey.encode('hex')
|
||||
print "Amount transferred " + \
|
||||
str((Decimal(amount) / 10 ** 18)) + " to " + encodedPublicKey.encode('hex')
|
||||
|
||||
txData = SPLIT_CONTRACT_FUNCTION
|
||||
txData += "\x00" * 31
|
||||
if (args.split_to_eth):
|
||||
txData += "\x01"
|
||||
txData += "\x01"
|
||||
else:
|
||||
txData += "\x00"
|
||||
txData += "\x00"
|
||||
txData += "\x00" * 12
|
||||
txData += encodedPublicKey
|
||||
|
||||
@@ -150,7 +172,8 @@ tx = Transaction(
|
||||
encodedTx = encode(tx, UnsignedTransaction)
|
||||
|
||||
donglePath = parse_bip32_path(args.path)
|
||||
apdu = "e0040000".decode('hex') + chr(len(donglePath) + 1 + len(encodedTx)) + chr(len(donglePath) / 4) + donglePath + encodedTx
|
||||
apdu = "e0040000".decode('hex') + chr(len(donglePath) + 1 +
|
||||
len(encodedTx)) + chr(len(donglePath) / 4) + donglePath + encodedTx
|
||||
|
||||
result = dongle.exchange(bytes(apdu))
|
||||
|
||||
@@ -158,11 +181,13 @@ v = result[0]
|
||||
r = int(str(result[1:1 + 32]).encode('hex'), 16)
|
||||
s = int(str(result[1 + 32: 1 + 32 + 32]).encode('hex'), 16)
|
||||
|
||||
tx = Transaction(tx.nonce, tx.gasprice, tx.startgas, tx.to, tx.value, tx.data, v, r, s)
|
||||
tx = Transaction(tx.nonce, tx.gasprice, tx.startgas,
|
||||
tx.to, tx.value, tx.data, v, r, s)
|
||||
serializedTx = encode(tx)
|
||||
|
||||
print "Signed transaction " + serializedTx.encode('hex')
|
||||
|
||||
if (args.broadcast):
|
||||
result = rpc_call(http, "https://api.etherscan.io/api?module=proxy&action=eth_sendRawTransaction&hex=0x" + serializedTx.encode('hex'), "sendRawTransaction")
|
||||
print result
|
||||
result = rpc_call(http, "https://api.etherscan.io/api?module=proxy&action=eth_sendRawTransaction&hex=0x" +
|
||||
serializedTx.encode('hex'), "sendRawTransaction")
|
||||
print result
|
||||
|
||||
Reference in New Issue
Block a user