Ejemplo n.º 1
0
 def test_tx_deserialize(self):
     tx, _ = TxSerializer().deserialize(
         decodehexstr(
             "01000000010e0907321e9fcb500c20eb658667b40ac721870ddc2ce165ab53a47b68cbc517000000008b483045022100ab2dc8932ca1d26f4cdac1feae09020a60ccc4d17b14e5fc5b21f3ab8c3a9cee022040a7208c172d19a19902280d66201c7fe2c3d8b2df7e23cc4e5b70fd52ecba2c014104b77dd1f3a21cb3d067a7e76982a609d7310f8692f5d61346f3225323c425604a0c12862755335c49e392673106adfc5dfdee1e4d367f10353e8911fac687db3effffffff02802385d1050000001976a9144d8b17fbce571614be89df4bd872de892a47984488ac00f2052a010000001976a914fadad27c40adbe230f5e3c04d44a29297508483188ac00000000"
         ))
     tx_expected = Tx(
         version=1,
         in_list=[
             TxIn(previous_output=Outpoint(hash=Uint256.from_hexstr(
                 "17c5cb687ba453ab65e12cdc0d8721c70ab4678665eb200c50cb9f1e3207090e"
             ),
                                           index=0),
                  script=Script([
                      Instruction(
                          72,
                          decodehexstr(
                              "3045022100ab2dc8932ca1d26f4cdac1feae09020a60ccc4d17b14e5fc5b21f3ab8c3a9cee022040a7208c172d19a19902280d66201c7fe2c3d8b2df7e23cc4e5b70fd52ecba2c01"
                          )),
                      Instruction(
                          65,
                          decodehexstr(
                              "04b77dd1f3a21cb3d067a7e76982a609d7310f8692f5d61346f3225323c425604a0c12862755335c49e392673106adfc5dfdee1e4d367f10353e8911fac687db3e"
                          ))
                  ]),
                  sequence=TxIn.NSEQUENCE_FINAL)
         ],
         out_list=[
             TxOut(value=24990000000,
                   script=Script([
                       Instruction(OP_DUP),
                       Instruction(OP_HASH160),
                       Instruction(
                           20,
                           decodehexstr(
                               "4d8b17fbce571614be89df4bd872de892a479844")),
                       Instruction(OP_EQUALVERIFY),
                       Instruction(OP_CHECKSIG)
                   ])),
             TxOut(value=5000000000,
                   script=Script([
                       Instruction(OP_DUP),
                       Instruction(OP_HASH160),
                       Instruction(
                           20,
                           decodehexstr(
                               "fadad27c40adbe230f5e3c04d44a292975084831")),
                       Instruction(OP_EQUALVERIFY),
                       Instruction(OP_CHECKSIG)
                   ]))
         ],
         locktime=0)
     self.assertEquals(tx, tx_expected)
Ejemplo n.º 2
0
 def setUp(self):
     self.script1 = Script([Instruction(OP_2),
                            Instruction(33, decodehexstr("02547b223d58eb5da7c7690748f70a3bab1509cb7578faac9032399f0b6bce31d6")),
                            Instruction(33, decodehexstr("c47a2dbbfb38f02205a3a72a37c68a8c068fe71a1351516f0f1fe7dd3c7afce38f")),
                            Instruction(33, decodehexstr("9f1de01000000fd45010047304402200983ab9c46fd1194e541c683828bbb92ce9")),
                            Instruction(OP_3),
                            Instruction(OP_CHECKMULTISIG)])
Ejemplo n.º 3
0
def sign_transaction_input(tx,
                           input_index,
                           txout_script,
                           intput_script_type,
                           secret,
                           compressed_pubkey=True):
    # Set all txin to empty
    txin_scripts_save = [txin.script for txin in tx.in_list]
    for txin in tx.in_list:
        txin.script = Script([])
    # Set the current input script to the outpoint's script value
    tx.in_list[input_index].script = txout_script
    # Serialize and append hash type
    enctx = TxSerializer().serialize(tx) + b"\x01\x00\x00\x00"
    # Get hash and Sign
    txhash = doublesha256(enctx)
    key = KEY()
    key.set_secret(secret, compressed_pubkey)
    signature = key.sign(txhash) + "\x01"  # append hash_type SIGHASH_ALL
    # Restore Txin scripts
    for txin, script_save in zip(tx.in_list, txin_scripts_save):
        txin.script = script_save
    # Set the signed script
    if intput_script_type == TX_PUBKEYHASH:
        tx.in_list[input_index].script = make_script_pubkeyhash_sig(
            key.get_pubkey(), signature)
    if intput_script_type == TX_PUBKEY:
        tx.in_list[input_index].script = make_script_pubkey_sig(signature)
Ejemplo n.º 4
0
 def rebuild_template(self):
     """Rebuild a version 2 blockheader and serialize it.
         
        oldest satoshi client coinbase script contains:
            nBits, bnExtraNonce (nBits = GetNextWorkRequired(pindexPrev);)
        later timestamp was used instead of "bits" to ensure coinbase txn is unique even if address is the same (github:83f4cd156e9d52bd7c4351336dfa4806a43ee4e4=.
        now in version 2 blocks the height is included instead of the timestamp.
     """
     coinbase_script = Script(
         [push_bignum_instruction(self.block_height)] +
         [push_bignum_instruction(self.extra_nonce)] +
         [push_data_instruction(flag) for flag in self.coinbase_flags])
     coinbase_txin = TxIn(Outpoint.null(),
                          coinbase_script,
                          sequence=TxIn.NSEQUENCE_FINAL)
     coinbase_tx = Tx(version=1,
                      in_list=[coinbase_txin],
                      out_list=self.coinbase_txout_list,
                      locktime=0)
     self.block_transactions = [coinbase_tx] + self.transactions
     self.blockheader = BlockHeader(version=2,
                                    hash_prev=self.hash_prev,
                                    hash_merkle=compute_merkle_root(
                                        self.block_transactions),
                                    time=self.time,
                                    bits=self.bits,
                                    nonce=self.nonce)
     self.serialized = self.serializer.serialize(self.blockheader)
Ejemplo n.º 5
0
 def get_script(self):
     return Script([
         Instruction(OP_DUP),
         Instruction(OP_HASH160),
         push_data_instruction(data=self.pubkey_hash),
         Instruction(OP_EQUALVERIFY),
         Instruction(OP_CHECKSIG)
     ])
Ejemplo n.º 6
0
    def test_sign_transaction(self):
        tx = Tx(version=1, 
                in_list=[TxIn(previous_output=Outpoint(hash=Uint256.from_hexstr("d2a42ebcb98b598ddcb6d430ce9061dba76804a97f7c1413dd3faef744f909a8"),index=0), 
                   script=Script(instructions=[]), 
                   sequence=4294967295),TxIn(previous_output=Outpoint(hash=Uint256.from_hexstr("2be8e319be1d15c1ba54708bdd7969b638e7e6fa2154819136e363ea6d33664a"),index=1), 
                   script=Script(instructions=[]), 
                   sequence=4294967295)], 
                out_list=[TxOut(value=3315075843, 
                    script=Script(instructions=[Instruction(OP_DUP),Instruction(OP_HASH160),Instruction(20,  decodehexstr("297c5a2ee31a1ab721115722d83f8654ca21d5df")),Instruction(OP_EQUALVERIFY),Instruction(OP_CHECKSIG)])),TxOut(value=2971972133, 
                    script=Script(instructions=[Instruction(OP_DUP),Instruction(OP_HASH160),Instruction(20,  decodehexstr("7d5feab86e31e8fc99d8e735d56226de9043e5fc")),Instruction(OP_EQUALVERIFY),Instruction(OP_CHECKSIG)])),TxOut(value=1046301823, 
                    script=Script(instructions=[Instruction(OP_DUP),Instruction(OP_HASH160),Instruction(20,  decodehexstr("fd91232a2fa0c389fa0188efde26c8a6165f4c50")),Instruction(OP_EQUALVERIFY),Instruction(OP_CHECKSIG)]))], 
                locktime=0)

        outscript0 = Script(instructions=[Instruction(33,  decodehexstr("03409fe679bdff9e801692c999b86d0c47b62dc02cdd10591ee70ca8056cd05023")),Instruction(OP_CHECKSIG)])
        outscript1 = Script(instructions=[Instruction(OP_DUP),Instruction(OP_HASH160),Instruction(20,  decodehexstr("5fb7fdb3d1ab0f3fec90b38417cca8ab736b10c6")),Instruction(OP_EQUALVERIFY),Instruction(OP_CHECKSIG)])

        # Sign TX_PUBKEY 
        sign_transaction(tx, 
                         [TxOut(None, outscript0), TxOut(None, outscript1)],
                         [decodehexstr("f006b27418527b1c400bbc434a3f22ee57c376bd4819cfe2a1162682788ae714"),
                          decodehexstr("c693115901c2840badd1e404706a4866a90d3afa16a542c1a3d0de9aad0875fe")])

        vm = TxValidationVM()
        valid, reason = vm.validate(tx, 0, outscript0, tx.in_list[0].script)
        if not valid:
            raise Exception(reason)
        valid, reason = vm.validate(tx, 1, outscript1, tx.in_list[1].script)
        if not valid:
            raise Exception(reason)
Ejemplo n.º 7
0
 def setUp(self):
     self.script1 = Script([
         Instruction(
             33,
             decodehexstr(
                 "02547b223d58eb5da7c7690748f70a3bab1509cb7578faac9032399f0b6bce31d6"
             )),
         Instruction(OP_CHECKSIG)
     ])
Ejemplo n.º 8
0
 def deserialize(self, data, pos=0):
     try:
         l = len(data)
         instructions = []
         while (pos < l):
             instr, pos = self.iser.deserialize(data, pos)
             instructions.append(instr)
         return Script(instructions, l)
     except:
         return RawScript(data)
Ejemplo n.º 9
0
def make_script_pubkeyhash(pubkey_hash):
    #Improve to take a BitcoinAddress?
    instructions = [
        Instruction(OP_DUP),
        Instruction(OP_HASH160),
        push_data_instruction(data=pubkey_hash),
        Instruction(OP_EQUALVERIFY),
        Instruction(OP_CHECKSIG)
    ]
    return Script(instructions)
Ejemplo n.º 10
0
 def test_varstr_script_serialize(self):
     script = Script([
         Instruction(OP_DUP),
         Instruction(OP_HASH160),
         Instruction(
             20, decodehexstr("fadad27c40adbe230f5e3c04d44a292975084831")),
         Instruction(OP_EQUALVERIFY),
         Instruction(OP_CHECKSIG)
     ])
     self.assertEquals(
         hexstr(VarstrScriptSerializer().serialize(script)),
         "1976a914fadad27c40adbe230f5e3c04d44a29297508483188ac")
Ejemplo n.º 11
0
def create_pubkeyhash_transaction(output_list, address, change_adress, amount,
                                  fee):
    amount_in = sum(txout.value for outpoint, txout in output_list)
    amount_change = int(amount_in - amount - fee)
    #assert amount_change > 0
    in_list = [
        TxIn(previous_output=outpoint, script=Script([]))
        for outpoint, txout in output_list
    ]
    out_list = [
        TxOut(value=amount, script=make_script_pubkeyhash(address)),
        TxOut(value=amount_change,
              script=make_script_pubkeyhash(change_adress))
    ]
    return Tx(version=1, in_list=in_list, out_list=out_list, locktime=0)
Ejemplo n.º 12
0
 def test_txout_serialize(self):
     txout = TxOut(
         value=24990000000,
         script=Script([
             Instruction(OP_DUP),
             Instruction(OP_HASH160),
             Instruction(
                 20,
                 decodehexstr("4d8b17fbce571614be89df4bd872de892a479844")),
             Instruction(OP_EQUALVERIFY),
             Instruction(OP_CHECKSIG)
         ]))
     self.assertEquals(
         hexstr(TxoutSerializer().serialize(txout)),
         "802385d1050000001976a9144d8b17fbce571614be89df4bd872de892a47984488ac"
     )
Ejemplo n.º 13
0
 def test_blockheader_template(self):
     """ Change nonce and extra_nonce in a blockheader_template """
     template = BlockheaderTemplate(
         Uint256.from_hexstr(
             "0009d8ab497a46a0d6a2b9b302993bd26613b145695d986be50e0b6e68c5b524"
         ),
         1,  # version 2 blocks see BIP-34
         [
             TxOut(
                 5000000000,
                 Script([
                     Instruction(
                         OP_PUSHDATA,
                         decodehexstr(
                             "04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f"
                         )),
                     Instruction(OP_CHECKSIG)
                 ]))
         ],
         [],
         time=1356447036,
         bits=524287999,
         nonce=0,
         extra_nonce=0,
         coinbase_flags=["/P2SH/"])
     self.assertEquals(
         hexstr(template.get_serialized()),
         "0200000024b5c5686e0b0ee56b985d6945b11366d23b9902b3b9a2d6a0467a49abd80900ad7af2ede803ff129e66775d56153a7a649721e524eecad69a437ecdc01e29343cbdd950ffff3f1f00000000"
     )
     template.set_nonce(8)
     self.assertEquals(
         hexstr(template.get_serialized()),
         "0200000024b5c5686e0b0ee56b985d6945b11366d23b9902b3b9a2d6a0467a49abd80900ad7af2ede803ff129e66775d56153a7a649721e524eecad69a437ecdc01e29343cbdd950ffff3f1f08000000"
     )
     template.set_nonce(492498294)
     self.assertEquals(
         hexstr(template.get_serialized()),
         "0200000024b5c5686e0b0ee56b985d6945b11366d23b9902b3b9a2d6a0467a49abd80900ad7af2ede803ff129e66775d56153a7a649721e524eecad69a437ecdc01e29343cbdd950ffff3f1f76ed5a1d"
     )
     template.set_extra_nonce(2)
     template.set_nonce(0)
     self.assertEquals(
         hexstr(template.get_serialized()),
         "0200000024b5c5686e0b0ee56b985d6945b11366d23b9902b3b9a2d6a0467a49abd80900e1cc38b530e20307a310a57a6e2e22985ce2e292bfc73b28a723dd77f8e8f0ca3cbdd950ffff3f1f00000000"
     )
Ejemplo n.º 14
0
    def test_mine_block(self):
        """ Mine a block changing both nonce and extra_nonce"""
        def nonce_changer(template):
            if template.nonce >= 20:
                template.set_extra_nonce(template.extra_nonce + 1)
                template.set_nonce(0)
            else:
                template.set_nonce(template.nonce + 1)

        time_source = MockTimeSource(time=1356446436)
        miner = BitcoinMiner()
        block, template = miner.mine_block(
            hash_prev=Uint256.from_hexstr(
                "0000000000000000000000000000000000000000000000000000000000000000"
            ),
            block_height=0,
            time_source=time_source,
            difficulty_bits=524287999,
            transactions=[],
            coinbase_txout_list=[
                TxOut(
                    5000000000,
                    Script([
                        Instruction(
                            OP_PUSHDATA,
                            decodehexstr(
                                "04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f"
                            )),
                        Instruction(OP_CHECKSIG)
                    ]))
            ],
            nonce_changer=nonce_changer)
        self.assertEquals(block.blockheader.nonce, 8)
        self.assertEquals(template.nonce, 8)
        self.assertEquals(template.extra_nonce, 14)
        self.assertEquals(
            block.blockheader.hash_merkle,
            Uint256.from_hexstr(
                "ca839450c8702d6768d1803bb6d99c6d059a56240933e5bf72cb2936f6c9e211"
            ))
        self.assertEquals(
            hash_block(block),
            Uint256.from_hexstr(
                "003c7a06c7efe128cb3bea692e1a485f7400f3670df7986c020083e9b10e295d"
            ))
Ejemplo n.º 15
0
 def test_mine_genesis_block(self):
     """ Mine the unitnet GENESIS block """
     time_source = MockTimeSource(time=1356446436)
     miner = BitcoinMiner()
     block, template = miner.mine_block(
         hash_prev=Uint256.from_hexstr(
             "0000000000000000000000000000000000000000000000000000000000000000"
         ),
         block_height=0,
         time_source=time_source,
         difficulty_bits=524287999,
         transactions=[],
         coinbase_txout_list=[
             TxOut(
                 5000000000,
                 Script([
                     push_data_instruction(
                         decodehexstr(
                             "04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f"
                         )),
                     Instruction(OP_CHECKSIG)
                 ]))
         ],
         coinbase_flags=[
             "/P2SH/",
             "The Times 03/Jan/2009 Chancellor on brink of second bailout for banks"
         ])
     self.assertEquals(block.blockheader.nonce, 1260)
     self.assertEquals(
         block.blockheader.hash_merkle,
         Uint256.from_hexstr(
             "cf7ac9a3b387cf5e9fc14e03e2a5bbfc16d1ba00d2af6c96869ab4da949bd240"
         ))
     self.assertEquals(
         hash_block(block),
         Uint256.from_hexstr(
             "003ee3cf880906caa5662f10d4b4fb1c86c1853230dee8a7b8a62f434c73da5f"
         ))
Ejemplo n.º 16
0
 def test_txin_serialize(self):
     txin = TxIn(
         previous_output=Outpoint(hash=Uint256.from_hexstr(
             "17c5cb687ba453ab65e12cdc0d8721c70ab4678665eb200c50cb9f1e3207090e"
         ),
                                  index=0),
         script=Script([
             Instruction(
                 72,
                 decodehexstr(
                     "3045022100ab2dc8932ca1d26f4cdac1feae09020a60ccc4d17b14e5fc5b21f3ab8c3a9cee022040a7208c172d19a19902280d66201c7fe2c3d8b2df7e23cc4e5b70fd52ecba2c01"
                 )),
             Instruction(
                 65,
                 decodehexstr(
                     "04b77dd1f3a21cb3d067a7e76982a609d7310f8692f5d61346f3225323c425604a0c12862755335c49e392673106adfc5dfdee1e4d367f10353e8911fac687db3e"
                 ))
         ]),
         sequence=TxIn.NSEQUENCE_FINAL)
     self.assertEquals(
         hexstr(TxinSerializer().serialize(txin)),
         "0e0907321e9fcb500c20eb658667b40ac721870ddc2ce165ab53a47b68cbc517000000008b483045022100ab2dc8932ca1d26f4cdac1feae09020a60ccc4d17b14e5fc5b21f3ab8c3a9cee022040a7208c172d19a19902280d66201c7fe2c3d8b2df7e23cc4e5b70fd52ecba2c014104b77dd1f3a21cb3d067a7e76982a609d7310f8692f5d61346f3225323c425604a0c12862755335c49e392673106adfc5dfdee1e4d367f10353e8911fac687db3effffffff"
     )
Ejemplo n.º 17
0
def checksig(vm, sig_param, pubkey_param):
    transaction, inputindex, unspent_script = vm.checksig_data
    #Hash type is the last byte of the signature
    hash_type, sig = ord(sig_param[-1]), sig_param[:-1]

    # last 5 bits of hash_type : 1=SIGHASH_ALL,2=SIGHASH_NONE, 3=SIGHASH_SINGLE
    # SIGHASH_ANYONECANPAY = 0x80

    # For performance reasons no full copy is made of the transaction
    # although it would be simpler to read.
    # e.g. tx_tmp = copy.deepcopy(transaction)
    # The input scripts are saved and then restored.
    tx_tmp = Tx(transaction.version, [
        TxIn(txin.previous_output, txin.script, txin.sequence)
        for txin in transaction.in_list
    ], [TxOut(txout.value, txout.script) for txout in transaction.out_list],
                transaction.locktime)
    #Save input scripts to restore them later
    #inlist = transaction.in_list
    #outlist = transaction.out_list
    #inscripts = [txin.script for txin in transaction.in_list]
    #TODO: blank out ouputs depending of hash_type (SIGHASH_NONE, SIGHASH_SINGLE)
    if (hash_type & SIGHASH_MASK == SIGHASH_NONE):
        tx_tmp.out_list = []
    if (hash_type & SIGHASH_MASK == SIGHASH_SINGLE):
        if (inputindex > len(tx_tmp.out_list)):
            raise Exception(
                "OP_CHECKSIG: no corresponding output for input %d using SIGHASH_SINGLE "
                % (inputindex))
        #n-1 empty TxOuts + original Txout
        tx_tmp.out_list = [TxOut(-1, Script([])) for _ in range(inputindex)] + \
                          [tx_tmp.out_list[inputindex]]
    if (hash_type & SIGHASH_MASK == SIGHASH_SINGLE
            or hash_type & SIGHASH_MASK == SIGHASH_NONE):
        # let others update at will
        for i in range(len(tx_tmp.in_list)):
            if i != inputindex:
                tx_tmp.in_list[i].sequence = 0
    #blank out other inputs in case of SIGHASH_ANYONECANPAY
    if (hash_type & SIGHASH_ANYONECANPAY):
        tx_tmp.in_list = [tx_tmp.in_list[inputindex]]
        inputindex = 0
    #blank out input scripts
    for txin in tx_tmp.in_list:
        txin.script = Script([])
    #except the current one that is replaced by the signed part (e.g. from the last OP_CODESEPARATOR)
    # of current_script with signature push_data removed
    # note: only 'optimal' push_data instructions with the same signature are removed
    current_script = Script(
        filter(lambda instr: instr != push_data_instruction(sig_param),
               vm.current_script.signed_part().instructions))
    tx_tmp.in_list[inputindex].script = current_script
    #serialize and append hash type
    enctx = TxSerializer().serialize(tx_tmp) + chr(hash_type) + b"\x00\x00\x00"

    #print "enctx:", hexstr(enctx)
    #print "sig:", hexstr(sig)
    #print "pubkey:", hexstr(pubkey_param)

    #Get hash
    hash = doublesha256(enctx)
    #Verify
    key = KEY()
    key.set_pubkey(pubkey_param)
    #ECDSA_verify: 1 = OK, 0=NOK, -1=ERROR
    result = key.verify(hash, sig) == 1
    if not result:
        pass
    #Restore transaction scripts
    #for txin, script in zip(inlist,inscripts):
    #    txin.script = script
    #transaction.in_list = inlist
    return (result)
Ejemplo n.º 18
0
from coinpy.lib.vm.script.push_data import push_bignum_instruction,\
    push_data_instruction
from coinpy.lib.transactions.merkle_tree import compute_merkle_root
from coinpy.lib.serialization.scripts.serialize import ScriptSerializer

GENESIS_MAIN =  Block(
    BlockHeader(1, 
                Uint256.from_hexstr("0000000000000000000000000000000000000000000000000000000000000000"), #hash_prev
                Uint256.from_hexstr("4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b"), #merkle
                1231006505,  #time
                486604799,   #bits
                2083236893), #nonce
    [Tx(1, #version
        [TxIn(Outpoint(Uint256.from_hexstr("0000000000000000000000000000000000000000000000000000000000000000"), 4294967295),     
                Script([push_bignum_instruction(486604799), #bits
                        push_bignum_instruction(4), #extra_nonce
                        push_data_instruction("The Times 03/Jan/2009 Chancellor on brink of second bailout for banks")]), #script
                4294967295) ], #inlist
        [TxOut(5000000000, #value
                Script([push_data_instruction(decodehexstr("04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f")),Instruction(OP_CHECKSIG)]))],
                0) #locktime         
     ])

