def get_outputs(self): """convert pubkeys to addresses""" o = [] for type, x, v in self.outputs(): if type & (TYPE_CLAIM | TYPE_UPDATE | TYPE_SUPPORT): x = x[1] if type & TYPE_ADDRESS: addr = x elif type & TYPE_PUBKEY: addr = public_key_to_bc_address(x.decode('hex')) else: addr = 'SCRIPT ' + x.encode('hex') o.append((addr, v)) # consider using yield (addr, v) return o
def can_sign_xpubkey(self, x_pubkey): if x_pubkey[0:2] in ['02', '03', '04']: addr = public_key_to_bc_address(x_pubkey.decode('hex')) return self.is_mine(addr) elif x_pubkey[0:2] == 'ff': xpub, sequence = BIP32_Account.parse_xpubkey(x_pubkey) return xpub in [ self.master_public_keys[k] for k in self.master_private_keys.keys() ] elif x_pubkey[0:2] == 'fd': addrtype = ord(x_pubkey[2:4].decode('hex')) addr = hash_160_to_bc_address(x_pubkey[4:].decode('hex'), addrtype) return self.is_mine(addr) else: raise BaseException("z")
def parse_xpub(x_pubkey): if x_pubkey[0:2] in ['02', '03', '04']: pubkey = x_pubkey elif x_pubkey[0:2] == 'ff': from account import BIP32_Account xpub, s = BIP32_Account.parse_xpubkey(x_pubkey) pubkey = BIP32_Account.derive_pubkey_from_xpub(xpub, s[0], s[1]) elif x_pubkey[0:2] == 'fd': addrtype = ord(x_pubkey[2:4].decode('hex')) hash160 = x_pubkey[4:].decode('hex') pubkey = None address = hash_160_to_bc_address(hash160, addrtype) else: raise BaseException("Cannnot parse pubkey") if pubkey: address = public_key_to_bc_address(pubkey.decode('hex')) return pubkey, address
def get_private_key_from_xpubkey(self, x_pubkey): if x_pubkey[0:2] in ['02', '03', '04']: addr = public_key_to_bc_address(x_pubkey.decode('hex')) if self.is_mine(addr): return self.get_private_key(addr)[0] elif x_pubkey[0:2] == 'ff': xpub, sequence = BIP32_Account.parse_xpubkey(x_pubkey) for k, v in self.master_public_keys.items(): if v == xpub: xprv = self.decoded_xprv if xprv: _, _, _, c, k = deserialize_xkey(xprv) return bip32_private_key(sequence, k, c) elif x_pubkey[0:2] == 'fd': addrtype = ord(x_pubkey[2:4].decode('hex')) addr = hash_160_to_bc_address(x_pubkey[4:].decode('hex'), addrtype) if self.is_mine(addr): return self.get_private_key(addr)[0] else: raise BaseException("z")
def add_transaction(self, tx_hash, tx): log.info("Adding tx: %s", tx_hash) is_coinbase = True if tx.inputs()[0].get('is_coinbase') else False # warring: 不能去掉dict temp_txi = dict(self.txi) temp_txo = dict(self.txo) with self.transaction_lock: # add inputs temp_txi[tx_hash] = d = {} for txi in tx.inputs(): addr = txi.get('address') if not txi.get('is_coinbase'): prevout_hash = txi['prevout_hash'] prevout_n = txi['prevout_n'] ser = prevout_hash + ':%d' % prevout_n if addr == "(pubkey)": addr = self.find_pay_to_pubkey_address( prevout_hash, prevout_n) # find value from prev output if addr and self.is_mine(addr): dd = temp_txo.get(prevout_hash, {}) for n, v, is_cb in dd.get(addr, []): if n == prevout_n: if d.get(addr) is None: d[addr] = [] d[addr].append((ser, v)) break else: self.pruned_txo[ser] = tx_hash # add outputs temp_txo[tx_hash] = d = {} for n, txo in enumerate(tx.outputs()): ser = tx_hash + ':%d' % n _type, x, v = txo if _type & (TYPE_CLAIM | TYPE_UPDATE | TYPE_SUPPORT): x = x[1] self.claimtrie_transactions[ser] = _type if _type & TYPE_ADDRESS: addr = x elif _type & TYPE_PUBKEY: addr = public_key_to_bc_address(x.decode('hex')) else: addr = None if addr and self.is_mine(addr): if d.get(addr) is None: d[addr] = [] d[addr].append((n, v, is_coinbase)) # give v to txi that spends me next_tx = self.pruned_txo.get(ser) if next_tx is not None: self.pruned_txo.pop(ser) dd = temp_txi.get(next_tx, {}) if dd.get(addr) is None: dd[addr] = [] dd[addr].append((ser, v)) # save important_print('>>>>>>>>>>>> add transactions', tx_hash) self.tx_transactions[tx_hash] = tx self.transactions[tx_hash] = str(tx) self.txi = temp_txi self.txo = temp_txo log.info("Saved transaction, txi and txo")
def pubkeys_to_address(self, pubkey): return public_key_to_bc_address(pubkey.decode('hex'))
def test_pubkey_to_addr(self): """Test pubkey to address """ pubkey = self._pubk.decode("hex") result = public_key_to_bc_address(pubkey) expected = self._addr self.assertEqual(expected, result)