[#822] moprh/event: Add parsers from Op struct
Signed-off-by: Pavel Karpy <carpawell@nspcc.ru>
This commit is contained in:
parent
61b4baf736
commit
4f3de1a9af
2 changed files with 112 additions and 1 deletions
|
@ -1,6 +1,11 @@
|
|||
package event
|
||||
|
||||
import "github.com/nspcc-dev/neo-go/pkg/vm/opcode"
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/nspcc-dev/neo-go/pkg/encoding/bigint"
|
||||
"github.com/nspcc-dev/neo-go/pkg/vm/opcode"
|
||||
)
|
||||
|
||||
// Op is wrapper over Neo VM's opcode
|
||||
// and its parameter.
|
||||
|
@ -19,3 +24,27 @@ func (o Op) Code() opcode.Opcode {
|
|||
func (o Op) Param() []byte {
|
||||
return o.param
|
||||
}
|
||||
|
||||
// BytesFromOpcode tries to retrieve bytes from Op.
|
||||
func BytesFromOpcode(op Op) ([]byte, error) {
|
||||
switch code := op.Code(); code {
|
||||
case opcode.PUSHDATA1, opcode.PUSHDATA2, opcode.PUSHDATA4:
|
||||
return op.Param(), nil
|
||||
default:
|
||||
return nil, fmt.Errorf("unexpected ByteArray opcode %s", code)
|
||||
}
|
||||
}
|
||||
|
||||
// IntFromOpcode tries to retrieve bytes from Op.
|
||||
func IntFromOpcode(op Op) (int64, error) {
|
||||
switch code := op.Code(); {
|
||||
case code == opcode.PUSHM1:
|
||||
return -1, nil
|
||||
case code >= opcode.PUSH0 && code <= opcode.PUSH16:
|
||||
return int64(code - opcode.PUSH0), nil
|
||||
case code <= opcode.PUSHINT256:
|
||||
return bigint.FromBytes(op.Param()).Int64(), nil
|
||||
default:
|
||||
return 0, fmt.Errorf("unexpected INT opcode %s", code)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue