Пример #1
0
def test_unpackers():
    b = bytes(range(256))
    assert util.unpack_le_int32_from(b, 0) == (50462976, )
    assert util.unpack_le_int32_from(b, 42) == (757869354, )
    assert util.unpack_le_int64_from(b, 0) == (506097522914230528, )
    assert util.unpack_le_int64_from(b, 42) == (3544384782113450794, )

    assert util.unpack_le_uint16_from(b, 0) == (256, )
    assert util.unpack_le_uint16_from(b, 42) == (11050, )
    assert util.unpack_le_uint32_from(b, 0) == (50462976, )
    assert util.unpack_le_uint32_from(b, 42) == (757869354, )
    assert util.unpack_le_uint64_from(b, 0) == (506097522914230528, )
    assert util.unpack_le_uint64_from(b, 42) == (3544384782113450794, )
Пример #2
0
    def get_ops(cls, script):
        ops = []

        # The unpacks or script[n] below throw on truncated scripts
        try:
            n = 0
            while n < len(script):
                op = script[n]
                n += 1

                if op <= OpCodes.OP_PUSHDATA4:
                    # Raw bytes follow
                    if op < OpCodes.OP_PUSHDATA1:
                        dlen = op
                    elif op == OpCodes.OP_PUSHDATA1:
                        dlen = script[n]
                        n += 1
                    elif op == OpCodes.OP_PUSHDATA2:
                        dlen, = unpack_le_uint16_from(script[n:n + 2])
                        n += 2
                    else:
                        dlen, = unpack_le_uint32_from(script[n:n + 4])
                        n += 4
                    if n + dlen > len(script):
                        raise IndexError
                    op = (op, script[n:n + dlen])
                    n += dlen

                ops.append(op)
        except Exception:
            # Truncated script; e.g. tx_hash
            # ebc9fa1196a59e192352d76c0f6e73167046b9d37b8302b6bb6968dfd279b767
            raise ScriptError('truncated script') from None

        return ops
Пример #3
0
    def header_hash(cls, header):
        '''Given a header return the hash.'''
        if cls.HEADER_HASH is None:
            import scrypt
            cls.HEADER_HASH = lambda x: scrypt.hash(x, x, 1024, 1, 1, 32)

        version, = util.unpack_le_uint32_from(header)
        if version > 6:
            return super().header_hash(header)
        else:
            return cls.HEADER_HASH(header)
    def get_ops(cls, script):
        '''
        Returns a tuple list of (op_code, index of next op in script, pushed bytes if any)

        If at any point the script fails do decode, a tuple of (-1, len(script), remaining script) is appended
        '''
        ops = []

        # The unpacks or script[n]
        n = 0
        try:
            while n < len(script):
                op = script[n]
                op_v = (script[n], n + 1)
                n += 1
                if op <= OpCodes.OP_PUSHDATA4:
                    # Raw bytes follow
                    if op < OpCodes.OP_PUSHDATA1:
                        dlen = op
                        n1 = 0
                    elif op == OpCodes.OP_PUSHDATA1:
                        dlen = script[n]
                        n1 = 1
                    elif op == OpCodes.OP_PUSHDATA2:
                        dlen, = unpack_le_uint16_from(script[n:n + 2])
                        n1 = 2
                    else:
                        dlen, = unpack_le_uint32_from(script[n:n + 4])
                        n1 = 4
                    if n + n1 + dlen > len(script):
                        raise IndexError
                    n += n1
                    op_v = (op, n + dlen, script[n:n + dlen])
                    n += dlen

                ops.append(op_v)
        except (IndexError, struct.error):
            # n - 1 because we read a byte first
            ops.append((-1, len(script), script[n - 1:]))

        return ops
Пример #5
0
 def _read_le_uint32(self):
     result, = unpack_le_uint32_from(self.binary, self.cursor)
     self.cursor += 4
     return result
Пример #6
0
def read_le_uint32(buf, cursor):
    result, = unpack_le_uint32_from(buf, cursor)
    return result, cursor + 4