Skip to content

Commit

Permalink
support lower-case opcodes and omitting the "OP_"-prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Bernauer committed Nov 12, 2011
1 parent dc427be commit 0725f17
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/Language/Bitcoin/Parser.hs
Expand Up @@ -6,7 +6,7 @@ module Language.Bitcoin.Parser

-- import {{{1
import Control.Monad (liftM, when)
import Data.Char (isSpace)
import Data.Char (isSpace, toUpper)
import Data.Maybe (catMaybes)
import Language.Bitcoin.Types
import Language.Bitcoin.Numbers (BCI)
Expand Down Expand Up @@ -47,14 +47,18 @@ operation = do
command <- many (alphaNum <|> char '_' <?> "opcode")
if command == ""
then return Nothing
else liftM Just $ case command of
else liftM Just $ case map toUpper command of
"DATA" -> dataCmd
"KEY" -> keyOrSig KEY
"SIG" -> keyOrSig SIG
"OP_PUSHDATA" -> push
"OP_PUSHDATA1" -> push1
"OP_PUSHDATA2" -> push2
"OP_PUSHDATA4" -> push4
"PUSHDATA" -> push
"PUSHDATA1" -> push1
"PUSHDATA2" -> push2
"PUSHDATA4" -> push4
x -> opcode x
spaces >> return op

Expand All @@ -70,7 +74,7 @@ keyOrSig createCommand = do
return $ createCommand number

opcode :: String -> Parser Command
opcode x = liftM CmdOpcode $ liftReadS reads x
opcode x = liftM CmdOpcode $ liftReadS reads $ if take 3 x == "OP_" then x else "OP_" ++ x

push :: Parser Command
push = pushN checkLength checkValue Direct
Expand Down
1 change: 1 addition & 0 deletions src/Language/Bitcoin/Preprocessor.hs
Expand Up @@ -35,6 +35,7 @@ getOrCreate keys keyId =
Just keypair ->
(keys, keypair)


push :: BCI -> Opcode
push data_ = OP_PUSHDATA pushType data_
where
Expand Down
9 changes: 9 additions & 0 deletions test/Language/Bitcoin/Test/Parser.hs
Expand Up @@ -14,6 +14,9 @@ tests = TestLabel "Parser" $ TestList $ good ++ bad

goodCases = [
("OP_FALSE", [CmdOpcode OP_FALSE])
, ("FALSE", [CmdOpcode OP_FALSE])
, ("op_false", [CmdOpcode OP_FALSE])
, ("false", [CmdOpcode OP_FALSE])
, ("OP_FALSE ", [CmdOpcode OP_FALSE])
, (" OP_FALSE", [CmdOpcode OP_FALSE])
, (" OP_FALSE ; ", [CmdOpcode OP_FALSE])
Expand All @@ -24,13 +27,19 @@ goodCases = [
, ("# comment\nOP_FALSE", [CmdOpcode OP_FALSE])
, ("OP_FALSE;OP_TRUE", [CmdOpcode OP_FALSE, CmdOpcode OP_TRUE])
, ("OP_PUSHDATA 01 23", [CmdOpcode $ OP_PUSHDATA Direct (23)])
, ("PUSHDATA 01 23", [CmdOpcode $ OP_PUSHDATA Direct (23)])
, ("op_pushdata 01 23", [CmdOpcode $ OP_PUSHDATA Direct (23)])
, ("pushdata 01 23", [CmdOpcode $ OP_PUSHDATA Direct (23)])
, ("OP_PUSHDATA 01 0x23", [CmdOpcode $ OP_PUSHDATA Direct (0x23)])
, ("OP_PUSHDATA1 06 0x040815162342", [CmdOpcode $ OP_PUSHDATA OneByte (0x40815162342)])
, ("OP_PUSHDATA2 0006 0x040815162342", [CmdOpcode $ OP_PUSHDATA TwoBytes (0x40815162342)])
, ("OP_PUSHDATA4 00000006 0x040815162342", [CmdOpcode $ OP_PUSHDATA FourBytes (0x40815162342)])
, ("DATA 0x040815162342", [DATA 0x40815162342])
, ("data 0x040815162342", [DATA 0x40815162342])
, ("KEY 1", [KEY 1])
, ("key 1", [KEY 1])
, ("SIG 1", [SIG 1])
, ("sig 1", [SIG 1])
]

badCases = [
Expand Down

0 comments on commit 0725f17

Please sign in to comment.