Skip to content
This repository has been archived by the owner on May 28, 2019. It is now read-only.

BitcoinScript may not get script size #75

Open
zhaojiewen opened this issue Nov 1, 2018 · 1 comment
Open

BitcoinScript may not get script size #75

zhaojiewen opened this issue Nov 1, 2018 · 1 comment

Comments

@zhaojiewen
Copy link

In BitcoinScript Extention,function "func getScriptOp(index: inout Int) -> (opcode: UInt8, operand: Data?)? " below:
func getScriptOp(index: inout Int) -> (opcode: UInt8, operand: Data?)? {
// Read instruction
if index >= bytes.endIndex {
return nil
}

    let opcode = bytes[index]
    index += 1

    if opcode > OpCode.OP_PUSHDATA4 {
        return (opcode: opcode, operand: nil)
    }

    // Immediate operand
    var size = 0
    if opcode < OpCode.OP_PUSHDATA1 {
        size = Int(opcode)
    } else if opcode == OpCode.OP_PUSHDATA1 {
        if bytes.endIndex - index < 1 {
            return nil
        }
        size = index
        index += 1
    } else if opcode == OpCode.OP_PUSHDATA2 {
        if bytes.endIndex - index < 2 {
            return nil
        }
        size = Int(readLE16(at: index))
        index += 2
    } else if opcode == OpCode.OP_PUSHDATA4 {
        if bytes.endIndex - index < 4 {
            return nil
        }
        size = Int(readLE32(at: index))
        index += 4
    }
    if bytes.endIndex - index < size {
        return nil
    }
    let operand = data[index ..< index + size]
    index += size

    return (opcode: opcode, operand: operand)
}

when opcode == OpCode.OP_PUSHDATA1, that 'size = index' should be replaced by 'size = Int(bytes[index])'

@hewigovens
Copy link
Contributor

Hi, could you please submit a PR?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants