Пример #1
0
def decode_raw_tx(tx_hex):
    tx = TX()
    tx.hex = tx_hex

    tx.version = parse_element(tx, 4)
    tx.inputs = parse_varint(tx)

    for i in range(decode_varint(tx.inputs)):
        tx.prev_tx_id.append(parse_element(tx, 32))
        tx.prev_out_index.append(parse_element(tx, 4))
        tx.scriptSig_len.append(parse_varint(tx))
        tx.scriptSig.append(
            parse_element(tx, decode_varint(tx.scriptSig_len[i])))
        tx.nSequence.append(parse_element(tx, 4))

    tx.outputs = parse_varint(tx)

    for i in range(decode_varint(tx.outputs)):
        tx.value.append(parse_element(tx, 8))
        tx.scriptPubKey_len.append(parse_varint(tx))
        tx.scriptPubKey.append(
            parse_element(tx, decode_varint(tx.scriptPubKey_len[i])))

    tx.nLockTime = parse_element(tx, 4)

    assert tx.offset == len(tx.hex)

    tx.print_elements()
Пример #2
0
 def send_currents(self, currents, joint_id=None):
     tx = TX()
     if joint_id == None:
         for joint_id in range(MAX_JOINT_NUM):
             tx.joints[joint_id].joint_order = ORDER_CURRENT
             tx.joints[joint_id].value1 = currents[joint_id]
     else:
         tx.joints[joint_id].joint_order = ORDER_CURRENT
         tx.joints[joint_id].value1 = currents[0]
     self.send_with_confirmation(tx)
Пример #3
0
 def send(self, order, joint_id=None, value1=0.0, value2=0.0):
     tx = TX()
     if joint_id == None:
         for joint_id in range(MAX_JOINT_NUM):
             tx.joints[joint_id].joint_order = order
             tx.joints[joint_id].value1 = value1
             tx.joints[joint_id].value2 = value2
     else:
         tx.joints[joint_id].joint_order = order
         tx.joints[joint_id].value1 = value1
         tx.joints[joint_id].value2 = value2
     self.send_with_confirmation(tx)
Пример #4
0
 def send_points(self, positions, duration, joint_id=None):
     tx = TX()
     if joint_id == None:
         for joint_id in range(MAX_JOINT_NUM):
             tx.joints[joint_id].joint_order = ORDER_TRAJ_VIA_APPEND_PT_SIN
             tx.joints[joint_id].value1 = positions[joint_id]
             tx.joints[joint_id].value2 = duration
     else:
         tx.joints[joint_id].joint_order = ORDER_TRAJ_VIA_APPEND_PT_SIN
         tx.joints[joint_id].value1 = positions[0]
         tx.joints[joint_id].value2 = duration
     self.send_with_confirmation(tx)
Пример #5
0
def build_raw_tx(prev_tx_id,
                 prev_out_index,
                 src_btc_addr,
                 value,
                 dest_btc_addr,
                 scriptPubKey=None,
                 scriptSig=None):

    assert len(prev_tx_id) == len(prev_out_index) == len(src_btc_addr)
    assert len(value) == len(dest_btc_addr)

    if scriptPubKey is None:
        scriptPubKey = []
        for i in range(len(dest_btc_addr)):
            scriptPubKey.append(generate_std_scriptpubkey(dest_btc_addr[i]))
    else:
        assert len(scriptPubKey) == len(dest_btc_addr)

    tx = TX()

    if scriptSig is None:
        tx.build_p2pkh_std_tx(prev_tx_id, prev_out_index, value, scriptPubKey)
        raw_tx = tx.hex
        for i in range(len(src_btc_addr)):
            priv_key = src_btc_addr[i] + "/sk.pem"
            priv_key_hex = get_priv_key_hex(priv_key)
            raw_tx = sign(raw_tx, i, priv_key_hex)

    else:
        assert len(scriptPubKey) == len(src_btc_addr)
        tx.build_p2pkh_std_tx(prev_tx_id, prev_out_index, value, scriptPubKey,
                              scriptSig)
        raw_tx = tx.hex

    return raw_tx
