Skip to content

Commit

Permalink
Fix ledger signing
Browse files Browse the repository at this point in the history
  • Loading branch information
learnforpractice committed Jun 10, 2022
1 parent e6162a9 commit 66a869a
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 85 deletions.
2 changes: 1 addition & 1 deletion pysrc/chainapi_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ async def generate_packed_transaction(self, actions, expiration, ref_block, chai
tx_json = tx.json()
for key in sign_keys:
index = indexes[ledger_pub_keys.index(key)]
signs = ledger.sign(self.chain_index, tx_json, [index], chain_id)
signs = ledger.sign(tx_json, [index], chain_id)
signatures |= set(signs)
packed_tx['signatures'] = list(signatures)
return json.dumps(packed_tx)
Expand Down
2 changes: 1 addition & 1 deletion pysrc/chainapi_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def generate_packed_transaction(self, actions, expiration, ref_block, chain_id,
tx_json = tx.json()
for key in sign_keys:
index = indexes[ledger_pub_keys.index(key)]
signs = ledger.sign(self.chain_index, tx_json, [index], chain_id)
signs = ledger.sign(tx_json, [index], chain_id)
signatures |= set(signs)
packed_tx['signatures'] = list(signatures)
return json.dumps(packed_tx)
Expand Down
155 changes: 76 additions & 79 deletions pysrc/eosBase.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,85 +287,82 @@ def parse_unknown(data):
return parameters

@staticmethod
def parse(chain_index, json):
try:
tx = Transaction(chain_index)
tx.json = json

tx.chain_id = binascii.unhexlify(json['chain_id'])

body = json['transaction']

# expiration = int(datetime.strptime(body['expiration'], '%Y-%m-%dT%H:%M:%S').strftime("%s"))
delta = datetime.strptime(body['expiration'], '%Y-%m-%dT%H:%M:%S') - datetime(1970, 1, 1)
expiration = int(delta.total_seconds())
tx.expiration = struct.pack('I', expiration)
tx.ref_block_num = struct.pack('H', body['ref_block_num'])
tx.ref_block_prefix = struct.pack('I', body['ref_block_prefix'])
tx.net_usage_words = struct.pack('B', body['max_net_usage_words'])
tx.max_cpu_usage_ms = struct.pack('B', body['max_cpu_usage_ms'])
tx.delay_sec = struct.pack('B', body['delay_sec'])

tx.ctx_free_actions_size = struct.pack('B', len(body['context_free_actions']))
tx.actions_size = struct.pack('B', len(body['actions']))

tx.actions = []
for action in body['actions']:
act = Action()
act.account = Transaction.name_to_number(action['account'])
act.name = Transaction.name_to_number(action['name'])

act.auth_size = struct.pack('B', len(action['authorization']))
act.auth = []
for auth in action['authorization']:
act.auth.append((Transaction.name_to_number(auth['actor']), Transaction.name_to_number(auth['permission'])))

data = action['data']
if isinstance(data, str):
parameters = bytes.fromhex(data)
elif action['name'] == 'transfer':
parameters = Transaction.parse_transfer(data)
elif action['name'] == 'voteproducer':
parameters = Transaction.parse_vote_producer(data)
elif action['name'] == 'buyram':
parameters = Transaction.parse_buy_ram(data)
elif action['name'] == 'buyrambytes':
parameters = Transaction.parse_buy_rambytes(data)
elif action['name'] == 'sellram':
parameters = Transaction.parse_sell_ram(data)
elif action['name'] == 'updateauth':
parameters = Transaction.parse_update_auth(data)
elif action['name'] == 'deleteauth':
parameters = Transaction.parse_delete_auth(data)
elif action['name'] == 'refund':
parameters = Transaction.parse_refund(data)
elif action['name'] == 'linkauth':
parameters = Transaction.parse_link_auth(data)
elif action['name'] == 'unlinkauth':
parameters = Transaction.parse_unlink_auth(data)
elif action['name'] == 'newaccount':
parameters = Transaction.parse_newaccount(data)
elif action['name'] == 'delegatebw':
parameters = Transaction.parse_delegate(data)
else:
parameters = Transaction.parse_unknown(data)

act.data_size = Transaction.pack_fc_uint(len(parameters))
act.data = parameters

tx.actions.append(act)

tx.tx_ext = struct.pack('B', len(body['transaction_extensions']))
tx.cfd = binascii.unhexlify('00' * 32)

for action in tx.actions:
sha = hashlib.sha256()
sha.update(action.data_size)
sha.update(action.data)

return tx
finally:
tx.free()
def parse(json):
tx = Transaction()
tx.json = json

tx.chain_id = binascii.unhexlify(json['chain_id'])

body = json['transaction']

# expiration = int(datetime.strptime(body['expiration'], '%Y-%m-%dT%H:%M:%S').strftime("%s"))
delta = datetime.strptime(body['expiration'], '%Y-%m-%dT%H:%M:%S') - datetime(1970, 1, 1)
expiration = int(delta.total_seconds())
tx.expiration = struct.pack('I', expiration)
tx.ref_block_num = struct.pack('H', body['ref_block_num'])
tx.ref_block_prefix = struct.pack('I', body['ref_block_prefix'])
tx.net_usage_words = struct.pack('B', body['max_net_usage_words'])
tx.max_cpu_usage_ms = struct.pack('B', body['max_cpu_usage_ms'])
tx.delay_sec = struct.pack('B', body['delay_sec'])

tx.ctx_free_actions_size = struct.pack('B', len(body['context_free_actions']))
tx.actions_size = struct.pack('B', len(body['actions']))

tx.actions = []
for action in body['actions']:
act = Action()
act.account = Transaction.name_to_number(action['account'])
act.name = Transaction.name_to_number(action['name'])

act.auth_size = struct.pack('B', len(action['authorization']))
act.auth = []
for auth in action['authorization']:
act.auth.append((Transaction.name_to_number(auth['actor']), Transaction.name_to_number(auth['permission'])))

data = action['data']
if isinstance(data, str):
parameters = bytes.fromhex(data)
elif action['name'] == 'transfer':
parameters = Transaction.parse_transfer(data)
elif action['name'] == 'voteproducer':
parameters = Transaction.parse_vote_producer(data)
elif action['name'] == 'buyram':
parameters = Transaction.parse_buy_ram(data)
elif action['name'] == 'buyrambytes':
parameters = Transaction.parse_buy_rambytes(data)
elif action['name'] == 'sellram':
parameters = Transaction.parse_sell_ram(data)
elif action['name'] == 'updateauth':
parameters = Transaction.parse_update_auth(data)
elif action['name'] == 'deleteauth':
parameters = Transaction.parse_delete_auth(data)
elif action['name'] == 'refund':
parameters = Transaction.parse_refund(data)
elif action['name'] == 'linkauth':
parameters = Transaction.parse_link_auth(data)
elif action['name'] == 'unlinkauth':
parameters = Transaction.parse_unlink_auth(data)
elif action['name'] == 'newaccount':
parameters = Transaction.parse_newaccount(data)
elif action['name'] == 'delegatebw':
parameters = Transaction.parse_delegate(data)
else:
parameters = Transaction.parse_unknown(data)

act.data_size = Transaction.pack_fc_uint(len(parameters))
act.data = parameters

tx.actions.append(act)

tx.tx_ext = struct.pack('B', len(body['transaction_extensions']))
tx.cfd = binascii.unhexlify('00' * 32)

for action in tx.actions:
sha = hashlib.sha256()
sha.update(action.data_size)
sha.update(action.data)

return tx

def encode(self):
encoder = Encoder()
Expand Down
8 changes: 4 additions & 4 deletions pysrc/ledger.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ def close_dongle():
dongle.close()
dongle = None

def sign_by_index(chain_index, obj, index):
def sign_by_index(obj, index):
donglePath = parse_bip32_path(f"44'/194'/0'/0/{index}")
pathSize = len(donglePath) // 4

tx = Transaction.parse(chain_index, obj)
tx = Transaction.parse(obj)
tx_chunks = tx.encode2()
first = True
dongle = get_dongle()
Expand Down Expand Up @@ -78,7 +78,7 @@ def sign_by_index(chain_index, obj, index):
result = 'SIG_K1_' + base58.b58encode(result).decode()
return result

def sign(chain_index, tx, indexes, chain_id):
def sign(tx, indexes, chain_id):
if isinstance(tx, str):
tx = json.loads(tx)
#TODO: verify public keys with indexes
Expand All @@ -88,7 +88,7 @@ def sign(chain_index, tx, indexes, chain_id):
}
signatures = []
for index in indexes:
signature = sign_by_index(chain_index, obj, index)
signature = sign_by_index(obj, index)
signatures.append(signature)
signatures.sort()
return signatures
Expand Down

0 comments on commit 66a869a

Please sign in to comment.