예제 #1
0
 def test_decrypt_private_key(self):
     master_key = MasterKey(crypted_key=decodehexstr("be4afa6923ad06790b0f8c3345131499cf2b149ca422bd11a7e67a76347c51a456a2d626f75da1ff809632fca7165d71"), 
                            salt=decodehexstr("8cdcbd8a494b0eeb"),
                            derivation_method=MasterKey.DERIVMETHOD_EVP_SHA512, 
                            derive_iterations=45193, other_derivation_parameters="")
     #Decrypt the master crypted_key using the passphrase
     plain_masterkey = decrypt_masterkey(master_key, "hello")
     print "plain_masterkey:", hexstr(plain_masterkey) 
     assert hexstr(plain_masterkey) == "56722b42c4b9f8689fe9b38745fe75af92d0a50d6fd94b34de6b6d5e287bbed3"
     #Decrypt a crypted_secret
     public_key = decodehexstr("046a82d73af2cc093e3df7ae0185f045946970bcd5f0ef26f82d4f9a24e0d50f977c51e311e079e3183cfadd67d9b3f089fe7ba94a196c365fbd9e03b8c423787d")
     crypted_secret = decodehexstr("ff914ab69f58af92ac56de85051441e729cc51e11608d563e2a266ce3b8c59f573ed6a1828ff98fadb345890b6ed2626")
     crypter2 = Crypter()
     crypter2.set_key(plain_masterkey, doublesha256(public_key))
     secret = crypter2.decrypt(crypted_secret)
     print "secret:", hexstr(secret) 
     assert hexstr(secret) == "1c7552a9b755d29d081efd71f7811cc8ab2c9f2c634f489e6b45700711c8a304"
     #Test the secret
     k = KEY()
     k.set_secret(secret)
     assert k.get_pubkey() == public_key
     sig1 = k.sign("sign something")
     k2 = KEY()
     k2.set_pubkey(public_key)
     assert k2.verify("sign something", sig1) == 1
예제 #2
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)
예제 #3
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)])
예제 #4
0
 def test_serialize_alert_message(self):
     alert_msg = AlertMessage(AlertPayload(1, 1329620535, 1329792435, 1010, 1009, set(), 10000, 61000, set(),
                                100, "", "See bitcoin.org/feb20 if you have trouble connecting after 20 February", "")
                              , decodehexstr("30450221008389df45f0703f39ec8c1cc42c13810ffcae14995bb648340219e353b63b53eb022009ec65e1c1aaeec1fd334c6b684bde2b3f573060d5b70c3a46723326e4e8a4f1"))
     serialized_msg = AlertMessageSerializer().serialize(alert_msg)
     self.assertEquals(serialized_msg,
                       decodehexstr("73010000003766404f00000000b305434f00000000f2030000f1030000001027000048ee00000064000000004653656520626974636f696e2e6f72672f666562323020696620796f7520686176652074726f75626c6520636f6e6e656374696e67206166746572203230204665627275617279004730450221008389df45f0703f39ec8c1cc42c13810ffcae14995bb648340219e353b63b53eb022009ec65e1c1aaeec1fd334c6b684bde2b3f573060d5b70c3a46723326e4e8a4f1"))
 def test_deserialize_errors(self):
     # unknown command
     serialized_inv_message = decodehexstr(
         "aabfb5da6c6e76000000000000000000490000008be920f5020100000055b5d3d496e196d624a471b818ba0b1778417ae335a544033536654fdda3eef602000000497b15826573b28c3e83a5d0c5ed30cf48b97dd6bd797849144ea24000000000"
     )
     with self.assertRaises(FormatErrorException):
         inv_message, _ = MessageSerializer(TESTNET).deserialize(
             serialized_inv_message)
     # wrong magic (testnet magic on MAIN)
     serialized_inv_message = decodehexstr(
         "fabfb5da696e76000000000000000000490000008be920f5020100000055b5d3d496e196d624a471b818ba0b1778417ae335a544033536654fdda3eef602000000497b15826573b28c3e83a5d0c5ed30cf48b97dd6bd797849144ea24000000000"
     )
     with self.assertRaises(FormatErrorException):
         inv_message, _ = MessageSerializer(MAIN).deserialize(
             serialized_inv_message)
     # length field too large compared to data
     serialized_inv_message = decodehexstr(
         "fabfb5da696e760000000000000000004A0000008be920f5020100000055b5d3d496e196d624a471b818ba0b1778417ae335a544033536654fdda3eef602000000497b15826573b28c3e83a5d0c5ed30cf48b97dd6bd797849144ea24000000000"
     )
     with self.assertRaises(MissingDataException):
         inv_message, _ = MessageSerializer(TESTNET).deserialize(
             serialized_inv_message)
     # checksum error
     serialized_inv_message = decodehexstr(
         "fabfb5da696e76000000000000000000490000008bf920f5020100000055b5d3d496e196d624a471b818ba0b1778417ae335a544033536654fdda3eef602000000497b15826573b28c3e83a5d0c5ed30cf48b97dd6bd797849144ea24000000000"
     )
     with self.assertRaises(FormatErrorException):
         inv_message, _ = MessageSerializer(TESTNET).deserialize(
             serialized_inv_message)
예제 #6
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")
예제 #7
0
 def test_varstr_script_deserialize(self):
     script, _ = VarstrScriptSerializer().deserialize(decodehexstr("1976a914fadad27c40adbe230f5e3c04d44a29297508483188ac"))
     self.assertEquals(script, Script([Instruction(OP_DUP),
                                       Instruction(OP_HASH160),
                                       Instruction(20, decodehexstr("fadad27c40adbe230f5e3c04d44a292975084831")), 
                                       Instruction(OP_EQUALVERIFY),
                                       Instruction(OP_CHECKSIG)]))
예제 #8
0
 def test_txout_deserialize(self):
     txout, _ = TxoutSerializer().deserialize(decodehexstr("802385d1050000001976a9144d8b17fbce571614be89df4bd872de892a47984488ac"))
     self.assertEquals(txout, TxOut(value=24990000000, 
                                    script=Script([Instruction(OP_DUP),
                                                   Instruction(OP_HASH160),
                                                   Instruction(20, decodehexstr("4d8b17fbce571614be89df4bd872de892a479844")), 
                                                   Instruction(OP_EQUALVERIFY),
                                                   Instruction(OP_CHECKSIG)])))
예제 #9
0
 def test_deserialize_error_length(self):
     data1 = decodehexstr("032422")
     data2 = decodehexstr("032422032422032422032422")
     
     with self.assertRaises(DeserializationException):
         LogIndexEntrySerializer.deserialize(data1)
     with self.assertRaises(DeserializationException):
         LogIndexEntrySerializer.deserialize(data2)
예제 #10
0
    def test_deserialize_error_length(self):
        data1 = decodehexstr("032422")
        data2 = decodehexstr("032422032422032422032422")

        with self.assertRaises(DeserializationException):
            LogIndexEntrySerializer.deserialize(data1)
        with self.assertRaises(DeserializationException):
            LogIndexEntrySerializer.deserialize(data2)
