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
| 651 | |
| 652 | |
| 653 | class CScript(bytes): |
| 654 | """Serialized script |
| 655 | |
| 656 | A bytes subclass, so you can use this directly whenever bytes are accepted. |
| 657 | Note that this means that indexing does *not* work - you'll get an index by |
| 658 | byte rather than opcode. This format was chosen for efficiency so that the |
| 659 | general case would not require creating a lot of little CScriptOP objects. |
| 660 | |
| 661 | iter(script) however does iterate by opcode. |
| 662 | """ |
| 663 | @classmethod |
| 664 | def __coerce_instance(cls, other): |
| 665 | # Coerce other into bytes |
| 666 | if isinstance(other, CScriptOp): |
| 667 | other = bchr(other) |
| 668 | elif isinstance(other, CScriptNum): |
| 669 | if (other.value == 0): |
| 670 | other = bchr(CScriptOp(OP_0)) |
| 671 | else: |
| 672 | other = CScriptNum.encode(other) |
| 673 | elif isinstance(other, int): |
| 674 | if 0 <= other <= 16: |
| 675 | other = bytes(bchr(CScriptOp.encode_op_n(other))) |
| 676 | elif other == -1: |
| 677 | other = bytes(bchr(OP_1NEGATE)) |
| 678 | else: |
| 679 | other = CScriptOp.encode_op_pushdata(bn2vch(other)) |
| 680 | elif isinstance(other, (bytes, bytearray)): |
| 681 | other = CScriptOp.encode_op_pushdata(other) |
| 682 | return other |
| 683 | |
| 684 | def __add__(self, other): |
| 685 | # Do the coercion outside of the try block so that errors in it are |
| 686 | # noticed. |
| 687 | other = self.__coerce_instance(other) |
| 688 | |
| 689 | try: |
| 690 | # bytes.__add__ always returns bytes instances unfortunately |
| 691 | return CScript(super(CScript, self).__add__(other)) |
| 692 | except TypeError: |
| 693 | raise TypeError('Can not add a %r instance to a CScript' % other.__class__) |
| 694 | |
| 695 | def join(self, iterable): |
| 696 | # join makes no sense for a CScript() |
| 697 | raise NotImplementedError |
| 698 | |
| 699 | def __new__(cls, value=b''): |
| 700 | if isinstance(value, bytes) or isinstance(value, bytearray): |
| 701 | return super(CScript, cls).__new__(cls, value) |
| 702 | else: |
| 703 | def coerce_iterable(iterable): |
| 704 | for instance in iterable: |
| 705 | yield cls.__coerce_instance(instance) |
| 706 | # Annoyingly on both python2 and python3 bytes.join() always |
| 707 | # returns a bytes instance even when subclassed. |
| 708 | return super(CScript, cls).__new__(cls, b''.join(coerce_iterable(value))) |
| 709 | |
| 710 | def raw_iter(self): |
no outgoing calls
no test coverage detected