def sign_tx(self, input_count, input_txhash, input_amount, input_path, trx_amount, trx_addr): # padding to 8 bytes trx_amount_form = '0' * (16 - len(format(trx_amount, '02x'))) + format( trx_amount, '02x') signatures = [None] * input_count self.conn.change_apdu_CLA('81') # 1. Trx Finish TrxFinish(self.conn) # 2. Prep Trx Sign #------------------ prep_n times (input_amount, input_txhash)----------------------- prep_n = 0 while prep_n < input_count: input_path_n = input_path[prep_n] acc_id = input_path_n[2].to_bytes( 4, 'little').hex()[:-2] + '00' # 4 bytes keychain_id = input_path_n[3].to_bytes(1, 'little').hex() # 1 byte key_id = input_path_n[4].to_bytes(4, 'little').hex() # 4 byte: HEX Client_log.debug('Prep data : %s ', prep_n) Client_log.debug('Prep data [key path](ACC/KCID/KID): %s/%s/%s', acc_id, keychain_id, key_id) Client_log.debug('Prep data [input_amount]: %s', input_amount[prep_n]) Client_log.debug('Prep data [input_txhash]: %s', input_txhash[prep_n]) #------------------ check key point ------------------ if keychain_id == '00': # external KCID = '02' elif keychain_id == '01': # internal KCID = '03' key_point = analysisReData( self.conn.se_hdw_qry_acc_info(KCID, '00', acc_id)) key_point = int(key_point[:2], 16) Client_log.debug('[keychain_id]:%s, [key_point]:%s', keychain_id, key_point) Client_log.debug('Trx input key path [keyid]:%s', input_path_n[4]) if key_point < (input_path_n[4]): # GetNextPath to path point keydiff = input_path_n[4] - key_point for x in range(0, keydiff + 1): HDWNextTrxAddress(self.conn, keychain_id, acc_id) #-------------------------------------------------------- # padding to 8 bytes inputbalance = '0' * (16 - len(format(input_amount[prep_n], '02x')) ) + format(input_amount[prep_n], '02x') HDWPrepTrxSign(self.conn, self.hstCred, acc_id, keychain_id, key_id, "{:0>2d}".format(prep_n), inputbalance, input_txhash[prep_n]) prep_n += 1 #----------------------------------------------------------------------------------- # 3. Trx Begin Client_log.debug('Output data [trx_amount]: %s ', trx_amount_form) Client_log.debug('Output data [trx_addr]: %s ', trx_addr) TrxBegin(self.conn, self.hstCred, trx_amount_form, decode(trx_addr)) input( '\r\n[press the buttom on the card, then press enter to continue...]\r\n' ) self.conn.se_trx_status() self.conn.se_trx_status() # 4. Trx Sign sign_n = 0 while sign_n < input_count: sig = TrxSign(self.conn, "{:0>2d}".format(sign_n), self.hstCred) signatures[sign_n] = bytes.fromhex(sig) Client_log.debug('sign_n %s', sign_n) Client_log.debug('[signatures]: %s', signatures[sign_n].hex()) sign_n += 1 # 5. Trx Finish TrxFinish(self.conn) return signatures
def xpub_test_2_main(xpub: str) -> str: data = base58.decode(xpub) main_data = b"\x04\x88\xb2\x1e" + data[4:-4] checksum = base58.hash256(main_data)[0:4] return base58.encode(main_data + checksum)
def test_decoding(self): """Test base58 decoding""" for pair in TEST_VECTORS: decoded: bytes = base58.decode(pair[1]) self.assertEqual(decoded, unhexlify(pair[0]))