예제 #11
0
 def test_address_from_bytestring(self):
     addr1 = BitcoinAddress.from_bytestring(
         decodehexstr("0041fe507633463c246ec91502c2d67e8b3d81618e"))
     self.assertEquals(addr1.to_base58addr(),
                       "171waY81rzeFaBYhsiKibhBW5WAG8X7DLk")
     with self.assertRaises(InvalidBitcoinAddress):
         # not 21 characters
         addr2 = BitcoinAddress.from_bytestring(
             decodehexstr("0041fe507633463c246ec91502c2d67e8b3d81618e61"))
예제 #12
0
 def test_ssl_sign_verify(self):
     key = KEY()
     key.set_secret(decodehexstr("30d1d8d1d243ab41a80a3cc1481a626a137f771a636b2daca06c1f86cdfecffb"))
     sig = key.sign("cool")
     # verify on another key
     key2 = KEY()
     key2.set_pubkey(decodehexstr("030a43196c8bf389c0ce5987a3f4dac57f4ca0d9733c232659717d9404074b4504"))
     self.assertEquals(key2.verify("cool", sig), 1)
     self.assertEquals(key2.verify("coolx", sig), 0)
     self.assertEquals(key2.verify("cool", decodehexstr("3045022100ea3cbfca49123ecdcc419cf3277597307dca70b548ca1d4312f39186b043e86802201057af5c3889b65a59333d4f23bea915e76c2c26606dd35c57e00adf416ca31600")), 0)
예제 #13
0
    def test_validate_pubkey(self):
        # tx 1 of block 383 on testnet 3 (2 PUBKEY inputs)
        # tx: 43cd28c3b56543298ee264e3c01ef2dd8189276bd0c1cf6b86269018c710ce28
        tx, _ =  TxSerializer().deserialize(decodehexstr("01000000028e3430573cfde2f3e1eece8aefe661dd841bcb665d35832415bab4f75267852200000000494830450221009cca8fb1c4a34982c4e9a59fd15856404c02085f926043cfc6664924a5b9e36d02205811f85b395c43492164b8b33867d745211d8c7ffdcd1b288e56f74dbdaed15b01ffffffffe3abf5981a1bd6457ec0cdcab76cc2a176dc0d7e16f6d3781aebc684f13cc4fd00000000484730440220387bbcee99e370ea2115eaccfd8c48eb380653303b66c6946e0903fb0bbc6f8602202b2ef048519d45525413f64b727a1d85ff2cf7d91ab4607308224b4c30d5846801ffffffff0240f1f23a000000001976a9142203ca59edf66969757e6cc9238b1b36f4dc35b888acc0f218190200000017a9149eb21980dc9d413d8eac27314938b9da920ee53e8700000000"))
        outscript1 = ScriptSerializer().deserialize(decodehexstr("2102547b223d58eb5da7c7690748f70a3bab1509cb7578faac9032399f0b6bce31d6ac"))
        outscript2 = ScriptSerializer().deserialize(decodehexstr("2103fcc9ce029ad74af9fecacce68bcc775cc6efcb000a0b8cc2b3aacad4850bc4b0ac"))

        vm = TxValidationVM()
        valid, reason = vm.validate(tx, 0, outscript1, tx.in_list[0].script)
        assert valid, reason
        valid, reason = vm.validate(tx, 1, outscript2, tx.in_list[1].script)
        assert valid, reason
예제 #14
0
 def test_validate_pubkeyhash(self):
     # tx 5 of block 506 on testnet 3 (2 PUBKEYHASH inputs)
     # tx: e79d5732b9914a511c0ad002c930271aa843ae64ed0abf54bff2869596c04a59
     tx, _ =  TxSerializer().deserialize(decodehexstr("010000000202f630b1641c6988224295e4299c14d176fb4446d0f62a682efc6adbd049de77000000006b483045022100cb6600d5be418a84e3f5c7e7ece2836de358ccbdb26fc1a4e4c262c0db117cf2022058cceb2e810ed5c7c3a4d47c0fec4e09e2672490645767579893ce89c0c50087012102de4a86f3c045cfa9460dbe867ebedab96df36aa662800e2f1d8645374f0d10a4ffffffff3b7de9fe37b52af96fd4f7134df83c91358c40687430812f8e29841085363a87000000006a47304402205b26712719619a2d8510d888a2e2cd65ceba00b104f95d1bc46da04f71814a7d022046af82c57609914a855de48ea0036a09fdbccc7996c79406a6eef383242f994301210321b73cc97e906155d3c5bf65e5a2ee75f6792c97916f1c6ceda59777baa0dcc5ffffffff02b3c40e00000000001976a9146d9ad15ddc32b1f96d9f496a4a8a89a77ab88fa388ac87200f00000000001976a914431143b62421c12c1149f1252ae1e181547b0e5d88ac00000000"))
     outscript1 = ScriptSerializer().deserialize(decodehexstr("76a9141b05d1cc4b3158519041814fdac587326422865b88ac"))
     outscript2 = ScriptSerializer().deserialize(decodehexstr("76a914b5d23a56ad5657163698a6817420f17b47cf3c4588ac"))
     vm = TxValidationVM()
     valid, reason = vm.validate(tx, 0, outscript1, tx.in_list[0].script)
     assert valid, reason
     vm = TxValidationVM()
     valid, reason = vm.validate(tx, 1, outscript2, tx.in_list[1].script)
     assert valid, reason
예제 #15
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)
예제 #16
0
 def test_serialize_tx_message(self):
     # TX 1 from block 389 on testnet 3
     tx_message = TxMessage(Tx(version=1, 
                             in_list=[TxIn(previous_output=Outpoint(hash=Uint256.from_hexstr("f519f238c54e20bb418bb4513ce75484546d198200a901be55152e12cfa82652"),index=1), 
                                script=Script(instructions=[Instruction(73,  decodehexstr("3046022100b96e2fd2015bb3fe22c23c712c3ac4a8636d4a78df7d669babd64eaba314c236022100f402d7e3392d1770ce2be0c823eab0122bb83ee0bd65f5de54a1feb53e452eed01")),Instruction(65,  decodehexstr("04c4bee5e6dbb5c1651437cb4386c1515c7776c64535077204c6f24f05a37d04a32bc78beb2193b53b104c9954c44b0ce168bc78efd5f1e1c7db9d6c21b3016599"))]), 
                                sequence=4294967295),TxIn(previous_output=Outpoint(hash=Uint256.from_hexstr("d748009c88abacfbd8d0dafb5552f0700a16f9a35ff41583988d9f70618c1971"),index=0), 
                                script=Script(instructions=[Instruction(72,  decodehexstr("304502210092e96f4185fc1bb74b4d9c258219dc5ffc71afb43e73ec0e78bdd91bafbe386e022019fc5b3d9602005b61f5dc75e7bd4dde1c19a0700c0a7958503528014d39019101")),Instruction(33,  decodehexstr("02274eae01d391bbec32d4b60e24d5d24f9cc23ca8da6708cf6c062381bfa71006"))]), 
                                sequence=4294967295)], 
                             out_list=[TxOut(value=5000000000, 
                                 script=Script(instructions=[Instruction(OP_DUP),Instruction(OP_HASH160),Instruction(20,  decodehexstr("dbf89509176a975e41d04cc0af24cfc8de4394a9")),Instruction(OP_EQUALVERIFY),Instruction(OP_CHECKSIG)]))], 
                             locktime=0))
     serialized_msg = TxMessageSerializer().serialize(tx_message)
     self.assertEquals(hexstr(serialized_msg), "01000000025226a8cf122e1555be01a90082196d548454e73c51b48b41bb204ec538f219f5010000008c493046022100b96e2fd2015bb3fe22c23c712c3ac4a8636d4a78df7d669babd64eaba314c236022100f402d7e3392d1770ce2be0c823eab0122bb83ee0bd65f5de54a1feb53e452eed014104c4bee5e6dbb5c1651437cb4386c1515c7776c64535077204c6f24f05a37d04a32bc78beb2193b53b104c9954c44b0ce168bc78efd5f1e1c7db9d6c21b3016599ffffffff71198c61709f8d988315f45fa3f9160a70f05255fbdad0d8fbacab889c0048d7000000006b48304502210092e96f4185fc1bb74b4d9c258219dc5ffc71afb43e73ec0e78bdd91bafbe386e022019fc5b3d9602005b61f5dc75e7bd4dde1c19a0700c0a7958503528014d390191012102274eae01d391bbec32d4b60e24d5d24f9cc23ca8da6708cf6c062381bfa71006ffffffff0100f2052a010000001976a914dbf89509176a975e41d04cc0af24cfc8de4394a988ac00000000")
예제 #17
0
    def test_validate_scripthash_multisig(self):
        # tx 1 of block 504 on testnet 3, 2 TX_SCRIPTHASH inputs, 2 PUBKEYHASH outputs
        # SCRIPTHASH of 1-of-2 and 2-of-3 multisig transactions
        # tx: 1915695f1de19d9a2e1fb1bd9c5e2f816022ec41a58e9f298396c8a4586a8c5e
        tx, _ =  TxSerializer().deserialize(decodehexstr("0100000002c142078d793bb5b4aadba6009e2b142872b1ed3a7f0f7336ebe0e38180d8d682010000009200483045022100d6868ef86978ffad889a312be2991cbfe2c2d27b40fed038dc65da83565f5b0f02205f7019af9b75a5e9ba9c394f6ce883fa72e8e0951cf2ca5011af5888026663f3014751210341d8a934cfca197c7acaa358a1c39b3317a3e6d80b808ad367278ddb6661d16f210220108eea941a69b20d320e9a82c4bc5f096edb7e7c74638810227594a8033c1d52aeffffffff3f2dc10288706fe1c3116bec01dc2a259bfc2802b60d9b13787195fa8205f0cf01000000b400473044022066952731f8c5663fd61b2cbc33361e79e27a8c0937c32b42cc2dd125215b98bb02202c90d8022337c868c1f00ab59a9cd833cad2522de4b703264e9b4e271d11cd12014c6951210220108eea941a69b20d320e9a82c4bc5f096edb7e7c74638810227594a8033c1d210386380591c6fe54fe67d54e809d38d2db63886c1d228beb744abc9327bee392882102c4c0101075ed2d413dd328841c87f7a39ff5a75d793decab1da9fb1128082c3b53aeffffffff02c11e1400000000001976a914724192ac202d067d06635dc043b84abdad14de0088ac2b540400000000001976a914342e5d1f2eb9c6e99fda90c85ca05aa36616644c88ac00000000"))
        outscript1 = ScriptSerializer().deserialize(decodehexstr("a914d9f26cca817fa116dc76e1be7a17067eb843625087"))
        outscript2 = ScriptSerializer().deserialize(decodehexstr("a9145fc04212c7fb1103d99dd7ff454797a281ef92f587"))

        vm = TxValidationVM()
        valid, reason = vm.validate(tx, 0, outscript1, tx.in_list[0].script)
        assert valid, reason
        valid, reason = vm.validate(tx, 1, outscript2, tx.in_list[1].script)
        assert valid, reason
예제 #18
0
 def test_varstr_script_deserialize(self):
     script, _ = VarstrScriptSerializer().deserialize(
         decodehexstr(
             "1976a914fadad27c40adbe230f5e3c04d44a29297508483188ac"))
     self.assertEquals(
         script,
         Script([
             Instruction(OP_DUP),
             Instruction(OP_HASH160),
             Instruction(
                 20,
                 decodehexstr("fadad27c40adbe230f5e3c04d44a292975084831")),
             Instruction(OP_EQUALVERIFY),
             Instruction(OP_CHECKSIG)
         ]))
예제 #19
0
 def test_pubkeyhash_from_script(self):
     self.assertEquals(
         ScriptPubkey.from_script(self.script1),
         ScriptPubkey(
             decodehexstr(
                 "02547b223d58eb5da7c7690748f70a3bab1509cb7578faac9032399f0b6bce31d6"
             )))
예제 #20
0
    def test_serialize_logindex_begin_tx(self):
        log_index = LogIndexEntry(LogIndexEntry.BEGIN_TX, needs_commit=True)
        
        serialized = LogIndexEntrySerializer.serialize(log_index)

        self.assertEquals(serialized, 
                          decodehexstr("010000000001"))
예제 #21
0
 def test_blocklocator_deserialize(self):
     loc, cursor = BlockLocatorSerializer().deserialize(
         decodehexstr(
             "0100000005a329e1166037b2ea0cfbef5060c011c1cbbd37ad047a47b62d3dae020000000070bdb41302a622800ddfb2b3ebdd937fdca812ec8a7ab90ef250410700000000398052b24b2cc45ef953222e23880d25c3df95a913e72e9b015b34250000000066739a04b15f117929b581f6fe0ba2906bc4323829400f949086bb7d0000000008b067b31dc139ee8e7a76a4f2cfcca477c4c06e1ef89f4ae308951907000000"
         ))
     self.assertEquals(
         loc,
         BlockLocator(
             version=1,
             blockhashlist=[
                 Uint256.from_hexstr(
                     "0000000002ae3d2db6477a04ad37bdcbc111c06050effb0ceab2376016e129a3"
                 ),
                 Uint256.from_hexstr(
                     "00000000074150f20eb97a8aec12a8dc7f93ddebb3b2df0d8022a60213b4bd70"
                 ),
                 Uint256.from_hexstr(
                     "0000000025345b019b2ee713a995dfc3250d88232e2253f95ec42c4bb2528039"
                 ),
                 Uint256.from_hexstr(
                     "000000007dbb8690940f40293832c46b90a20bfef681b52979115fb1049a7366"
                 ),
                 Uint256.from_hexstr(
                     "00000007199508e34a9ff81e6ec0c477a4cccff2a4767a8eee39c11db367b008"
                 )
             ]))
