|
| 1 | +#!/usr/bin/env python |
| 2 | +""" |
| 3 | +******************************************************************************* |
| 4 | +* Ledger Ethereum App |
| 5 | +* (c) 2016-2019 Ledger |
| 6 | +* |
| 7 | +* Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | +* you may not use this file except in compliance with the License. |
| 9 | +* You may obtain a copy of the License at |
| 10 | +* |
| 11 | +* http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +* |
| 13 | +* Unless required by applicable law or agreed to in writing, software |
| 14 | +* distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | +* See the License for the specific language governing permissions and |
| 17 | +* limitations under the License. |
| 18 | +******************************************************************************** |
| 19 | +""" |
| 20 | +from __future__ import print_function |
| 21 | + |
| 22 | +from ledgerblue.comm import getDongle |
| 23 | +from ledgerblue.commException import CommException |
| 24 | +import argparse |
| 25 | +import struct |
| 26 | +import binascii |
| 27 | +import math |
| 28 | +import rlp |
| 29 | + |
| 30 | + |
| 31 | + |
| 32 | +def der_encode(value): |
| 33 | + value_bytes = value.to_bytes(max(1, (value.bit_length() + 7) // 8), 'big') |
| 34 | + if value >= 0x80: |
| 35 | + value_bytes = (0x80 | len(value_bytes)).to_bytes(1, 'big') + value_bytes |
| 36 | + return value_bytes |
| 37 | + |
| 38 | +def tlv_encode(tag, value): |
| 39 | + return der_encode(tag) + der_encode(len(value)) + value |
| 40 | + |
| 41 | +def parse_bip32_path(path): |
| 42 | + if len(path) == 0: |
| 43 | + return b"" |
| 44 | + result = b"" |
| 45 | + elements = path.split('/') |
| 46 | + for pathElement in elements: |
| 47 | + element = pathElement.split('\'') |
| 48 | + result = result + der_encode(0x01) + der_encode(0x04) |
| 49 | + if len(element) == 1: |
| 50 | + result = result + struct.pack(">I", int(element[0])) |
| 51 | + else: |
| 52 | + result = result + struct.pack(">I", 0x80000000 | int(element[0])) |
| 53 | + return result |
| 54 | + |
| 55 | + |
| 56 | +parser = argparse.ArgumentParser() |
| 57 | +parser.add_argument('--path', help="BIP 32 path to retrieve") |
| 58 | +parser.add_argument('--chainid', help="Chain ID", type=int, required=True) |
| 59 | +parser.add_argument('--nonce', help="Account Nonce", type=int, required=True) |
| 60 | +parser.add_argument('--delegate', help="Delegate address", type=str, required=True) |
| 61 | +args = parser.parse_args() |
| 62 | + |
| 63 | +if args.path == None: |
| 64 | + args.path = "44'/60'/0'/0/0" |
| 65 | + |
| 66 | +tmp = tlv_encode(0x00, struct.pack(">B", 0x01)) |
| 67 | +tmp += parse_bip32_path(args.path) |
| 68 | +data = binascii.unhexlify(args.delegate[2:]) |
| 69 | +tmp += tlv_encode(0x02, data) |
| 70 | +tmp += tlv_encode(0x03, struct.pack(">Q", args.chainid)) |
| 71 | +tmp += tlv_encode(0x04, struct.pack(">Q", args.nonce)) |
| 72 | + |
| 73 | +tmp = struct.pack(">H", len(tmp)) + tmp |
| 74 | + |
| 75 | +apdu = bytearray.fromhex("e0340100") |
| 76 | +apdu += struct.pack(">B", len(tmp)) |
| 77 | +apdu += tmp |
| 78 | + |
| 79 | +dongle = getDongle(True) |
| 80 | +result = dongle.exchange(bytes(apdu)) |
| 81 | + |
| 82 | +v = result[0] |
| 83 | +r = result[1 : 1 + 32] |
| 84 | +s = result[1 + 32 :] |
| 85 | + |
| 86 | +print("v = " + str(v)) |
| 87 | +print("r = " + binascii.hexlify(r).decode('utf-8')) |
| 88 | +print("s = " + binascii.hexlify(s).decode('utf-8')) |
| 89 | + |
| 90 | +rlpData = [ args.chainid, binascii.unhexlify(args.delegate[2:]), args.nonce, v, r, s ] |
| 91 | +print(binascii.hexlify(rlp.encode(rlpData)).decode('utf-8')) |
0 commit comments