2020-03-18 15:21:12 +00:00
|
|
|
package manifest
|
|
|
|
|
|
|
|
import (
|
2024-08-26 16:45:53 +00:00
|
|
|
"cmp"
|
2021-02-03 18:09:50 +00:00
|
|
|
"crypto/elliptic"
|
2020-03-18 15:21:12 +00:00
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2024-08-23 21:07:41 +00:00
|
|
|
"slices"
|
2020-03-18 15:21:12 +00:00
|
|
|
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
2021-02-03 18:09:50 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
2020-03-18 15:21:12 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// PermissionType represents permission type.
|
|
|
|
type PermissionType uint8
|
|
|
|
|
|
|
|
const (
|
|
|
|
// PermissionWildcard allows everything.
|
|
|
|
PermissionWildcard PermissionType = 0
|
|
|
|
// PermissionHash restricts called contracts based on hash.
|
|
|
|
PermissionHash PermissionType = 1
|
|
|
|
// PermissionGroup restricts called contracts based on public key.
|
|
|
|
PermissionGroup PermissionType = 2
|
|
|
|
)
|
|
|
|
|
|
|
|
// PermissionDesc is a permission descriptor.
|
|
|
|
type PermissionDesc struct {
|
|
|
|
Type PermissionType
|
2023-04-03 10:34:24 +00:00
|
|
|
Value any
|
2020-03-18 15:21:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Permission describes which contracts may be invoked and which methods are called.
|
|
|
|
type Permission struct {
|
|
|
|
Contract PermissionDesc `json:"contract"`
|
|
|
|
Methods WildStrings `json:"methods"`
|
|
|
|
}
|
|
|
|
|
2021-02-08 18:07:05 +00:00
|
|
|
// Permissions is just an array of Permission.
|
|
|
|
type Permissions []Permission
|
|
|
|
|
2020-03-18 15:21:12 +00:00
|
|
|
type permissionAux struct {
|
|
|
|
Contract PermissionDesc `json:"contract"`
|
|
|
|
Methods WildStrings `json:"methods"`
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// NewPermission returns a new permission of the given type.
|
2023-04-03 10:34:24 +00:00
|
|
|
func NewPermission(typ PermissionType, args ...any) *Permission {
|
2020-03-18 15:21:12 +00:00
|
|
|
return &Permission{
|
|
|
|
Contract: *newPermissionDesc(typ, args...),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-03 10:34:24 +00:00
|
|
|
func newPermissionDesc(typ PermissionType, args ...any) *PermissionDesc {
|
2020-03-18 15:21:12 +00:00
|
|
|
desc := &PermissionDesc{Type: typ}
|
|
|
|
switch typ {
|
|
|
|
case PermissionWildcard:
|
|
|
|
if len(args) != 0 {
|
|
|
|
panic("wildcard permission has no arguments")
|
|
|
|
}
|
|
|
|
case PermissionHash:
|
|
|
|
if len(args) == 0 {
|
|
|
|
panic("hash permission should have an argument")
|
|
|
|
} else if u, ok := args[0].(util.Uint160); !ok {
|
|
|
|
panic("hash permission should have util.Uint160 argument")
|
|
|
|
} else {
|
|
|
|
desc.Value = u
|
|
|
|
}
|
|
|
|
case PermissionGroup:
|
|
|
|
if len(args) == 0 {
|
|
|
|
panic("group permission should have an argument")
|
|
|
|
} else if pub, ok := args[0].(*keys.PublicKey); !ok {
|
|
|
|
panic("group permission should have a public key argument")
|
|
|
|
} else {
|
|
|
|
desc.Value = pub
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return desc
|
|
|
|
}
|
|
|
|
|
|
|
|
// Hash returns hash for hash-permission.
|
|
|
|
func (d *PermissionDesc) Hash() util.Uint160 {
|
|
|
|
return d.Value.(util.Uint160)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Group returns group's public key for group-permission.
|
|
|
|
func (d *PermissionDesc) Group() *keys.PublicKey {
|
|
|
|
return d.Value.(*keys.PublicKey)
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Less returns true if this value is less than the given PermissionDesc value.
|
2021-05-04 11:48:50 +00:00
|
|
|
func (d *PermissionDesc) Less(d1 PermissionDesc) bool {
|
2024-08-26 16:45:53 +00:00
|
|
|
return d.Compare(d1) < 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compare performs three-way comparison of two [PermissionDesc] values.
|
|
|
|
func (d PermissionDesc) Compare(d1 PermissionDesc) int {
|
|
|
|
r := cmp.Compare(d.Type, d1.Type)
|
|
|
|
if r != 0 {
|
|
|
|
return r
|
2021-05-04 11:48:50 +00:00
|
|
|
}
|
|
|
|
switch d.Type {
|
|
|
|
case PermissionHash:
|
2024-08-26 16:45:53 +00:00
|
|
|
return d.Hash().Compare(d1.Hash())
|
2021-05-04 11:48:50 +00:00
|
|
|
case PermissionGroup:
|
2024-08-26 16:45:53 +00:00
|
|
|
return d.Group().Cmp(d1.Group())
|
*: fix linter exhaustive errors
```
pkg/smartcontract/rpcbinding/binding.go:523:2 exhaustive missing
cases in switch of type smartcontract.ParamType: smartcontract
.UnknownType
pkg/smartcontract/rpcbinding/binding.go:572:2 exhaustive missing
cases in switch of type smartcontract.ParamType: smartcontract
.UnknownType
pkg/smartcontract/rpcbinding/binding.go:862:2 exhaustive missing
cases in switch of type smartcontract.ParamType: smartcontract
.UnknownType, smartcontract.AnyType, smartcontract.BoolType,
smartcontract.IntegerType, smartcontract.ByteArrayType, smartcontract
.Hash160Type, smartcontract.Hash256Type, smartcontract.SignatureType,
smartcontract.InteropInterfaceType, smartcontract.VoidType
pkg/smartcontract/param_type.go:165:2 exhaustive missing
cases in switch of type smartcontract.ParamType: smartcontract
.UnknownType
pkg/smartcontract/manifest/permission.go:103:2 exhaustive missing
cases in switch of type manifest.PermissionType: manifest
.PermissionWildcard
pkg/services/notary/core_test.go:223:4 exhaustive missing
cases in switch of type notary.RequestType: notary.Contract
pkg/services/notary/core_test.go:292:4 exhaustive missing
cases in switch of type notary.RequestType: notary.Contract
pkg/services/oracle/jsonpath/jsonpath.go:62:3 exhaustive missing
cases in switch of type jsonpath.pathTokenType: jsonpath.pathInvalid,
jsonpath.pathRoot, jsonpath.pathRightBracket, jsonpath.pathAsterisk,
jsonpath.pathComma, jsonpath.pathColon, jsonpath.pathIdentifier,
jsonpath.pathString, jsonpath.pathNumber
pkg/services/rpcsrv/server.go:2740:3 exhaustive missing
cases in switch of type neorpc.EventID: neorpc.InvalidEventID, neorpc
.MissedEventID
pkg/services/rpcsrv/server.go:2804:2 exhaustive missing
cases in switch of type neorpc.EventID: neorpc.InvalidEventID, neorpc
.MissedEventID
pkg/services/rpcsrv/server.go:2864:2 exhaustive missing
cases in switch of type neorpc.EventID: neorpc.InvalidEventID, neorpc
.MissedEventID
pkg/vm/contract_checks.go:153:3 exhaustive missing
cases in switch of type opcode.Opcode: opcode.PUSHINT8, opcode
.PUSHINT16, opcode.PUSHINT32, opcode.PUSHINT64, opcode.PUSHINT128,
opcode.PUSHINT256, opcode.PUSHT, opcode.PUSHF, opcode.PUSHNULL, opcode
.PUSHDATA1, opcode.PUSHDATA2, opcode.PUSHDATA4, opcode.PUSHM1, opcode
.PUSH0, opcode.PUSH1, opcode.PUSH2, opcode.PUSH3, opcode.PUSH4, opcode
.PUSH5, opcode.PUSH6, opcode.PUSH7, opcode.PUSH8, opcode.PUSH9, opcode
.PUSH10, opcode.PUSH11, opcode.PUSH12, opcode.PUSH13, opcode.PUSH14,
opcode.PUSH15, opcode.PUSH16, opcode.NOP, opcode.CALLA, opcode.CALLT,
opcode.ABORT, opcode.ASSERT, opcode.THROW, opcode.ENDFINALLY, opcode
.RET, opcode.SYSCALL, opcode.DEPTH, opcode.DROP, opcode.NIP, opcode
.XDROP, opcode.CLEAR, opcode.DUP, opcode.OVER, opcode.PICK, opcode.TUCK,
opcode.SWAP, opcode.ROT, opcode.ROLL, opcode.REVERSE3, opcode.REVERSE4,
opcode.REVERSEN, opcode.INITSSLOT, opcode.INITSLOT, opcode.LDSFLD0,
opcode.LDSFLD1, opcode.LDSFLD2, opcode.LDSFLD3, opcode.LDSFLD4, opcode
.LDSFLD5, opcode.LDSFLD6, opcode.LDSFLD, opcode.STSFLD0, opcode
.STSFLD1, opcode.STSFLD2, opcode.STSFLD3, opcode.STSFLD4, opcode
.STSFLD5, opcode.STSFLD6, opcode.STSFLD, opcode.LDLOC0, opcode.LDLOC1,
opcode.LDLOC2, opcode.LDLOC3, opcode.LDLOC4, opcode.LDLOC5, opcode
.LDLOC6, opcode.LDLOC, opcode.STLOC0, opcode.STLOC1, opcode.STLOC2,
opcode.STLOC3, opcode.STLOC4, opcode.STLOC5, opcode.STLOC6, opcode
.STLOC, opcode.LDARG0, opcode.LDARG1, opcode.LDARG2, opcode.LDARG3,
opcode.LDARG4, opcode.LDARG5, opcode.LDARG6, opcode.LDARG, opcode
.STARG0, opcode.STARG1, opcode.STARG2, opcode.STARG3, opcode.STARG4,
opcode.STARG5, opcode.STARG6, opcode.STARG, opcode.NEWBUFFER, opcode
.MEMCPY, opcode.CAT, opcode.SUBSTR, opcode.LEFT, opcode.RIGHT, opcode
.INVERT, opcode.AND, opcode.OR, opcode.XOR, opcode.EQUAL, opcode
.NOTEQUAL, opcode.SIGN, opcode.ABS, opcode.NEGATE, opcode.INC, opcode
.DEC, opcode.ADD, opcode.SUB, opcode.MUL, opcode.DIV, opcode.MOD,
opcode.POW, opcode.SQRT, opcode.MODMUL, opcode.MODPOW, opcode.SHL,
opcode.SHR, opcode.NOT, opcode.BOOLAND, opcode.BOOLOR, opcode.NZ,
opcode.NUMEQUAL, opcode.NUMNOTEQUAL, opcode.LT, opcode.LE, opcode.GT,
opcode.GE, opcode.MIN, opcode.MAX, opcode.WITHIN, opcode.PACKMAP,
opcode.PACKSTRUCT, opcode.PACK, opcode.UNPACK, opcode.NEWARRAY0,
opcode.NEWARRAY, opcode.NEWSTRUCT0, opcode.NEWSTRUCT, opcode.NEWMAP,
opcode.SIZE, opcode.HASKEY, opcode.KEYS, opcode.VALUES, opcode
.PICKITEM, opcode.APPEND, opcode.SETITEM, opcode.REVERSEITEMS,
opcode.REMOVE, opcode.CLEARITEMS, opcode.POPITEM, opcode.ISNULL,
opcode.ABORTMSG, opcode.ASSERTMSG
pkg/vm/vm.go:912:3 exhaustive missing
cases in switch of type opcode.Opcode: opcode.PUSHINT8, opcode
.PUSHINT16, opcode.PUSHINT32, opcode.PUSHINT64, opcode.PUSHINT128,
opcode.PUSHINT256, opcode.PUSHT, opcode.PUSHF, opcode.PUSHA, opcode
.PUSHNULL, opcode.PUSHDATA1, opcode.PUSHDATA2, opcode.PUSHDATA4, opcode
.PUSHM1, opcode.PUSH0, opcode.PUSH1, opcode.PUSH2, opcode.PUSH3, opcode
.PUSH4, opcode.PUSH5, opcode.PUSH6, opcode.PUSH7, opcode.PUSH8, opcode
.PUSH9, opcode.PUSH10, opcode.PUSH11, opcode.PUSH12, opcode.PUSH13,
opcode.PUSH14, opcode.PUSH15, opcode.PUSH16, opcode.NOP, opcode.JMP,
opcode.JMPL, opcode.JMPIF, opcode.JMPIFL, opcode.JMPIFNOT, opcode
.JMPIFNOTL, opcode.JMPEQ, opcode.JMPEQL, opcode.JMPNE, opcode.JMPNEL,
opcode.JMPGT, opcode.JMPGTL, opcode.JMPGE, opcode.JMPGEL, opcode.JMPLT,
opcode.JMPLTL, opcode.JMPLE, opcode.JMPLEL, opcode.CALL, opcode.CALLL,
opcode.CALLA, opcode.CALLT, opcode.ABORT, opcode.ASSERT, opcode.THROW,
opcode.TRY, opcode.TRYL, opcode.ENDTRY, opcode.ENDTRYL, opcode
.ENDFINALLY, opcode.RET, opcode.SYSCALL, opcode.DEPTH, opcode.DROP,
opcode.NIP, opcode.XDROP, opcode.CLEAR, opcode.DUP, opcode.OVER, opcode
.PICK, opcode.TUCK, opcode.SWAP, opcode.ROT, opcode.ROLL, opcode
.REVERSE3, opcode.INITSSLOT, opcode.INITSLOT, opcode.LDSFLD0, opcode
.LDSFLD1, opcode.LDSFLD2, opcode.LDSFLD3, opcode.LDSFLD4, opcode
.LDSFLD5, opcode.LDSFLD6, opcode.LDSFLD, opcode.STSFLD0, opcode.STSFLD1,
opcode.STSFLD2, opcode.STSFLD3, opcode.STSFLD4, opcode.STSFLD5, opcode
.STSFLD6, opcode.STSFLD, opcode.LDLOC0, opcode.LDLOC1, opcode.LDLOC2,
opcode.LDLOC3, opcode.LDLOC4, opcode.LDLOC5, opcode.LDLOC6, opcode
.LDLOC, opcode.STLOC0, opcode.STLOC1, opcode.STLOC2, opcode.STLOC3,
opcode.STLOC4, opcode.STLOC5, opcode.STLOC6, opcode.STLOC, opcode
.LDARG0, opcode.LDARG1, opcode.LDARG2, opcode.LDARG3, opcode.LDARG4,
opcode.LDARG5, opcode.LDARG6, opcode.LDARG, opcode.STARG0, opcode
.STARG1, opcode.STARG2, opcode.STARG3, opcode.STARG4, opcode.STARG5,
opcode.STARG6, opcode.STARG, opcode.NEWBUFFER, opcode.MEMCPY, opcode
.CAT, opcode.SUBSTR, opcode.LEFT, opcode.RIGHT, opcode.INVERT, opcode
.AND, opcode.OR, opcode.XOR, opcode.EQUAL, opcode.NOTEQUAL, opcode
.SIGN, opcode.ABS, opcode.NEGATE, opcode.INC, opcode.DEC, opcode.ADD,
opcode.SUB, opcode.MUL, opcode.DIV, opcode.MOD, opcode.POW, opcode
.SQRT, opcode.MODMUL, opcode.MODPOW, opcode.SHL, opcode.SHR, opcode
.NOT, opcode.BOOLAND, opcode.BOOLOR, opcode.NZ, opcode.NUMEQUAL, opcode
.NUMNOTEQUAL, opcode.LT, opcode.LE, opcode.GT, opcode.GE, opcode.MIN,
opcode.MAX, opcode.WITHIN, opcode.PACKMAP, opcode.PACKSTRUCT, opcode
.PACK, opcode.UNPACK, opcode.NEWARRAY0, opcode.NEWARRAY, opcode
.NEWARRAYT, opcode.NEWSTRUCT0, opcode.NEWSTRUCT, opcode.NEWMAP, opcode
.SIZE, opcode.HASKEY, opcode.KEYS, opcode.VALUES, opcode.PICKITEM,
opcode.APPEND, opcode.SETITEM, opcode.REVERSEITEMS, opcode.REMOVE,
opcode.CLEARITEMS, opcode.POPITEM, opcode.ISNULL, opcode.ISTYPE, opcode
.CONVERT, opcode.ABORTMSG, opcode.ASSERTMSG
pkg/vm/vm.go:1116:4 exhaustive missing
cases in switch of type opcode.Opcode: opcode.PUSHINT8, opcode
.PUSHINT16, opcode.PUSHINT32, opcode.PUSHINT64, opcode.PUSHINT128,
opcode.PUSHINT256, opcode.PUSHT, opcode.PUSHF, opcode.PUSHA, opcode
.PUSHNULL, opcode.PUSHDATA1, opcode.PUSHDATA2, opcode.PUSHDATA4, opcode
.PUSHM1, opcode.PUSH0, opcode.PUSH1, opcode.PUSH2, opcode.PUSH3, opcode
.PUSH4, opcode.PUSH5, opcode.PUSH6, opcode.PUSH7, opcode.PUSH8, opcode
.PUSH9, opcode.PUSH10, opcode.PUSH11, opcode.PUSH12, opcode.PUSH13,
opcode.PUSH14, opcode.PUSH15, opcode.PUSH16, opcode.NOP, opcode.JMP,
opcode.JMPL, opcode.JMPIF, opcode.JMPIFL, opcode.JMPIFNOT, opcode
.JMPIFNOTL, opcode.JMPEQ, opcode.JMPEQL, opcode.JMPNE, opcode.JMPNEL,
opcode.JMPGT, opcode.JMPGTL, opcode.JMPGE, opcode.JMPGEL, opcode.JMPLT,
opcode.JMPLTL, opcode.JMPLE, opcode.JMPLEL, opcode.CALL, opcode.CALLL,
opcode.CALLA, opcode.CALLT, opcode.ABORT, opcode.ASSERT, opcode.THROW,
opcode.TRY, opcode.TRYL, opcode.ENDTRY, opcode.ENDTRYL, opcode
.ENDFINALLY, opcode.RET, opcode.SYSCALL, opcode.DEPTH, opcode.DROP,
opcode.NIP, opcode.XDROP, opcode.CLEAR, opcode.DUP, opcode.OVER, opcode
.PICK, opcode.TUCK, opcode.SWAP, opcode.ROT, opcode.ROLL, opcode
.REVERSE3, opcode.REVERSE4, opcode.REVERSEN, opcode.INITSSLOT, opcode
.INITSLOT, opcode.LDSFLD0, opcode.LDSFLD1, opcode.LDSFLD2, opcode
.LDSFLD3, opcode.LDSFLD4, opcode.LDSFLD5, opcode.LDSFLD6, opcode.LDSFLD,
opcode.STSFLD0, opcode.STSFLD1, opcode.STSFLD2, opcode.STSFLD3, opcode
.STSFLD4, opcode.STSFLD5, opcode.STSFLD6, opcode.STSFLD, opcode.LDLOC0,
opcode.LDLOC1, opcode.LDLOC2, opcode.LDLOC3, opcode.LDLOC4, opcode
.LDLOC5, opcode.LDLOC6, opcode.LDLOC, opcode.STLOC0, opcode.STLOC1,
opcode.STLOC2, opcode.STLOC3, opcode.STLOC4, opcode.STLOC5, opcode
.STLOC6, opcode.STLOC, opcode.LDARG0, opcode.LDARG1, opcode.LDARG2,
opcode.LDARG3, opcode.LDARG4, opcode.LDARG5, opcode.LDARG6, opcode
.LDARG, opcode.STARG0, opcode.STARG1, opcode.STARG2, opcode.STARG3,
opcode.STARG4, opcode.STARG5, opcode.STARG6, opcode.STARG, opcode
.NEWBUFFER, opcode.MEMCPY, opcode.CAT, opcode.SUBSTR, opcode.LEFT,
opcode.RIGHT, opcode.INVERT, opcode.AND, opcode.OR, opcode.XOR, opcode
.EQUAL, opcode.NOTEQUAL, opcode.SIGN, opcode.ABS, opcode.NEGATE,
opcode.INC, opcode.DEC, opcode.ADD, opcode.SUB, opcode.MUL, opcode
.DIV, opcode.MOD, opcode.POW, opcode.SQRT, opcode.MODMUL, opcode
.MODPOW, opcode.SHL, opcode.SHR, opcode.NOT, opcode.BOOLAND, opcode
.BOOLOR, opcode.NZ, opcode.NUMEQUAL, opcode.NUMNOTEQUAL, opcode.MIN,
opcode.MAX, opcode.WITHIN, opcode.PACKMAP, opcode.PACKSTRUCT, opcode
.PACK, opcode.UNPACK, opcode.NEWARRAY0, opcode.NEWARRAY, opcode
.NEWARRAYT, opcode.NEWSTRUCT0, opcode.NEWSTRUCT, opcode.NEWMAP, opcode
.SIZE, opcode.HASKEY, opcode.KEYS, opcode.VALUES, opcode.PICKITEM,
opcode.APPEND, opcode.SETITEM, opcode.REVERSEITEMS, opcode.REMOVE,
opcode.CLEARITEMS, opcode.POPITEM, opcode.ISNULL, opcode.ISTYPE,
opcode.CONVERT, opcode.ABORTMSG, opcode.ASSERTMSG
pkg/compiler/codegen.go:944:5 exhaustive missing
cases in switch of type smartcontract.ParamType: smartcontract
.UnknownType, smartcontract.AnyType, smartcontract.BoolType,
smartcontract.IntegerType, smartcontract.ByteArrayType, smartcontract
.StringType, smartcontract.PublicKeyType, smartcontract.SignatureType,
smartcontract.ArrayType, smartcontract.MapType, smartcontract
.InteropInterfaceType, smartcontract.VoidType
pkg/compiler/codegen.go:1221:3 exhaustive missing
cases in switch of type token.Token: token.ILLEGAL, token.EOF, token
.COMMENT, token.IDENT, token.INT, token.FLOAT, token.IMAG, token.CHAR,
token.STRING, token.ADD, token.SUB, token.MUL, token.QUO, token.REM,
token.AND, token.OR, token.XOR, token.SHL, token.SHR, token.AND_NOT,
token.ADD_ASSIGN, token.SUB_ASSIGN, token.MUL_ASSIGN, token.QUO_ASSIGN,
token.REM_ASSIGN, token.AND_ASSIGN, token.OR_ASSIGN, token.XOR_ASSIGN,
token.SHL_ASSIGN, token.SHR_ASSIGN, token.AND_NOT_ASSIGN, token.LAND,
token.LOR, token.ARROW, token.INC, token.DEC, token.EQL, token.LSS,
token.GTR, token.ASSIGN, token.NOT, token.NEQ, token.LEQ, token.GEQ,
token.DEFINE, token.ELLIPSIS, token.LPAREN, token.LBRACK, token.LBRACE,
token.COMMA, token.PERIOD, token.RPAREN, token.RBRACK, token.RBRACE,
token.SEMICOLON, token.COLON, token.CASE, token.CHAN, token.CONST, token
.DEFAULT, token.DEFER, token.ELSE, token.FALLTHROUGH, token.FOR, token
.FUNC, token.GO, token.GOTO, token.IF, token.IMPORT, token.INTERFACE,
token.MAP, token.PACKAGE, token.RANGE, token.RETURN, token.SELECT, token
.STRUCT, token.SWITCH, token.TYPE, token.VAR, token.TILDE
pkg/compiler/codegen.go:1709:2 exhaustive missing
cases in switch of type token.Token: token.ILLEGAL, token.EOF, token
.COMMENT, token.IDENT, token.INT, token.FLOAT, token.IMAG, token.CHAR,
token.STRING, token.ADD, token.SUB, token.MUL, token.QUO, token.REM,
token.AND, token.OR, token.XOR, token.SHL, token.SHR, token.AND_NOT,
token.ADD_ASSIGN, token.SUB_ASSIGN, token.MUL_ASSIGN, token.QUO_ASSIGN,
token.REM_ASSIGN, token.AND_ASSIGN, token.OR_ASSIGN, token.XOR_ASSIGN,
token.SHL_ASSIGN, token.SHR_ASSIGN, token.AND_NOT_ASSIGN, token.LAND,
token.LOR, token.ARROW, token.INC, token.DEC, token.ASSIGN, token.NOT,
token.DEFINE, token.ELLIPSIS, token.LPAREN, token.LBRACK, token.LBRACE,
token.COMMA, token.PERIOD, token.RPAREN, token.RBRACK, token.RBRACE,
token.SEMICOLON, token.COLON, token.BREAK, token.CASE, token.CHAN, token
.CONST, token.CONTINUE, token.DEFAULT, token.DEFER, token.ELSE, token
.FALLTHROUGH, token.FOR, token.FUNC, token.GO, token.GOTO, token.IF,
token.IMPORT, token.INTERFACE, token.MAP, token.PACKAGE, token.RANGE,
token.RETURN, token.SELECT, token.STRUCT, token.SWITCH, token.TYPE,
token.VAR, token.TILDE
pkg/compiler/codegen.go:2353:3 exhaustive missing
cases in switch of type opcode.Opcode: opcode.PUSHINT8, opcode
.PUSHINT16, opcode.PUSHINT32, opcode.PUSHINT64, opcode.PUSHINT128,
opcode.PUSHINT256, opcode.PUSHT, opcode.PUSHF, opcode.PUSHNULL, opcode
.PUSHDATA1, opcode.PUSHDATA2, opcode.PUSHDATA4, opcode.PUSHM1, opcode
.PUSH0, opcode.PUSH1, opcode.PUSH2, opcode.PUSH3, opcode.PUSH4, opcode
.PUSH5, opcode.PUSH6, opcode.PUSH7, opcode.PUSH8, opcode.PUSH9, opcode
.PUSH10, opcode.PUSH11, opcode.PUSH12, opcode.PUSH13, opcode.PUSH14,
opcode.PUSH15, opcode.PUSH16, opcode.NOP, opcode.CALLA, opcode.CALLT,
opcode.ABORT, opcode.ASSERT, opcode.THROW, opcode.TRY, opcode.ENDTRY,
opcode.ENDFINALLY, opcode.RET, opcode.SYSCALL, opcode.DEPTH, opcode
.DROP, opcode.NIP, opcode.XDROP, opcode.CLEAR, opcode.DUP, opcode.OVER,
opcode.PICK, opcode.TUCK, opcode.SWAP, opcode.ROT, opcode.ROLL, opcode
.REVERSE3, opcode.REVERSE4, opcode.REVERSEN, opcode.INITSSLOT, opcode
.LDSFLD0, opcode.LDSFLD1, opcode.LDSFLD2, opcode.LDSFLD3, opcode
.LDSFLD4, opcode.LDSFLD5, opcode.LDSFLD6, opcode.LDSFLD, opcode.STSFLD0,
opcode.STSFLD1, opcode.STSFLD2, opcode.STSFLD3, opcode.STSFLD4, opcode
.STSFLD5, opcode.STSFLD6, opcode.STSFLD, opcode.LDLOC0, opcode.LDLOC1,
opcode.LDLOC2, opcode.LDLOC3, opcode.LDLOC4, opcode.LDLOC5, opcode
.LDLOC6, opcode.LDLOC, opcode.STLOC0, opcode.STLOC1, opcode.STLOC2,
opcode.STLOC3, opcode.STLOC4, opcode.STLOC5, opcode.STLOC6, opcode
.STLOC, opcode.LDARG0, opcode.LDARG1, opcode.LDARG2, opcode.LDARG3,
opcode.LDARG4, opcode.LDARG5, opcode.LDARG6, opcode.LDARG, opcode
.STARG0, opcode.STARG1, opcode.STARG2, opcode.STARG3, opcode.STARG4,
opcode.STARG5, opcode.STARG6, opcode.STARG, opcode.NEWBUFFER, opcode
.MEMCPY, opcode.CAT, opcode.SUBSTR, opcode.LEFT, opcode.RIGHT, opcode
.INVERT, opcode.AND, opcode.OR, opcode.XOR, opcode.EQUAL, opcode
.NOTEQUAL, opcode.SIGN, opcode.ABS, opcode.NEGATE, opcode.INC, opcode
.DEC, opcode.ADD, opcode.SUB, opcode.MUL, opcode.DIV, opcode.MOD,
opcode.POW, opcode.SQRT, opcode.MODMUL, opcode.MODPOW, opcode.SHL,
opcode.SHR, opcode.NOT, opcode.BOOLAND, opcode.BOOLOR, opcode.NZ,
opcode.NUMEQUAL, opcode.NUMNOTEQUAL, opcode.LT, opcode.LE, opcode.GT,
opcode.GE, opcode.MIN, opcode.MAX, opcode.WITHIN, opcode.PACKMAP,
opcode.PACKSTRUCT, opcode.PACK, opcode.UNPACK, opcode.NEWARRAY0, opcode
.NEWARRAY, opcode.NEWARRAYT, opcode.NEWSTRUCT0, opcode.NEWSTRUCT,
opcode.NEWMAP, opcode.SIZE, opcode.HASKEY, opcode.KEYS, opcode.VALUES,
opcode.PICKITEM, opcode.APPEND, opcode.SETITEM, opcode.REVERSEITEMS,
opcode.REMOVE, opcode.CLEARITEMS, opcode.POPITEM, opcode.ISNULL, opcode
.ISTYPE, opcode.CONVERT, opcode.ABORTMSG, opcode.ASSERTMSG
pkg/compiler/codegen.go:2474:3 exhaustive missing
cases in switch of type opcode.Opcode: opcode.PUSHINT8, opcode
.PUSHINT16, opcode.PUSHINT32, opcode.PUSHINT64, opcode.PUSHINT128,
opcode.PUSHINT256, opcode.PUSHT, opcode.PUSHF, opcode.PUSHNULL, opcode
.PUSHDATA1, opcode.PUSHDATA2, opcode.PUSHDATA4, opcode.PUSHM1, opcode
.PUSH0, opcode.PUSH1, opcode.PUSH2, opcode.PUSH3, opcode.PUSH4, opcode
.PUSH5, opcode.PUSH6, opcode.PUSH7, opcode.PUSH8, opcode.PUSH9, opcode
.PUSH10, opcode.PUSH11, opcode.PUSH12, opcode.PUSH13, opcode.PUSH14,
opcode.PUSH15, opcode.PUSH16, opcode.NOP, opcode.CALLA, opcode.CALLT,
opcode.ABORT, opcode.ASSERT, opcode.THROW, opcode.ENDFINALLY, opcode
.RET, opcode.SYSCALL, opcode.DEPTH, opcode.DROP, opcode.NIP, opcode
.XDROP, opcode.CLEAR, opcode.DUP, opcode.OVER, opcode.PICK, opcode.TUCK,
opcode.SWAP, opcode.ROT, opcode.ROLL, opcode.REVERSE3, opcode.REVERSE4,
opcode.REVERSEN, opcode.INITSSLOT, opcode.INITSLOT, opcode.LDSFLD0,
opcode.LDSFLD1, opcode.LDSFLD2, opcode.LDSFLD3, opcode.LDSFLD4, opcode
.LDSFLD5, opcode.LDSFLD6, opcode.LDSFLD, opcode.STSFLD0, opcode
.STSFLD1, opcode.STSFLD2, opcode.STSFLD3, opcode.STSFLD4, opcode
.STSFLD5, opcode.STSFLD6, opcode.STSFLD, opcode.LDLOC0, opcode.LDLOC1,
opcode.LDLOC2, opcode.LDLOC3, opcode.LDLOC4, opcode.LDLOC5, opcode
.LDLOC6, opcode.LDLOC, opcode.STLOC0, opcode.STLOC1, opcode.STLOC2,
opcode.STLOC3, opcode.STLOC4, opcode.STLOC5, opcode.STLOC6, opcode
.STLOC, opcode.LDARG0, opcode.LDARG1, opcode.LDARG2, opcode.LDARG3,
opcode.LDARG4, opcode.LDARG5, opcode.LDARG6, opcode.LDARG, opcode
.STARG0, opcode.STARG1, opcode.STARG2, opcode.STARG3, opcode.STARG4,
opcode.STARG5, opcode.STARG6, opcode.STARG, opcode.NEWBUFFER, opcode
.MEMCPY, opcode.CAT, opcode.SUBSTR, opcode.LEFT, opcode.RIGHT, opcode
.INVERT, opcode.AND, opcode.OR, opcode.XOR, opcode.EQUAL, opcode
.NOTEQUAL, opcode.SIGN, opcode.ABS, opcode.NEGATE, opcode.INC, opcode
.DEC, opcode.ADD, opcode.SUB, opcode.MUL, opcode.DIV, opcode.MOD,
opcode.POW, opcode.SQRT, opcode.MODMUL, opcode.MODPOW, opcode.SHL,
opcode.SHR, opcode.NOT, opcode.BOOLAND, opcode.BOOLOR, opcode.NZ,
opcode.NUMEQUAL, opcode.NUMNOTEQUAL, opcode.LT, opcode.LE, opcode.GT,
opcode.GE, opcode.MIN, opcode.MAX, opcode.WITHIN, opcode.PACKMAP,
opcode.PACKSTRUCT, opcode.PACK, opcode.UNPACK, opcode.NEWARRAY0,
opcode.NEWARRAY, opcode.NEWARRAYT, opcode.NEWSTRUCT0, opcode
.NEWSTRUCT, opcode.NEWMAP, opcode.SIZE, opcode.HASKEY, opcode.KEYS,
opcode.VALUES, opcode.PICKITEM, opcode.APPEND, opcode.SETITEM,
opcode.REVERSEITEMS, opcode.REMOVE, opcode.CLEARITEMS, opcode
.POPITEM, opcode.ISNULL, opcode.ISTYPE, opcode.CONVERT, opcode
.ABORTMSG, opcode.ASSERTMSG
pkg/compiler/inline_test.go:34:3 exhaustive missing
cases in switch of type opcode.Opcode: opcode.PUSHINT8, opcode
.PUSHINT16, opcode.PUSHINT32, opcode.PUSHINT64, opcode.PUSHINT128,
opcode.PUSHINT256, opcode.PUSHT, opcode.PUSHF, opcode.PUSHA, opcode
.PUSHNULL, opcode.PUSHDATA1, opcode.PUSHDATA2, opcode.PUSHDATA4, opcode
.PUSHM1, opcode.PUSH0, opcode.PUSH1, opcode.PUSH2, opcode.PUSH3, opcode
.PUSH4, opcode.PUSH5, opcode.PUSH6, opcode.PUSH7, opcode.PUSH8, opcode
.PUSH9, opcode.PUSH10, opcode.PUSH11, opcode.PUSH12, opcode.PUSH13,
opcode.PUSH14, opcode.PUSH15, opcode.PUSH16, opcode.NOP, opcode.JMP,
opcode.JMPL, opcode.JMPIF, opcode.JMPIFL, opcode.JMPIFNOT, opcode
.JMPIFNOTL, opcode.JMPEQ, opcode.JMPEQL, opcode.JMPNE, opcode.JMPNEL,
opcode.JMPGT, opcode.JMPGTL, opcode.JMPGE, opcode.JMPGEL, opcode.JMPLT,
opcode.JMPLTL, opcode.JMPLE, opcode.JMPLEL, opcode.CALLA, opcode.CALLT,
opcode.ABORT, opcode.ASSERT, opcode.THROW, opcode.TRY, opcode.TRYL,
opcode.ENDTRY, opcode.ENDTRYL, opcode.ENDFINALLY, opcode.RET, opcode
.SYSCALL, opcode.DEPTH, opcode.DROP, opcode.NIP, opcode.XDROP, opcode
.CLEAR, opcode.DUP, opcode.OVER, opcode.PICK, opcode.TUCK, opcode.SWAP,
opcode.ROT, opcode.ROLL, opcode.REVERSE3, opcode.REVERSE4, opcode
.REVERSEN, opcode.LDSFLD0, opcode.LDSFLD1, opcode.LDSFLD2, opcode
.LDSFLD3, opcode.LDSFLD4, opcode.LDSFLD5, opcode.LDSFLD6, opcode.LDSFLD,
opcode.STSFLD0, opcode.STSFLD1, opcode.STSFLD2, opcode.STSFLD3, opcode
.STSFLD4, opcode.STSFLD5, opcode.STSFLD6, opcode.STSFLD, opcode.LDLOC0,
opcode.LDLOC1, opcode.LDLOC2, opcode.LDLOC3, opcode.LDLOC4, opcode
.LDLOC5, opcode.LDLOC6, opcode.LDLOC, opcode.STLOC0, opcode.STLOC1,
opcode.STLOC2, opcode.STLOC3, opcode.STLOC4, opcode.STLOC5, opcode
.STLOC6, opcode.STLOC, opcode.LDARG0, opcode.LDARG1, opcode.LDARG2,
opcode.LDARG3, opcode.LDARG4, opcode.LDARG5, opcode.LDARG6, opcode
.LDARG, opcode.STARG0, opcode.STARG1, opcode.STARG2, opcode.STARG3,
opcode.STARG4, opcode.STARG5, opcode.STARG6, opcode.STARG, opcode
.NEWBUFFER, opcode.MEMCPY, opcode.CAT, opcode.SUBSTR, opcode.LEFT,
opcode.RIGHT, opcode.INVERT, opcode.AND, opcode.OR, opcode.XOR, opcode
.EQUAL, opcode.NOTEQUAL, opcode.SIGN, opcode.ABS, opcode.NEGATE,
opcode.INC, opcode.DEC, opcode.ADD, opcode.SUB, opcode.MUL, opcode
.DIV, opcode.MOD, opcode.POW, opcode.SQRT, opcode.MODMUL, opcode
.MODPOW, opcode.SHL, opcode.SHR, opcode.NOT, opcode.BOOLAND, opcode
.BOOLOR, opcode.NZ, opcode.NUMEQUAL, opcode.NUMNOTEQUAL, opcode.LT,
opcode.LE, opcode.GT, opcode.GE, opcode.MIN, opcode.MAX, opcode
.WITHIN, opcode.PACKMAP, opcode.PACKSTRUCT, opcode.PACK, opcode
.UNPACK, opcode.NEWARRAY0, opcode.NEWARRAY, opcode.NEWARRAYT, opcode
.NEWSTRUCT0, opcode.NEWSTRUCT, opcode.NEWMAP, opcode.SIZE, opcode
.HASKEY, opcode.KEYS, opcode.VALUES, opcode.PICKITEM, opcode.APPEND,
opcode.SETITEM, opcode.REVERSEITEMS, opcode.REMOVE, opcode.CLEARITEMS,
opcode.POPITEM, opcode.ISNULL, opcode.ISTYPE, opcode.CONVERT, opcode
.ABORTMSG, opcode.ASSERTMSG
pkg/network/server.go:1395:3 exhaustive missing
cases in switch of type network.CommandType: network.CMDNotFound,
network.CMDReject, network.CMDFilterLoad, network.CMDFilterAdd, network
.CMDFilterClear, network.CMDMerkleBlock, network.CMDAlert
pkg/network/server_test.go:532:3 exhaustive missing
cases in switch of type network.CommandType: network.CMDVersion, network
.CMDVerack, network.CMDGetAddr, network.CMDAddr, network.CMDPing,
network.CMDPong, network.CMDGetHeaders, network.CMDHeaders, network
.CMDGetBlocks, network.CMDMempool, network.CMDInv, network.CMDGetData,
network.CMDGetBlockByIndex, network.CMDGetMPTData, network.CMDMPTData,
network.CMDReject, network.CMDFilterLoad, network.CMDFilterAdd, network
.CMDFilterClear, network.CMDMerkleBlock, network.CMDAlert
pkg/network/server_test.go:817:4 exhaustive missing
cases in switch of type network.CommandType: network.CMDVersion, network
.CMDVerack, network.CMDGetAddr, network.CMDAddr, network.CMDPing,
network.CMDPong, network.CMDGetHeaders, network.CMDHeaders, network
.CMDGetBlocks, network.CMDMempool, network.CMDInv, network.CMDGetData,
network.CMDGetBlockByIndex, network.CMDNotFound, network.CMDTX, network
.CMDBlock, network.CMDExtensible, network.CMDP2PNotaryRequest, network
.CMDGetMPTData, network.CMDReject, network.CMDFilterLoad, network
.CMDFilterAdd, network.CMDFilterClear, network.CMDMerkleBlock, network
.CMDAlert
pkg/core/native/designate.go:262:2 exhaustive missing
cases in switch of type noderoles.Role: noderoles.NeoFSAlphabet
pkg/neorpc/rpcevent/filter.go:36:2 exhaustive missing cases in switch
of type neorpc.EventID: neorpc.InvalidEventID, neorpc.MissedEventID
pkg/consensus/recovery_message.go:145:2 exhaustive missing
cases in switch of type dbft.MessageType: dbft.PreCommitType, dbft
.RecoveryRequestType, dbft.RecoveryMessageType
cli/cmdargs/parser.go:202:3 exhaustive missing cases in switch of type
transaction.WitnessScope: transaction.None, transaction.CalledByEntry,
transaction.Rules, transaction.Global
```
Signed-off-by: Ekaterina Pavlova <ekt@morphbits.io>
2024-09-26 08:30:54 +00:00
|
|
|
default:
|
|
|
|
return 0 // wildcard or type that we can't compare.
|
2021-05-04 11:48:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Equals returns true if both PermissionDesc values are the same.
|
|
|
|
func (d *PermissionDesc) Equals(v PermissionDesc) bool {
|
2024-08-26 16:45:53 +00:00
|
|
|
return d.Compare(v) == 0
|
2021-05-04 11:48:50 +00:00
|
|
|
}
|
|
|
|
|
2021-02-08 18:07:05 +00:00
|
|
|
// IsValid checks if Permission is correct.
|
|
|
|
func (p *Permission) IsValid() error {
|
2024-08-24 19:03:41 +00:00
|
|
|
if slices.Contains(p.Methods.Value, "") {
|
|
|
|
return errors.New("empty method name")
|
2021-02-08 18:07:05 +00:00
|
|
|
}
|
2024-08-26 17:08:58 +00:00
|
|
|
if sliceHasDups(p.Methods.Value, cmp.Compare) {
|
2021-02-08 18:07:05 +00:00
|
|
|
return errors.New("duplicate method names")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// AreValid checks each Permission and ensures there are no duplicates.
|
|
|
|
func (ps Permissions) AreValid() error {
|
|
|
|
for i := range ps {
|
|
|
|
err := ps[i].IsValid()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2024-08-26 17:08:58 +00:00
|
|
|
if sliceHasDups(ps, func(a, b Permission) int {
|
|
|
|
return a.Contract.Compare(b.Contract)
|
|
|
|
}) {
|
2021-05-04 11:48:50 +00:00
|
|
|
return errors.New("contracts have duplicates")
|
2021-02-08 18:07:05 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// IsAllowed checks if the method is allowed to be executed.
|
2020-11-18 09:43:51 +00:00
|
|
|
func (p *Permission) IsAllowed(hash util.Uint160, m *Manifest, method string) bool {
|
2020-03-18 15:21:12 +00:00
|
|
|
switch p.Contract.Type {
|
|
|
|
case PermissionWildcard:
|
|
|
|
case PermissionHash:
|
2020-11-18 09:43:51 +00:00
|
|
|
if !p.Contract.Hash().Equals(hash) {
|
2020-03-18 15:21:12 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
case PermissionGroup:
|
2024-08-24 19:03:41 +00:00
|
|
|
contractG := p.Contract.Group()
|
|
|
|
return slices.ContainsFunc(m.Groups, func(manifestG Group) bool {
|
|
|
|
return contractG.Equal(manifestG.PublicKey)
|
|
|
|
})
|
2020-03-18 15:21:12 +00:00
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("unexpected permission: %d", p.Contract.Type))
|
|
|
|
}
|
|
|
|
if p.Methods.IsWildcard() {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return p.Methods.Contains(method)
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// UnmarshalJSON implements the json.Unmarshaler interface.
|
2020-03-18 15:21:12 +00:00
|
|
|
func (p *Permission) UnmarshalJSON(data []byte) error {
|
|
|
|
aux := new(permissionAux)
|
|
|
|
if err := json.Unmarshal(data, aux); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
p.Contract = aux.Contract
|
|
|
|
p.Methods = aux.Methods
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// MarshalJSON implements the json.Marshaler interface.
|
2020-03-18 15:21:12 +00:00
|
|
|
func (d *PermissionDesc) MarshalJSON() ([]byte, error) {
|
|
|
|
switch d.Type {
|
|
|
|
case PermissionHash:
|
|
|
|
return json.Marshal("0x" + d.Hash().StringLE())
|
|
|
|
case PermissionGroup:
|
2024-04-09 09:13:43 +00:00
|
|
|
return json.Marshal(d.Group().StringCompressed())
|
2020-03-18 15:21:12 +00:00
|
|
|
default:
|
|
|
|
return []byte(`"*"`), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// UnmarshalJSON implements the json.Unmarshaler interface.
|
2020-03-18 15:21:12 +00:00
|
|
|
func (d *PermissionDesc) UnmarshalJSON(data []byte) error {
|
|
|
|
var s string
|
|
|
|
if err := json.Unmarshal(data, &s); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
const uint160HexSize = 2 * util.Uint160Size
|
|
|
|
switch len(s) {
|
|
|
|
case 2 + uint160HexSize:
|
|
|
|
// allow to unmarshal both hex and 0xhex forms
|
|
|
|
if s[0] != '0' || s[1] != 'x' {
|
|
|
|
return errors.New("invalid uint160")
|
|
|
|
}
|
|
|
|
s = s[2:]
|
|
|
|
fallthrough
|
|
|
|
case uint160HexSize:
|
|
|
|
u, err := util.Uint160DecodeStringLE(s)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
d.Type = PermissionHash
|
|
|
|
d.Value = u
|
|
|
|
return nil
|
|
|
|
case 66:
|
|
|
|
pub, err := keys.NewPublicKeyFromString(s)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
d.Type = PermissionGroup
|
|
|
|
d.Value = pub
|
|
|
|
return nil
|
|
|
|
case 1:
|
|
|
|
if s == "*" {
|
|
|
|
d.Type = PermissionWildcard
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return errors.New("unknown permission")
|
|
|
|
}
|
2021-02-03 18:09:50 +00:00
|
|
|
|
2021-05-04 11:48:50 +00:00
|
|
|
// ToStackItem converts PermissionDesc to stackitem.Item.
|
|
|
|
func (d *PermissionDesc) ToStackItem() stackitem.Item {
|
|
|
|
switch d.Type {
|
2021-02-03 18:09:50 +00:00
|
|
|
case PermissionWildcard:
|
2021-05-04 11:48:50 +00:00
|
|
|
return stackitem.Null{}
|
2021-02-03 18:09:50 +00:00
|
|
|
case PermissionHash:
|
2021-05-04 11:48:50 +00:00
|
|
|
return stackitem.NewByteArray(d.Hash().BytesBE())
|
2021-02-03 18:09:50 +00:00
|
|
|
case PermissionGroup:
|
2021-05-04 11:48:50 +00:00
|
|
|
return stackitem.NewByteArray(d.Group().Bytes())
|
|
|
|
default:
|
|
|
|
panic("unsupported PermissionDesc type")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// FromStackItem converts stackitem.Item to PermissionDesc.
|
|
|
|
func (d *PermissionDesc) FromStackItem(item stackitem.Item) error {
|
|
|
|
if _, ok := item.(stackitem.Null); ok {
|
|
|
|
d.Type = PermissionWildcard
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if item.Type() != stackitem.ByteArrayT {
|
|
|
|
return fmt.Errorf("unsupported permission descriptor type: %s", item.Type())
|
|
|
|
}
|
|
|
|
byteArr, err := item.TryBytes()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
switch len(byteArr) {
|
|
|
|
case util.Uint160Size:
|
|
|
|
hash, _ := util.Uint160DecodeBytesBE(byteArr)
|
|
|
|
d.Type = PermissionHash
|
|
|
|
d.Value = hash
|
|
|
|
case 33:
|
|
|
|
pKey, err := keys.NewPublicKeyFromBytes(byteArr, elliptic.P256())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
d.Type = PermissionGroup
|
|
|
|
d.Value = pKey
|
|
|
|
default:
|
|
|
|
return errors.New("invalid ByteArray length")
|
2021-02-03 18:09:50 +00:00
|
|
|
}
|
2021-05-04 11:48:50 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToStackItem converts Permission to stackitem.Item.
|
|
|
|
func (p *Permission) ToStackItem() stackitem.Item {
|
|
|
|
var methods stackitem.Item
|
|
|
|
contract := p.Contract.ToStackItem()
|
2021-02-03 18:09:50 +00:00
|
|
|
if p.Methods.IsWildcard() {
|
|
|
|
methods = stackitem.Null{}
|
|
|
|
} else {
|
|
|
|
m := make([]stackitem.Item, len(p.Methods.Value))
|
|
|
|
for i := range p.Methods.Value {
|
|
|
|
m[i] = stackitem.Make(p.Methods.Value[i])
|
|
|
|
}
|
|
|
|
methods = stackitem.Make(m)
|
|
|
|
}
|
|
|
|
return stackitem.NewStruct([]stackitem.Item{
|
|
|
|
contract,
|
|
|
|
methods,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// FromStackItem converts stackitem.Item to Permission.
|
|
|
|
func (p *Permission) FromStackItem(item stackitem.Item) error {
|
|
|
|
var err error
|
|
|
|
if item.Type() != stackitem.StructT {
|
|
|
|
return errors.New("invalid Permission stackitem type")
|
|
|
|
}
|
|
|
|
str := item.Value().([]stackitem.Item)
|
|
|
|
if len(str) != 2 {
|
|
|
|
return errors.New("invalid Permission stackitem length")
|
|
|
|
}
|
2021-05-04 11:48:50 +00:00
|
|
|
desc := new(PermissionDesc)
|
|
|
|
err = desc.FromStackItem(str[0])
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("invalid Contract stackitem: %w", err)
|
2021-02-03 18:09:50 +00:00
|
|
|
}
|
2021-05-04 11:48:50 +00:00
|
|
|
p.Contract = *desc
|
2021-02-03 18:09:50 +00:00
|
|
|
if _, ok := str[1].(stackitem.Null); ok {
|
|
|
|
p.Methods = WildStrings{Value: nil}
|
|
|
|
} else {
|
|
|
|
if str[1].Type() != stackitem.ArrayT {
|
|
|
|
return errors.New("invalid Methods stackitem type")
|
|
|
|
}
|
|
|
|
methods := str[1].Value().([]stackitem.Item)
|
|
|
|
p.Methods = WildStrings{
|
|
|
|
Value: make([]string, len(methods)),
|
|
|
|
}
|
|
|
|
for i := range methods {
|
|
|
|
p.Methods.Value[i], err = stackitem.ToString(methods[i])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|