예제 #22
0
 def test_deserialize_getdata_message(self):
     serialized_getdata = decodehexstr("030100000098a23359c17ca2678e2039c8ff9081b18c4913749c9a081ac3f62958f09fa4720100000055b5d3d496e196d624a471b818ba0b1778417ae335a544033536654fdda3eef602000000497b15826573b28c3e83a5d0c5ed30cf48b97dd6bd797849144ea24000000000")
     getdata_msg, _ = GetdataMessageSerializer().deserialize(serialized_getdata)
     expected_msg = GetdataMessage([Invitem(INV_TX, Uint256.from_hexstr("72a49ff05829f6c31a089a9c7413498cb18190ffc839208e67a27cc15933a298")),
                                   Invitem(INV_TX, Uint256.from_hexstr("f6eea3dd4f6536350344a535e37a4178170bba18b871a424d696e196d4d3b555")),
                                   Invitem(INV_BLOCK, Uint256.from_hexstr("0000000040a24e14497879bdd67db948cf30edc5d0a5833e8cb2736582157b49"))])
     self.assertEquals(getdata_msg, expected_msg)
예제 #23
0
    def test_serialize_logindex_end_tx(self):
        log_index = LogIndexEntry(LogIndexEntry.END_TX, needs_commit=False)
        
        serialized = LogIndexEntrySerializer.serialize(log_index)

        self.assertEquals(serialized, 
                          decodehexstr("020000000000"))
예제 #24
0
    def test_serialize_logindex_write(self):
        log_index = LogIndexEntry(LogIndexEntry.WRITE, 34234242, needs_commit=True)
        
        serialized = LogIndexEntrySerializer.serialize(log_index)

        self.assertEquals(serialized, 
                          decodehexstr("03020a5f8201"))
예제 #25
0
    def test_deserialize_logindex_begin_tx(self):
        log_index = LogIndexEntrySerializer.deserialize(
            decodehexstr("010000000001"))

        self.assertEquals(
            log_index, LogIndexEntry(LogIndexEntry.BEGIN_TX,
                                     needs_commit=True))
예제 #26
0
    def test_deserialize_chunk_header(self):
        data = decodehexstr("6e616d650163e710053447b900074d22")
        
        deserialized = ChunkHeaderSerializer.deserialize(data)

        self.assertEquals(deserialized, 
                          ChunkHeader("name", 23324432, 87312313, 478498))
예제 #27
0
    def test_deserialize_logindex_write(self):
        log_index = LogIndexEntrySerializer.deserialize(
            decodehexstr("03020a5f8201"))

        self.assertEquals(
            log_index,
            LogIndexEntry(LogIndexEntry.WRITE, 34234242, needs_commit=True))
예제 #28
0
    def test_deserialize_chunk_header(self):
        data = decodehexstr("6e616d650163e710053447b900074d22")

        deserialized = ChunkHeaderSerializer.deserialize(data)

        self.assertEquals(deserialized,
                          ChunkHeader("name", 23324432, 87312313, 478498))
예제 #29
0
 def test_address_from_public_key(self):
     """
     public: 023053536687205cbf57a25386ac466c7f85105032ced1ae9c54486a83c6dd3bab
     private: 049db42589b263e8700eb747a402b74604aae54ebc04f1cbe9a1cf584683f100
     """
     addr1 = BitcoinAddress.from_publickey(decodehexstr("023053536687205cbf57a25386ac466c7f85105032ced1ae9c54486a83c6dd3bab"), MAIN)
     self.assertEquals(addr1.to_base58addr(), "171waY81rzeFaBYhsiKibhBW5WAG8X7DLk")
예제 #30
0
 def test_validate_pubkeyhash(self):
     # tx 5 of block 506 on testnet 3 (2 PUBKEYHASH inputs)
     # tx: e79d5732b9914a511c0ad002c930271aa843ae64ed0abf54bff2869596c04a59
     tx, _ = TxSerializer().deserialize(
         decodehexstr(
             "010000000202f630b1641c6988224295e4299c14d176fb4446d0f62a682efc6adbd049de77000000006b483045022100cb6600d5be418a84e3f5c7e7ece2836de358ccbdb26fc1a4e4c262c0db117cf2022058cceb2e810ed5c7c3a4d47c0fec4e09e2672490645767579893ce89c0c50087012102de4a86f3c045cfa9460dbe867ebedab96df36aa662800e2f1d8645374f0d10a4ffffffff3b7de9fe37b52af96fd4f7134df83c91358c40687430812f8e29841085363a87000000006a47304402205b26712719619a2d8510d888a2e2cd65ceba00b104f95d1bc46da04f71814a7d022046af82c57609914a855de48ea0036a09fdbccc7996c79406a6eef383242f994301210321b73cc97e906155d3c5bf65e5a2ee75f6792c97916f1c6ceda59777baa0dcc5ffffffff02b3c40e00000000001976a9146d9ad15ddc32b1f96d9f496a4a8a89a77ab88fa388ac87200f00000000001976a914431143b62421c12c1149f1252ae1e181547b0e5d88ac00000000"
         ))
     outscript1 = ScriptSerializer().deserialize(
         decodehexstr("76a9141b05d1cc4b3158519041814fdac587326422865b88ac"))
     outscript2 = ScriptSerializer().deserialize(
         decodehexstr("76a914b5d23a56ad5657163698a6817420f17b47cf3c4588ac"))
     vm = TxValidationVM()
     valid, reason = vm.validate(tx, 0, outscript1, tx.in_list[0].script)
     assert valid, reason
     vm = TxValidationVM()
     valid, reason = vm.validate(tx, 1, outscript2, tx.in_list[1].script)
     assert valid, reason
예제 #31
0
 def test_deserialize_errors(self):
     # unknown command
     serialized_inv_message = decodehexstr("aabfb5da6c6e76000000000000000000490000008be920f5020100000055b5d3d496e196d624a471b818ba0b1778417ae335a544033536654fdda3eef602000000497b15826573b28c3e83a5d0c5ed30cf48b97dd6bd797849144ea24000000000")
     with self.assertRaises(FormatErrorException):
         inv_message, _ = MessageSerializer(TESTNET).deserialize(serialized_inv_message)
     # wrong magic (testnet magic on MAIN)
     serialized_inv_message = decodehexstr("fabfb5da696e76000000000000000000490000008be920f5020100000055b5d3d496e196d624a471b818ba0b1778417ae335a544033536654fdda3eef602000000497b15826573b28c3e83a5d0c5ed30cf48b97dd6bd797849144ea24000000000")
     with self.assertRaises(FormatErrorException):
         inv_message, _ = MessageSerializer(MAIN).deserialize(serialized_inv_message)
     # length field too large compared to data
     serialized_inv_message = decodehexstr("fabfb5da696e760000000000000000004A0000008be920f5020100000055b5d3d496e196d624a471b818ba0b1778417ae335a544033536654fdda3eef602000000497b15826573b28c3e83a5d0c5ed30cf48b97dd6bd797849144ea24000000000")
     with self.assertRaises(MissingDataException):
         inv_message, _ = MessageSerializer(TESTNET).deserialize(serialized_inv_message)
     # checksum error
     serialized_inv_message = decodehexstr("fabfb5da696e76000000000000000000490000008bf920f5020100000055b5d3d496e196d624a471b818ba0b1778417ae335a544033536654fdda3eef602000000497b15826573b28c3e83a5d0c5ed30cf48b97dd6bd797849144ea24000000000")
     with self.assertRaises(FormatErrorException):
         inv_message, _ = MessageSerializer(TESTNET).deserialize(serialized_inv_message)
