Exemplo n.º 1
0
def _read_length(r: utils.BufferReader, aux: int) -> int:
    if aux < _CBOR_UINT8_FOLLOWS:
        return aux
    elif aux == _CBOR_UINT8_FOLLOWS:
        return r.get()
    elif aux == _CBOR_UINT16_FOLLOWS:
        return readers.read_uint16_be(r)
    elif aux == _CBOR_UINT32_FOLLOWS:
        return readers.read_uint32_be(r)
    elif aux == _CBOR_UINT64_FOLLOWS:
        return readers.read_uint64_be(r)
    else:
        raise NotImplementedError("Length %d not suppported" % aux)
Exemplo n.º 2
0
def check_tx_params_size(params: bytes) -> None:
    try:
        r = BufferReader(params)
        tag = r.get()
        if tag == EP_TAG_NAMED:
            n = r.get()
            r.read(n)
        elif tag > 4:
            raise wire.DataError("Unknown entrypoint tag")
        n = read_uint32_be(r)
        ensure(r.remaining_count() == n)
    except (AssertionError, EOFError):
        raise wire.DataError("Invalid transaction parameters")
Exemplo n.º 3
0
def read_op_push(r: BufferReader) -> int:
    prefix = r.get()
    if prefix < 0x4C:
        n = prefix
    elif prefix == 0x4C:
        n = r.get()
    elif prefix == 0x4D:
        n = r.get()
        n += r.get() << 8
    elif prefix == 0x4E:
        n = r.get()
        n += r.get() << 8
        n += r.get() << 16
        n += r.get() << 24
    else:
        raise ValueError
    return n
Exemplo n.º 4
0
def read_int(r: BufferReader) -> memoryview:
    if r.get() != 0x02:
        raise ValueError

    n = read_length(r)
    if n == 0:
        raise ValueError

    if r.peek() & 0x80:
        raise ValueError  # negative integer

    if r.peek() == 0x00 and n > 1:
        r.get()
        n -= 1
        if r.peek() & 0x80 == 0x00:
            raise ValueError  # excessive zero-padding

        if r.peek() == 0x00:
            raise ValueError  # excessive zero-padding

    return r.read_memoryview(n)
Exemplo n.º 5
0
def decode_int(r: BufferReader) -> bytes:
    if r.get() != 0x02:
        raise ValueError

    n = decode_length(r)
    if n == 0:
        raise ValueError

    if r.peek() & 0x80:
        raise ValueError  # negative integer

    if r.peek() == 0x00 and n > 1:
        r.get()
        n -= 1
        if r.peek() & 0x80 == 0x00:
            raise ValueError  # excessive zero-padding

        if r.peek() == 0x00:
            raise ValueError  # excessive zero-padding

    return r.read(n)
Exemplo n.º 6
0
def read_bitcoin_varint(r: BufferReader) -> int:
    prefix = r.get()
    if prefix < 253:
        n = prefix
    elif prefix == 253:
        n = r.get()
        n += r.get() << 8
    elif prefix == 254:
        n = r.get()
        n += r.get() << 8
        n += r.get() << 16
        n += r.get() << 24
    else:
        raise ValueError
    return n
Exemplo n.º 7
0
def load_message(msg_type, buffer: bytearray) -> protobuf.MessageType:
    return protobuf.load_message(BufferReader(buffer), msg_type)
Exemplo n.º 8
0
def read_uint16_be(r: BufferReader) -> int:
    n = r.get()
    return (n << 8) + r.get()
Exemplo n.º 9
0
def load_uvarint(data: bytes) -> int:
    reader = BufferReader(data)
    return protobuf.load_uvarint(reader)
Exemplo n.º 10
0
def read_memoryview_prefixed(r: BufferReader) -> memoryview:
    n = read_bitcoin_varint(r)
    return r.read_memoryview(n)
Exemplo n.º 11
0
def read_bip322_signature_proof(r: utils.BufferReader) -> Tuple[bytes, bytes]:
    script_sig = read_bytes_prefixed(r)
    witness = r.read()
    return script_sig, witness
Exemplo n.º 12
0
def _cbor_decode(r: utils.BufferReader) -> Value:
    fb = r.get()
    fb_type = fb & _CBOR_TYPE_MASK
    fb_aux = fb & _CBOR_INFO_BITS
    if fb_type == _CBOR_UNSIGNED_INT:
        return _read_length(r, fb_aux)
    elif fb_type == _CBOR_NEGATIVE_INT:
        val = _read_length(r, fb_aux)
        return -1 - val
    elif fb_type == _CBOR_BYTE_STRING:
        ln = _read_length(r, fb_aux)
        return r.read(ln)
    elif fb_type == _CBOR_TEXT_STRING:
        ln = _read_length(r, fb_aux)
        return r.read(ln).decode()
    elif fb_type == _CBOR_ARRAY:
        if fb_aux == _CBOR_VAR_FOLLOWS:
            res: Value = []
            while True:
                item = _cbor_decode(r)
                if item == _CBOR_PRIMITIVE + _CBOR_BREAK:
                    break
                res.append(item)
            return res
        else:
            ln = _read_length(r, fb_aux)
            res = []
            for _ in range(ln):
                item = _cbor_decode(r)
                res.append(item)
            return res
    elif fb_type == _CBOR_MAP:
        res = {}
        if fb_aux == _CBOR_VAR_FOLLOWS:
            while True:
                key = _cbor_decode(r)
                if key in res:
                    raise ValueError
                if key == _CBOR_PRIMITIVE + _CBOR_BREAK:
                    break
                value = _cbor_decode(r)
                res[key] = value
        else:
            ln = _read_length(r, fb_aux)
            for _ in range(ln):
                key = _cbor_decode(r)
                if key in res:
                    raise ValueError
                value = _cbor_decode(r)
                res[key] = value
        return res
    elif fb_type == _CBOR_TAG:
        val = _read_length(r, fb_aux)
        item = _cbor_decode(r)
        if val == _CBOR_RAW_TAG:  # only tag 24 (0x18) is supported
            return item
        else:
            return Tagged(val, item)
    elif fb_type == _CBOR_PRIMITIVE:
        if fb_aux == _CBOR_FALSE:
            return False
        elif fb_aux == _CBOR_TRUE:
            return True
        elif fb_aux == _CBOR_NULL:
            return None
        elif fb_aux == _CBOR_BREAK:
            return fb
        else:
            raise NotImplementedError
    else:
        if __debug__:
            log.debug(__name__, "not implemented (decode): %s", fb)
        raise NotImplementedError
Exemplo n.º 13
0
def read_compact_size(r: BufferReader) -> int:
    prefix = r.get()
    if prefix < 253:
        n = prefix
    elif prefix == 253:
        n = r.get()
        n += r.get() << 8
    elif prefix == 254:
        n = r.get()
        n += r.get() << 8
        n += r.get() << 16
        n += r.get() << 24
    elif prefix == 255:
        n = r.get()
        n += r.get() << 8
        n += r.get() << 16
        n += r.get() << 24
        n += r.get() << 32
        n += r.get() << 40
        n += r.get() << 48
        n += r.get() << 56
    else:
        raise ValueError
    return n
Exemplo n.º 14
0
def read_uint64_be(r: BufferReader) -> int:
    n = r.get()
    for _ in range(7):
        n = (n << 8) + r.get()
    return n
Exemplo n.º 15
0
def read_uint32_be(r: BufferReader) -> int:
    n = r.get() << 24
    n += r.get() << 16
    n += r.get() << 8
    n += r.get()
    return n
Exemplo n.º 16
0
def read_memoryview_prefixed(r: BufferReader) -> memoryview:
    n = read_compact_size(r)
    return r.read_memoryview(n)
Exemplo n.º 17
0
def read_bytes_prefixed(r: BufferReader) -> bytes:
    n = read_bitcoin_varint(r)
    return r.read(n)
Exemplo n.º 18
0
def read_bip322_signature_proof(
        r: utils.BufferReader) -> tuple[memoryview, memoryview]:
    script_sig = read_memoryview_prefixed(r)
    witness = r.read_memoryview()
    return script_sig, witness