Пример #6
0
    def __init__(self, platform):
        """
        we: Write button. When is asserted, all data and data type from 
        memory is written into the FIFO. 
        link_ready: When is asserted, the transmision starts.
        trans_en: When is asserted, the transmision of data received in
        RX to the PC via UART starts.
        reset: General reset
        """
        self.we = Signal()
        self.link_ready = Signal()
        self.trans_en = Signal()
        self.reset = Signal()
        #   #   #
        self.reset = platform.request("reset")

        write_clk = Signal()  #clock that drives fifo writing
        write_clk_bufg = Signal()
        write_clk_pads = platform.request("write_clk")  #differential clock
        self.specials += [
            Instance("IBUFGDS",
                     i_I=write_clk_pads.p,
                     i_IB=write_clk_pads.n,
                     o_O=write_clk_bufg),
            Instance("BUFG", i_I=write_clk_bufg, o_O=write_clk)
        ]
        self.clock_domains.cd_write = ClockDomain(
            name="write")  #fifo writing clock domain

        gtp_clk = Signal()  #gtp reference clk
        gtp_clk_bufg = Signal()
        gtp_clk_freq = 240e6
        gtp_clk_pads = platform.request("gtp_clk")
        self.specials += [
            Instance("IBUFDS_GTE2",
                     i_CEB=0,
                     i_I=gtp_clk_pads.p,
                     i_IB=gtp_clk_pads.n,
                     o_O=gtp_clk_bufg),
            Instance("BUFG", i_I=gtp_clk_bufg, o_O=gtp_clk)
        ]
        self.submodules.crg = CRG(gtp_clk,
                                  rst=self.reset)  #tx_init clock region
        self.comb += []
        qpll = GTPQuadPLL(gtp_clk, gtp_clk_freq, 4.8e9)
        print(qpll)
        self.submodules += qpll

        tx_pads = platform.request("gtp_tx")
        rx_pads = platform.request("gtp_rx")
        gtp = GTP(qpll, tx_pads, rx_pads, gtp_clk_freq)
        self.submodules += gtp

        self.we = platform.request("we")
        self.link_ready = platform.request("link_ready")
        self.trans_en = platform.request("trans_en")

        tx = TX()
        tx = ClockDomainsRenamer("tx")(tx)
        rx = RX()
        rx = ClockDomainsRenamer("tx")(rx)
        fifo = AsyncFIFO(width=32, depth=32)
        fifo = ClockDomainsRenamer({"read": "tx"})(fifo)

        self.submodules += [fifo, tx, rx]

        generator_sel = 0  #1 for memory, 0 for PRBS
        n_data = 6  #number of data generated by the PRBS
        if generator_sel:
            memory = mem()
            memory = ClockDomainsRenamer("write")(memory)
            self.submodules += memory
            index = Signal(5)  #Memory index
            write_fifo = Signal()  #fifo write enable
            #FSM to control the FIFO writing process
            self.submodules.memory_fsm = FSM(reset_state="INIT")
            self.memory_fsm = ClockDomainsRenamer("write")(self.memory_fsm)
            self.memory_fsm.act(
                "INIT",
                If(
                    self.we,
                    NextValue(index, 1),
                    NextState("WRITING"),
                    NextValue(write_fifo, 1),
                ))
            self.memory_fsm.act(
                "WRITING",
                NextValue(index, index + 1),
                If(
                    index == (memory.n_value),  #Last word reached
                    NextValue(index, 0),
                    NextState("WRITING_EOP"),
                    NextValue(write_fifo, 0)))
            self.memory_fsm.act(
                "WRITING_EOP",
                NextState("IDLE"),
            )
            self.memory_fsm.act(
                "IDLE",
                If(
                    ~self.we,  #Process starts again
                    NextState("INIT")))
            self.comb += [
                fifo.din.eq(memory.data_out),
                fifo.dtin.eq(memory.type_out),
                fifo.we.eq(write_fifo),
                memory.index.eq(index)
            ]

        else:
            prbs = PRBSGenerator(n_out=32)
            prbs = ClockDomainsRenamer("write")(prbs)
            self.submodules += prbs
            prbs_en = Signal()
            data_type = Signal(2)
            index = Signal(max=n_data + 2)
            i_ignored = Signal(max=n_data - 1)
            write_fifo = Signal()
            self.comb += i_ignored.eq(random.randint(2, n_data - 1))
            self.submodules.prbs_fsm = FSM(reset_state="INIT")
            self.prbs_fsm = ClockDomainsRenamer("write")(self.prbs_fsm)
            self.prbs_fsm.act(
                "INIT",
                If(self.we, NextValue(prbs_en, 1), NextValue(data_type, 1),
                   NextValue(write_fifo, 1), NextState("SOP")))
            self.prbs_fsm.act("SOP", NextValue(data_type, 0),
                              NextValue(index, index + 1),
                              NextState("MIDDLE_WORD"))

            self.prbs_fsm.act(
                "MIDDLE_WORD",
                If(
                    index < n_data,
                    If(index == i_ignored - 1,
                       NextValue(data_type,
                                 0b11)).Else(NextValue(data_type, 0b00)),
                    NextValue(index, index + 1),
                    NextState("MIDDLE_WORD")).Else(NextState("EOP"),
                                                   NextValue(data_type, 0b10),
                                                   NextValue(index, 0)))
            self.prbs_fsm.act("EOP", NextValue(prbs_en, 0),
                              NextValue(write_fifo, 0),
                              NextState("WAITS_RESET"))
            self.prbs_fsm.act("WAITS_RESET", If(~self.we, NextState("INIT")))

            self.comb += [
                fifo.din.eq(prbs.o),
                fifo.dtin.eq(data_type),
                prbs.enable.eq(prbs_en),
                fifo.we.eq(write_fifo),
            ]

        self.rxinit_done = Signal()
        #rx_init_done signal goes to tb only for simulation purposes
        self.rxinit_done = platform.request("rxinit_done")

        self.comb += [
            gtp.reset.eq(self.reset),
            fifo.re.eq(tx.fifo_re),
            tx.link_ready.eq(self.link_ready),
            tx.fifo_empty.eq(~fifo.readable),
            tx.tx_init_done.eq(gtp.tx_init_done),
            tx.pll_lock.eq(gtp.pll_lock),
            If(
                (self.link_ready & fifo.readable),
                tx.data_type_in.eq(fifo.dtout),
                tx.data_in.eq(fifo.dout),
            ),
            gtp.tx_data.eq(tx.data_out),
            rx.data_in.eq(gtp.rx_data),
            rx.aligned.eq(gtp.rxbytealigned),
            rx.rx_init_done.eq(gtp.rxinit_done),
            rx.pll_lock.eq(gtp.pll_lock),
            rx.trans_en.eq(self.trans_en),
            self.rxinit_done.eq(gtp.rxinit_done),
            #self.rxinit_done.eq(gtp.tx_init_done),
            self.cd_write.clk.eq(write_clk),
            self.cd_write.rst.eq(self.reset)
        ]