예제 #32
0
 def test_blocklocator_deserialize(self):
     loc, cursor = BlockLocatorSerializer().deserialize(decodehexstr("0100000005a329e1166037b2ea0cfbef5060c011c1cbbd37ad047a47b62d3dae020000000070bdb41302a622800ddfb2b3ebdd937fdca812ec8a7ab90ef250410700000000398052b24b2cc45ef953222e23880d25c3df95a913e72e9b015b34250000000066739a04b15f117929b581f6fe0ba2906bc4323829400f949086bb7d0000000008b067b31dc139ee8e7a76a4f2cfcca477c4c06e1ef89f4ae308951907000000"))
     self.assertEquals(loc, BlockLocator(version=1,
                                         blockhashlist=[Uint256.from_hexstr("0000000002ae3d2db6477a04ad37bdcbc111c06050effb0ceab2376016e129a3"),
                                                        Uint256.from_hexstr("00000000074150f20eb97a8aec12a8dc7f93ddebb3b2df0d8022a60213b4bd70"),
                                                        Uint256.from_hexstr("0000000025345b019b2ee713a995dfc3250d88232e2253f95ec42c4bb2528039"),
                                                        Uint256.from_hexstr("000000007dbb8690940f40293832c46b90a20bfef681b52979115fb1049a7366"),
                                                        Uint256.from_hexstr("00000007199508e34a9ff81e6ec0c477a4cccff2a4767a8eee39c11db367b008")]))
예제 #33
0
 def test_deserialize_getblocks_message(self):
     serialized_getblocks = decodehexstr("c87d0000016fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d61900000000000000000000000000000000000000000000000000000000000000000000000000")
     
     getblocks_msg, _ = GetblocksMessageSerializer().deserialize(serialized_getblocks)
     expected_msg = GetblocksMessage(BlockLocator(32200,
                                                   [Uint256.from_hexstr("000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f")]), 
                                      Uint256.zero())
     self.assertEquals(getblocks_msg, expected_msg)
예제 #34
0
 def test_txout_deserialize(self):
     txout, _ = TxoutSerializer().deserialize(
         decodehexstr(
             "802385d1050000001976a9144d8b17fbce571614be89df4bd872de892a47984488ac"
         ))
     self.assertEquals(
         txout,
         TxOut(value=24990000000,
               script=Script([
                   Instruction(OP_DUP),
                   Instruction(OP_HASH160),
                   Instruction(
                       20,
                       decodehexstr(
                           "4d8b17fbce571614be89df4bd872de892a479844")),
                   Instruction(OP_EQUALVERIFY),
                   Instruction(OP_CHECKSIG)
               ])))
예제 #35
0
    def test_deserialize_pubkey_outpoint(self):
        serialized_data = decodehexstr("022af4cc9ec3358354345c91694031a1fcdbe9a9064197521814e8a20fe018eb5f")
        
        pubkey_outpoint = PubKeyOutpointSerializer.deserialize(serialized_data, 
                                                               outpoint_type=OutpointIndex.PUBKEY)

        self.assertEquals(pubkey_outpoint, 
                          PubKeyOutpoint(PublicKey.from_hexstr("022af4cc9ec3358354345c91694031a1fcdbe9a9064197521814e8a20fe018eb5f"), 
                                                                is_pubkey_hash=False))
예제 #36
0
 def test_blockheader_deserialize(self):
     h, cursor = BlockheaderSerializer().deserialize(decodehexstr("01000000edfd7a7b12b27ac72c3e6776ef885a4a3ba3f687bc318c8151323a9fb8aa4b1e3ba3edfd7a7b12b27ac72c3e67768f617fc81bc3888a51323a9fb8aa4b1e5e4aa938bb49e703011d1dac2b7c"))
     self.assertEquals(h,
                       BlockHeader(version=1, 
                                   hash_prev=Uint256.from_hexstr("1e4baab89f3a3251818c31bc87f6a33b4a5a88ef76673e2cc77ab2127b7afded"), #hash_prev
                                   hash_merkle=Uint256.from_hexstr("4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b"), #merkle
                                   time=1237006505, 
                                   bits=486605799,
                                   nonce=2083236893)) 
예제 #37
0
 def test_timenetaddr_deserialize(self):
     timenetaddr, _ = TimenetaddrSerializer().deserialize(
         decodehexstr(
             "b5b5cd50000000000000000000000000000000000000ffffb203050c07d7")
     )
     self.assertEquals(
         timenetaddr,
         Timenetaddr(timestamp=1355658677,
                     netaddr=Netaddr(SERVICES_NONE, "178.3.5.12", 2007)))
예제 #38
0
    def test_validate_scripthash_multisig(self):
        # tx 1 of block 504 on testnet 3, 2 TX_SCRIPTHASH inputs, 2 PUBKEYHASH outputs
        # SCRIPTHASH of 1-of-2 and 2-of-3 multisig transactions
        # tx: 1915695f1de19d9a2e1fb1bd9c5e2f816022ec41a58e9f298396c8a4586a8c5e
        tx, _ = TxSerializer().deserialize(
            decodehexstr(
                "0100000002c142078d793bb5b4aadba6009e2b142872b1ed3a7f0f7336ebe0e38180d8d682010000009200483045022100d6868ef86978ffad889a312be2991cbfe2c2d27b40fed038dc65da83565f5b0f02205f7019af9b75a5e9ba9c394f6ce883fa72e8e0951cf2ca5011af5888026663f3014751210341d8a934cfca197c7acaa358a1c39b3317a3e6d80b808ad367278ddb6661d16f210220108eea941a69b20d320e9a82c4bc5f096edb7e7c74638810227594a8033c1d52aeffffffff3f2dc10288706fe1c3116bec01dc2a259bfc2802b60d9b13787195fa8205f0cf01000000b400473044022066952731f8c5663fd61b2cbc33361e79e27a8c0937c32b42cc2dd125215b98bb02202c90d8022337c868c1f00ab59a9cd833cad2522de4b703264e9b4e271d11cd12014c6951210220108eea941a69b20d320e9a82c4bc5f096edb7e7c74638810227594a8033c1d210386380591c6fe54fe67d54e809d38d2db63886c1d228beb744abc9327bee392882102c4c0101075ed2d413dd328841c87f7a39ff5a75d793decab1da9fb1128082c3b53aeffffffff02c11e1400000000001976a914724192ac202d067d06635dc043b84abdad14de0088ac2b540400000000001976a914342e5d1f2eb9c6e99fda90c85ca05aa36616644c88ac00000000"
            ))
        outscript1 = ScriptSerializer().deserialize(
            decodehexstr("a914d9f26cca817fa116dc76e1be7a17067eb843625087"))
        outscript2 = ScriptSerializer().deserialize(
            decodehexstr("a9145fc04212c7fb1103d99dd7ff454797a281ef92f587"))

        vm = TxValidationVM()
        valid, reason = vm.validate(tx, 0, outscript1, tx.in_list[0].script)
        assert valid, reason
        valid, reason = vm.validate(tx, 1, outscript2, tx.in_list[1].script)
        assert valid, reason
