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
| 509 | return result |
| 510 | |
| 511 | class CScript(bytes): |
| 512 | """Serialized script |
| 513 | |
| 514 | A bytes subclass, so you can use this directly whenever bytes are accepted. |
| 515 | Note that this means that indexing does *not* work - you'll get an index by |
| 516 | byte rather than opcode. This format was chosen for efficiency so that the |
| 517 | general case would not require creating a lot of little CScriptOP objects. |
| 518 | |
| 519 | iter(script) however does iterate by opcode. |
| 520 | """ |
| 521 | __slots__ = () |
| 522 | |
| 523 | @classmethod |
| 524 | def __coerce_instance(cls, other): |
| 525 | # Coerce other into bytes |
| 526 | if isinstance(other, CScriptOp): |
| 527 | other = bytes([other]) |
| 528 | elif isinstance(other, CScriptNum): |
| 529 | if (other.value == 0): |
| 530 | other = bytes([CScriptOp(OP_0)]) |
| 531 | else: |
| 532 | other = CScriptNum.encode(other) |
| 533 | elif isinstance(other, int): |
| 534 | if 0 <= other <= 16: |
| 535 | other = bytes([CScriptOp.encode_op_n(other)]) |
| 536 | elif other == -1: |
| 537 | other = bytes([OP_1NEGATE]) |
| 538 | else: |
| 539 | other = CScriptOp.encode_op_pushdata(bn2vch(other)) |
| 540 | elif isinstance(other, (bytes, bytearray)): |
| 541 | other = CScriptOp.encode_op_pushdata(other) |
| 542 | return other |
| 543 | |
| 544 | def __add__(self, other): |
| 545 | # add makes no sense for a CScript() |
| 546 | raise NotImplementedError |
| 547 | |
| 548 | def join(self, iterable): |
| 549 | # join makes no sense for a CScript() |
| 550 | raise NotImplementedError |
| 551 | |
| 552 | def __new__(cls, value=b''): |
| 553 | if isinstance(value, bytes) or isinstance(value, bytearray): |
| 554 | return super().__new__(cls, value) |
| 555 | else: |
| 556 | def coerce_iterable(iterable): |
| 557 | for instance in iterable: |
| 558 | yield cls.__coerce_instance(instance) |
| 559 | # Annoyingly on both python2 and python3 bytes.join() always |
| 560 | # returns a bytes instance even when subclassed. |
| 561 | return super().__new__(cls, b''.join(coerce_iterable(value))) |
| 562 | |
| 563 | def raw_iter(self): |
| 564 | """Raw iteration |
| 565 | |
| 566 | Yields tuples of (opcode, data, sop_idx) so that the different possible |
| 567 | PUSHDATA encodings can be accurately distinguished, as well as |
| 568 | determining the exact opcode byte indexes. (sop_idx) |
no outgoing calls