| 363 | |
| 364 | # This is used, eg, for blockchain heights in coinbase scripts (bip34) |
| 365 | class CScriptNum(): |
| 366 | def __init__(self, d=0): |
| 367 | self.value = d |
| 368 | |
| 369 | @staticmethod |
| 370 | def encode(obj): |
| 371 | r = bytearray(0) |
| 372 | if obj.value == 0: |
| 373 | return bytes(r) |
| 374 | neg = obj.value < 0 |
| 375 | absvalue = -obj.value if neg else obj.value |
| 376 | while (absvalue): |
| 377 | r.append(absvalue & 0xff) |
| 378 | absvalue >>= 8 |
| 379 | if r[-1] & 0x80: |
| 380 | r.append(0x80 if neg else 0) |
| 381 | elif neg: |
| 382 | r[-1] |= 0x80 |
| 383 | return bytes([len(r)]) + r |
| 384 | |
| 385 | |
| 386 | class CScript(bytes): |
no outgoing calls