예제 #39
0
 def test_deserialize_tx_message(self):
     # TX 1 from block 398 on testnet 3
     serialized_tx_message = decodehexstr("010000000328ce10c7189026866bcfc1d06b278981ddf21ec0e364e28e294365b5c328cd43000000006c493046022100b918951cc55fbb285168004de7eab21e702e89dc43c167ba0fc1b09273e29cf5022100f95c24b6c740e2306a45bea1e7a0c8f2032c9d4da4d8d1cc6b5c4166d1fabfe50121033b2dd6fe53611ef3d112b7098a8999f48bf27c7bf94bd045439d87a9ec11fca3fffffffffc4f1ed498c5f31fe90b10389f12566a3350a5080db1dba1f01f8834e5813ca900000000484730440220161ee27efad282e8c984051418fcc2bca6854f3f5d4e24031b950bcd7853943a02202b5566c72aeaff2295db82c79d52d88dbe9450e01c6c951d5d0148c290f6cbbe01fffffffffc5eecfa90d46aeeda36bb1a2f2da61e4f9be81253033ae55625d00acb13ef35000000004a493046022100f7d996bc39ef00b217014ee3e7f8b49ed714fcce2fe970a2ada2b9a87a741c2d022100c55a6089c2b23ec87c5063513c6c8abfc14c913f8ab69cf4ebed6a58c220946901ffffffff0340f1f23a000000001976a914f069756ffd9d331119e5430268437aed14210afb88ac00f2052a0100000017a914290bba32a49315789a030bb40b0047f8fb90ff668700f2052a0100000017a9141d9ca71efa36d814424ea6ca1437e67287aebe348700000000")
     tx_message, _ = TxMessageSerializer().deserialize(serialized_tx_message)
     expected_message = TxMessage(Tx(version=1, 
             in_list=[TxIn(previous_output=Outpoint(hash=Uint256.from_hexstr("43cd28c3b56543298ee264e3c01ef2dd8189276bd0c1cf6b86269018c710ce28"),index=0), 
                script=Script(instructions=[Instruction(73,  decodehexstr("3046022100b918951cc55fbb285168004de7eab21e702e89dc43c167ba0fc1b09273e29cf5022100f95c24b6c740e2306a45bea1e7a0c8f2032c9d4da4d8d1cc6b5c4166d1fabfe501")),Instruction(33,  decodehexstr("033b2dd6fe53611ef3d112b7098a8999f48bf27c7bf94bd045439d87a9ec11fca3"))]), 
                sequence=4294967295),TxIn(previous_output=Outpoint(hash=Uint256.from_hexstr("a93c81e534881ff0a1dbb10d08a550336a56129f38100be91ff3c598d41e4ffc"),index=0), 
                script=Script(instructions=[Instruction(71,  decodehexstr("30440220161ee27efad282e8c984051418fcc2bca6854f3f5d4e24031b950bcd7853943a02202b5566c72aeaff2295db82c79d52d88dbe9450e01c6c951d5d0148c290f6cbbe01"))]), 
                sequence=4294967295),TxIn(previous_output=Outpoint(hash=Uint256.from_hexstr("35ef13cb0ad02556e53a035312e89b4f1ea62d2f1abb36daee6ad490faec5efc"),index=0), 
                script=Script(instructions=[Instruction(73,  decodehexstr("3046022100f7d996bc39ef00b217014ee3e7f8b49ed714fcce2fe970a2ada2b9a87a741c2d022100c55a6089c2b23ec87c5063513c6c8abfc14c913f8ab69cf4ebed6a58c220946901"))]), 
                sequence=4294967295)], 
             out_list=[TxOut(value=989000000, 
                 script=Script(instructions=[Instruction(OP_DUP),Instruction(OP_HASH160),Instruction(20,  decodehexstr("f069756ffd9d331119e5430268437aed14210afb")),Instruction(OP_EQUALVERIFY),Instruction(OP_CHECKSIG)])),TxOut(value=5000000000, 
                 script=Script(instructions=[Instruction(OP_HASH160),Instruction(20,  decodehexstr("290bba32a49315789a030bb40b0047f8fb90ff66")),Instruction(OP_EQUAL)])),TxOut(value=5000000000, 
                 script=Script(instructions=[Instruction(OP_HASH160),Instruction(20,  decodehexstr("1d9ca71efa36d814424ea6ca1437e67287aebe34")),Instruction(OP_EQUAL)]))], 
             locktime=0))
     self.assertEquals(tx_message, expected_message)
예제 #40
0
 def setUp(self):
     self.script1 = Script([
         Instruction(
             33,
             decodehexstr(
                 "02547b223d58eb5da7c7690748f70a3bab1509cb7578faac9032399f0b6bce31d6"
             )),
         Instruction(OP_CHECKSIG)
     ])
예제 #41
0
    def test_shamir_share_private_key(self):
        ssl_add_system_seeds()
        k = KEY()
        k.generate()
        pkey_bignum = k.get_privkey_bignum()
        pubkey = k.get_pubkey()
        numshares = 600
        threshold = 100
        sharenum_bytes = 2
        print "private_key_bignum:", pkey_bignum
        print "public_key:", hexstr(pubkey)
        print "address:", BitcoinAddress.from_publickey(pubkey, MAIN)
        
        field = ZpField()
        V = field.value_type
        ZpPkey = V(pkey_bignum)

        sharer = SecretSharer(field, ZpRandom(field))
        shares = sharer.share(ZpPkey, threshold, [V(i+1) for i in range(numshares)])
        # print shares
        print "Shamir Shares: (%d/%d):" % (threshold, numshares)
        shares_hex = [hexstr(base256encode(int(pt), sharenum_bytes) + base256encode(int(value), 32)) for pt, value in shares]
        
        for share in shares_hex:
            print share
        # Try to reconstruct the private key using the hex encoded shares.
        recombiner = SecretRecombiner(field)
        for i in range(10):
            random4_hex = random.sample(shares_hex, threshold)
            random4_decoded = [decodehexstr(h) for h in random4_hex]
            random4 = [(V(base256decode(data[:sharenum_bytes])), V(base256decode(data[sharenum_bytes:]))) for data in random4_decoded]
            recombined_pkey_bignum = recombiner.recombine(random4, V(0))
            assert recombined_pkey_bignum == ZpPkey
            k2 = KEY()
            k2.set_privkey_bignum(int(recombined_pkey_bignum))
            assert k2.get_pubkey() == pubkey
            print i
        # With threshold-1 shares this fails
        for i in range(10):
            random4_hex = random.sample(shares_hex, threshold-1)
            random4_decoded = [decodehexstr(h) for h in random4_hex]
            random4 = [(V(base256decode(data[:sharenum_bytes])), V(base256decode(data[sharenum_bytes:]))) for data in random4_decoded]
            recombined_pkey_bignum = recombiner.recombine(random4, V(0))
            assert recombined_pkey_bignum != ZpPkey
