| 425 | |
| 426 | |
| 427 | class CTransaction(object): |
| 428 | def __init__(self, tx=None): |
| 429 | if tx is None: |
| 430 | self.nVersion = 1 |
| 431 | self.vin = [] |
| 432 | self.vout = [] |
| 433 | self.wit = CTxWitness() |
| 434 | self.nLockTime = 0 |
| 435 | self.sha256 = None |
| 436 | self.hash = None |
| 437 | else: |
| 438 | self.nVersion = tx.nVersion |
| 439 | self.vin = copy.deepcopy(tx.vin) |
| 440 | self.vout = copy.deepcopy(tx.vout) |
| 441 | self.nLockTime = tx.nLockTime |
| 442 | self.sha256 = tx.sha256 |
| 443 | self.hash = tx.hash |
| 444 | self.wit = copy.deepcopy(tx.wit) |
| 445 | |
| 446 | def deserialize(self, f): |
| 447 | self.nVersion = struct.unpack("<i", f.read(4))[0] |
| 448 | self.vin = deser_vector(f, CTxIn) |
| 449 | flags = 0 |
| 450 | if len(self.vin) == 0: |
| 451 | flags = struct.unpack("<B", f.read(1))[0] |
| 452 | # Not sure why flags can't be zero, but this |
| 453 | # matches the implementation in luxd |
| 454 | if (flags != 0): |
| 455 | self.vin = deser_vector(f, CTxIn) |
| 456 | self.vout = deser_vector(f, CTxOut) |
| 457 | else: |
| 458 | self.vout = deser_vector(f, CTxOut) |
| 459 | if flags != 0: |
| 460 | self.wit.vtxinwit = [CTxInWitness() for i in range(len(self.vin))] |
| 461 | self.wit.deserialize(f) |
| 462 | self.nLockTime = struct.unpack("<I", f.read(4))[0] |
| 463 | self.sha256 = None |
| 464 | self.hash = None |
| 465 | |
| 466 | def serialize_without_witness(self): |
| 467 | r = b"" |
| 468 | r += struct.pack("<i", self.nVersion) |
| 469 | r += ser_vector(self.vin) |
| 470 | r += ser_vector(self.vout) |
| 471 | r += struct.pack("<I", self.nLockTime) |
| 472 | return r |
| 473 | |
| 474 | # Only serialize with witness when explicitly called for |
| 475 | def serialize_with_witness(self): |
| 476 | flags = 0 |
| 477 | if not self.wit.is_null(): |
| 478 | flags |= 1 |
| 479 | r = b"" |
| 480 | r += struct.pack("<i", self.nVersion) |
| 481 | if flags: |
| 482 | dummy = [] |
| 483 | r += ser_vector(dummy) |
| 484 | r += struct.pack("<B", flags) |
no outgoing calls