Skip to content

Commit

Permalink
Merge pull request #134 from LedgerHQ/load_app_callable_main
Browse files Browse the repository at this point in the history
Making the main fucntion of loadApp.py callable
  • Loading branch information
tjoly-ledger authored Apr 23, 2024
2 parents 7c228d2 + a4f0b93 commit e2ec856
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions ledgerblue/loadApp.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,22 +91,22 @@ def string_to_bytes(x):
return bytes(x, 'ascii')


if __name__ == '__main__':
def main(args, debug: bool = True):
from .ecWrapper import PrivateKey
from .comm import getDongle
from .hexParser import IntelHexParser, IntelHexPrinter
from .hexLoader import HexLoader
from .hexLoader import *
from .hexLoader import HexLoader, encodetlv, BOLOS_TAG_APPNAME, BOLOS_TAG_DERIVEPATH
from .deployed import getDeployedSecretV1, getDeployedSecretV2
import struct
import binascii

args = get_argparser().parse_args()
args = get_argparser().parse_args(args)

if args.rootPrivateKey == None:
privateKey = PrivateKey()
publicKey = binascii.hexlify(privateKey.pubkey.serialize(compressed=False))
print("Generated random root public key : %s" % publicKey)
if debug:
print("Generated random root public key : %s" % publicKey)
args.rootPrivateKey = privateKey.serialize()

args.appName = string_to_bytes(args.appName)
Expand Down Expand Up @@ -270,16 +270,25 @@ def close(self):

hash = loader.load(0x0, 0xF0, printer, targetId=args.targetId, targetVersion=args.targetVersion, doCRC=not (args.nocrc or NOCRC))

print("Application full hash : " + hash)
if debug:
print("Application full hash : " + hash)

if signature == None and args.signApp:
masterPrivate = PrivateKey(bytes(bytearray.fromhex(args.signPrivateKey)))
signature = masterPrivate.ecdsa_serialize(masterPrivate.ecdsa_sign(bytes(binascii.unhexlify(hash)), raw=True))
print("Application signature: " + str(binascii.hexlify(signature)))
if debug:
print("Application signature: " + str(binascii.hexlify(signature)))

if args.tlv:
loader.commit(signature)
else:
loader.run(args.bootAddr-printer.minAddr(), signature)

dongle.close()
return hash


if __name__ == '__main__':
import sys

sys.exit(main(sys.argv[1:]))

0 comments on commit e2ec856

Please sign in to comment.