예제 #42
0
 def test_varint_deserialize(self):
     self.assertEquals(VarintSerializer().deserialize(decodehexstr("05"))[0], 5)
     self.assertEquals(VarintSerializer().deserialize(decodehexstr("fc"))[0], 252)
     self.assertEquals(VarintSerializer().deserialize(decodehexstr("fdfd00"))[0], 253)
     self.assertEquals(VarintSerializer().deserialize(decodehexstr("fdffff"))[0], 65535)
     self.assertEquals(VarintSerializer().deserialize(decodehexstr("fe00000100"))[0], 65536)
     self.assertEquals(VarintSerializer().deserialize(decodehexstr("feffffffff"))[0], 4294967295)
     self.assertEquals(VarintSerializer().deserialize(decodehexstr("ff0000000001000000"))[0], 4294967296)
예제 #43
0
 def test_deserialize_block_message(self):
     #block 22 on testnet 3
     serialized_block_msg= decodehexstr("0100000073379e3ff3dffd006e0090e52ac571a9a309490a23e64d15f8af291a0000000051f1c5b2b7c8f980e7715b4d3ce0180f99c44a16fc9c00ede2f5984b8d7cc22d16ed494dffff001d0082467f0101000000010000000000000000000000000000000000000000000000000000000000000000ffffffff0f0416ed494d02fa00062f503253482fffffffff0100f2052a01000000232103b787920594cb47fbfccee915befae40ac2e2b295224460eee075ad0ada4c0c54ac00000000")
     
     block_msg, _ = BlockMessageSerializer().deserialize(serialized_block_msg)
     expected_block_msg = BlockMessage(Block(blockheader=BlockHeader(version=1, 
                       hash_prev=Uint256.from_hexstr("000000001a29aff8154de6230a4909a3a971c52ae590006e00fddff33f9e3773"), 
                       hash_merkle=Uint256.from_hexstr("2dc27c8d4b98f5e2ed009cfc164ac4990f18e03c4d5b71e780f9c8b7b2c5f151"), 
                       time=1296690454,
                       bits=486604799,
                       nonce=2135327232), 
                 transactions=[Tx(version=1, 
                     in_list=[TxIn(previous_output=Outpoint(hash=Uint256.from_hexstr("0000000000000000000000000000000000000000000000000000000000000000"),index=4294967295), 
                        script=Script(instructions=[Instruction(4,  decodehexstr("16ed494d")),Instruction(2,  decodehexstr("fa00")),Instruction(6,  decodehexstr("2f503253482f"))]), 
                        sequence=4294967295)], 
                     out_list=[TxOut(value=5000000000, 
                         script=Script(instructions=[Instruction(33,  decodehexstr("03b787920594cb47fbfccee915befae40ac2e2b295224460eee075ad0ada4c0c54")),Instruction(OP_CHECKSIG)]))], 
                     locktime=0)]))
     self.assertEquals(block_msg, expected_block_msg)
예제 #44
0
 def test_uint256_deserialize(self):
     uint256, _ = Uint256Serializer().deserialize(
         decodehexstr(
             "6a921ac1ae5e23610d07d2b2a9377fbc3c3bb233e900aa1a5bc40f5562a19d0d"
         ))
     self.assertEquals(
         uint256,
         Uint256.from_hexstr(
             "0d9da162550fc45b1aaa00e933b23b3cbc7f37a9b2d2070d61235eaec11a926a"
         ))
예제 #45
0
 def test_tx_serialize(self):
     tx = 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(hexstr(TxSerializer().serialize(tx)), "01000000010e0907321e9fcb500c20eb658667b40ac721870ddc2ce165ab53a47b68cbc517000000008b483045022100ab2dc8932ca1d26f4cdac1feae09020a60ccc4d17b14e5fc5b21f3ab8c3a9cee022040a7208c172d19a19902280d66201c7fe2c3d8b2df7e23cc4e5b70fd52ecba2c014104b77dd1f3a21cb3d067a7e76982a609d7310f8692f5d61346f3225323c425604a0c12862755335c49e392673106adfc5dfdee1e4d367f10353e8911fac687db3effffffff02802385d1050000001976a9144d8b17fbce571614be89df4bd872de892a47984488ac00f2052a010000001976a914fadad27c40adbe230f5e3c04d44a29297508483188ac00000000")
예제 #46
0
 def test_outpoint_deserialize(self):
     outpoint, _ = OutpointSerializer().deserialize(
         decodehexstr(
             "0e0907321e9fcb500c20eb658667b40ac721870ddc2ce165ab53a47b68cbc51700000000"
         ))
     self.assertEquals(
         outpoint,
         Outpoint(hash=Uint256.from_hexstr(
             "17c5cb687ba453ab65e12cdc0d8721c70ab4678665eb200c50cb9f1e3207090e"
         ),
                  index=0))
예제 #47
0
 def test_address_from_public_key(self):
     """
     public: 023053536687205cbf57a25386ac466c7f85105032ced1ae9c54486a83c6dd3bab
     private: 049db42589b263e8700eb747a402b74604aae54ebc04f1cbe9a1cf584683f100
     """
     addr1 = BitcoinAddress.from_publickey(
         decodehexstr(
             "023053536687205cbf57a25386ac466c7f85105032ced1ae9c54486a83c6dd3bab"
         ), MAIN)
     self.assertEquals(addr1.to_base58addr(),
                       "171waY81rzeFaBYhsiKibhBW5WAG8X7DLk")
예제 #48
0
    def test_deserialize_multisig_outpoint(self):
        serialized_data = decodehexstr("0000000200000003022af4cc9ec3358354345c91694031a1fcdbe9a9064197521814e8a20fe018eb5f022dc45c9ec3358354345c91694031a1fcdbe9a9064197521814e8a20fe018eb5f029ce0129ec3358354345c91694031a1fcdbe9a9064197521814e8a20fe018eb5f")
        
        outpoint = MultiSigOutpointSerializer.deserialize(serialized_data)

        self.assertEquals(outpoint, 
                          MultiSigOutpoint(2, 3,
                                    [PublicKey.from_hexstr("022af4cc9ec3358354345c91694031a1fcdbe9a9064197521814e8a20fe018eb5f"), 
                                     PublicKey.from_hexstr("022dc45c9ec3358354345c91694031a1fcdbe9a9064197521814e8a20fe018eb5f"), 
                                     PublicKey.from_hexstr("029ce0129ec3358354345c91694031a1fcdbe9a9064197521814e8a20fe018eb5f")])
                          )
