Esempio n. 1
0
    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()
            arg = str(sender_gid) + " Transaction " + hash + " added to the mempool."
            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()
            # TODO: merkle proof for transaction
            arg = str(sender_gid) + " Transaction " + hash + " confirmed in block " + str(blockheight) + "."
            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_put_new_payload_out_of_order(self, segments):
     sg0 = TxTennaSegment("1005",
                          "the payload data for segment 0",
                          tx_hash="eeeeee",
                          sequence_num=0,
                          segment_count=3)
     sg1 = TxTennaSegment("1005",
                          "the payload data for segment 1",
                          sequence_num=1)
     segments.put(sg1)
     segments.put(sg0)
     gsg = segments.get("1005")
     gsg2 = segments.get_by_transaction_id("eeeeee")
     assert gsg2[0].payload_id == gsg[0].payload_id
     assert len(gsg) == 2
     assert gsg[0].sequence_num == 0
     assert gsg[1].sequence_num == 1
Esempio n. 3
0
 def test_constructor_defaults(self):
     segment = TxTennaSegment("1000", "w]8f<vRG}fayY4]vRG}fayYm#vRG}fayYnc")
     assert segment.tx_hash is None
     assert segment.payload_id == "1000"
     assert segment.payload == "w]8f<vRG}fayY4]vRG}fayYm#vRG}fayYnc"
     assert segment.sequence_num == 0
     assert not segment.testnet
     assert segment.segment_count is None
 def test_put_new_payload(self, segments):
     sg = TxTennaSegment("1004",
                         "the payload data for segment 0",
                         tx_hash="789fff",
                         sequence_num=0,
                         segment_count=3)
     segments.put(sg)
     gsg = segments.get("1004")
     assert gsg is not None
     assert gsg[0].tx_hash == "789fff"
def segments():
    payload1seg1 = TxTennaSegment("1000",
                                  "ra]?=rb3hXB09d)awc6WatLS8ir",
                                  tx_hash="abc123",
                                  sequence_num=0,
                                  segment_count=3,
                                  testnet=False)
    payload1seg2 = TxTennaSegment("1000",
                                  "lUG[Cv}xE)z/M$szxIn^x(mX",
                                  sequence_num=1,
                                  testnet=False)
    payload1seg3 = TxTennaSegment("1000",
                                  "z!pa<wN/T@wfsiEx(4ZgBrCglAV^XSzFKp",
                                  sequence_num=2,
                                  testnet=False)

    payload2seg1 = TxTennaSegment("1001",
                                  "ra]?=B9z5kz6iLlazts{wmYo]Bz(wiA+6klB-N",
                                  tx_hash="def456",
                                  sequence_num=0,
                                  segment_count=2,
                                  testnet=False)

    payload3seg2 = TxTennaSegment(
        "1002",
        "vS==nBzbkdxLzKfz/QaCz!pb7x<(9av%8*jwO#0(wPF#dB0b7qy?$kjw/$M6wNP7ZwPF#jw/#hevrrSlA=(CB",
        sequence_num=1,
        testnet=False)

    # payload3seg1 = "ra]?=v}xL1A:-=bvQTd&az(mxBrC47aARTDB97#7az#ataARJAwmYjUB7DFoxK@q@B-IIlzEW@y"

    storage = SegmentStorage()

    storage.put(payload1seg1)
    storage.put(payload1seg2)
    storage.put(payload1seg3)
    storage.put(payload2seg1)
    storage.put(payload3seg2)
    return storage
Esempio n. 6
0
    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.")
Esempio n. 7
0
    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"}'
Esempio n. 8
0
    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"}'
Esempio n. 9
0
    def test_repr(self):
        segment = TxTennaSegment("1000", "w]8f<vRG}fayY4]vRG}fayYm#vRG}fayYnc",
                                 tx_hash="123abc", testnet=False, segment_count=1, sequence_num=0)

        segment_repr = repr(segment)
        assert segment_repr == '{"i": "1000", "t": "w]8f<vRG}fayY4]vRG}fayYm#vRG}fayYnc", "s": 1, "h": "123abc"}'
Esempio n. 10
0
 def test_str(self):
     segment = TxTennaSegment("1000", "w]8f<vRG}fayY4]vRG}fayYm#vRG}fayYnc",
                              tx_hash="123abc", testnet=False, segment_count=1, sequence_num=0)
     segment_str = str(segment)
     assert segment_str == "Tx 123abc Part 0"