Example #1
0
    def serialize_prefix(self, f):
        VarIntSerializer.stream_serialize(len(self.txins), f)
        for txin in self.txins:
            txin.serialize_prefix(f)

        VectorSerializer.stream_serialize(TxOut, self.txouts, f)
        f.write(struct.pack(b'<I', self.locktime))
        f.write(struct.pack(b'<I', self.expiry))
Example #2
0
 def stream_serialize(self, f):
     VarIntSerializer.stream_serialize(len(self.pubkeys), f)
     for pubkey in self.pubkeys:
         pubkey_bytes = pubkey.to_bytes()
         assert len(pubkey_bytes) == PUB_KEY_LENGTH
         f.write(pubkey_bytes)
         # BytesSerializer.stream_serialize(pubkey, f)
     f.write(struct.pack(b"<i", self.nMinBlockHeight))
     f.write(struct.pack(b"<i", self.nMaxBlockHeight))
     assert len(self.hashReplySqk) == HASH_LENGTH
     f.write(self.hashReplySqk)
Example #3
0
 def stream_serialize(self, f):
     super(CMerkleBlock, self).stream_serialize(f)
     f.write(struct.pack('<L', self.nTX))
     VarIntSerializer.stream_serialize(len(self.vHashes), f)
     for hash in self.vHashes:
         f.write(hash)
     VarIntSerializer.stream_serialize(len(self.vFlags)/8, f)
     bin_string = ""
     for bit in self.vFlags:
         bin_string += str(bit)
         if len(bin_string) == 8:
             f.write(struct.pack('B', int(bin_string[::-1], 2)))
             bin_string = ""
Example #4
0
 def stream_serialize(self, f):
     super(CMerkleBlock, self).stream_serialize(f)
     f.write(struct.pack('<L', self.nTX))
     VarIntSerializer.stream_serialize(len(self.vHashes), f)
     for hash in self.vHashes:
         f.write(hash)
     VarIntSerializer.stream_serialize(len(self.vFlags) / 8, f)
     bin_string = ""
     for bit in self.vFlags:
         bin_string += str(bit)
         if len(bin_string) == 8:
             f.write(struct.pack('B', int(bin_string[::-1], 2)))
             bin_string = ""
Example #5
0
    def on_tx_selection(self, selected, deselected):
        if not self.block or not len(selected.indexes()):
            return
        index = selected.indexes()[0]
        row = index.row()

        def tx_len(i):
            return len(self.block.vtx[i].serialize()) * 2

        start = BlockHeader.header_length() * 2 + sum(tx_len(i) for i in range(row))
        # Account for VarInt.
        _buf = BytesIO()
        VarIntSerializer.stream_serialize(len(self.block.vtx), _buf)
        start += len(_buf.getvalue()) * 2

        length = len(self.block.vtx[row].serialize()) * 2
        self.select_block_text(start, length)
Example #6
0
    def on_tx_selection(self, selected, deselected):
        if not self.block or not len(selected.indexes()):
            return
        index = selected.indexes()[0]
        row = index.row()

        def tx_len(i):
            return len(self.block.vtx[i].serialize()) * 2

        start = BlockHeader.header_length() * 2 + sum(
            tx_len(i) for i in range(row))
        # Account for VarInt.
        _buf = BytesIO()
        VarIntSerializer.stream_serialize(len(self.block.vtx), _buf)
        start += len(_buf.getvalue()) * 2

        length = len(self.block.vtx[row].serialize()) * 2
        self.select_block_text(start, length)
