def check_script_size(script: bytes) -> None: try: r = BufferReader(script) n = read_uint32_be(r) r.read(n) n = read_uint32_be(r) ensure(r.remaining_count() == n) except (AssertionError, EOFError): raise wire.DataError("Invalid script")
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")
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)
def read_bip322_signature_proof(r: utils.BufferReader) -> Tuple[bytes, bytes]: script_sig = read_bytes_prefixed(r) witness = r.read() return script_sig, witness
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
def read_bytes_prefixed(r: BufferReader) -> bytes: n = read_bitcoin_varint(r) return r.read(n)