def confirm_bitcoin_tx_online(self, hash, sender_gid, network): """ confirm bitcoin transaction using default online Samourai API instance Usage: confirm_bitcoin_tx tx_id gid network """ if network == 't' : url = "https://api.samourai.io/test/v2/tx/" + hash ## default testnet txtenna-server else : url = "https://api.samourai.io/v2/tx/" + hash ## default txtenna-server try: r = requests.get(url) ## print(r.text) while r.status_code != 200: sleep(60) # sleep for a minute r = requests.get(url) ## send zero-conf message back to tx sender rObj = TxTennaSegment('', '', tx_hash=hash, block=0) arg = str(sender_gid) + ' ' + rObj.serialize_to_json() self.do_send_private(arg) print("\nSent to GID: " + str(sender_gid) + ": Transaction " + hash + " added to the mempool.") r_text = "".join(r.text.split()) # remove whitespace obj = json.loads(r_text) while not 'block' in obj.keys(): sleep(60) # sleep for a minute r = requests.get(url) r_text = "".join(r.text.split()) obj = json.loads(r_text) ## send block height message back to tx sender blockheight = obj['block']['height'] rObj = TxTennaSegment('', '', tx_hash=hash, block=blockheight) arg = str(sender_gid) + ' ' + rObj.serialize_to_json() self.do_send_private(arg) print("\nSent to GID: " + str(sender_gid) + ": Transaction " + hash + " confirmed in block " + str(blockheight) + ".") except: traceback.print_exc()
def test_serialize_testnet_segment(self): segment = TxTennaSegment("1000", "w]8f<vRG}fayY4]vRG}fayYm#vRG}fayYnc", tx_hash="123abc", testnet=True, segment_count=2, sequence_num=1) json_ser = segment.serialize_to_json() assert json_ser == '{"i": "1000", "t": "w]8f<vRG}fayY4]vRG}fayYm#vRG}fayYnc", "c": 1, "n": "t"}'
def confirm_bitcoin_tx_local(self, hash, sender_gid): """ Confirm bitcoin transaction using local bitcoind instance Usage: confirm_bitcoin_tx tx_id gid """ ## send transaction to local bitcond segments = self.segment_storage.get_by_transaction_id(hash) raw_tx = self.segment_storage.get_raw_tx(segments) ## pass hex string converted to bytes try : proxy1 = bitcoin.rpc.Proxy() raw_tx_bytes = x(raw_tx) tx = CMutableTransaction.stream_deserialize(BytesIO(raw_tx_bytes)) r1 = proxy1.sendrawtransaction(tx) except : print("Invalid Transaction! Could not send to network.") return ## try for 30 minutes to confirm the transaction for n in range(0, 30) : try : proxy2 = bitcoin.rpc.Proxy() r2 = proxy2.getrawtransaction(r1, True) ## send zero-conf message back to tx sender confirmations = r2.get('confirmations', 0) rObj = TxTennaSegment('', '', tx_hash=hash, block=confirmations) arg = str(sender_gid) + ' ' + rObj.serialize_to_json() self.do_send_private(arg) print("\nSent to GID: " + str(sender_gid) + ": Transaction " + hash + " added to the mempool.") break except IndexError: ## tx_id not yet in the global mempool, sleep for a minute and then try again sleep(60) continue ## wait for atleast one confirmation for m in range(0, 30): sleep(60) # sleep for a minute try : proxy3= bitcoin.rpc.Proxy() r3 = proxy3.getrawtransaction(r1, True) confirmations = r3.get('confirmations', 0) ## keep waiting until 1 or more confirmations if confirmations > 0: break except : ## unknown RPC error, but keep trying traceback.print_exc() if confirmations > 0 : ## send confirmations message back to tx sender if confirmations > 0 rObj = TxTennaSegment('', '', tx_hash=hash, block=confirmations) arg = str(sender_gid) + ' ' + rObj.serialize_to_json() self.do_send_private(arg) print("\nSent to GID: " + str(sender_gid) + ", Transaction " + hash + " confirmed in " + str(confirmations) + " blocks.") else : print("\CTransaction from GID: " + str(sender_gid) + ", Transaction " + hash + " not confirmed after 30 minutes.")
def test_serialize_first_segment(self): segment = TxTennaSegment("1000", "w]8f<vRG}fayY4]vRG}fayYm#vRG}fayYnc", tx_hash="123abc", testnet=False, segment_count=1, sequence_num=0) json_ser = segment.serialize_to_json() assert json_ser == '{"i": "1000", "t": "w]8f<vRG}fayY4]vRG}fayYm#vRG}fayYnc", "s": 1, "h": "123abc"}'