Example #7
0
    def stream_serialize_value(self, value, f):
        if value is None:
            if self.type is FieldType.Type.str or self.type is FieldType.Type.bytes:
                f.write(bytes([0x00]))
            elif self.type is FieldType.Type.fvi:
                f.write(bytes([0xFF]))
            elif self.type in [FieldType.Type.sha256, FieldType.Type.sha256d]:
                f.write(bytes([0]*32))
            elif self.type in [FieldType.Type.ripmd160, FieldType.Type.hash160]:
                f.write(bytes([0]*20))
            elif self.type is FieldType.Type.pubkey:
                f.write(bytes([0x00]))
            elif self.type is FieldType.Type.ecdsa:
                f.write(bytes([0x00]))
            return

        lut = {
            FieldType.Type.u8: lambda x: f.write(bytes([x])),
            FieldType.Type.u16: lambda x: f.write(struct.pack(b'<H', x)),
            FieldType.Type.u32: lambda x: f.write(struct.pack(b'<I', x)),
            FieldType.Type.u64: lambda x: f.write(struct.pack(b'<Q', x)),
            FieldType.Type.i8: lambda x: f.write(bytes([x])),
            FieldType.Type.i16: lambda x: f.write(struct.pack(b'<h', x)),
            FieldType.Type.i32: lambda x: f.write(struct.pack(b'<i', x)),
            FieldType.Type.i64: lambda x: f.write(struct.pack(b'<q', x)),
            FieldType.Type.vi: lambda x: VarIntSerializer.stream_serialize(x, f),
            FieldType.Type.fvi: lambda x: FlagVarIntSerializer.stream_serialize((x, False), f),
            FieldType.Type.str: lambda x: VarStringSerializer.stream_serialize(x.encode('utf-8'), f),
            FieldType.Type.bytes: lambda x: BytesSerializer.stream_serialize(x, f),
            FieldType.Type.sha256: lambda x: x.stream_serealize(f),
            FieldType.Type.sha256d: lambda x: x.stream_serealize(f),
            FieldType.Type.ripmd160: lambda x: x.stream_serealize(f),
            FieldType.Type.hash160: lambda x: x.stream_serealize(f),
            FieldType.Type.pubkey: lambda x: x.stream_serealize(f),
            FieldType.Type.ecdsa: lambda _: None,
        }
        if self.type in lut.keys():
            if self.type in [FieldType.Type.sha256, FieldType.Type.sha256d,
                             FieldType.Type.ripmd160, FieldType.Type.hash160] and not issubclass(value, HashId):
                raise ValueError('in order to serialize hash value you need to provide an instance of HashId class')
            lut[self.type](value)
        else:
            raise NotImplementedError('ECDSA serialization is not implemented')
Example #8
0
 def serialize_witness_value_signing(self, f):
     VarIntSerializer.stream_serialize(len(self.txins), f)
     for txin in self.txins:
         txin.serialize_witness_value_signing(f)
Example #9
0
 def serialize_witness(self, f):
     VarIntSerializer.stream_serialize(len(self.txins), f)
     for txin in self.txins:
         txin.serialize_witness(f)
Example #10
0
 def stream_serialize_state(self, state, f):
     if self.type is SealType.Type.balance:
         VarIntSerializer.stream_serialize(state, f)
Example #11
0
    def stream_serialize(self, f, **kwargs):
        # Serialize proof header
        # - version with flag
        ver = self.ver if self.ver is not None else 0
        flag = self.format is ProofFormat.root or self.format is ProofFormat.upgrade
        FlagVarIntSerializer.stream_serialize((ver, flag), f)
        # - root proof fields
        if self.format is ProofFormat.root:
            self.schema.stream_serialize(f)
            VarIntSerializer.stream_serialize(self.network.value, f)
            self.root.stream_serialize(f, short_form=False)
        # - version upgrade proof fields
        elif self.format is ProofFormat.upgrade:
            if self.schema is not None:
                self.schema.stream_serialize(f)
            else:
                ZeroBytesSerializer.stream_serialize(32, f)
            ZeroBytesSerializer.stream_serialize(1, f)

        # Serialize proof body
        # - serializing proof type
        if self.type_no is None:
            raise ValueError(
                'proof consensus serialization requires `type_no` to be known')
        f.write(bytes([self.type_no]))

        # - writing `seal_sequence` structure
        current_type_no = None
        for seal in self.seals:
            if current_type_no is None:
                current_type_no = seal.type_no
            elif seal.type_no is not current_type_no:
                # -- writing EOL byte to signify the change of the type
                [
                    f.write(bytes([0x7F]))
                    for n in range(current_type_no, seal.type_no)
                ]
                current_type_no = seal.type_no
            seal.stream_serialize(f, state=False)
        f.write(bytes([0xFF]))

        # - writing raw data for the sealed state
        length = reduce(
            (lambda acc, seal: acc + len(seal.serialize({'state': True}))),
            [0] + self.seals)
        VarIntSerializer.stream_serialize(length, f)
        [seal.stream_serialize(f, state=True) for seal in self.seals]

        # - writing raw data for all metafields
        length = reduce((lambda acc, field: acc + len(field.serialize())),
                        [0] + self.fields)
        VarIntSerializer.stream_serialize(length, f)
        [field.stream_serialize(f) for field in self.fields]

        # Serialize original public key
        if self.pubkey is not None:
            self.pubkey.stream_serialize(f)
        else:
            ZeroBytesSerializer.stream_serialize(1, f)

        # Serialize prunable data
        if self.txid is not None:
            if self.parents is not None:
                f.write(bytes(0x03))
                self.txid.stream_serialize(f)
            else:
                f.write(bytes(0x01))
            VectorSerializer.stream_serialize(Hash256Id, self.parents, f)
        elif self.parents is not None:
            f.write(bytes(0x02))
            VectorSerializer.stream_serialize(Hash256Id, self.parents, f)
        else:
            f.write(bytes(0x00))
Example #12
0
 def stream_serialize(self, f):
     VarIntSerializer.stream_serialize(self.type_pos, f)
     f.write(bytes([self.bounds.min()]))
     f.write(bytes([self.bounds.max()]))