def parseSegwit(self, payload, opts): if decodeVarLength(payload) == 0: return False self.is_segwit = True txInputCount = decodeVarLength(payload) for _i in range(0, txInputCount): prev_out = (payload.read(32)[::-1], struct.unpack("<I", payload.read(4))[0]) script_length = decodeVarLength(payload) script = payload.read(script_length) sequence, = struct.unpack("<I", payload.read(4)) self.inputs.append((prev_out, script, sequence)) txOutputCount = decodeVarLength(payload) for _i in range(0, txOutputCount): value, = struct.unpack("<Q", payload.read(8)) script = decodeVarString(payload) self.outputs.append((value, script)) for i in range(0, txInputCount): nelements = decodeVarLength(payload) self.witnesses.append( [decodeVarString(payload) for _ in range(nelements)]) self.lock_time, = struct.unpack("<I", payload.read(4)) return True
def parse(self, payload, opts): Packet.parse(self, payload, opts) self.version, = struct.unpack("<I", payload.read(4)) txInputCount = decodeVarLength(payload) if txInputCount == 0: return self.parseSegwit(payload, opts) for _i in range(0, txInputCount): prev_out = ( payload.read(32)[::-1], struct.unpack("<I", payload.read(4))[0] ) script_length = decodeVarLength(payload) script = payload.read(script_length) sequence, = struct.unpack("<I", payload.read(4)) self.inputs.append((prev_out, script, sequence)) txOutputCount = decodeVarLength(payload) for _i in range(0, txOutputCount): value, = struct.unpack("<Q", payload.read(8)) script = decodeVarString(payload) self.outputs.append((value, script)) self.lock_time, = struct.unpack("<I", payload.read(4)) return True
def parseSegwit(self, payload, opts): if decodeVarLength(payload) == 0: return False self.is_segwit = True txInputCount = decodeVarLength(payload) for _i in range(0, txInputCount): prev_out = ( payload.read(32)[::-1], struct.unpack("<I", payload.read(4))[0] ) script_length = decodeVarLength(payload) script = payload.read(script_length) sequence, = struct.unpack("<I", payload.read(4)) self.inputs.append((prev_out, script, sequence)) txOutputCount = decodeVarLength(payload) for _i in range(0, txOutputCount): value, = struct.unpack("<Q", payload.read(8)) script = decodeVarString(payload) self.outputs.append((value, script)) for i in range(0, txInputCount): nelements = decodeVarLength(payload) self.witnesses.append([decodeVarString(payload) for _ in range(nelements)]) self.lock_time, = struct.unpack("<I", payload.read(4)) return True
def testVarLengthDecode(self): self.assertEqual(decodeVarLength(StringIO("01DEAD".decode("hex"))), 1) self.assertEqual( decodeVarLength(StringIO("fd000401DEAD".decode("hex"))), 1024) self.assertEqual( decodeVarLength(StringIO("feefbeadde01DEAD".decode("hex"))), 3735928559) self.assertEqual( decodeVarLength(StringIO( 'ffdeadbeefdeadbeef01DEAD'.decode("hex"))), 17275436393656397278)
def testVarLengthDecode(self): self.assertEqual(decodeVarLength(StringIO("01DEAD".decode("hex"))), 1, "Simple 1 char number") self.assertEqual( decodeVarLength(StringIO("fd000401DEAD".decode("hex"))), 1024, "2 + 1 byte number") self.assertEqual( decodeVarLength(StringIO("feefbeadde01DEAD".decode("hex"))), 3735928559, "4 + 1 byte number") self.assertEqual( decodeVarLength(StringIO( "ffdeadbeefdeadbeef01DEAD".decode("hex"))), 17275436393656397278, "8 + 1 byte number")
def parse(self, payload, opts): length = decodeVarLength(payload) while len(self.hashes) < length: t, = struct.unpack("<I", payload.read(4)) h = payload.read(32)[::-1] if t == 5 and self.type == "getdata": m, = struct.unpack("<I", payload.read(4)) self.hashes.append((t, h, m)) else: self.hashes.append((t, h))
def testVarLengthDecode(self): self.assertEqual( decodeVarLength(StringIO("01DEAD".decode("hex"))), 1 ) self.assertEqual( decodeVarLength(StringIO("fd000401DEAD".decode("hex"))), 1024 ) self.assertEqual( decodeVarLength(StringIO("feefbeadde01DEAD".decode("hex"))), 3735928559 ) self.assertEqual( decodeVarLength( StringIO('ffdeadbeefdeadbeef01DEAD'.decode("hex")) ), 17275436393656397278 )
def parse(self, payload, opts): Packet.parse(self, payload, opts) self.version, = struct.unpack("<I", payload.read(4)) txInputCount = decodeVarLength(payload) if txInputCount == 0: return self.parseSegwit(payload, opts) for _i in range(0, txInputCount): prev_out = (payload.read(32)[::-1], struct.unpack("<I", payload.read(4))[0]) script_length = decodeVarLength(payload) script = payload.read(script_length) sequence, = struct.unpack("<I", payload.read(4)) self.inputs.append((prev_out, script, sequence)) txOutputCount = decodeVarLength(payload) for _i in range(0, txOutputCount): value, = struct.unpack("<Q", payload.read(8)) script = decodeVarString(payload) self.outputs.append((value, script)) self.lock_time, = struct.unpack("<I", payload.read(4)) return True
def parse(self, payload, opts): Packet.parse(self, payload, opts) self.version, self.prev_block, self.merkle_root = struct.unpack( '<I32s32s', payload.read(68)) self.prev_block = self.prev_block[::-1] self.merkle_root = self.merkle_root[::-1] self.timestamp, self.bits, self.nonce = struct.unpack( '<III', payload.read(12)) transactionCount = decodeVarLength(payload) while len(self.transactions) < transactionCount: t = TxPacket() t.parse(payload, opts) self.transactions.append(t) self._hash = doubleSha256(payload.getvalue()[:80])[::-1]
def parse(self, payload, version): Packet.parse(self, payload, version) self.version, self.prev_block, self.merkle_root = struct.unpack( '<I32s32s', payload.read(68)) self.prev_block = self.prev_block[::-1] self.merkle_root = self.merkle_root[::-1] self.timestamp, self.bits, self.nonce = struct.unpack( '<III', payload.read(12)) transactionCount = decodeVarLength(payload) while len(self.transactions) < transactionCount: t = TxPacket() t.parse(payload, version) self.transactions.append(t) self._hash = doubleSha256(payload.getvalue()[:80])[::-1]
def testVarLengthDecode(self): self.assertEqual(decodeVarLength(StringIO("01DEAD".decode("hex"))), 1, "Simple 1 char number") self.assertEqual(decodeVarLength(StringIO("fd000401DEAD".decode("hex"))),1024, "2 + 1 byte number") self.assertEqual(decodeVarLength(StringIO("feefbeadde01DEAD".decode("hex"))), 3735928559, "4 + 1 byte number") self.assertEqual(decodeVarLength(StringIO("ffdeadbeefdeadbeef01DEAD".decode("hex"))),17275436393656397278, "8 + 1 byte number")
def parse(self, payload, opts): l = decodeVarLength(payload) for _ in range(0, l): a = Address() a.parse(payload, opts) self.addresses.append(a)
def parse(self, payload, version): l = decodeVarLength(payload) for _ in range(0, l): a = Address() a.parse(payload, version) self.addresses.append(a)
def parse(self, payload, unused_version): length = decodeVarLength(payload) while len(self.hashes) < length: t, = struct.unpack("<I", payload.read(4)) h = payload.read(32)[::-1] self.hashes.append((t, h))