def parse_script(json_script):
    serializer = ScriptSerializer()
    if json_script != "":
        instr = [parse_instruction(elm) for elm in re.split("[ \n\t]", json_script)]
        script_bin = "".join(instr)
    else:
        script_bin = ""
    result = serializer.deserialize(script_bin)
    return result
Exemple #2
0
def parse_script(json_script):
    serializer = ScriptSerializer()
    if json_script != "":
        instr = [
            parse_instruction(elm) for elm in re.split("[ \n\t]", json_script)
        ]
        script_bin = "".join(instr)
    else:
        script_bin = ""
    result = serializer.deserialize(script_bin)
    return result
Exemple #3
0
class VarstrScriptSerializer(Serializer):
    def __init__(self):
        self.serializer = ScriptSerializer()
        self.strencoder = VarstrSerializer()

    def serialize(self, script):
        scriptstr = self.serializer.serialize(script)
        return (self.strencoder.serialize(scriptstr))

    def get_size(self, script):
        return self.strencoder.get_size_for_len(self.serializer.get_size(script))

    def deserialize(self, data, cursor=0):
        scriptstr, newcursor = self.strencoder.deserialize(data, cursor)
        return (self.serializer.deserialize(scriptstr), newcursor)
class VarstrScriptSerializer(Serializer):
    def __init__(self):
        self.serializer = ScriptSerializer()
        self.strencoder = VarstrSerializer()

    def serialize(self, script):
        scriptstr = self.serializer.serialize(script)
        return (self.strencoder.serialize(scriptstr))

    def get_size(self, script):
        return self.strencoder.get_size_for_len(
            self.serializer.get_size(script))

    def deserialize(self, data, cursor=0):
        scriptstr, newcursor = self.strencoder.deserialize(data, cursor)
        return (self.serializer.deserialize(scriptstr), newcursor)
Exemple #5
0
class TxVerifier():
    def __init__(self, runmode):
        self.runmode = runmode
        self.tx_serializer = TxSerializer()
        self.script_serializer = ScriptSerializer()

    """
        basic_check: run tests that don't require any context.
    """

    def basic_checks(self, tx):
        self.check_size_limit(tx)
        self.check_vin_empty(tx)
        self.check_vout_empty(tx)
        self.check_money_range(tx)
        self.check_dupplicate_inputs(tx)
        self.check_coinbase_script_size(tx)
        self.check_null_inputs(tx)

    def check_size_limit(self, tx):
        if not tx.rawdata:
            tx.rawdata = self.tx_serializer.serialize(tx)
        if len(tx.rawdata) > MAX_BLOCK_SIZE:
            raise Exception("Transaction too large : %d bytes" %
                            (len(tx.rawdata)))

    def check_vin_empty(self, tx):
        if (not tx.in_list):
            raise Exception("vin empty")

    def check_vout_empty(self, tx):
        if (not tx.out_list):
            raise Exception("vout empty")

    def check_money_range(self, tx):
        for txout in tx.out_list:
            if not is_money_range(txout.value):
                raise Exception("txout not in money range")
        if not is_money_range(sum(txout.value for txout in tx.out_list)):
            raise Exception("txout total not in money range")

    def check_dupplicate_inputs(self, tx):
        inputs = set()
        for txin in tx.in_list:
            if txin.previous_output in inputs:
                raise Exception("dupplicate txin")
            inputs.add(txin.previous_output)

    def check_coinbase_script_size(self, tx):
        if tx.iscoinbase():
            bin_script = self.script_serializer.serialize(tx.in_list[0].script)
            if (len(bin_script) < 2 or len(bin_script) > 100):
                raise Exception("incorrect coinbase script size : %d" %
                                (len(bin_script)))

    def check_null_inputs(self, tx):
        if not tx.iscoinbase():
            for txin in tx.in_list:
                if (txin.previous_output.is_null()):
                    raise Exception("null prevout")
