示例#1
0
    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
示例#2
0
    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
示例#3
0
    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
示例#4
0
 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)
示例#5
0
 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")
示例#6
0
 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))
示例#7
0
 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
     )
示例#8
0
    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
示例#9
0
    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]
示例#10
0
    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]
示例#11
0
 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")
示例#12
0
 def parse(self, payload, opts):
     l = decodeVarLength(payload)
     for _ in range(0, l):
         a = Address()
         a.parse(payload, opts)
         self.addresses.append(a)
示例#13
0
 def parse(self, payload, version):
     l = decodeVarLength(payload)
     for _ in range(0, l):
         a = Address()
         a.parse(payload, version)
         self.addresses.append(a)
示例#14
0
 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))