예제 #49
0
    def test_validate_pubkey(self):
        # tx 1 of block 383 on testnet 3 (2 PUBKEY inputs)
        # tx: 43cd28c3b56543298ee264e3c01ef2dd8189276bd0c1cf6b86269018c710ce28
        tx, _ = TxSerializer().deserialize(
            decodehexstr(
                "01000000028e3430573cfde2f3e1eece8aefe661dd841bcb665d35832415bab4f75267852200000000494830450221009cca8fb1c4a34982c4e9a59fd15856404c02085f926043cfc6664924a5b9e36d02205811f85b395c43492164b8b33867d745211d8c7ffdcd1b288e56f74dbdaed15b01ffffffffe3abf5981a1bd6457ec0cdcab76cc2a176dc0d7e16f6d3781aebc684f13cc4fd00000000484730440220387bbcee99e370ea2115eaccfd8c48eb380653303b66c6946e0903fb0bbc6f8602202b2ef048519d45525413f64b727a1d85ff2cf7d91ab4607308224b4c30d5846801ffffffff0240f1f23a000000001976a9142203ca59edf66969757e6cc9238b1b36f4dc35b888acc0f218190200000017a9149eb21980dc9d413d8eac27314938b9da920ee53e8700000000"
            ))
        outscript1 = ScriptSerializer().deserialize(
            decodehexstr(
                "2102547b223d58eb5da7c7690748f70a3bab1509cb7578faac9032399f0b6bce31d6ac"
            ))
        outscript2 = ScriptSerializer().deserialize(
            decodehexstr(
                "2103fcc9ce029ad74af9fecacce68bcc775cc6efcb000a0b8cc2b3aacad4850bc4b0ac"
            ))

        vm = TxValidationVM()
        valid, reason = vm.validate(tx, 0, outscript1, tx.in_list[0].script)
        assert valid, reason
        valid, reason = vm.validate(tx, 1, outscript2, tx.in_list[1].script)
        assert valid, reason
예제 #50
0
 def test_invitem_deserialize(self):
     blockitem, _ = InvitemSerializer().deserialize(
         decodehexstr(
             "0200000008b067b31dc139ee8e7a76a4f2cfcca477c4c06e1ef89f4ae308951907000000"
         ))
     txitem, _ = InvitemSerializer().deserialize(
         decodehexstr(
             "0100000088f2275b202226394d27af9a80b3dcf785834da5a3050eb7a0a091a3f33ceb84"
         ))
     self.assertEquals(
         blockitem,
         Invitem(
             INV_BLOCK,
             Uint256.from_hexstr(
                 "00000007199508e34a9ff81e6ec0c477a4cccff2a4767a8eee39c11db367b008"
             )))
     self.assertEquals(
         txitem,
         Invitem(
             INV_TX,
             Uint256.from_hexstr(
                 "84eb3cf3a391a0a0b70e05a3a54d8385f7dcb3809aaf274d392622205b27f288"
             )))
예제 #51
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"
     )
예제 #52
0
    def test_deserialize_pubkey_outpoint(self):
        serialized_data = decodehexstr(
            "022af4cc9ec3358354345c91694031a1fcdbe9a9064197521814e8a20fe018eb5f"
        )

        pubkey_outpoint = PubKeyOutpointSerializer.deserialize(
            serialized_data, outpoint_type=OutpointIndex.PUBKEY)

        self.assertEquals(
            pubkey_outpoint,
            PubKeyOutpoint(PublicKey.from_hexstr(
                "022af4cc9ec3358354345c91694031a1fcdbe9a9064197521814e8a20fe018eb5f"
            ),
                           is_pubkey_hash=False))
예제 #53
0
 def test_decrypt_private_key(self):
     master_key = MasterKey(crypted_key=decodehexstr(
         "be4afa6923ad06790b0f8c3345131499cf2b149ca422bd11a7e67a76347c51a456a2d626f75da1ff809632fca7165d71"
     ),
                            salt=decodehexstr("8cdcbd8a494b0eeb"),
                            derivation_method=MasterKey.
                            DERIVMETHOD_EVP_SHA512,
                            derive_iterations=45193,
                            other_derivation_parameters="")
     #Decrypt the master crypted_key using the passphrase
     plain_masterkey = decrypt_masterkey(master_key, "hello")
     print "plain_masterkey:", hexstr(plain_masterkey)
     assert hexstr(
         plain_masterkey
     ) == "56722b42c4b9f8689fe9b38745fe75af92d0a50d6fd94b34de6b6d5e287bbed3"
     #Decrypt a crypted_secret
     public_key = decodehexstr(
         "046a82d73af2cc093e3df7ae0185f045946970bcd5f0ef26f82d4f9a24e0d50f977c51e311e079e3183cfadd67d9b3f089fe7ba94a196c365fbd9e03b8c423787d"
     )
     crypted_secret = decodehexstr(
         "ff914ab69f58af92ac56de85051441e729cc51e11608d563e2a266ce3b8c59f573ed6a1828ff98fadb345890b6ed2626"
     )
     crypter2 = Crypter()
     crypter2.set_key(plain_masterkey, doublesha256(public_key))
     secret = crypter2.decrypt(crypted_secret)
     print "secret:", hexstr(secret)
     assert hexstr(
         secret
     ) == "1c7552a9b755d29d081efd71f7811cc8ab2c9f2c634f489e6b45700711c8a304"
     #Test the secret
     k = KEY()
     k.set_secret(secret)
     assert k.get_pubkey() == public_key
     sig1 = k.sign("sign something")
     k2 = KEY()
     k2.set_pubkey(public_key)
     assert k2.verify("sign something", sig1) == 1
예제 #54
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"
     )
예제 #55
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"
            ))
예제 #56
0
    def test_deserialize_outpoint_index(self):
        serialized_data = decodehexstr(
            "0000000c2af4cc9ec3358354345c91694031a1fcdbe9a9064197521814e8a20fe018eb5f030200000004022af4cc9ec3358354345c91691031a1fcdbe9a9064197521814e8a20fe018eb5f"
        )

        outpoint_index = OutpointIndexSerializer.deserialize(serialized_data)

        self.assertEquals(
            outpoint_index,
            OutpointIndex(
                12,
                Uint256.from_hexstr(
                    "2af4cc9ec3358354345c91694031a1fcdbe9a9064197521814e8a20fe018eb5f"
                ), 3, OutpointIndex.PUBKEY_HASH, 4,
                PubKeyOutpoint(PublicKey.from_hexstr(
                    "022af4cc9ec3358354345c91691031a1fcdbe9a9064197521814e8a20fe018eb5f"
                ),
                               is_pubkey_hash=True)))
예제 #57
0
 def test_blockheader_deserialize(self):
     h, cursor = BlockheaderSerializer().deserialize(
         decodehexstr(
             "01000000edfd7a7b12b27ac72c3e6776ef885a4a3ba3f687bc318c8151323a9fb8aa4b1e3ba3edfd7a7b12b27ac72c3e67768f617fc81bc3888a51323a9fb8aa4b1e5e4aa938bb49e703011d1dac2b7c"
         ))
     self.assertEquals(
         h,
         BlockHeader(
             version=1,
             hash_prev=Uint256.from_hexstr(
                 "1e4baab89f3a3251818c31bc87f6a33b4a5a88ef76673e2cc77ab2127b7afded"
             ),  #hash_prev
             hash_merkle=Uint256.from_hexstr(
                 "4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b"
             ),  #merkle
             time=1237006505,
             bits=486605799,
             nonce=2083236893))