Exemple #6
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
Exemple #7
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
Exemple #8
0
class TxVerifier():
    def __init__(self, runmode):
        self.runmode = runmode
        self.tx_serializer = TxSerializer()
        self.script_serializer = ScriptSerializer()
    """
        basic_check: run tests that don't require any context.
    """
    def basic_checks(self, tx):
        self.check_size_limit(tx)
        self.check_vin_empty(tx)
        self.check_vout_empty(tx)
        self.check_money_range(tx)
        self.check_dupplicate_inputs(tx)
        self.check_coinbase_script_size(tx)
        self.check_null_inputs(tx)
    
    def check_size_limit(self, tx):
        if not tx.rawdata:
            tx.rawdata = self.tx_serializer.serialize(tx)
        if len(tx.rawdata) > MAX_BLOCK_SIZE:
            raise Exception("Transaction too large : %d bytes" % (len(tx.rawdata)))
    
    def check_vin_empty(self, tx):
        if (not tx.in_list):
            raise Exception("vin empty" )
        
    def check_vout_empty(self, tx):
        if (not tx.out_list):
            raise Exception("vout empty" )
                
    def check_money_range(self, tx):
        for txout in tx.out_list:
            if not is_money_range(txout.value):
                raise Exception("txout not in money range")
        if not is_money_range(sum(txout.value for txout in tx.out_list)):
            raise Exception("txout total not in money range")

    def check_dupplicate_inputs(self, tx):
        inputs = set()
        for txin in tx.in_list:
            if txin.previous_output in inputs:
                raise Exception("dupplicate txin")
            inputs.add(txin.previous_output)

    def check_coinbase_script_size(self, tx):
        if tx.iscoinbase():
            bin_script = self.script_serializer.serialize(tx.in_list[0].script)
            if (len(bin_script) < 2 or len(bin_script) > 100):
                raise Exception("incorrect coinbase script size : %d" % (len(bin_script)))
            
    def check_null_inputs(self, tx):
        if not tx.iscoinbase():
            for txin in tx.in_list:
                if (txin.previous_output.is_null()):
                    raise Exception("null prevout")
Exemple #9
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
Exemple #10
0
 def validate_p2sh(self, transaction, inputindex, unspent_script, claim_script):
     if not claim_script.is_pushonly():
         raise  Exception("scriptSig not PUSH_DATA only in P2SH transaction" + str(claim_script))
     # Eval and check the hash
     self.eval(claim_script)        
     if len(self.stack) < 1:
         raise Exception("P2SH script: Missing argument")
     p2sh_script = ScriptSerializer().deserialize(self.stack[-1])
     self.eval(unspent_script)
     if not self.stack.is_true():
         raise Exception("P2SH script doesn't match HASH160")
     self.stack.pop()
     self.set_checksig_data(transaction, inputindex, unspent_script)
     self.stack.clear_altstack() # alt stack not shared between sig/pubkey
     self.eval(p2sh_script)
     if not self.stack.is_true():
         raise Exception("Top of stack doens't contain 'True'")
Exemple #11
0
        if self.cond_stack:
            raise Exception("Conditionals not matching")

    def set_checksig_data(self, transaction, inputindex, unspent_script):
        self.checksig_data = (transaction, inputindex, unspent_script.signed_part())  
    
    """
        ['OP_PUSHDATA(73:30460221...]', 'OP_PUSHDATA(65:048791c5...]']
        ['OP_DUP', 'OP_HASH160', 'OP_PUSHDATA(20:76cd3eb3...]', 'OP_EQUALVERIFY', 'OP_CHECKSIG']
    """