Пример #7
0
def escrow_example():
    '''
    P2SH Address:
    2MzDcMCxcZnNnAZzFqsyuyZ4vuNCB6Qjvxd

    Funding tx:
    db1b045ea09581a51a5b5f851e7e3a64542123885e071fd6d4ff7428703a2504
    Spending tx nlocktime = 30:

    Refund TX:
    23daa9d3868255bab14d5dfe46f7fb787aaef49b17d0014902e36e4491440311
    '''
    SelectParams('testnet')

    tx = TX(test=True)

    print "=" * 50
    print("=" * 10 + "  ESCROW EXAMPLE")
    print "=" * 50 + "\n"

    # Setup keys
    data = open("./keys/EC_private_test.bin", "rb").read()
    alice = CECKey()
    alice.set_privkey(data)

    data = open("./keys/EC_private_test2.bin", "rb").read()
    bob = CECKey()
    bob.set_privkey(data)

    amount = 0.001  # In bitcoins

    redeem_script, p2sh_address = tx.setup_escrow(alice.get_pubkey(),
                                                  bob.get_pubkey(), amount, 30)

    # Funding tx id + redeeming bitcoin address
    funding_tx = "db1b045ea09581a51a5b5f851e7e3a6" + \
                 "4542123885e071fd6d4ff7428703a2504"
    address = "mweZnPjTeyGHVS2d3SojAGujY36sd3wQ49"

    # Note: Make sure pick correct vout - default is 0
    tx2, sighash = tx.get_tx(redeem_script,
                             address,
                             amount - 0.0001,
                             funding_tx,
                             20,
                             vout=1)

    # Sign
    alice_sig = alice.sign(sighash)
    bob_sig = bob.sign(sighash)

    print "P2SH %s" % p2sh_address
    print "Redeem script:\n%s\n" % binascii.hexlify(redeem_script)
    print "Redeem script Hash:\n%s\n" % binascii.hexlify(
        sha256(sha256(redeem_script)))

    redeem_tx = tx.spend_escrow(alice_sig, bob_sig, tx2, redeem_script)
    print "REDEEM TX is:\n%s\n" % binascii.hexlify(redeem_tx)

    refunded_tx = tx.refund_tx(alice_sig, tx2, redeem_script)
    print "REFUND TX is:\n%s\n" % binascii.hexlify(refunded_tx)
    print "=" * 50 + "\n\n"
