def scanForBip32(self, account, address, starting_spath=0, spath_count=10, isTestnet=False): found = False spath = -1 printOK("Scanning for Bip32 path of address: %s" % address) for i in range(starting_spath, starting_spath+spath_count): curr_path = MPATH + "%d'/0/%d" % (account, i) printDbg("checking path... %s" % curr_path) self.lock.acquire() try: if not isTestnet: curr_addr = self.chip.getWalletPublicKey(curr_path).get('address')[12:-2] else: pubkey = compress_public_key(self.chip.getWalletPublicKey(curr_path).get('publicKey')).hex() curr_addr = pubkey_to_address(pubkey, isTestnet) if curr_addr == address: found = True spath = i break sleep(0.01) except Exception as e: err_msg = 'error in scanForBip32' printException(getCallerName(), getFunctionName(), err_msg, e.args) finally: self.lock.release() return (found, spath)
def test_pubkey_to_address(self): # generate random private key and convert to public randomPubKey = bitcoin.privkey_to_pubkey(generate_privkey()) # compute address randomQmcAddr = pubkey_to_address(randomPubKey) # check leading char 'D' self.assertEqual(randomQmcAddr[0], 'D') # decode and verify checksum randomQmcAddr_bin = bytes.fromhex(b58decode(randomQmcAddr).hex()) randomQmcAddr_bin_check = bitcoin.bin_dbl_sha256( randomQmcAddr_bin[0:-4])[0:4] self.assertEqual(randomQmcAddr_bin[-4:], randomQmcAddr_bin_check)
def test_checkQmcAddr(self): # Generate Valid QMC address pK = privkey_to_pubkey(generate_privkey()) qmcAddr = pubkey_to_address(pK) # Check valid address self.assertTrue(checkQmcAddr(qmcAddr)) # Check malformed address 1: change leading char qmcAddr2 = self.getRandomChar() + qmcAddr[1:] while qmcAddr2[0] == 'D': qmcAddr2 = self.getRandomChar() + qmcAddr[1:] self.assertFalse(checkQmcAddr(qmcAddr2)) # Check malformed address 1: add random chars qmcAddr3 = qmcAddr for _ in range(10): qmcAddr3 += self.getRandomChar() self.assertFalse(checkQmcAddr(qmcAddr3))
def test_compose_tx_locking_script(self): # check with P2PKH addresses # Generate Valid QMC address pK = privkey_to_pubkey(generate_privkey()) qmcAddr = pubkey_to_address(pK) # compose TX script result = compose_tx_locking_script(qmcAddr) print(result) # check OP_DUP self.assertEqual(result[0], int('76', 16)) # check OP_HASH160 self.assertEqual(result[1], int('A9', 16)) pubkey_hash = bytearray.fromhex(b58check_to_hex(qmcAddr)) self.assertEqual(result[2], len(pubkey_hash)) self.assertEqual(result[3:23], pubkey_hash) # check OP_QEUALVERIFY self.assertEqual(result[23], int('88', 16)) # check OP_CHECKSIG self.assertEqual(result[24], int('AC', 16))
def scanForAddress(self, account, spath, isTestnet=False): printOK("Scanning for Address n. %d on account n. %d" % (spath, account)) curr_path = MPATH + "%d'/0/%d" % (account, spath) self.lock.acquire() try: if not isTestnet: curr_addr = self.chip.getWalletPublicKey(curr_path).get('address')[12:-2] else: pubkey = compress_public_key(self.chip.getWalletPublicKey(curr_path).get('publicKey')).hex() curr_addr = pubkey_to_address(pubkey, isTestnet) except Exception as e: err_msg = 'error in scanForAddress' printException(getCallerName(), getFunctionName(), err_msg, e.args) return None finally: self.lock.release() return curr_addr
def finalizeStartMessage_end(self, text): # decode message ret = self.caller.rpcClient.decodemasternodebroadcast(text) # find masternode in list and check for mnode in self.mnode_list: ip_addr = mnode.ip + ":" + mnode.port if ret['addr'] == ip_addr: # check ping signature ping_sig = b64encode(text[638:768]) self.assertEqual(ret['lastPing'].get('vchSig'), ping_sig) # check nLastDsq self.assertEqual(ret['nLastDsq'], 0) # check protocol version pv = self.rpcClient.getProtocolVersion() self.assertEqual(ret['protocolVersion'], pv) # check masternode pubkey1 self.assertEqual(ret['pubkey'], mnode['collateral'].get('address')) # check masternode pubkey2 pk2 = pubkey_to_address(privkey_to_pubkey(mnode.mnPrivKey)) self.assertEqual(ret['pubkey2'], pk2) # check masternode signature node_sig = b64encode(text[320:450]) self.assertEqual(ret['vchSig'], node_sig)