Serialized script A bytes subclass, so you can use this directly whenever bytes are accepted. Note that this means that indexing does *not* work - you'll get an index by byte rather than opcode. This format was chosen for efficiency so that the general case would not require creatin
| 426 | |
| 427 | |
| 428 | class CScript(bytes): |
| 429 | """Serialized script |
| 430 | |
| 431 | A bytes subclass, so you can use this directly whenever bytes are accepted. |
| 432 | Note that this means that indexing does *not* work - you'll get an index by |
| 433 | byte rather than opcode. This format was chosen for efficiency so that the |
| 434 | general case would not require creating a lot of little CScriptOP objects. |
| 435 | |
| 436 | iter(script) however does iterate by opcode. |
| 437 | """ |
| 438 | __slots__ = () |
| 439 | |
| 440 | @classmethod |
| 441 | def __coerce_instance(cls, other): |
| 442 | # Coerce other into bytes |
| 443 | if isinstance(other, CScriptOp): |
| 444 | other = bytes([other]) |
| 445 | elif isinstance(other, CScriptNum): |
| 446 | if (other.value == 0): |
| 447 | other = bytes([CScriptOp(OP_0)]) |
| 448 | else: |
| 449 | other = CScriptNum.encode(other) |
| 450 | elif isinstance(other, int): |
| 451 | if 0 <= other <= 16: |
| 452 | other = bytes([CScriptOp.encode_op_n(other)]) |
| 453 | elif other == -1: |
| 454 | other = bytes([OP_1NEGATE]) |
| 455 | else: |
| 456 | other = CScriptOp.encode_op_pushdata(bn2vch(other)) |
| 457 | elif isinstance(other, (bytes, bytearray)): |
| 458 | other = CScriptOp.encode_op_pushdata(other) |
| 459 | return other |
| 460 | |
| 461 | def __add__(self, other): |
| 462 | # add makes no sense for a CScript() |
| 463 | raise NotImplementedError |
| 464 | |
| 465 | def join(self, iterable): |
| 466 | # join makes no sense for a CScript() |
| 467 | raise NotImplementedError |
| 468 | |
| 469 | def __new__(cls, value=b''): |
| 470 | if isinstance(value, bytes) or isinstance(value, bytearray): |
| 471 | return super().__new__(cls, value) |
| 472 | else: |
| 473 | def coerce_iterable(iterable): |
| 474 | for instance in iterable: |
| 475 | yield cls.__coerce_instance(instance) |
| 476 | # Annoyingly on both python2 and python3 bytes.join() always |
| 477 | # returns a bytes instance even when subclassed. |
| 478 | return super().__new__(cls, b''.join(coerce_iterable(value))) |
| 479 | |
| 480 | def raw_iter(self): |
| 481 | """Raw iteration |
| 482 | |
| 483 | Yields tuples of (opcode, data, sop_idx) so that the different possible |
| 484 | PUSHDATA encodings can be accurately distinguished, as well as |
| 485 | determining the exact opcode byte indexes. (sop_idx) |
no outgoing calls