Пример #8
0
def preimage_example():
    '''
    Refund test:
    P2SH Address:
    2MscMqe6Ag5NmZKCsELKDPJRJWnPR6GGD9B

    Funding tx:
    d49038dd9141f77c230208fe1cdd24937c61a1b63f40b8a87ab50971970ac2b7
    Spending tx nlocktime = 10:

    Refund TX:
    f77245db1c81b49c72464e61e3738a60f6e21c0bb744f8729def4a9877082e73

    Preimage test:
    P2SH Address:
    2NEFkj2gMZguX3QtFp31XKfnRdUpEoXTVNv

    Funding tx:
    91e6953fdcc15687ffc54e76d55ed4b92ef60cd483e0633ef226f7509513c7d2
    Spending tx nlocktime = 10:

    Spending TX:
    e006b7d1566045e136c13446114e51513f1453a7e2eb7e534d04bd7dc09532f7

    Other:
    1/
    P2SH Address: 2N6uq4bLT6UTqqxNo7YQ5TyRrFVWSRAFyxT
    Funding TX:
    8f717d1babd532b337cb80e743844f270129c744ebfc1ff7f6b43c1d855adc75
    Spending TX:
    b9cfbbdef00319db95c0beae8f73de4f7639eacc9b2006a70d64b494673921eb

    '''
    SelectParams('testnet')

    tx = TX(test=True)

    print "=" * 50
    print("=" * 10 + "  PREIMAGE EXAMPLE")
    print "=" * 50 + "\n"

    # Setup keys
    data = open("./keys/EC_private_test.bin", "rb").read()
    alice = CECKey()
    alice.set_privkey(data)

    data = open("./keys/EC_private_test2.bin", "rb").read()
    bob = CECKey()
    bob.set_privkey(data)

    preimages = [sha256("test" + str(x)) for x in range(1, 16)]
    hashes = [ripemd160(x) for x in preimages]

    # Serialize hashes
    serial_hashes = ""
    for i in range(len(hashes)):
        serial_hashes += hashes[i]
    tx.get_hashes_from_serial(serial_hashes, 15, 20)

    amount = 0.001  # In bitcoins
    redeem_script, funding_tx = tx.setup_preimage(alice.get_pubkey(),
                                                  bob.get_pubkey(), hashes,
                                                  amount, 10)

    # Funding tx id + redeeming bitcoin address
    funding_tx = "8f717d1babd532b337cb80e743844f270" + \
                 "129c744ebfc1ff7f6b43c1d855adc75"
    address = "mweZnPjTeyGHVS2d3SojAGujY36sd3wQ49"

    # Note: Make sure pick correct vout - default is 0
    tx2, sighash = tx.get_tx(redeem_script, address, amount - 0.0001,
                             funding_tx, 5)

    # Sign
    alice_sig = alice.sign(sighash)
    bob_sig = bob.sign(sighash)

    redeem_tx = tx.spend_preimage(preimages, bob_sig, tx2, redeem_script)
    print "REDEEM TX is:\n%s\n" % binascii.hexlify(redeem_tx)

    refunded_tx = tx.refund_tx(alice_sig, tx2, redeem_script)
    print "REFUND TX is:\n%s\n" % binascii.hexlify(refunded_tx)

    serial_keys = tx.get_keys_from_tx(redeem_tx)
    # print "SERIAL KEYS:\n%s\n" % binascii.hexlify(serial_keys)

    # # write to file to test in c++
    # target = open("keys.bin", 'wb')
    # target.write(serial_keys)
    # target.close()

    print "=" * 50 + "\n\n"