if __name__ == '__main__':
    script_in = [73, 48, 70, 2, 33, 0, 188, 221, 205, 147, 181, 60, 249, 233, 89, 25, 224, 231, 189, 215, 220, 191, 14, 134, 226, 144, 44, 104, 222, 114, 183, 105, 188, 254, 100, 104, 201, 6, 2, 33, 0, 254, 79, 216, 238, 184, 16, 161, 169, 207, 84, 125, 153, 254, 113, 118, 138, 213, 121, 191, 241, 96, 53, 230, 134, 144, 161, 236, 164, 171, 154, 185, 20, 1, 65, 4, 135, 145, 197, 22, 141, 185, 55, 52, 230, 127, 0, 161, 37, 96, 89, 76, 201, 148, 92, 112, 134, 43, 85, 56, 39, 116, 187, 189, 33, 94, 55, 62, 198, 244, 141, 149, 104, 149, 173, 203, 119, 180, 57, 213, 161, 186, 248, 44, 10, 231, 179, 146, 77, 86, 251, 199, 167, 244, 243, 180, 31, 101, 116, 95]
    script_out = [118, 169, 20, 118, 205, 62, 179, 249, 40, 71, 171, 78, 57, 98, 179, 1, 106, 201, 223, 31, 19, 69, 100, 136, 172]
    from coinpy.lib.serialization.scripts.serialize import ScriptSerializer
    s1 = ScriptSerializer().deserialize("".join(chr(c) for c in script_in))
    s2 = ScriptSerializer().deserialize("".join(chr(c) for c in script_out))
    print [str(i) for i in s1.instructions]
    print [str(i) for i in s2.instructions]
    
    txdata = "0100000003219f0a306fa848cc4d028d90fc2c566acb5f594d01f2aacb4c09ce362903ee82000000008b483045022100a63ae2cc3e8ffc169d6e5169c6d6b2af3446efe268adebede497c6fca4b6f67f02205d838c285af5161032db5474bfdec4e69e2e0f66b8ef5458b5b18281eeabbc2b014104ab35c8334824f753e7502948d03aef75d7c7a724a7f2363256d4055c5d06aae9c0412fe01aabd5069f2c60477372bbee3b6aa1c9e2bbc95b280c9a28ed4c16a4ffffffff8bac88cc30abb176a17c5d91f66d2aafd5443817edef88bad43d161b4e3432ce000000008a47304402206a6e84e844210abdb42e71864d64942dc1e8d37ba6e85853596b9bf56bc3eda7022032c5ade4b0bfe019581756ccf063d72a1a3a13c343177443a97e3392406e1ef5014104a4586fe5c49cb2e4510caaaf45e01d31bcfeddaa9bb043b6deb56fd6ab23d7187796d82c45c3a55824b6e32dd45e56182ffb570299569086a24f4ba94419c26dffffffffc93eaf1a0b51a96e8d6facd60be8924b6d7618d2baca23abfe23d97178c17541000000008b48304502204dc83602eac8b2382ebd851f7bda02fae6a4f9b32d2fda7ac1dd977a417718c4022100d4fded75970fad1e51a392f735663fa7e3f794b2cdfe1c9708cc977677a0620a0141049171f7af90d0d6e8ef89a19844a9f685e96606065590d6e17c6ba2f1383fa679fd00a4c41274abfa3234fb7b7747ea167299c963ea3ee4b991c93507a456a08dffffffff0240ac2700000000001976a914119017a544834d557fe126f9411cdad07c99a7b888ac801d2c04000000001976a914129d3ea19d92a9620cf5d17c25a4f0288bd2951688ac00000000"
    tx, cursor = tx_encoder().decode(decodehexstr(txdata))
    unspent_script_hex = "76a914db1cab70296702a214726b5eb0b7da5dac99b46388ac"
    unspent_script = ScriptSerializer().deserialize( decodehexstr(unspent_script_hex))
    inputindex = 0
    
    vm = TxValidationVM()
    isvalid = vm.validate(tx, inputindex, unspent_script, tx.in_list[inputindex].script)
    print isvalid

Exemple #12
0
 def __init__(self, runmode):
     self.runmode = runmode
     self.tx_serializer = TxSerializer()
     self.script_serializer = ScriptSerializer()
Exemple #13
0
def cached_serialized_size(script):
    if script.serialized_size is None:
        script.serialized_size = ScriptSerializer().get_size(script)
    return script.serialized_size
 def __init__(self):
     self.serializer = ScriptSerializer()
     self.strencoder = VarstrSerializer()
Exemple #15
0
 def __init__(self):
     self.serializer = ScriptSerializer()
     self.strencoder = VarstrSerializer()
Exemple #16
0
 def __init__(self, runmode):
     self.runmode = runmode
     self.tx_serializer = TxSerializer()
     self.script_serializer = ScriptSerializer()