Пример #1
0
 def stream_deserialize_value(self, f):
     lut = {
         FieldType.Type.u8: lambda: ser_read(f, 1),
         FieldType.Type.u16: lambda: struct.unpack(b'<H', ser_read(f, 2))[0],
         FieldType.Type.u32: lambda: struct.unpack(b'<I', ser_read(f, 4))[0],
         FieldType.Type.u64: lambda: struct.unpack(b'<Q', ser_read(f, 8))[0],
         FieldType.Type.i8: lambda: ser_read(f, 1),
         FieldType.Type.i16: lambda: struct.unpack(b'<h', ser_read(f, 2))[0],
         FieldType.Type.i32: lambda: struct.unpack(b'<i', ser_read(f, 4))[0],
         FieldType.Type.i64: lambda: struct.unpack(b'<q', ser_read(f, 8))[0],
         FieldType.Type.vi: lambda: VarIntSerializer.stream_deserialize(f),
         FieldType.Type.fvi: lambda: FlagVarIntSerializer.stream_deserialize(f),
         FieldType.Type.str: lambda: VarStringSerializer.stream_deserialize(f).decode('utf-8'),
         FieldType.Type.bytes: lambda: BytesSerializer.stream_deserialize(f),
         FieldType.Type.sha256: lambda: Hash256Id.stream_deserialize(f),
         FieldType.Type.sha256d: lambda: Hash256Id.stream_deserialize(f),
         FieldType.Type.ripmd160: lambda: Hash160Id.stream_deserialize(f),
         FieldType.Type.hash160: lambda: Hash160Id.stream_deserialize(f),
         FieldType.Type.pubkey: lambda: PubKey.stream_deserialize(f),
         FieldType.Type.ecdsa: lambda: None,
     }
     if self.type in lut.keys():
         return lut[self.type]()
     else:
         raise NotImplementedError()
Пример #2
0
    def stream_deserialize(cls, f):
        prev_out = OutPoint.stream_deserialize(f)
        sequence = struct.unpack(b'<I', ser_read(f, 4))[0]

        value = struct.unpack(b'<q', ser_read(f, 8))[0]
        block_height = struct.unpack(b'<I', ser_read(f, 4))[0]
        block_index = struct.unpack(b'<I', ser_read(f, 4))[0]
        sig_script = BytesSerializer.stream_deserialize(f)
        return cls(prev_out, sequence, value, block_height, block_index, sig_script)
Пример #3
0
 def deserialize_field(self, tx, kwargs, attr, fmt, num_bytes, f):
     if fmt not in ['inputs', 'outputs', 'bytes']:
         kwargs[attr] = struct.unpack(fmt, ser_read(f, num_bytes))[0]
     elif fmt == 'inputs':
         kwargs[attr] = VectorSerializer.stream_deserialize(CMutableTxIn, f)
     elif fmt == 'outputs':
         kwargs[attr] = VectorSerializer.stream_deserialize(CMutableTxOut, f)
     elif fmt == 'bytes':
         kwargs[attr] =  BytesSerializer.stream_deserialize(f)
Пример #4
0
    def deserialize_witness(self, f):
        value = struct.unpack(b'<q', ser_read(f, 8))[0]
        block_height = struct.unpack(b'<I', ser_read(f, 4))[0]
        block_index = struct.unpack(b'<I', ser_read(f, 4))[0]
        sig_script = BytesSerializer.stream_deserialize(f)

        self.value = value
        self.block_height = block_height
        self.block_index = block_index
        self.sig_script = sig_script
Пример #5
0
    def stream_deserialize(cls, f):
        self = super(Block, cls).stream_deserialize(f)
        for attr, fmt, num_bytes, _ in self.block_fields:
            if fmt not in ['bytes', 'vectortx']:
                setattr(self, attr, struct.unpack(fmt, ser_read(f, num_bytes))[0])
            elif fmt == 'bytes':
                setattr(self, attr, BytesSerializer.stream_deserialize(f))
            elif fmt == 'vectortx':
                setattr(self, attr, VectorSerializer.stream_deserialize(Transaction, f))

        setattr(self, 'vMerkleTree', tuple(Block.build_merkle_tree_from_txs(getattr(self, 'vtx'))))
        return self
Пример #6
0
 def stream_deserialize(cls, f):
     self = cls()
     for attr, fmt, num_bytes, _ in self.fields:
         if fmt not in ['inputs', 'outputs', 'bytes']:
             setattr(self, attr, struct.unpack(fmt, ser_read(f, num_bytes))[0])
         elif fmt == 'inputs':
             setattr(self, attr, VectorSerializer.stream_deserialize(CTxIn, f))
         elif fmt == 'outputs':
             setattr(self, attr, VectorSerializer.stream_deserialize(CTxOut, f))
         elif fmt == 'bytes':
             setattr(self, attr, BytesSerializer.stream_deserialize(f))
     return self
Пример #7
0
 def stream_deserialize(cls, f):
     value = struct.unpack(b'<q', ser_read(f, 8))[0]
     version = struct.unpack(b'<H', ser_read(f, 2))[0]
     pk_script = BytesSerializer.stream_deserialize(f)
     return cls(value, version, pk_script)
Пример #8
0
    def deserialize_witness_value_signing(self, f):
        value = struct.unpack(b'<q', ser_read(f, 8))[0]
        sig_script = BytesSerializer.stream_deserialize(f)

        self.value = value
        self.sig_script = sig_script
Пример #9
0
 def deserialize_witness_signing(self, f):
     sig_script = BytesSerializer.stream_deserialize(f)
     self.sig_script = sig_script
Пример #10
0
 def stream_deserialize(cls, f):
     hash = ser_read(f, 32)
     vin = CTxIn.stream_deserialize(f)
     sig = BytesSerializer.stream_deserialize(f)
     height = struct.unpack(b"<I", ser_read(f, 4))[0]
     return cls(hash, vin, sig, height)
Пример #11
0
 def stream_deserialize(cls, f):
     hash = ser_read(f, 32)
     vin = CTxIn.stream_deserialize(f)
     sig = BytesSerializer.stream_deserialize(f)
     height = struct.unpack(b"<I", ser_read(f, 4))[0]
     return cls(hash, vin, sig, height)
Пример #12
0
    def stream_deserialize(cls, f, **kwargs):
        schema_obj = kwargs['schema_obj'] if 'schema_obj' in kwargs else None
        if not isinstance(schema_obj, Schema):
            raise ValueError(
                f'`schema_obj` parameter must be of Schema type; got `{schema_obj}` instead'
            )

        # Deserialize proof header
        # - version with flag
        (ver, flag) = FlagVarIntSerializer.stream_deserialize(f)
        # - fields common for root and upgrade proofs
        if flag:
            schema = Hash256Id.stream_deserialize(f)
            network = VarIntSerializer.stream_deserialize(f)
            if network is 0x00:
                format = ProofFormat.upgrade
            # - root-specific fields
            else:
                network = Network(network)
                format = ProofFormat.root
                root = OutPoint.stream_deserialize(f, short=False)
        else:
            format = ProofFormat.ordinary

        # Deserialize proof body
        # - reading proof type
        type_no = ser_read(f, 1)[0]

        # - reading `seal_sequence` structure
        seals = []
        seal_type_no = 0
        # -- we iterate over the seals until 0xFF (=FlagVarIntSerializer.Separator.EOF) byte is met
        while True:
            try:
                # -- reading seal with the current type number
                seal = Seal.stream_deserialize(f,
                                               type_no=seal_type_no,
                                               schema_obj=schema_obj)
            except BaseException as ex:
                # due to some strange bug, python 3 is unable to capture SeparatorByteSignal exception by its type,
                # and `isinstance(ex, SeparatorByteSignal)` returns False as well :(
                # so we have to capture generic exception and re-raise if it is not SeparatorByteSignal, which
                # can be determined only by the presence of its method
                if not callable(getattr(ex, "is_eol", None)):
                    raise
                if ex.is_eol():
                    # -- met 0xFE separator byte, increasing current type number
                    seal_type_no = seal_type_no + 1
                elif ex.is_eof():
                    # -- end of `seal_sequence` structure
                    break
            else:
                # -- otherwise append read seal to the list of seals
                seals.append(seal)

        # -- if we had zero seals implies proof of state destruction format
        if len(seals) is 0:
            format = ProofFormat.burn

        # - reading unparsed state and metadata bytes
        state = BytesSerializer.stream_deserialize(f)
        metadata = BytesSerializer.stream_deserialize(f)

        # Deserialize original public key
        pkcode = ser_read(f, 1)
        if pkcode is 0x00:
            pubkey = None
        else:
            buf = pkcode + ser_read(f, 32)
            pubkey = PubKey.deserialize(buf)

        # Deserialize prunable data
        try:
            pruned_flag = ser_read(f, 1)
        except:
            pruned_flag = 0x00

        txid, parents = None, None
        if pruned_flag & 0x01 > 0:
            txid = Hash256Id.stream_deserialize(f)
        if pruned_flag & 0x02 > 0:
            parents = VectorSerializer.stream_deserialize(Hash256Id, f)

        proof = Proof(schema_obj=schema_obj,
                      type_no=type_no,
                      ver=ver,
                      format=format,
                      schema=schema,
                      network=network,
                      root=root,
                      pubkey=pubkey,
                      fields=None,
                      seals=seals,
                      txid=txid,
                      parents=parents,
                      metadata=metadata,
                      state=state)

        # Parsing raw seals and metadata and resolving types against the provided Schema
        if 'schema_obj' in kwargs:
            schema_obj = kwargs['schema_obj']
        if isinstance(schema_obj, Schema):
            proof.resolve_schema(schema_obj)

        return proof