GENESIS_TESTNET =  Block(
    BlockHeader(1, 
                Uint256.from_hexstr("0000000000000000000000000000000000000000000000000000000000000000"), #hash_prev
                Uint256.from_hexstr("4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b"), #merkle
                1296688602,  #time
                487063544,   #bits
                384568319), #nonce
    [Tx(1, #version
Ejemplo n.º 19
0
    import struct
    from coinpy.tools.hex import hexstr

    print hexstr(struct.pack("<I", 213566))
    print uint256_difficulty(524287999)
    time_source = SystemTimeSource()
    miner = BitcoinMiner()

    block = miner.mine_block(
        hash_prev=Uint256.from_hexstr(
            "0000000000000000000000000000000000000000000000000000000000000000"
        ),
        block_height=0,
        time_source=time_source,
        difficulty_bits=524287999,
        transactions=[],
        coinbase_txout_list=[
            TxOut(
                5000000000,
                Script([
                    Instruction(
                        OP_PUSHDATA,
                        decodehexstr(
                            "04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f"
                        )),
                    Instruction(OP_CHECKSIG)
                ]))
        ],
        coinbase_flags=[])
    print block
Ejemplo n.º 20
0
 def get_script(self):
     return Script([
         Instruction(OP_HASH160),
         push_data_instruction(data=self.hash),
         Instruction(OP_EQUAL)
     ])
Ejemplo n.º 21
0
 def test_block_serialize(self):
     blockheader = BlockHeader(
         version=1,
         hash_prev=Uint256.from_hexstr(
             "000000000fec081581146e8b16b275bfa52150ac4174a246cdf62694671ea7a3"
         ),
         hash_merkle=Uint256.from_hexstr(
             "0d9da162550fc45b1aaa00e933b23b3cbc7f37a9b2d2070d61235eaec11a926a"
         ),
         time=1301129903,
         bits=470809215,
         nonce=1280448751)
     tx1 = Tx(
         version=1,
         in_list=[
             TxIn(previous_output=Outpoint.null(),
                  script=Script([
                      Instruction(4, decodehexstr("7ffa0f1c")),
                      Instruction(1, decodehexstr("4e"))
                  ]),
                  sequence=TxIn.NSEQUENCE_FINAL)
         ],
         out_list=[
             TxOut(
                 value=5002000000,
                 script=Script([
                     Instruction(
                         65,
                         decodehexstr(
                             "049cc3cae30927c40598032044b9e9e25f4739b0d7ade62803f5e9cf075debc817e6d29f42c70d0a1beb1c904eaaa50ef885b011f9fbaa16ef288a7ad193e11567"
                         )),
                     Instruction(OP_CHECKSIG)
                 ]))
         ],
         locktime=0)
     tx2 = Tx(
         version=1,
         in_list=[
             TxIn(previous_output=Outpoint(hash=Uint256.from_hexstr(
                 "17c5cb687ba453ab65e12cdc0d8721c70ab4678665eb200c50cb9f1e3207090e"
             ),
                                           index=0),
                  script=Script([
                      Instruction(
                          72,
                          decodehexstr(
                              "3045022100ab2dc8932ca1d26f4cdac1feae09020a60ccc4d17b14e5fc5b21f3ab8c3a9cee022040a7208c172d19a19902280d66201c7fe2c3d8b2df7e23cc4e5b70fd52ecba2c01"
                          )),
                      Instruction(
                          65,
                          decodehexstr(
                              "04b77dd1f3a21cb3d067a7e76982a609d7310f8692f5d61346f3225323c425604a0c12862755335c49e392673106adfc5dfdee1e4d367f10353e8911fac687db3e"
                          ))
                  ]),
                  sequence=TxIn.NSEQUENCE_FINAL)
         ],
         out_list=[
             TxOut(value=24990000000,
                   script=Script([
                       Instruction(OP_DUP),
                       Instruction(OP_HASH160),
                       Instruction(
                           20,
                           decodehexstr(
                               "4d8b17fbce571614be89df4bd872de892a479844")),
                       Instruction(OP_EQUALVERIFY),
                       Instruction(OP_CHECKSIG)
                   ])),
             TxOut(value=5000000000,
                   script=Script([
                       Instruction(OP_DUP),
                       Instruction(OP_HASH160),
                       Instruction(
                           20,
                           decodehexstr(
                               "fadad27c40adbe230f5e3c04d44a292975084831")),
                       Instruction(OP_EQUALVERIFY),
                       Instruction(OP_CHECKSIG)
                   ]))
         ],
         locktime=0)
     blk = Block(blockheader, [tx1, tx2])
     data = BlockSerializer().serialize(blk)
     self.assertEquals(
         data,
         decodehexstr(
             "01000000a3a71e679426f6cd46a27441ac5021a5bf75b2168b6e14811508ec0f000000006a921ac1ae5e23610d07d2b2a9377fbc3c3bb233e900aa1a5bc40f5562a19d0dafaa8d4d7ffa0f1cef18524c0201000000010000000000000000000000000000000000000000000000000000000000000000ffffffff07047ffa0f1c014effffffff018076242a010000004341049cc3cae30927c40598032044b9e9e25f4739b0d7ade62803f5e9cf075debc817e6d29f42c70d0a1beb1c904eaaa50ef885b011f9fbaa16ef288a7ad193e11567ac0000000001000000010e0907321e9fcb500c20eb658667b40ac721870ddc2ce165ab53a47b68cbc517000000008b483045022100ab2dc8932ca1d26f4cdac1feae09020a60ccc4d17b14e5fc5b21f3ab8c3a9cee022040a7208c172d19a19902280d66201c7fe2c3d8b2df7e23cc4e5b70fd52ecba2c014104b77dd1f3a21cb3d067a7e76982a609d7310f8692f5d61346f3225323c425604a0c12862755335c49e392673106adfc5dfdee1e4d367f10353e8911fac687db3effffffff02802385d1050000001976a9144d8b17fbce571614be89df4bd872de892a47984488ac00f2052a010000001976a914fadad27c40adbe230f5e3c04d44a29297508483188ac00000000"
         ))
Ejemplo n.º 22
0
 def get_script(self):
     return Script([
         push_data_instruction(data=self.pubkey),
         Instruction(OP_CHECKSIG)
     ])
Ejemplo n.º 23
0
def make_script_pubkeyhash_sig(pubkey, sig):
    return Script(
        [push_data_instruction(data=sig),
         push_data_instruction(data=pubkey)])
Ejemplo n.º 24
0
def make_script_pubkey_sig(sig):
    return Script([push_data_instruction(data=sig)])
Ejemplo n.º 25
0
def make_script_pubkey(pubkey):
    instructions = [
        push_data_instruction(data=pubkey),
        Instruction(OP_CHECKSIG)
    ]
    return Script(instructions)
Ejemplo n.º 26
0
    def test_merkletx_deserialize(self):
        # This is tx 37 of block 429 on testnet3
        merkle_tx_data = "0100000001b088b783eddb1471bf99a790a78adf956b57410de2e4829de87f220a5744c888000000006b483045022100813a4dfdfda02946bf9fddc59ffd0b8d2feaa618518a3015fd5e65dd88a0d4480220137b25ea7531e103405bfe1617cb92f524e88c86e66a79c1e3c62558d0195282012103cf163e38520a7b390305528eca2fa1c620f0da225a044017fd92b30067875dc8ffffffff0290900b24010000001976a91410170e3f7c3b0c93d2e07e90e307d3fb2181170288acb58b0d000000000017a914184cd0a38ac3b1357d07179553788375a9e8a3b887000000009ff0cf741383d94f79a6efeff2a317aaf6375ac3d252db55362280e00000000007b088b783eddb1471bf99a790a78adf956b57410de2e4829de87f220a5744c888030d1b4ae01dc5165aa3c3a966dd535032a1ccd5d4722dbf9de57b85a6d65d22fa109a70ffb657a7299604ae344d8432cb37806da51a5840bfcbc2e70290f77434ab33eed85b14302067bc7ee120c49d8ac1c8de22c9cc873f6cf46b8b9d0902cff8e9a5461ef46108e56d3ee115df7cbe39186160a24e8c63d21a7bc402b2157921dea921eeb09cc757cf7ab383e122cca730c51a976e69ce37daaf66217e00d42144297dd9954a62226db7fa2de24cba50b4c963ae6226ae5fa652946dd0a725000000"
        expected_merkle_tx = MerkleTx(
            Tx(version=1,
               in_list=[
                   TxIn(previous_output=Outpoint(hash=Uint256.from_hexstr(
                       "88c844570a227fe89d82e4e20d41576b95df8aa790a799bf7114dbed83b788b0"
                   ),
                                                 index=0),
                        script=Script(instructions=[
                            Instruction(
                                72,
                                decodehexstr(
                                    "3045022100813a4dfdfda02946bf9fddc59ffd0b8d2feaa618518a3015fd5e65dd88a0d4480220137b25ea7531e103405bfe1617cb92f524e88c86e66a79c1e3c62558d019528201"
                                )),
                            Instruction(
                                33,
                                decodehexstr(
                                    "03cf163e38520a7b390305528eca2fa1c620f0da225a044017fd92b30067875dc8"
                                ))
                        ]),
                        sequence=4294967295)
               ],
               out_list=[
                   TxOut(value=4899704976,
                         script=Script(instructions=[
                             Instruction(OP_DUP),
                             Instruction(OP_HASH160),
                             Instruction(
                                 20,
                                 decodehexstr(
                                     "10170e3f7c3b0c93d2e07e90e307d3fb21811702"
                                 )),
                             Instruction(OP_EQUALVERIFY),
                             Instruction(OP_CHECKSIG)
                         ])),
                   TxOut(value=887733,
                         script=Script(instructions=[
                             Instruction(OP_HASH160),
                             Instruction(
                                 20,
                                 decodehexstr(
                                     "184cd0a38ac3b1357d07179553788375a9e8a3b8"
                                 )),
                             Instruction(OP_EQUAL)
                         ]))
               ],
               locktime=0),
            blockhash=Uint256.from_hexstr(
                "00000000e080223655db52d2c35a37f6aa17a3f2efefa6794fd9831374cff09f"
            ),
            merkle_branch=[
                Uint256.from_hexstr(
                    "88c844570a227fe89d82e4e20d41576b95df8aa790a799bf7114dbed83b788b0"
                ),
                Uint256.from_hexstr(
                    "225dd6a6857be59dbf2d72d4d5cca1325053dd66a9c3a35a16c51de04a1b0d03"
                ),
                Uint256.from_hexstr(
                    "74f79002e7c2cbbf40581aa56d8037cb32844d34ae049629a757b6ff709a10fa"
                ),
                Uint256.from_hexstr(
                    "02099d8b6bf46c3f87ccc922dec8c18a9dc420e17ebc672030145bd8ee33ab34"
                ),
                Uint256.from_hexstr(
                    "15b202c47b1ad2638c4ea260611839be7cdf15e13e6de50861f41e46a5e9f8cf"
                ),
                Uint256.from_hexstr(
                    "007e2166afda37ce696e971ac530a7cc22e183b37acf57c79cb0ee21a9de2179"
                ),
                Uint256.from_hexstr(
                    "a7d06d9452a65fae2662ae63c9b450ba4ce22dfab76d22624a95d97d294421d4"
                )
            ],
            nindex=37)

        self.assertEquals(
            MerkleTxSerializer().deserialize(decodehexstr(merkle_tx_data))[0],
            expected_merkle_tx)
Ejemplo n.º 27
0
    OP_EQUALVERIFY, OP_HASH160
from coinpy.model.protocol.structures.blockheader import BlockHeader
from coinpy.model.protocol.structures.block import Block
from coinpy.model.scripts.script import Script
from coinpy.model.protocol.structures.tx_out import TxOut
from coinpy.lib.blocks.hash_block import hash_block

blockchain_5blocks_unitnet = [Block(blockheader=BlockHeader(version=2, 
                          hash_prev=Uint256.from_hexstr("0000000000000000000000000000000000000000000000000000000000000000"), 
                          hash_merkle=Uint256.from_hexstr("cf7ac9a3b387cf5e9fc14e03e2a5bbfc16d1ba00d2af6c96869ab4da949bd240"), 
                          time=1356446436,
                          bits=524287999,
                          nonce=1260), 
                    transactions=[Tx(version=1, 
                in_list=[TxIn(previous_output=Outpoint(hash=Uint256.from_hexstr("0000000000000000000000000000000000000000000000000000000000000000"),index=4294967295), 
                   script=Script(instructions=[Instruction(OP_0),Instruction(OP_0),Instruction(6,  decodehexstr("2f503253482f")),Instruction(69,  decodehexstr("5468652054696d65732030332f4a616e2f32303039204368616e63656c6c6f72206f6e206272696e6b206f66207365636f6e64206261696c6f757420666f722062616e6b73"))]), 
                   sequence=4294967295)], 
                out_list=[TxOut(value=5000000000, 
                    script=Script(instructions=[Instruction(65,  decodehexstr("04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f")),Instruction(OP_CHECKSIG)]))], 
                locktime=0)]),
Block(blockheader=BlockHeader(version=2, 
                          hash_prev=Uint256.from_hexstr("003ee3cf880906caa5662f10d4b4fb1c86c1853230dee8a7b8a62f434c73da5f"), 
                          hash_merkle=Uint256.from_hexstr("ef4bfb7cdb962b4d04e3828e4e0a4c09322ae065fa34c2c81ef7d0e0273ec5eb"), 
                          time=1356447036,
                          bits=524287999,
                          nonce=905), 
                    transactions=[Tx(version=1, 
                in_list=[TxIn(previous_output=Outpoint(hash=Uint256.from_hexstr("0000000000000000000000000000000000000000000000000000000000000000"),index=4294967295), 
                   script=Script(instructions=[Instruction(1,  decodehexstr("01")),Instruction(OP_0),Instruction(6,  decodehexstr("2f503253482f"))]), 
                   sequence=4294967295)], 
                out_list=[TxOut(value=5000000000, 
Ejemplo n.º 28
0
 def get_script(self):
     return Script(
         [push_smallint(self.m)] +
         [push_data_instruction(pubkey) for pubkey in self.public_keys] +
         [push_smallint(len(self.public_keys))] +
         [Instruction(OP_CHECKMULTISIG)])