diff --git a/Makefile b/Makefile index b657303e..6e5431fa 100644 --- a/Makefile +++ b/Makefile @@ -7,7 +7,7 @@ export GOBIN ?= $(shell pwd)/bin export CGO_ENABLED=0 NEOGO ?= $(GOBIN)/cli VERSION ?= $(shell git describe --tags --dirty --match "v*" --always --abbrev=8 2>/dev/null || cat VERSION 2>/dev/null || echo "develop") -NEOGOORIGMOD = github.com/nspcc-dev/neo-go@v0.103.1 +NEOGOORIGMOD = github.com/nspcc-dev/neo-go@v0.104.0 NEOGOMOD = $(shell go list -f '{{.Path}}' -m $(NEOGOORIGMOD)) NEOGOVER = $(shell go list -f '{{.Version}}' -m $(NEOGOORIGMOD) | tr -d v) diff --git a/README.md b/README.md index 16466edb..f93c7162 100644 --- a/README.md +++ b/README.md @@ -36,7 +36,7 @@ Sidechain contracts: To compile smart contracts you need: -- [neo-go](https://github.com/nspcc-dev/neo-go) >= 0.103.1 +- [neo-go](https://github.com/nspcc-dev/neo-go) >= 0.104.0 ## Compilation diff --git a/go.mod b/go.mod index 451f5119..e40ded43 100644 --- a/go.mod +++ b/go.mod @@ -5,8 +5,8 @@ go 1.19 require ( github.com/google/uuid v1.3.1 github.com/mr-tron/base58 v1.2.0 - github.com/nspcc-dev/neo-go v0.103.1 - github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20231020160724-c3955f87d1b5 + github.com/nspcc-dev/neo-go v0.104.0 + github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20231121104256-0493ddbd70b2 github.com/stretchr/testify v1.8.4 ) @@ -28,7 +28,7 @@ require ( github.com/holiman/uint256 v1.2.0 // indirect github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect github.com/mmcloughlin/addchain v0.4.0 // indirect - github.com/nspcc-dev/go-ordered-json v0.0.0-20220111165707-25110be27d22 // indirect + github.com/nspcc-dev/go-ordered-json v0.0.0-20231123160306-3374ff1e7a3c // indirect github.com/nspcc-dev/rfc6979 v0.2.0 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect github.com/prometheus/client_golang v1.13.0 // indirect diff --git a/go.sum b/go.sum index d7b39195..3ebcd296 100644 --- a/go.sum +++ b/go.sum @@ -195,13 +195,13 @@ github.com/mr-tron/base58 v1.2.0/go.mod h1:BinMc/sQntlIE1frQmRFPUoPA1Zkr8VRgBdjW github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= github.com/nspcc-dev/dbft v0.0.0-20230515113611-25db6ba61d5c h1:uyK5aLbAhrnZtnvobJLN24gGUrlxIJAAFqiWl+liZuo= -github.com/nspcc-dev/go-ordered-json v0.0.0-20220111165707-25110be27d22 h1:n4ZaFCKt1pQJd7PXoMJabZWK9ejjbLOVrkl/lOUmshg= -github.com/nspcc-dev/go-ordered-json v0.0.0-20220111165707-25110be27d22/go.mod h1:79bEUDEviBHJMFV6Iq6in57FEOCMcRhfQnfaf0ETA5U= +github.com/nspcc-dev/go-ordered-json v0.0.0-20231123160306-3374ff1e7a3c h1:OOQeE613BH93ICPq3eke5N78gWNeMjcBWkmD2NKyXVg= +github.com/nspcc-dev/go-ordered-json v0.0.0-20231123160306-3374ff1e7a3c/go.mod h1:79bEUDEviBHJMFV6Iq6in57FEOCMcRhfQnfaf0ETA5U= github.com/nspcc-dev/hrw v1.0.9 h1:17VcAuTtrstmFppBjfRiia4K2wA/ukXZhLFS8Y8rz5Y= -github.com/nspcc-dev/neo-go v0.103.1 h1:BfRBceHUu8jSc1KQy7CzmQ/pa+xzAmgcyteGf0/IGgM= -github.com/nspcc-dev/neo-go v0.103.1/go.mod h1:MD7MPiyshUwrE5n1/LzxeandbItaa/iLW/bJb6gNs/U= -github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20231020160724-c3955f87d1b5 h1:09CpI5uwsxb1EeFPIKQRwwWlfCmDD/Dwwh01lPiQScM= -github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20231020160724-c3955f87d1b5/go.mod h1:J/Mk6+nKeKSW4wygkZQFLQ6SkLOSGX5Ga0RuuuktEag= +github.com/nspcc-dev/neo-go v0.104.0 h1:FGj3Z46yABcFIAI1SCLd1jQSoh+B00h/2VAgEgY1JKQ= +github.com/nspcc-dev/neo-go v0.104.0/go.mod h1:omsUK5PAtG2/nQ3/evs95QEg3wtkj3LH53e0NKtXVwQ= +github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20231121104256-0493ddbd70b2 h1:hPVF8iMmsQ15GSemj1ma6C9BkwfAugEXsUAVTEniK5M= +github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20231121104256-0493ddbd70b2/go.mod h1:J/Mk6+nKeKSW4wygkZQFLQ6SkLOSGX5Ga0RuuuktEag= github.com/nspcc-dev/neofs-api-go/v2 v2.14.0 h1:jhuN8Ldqz7WApvUJRFY0bjRXE1R3iCkboMX5QVZhHVk= github.com/nspcc-dev/neofs-crypto v0.4.0 h1:5LlrUAM5O0k1+sH/sktBtrgfWtq1pgpDs09fZo+KYi4= github.com/nspcc-dev/neofs-sdk-go v1.0.0-rc.11 h1:QOc8ZRN5DXlAeRPh5QG9u8rMLgoeRNiZF5/vL7QupWg= diff --git a/rpc/alphabet/rpcbinding.go b/rpc/alphabet/rpcbinding.go index a110a889..c5fe2557 100644 --- a/rpc/alphabet/rpcbinding.go +++ b/rpc/alphabet/rpcbinding.go @@ -14,7 +14,6 @@ import ( "github.com/nspcc-dev/neo-go/pkg/util" "github.com/nspcc-dev/neo-go/pkg/vm/stackitem" "math/big" - "unicode/utf8" ) // CommonBallot is a contract-specific common.Ballot type used by its methods. @@ -24,126 +23,6 @@ type CommonBallot struct { Height *big.Int } -// LedgerBlock is a contract-specific ledger.Block type used by its methods. -type LedgerBlock struct { - Hash util.Uint256 - Version *big.Int - PrevHash util.Uint256 - MerkleRoot util.Uint256 - Timestamp *big.Int - Nonce *big.Int - Index *big.Int - NextConsensus util.Uint160 - TransactionsLength *big.Int -} - -// LedgerBlockSR is a contract-specific ledger.BlockSR type used by its methods. -type LedgerBlockSR struct { - Hash util.Uint256 - Version *big.Int - PrevHash util.Uint256 - MerkleRoot util.Uint256 - Timestamp *big.Int - Nonce *big.Int - Index *big.Int - NextConsensus util.Uint160 - TransactionsLength *big.Int - PrevStateRoot util.Uint256 -} - -// LedgerTransaction is a contract-specific ledger.Transaction type used by its methods. -type LedgerTransaction struct { - Hash util.Uint256 - Version *big.Int - Nonce *big.Int - Sender util.Uint160 - SysFee *big.Int - NetFee *big.Int - ValidUntilBlock *big.Int - Script []byte -} - -// LedgerTransactionSigner is a contract-specific ledger.TransactionSigner type used by its methods. -type LedgerTransactionSigner struct { - Account util.Uint160 - Scopes *big.Int - AllowedContracts []util.Uint160 - AllowedGroups keys.PublicKeys - Rules []*LedgerWitnessRule -} - -// LedgerWitnessCondition is a contract-specific ledger.WitnessCondition type used by its methods. -type LedgerWitnessCondition struct { - Type *big.Int - Value any -} - -// LedgerWitnessRule is a contract-specific ledger.WitnessRule type used by its methods. -type LedgerWitnessRule struct { - Action *big.Int - Condition *LedgerWitnessCondition -} - -// ManagementABI is a contract-specific management.ABI type used by its methods. -type ManagementABI struct { - Methods []*ManagementMethod - Events []*ManagementEvent -} - -// ManagementContract is a contract-specific management.Contract type used by its methods. -type ManagementContract struct { - ID *big.Int - UpdateCounter *big.Int - Hash util.Uint160 - NEF []byte - Manifest *ManagementManifest -} - -// ManagementEvent is a contract-specific management.Event type used by its methods. -type ManagementEvent struct { - Name string - Params []*ManagementParameter -} - -// ManagementGroup is a contract-specific management.Group type used by its methods. -type ManagementGroup struct { - PublicKey *keys.PublicKey - Signature []byte -} - -// ManagementManifest is a contract-specific management.Manifest type used by its methods. -type ManagementManifest struct { - Name string - Groups []*ManagementGroup - Features map[string]string - SupportedStandards []string - ABI *ManagementABI - Permissions []*ManagementPermission - Trusts []util.Uint160 - Extra any -} - -// ManagementMethod is a contract-specific management.Method type used by its methods. -type ManagementMethod struct { - Name string - Params []*ManagementParameter - ReturnType *big.Int - Offset *big.Int - Safe bool -} - -// ManagementParameter is a contract-specific management.Parameter type used by its methods. -type ManagementParameter struct { - Name string - Type *big.Int -} - -// ManagementPermission is a contract-specific management.Permission type used by its methods. -type ManagementPermission struct { - Contract util.Uint160 - Methods []string -} - // Invoker is used by ContractReader to call various safe methods. type Invoker interface { Call(contract util.Uint160, operation string, params ...any) (*result.Invoke, error) @@ -338,1205 +217,3 @@ func (res *CommonBallot) FromStackItem(item stackitem.Item) error { return nil } - -// itemToLedgerBlock converts stack item into *LedgerBlock. -func itemToLedgerBlock(item stackitem.Item, err error) (*LedgerBlock, error) { - if err != nil { - return nil, err - } - var res = new(LedgerBlock) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of LedgerBlock from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *LedgerBlock) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 9 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Hash, err = func(item stackitem.Item) (util.Uint256, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint256{}, err - } - u, err := util.Uint256DecodeBytesBE(b) - if err != nil { - return util.Uint256{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Hash: %w", err) - } - - index++ - res.Version, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Version: %w", err) - } - - index++ - res.PrevHash, err = func(item stackitem.Item) (util.Uint256, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint256{}, err - } - u, err := util.Uint256DecodeBytesBE(b) - if err != nil { - return util.Uint256{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field PrevHash: %w", err) - } - - index++ - res.MerkleRoot, err = func(item stackitem.Item) (util.Uint256, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint256{}, err - } - u, err := util.Uint256DecodeBytesBE(b) - if err != nil { - return util.Uint256{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field MerkleRoot: %w", err) - } - - index++ - res.Timestamp, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Timestamp: %w", err) - } - - index++ - res.Nonce, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Nonce: %w", err) - } - - index++ - res.Index, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Index: %w", err) - } - - index++ - res.NextConsensus, err = func(item stackitem.Item) (util.Uint160, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint160{}, err - } - u, err := util.Uint160DecodeBytesBE(b) - if err != nil { - return util.Uint160{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field NextConsensus: %w", err) - } - - index++ - res.TransactionsLength, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field TransactionsLength: %w", err) - } - - return nil -} - -// itemToLedgerBlockSR converts stack item into *LedgerBlockSR. -func itemToLedgerBlockSR(item stackitem.Item, err error) (*LedgerBlockSR, error) { - if err != nil { - return nil, err - } - var res = new(LedgerBlockSR) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of LedgerBlockSR from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *LedgerBlockSR) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 10 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Hash, err = func(item stackitem.Item) (util.Uint256, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint256{}, err - } - u, err := util.Uint256DecodeBytesBE(b) - if err != nil { - return util.Uint256{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Hash: %w", err) - } - - index++ - res.Version, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Version: %w", err) - } - - index++ - res.PrevHash, err = func(item stackitem.Item) (util.Uint256, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint256{}, err - } - u, err := util.Uint256DecodeBytesBE(b) - if err != nil { - return util.Uint256{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field PrevHash: %w", err) - } - - index++ - res.MerkleRoot, err = func(item stackitem.Item) (util.Uint256, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint256{}, err - } - u, err := util.Uint256DecodeBytesBE(b) - if err != nil { - return util.Uint256{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field MerkleRoot: %w", err) - } - - index++ - res.Timestamp, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Timestamp: %w", err) - } - - index++ - res.Nonce, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Nonce: %w", err) - } - - index++ - res.Index, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Index: %w", err) - } - - index++ - res.NextConsensus, err = func(item stackitem.Item) (util.Uint160, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint160{}, err - } - u, err := util.Uint160DecodeBytesBE(b) - if err != nil { - return util.Uint160{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field NextConsensus: %w", err) - } - - index++ - res.TransactionsLength, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field TransactionsLength: %w", err) - } - - index++ - res.PrevStateRoot, err = func(item stackitem.Item) (util.Uint256, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint256{}, err - } - u, err := util.Uint256DecodeBytesBE(b) - if err != nil { - return util.Uint256{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field PrevStateRoot: %w", err) - } - - return nil -} - -// itemToLedgerTransaction converts stack item into *LedgerTransaction. -func itemToLedgerTransaction(item stackitem.Item, err error) (*LedgerTransaction, error) { - if err != nil { - return nil, err - } - var res = new(LedgerTransaction) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of LedgerTransaction from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *LedgerTransaction) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 8 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Hash, err = func(item stackitem.Item) (util.Uint256, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint256{}, err - } - u, err := util.Uint256DecodeBytesBE(b) - if err != nil { - return util.Uint256{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Hash: %w", err) - } - - index++ - res.Version, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Version: %w", err) - } - - index++ - res.Nonce, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Nonce: %w", err) - } - - index++ - res.Sender, err = func(item stackitem.Item) (util.Uint160, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint160{}, err - } - u, err := util.Uint160DecodeBytesBE(b) - if err != nil { - return util.Uint160{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Sender: %w", err) - } - - index++ - res.SysFee, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field SysFee: %w", err) - } - - index++ - res.NetFee, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field NetFee: %w", err) - } - - index++ - res.ValidUntilBlock, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field ValidUntilBlock: %w", err) - } - - index++ - res.Script, err = arr[index].TryBytes() - if err != nil { - return fmt.Errorf("field Script: %w", err) - } - - return nil -} - -// itemToLedgerTransactionSigner converts stack item into *LedgerTransactionSigner. -func itemToLedgerTransactionSigner(item stackitem.Item, err error) (*LedgerTransactionSigner, error) { - if err != nil { - return nil, err - } - var res = new(LedgerTransactionSigner) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of LedgerTransactionSigner from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *LedgerTransactionSigner) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 5 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Account, err = func(item stackitem.Item) (util.Uint160, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint160{}, err - } - u, err := util.Uint160DecodeBytesBE(b) - if err != nil { - return util.Uint160{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Account: %w", err) - } - - index++ - res.Scopes, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Scopes: %w", err) - } - - index++ - res.AllowedContracts, err = func(item stackitem.Item) ([]util.Uint160, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make([]util.Uint160, len(arr)) - for i := range res { - res[i], err = func(item stackitem.Item) (util.Uint160, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint160{}, err - } - u, err := util.Uint160DecodeBytesBE(b) - if err != nil { - return util.Uint160{}, err - } - return u, nil - }(arr[i]) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field AllowedContracts: %w", err) - } - - index++ - res.AllowedGroups, err = func(item stackitem.Item) (keys.PublicKeys, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make(keys.PublicKeys, len(arr)) - for i := range res { - res[i], err = func(item stackitem.Item) (*keys.PublicKey, error) { - b, err := item.TryBytes() - if err != nil { - return nil, err - } - k, err := keys.NewPublicKeyFromBytes(b, elliptic.P256()) - if err != nil { - return nil, err - } - return k, nil - }(arr[i]) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field AllowedGroups: %w", err) - } - - index++ - res.Rules, err = func(item stackitem.Item) ([]*LedgerWitnessRule, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make([]*LedgerWitnessRule, len(arr)) - for i := range res { - res[i], err = itemToLedgerWitnessRule(arr[i], nil) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Rules: %w", err) - } - - return nil -} - -// itemToLedgerWitnessCondition converts stack item into *LedgerWitnessCondition. -func itemToLedgerWitnessCondition(item stackitem.Item, err error) (*LedgerWitnessCondition, error) { - if err != nil { - return nil, err - } - var res = new(LedgerWitnessCondition) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of LedgerWitnessCondition from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *LedgerWitnessCondition) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 2 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Type, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Type: %w", err) - } - - index++ - res.Value, err = arr[index].Value(), error(nil) - if err != nil { - return fmt.Errorf("field Value: %w", err) - } - - return nil -} - -// itemToLedgerWitnessRule converts stack item into *LedgerWitnessRule. -func itemToLedgerWitnessRule(item stackitem.Item, err error) (*LedgerWitnessRule, error) { - if err != nil { - return nil, err - } - var res = new(LedgerWitnessRule) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of LedgerWitnessRule from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *LedgerWitnessRule) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 2 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Action, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Action: %w", err) - } - - index++ - res.Condition, err = itemToLedgerWitnessCondition(arr[index], nil) - if err != nil { - return fmt.Errorf("field Condition: %w", err) - } - - return nil -} - -// itemToManagementABI converts stack item into *ManagementABI. -func itemToManagementABI(item stackitem.Item, err error) (*ManagementABI, error) { - if err != nil { - return nil, err - } - var res = new(ManagementABI) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of ManagementABI from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *ManagementABI) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 2 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Methods, err = func(item stackitem.Item) ([]*ManagementMethod, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make([]*ManagementMethod, len(arr)) - for i := range res { - res[i], err = itemToManagementMethod(arr[i], nil) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Methods: %w", err) - } - - index++ - res.Events, err = func(item stackitem.Item) ([]*ManagementEvent, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make([]*ManagementEvent, len(arr)) - for i := range res { - res[i], err = itemToManagementEvent(arr[i], nil) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Events: %w", err) - } - - return nil -} - -// itemToManagementContract converts stack item into *ManagementContract. -func itemToManagementContract(item stackitem.Item, err error) (*ManagementContract, error) { - if err != nil { - return nil, err - } - var res = new(ManagementContract) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of ManagementContract from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *ManagementContract) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 5 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.ID, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field ID: %w", err) - } - - index++ - res.UpdateCounter, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field UpdateCounter: %w", err) - } - - index++ - res.Hash, err = func(item stackitem.Item) (util.Uint160, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint160{}, err - } - u, err := util.Uint160DecodeBytesBE(b) - if err != nil { - return util.Uint160{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Hash: %w", err) - } - - index++ - res.NEF, err = arr[index].TryBytes() - if err != nil { - return fmt.Errorf("field NEF: %w", err) - } - - index++ - res.Manifest, err = itemToManagementManifest(arr[index], nil) - if err != nil { - return fmt.Errorf("field Manifest: %w", err) - } - - return nil -} - -// itemToManagementEvent converts stack item into *ManagementEvent. -func itemToManagementEvent(item stackitem.Item, err error) (*ManagementEvent, error) { - if err != nil { - return nil, err - } - var res = new(ManagementEvent) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of ManagementEvent from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *ManagementEvent) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 2 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Name, err = func(item stackitem.Item) (string, error) { - b, err := item.TryBytes() - if err != nil { - return "", err - } - if !utf8.Valid(b) { - return "", errors.New("not a UTF-8 string") - } - return string(b), nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Name: %w", err) - } - - index++ - res.Params, err = func(item stackitem.Item) ([]*ManagementParameter, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make([]*ManagementParameter, len(arr)) - for i := range res { - res[i], err = itemToManagementParameter(arr[i], nil) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Params: %w", err) - } - - return nil -} - -// itemToManagementGroup converts stack item into *ManagementGroup. -func itemToManagementGroup(item stackitem.Item, err error) (*ManagementGroup, error) { - if err != nil { - return nil, err - } - var res = new(ManagementGroup) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of ManagementGroup from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *ManagementGroup) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 2 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.PublicKey, err = func(item stackitem.Item) (*keys.PublicKey, error) { - b, err := item.TryBytes() - if err != nil { - return nil, err - } - k, err := keys.NewPublicKeyFromBytes(b, elliptic.P256()) - if err != nil { - return nil, err - } - return k, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field PublicKey: %w", err) - } - - index++ - res.Signature, err = arr[index].TryBytes() - if err != nil { - return fmt.Errorf("field Signature: %w", err) - } - - return nil -} - -// itemToManagementManifest converts stack item into *ManagementManifest. -func itemToManagementManifest(item stackitem.Item, err error) (*ManagementManifest, error) { - if err != nil { - return nil, err - } - var res = new(ManagementManifest) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of ManagementManifest from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *ManagementManifest) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 8 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Name, err = func(item stackitem.Item) (string, error) { - b, err := item.TryBytes() - if err != nil { - return "", err - } - if !utf8.Valid(b) { - return "", errors.New("not a UTF-8 string") - } - return string(b), nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Name: %w", err) - } - - index++ - res.Groups, err = func(item stackitem.Item) ([]*ManagementGroup, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make([]*ManagementGroup, len(arr)) - for i := range res { - res[i], err = itemToManagementGroup(arr[i], nil) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Groups: %w", err) - } - - index++ - res.Features, err = func(item stackitem.Item) (map[string]string, error) { - m, ok := item.Value().([]stackitem.MapElement) - if !ok { - return nil, fmt.Errorf("%s is not a map", item.Type().String()) - } - res := make(map[string]string) - for i := range m { - k, err := func(item stackitem.Item) (string, error) { - b, err := item.TryBytes() - if err != nil { - return "", err - } - if !utf8.Valid(b) { - return "", errors.New("not a UTF-8 string") - } - return string(b), nil - }(m[i].Key) - if err != nil { - return nil, fmt.Errorf("key %d: %w", i, err) - } - v, err := func(item stackitem.Item) (string, error) { - b, err := item.TryBytes() - if err != nil { - return "", err - } - if !utf8.Valid(b) { - return "", errors.New("not a UTF-8 string") - } - return string(b), nil - }(m[i].Value) - if err != nil { - return nil, fmt.Errorf("value %d: %w", i, err) - } - res[k] = v - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Features: %w", err) - } - - index++ - res.SupportedStandards, err = func(item stackitem.Item) ([]string, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make([]string, len(arr)) - for i := range res { - res[i], err = func(item stackitem.Item) (string, error) { - b, err := item.TryBytes() - if err != nil { - return "", err - } - if !utf8.Valid(b) { - return "", errors.New("not a UTF-8 string") - } - return string(b), nil - }(arr[i]) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field SupportedStandards: %w", err) - } - - index++ - res.ABI, err = itemToManagementABI(arr[index], nil) - if err != nil { - return fmt.Errorf("field ABI: %w", err) - } - - index++ - res.Permissions, err = func(item stackitem.Item) ([]*ManagementPermission, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make([]*ManagementPermission, len(arr)) - for i := range res { - res[i], err = itemToManagementPermission(arr[i], nil) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Permissions: %w", err) - } - - index++ - res.Trusts, err = func(item stackitem.Item) ([]util.Uint160, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make([]util.Uint160, len(arr)) - for i := range res { - res[i], err = func(item stackitem.Item) (util.Uint160, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint160{}, err - } - u, err := util.Uint160DecodeBytesBE(b) - if err != nil { - return util.Uint160{}, err - } - return u, nil - }(arr[i]) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Trusts: %w", err) - } - - index++ - res.Extra, err = arr[index].Value(), error(nil) - if err != nil { - return fmt.Errorf("field Extra: %w", err) - } - - return nil -} - -// itemToManagementMethod converts stack item into *ManagementMethod. -func itemToManagementMethod(item stackitem.Item, err error) (*ManagementMethod, error) { - if err != nil { - return nil, err - } - var res = new(ManagementMethod) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of ManagementMethod from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *ManagementMethod) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 5 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Name, err = func(item stackitem.Item) (string, error) { - b, err := item.TryBytes() - if err != nil { - return "", err - } - if !utf8.Valid(b) { - return "", errors.New("not a UTF-8 string") - } - return string(b), nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Name: %w", err) - } - - index++ - res.Params, err = func(item stackitem.Item) ([]*ManagementParameter, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make([]*ManagementParameter, len(arr)) - for i := range res { - res[i], err = itemToManagementParameter(arr[i], nil) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Params: %w", err) - } - - index++ - res.ReturnType, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field ReturnType: %w", err) - } - - index++ - res.Offset, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Offset: %w", err) - } - - index++ - res.Safe, err = arr[index].TryBool() - if err != nil { - return fmt.Errorf("field Safe: %w", err) - } - - return nil -} - -// itemToManagementParameter converts stack item into *ManagementParameter. -func itemToManagementParameter(item stackitem.Item, err error) (*ManagementParameter, error) { - if err != nil { - return nil, err - } - var res = new(ManagementParameter) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of ManagementParameter from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *ManagementParameter) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 2 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Name, err = func(item stackitem.Item) (string, error) { - b, err := item.TryBytes() - if err != nil { - return "", err - } - if !utf8.Valid(b) { - return "", errors.New("not a UTF-8 string") - } - return string(b), nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Name: %w", err) - } - - index++ - res.Type, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Type: %w", err) - } - - return nil -} - -// itemToManagementPermission converts stack item into *ManagementPermission. -func itemToManagementPermission(item stackitem.Item, err error) (*ManagementPermission, error) { - if err != nil { - return nil, err - } - var res = new(ManagementPermission) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of ManagementPermission from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *ManagementPermission) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 2 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Contract, err = func(item stackitem.Item) (util.Uint160, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint160{}, err - } - u, err := util.Uint160DecodeBytesBE(b) - if err != nil { - return util.Uint160{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Contract: %w", err) - } - - index++ - res.Methods, err = func(item stackitem.Item) ([]string, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make([]string, len(arr)) - for i := range res { - res[i], err = func(item stackitem.Item) (string, error) { - b, err := item.TryBytes() - if err != nil { - return "", err - } - if !utf8.Valid(b) { - return "", errors.New("not a UTF-8 string") - } - return string(b), nil - }(arr[i]) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Methods: %w", err) - } - - return nil -} diff --git a/rpc/audit/rpcbinding.go b/rpc/audit/rpcbinding.go index d313338e..7bf1451e 100644 --- a/rpc/audit/rpcbinding.go +++ b/rpc/audit/rpcbinding.go @@ -14,7 +14,6 @@ import ( "github.com/nspcc-dev/neo-go/pkg/util" "github.com/nspcc-dev/neo-go/pkg/vm/stackitem" "math/big" - "unicode/utf8" ) // AuditAuditHeader is a contract-specific audit.AuditHeader type used by its methods. @@ -24,133 +23,6 @@ type AuditAuditHeader struct { From *keys.PublicKey } -// CommonBallot is a contract-specific common.Ballot type used by its methods. -type CommonBallot struct { - ID []byte - Voters keys.PublicKeys - Height *big.Int -} - -// LedgerBlock is a contract-specific ledger.Block type used by its methods. -type LedgerBlock struct { - Hash util.Uint256 - Version *big.Int - PrevHash util.Uint256 - MerkleRoot util.Uint256 - Timestamp *big.Int - Nonce *big.Int - Index *big.Int - NextConsensus util.Uint160 - TransactionsLength *big.Int -} - -// LedgerBlockSR is a contract-specific ledger.BlockSR type used by its methods. -type LedgerBlockSR struct { - Hash util.Uint256 - Version *big.Int - PrevHash util.Uint256 - MerkleRoot util.Uint256 - Timestamp *big.Int - Nonce *big.Int - Index *big.Int - NextConsensus util.Uint160 - TransactionsLength *big.Int - PrevStateRoot util.Uint256 -} - -// LedgerTransaction is a contract-specific ledger.Transaction type used by its methods. -type LedgerTransaction struct { - Hash util.Uint256 - Version *big.Int - Nonce *big.Int - Sender util.Uint160 - SysFee *big.Int - NetFee *big.Int - ValidUntilBlock *big.Int - Script []byte -} - -// LedgerTransactionSigner is a contract-specific ledger.TransactionSigner type used by its methods. -type LedgerTransactionSigner struct { - Account util.Uint160 - Scopes *big.Int - AllowedContracts []util.Uint160 - AllowedGroups keys.PublicKeys - Rules []*LedgerWitnessRule -} - -// LedgerWitnessCondition is a contract-specific ledger.WitnessCondition type used by its methods. -type LedgerWitnessCondition struct { - Type *big.Int - Value any -} - -// LedgerWitnessRule is a contract-specific ledger.WitnessRule type used by its methods. -type LedgerWitnessRule struct { - Action *big.Int - Condition *LedgerWitnessCondition -} - -// ManagementABI is a contract-specific management.ABI type used by its methods. -type ManagementABI struct { - Methods []*ManagementMethod - Events []*ManagementEvent -} - -// ManagementContract is a contract-specific management.Contract type used by its methods. -type ManagementContract struct { - ID *big.Int - UpdateCounter *big.Int - Hash util.Uint160 - NEF []byte - Manifest *ManagementManifest -} - -// ManagementEvent is a contract-specific management.Event type used by its methods. -type ManagementEvent struct { - Name string - Params []*ManagementParameter -} - -// ManagementGroup is a contract-specific management.Group type used by its methods. -type ManagementGroup struct { - PublicKey *keys.PublicKey - Signature []byte -} - -// ManagementManifest is a contract-specific management.Manifest type used by its methods. -type ManagementManifest struct { - Name string - Groups []*ManagementGroup - Features map[string]string - SupportedStandards []string - ABI *ManagementABI - Permissions []*ManagementPermission - Trusts []util.Uint160 - Extra any -} - -// ManagementMethod is a contract-specific management.Method type used by its methods. -type ManagementMethod struct { - Name string - Params []*ManagementParameter - ReturnType *big.Int - Offset *big.Int - Safe bool -} - -// ManagementParameter is a contract-specific management.Parameter type used by its methods. -type ManagementParameter struct { - Name string - Type *big.Int -} - -// ManagementPermission is a contract-specific management.Permission type used by its methods. -type ManagementPermission struct { - Contract util.Uint160 - Methods []string -} - // Invoker is used by ContractReader to call various safe methods. type Invoker interface { Call(contract util.Uint160, operation string, params ...any) (*result.Invoke, error) @@ -320,1274 +192,3 @@ func (res *AuditAuditHeader) FromStackItem(item stackitem.Item) error { return nil } - -// itemToCommonBallot converts stack item into *CommonBallot. -func itemToCommonBallot(item stackitem.Item, err error) (*CommonBallot, error) { - if err != nil { - return nil, err - } - var res = new(CommonBallot) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of CommonBallot from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *CommonBallot) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 3 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.ID, err = arr[index].TryBytes() - if err != nil { - return fmt.Errorf("field ID: %w", err) - } - - index++ - res.Voters, err = func(item stackitem.Item) (keys.PublicKeys, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make(keys.PublicKeys, len(arr)) - for i := range res { - res[i], err = func(item stackitem.Item) (*keys.PublicKey, error) { - b, err := item.TryBytes() - if err != nil { - return nil, err - } - k, err := keys.NewPublicKeyFromBytes(b, elliptic.P256()) - if err != nil { - return nil, err - } - return k, nil - }(arr[i]) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Voters: %w", err) - } - - index++ - res.Height, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Height: %w", err) - } - - return nil -} - -// itemToLedgerBlock converts stack item into *LedgerBlock. -func itemToLedgerBlock(item stackitem.Item, err error) (*LedgerBlock, error) { - if err != nil { - return nil, err - } - var res = new(LedgerBlock) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of LedgerBlock from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *LedgerBlock) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 9 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Hash, err = func(item stackitem.Item) (util.Uint256, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint256{}, err - } - u, err := util.Uint256DecodeBytesBE(b) - if err != nil { - return util.Uint256{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Hash: %w", err) - } - - index++ - res.Version, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Version: %w", err) - } - - index++ - res.PrevHash, err = func(item stackitem.Item) (util.Uint256, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint256{}, err - } - u, err := util.Uint256DecodeBytesBE(b) - if err != nil { - return util.Uint256{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field PrevHash: %w", err) - } - - index++ - res.MerkleRoot, err = func(item stackitem.Item) (util.Uint256, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint256{}, err - } - u, err := util.Uint256DecodeBytesBE(b) - if err != nil { - return util.Uint256{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field MerkleRoot: %w", err) - } - - index++ - res.Timestamp, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Timestamp: %w", err) - } - - index++ - res.Nonce, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Nonce: %w", err) - } - - index++ - res.Index, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Index: %w", err) - } - - index++ - res.NextConsensus, err = func(item stackitem.Item) (util.Uint160, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint160{}, err - } - u, err := util.Uint160DecodeBytesBE(b) - if err != nil { - return util.Uint160{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field NextConsensus: %w", err) - } - - index++ - res.TransactionsLength, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field TransactionsLength: %w", err) - } - - return nil -} - -// itemToLedgerBlockSR converts stack item into *LedgerBlockSR. -func itemToLedgerBlockSR(item stackitem.Item, err error) (*LedgerBlockSR, error) { - if err != nil { - return nil, err - } - var res = new(LedgerBlockSR) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of LedgerBlockSR from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *LedgerBlockSR) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 10 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Hash, err = func(item stackitem.Item) (util.Uint256, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint256{}, err - } - u, err := util.Uint256DecodeBytesBE(b) - if err != nil { - return util.Uint256{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Hash: %w", err) - } - - index++ - res.Version, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Version: %w", err) - } - - index++ - res.PrevHash, err = func(item stackitem.Item) (util.Uint256, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint256{}, err - } - u, err := util.Uint256DecodeBytesBE(b) - if err != nil { - return util.Uint256{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field PrevHash: %w", err) - } - - index++ - res.MerkleRoot, err = func(item stackitem.Item) (util.Uint256, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint256{}, err - } - u, err := util.Uint256DecodeBytesBE(b) - if err != nil { - return util.Uint256{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field MerkleRoot: %w", err) - } - - index++ - res.Timestamp, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Timestamp: %w", err) - } - - index++ - res.Nonce, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Nonce: %w", err) - } - - index++ - res.Index, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Index: %w", err) - } - - index++ - res.NextConsensus, err = func(item stackitem.Item) (util.Uint160, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint160{}, err - } - u, err := util.Uint160DecodeBytesBE(b) - if err != nil { - return util.Uint160{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field NextConsensus: %w", err) - } - - index++ - res.TransactionsLength, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field TransactionsLength: %w", err) - } - - index++ - res.PrevStateRoot, err = func(item stackitem.Item) (util.Uint256, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint256{}, err - } - u, err := util.Uint256DecodeBytesBE(b) - if err != nil { - return util.Uint256{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field PrevStateRoot: %w", err) - } - - return nil -} - -// itemToLedgerTransaction converts stack item into *LedgerTransaction. -func itemToLedgerTransaction(item stackitem.Item, err error) (*LedgerTransaction, error) { - if err != nil { - return nil, err - } - var res = new(LedgerTransaction) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of LedgerTransaction from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *LedgerTransaction) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 8 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Hash, err = func(item stackitem.Item) (util.Uint256, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint256{}, err - } - u, err := util.Uint256DecodeBytesBE(b) - if err != nil { - return util.Uint256{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Hash: %w", err) - } - - index++ - res.Version, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Version: %w", err) - } - - index++ - res.Nonce, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Nonce: %w", err) - } - - index++ - res.Sender, err = func(item stackitem.Item) (util.Uint160, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint160{}, err - } - u, err := util.Uint160DecodeBytesBE(b) - if err != nil { - return util.Uint160{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Sender: %w", err) - } - - index++ - res.SysFee, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field SysFee: %w", err) - } - - index++ - res.NetFee, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field NetFee: %w", err) - } - - index++ - res.ValidUntilBlock, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field ValidUntilBlock: %w", err) - } - - index++ - res.Script, err = arr[index].TryBytes() - if err != nil { - return fmt.Errorf("field Script: %w", err) - } - - return nil -} - -// itemToLedgerTransactionSigner converts stack item into *LedgerTransactionSigner. -func itemToLedgerTransactionSigner(item stackitem.Item, err error) (*LedgerTransactionSigner, error) { - if err != nil { - return nil, err - } - var res = new(LedgerTransactionSigner) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of LedgerTransactionSigner from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *LedgerTransactionSigner) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 5 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Account, err = func(item stackitem.Item) (util.Uint160, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint160{}, err - } - u, err := util.Uint160DecodeBytesBE(b) - if err != nil { - return util.Uint160{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Account: %w", err) - } - - index++ - res.Scopes, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Scopes: %w", err) - } - - index++ - res.AllowedContracts, err = func(item stackitem.Item) ([]util.Uint160, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make([]util.Uint160, len(arr)) - for i := range res { - res[i], err = func(item stackitem.Item) (util.Uint160, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint160{}, err - } - u, err := util.Uint160DecodeBytesBE(b) - if err != nil { - return util.Uint160{}, err - } - return u, nil - }(arr[i]) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field AllowedContracts: %w", err) - } - - index++ - res.AllowedGroups, err = func(item stackitem.Item) (keys.PublicKeys, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make(keys.PublicKeys, len(arr)) - for i := range res { - res[i], err = func(item stackitem.Item) (*keys.PublicKey, error) { - b, err := item.TryBytes() - if err != nil { - return nil, err - } - k, err := keys.NewPublicKeyFromBytes(b, elliptic.P256()) - if err != nil { - return nil, err - } - return k, nil - }(arr[i]) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field AllowedGroups: %w", err) - } - - index++ - res.Rules, err = func(item stackitem.Item) ([]*LedgerWitnessRule, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make([]*LedgerWitnessRule, len(arr)) - for i := range res { - res[i], err = itemToLedgerWitnessRule(arr[i], nil) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Rules: %w", err) - } - - return nil -} - -// itemToLedgerWitnessCondition converts stack item into *LedgerWitnessCondition. -func itemToLedgerWitnessCondition(item stackitem.Item, err error) (*LedgerWitnessCondition, error) { - if err != nil { - return nil, err - } - var res = new(LedgerWitnessCondition) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of LedgerWitnessCondition from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *LedgerWitnessCondition) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 2 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Type, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Type: %w", err) - } - - index++ - res.Value, err = arr[index].Value(), error(nil) - if err != nil { - return fmt.Errorf("field Value: %w", err) - } - - return nil -} - -// itemToLedgerWitnessRule converts stack item into *LedgerWitnessRule. -func itemToLedgerWitnessRule(item stackitem.Item, err error) (*LedgerWitnessRule, error) { - if err != nil { - return nil, err - } - var res = new(LedgerWitnessRule) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of LedgerWitnessRule from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *LedgerWitnessRule) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 2 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Action, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Action: %w", err) - } - - index++ - res.Condition, err = itemToLedgerWitnessCondition(arr[index], nil) - if err != nil { - return fmt.Errorf("field Condition: %w", err) - } - - return nil -} - -// itemToManagementABI converts stack item into *ManagementABI. -func itemToManagementABI(item stackitem.Item, err error) (*ManagementABI, error) { - if err != nil { - return nil, err - } - var res = new(ManagementABI) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of ManagementABI from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *ManagementABI) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 2 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Methods, err = func(item stackitem.Item) ([]*ManagementMethod, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make([]*ManagementMethod, len(arr)) - for i := range res { - res[i], err = itemToManagementMethod(arr[i], nil) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Methods: %w", err) - } - - index++ - res.Events, err = func(item stackitem.Item) ([]*ManagementEvent, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make([]*ManagementEvent, len(arr)) - for i := range res { - res[i], err = itemToManagementEvent(arr[i], nil) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Events: %w", err) - } - - return nil -} - -// itemToManagementContract converts stack item into *ManagementContract. -func itemToManagementContract(item stackitem.Item, err error) (*ManagementContract, error) { - if err != nil { - return nil, err - } - var res = new(ManagementContract) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of ManagementContract from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *ManagementContract) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 5 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.ID, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field ID: %w", err) - } - - index++ - res.UpdateCounter, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field UpdateCounter: %w", err) - } - - index++ - res.Hash, err = func(item stackitem.Item) (util.Uint160, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint160{}, err - } - u, err := util.Uint160DecodeBytesBE(b) - if err != nil { - return util.Uint160{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Hash: %w", err) - } - - index++ - res.NEF, err = arr[index].TryBytes() - if err != nil { - return fmt.Errorf("field NEF: %w", err) - } - - index++ - res.Manifest, err = itemToManagementManifest(arr[index], nil) - if err != nil { - return fmt.Errorf("field Manifest: %w", err) - } - - return nil -} - -// itemToManagementEvent converts stack item into *ManagementEvent. -func itemToManagementEvent(item stackitem.Item, err error) (*ManagementEvent, error) { - if err != nil { - return nil, err - } - var res = new(ManagementEvent) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of ManagementEvent from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *ManagementEvent) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 2 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Name, err = func(item stackitem.Item) (string, error) { - b, err := item.TryBytes() - if err != nil { - return "", err - } - if !utf8.Valid(b) { - return "", errors.New("not a UTF-8 string") - } - return string(b), nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Name: %w", err) - } - - index++ - res.Params, err = func(item stackitem.Item) ([]*ManagementParameter, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make([]*ManagementParameter, len(arr)) - for i := range res { - res[i], err = itemToManagementParameter(arr[i], nil) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Params: %w", err) - } - - return nil -} - -// itemToManagementGroup converts stack item into *ManagementGroup. -func itemToManagementGroup(item stackitem.Item, err error) (*ManagementGroup, error) { - if err != nil { - return nil, err - } - var res = new(ManagementGroup) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of ManagementGroup from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *ManagementGroup) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 2 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.PublicKey, err = func(item stackitem.Item) (*keys.PublicKey, error) { - b, err := item.TryBytes() - if err != nil { - return nil, err - } - k, err := keys.NewPublicKeyFromBytes(b, elliptic.P256()) - if err != nil { - return nil, err - } - return k, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field PublicKey: %w", err) - } - - index++ - res.Signature, err = arr[index].TryBytes() - if err != nil { - return fmt.Errorf("field Signature: %w", err) - } - - return nil -} - -// itemToManagementManifest converts stack item into *ManagementManifest. -func itemToManagementManifest(item stackitem.Item, err error) (*ManagementManifest, error) { - if err != nil { - return nil, err - } - var res = new(ManagementManifest) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of ManagementManifest from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *ManagementManifest) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 8 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Name, err = func(item stackitem.Item) (string, error) { - b, err := item.TryBytes() - if err != nil { - return "", err - } - if !utf8.Valid(b) { - return "", errors.New("not a UTF-8 string") - } - return string(b), nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Name: %w", err) - } - - index++ - res.Groups, err = func(item stackitem.Item) ([]*ManagementGroup, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make([]*ManagementGroup, len(arr)) - for i := range res { - res[i], err = itemToManagementGroup(arr[i], nil) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Groups: %w", err) - } - - index++ - res.Features, err = func(item stackitem.Item) (map[string]string, error) { - m, ok := item.Value().([]stackitem.MapElement) - if !ok { - return nil, fmt.Errorf("%s is not a map", item.Type().String()) - } - res := make(map[string]string) - for i := range m { - k, err := func(item stackitem.Item) (string, error) { - b, err := item.TryBytes() - if err != nil { - return "", err - } - if !utf8.Valid(b) { - return "", errors.New("not a UTF-8 string") - } - return string(b), nil - }(m[i].Key) - if err != nil { - return nil, fmt.Errorf("key %d: %w", i, err) - } - v, err := func(item stackitem.Item) (string, error) { - b, err := item.TryBytes() - if err != nil { - return "", err - } - if !utf8.Valid(b) { - return "", errors.New("not a UTF-8 string") - } - return string(b), nil - }(m[i].Value) - if err != nil { - return nil, fmt.Errorf("value %d: %w", i, err) - } - res[k] = v - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Features: %w", err) - } - - index++ - res.SupportedStandards, err = func(item stackitem.Item) ([]string, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make([]string, len(arr)) - for i := range res { - res[i], err = func(item stackitem.Item) (string, error) { - b, err := item.TryBytes() - if err != nil { - return "", err - } - if !utf8.Valid(b) { - return "", errors.New("not a UTF-8 string") - } - return string(b), nil - }(arr[i]) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field SupportedStandards: %w", err) - } - - index++ - res.ABI, err = itemToManagementABI(arr[index], nil) - if err != nil { - return fmt.Errorf("field ABI: %w", err) - } - - index++ - res.Permissions, err = func(item stackitem.Item) ([]*ManagementPermission, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make([]*ManagementPermission, len(arr)) - for i := range res { - res[i], err = itemToManagementPermission(arr[i], nil) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Permissions: %w", err) - } - - index++ - res.Trusts, err = func(item stackitem.Item) ([]util.Uint160, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make([]util.Uint160, len(arr)) - for i := range res { - res[i], err = func(item stackitem.Item) (util.Uint160, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint160{}, err - } - u, err := util.Uint160DecodeBytesBE(b) - if err != nil { - return util.Uint160{}, err - } - return u, nil - }(arr[i]) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Trusts: %w", err) - } - - index++ - res.Extra, err = arr[index].Value(), error(nil) - if err != nil { - return fmt.Errorf("field Extra: %w", err) - } - - return nil -} - -// itemToManagementMethod converts stack item into *ManagementMethod. -func itemToManagementMethod(item stackitem.Item, err error) (*ManagementMethod, error) { - if err != nil { - return nil, err - } - var res = new(ManagementMethod) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of ManagementMethod from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *ManagementMethod) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 5 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Name, err = func(item stackitem.Item) (string, error) { - b, err := item.TryBytes() - if err != nil { - return "", err - } - if !utf8.Valid(b) { - return "", errors.New("not a UTF-8 string") - } - return string(b), nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Name: %w", err) - } - - index++ - res.Params, err = func(item stackitem.Item) ([]*ManagementParameter, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make([]*ManagementParameter, len(arr)) - for i := range res { - res[i], err = itemToManagementParameter(arr[i], nil) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Params: %w", err) - } - - index++ - res.ReturnType, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field ReturnType: %w", err) - } - - index++ - res.Offset, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Offset: %w", err) - } - - index++ - res.Safe, err = arr[index].TryBool() - if err != nil { - return fmt.Errorf("field Safe: %w", err) - } - - return nil -} - -// itemToManagementParameter converts stack item into *ManagementParameter. -func itemToManagementParameter(item stackitem.Item, err error) (*ManagementParameter, error) { - if err != nil { - return nil, err - } - var res = new(ManagementParameter) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of ManagementParameter from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *ManagementParameter) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 2 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Name, err = func(item stackitem.Item) (string, error) { - b, err := item.TryBytes() - if err != nil { - return "", err - } - if !utf8.Valid(b) { - return "", errors.New("not a UTF-8 string") - } - return string(b), nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Name: %w", err) - } - - index++ - res.Type, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Type: %w", err) - } - - return nil -} - -// itemToManagementPermission converts stack item into *ManagementPermission. -func itemToManagementPermission(item stackitem.Item, err error) (*ManagementPermission, error) { - if err != nil { - return nil, err - } - var res = new(ManagementPermission) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of ManagementPermission from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *ManagementPermission) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 2 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Contract, err = func(item stackitem.Item) (util.Uint160, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint160{}, err - } - u, err := util.Uint160DecodeBytesBE(b) - if err != nil { - return util.Uint160{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Contract: %w", err) - } - - index++ - res.Methods, err = func(item stackitem.Item) ([]string, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make([]string, len(arr)) - for i := range res { - res[i], err = func(item stackitem.Item) (string, error) { - b, err := item.TryBytes() - if err != nil { - return "", err - } - if !utf8.Valid(b) { - return "", errors.New("not a UTF-8 string") - } - return string(b), nil - }(arr[i]) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Methods: %w", err) - } - - return nil -} diff --git a/rpc/balance/rpcbinding.go b/rpc/balance/rpcbinding.go index 9f17716f..cbae0f45 100644 --- a/rpc/balance/rpcbinding.go +++ b/rpc/balance/rpcbinding.go @@ -39,126 +39,6 @@ type CommonBallot struct { Height *big.Int } -// LedgerBlock is a contract-specific ledger.Block type used by its methods. -type LedgerBlock struct { - Hash util.Uint256 - Version *big.Int - PrevHash util.Uint256 - MerkleRoot util.Uint256 - Timestamp *big.Int - Nonce *big.Int - Index *big.Int - NextConsensus util.Uint160 - TransactionsLength *big.Int -} - -// LedgerBlockSR is a contract-specific ledger.BlockSR type used by its methods. -type LedgerBlockSR struct { - Hash util.Uint256 - Version *big.Int - PrevHash util.Uint256 - MerkleRoot util.Uint256 - Timestamp *big.Int - Nonce *big.Int - Index *big.Int - NextConsensus util.Uint160 - TransactionsLength *big.Int - PrevStateRoot util.Uint256 -} - -// LedgerTransaction is a contract-specific ledger.Transaction type used by its methods. -type LedgerTransaction struct { - Hash util.Uint256 - Version *big.Int - Nonce *big.Int - Sender util.Uint160 - SysFee *big.Int - NetFee *big.Int - ValidUntilBlock *big.Int - Script []byte -} - -// LedgerTransactionSigner is a contract-specific ledger.TransactionSigner type used by its methods. -type LedgerTransactionSigner struct { - Account util.Uint160 - Scopes *big.Int - AllowedContracts []util.Uint160 - AllowedGroups keys.PublicKeys - Rules []*LedgerWitnessRule -} - -// LedgerWitnessCondition is a contract-specific ledger.WitnessCondition type used by its methods. -type LedgerWitnessCondition struct { - Type *big.Int - Value any -} - -// LedgerWitnessRule is a contract-specific ledger.WitnessRule type used by its methods. -type LedgerWitnessRule struct { - Action *big.Int - Condition *LedgerWitnessCondition -} - -// ManagementABI is a contract-specific management.ABI type used by its methods. -type ManagementABI struct { - Methods []*ManagementMethod - Events []*ManagementEvent -} - -// ManagementContract is a contract-specific management.Contract type used by its methods. -type ManagementContract struct { - ID *big.Int - UpdateCounter *big.Int - Hash util.Uint160 - NEF []byte - Manifest *ManagementManifest -} - -// ManagementEvent is a contract-specific management.Event type used by its methods. -type ManagementEvent struct { - Name string - Params []*ManagementParameter -} - -// ManagementGroup is a contract-specific management.Group type used by its methods. -type ManagementGroup struct { - PublicKey *keys.PublicKey - Signature []byte -} - -// ManagementManifest is a contract-specific management.Manifest type used by its methods. -type ManagementManifest struct { - Name string - Groups []*ManagementGroup - Features map[string]string - SupportedStandards []string - ABI *ManagementABI - Permissions []*ManagementPermission - Trusts []util.Uint160 - Extra any -} - -// ManagementMethod is a contract-specific management.Method type used by its methods. -type ManagementMethod struct { - Name string - Params []*ManagementParameter - ReturnType *big.Int - Offset *big.Int - Safe bool -} - -// ManagementParameter is a contract-specific management.Parameter type used by its methods. -type ManagementParameter struct { - Name string - Type *big.Int -} - -// ManagementPermission is a contract-specific management.Permission type used by its methods. -type ManagementPermission struct { - Contract util.Uint160 - Methods []string -} - // LockEvent represents "Lock" event emitted by the contract. type LockEvent struct { TxID []byte @@ -537,1208 +417,6 @@ func (res *CommonBallot) FromStackItem(item stackitem.Item) error { return nil } -// itemToLedgerBlock converts stack item into *LedgerBlock. -func itemToLedgerBlock(item stackitem.Item, err error) (*LedgerBlock, error) { - if err != nil { - return nil, err - } - var res = new(LedgerBlock) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of LedgerBlock from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *LedgerBlock) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 9 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Hash, err = func(item stackitem.Item) (util.Uint256, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint256{}, err - } - u, err := util.Uint256DecodeBytesBE(b) - if err != nil { - return util.Uint256{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Hash: %w", err) - } - - index++ - res.Version, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Version: %w", err) - } - - index++ - res.PrevHash, err = func(item stackitem.Item) (util.Uint256, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint256{}, err - } - u, err := util.Uint256DecodeBytesBE(b) - if err != nil { - return util.Uint256{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field PrevHash: %w", err) - } - - index++ - res.MerkleRoot, err = func(item stackitem.Item) (util.Uint256, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint256{}, err - } - u, err := util.Uint256DecodeBytesBE(b) - if err != nil { - return util.Uint256{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field MerkleRoot: %w", err) - } - - index++ - res.Timestamp, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Timestamp: %w", err) - } - - index++ - res.Nonce, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Nonce: %w", err) - } - - index++ - res.Index, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Index: %w", err) - } - - index++ - res.NextConsensus, err = func(item stackitem.Item) (util.Uint160, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint160{}, err - } - u, err := util.Uint160DecodeBytesBE(b) - if err != nil { - return util.Uint160{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field NextConsensus: %w", err) - } - - index++ - res.TransactionsLength, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field TransactionsLength: %w", err) - } - - return nil -} - -// itemToLedgerBlockSR converts stack item into *LedgerBlockSR. -func itemToLedgerBlockSR(item stackitem.Item, err error) (*LedgerBlockSR, error) { - if err != nil { - return nil, err - } - var res = new(LedgerBlockSR) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of LedgerBlockSR from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *LedgerBlockSR) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 10 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Hash, err = func(item stackitem.Item) (util.Uint256, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint256{}, err - } - u, err := util.Uint256DecodeBytesBE(b) - if err != nil { - return util.Uint256{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Hash: %w", err) - } - - index++ - res.Version, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Version: %w", err) - } - - index++ - res.PrevHash, err = func(item stackitem.Item) (util.Uint256, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint256{}, err - } - u, err := util.Uint256DecodeBytesBE(b) - if err != nil { - return util.Uint256{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field PrevHash: %w", err) - } - - index++ - res.MerkleRoot, err = func(item stackitem.Item) (util.Uint256, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint256{}, err - } - u, err := util.Uint256DecodeBytesBE(b) - if err != nil { - return util.Uint256{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field MerkleRoot: %w", err) - } - - index++ - res.Timestamp, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Timestamp: %w", err) - } - - index++ - res.Nonce, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Nonce: %w", err) - } - - index++ - res.Index, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Index: %w", err) - } - - index++ - res.NextConsensus, err = func(item stackitem.Item) (util.Uint160, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint160{}, err - } - u, err := util.Uint160DecodeBytesBE(b) - if err != nil { - return util.Uint160{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field NextConsensus: %w", err) - } - - index++ - res.TransactionsLength, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field TransactionsLength: %w", err) - } - - index++ - res.PrevStateRoot, err = func(item stackitem.Item) (util.Uint256, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint256{}, err - } - u, err := util.Uint256DecodeBytesBE(b) - if err != nil { - return util.Uint256{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field PrevStateRoot: %w", err) - } - - return nil -} - -// itemToLedgerTransaction converts stack item into *LedgerTransaction. -func itemToLedgerTransaction(item stackitem.Item, err error) (*LedgerTransaction, error) { - if err != nil { - return nil, err - } - var res = new(LedgerTransaction) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of LedgerTransaction from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *LedgerTransaction) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 8 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Hash, err = func(item stackitem.Item) (util.Uint256, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint256{}, err - } - u, err := util.Uint256DecodeBytesBE(b) - if err != nil { - return util.Uint256{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Hash: %w", err) - } - - index++ - res.Version, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Version: %w", err) - } - - index++ - res.Nonce, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Nonce: %w", err) - } - - index++ - res.Sender, err = func(item stackitem.Item) (util.Uint160, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint160{}, err - } - u, err := util.Uint160DecodeBytesBE(b) - if err != nil { - return util.Uint160{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Sender: %w", err) - } - - index++ - res.SysFee, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field SysFee: %w", err) - } - - index++ - res.NetFee, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field NetFee: %w", err) - } - - index++ - res.ValidUntilBlock, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field ValidUntilBlock: %w", err) - } - - index++ - res.Script, err = arr[index].TryBytes() - if err != nil { - return fmt.Errorf("field Script: %w", err) - } - - return nil -} - -// itemToLedgerTransactionSigner converts stack item into *LedgerTransactionSigner. -func itemToLedgerTransactionSigner(item stackitem.Item, err error) (*LedgerTransactionSigner, error) { - if err != nil { - return nil, err - } - var res = new(LedgerTransactionSigner) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of LedgerTransactionSigner from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *LedgerTransactionSigner) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 5 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Account, err = func(item stackitem.Item) (util.Uint160, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint160{}, err - } - u, err := util.Uint160DecodeBytesBE(b) - if err != nil { - return util.Uint160{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Account: %w", err) - } - - index++ - res.Scopes, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Scopes: %w", err) - } - - index++ - res.AllowedContracts, err = func(item stackitem.Item) ([]util.Uint160, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make([]util.Uint160, len(arr)) - for i := range res { - res[i], err = func(item stackitem.Item) (util.Uint160, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint160{}, err - } - u, err := util.Uint160DecodeBytesBE(b) - if err != nil { - return util.Uint160{}, err - } - return u, nil - }(arr[i]) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field AllowedContracts: %w", err) - } - - index++ - res.AllowedGroups, err = func(item stackitem.Item) (keys.PublicKeys, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make(keys.PublicKeys, len(arr)) - for i := range res { - res[i], err = func(item stackitem.Item) (*keys.PublicKey, error) { - b, err := item.TryBytes() - if err != nil { - return nil, err - } - k, err := keys.NewPublicKeyFromBytes(b, elliptic.P256()) - if err != nil { - return nil, err - } - return k, nil - }(arr[i]) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field AllowedGroups: %w", err) - } - - index++ - res.Rules, err = func(item stackitem.Item) ([]*LedgerWitnessRule, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make([]*LedgerWitnessRule, len(arr)) - for i := range res { - res[i], err = itemToLedgerWitnessRule(arr[i], nil) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Rules: %w", err) - } - - return nil -} - -// itemToLedgerWitnessCondition converts stack item into *LedgerWitnessCondition. -func itemToLedgerWitnessCondition(item stackitem.Item, err error) (*LedgerWitnessCondition, error) { - if err != nil { - return nil, err - } - var res = new(LedgerWitnessCondition) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of LedgerWitnessCondition from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *LedgerWitnessCondition) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 2 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Type, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Type: %w", err) - } - - index++ - res.Value, err = arr[index].Value(), error(nil) - if err != nil { - return fmt.Errorf("field Value: %w", err) - } - - return nil -} - -// itemToLedgerWitnessRule converts stack item into *LedgerWitnessRule. -func itemToLedgerWitnessRule(item stackitem.Item, err error) (*LedgerWitnessRule, error) { - if err != nil { - return nil, err - } - var res = new(LedgerWitnessRule) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of LedgerWitnessRule from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *LedgerWitnessRule) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 2 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Action, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Action: %w", err) - } - - index++ - res.Condition, err = itemToLedgerWitnessCondition(arr[index], nil) - if err != nil { - return fmt.Errorf("field Condition: %w", err) - } - - return nil -} - -// itemToManagementABI converts stack item into *ManagementABI. -func itemToManagementABI(item stackitem.Item, err error) (*ManagementABI, error) { - if err != nil { - return nil, err - } - var res = new(ManagementABI) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of ManagementABI from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *ManagementABI) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 2 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Methods, err = func(item stackitem.Item) ([]*ManagementMethod, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make([]*ManagementMethod, len(arr)) - for i := range res { - res[i], err = itemToManagementMethod(arr[i], nil) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Methods: %w", err) - } - - index++ - res.Events, err = func(item stackitem.Item) ([]*ManagementEvent, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make([]*ManagementEvent, len(arr)) - for i := range res { - res[i], err = itemToManagementEvent(arr[i], nil) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Events: %w", err) - } - - return nil -} - -// itemToManagementContract converts stack item into *ManagementContract. -func itemToManagementContract(item stackitem.Item, err error) (*ManagementContract, error) { - if err != nil { - return nil, err - } - var res = new(ManagementContract) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of ManagementContract from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *ManagementContract) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 5 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.ID, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field ID: %w", err) - } - - index++ - res.UpdateCounter, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field UpdateCounter: %w", err) - } - - index++ - res.Hash, err = func(item stackitem.Item) (util.Uint160, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint160{}, err - } - u, err := util.Uint160DecodeBytesBE(b) - if err != nil { - return util.Uint160{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Hash: %w", err) - } - - index++ - res.NEF, err = arr[index].TryBytes() - if err != nil { - return fmt.Errorf("field NEF: %w", err) - } - - index++ - res.Manifest, err = itemToManagementManifest(arr[index], nil) - if err != nil { - return fmt.Errorf("field Manifest: %w", err) - } - - return nil -} - -// itemToManagementEvent converts stack item into *ManagementEvent. -func itemToManagementEvent(item stackitem.Item, err error) (*ManagementEvent, error) { - if err != nil { - return nil, err - } - var res = new(ManagementEvent) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of ManagementEvent from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *ManagementEvent) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 2 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Name, err = func(item stackitem.Item) (string, error) { - b, err := item.TryBytes() - if err != nil { - return "", err - } - if !utf8.Valid(b) { - return "", errors.New("not a UTF-8 string") - } - return string(b), nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Name: %w", err) - } - - index++ - res.Params, err = func(item stackitem.Item) ([]*ManagementParameter, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make([]*ManagementParameter, len(arr)) - for i := range res { - res[i], err = itemToManagementParameter(arr[i], nil) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Params: %w", err) - } - - return nil -} - -// itemToManagementGroup converts stack item into *ManagementGroup. -func itemToManagementGroup(item stackitem.Item, err error) (*ManagementGroup, error) { - if err != nil { - return nil, err - } - var res = new(ManagementGroup) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of ManagementGroup from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *ManagementGroup) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 2 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.PublicKey, err = func(item stackitem.Item) (*keys.PublicKey, error) { - b, err := item.TryBytes() - if err != nil { - return nil, err - } - k, err := keys.NewPublicKeyFromBytes(b, elliptic.P256()) - if err != nil { - return nil, err - } - return k, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field PublicKey: %w", err) - } - - index++ - res.Signature, err = arr[index].TryBytes() - if err != nil { - return fmt.Errorf("field Signature: %w", err) - } - - return nil -} - -// itemToManagementManifest converts stack item into *ManagementManifest. -func itemToManagementManifest(item stackitem.Item, err error) (*ManagementManifest, error) { - if err != nil { - return nil, err - } - var res = new(ManagementManifest) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of ManagementManifest from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *ManagementManifest) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 8 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Name, err = func(item stackitem.Item) (string, error) { - b, err := item.TryBytes() - if err != nil { - return "", err - } - if !utf8.Valid(b) { - return "", errors.New("not a UTF-8 string") - } - return string(b), nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Name: %w", err) - } - - index++ - res.Groups, err = func(item stackitem.Item) ([]*ManagementGroup, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make([]*ManagementGroup, len(arr)) - for i := range res { - res[i], err = itemToManagementGroup(arr[i], nil) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Groups: %w", err) - } - - index++ - res.Features, err = func(item stackitem.Item) (map[string]string, error) { - m, ok := item.Value().([]stackitem.MapElement) - if !ok { - return nil, fmt.Errorf("%s is not a map", item.Type().String()) - } - res := make(map[string]string) - for i := range m { - k, err := func(item stackitem.Item) (string, error) { - b, err := item.TryBytes() - if err != nil { - return "", err - } - if !utf8.Valid(b) { - return "", errors.New("not a UTF-8 string") - } - return string(b), nil - }(m[i].Key) - if err != nil { - return nil, fmt.Errorf("key %d: %w", i, err) - } - v, err := func(item stackitem.Item) (string, error) { - b, err := item.TryBytes() - if err != nil { - return "", err - } - if !utf8.Valid(b) { - return "", errors.New("not a UTF-8 string") - } - return string(b), nil - }(m[i].Value) - if err != nil { - return nil, fmt.Errorf("value %d: %w", i, err) - } - res[k] = v - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Features: %w", err) - } - - index++ - res.SupportedStandards, err = func(item stackitem.Item) ([]string, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make([]string, len(arr)) - for i := range res { - res[i], err = func(item stackitem.Item) (string, error) { - b, err := item.TryBytes() - if err != nil { - return "", err - } - if !utf8.Valid(b) { - return "", errors.New("not a UTF-8 string") - } - return string(b), nil - }(arr[i]) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field SupportedStandards: %w", err) - } - - index++ - res.ABI, err = itemToManagementABI(arr[index], nil) - if err != nil { - return fmt.Errorf("field ABI: %w", err) - } - - index++ - res.Permissions, err = func(item stackitem.Item) ([]*ManagementPermission, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make([]*ManagementPermission, len(arr)) - for i := range res { - res[i], err = itemToManagementPermission(arr[i], nil) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Permissions: %w", err) - } - - index++ - res.Trusts, err = func(item stackitem.Item) ([]util.Uint160, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make([]util.Uint160, len(arr)) - for i := range res { - res[i], err = func(item stackitem.Item) (util.Uint160, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint160{}, err - } - u, err := util.Uint160DecodeBytesBE(b) - if err != nil { - return util.Uint160{}, err - } - return u, nil - }(arr[i]) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Trusts: %w", err) - } - - index++ - res.Extra, err = arr[index].Value(), error(nil) - if err != nil { - return fmt.Errorf("field Extra: %w", err) - } - - return nil -} - -// itemToManagementMethod converts stack item into *ManagementMethod. -func itemToManagementMethod(item stackitem.Item, err error) (*ManagementMethod, error) { - if err != nil { - return nil, err - } - var res = new(ManagementMethod) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of ManagementMethod from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *ManagementMethod) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 5 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Name, err = func(item stackitem.Item) (string, error) { - b, err := item.TryBytes() - if err != nil { - return "", err - } - if !utf8.Valid(b) { - return "", errors.New("not a UTF-8 string") - } - return string(b), nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Name: %w", err) - } - - index++ - res.Params, err = func(item stackitem.Item) ([]*ManagementParameter, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make([]*ManagementParameter, len(arr)) - for i := range res { - res[i], err = itemToManagementParameter(arr[i], nil) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Params: %w", err) - } - - index++ - res.ReturnType, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field ReturnType: %w", err) - } - - index++ - res.Offset, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Offset: %w", err) - } - - index++ - res.Safe, err = arr[index].TryBool() - if err != nil { - return fmt.Errorf("field Safe: %w", err) - } - - return nil -} - -// itemToManagementParameter converts stack item into *ManagementParameter. -func itemToManagementParameter(item stackitem.Item, err error) (*ManagementParameter, error) { - if err != nil { - return nil, err - } - var res = new(ManagementParameter) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of ManagementParameter from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *ManagementParameter) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 2 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Name, err = func(item stackitem.Item) (string, error) { - b, err := item.TryBytes() - if err != nil { - return "", err - } - if !utf8.Valid(b) { - return "", errors.New("not a UTF-8 string") - } - return string(b), nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Name: %w", err) - } - - index++ - res.Type, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Type: %w", err) - } - - return nil -} - -// itemToManagementPermission converts stack item into *ManagementPermission. -func itemToManagementPermission(item stackitem.Item, err error) (*ManagementPermission, error) { - if err != nil { - return nil, err - } - var res = new(ManagementPermission) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of ManagementPermission from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *ManagementPermission) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 2 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Contract, err = func(item stackitem.Item) (util.Uint160, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint160{}, err - } - u, err := util.Uint160DecodeBytesBE(b) - if err != nil { - return util.Uint160{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Contract: %w", err) - } - - index++ - res.Methods, err = func(item stackitem.Item) ([]string, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make([]string, len(arr)) - for i := range res { - res[i], err = func(item stackitem.Item) (string, error) { - b, err := item.TryBytes() - if err != nil { - return "", err - } - if !utf8.Valid(b) { - return "", errors.New("not a UTF-8 string") - } - return string(b), nil - }(arr[i]) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Methods: %w", err) - } - - return nil -} - // LockEventsFromApplicationLog retrieves a set of all emitted events // with "Lock" name from the provided [result.ApplicationLog]. func LockEventsFromApplicationLog(log *result.ApplicationLog) ([]*LockEvent, error) { diff --git a/rpc/container/rpcbinding.go b/rpc/container/rpcbinding.go index 6287d7fa..ac03034f 100644 --- a/rpc/container/rpcbinding.go +++ b/rpc/container/rpcbinding.go @@ -15,7 +15,6 @@ import ( "github.com/nspcc-dev/neo-go/pkg/util" "github.com/nspcc-dev/neo-go/pkg/vm/stackitem" "math/big" - "unicode/utf8" ) // CommonBallot is a contract-specific common.Ballot type used by its methods. @@ -53,126 +52,6 @@ type ContainerExtendedACL struct { Token []byte } -// LedgerBlock is a contract-specific ledger.Block type used by its methods. -type LedgerBlock struct { - Hash util.Uint256 - Version *big.Int - PrevHash util.Uint256 - MerkleRoot util.Uint256 - Timestamp *big.Int - Nonce *big.Int - Index *big.Int - NextConsensus util.Uint160 - TransactionsLength *big.Int -} - -// LedgerBlockSR is a contract-specific ledger.BlockSR type used by its methods. -type LedgerBlockSR struct { - Hash util.Uint256 - Version *big.Int - PrevHash util.Uint256 - MerkleRoot util.Uint256 - Timestamp *big.Int - Nonce *big.Int - Index *big.Int - NextConsensus util.Uint160 - TransactionsLength *big.Int - PrevStateRoot util.Uint256 -} - -// LedgerTransaction is a contract-specific ledger.Transaction type used by its methods. -type LedgerTransaction struct { - Hash util.Uint256 - Version *big.Int - Nonce *big.Int - Sender util.Uint160 - SysFee *big.Int - NetFee *big.Int - ValidUntilBlock *big.Int - Script []byte -} - -// LedgerTransactionSigner is a contract-specific ledger.TransactionSigner type used by its methods. -type LedgerTransactionSigner struct { - Account util.Uint160 - Scopes *big.Int - AllowedContracts []util.Uint160 - AllowedGroups keys.PublicKeys - Rules []*LedgerWitnessRule -} - -// LedgerWitnessCondition is a contract-specific ledger.WitnessCondition type used by its methods. -type LedgerWitnessCondition struct { - Type *big.Int - Value any -} - -// LedgerWitnessRule is a contract-specific ledger.WitnessRule type used by its methods. -type LedgerWitnessRule struct { - Action *big.Int - Condition *LedgerWitnessCondition -} - -// ManagementABI is a contract-specific management.ABI type used by its methods. -type ManagementABI struct { - Methods []*ManagementMethod - Events []*ManagementEvent -} - -// ManagementContract is a contract-specific management.Contract type used by its methods. -type ManagementContract struct { - ID *big.Int - UpdateCounter *big.Int - Hash util.Uint160 - NEF []byte - Manifest *ManagementManifest -} - -// ManagementEvent is a contract-specific management.Event type used by its methods. -type ManagementEvent struct { - Name string - Params []*ManagementParameter -} - -// ManagementGroup is a contract-specific management.Group type used by its methods. -type ManagementGroup struct { - PublicKey *keys.PublicKey - Signature []byte -} - -// ManagementManifest is a contract-specific management.Manifest type used by its methods. -type ManagementManifest struct { - Name string - Groups []*ManagementGroup - Features map[string]string - SupportedStandards []string - ABI *ManagementABI - Permissions []*ManagementPermission - Trusts []util.Uint160 - Extra any -} - -// ManagementMethod is a contract-specific management.Method type used by its methods. -type ManagementMethod struct { - Name string - Params []*ManagementParameter - ReturnType *big.Int - Offset *big.Int - Safe bool -} - -// ManagementParameter is a contract-specific management.Parameter type used by its methods. -type ManagementParameter struct { - Name string - Type *big.Int -} - -// ManagementPermission is a contract-specific management.Permission type used by its methods. -type ManagementPermission struct { - Contract util.Uint160 - Methods []string -} - // PutSuccessEvent represents "PutSuccess" event emitted by the contract. type PutSuccessEvent struct { ContainerID util.Uint256 @@ -824,1208 +703,6 @@ func (res *ContainerExtendedACL) FromStackItem(item stackitem.Item) error { return nil } -// itemToLedgerBlock converts stack item into *LedgerBlock. -func itemToLedgerBlock(item stackitem.Item, err error) (*LedgerBlock, error) { - if err != nil { - return nil, err - } - var res = new(LedgerBlock) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of LedgerBlock from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *LedgerBlock) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 9 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Hash, err = func(item stackitem.Item) (util.Uint256, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint256{}, err - } - u, err := util.Uint256DecodeBytesBE(b) - if err != nil { - return util.Uint256{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Hash: %w", err) - } - - index++ - res.Version, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Version: %w", err) - } - - index++ - res.PrevHash, err = func(item stackitem.Item) (util.Uint256, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint256{}, err - } - u, err := util.Uint256DecodeBytesBE(b) - if err != nil { - return util.Uint256{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field PrevHash: %w", err) - } - - index++ - res.MerkleRoot, err = func(item stackitem.Item) (util.Uint256, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint256{}, err - } - u, err := util.Uint256DecodeBytesBE(b) - if err != nil { - return util.Uint256{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field MerkleRoot: %w", err) - } - - index++ - res.Timestamp, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Timestamp: %w", err) - } - - index++ - res.Nonce, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Nonce: %w", err) - } - - index++ - res.Index, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Index: %w", err) - } - - index++ - res.NextConsensus, err = func(item stackitem.Item) (util.Uint160, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint160{}, err - } - u, err := util.Uint160DecodeBytesBE(b) - if err != nil { - return util.Uint160{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field NextConsensus: %w", err) - } - - index++ - res.TransactionsLength, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field TransactionsLength: %w", err) - } - - return nil -} - -// itemToLedgerBlockSR converts stack item into *LedgerBlockSR. -func itemToLedgerBlockSR(item stackitem.Item, err error) (*LedgerBlockSR, error) { - if err != nil { - return nil, err - } - var res = new(LedgerBlockSR) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of LedgerBlockSR from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *LedgerBlockSR) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 10 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Hash, err = func(item stackitem.Item) (util.Uint256, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint256{}, err - } - u, err := util.Uint256DecodeBytesBE(b) - if err != nil { - return util.Uint256{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Hash: %w", err) - } - - index++ - res.Version, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Version: %w", err) - } - - index++ - res.PrevHash, err = func(item stackitem.Item) (util.Uint256, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint256{}, err - } - u, err := util.Uint256DecodeBytesBE(b) - if err != nil { - return util.Uint256{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field PrevHash: %w", err) - } - - index++ - res.MerkleRoot, err = func(item stackitem.Item) (util.Uint256, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint256{}, err - } - u, err := util.Uint256DecodeBytesBE(b) - if err != nil { - return util.Uint256{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field MerkleRoot: %w", err) - } - - index++ - res.Timestamp, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Timestamp: %w", err) - } - - index++ - res.Nonce, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Nonce: %w", err) - } - - index++ - res.Index, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Index: %w", err) - } - - index++ - res.NextConsensus, err = func(item stackitem.Item) (util.Uint160, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint160{}, err - } - u, err := util.Uint160DecodeBytesBE(b) - if err != nil { - return util.Uint160{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field NextConsensus: %w", err) - } - - index++ - res.TransactionsLength, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field TransactionsLength: %w", err) - } - - index++ - res.PrevStateRoot, err = func(item stackitem.Item) (util.Uint256, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint256{}, err - } - u, err := util.Uint256DecodeBytesBE(b) - if err != nil { - return util.Uint256{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field PrevStateRoot: %w", err) - } - - return nil -} - -// itemToLedgerTransaction converts stack item into *LedgerTransaction. -func itemToLedgerTransaction(item stackitem.Item, err error) (*LedgerTransaction, error) { - if err != nil { - return nil, err - } - var res = new(LedgerTransaction) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of LedgerTransaction from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *LedgerTransaction) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 8 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Hash, err = func(item stackitem.Item) (util.Uint256, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint256{}, err - } - u, err := util.Uint256DecodeBytesBE(b) - if err != nil { - return util.Uint256{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Hash: %w", err) - } - - index++ - res.Version, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Version: %w", err) - } - - index++ - res.Nonce, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Nonce: %w", err) - } - - index++ - res.Sender, err = func(item stackitem.Item) (util.Uint160, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint160{}, err - } - u, err := util.Uint160DecodeBytesBE(b) - if err != nil { - return util.Uint160{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Sender: %w", err) - } - - index++ - res.SysFee, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field SysFee: %w", err) - } - - index++ - res.NetFee, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field NetFee: %w", err) - } - - index++ - res.ValidUntilBlock, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field ValidUntilBlock: %w", err) - } - - index++ - res.Script, err = arr[index].TryBytes() - if err != nil { - return fmt.Errorf("field Script: %w", err) - } - - return nil -} - -// itemToLedgerTransactionSigner converts stack item into *LedgerTransactionSigner. -func itemToLedgerTransactionSigner(item stackitem.Item, err error) (*LedgerTransactionSigner, error) { - if err != nil { - return nil, err - } - var res = new(LedgerTransactionSigner) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of LedgerTransactionSigner from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *LedgerTransactionSigner) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 5 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Account, err = func(item stackitem.Item) (util.Uint160, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint160{}, err - } - u, err := util.Uint160DecodeBytesBE(b) - if err != nil { - return util.Uint160{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Account: %w", err) - } - - index++ - res.Scopes, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Scopes: %w", err) - } - - index++ - res.AllowedContracts, err = func(item stackitem.Item) ([]util.Uint160, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make([]util.Uint160, len(arr)) - for i := range res { - res[i], err = func(item stackitem.Item) (util.Uint160, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint160{}, err - } - u, err := util.Uint160DecodeBytesBE(b) - if err != nil { - return util.Uint160{}, err - } - return u, nil - }(arr[i]) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field AllowedContracts: %w", err) - } - - index++ - res.AllowedGroups, err = func(item stackitem.Item) (keys.PublicKeys, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make(keys.PublicKeys, len(arr)) - for i := range res { - res[i], err = func(item stackitem.Item) (*keys.PublicKey, error) { - b, err := item.TryBytes() - if err != nil { - return nil, err - } - k, err := keys.NewPublicKeyFromBytes(b, elliptic.P256()) - if err != nil { - return nil, err - } - return k, nil - }(arr[i]) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field AllowedGroups: %w", err) - } - - index++ - res.Rules, err = func(item stackitem.Item) ([]*LedgerWitnessRule, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make([]*LedgerWitnessRule, len(arr)) - for i := range res { - res[i], err = itemToLedgerWitnessRule(arr[i], nil) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Rules: %w", err) - } - - return nil -} - -// itemToLedgerWitnessCondition converts stack item into *LedgerWitnessCondition. -func itemToLedgerWitnessCondition(item stackitem.Item, err error) (*LedgerWitnessCondition, error) { - if err != nil { - return nil, err - } - var res = new(LedgerWitnessCondition) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of LedgerWitnessCondition from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *LedgerWitnessCondition) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 2 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Type, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Type: %w", err) - } - - index++ - res.Value, err = arr[index].Value(), error(nil) - if err != nil { - return fmt.Errorf("field Value: %w", err) - } - - return nil -} - -// itemToLedgerWitnessRule converts stack item into *LedgerWitnessRule. -func itemToLedgerWitnessRule(item stackitem.Item, err error) (*LedgerWitnessRule, error) { - if err != nil { - return nil, err - } - var res = new(LedgerWitnessRule) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of LedgerWitnessRule from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *LedgerWitnessRule) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 2 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Action, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Action: %w", err) - } - - index++ - res.Condition, err = itemToLedgerWitnessCondition(arr[index], nil) - if err != nil { - return fmt.Errorf("field Condition: %w", err) - } - - return nil -} - -// itemToManagementABI converts stack item into *ManagementABI. -func itemToManagementABI(item stackitem.Item, err error) (*ManagementABI, error) { - if err != nil { - return nil, err - } - var res = new(ManagementABI) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of ManagementABI from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *ManagementABI) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 2 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Methods, err = func(item stackitem.Item) ([]*ManagementMethod, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make([]*ManagementMethod, len(arr)) - for i := range res { - res[i], err = itemToManagementMethod(arr[i], nil) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Methods: %w", err) - } - - index++ - res.Events, err = func(item stackitem.Item) ([]*ManagementEvent, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make([]*ManagementEvent, len(arr)) - for i := range res { - res[i], err = itemToManagementEvent(arr[i], nil) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Events: %w", err) - } - - return nil -} - -// itemToManagementContract converts stack item into *ManagementContract. -func itemToManagementContract(item stackitem.Item, err error) (*ManagementContract, error) { - if err != nil { - return nil, err - } - var res = new(ManagementContract) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of ManagementContract from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *ManagementContract) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 5 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.ID, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field ID: %w", err) - } - - index++ - res.UpdateCounter, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field UpdateCounter: %w", err) - } - - index++ - res.Hash, err = func(item stackitem.Item) (util.Uint160, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint160{}, err - } - u, err := util.Uint160DecodeBytesBE(b) - if err != nil { - return util.Uint160{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Hash: %w", err) - } - - index++ - res.NEF, err = arr[index].TryBytes() - if err != nil { - return fmt.Errorf("field NEF: %w", err) - } - - index++ - res.Manifest, err = itemToManagementManifest(arr[index], nil) - if err != nil { - return fmt.Errorf("field Manifest: %w", err) - } - - return nil -} - -// itemToManagementEvent converts stack item into *ManagementEvent. -func itemToManagementEvent(item stackitem.Item, err error) (*ManagementEvent, error) { - if err != nil { - return nil, err - } - var res = new(ManagementEvent) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of ManagementEvent from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *ManagementEvent) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 2 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Name, err = func(item stackitem.Item) (string, error) { - b, err := item.TryBytes() - if err != nil { - return "", err - } - if !utf8.Valid(b) { - return "", errors.New("not a UTF-8 string") - } - return string(b), nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Name: %w", err) - } - - index++ - res.Params, err = func(item stackitem.Item) ([]*ManagementParameter, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make([]*ManagementParameter, len(arr)) - for i := range res { - res[i], err = itemToManagementParameter(arr[i], nil) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Params: %w", err) - } - - return nil -} - -// itemToManagementGroup converts stack item into *ManagementGroup. -func itemToManagementGroup(item stackitem.Item, err error) (*ManagementGroup, error) { - if err != nil { - return nil, err - } - var res = new(ManagementGroup) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of ManagementGroup from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *ManagementGroup) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 2 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.PublicKey, err = func(item stackitem.Item) (*keys.PublicKey, error) { - b, err := item.TryBytes() - if err != nil { - return nil, err - } - k, err := keys.NewPublicKeyFromBytes(b, elliptic.P256()) - if err != nil { - return nil, err - } - return k, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field PublicKey: %w", err) - } - - index++ - res.Signature, err = arr[index].TryBytes() - if err != nil { - return fmt.Errorf("field Signature: %w", err) - } - - return nil -} - -// itemToManagementManifest converts stack item into *ManagementManifest. -func itemToManagementManifest(item stackitem.Item, err error) (*ManagementManifest, error) { - if err != nil { - return nil, err - } - var res = new(ManagementManifest) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of ManagementManifest from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *ManagementManifest) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 8 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Name, err = func(item stackitem.Item) (string, error) { - b, err := item.TryBytes() - if err != nil { - return "", err - } - if !utf8.Valid(b) { - return "", errors.New("not a UTF-8 string") - } - return string(b), nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Name: %w", err) - } - - index++ - res.Groups, err = func(item stackitem.Item) ([]*ManagementGroup, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make([]*ManagementGroup, len(arr)) - for i := range res { - res[i], err = itemToManagementGroup(arr[i], nil) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Groups: %w", err) - } - - index++ - res.Features, err = func(item stackitem.Item) (map[string]string, error) { - m, ok := item.Value().([]stackitem.MapElement) - if !ok { - return nil, fmt.Errorf("%s is not a map", item.Type().String()) - } - res := make(map[string]string) - for i := range m { - k, err := func(item stackitem.Item) (string, error) { - b, err := item.TryBytes() - if err != nil { - return "", err - } - if !utf8.Valid(b) { - return "", errors.New("not a UTF-8 string") - } - return string(b), nil - }(m[i].Key) - if err != nil { - return nil, fmt.Errorf("key %d: %w", i, err) - } - v, err := func(item stackitem.Item) (string, error) { - b, err := item.TryBytes() - if err != nil { - return "", err - } - if !utf8.Valid(b) { - return "", errors.New("not a UTF-8 string") - } - return string(b), nil - }(m[i].Value) - if err != nil { - return nil, fmt.Errorf("value %d: %w", i, err) - } - res[k] = v - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Features: %w", err) - } - - index++ - res.SupportedStandards, err = func(item stackitem.Item) ([]string, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make([]string, len(arr)) - for i := range res { - res[i], err = func(item stackitem.Item) (string, error) { - b, err := item.TryBytes() - if err != nil { - return "", err - } - if !utf8.Valid(b) { - return "", errors.New("not a UTF-8 string") - } - return string(b), nil - }(arr[i]) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field SupportedStandards: %w", err) - } - - index++ - res.ABI, err = itemToManagementABI(arr[index], nil) - if err != nil { - return fmt.Errorf("field ABI: %w", err) - } - - index++ - res.Permissions, err = func(item stackitem.Item) ([]*ManagementPermission, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make([]*ManagementPermission, len(arr)) - for i := range res { - res[i], err = itemToManagementPermission(arr[i], nil) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Permissions: %w", err) - } - - index++ - res.Trusts, err = func(item stackitem.Item) ([]util.Uint160, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make([]util.Uint160, len(arr)) - for i := range res { - res[i], err = func(item stackitem.Item) (util.Uint160, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint160{}, err - } - u, err := util.Uint160DecodeBytesBE(b) - if err != nil { - return util.Uint160{}, err - } - return u, nil - }(arr[i]) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Trusts: %w", err) - } - - index++ - res.Extra, err = arr[index].Value(), error(nil) - if err != nil { - return fmt.Errorf("field Extra: %w", err) - } - - return nil -} - -// itemToManagementMethod converts stack item into *ManagementMethod. -func itemToManagementMethod(item stackitem.Item, err error) (*ManagementMethod, error) { - if err != nil { - return nil, err - } - var res = new(ManagementMethod) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of ManagementMethod from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *ManagementMethod) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 5 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Name, err = func(item stackitem.Item) (string, error) { - b, err := item.TryBytes() - if err != nil { - return "", err - } - if !utf8.Valid(b) { - return "", errors.New("not a UTF-8 string") - } - return string(b), nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Name: %w", err) - } - - index++ - res.Params, err = func(item stackitem.Item) ([]*ManagementParameter, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make([]*ManagementParameter, len(arr)) - for i := range res { - res[i], err = itemToManagementParameter(arr[i], nil) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Params: %w", err) - } - - index++ - res.ReturnType, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field ReturnType: %w", err) - } - - index++ - res.Offset, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Offset: %w", err) - } - - index++ - res.Safe, err = arr[index].TryBool() - if err != nil { - return fmt.Errorf("field Safe: %w", err) - } - - return nil -} - -// itemToManagementParameter converts stack item into *ManagementParameter. -func itemToManagementParameter(item stackitem.Item, err error) (*ManagementParameter, error) { - if err != nil { - return nil, err - } - var res = new(ManagementParameter) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of ManagementParameter from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *ManagementParameter) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 2 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Name, err = func(item stackitem.Item) (string, error) { - b, err := item.TryBytes() - if err != nil { - return "", err - } - if !utf8.Valid(b) { - return "", errors.New("not a UTF-8 string") - } - return string(b), nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Name: %w", err) - } - - index++ - res.Type, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Type: %w", err) - } - - return nil -} - -// itemToManagementPermission converts stack item into *ManagementPermission. -func itemToManagementPermission(item stackitem.Item, err error) (*ManagementPermission, error) { - if err != nil { - return nil, err - } - var res = new(ManagementPermission) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of ManagementPermission from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *ManagementPermission) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 2 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Contract, err = func(item stackitem.Item) (util.Uint160, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint160{}, err - } - u, err := util.Uint160DecodeBytesBE(b) - if err != nil { - return util.Uint160{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Contract: %w", err) - } - - index++ - res.Methods, err = func(item stackitem.Item) ([]string, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make([]string, len(arr)) - for i := range res { - res[i], err = func(item stackitem.Item) (string, error) { - b, err := item.TryBytes() - if err != nil { - return "", err - } - if !utf8.Valid(b) { - return "", errors.New("not a UTF-8 string") - } - return string(b), nil - }(arr[i]) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Methods: %w", err) - } - - return nil -} - // PutSuccessEventsFromApplicationLog retrieves a set of all emitted events // with "PutSuccess" name from the provided [result.ApplicationLog]. func PutSuccessEventsFromApplicationLog(log *result.ApplicationLog) ([]*PutSuccessEvent, error) { diff --git a/rpc/neofs/rpcbinding.go b/rpc/neofs/rpcbinding.go index 8bff2e09..d7d2f44f 100644 --- a/rpc/neofs/rpcbinding.go +++ b/rpc/neofs/rpcbinding.go @@ -14,7 +14,6 @@ import ( "github.com/nspcc-dev/neo-go/pkg/util" "github.com/nspcc-dev/neo-go/pkg/vm/stackitem" "math/big" - "unicode/utf8" ) // CommonBallot is a contract-specific common.Ballot type used by its methods. @@ -29,126 +28,6 @@ type CommonIRNode struct { PublicKey *keys.PublicKey } -// LedgerBlock is a contract-specific ledger.Block type used by its methods. -type LedgerBlock struct { - Hash util.Uint256 - Version *big.Int - PrevHash util.Uint256 - MerkleRoot util.Uint256 - Timestamp *big.Int - Nonce *big.Int - Index *big.Int - NextConsensus util.Uint160 - TransactionsLength *big.Int -} - -// LedgerBlockSR is a contract-specific ledger.BlockSR type used by its methods. -type LedgerBlockSR struct { - Hash util.Uint256 - Version *big.Int - PrevHash util.Uint256 - MerkleRoot util.Uint256 - Timestamp *big.Int - Nonce *big.Int - Index *big.Int - NextConsensus util.Uint160 - TransactionsLength *big.Int - PrevStateRoot util.Uint256 -} - -// LedgerTransaction is a contract-specific ledger.Transaction type used by its methods. -type LedgerTransaction struct { - Hash util.Uint256 - Version *big.Int - Nonce *big.Int - Sender util.Uint160 - SysFee *big.Int - NetFee *big.Int - ValidUntilBlock *big.Int - Script []byte -} - -// LedgerTransactionSigner is a contract-specific ledger.TransactionSigner type used by its methods. -type LedgerTransactionSigner struct { - Account util.Uint160 - Scopes *big.Int - AllowedContracts []util.Uint160 - AllowedGroups keys.PublicKeys - Rules []*LedgerWitnessRule -} - -// LedgerWitnessCondition is a contract-specific ledger.WitnessCondition type used by its methods. -type LedgerWitnessCondition struct { - Type *big.Int - Value any -} - -// LedgerWitnessRule is a contract-specific ledger.WitnessRule type used by its methods. -type LedgerWitnessRule struct { - Action *big.Int - Condition *LedgerWitnessCondition -} - -// ManagementABI is a contract-specific management.ABI type used by its methods. -type ManagementABI struct { - Methods []*ManagementMethod - Events []*ManagementEvent -} - -// ManagementContract is a contract-specific management.Contract type used by its methods. -type ManagementContract struct { - ID *big.Int - UpdateCounter *big.Int - Hash util.Uint160 - NEF []byte - Manifest *ManagementManifest -} - -// ManagementEvent is a contract-specific management.Event type used by its methods. -type ManagementEvent struct { - Name string - Params []*ManagementParameter -} - -// ManagementGroup is a contract-specific management.Group type used by its methods. -type ManagementGroup struct { - PublicKey *keys.PublicKey - Signature []byte -} - -// ManagementManifest is a contract-specific management.Manifest type used by its methods. -type ManagementManifest struct { - Name string - Groups []*ManagementGroup - Features map[string]string - SupportedStandards []string - ABI *ManagementABI - Permissions []*ManagementPermission - Trusts []util.Uint160 - Extra any -} - -// ManagementMethod is a contract-specific management.Method type used by its methods. -type ManagementMethod struct { - Name string - Params []*ManagementParameter - ReturnType *big.Int - Offset *big.Int - Safe bool -} - -// ManagementParameter is a contract-specific management.Parameter type used by its methods. -type ManagementParameter struct { - Name string - Type *big.Int -} - -// ManagementPermission is a contract-specific management.Permission type used by its methods. -type ManagementPermission struct { - Contract util.Uint160 - Methods []string -} - // NeofsRecord is a contract-specific neofs.Record type used by its methods. type NeofsRecord struct { Key []byte @@ -643,1208 +522,6 @@ func (res *CommonIRNode) FromStackItem(item stackitem.Item) error { return nil } -// itemToLedgerBlock converts stack item into *LedgerBlock. -func itemToLedgerBlock(item stackitem.Item, err error) (*LedgerBlock, error) { - if err != nil { - return nil, err - } - var res = new(LedgerBlock) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of LedgerBlock from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *LedgerBlock) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 9 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Hash, err = func(item stackitem.Item) (util.Uint256, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint256{}, err - } - u, err := util.Uint256DecodeBytesBE(b) - if err != nil { - return util.Uint256{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Hash: %w", err) - } - - index++ - res.Version, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Version: %w", err) - } - - index++ - res.PrevHash, err = func(item stackitem.Item) (util.Uint256, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint256{}, err - } - u, err := util.Uint256DecodeBytesBE(b) - if err != nil { - return util.Uint256{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field PrevHash: %w", err) - } - - index++ - res.MerkleRoot, err = func(item stackitem.Item) (util.Uint256, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint256{}, err - } - u, err := util.Uint256DecodeBytesBE(b) - if err != nil { - return util.Uint256{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field MerkleRoot: %w", err) - } - - index++ - res.Timestamp, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Timestamp: %w", err) - } - - index++ - res.Nonce, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Nonce: %w", err) - } - - index++ - res.Index, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Index: %w", err) - } - - index++ - res.NextConsensus, err = func(item stackitem.Item) (util.Uint160, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint160{}, err - } - u, err := util.Uint160DecodeBytesBE(b) - if err != nil { - return util.Uint160{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field NextConsensus: %w", err) - } - - index++ - res.TransactionsLength, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field TransactionsLength: %w", err) - } - - return nil -} - -// itemToLedgerBlockSR converts stack item into *LedgerBlockSR. -func itemToLedgerBlockSR(item stackitem.Item, err error) (*LedgerBlockSR, error) { - if err != nil { - return nil, err - } - var res = new(LedgerBlockSR) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of LedgerBlockSR from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *LedgerBlockSR) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 10 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Hash, err = func(item stackitem.Item) (util.Uint256, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint256{}, err - } - u, err := util.Uint256DecodeBytesBE(b) - if err != nil { - return util.Uint256{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Hash: %w", err) - } - - index++ - res.Version, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Version: %w", err) - } - - index++ - res.PrevHash, err = func(item stackitem.Item) (util.Uint256, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint256{}, err - } - u, err := util.Uint256DecodeBytesBE(b) - if err != nil { - return util.Uint256{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field PrevHash: %w", err) - } - - index++ - res.MerkleRoot, err = func(item stackitem.Item) (util.Uint256, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint256{}, err - } - u, err := util.Uint256DecodeBytesBE(b) - if err != nil { - return util.Uint256{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field MerkleRoot: %w", err) - } - - index++ - res.Timestamp, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Timestamp: %w", err) - } - - index++ - res.Nonce, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Nonce: %w", err) - } - - index++ - res.Index, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Index: %w", err) - } - - index++ - res.NextConsensus, err = func(item stackitem.Item) (util.Uint160, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint160{}, err - } - u, err := util.Uint160DecodeBytesBE(b) - if err != nil { - return util.Uint160{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field NextConsensus: %w", err) - } - - index++ - res.TransactionsLength, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field TransactionsLength: %w", err) - } - - index++ - res.PrevStateRoot, err = func(item stackitem.Item) (util.Uint256, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint256{}, err - } - u, err := util.Uint256DecodeBytesBE(b) - if err != nil { - return util.Uint256{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field PrevStateRoot: %w", err) - } - - return nil -} - -// itemToLedgerTransaction converts stack item into *LedgerTransaction. -func itemToLedgerTransaction(item stackitem.Item, err error) (*LedgerTransaction, error) { - if err != nil { - return nil, err - } - var res = new(LedgerTransaction) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of LedgerTransaction from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *LedgerTransaction) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 8 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Hash, err = func(item stackitem.Item) (util.Uint256, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint256{}, err - } - u, err := util.Uint256DecodeBytesBE(b) - if err != nil { - return util.Uint256{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Hash: %w", err) - } - - index++ - res.Version, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Version: %w", err) - } - - index++ - res.Nonce, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Nonce: %w", err) - } - - index++ - res.Sender, err = func(item stackitem.Item) (util.Uint160, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint160{}, err - } - u, err := util.Uint160DecodeBytesBE(b) - if err != nil { - return util.Uint160{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Sender: %w", err) - } - - index++ - res.SysFee, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field SysFee: %w", err) - } - - index++ - res.NetFee, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field NetFee: %w", err) - } - - index++ - res.ValidUntilBlock, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field ValidUntilBlock: %w", err) - } - - index++ - res.Script, err = arr[index].TryBytes() - if err != nil { - return fmt.Errorf("field Script: %w", err) - } - - return nil -} - -// itemToLedgerTransactionSigner converts stack item into *LedgerTransactionSigner. -func itemToLedgerTransactionSigner(item stackitem.Item, err error) (*LedgerTransactionSigner, error) { - if err != nil { - return nil, err - } - var res = new(LedgerTransactionSigner) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of LedgerTransactionSigner from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *LedgerTransactionSigner) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 5 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Account, err = func(item stackitem.Item) (util.Uint160, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint160{}, err - } - u, err := util.Uint160DecodeBytesBE(b) - if err != nil { - return util.Uint160{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Account: %w", err) - } - - index++ - res.Scopes, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Scopes: %w", err) - } - - index++ - res.AllowedContracts, err = func(item stackitem.Item) ([]util.Uint160, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make([]util.Uint160, len(arr)) - for i := range res { - res[i], err = func(item stackitem.Item) (util.Uint160, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint160{}, err - } - u, err := util.Uint160DecodeBytesBE(b) - if err != nil { - return util.Uint160{}, err - } - return u, nil - }(arr[i]) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field AllowedContracts: %w", err) - } - - index++ - res.AllowedGroups, err = func(item stackitem.Item) (keys.PublicKeys, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make(keys.PublicKeys, len(arr)) - for i := range res { - res[i], err = func(item stackitem.Item) (*keys.PublicKey, error) { - b, err := item.TryBytes() - if err != nil { - return nil, err - } - k, err := keys.NewPublicKeyFromBytes(b, elliptic.P256()) - if err != nil { - return nil, err - } - return k, nil - }(arr[i]) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field AllowedGroups: %w", err) - } - - index++ - res.Rules, err = func(item stackitem.Item) ([]*LedgerWitnessRule, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make([]*LedgerWitnessRule, len(arr)) - for i := range res { - res[i], err = itemToLedgerWitnessRule(arr[i], nil) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Rules: %w", err) - } - - return nil -} - -// itemToLedgerWitnessCondition converts stack item into *LedgerWitnessCondition. -func itemToLedgerWitnessCondition(item stackitem.Item, err error) (*LedgerWitnessCondition, error) { - if err != nil { - return nil, err - } - var res = new(LedgerWitnessCondition) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of LedgerWitnessCondition from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *LedgerWitnessCondition) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 2 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Type, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Type: %w", err) - } - - index++ - res.Value, err = arr[index].Value(), error(nil) - if err != nil { - return fmt.Errorf("field Value: %w", err) - } - - return nil -} - -// itemToLedgerWitnessRule converts stack item into *LedgerWitnessRule. -func itemToLedgerWitnessRule(item stackitem.Item, err error) (*LedgerWitnessRule, error) { - if err != nil { - return nil, err - } - var res = new(LedgerWitnessRule) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of LedgerWitnessRule from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *LedgerWitnessRule) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 2 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Action, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Action: %w", err) - } - - index++ - res.Condition, err = itemToLedgerWitnessCondition(arr[index], nil) - if err != nil { - return fmt.Errorf("field Condition: %w", err) - } - - return nil -} - -// itemToManagementABI converts stack item into *ManagementABI. -func itemToManagementABI(item stackitem.Item, err error) (*ManagementABI, error) { - if err != nil { - return nil, err - } - var res = new(ManagementABI) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of ManagementABI from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *ManagementABI) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 2 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Methods, err = func(item stackitem.Item) ([]*ManagementMethod, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make([]*ManagementMethod, len(arr)) - for i := range res { - res[i], err = itemToManagementMethod(arr[i], nil) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Methods: %w", err) - } - - index++ - res.Events, err = func(item stackitem.Item) ([]*ManagementEvent, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make([]*ManagementEvent, len(arr)) - for i := range res { - res[i], err = itemToManagementEvent(arr[i], nil) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Events: %w", err) - } - - return nil -} - -// itemToManagementContract converts stack item into *ManagementContract. -func itemToManagementContract(item stackitem.Item, err error) (*ManagementContract, error) { - if err != nil { - return nil, err - } - var res = new(ManagementContract) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of ManagementContract from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *ManagementContract) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 5 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.ID, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field ID: %w", err) - } - - index++ - res.UpdateCounter, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field UpdateCounter: %w", err) - } - - index++ - res.Hash, err = func(item stackitem.Item) (util.Uint160, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint160{}, err - } - u, err := util.Uint160DecodeBytesBE(b) - if err != nil { - return util.Uint160{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Hash: %w", err) - } - - index++ - res.NEF, err = arr[index].TryBytes() - if err != nil { - return fmt.Errorf("field NEF: %w", err) - } - - index++ - res.Manifest, err = itemToManagementManifest(arr[index], nil) - if err != nil { - return fmt.Errorf("field Manifest: %w", err) - } - - return nil -} - -// itemToManagementEvent converts stack item into *ManagementEvent. -func itemToManagementEvent(item stackitem.Item, err error) (*ManagementEvent, error) { - if err != nil { - return nil, err - } - var res = new(ManagementEvent) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of ManagementEvent from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *ManagementEvent) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 2 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Name, err = func(item stackitem.Item) (string, error) { - b, err := item.TryBytes() - if err != nil { - return "", err - } - if !utf8.Valid(b) { - return "", errors.New("not a UTF-8 string") - } - return string(b), nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Name: %w", err) - } - - index++ - res.Params, err = func(item stackitem.Item) ([]*ManagementParameter, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make([]*ManagementParameter, len(arr)) - for i := range res { - res[i], err = itemToManagementParameter(arr[i], nil) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Params: %w", err) - } - - return nil -} - -// itemToManagementGroup converts stack item into *ManagementGroup. -func itemToManagementGroup(item stackitem.Item, err error) (*ManagementGroup, error) { - if err != nil { - return nil, err - } - var res = new(ManagementGroup) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of ManagementGroup from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *ManagementGroup) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 2 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.PublicKey, err = func(item stackitem.Item) (*keys.PublicKey, error) { - b, err := item.TryBytes() - if err != nil { - return nil, err - } - k, err := keys.NewPublicKeyFromBytes(b, elliptic.P256()) - if err != nil { - return nil, err - } - return k, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field PublicKey: %w", err) - } - - index++ - res.Signature, err = arr[index].TryBytes() - if err != nil { - return fmt.Errorf("field Signature: %w", err) - } - - return nil -} - -// itemToManagementManifest converts stack item into *ManagementManifest. -func itemToManagementManifest(item stackitem.Item, err error) (*ManagementManifest, error) { - if err != nil { - return nil, err - } - var res = new(ManagementManifest) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of ManagementManifest from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *ManagementManifest) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 8 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Name, err = func(item stackitem.Item) (string, error) { - b, err := item.TryBytes() - if err != nil { - return "", err - } - if !utf8.Valid(b) { - return "", errors.New("not a UTF-8 string") - } - return string(b), nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Name: %w", err) - } - - index++ - res.Groups, err = func(item stackitem.Item) ([]*ManagementGroup, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make([]*ManagementGroup, len(arr)) - for i := range res { - res[i], err = itemToManagementGroup(arr[i], nil) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Groups: %w", err) - } - - index++ - res.Features, err = func(item stackitem.Item) (map[string]string, error) { - m, ok := item.Value().([]stackitem.MapElement) - if !ok { - return nil, fmt.Errorf("%s is not a map", item.Type().String()) - } - res := make(map[string]string) - for i := range m { - k, err := func(item stackitem.Item) (string, error) { - b, err := item.TryBytes() - if err != nil { - return "", err - } - if !utf8.Valid(b) { - return "", errors.New("not a UTF-8 string") - } - return string(b), nil - }(m[i].Key) - if err != nil { - return nil, fmt.Errorf("key %d: %w", i, err) - } - v, err := func(item stackitem.Item) (string, error) { - b, err := item.TryBytes() - if err != nil { - return "", err - } - if !utf8.Valid(b) { - return "", errors.New("not a UTF-8 string") - } - return string(b), nil - }(m[i].Value) - if err != nil { - return nil, fmt.Errorf("value %d: %w", i, err) - } - res[k] = v - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Features: %w", err) - } - - index++ - res.SupportedStandards, err = func(item stackitem.Item) ([]string, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make([]string, len(arr)) - for i := range res { - res[i], err = func(item stackitem.Item) (string, error) { - b, err := item.TryBytes() - if err != nil { - return "", err - } - if !utf8.Valid(b) { - return "", errors.New("not a UTF-8 string") - } - return string(b), nil - }(arr[i]) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field SupportedStandards: %w", err) - } - - index++ - res.ABI, err = itemToManagementABI(arr[index], nil) - if err != nil { - return fmt.Errorf("field ABI: %w", err) - } - - index++ - res.Permissions, err = func(item stackitem.Item) ([]*ManagementPermission, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make([]*ManagementPermission, len(arr)) - for i := range res { - res[i], err = itemToManagementPermission(arr[i], nil) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Permissions: %w", err) - } - - index++ - res.Trusts, err = func(item stackitem.Item) ([]util.Uint160, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make([]util.Uint160, len(arr)) - for i := range res { - res[i], err = func(item stackitem.Item) (util.Uint160, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint160{}, err - } - u, err := util.Uint160DecodeBytesBE(b) - if err != nil { - return util.Uint160{}, err - } - return u, nil - }(arr[i]) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Trusts: %w", err) - } - - index++ - res.Extra, err = arr[index].Value(), error(nil) - if err != nil { - return fmt.Errorf("field Extra: %w", err) - } - - return nil -} - -// itemToManagementMethod converts stack item into *ManagementMethod. -func itemToManagementMethod(item stackitem.Item, err error) (*ManagementMethod, error) { - if err != nil { - return nil, err - } - var res = new(ManagementMethod) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of ManagementMethod from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *ManagementMethod) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 5 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Name, err = func(item stackitem.Item) (string, error) { - b, err := item.TryBytes() - if err != nil { - return "", err - } - if !utf8.Valid(b) { - return "", errors.New("not a UTF-8 string") - } - return string(b), nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Name: %w", err) - } - - index++ - res.Params, err = func(item stackitem.Item) ([]*ManagementParameter, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make([]*ManagementParameter, len(arr)) - for i := range res { - res[i], err = itemToManagementParameter(arr[i], nil) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Params: %w", err) - } - - index++ - res.ReturnType, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field ReturnType: %w", err) - } - - index++ - res.Offset, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Offset: %w", err) - } - - index++ - res.Safe, err = arr[index].TryBool() - if err != nil { - return fmt.Errorf("field Safe: %w", err) - } - - return nil -} - -// itemToManagementParameter converts stack item into *ManagementParameter. -func itemToManagementParameter(item stackitem.Item, err error) (*ManagementParameter, error) { - if err != nil { - return nil, err - } - var res = new(ManagementParameter) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of ManagementParameter from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *ManagementParameter) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 2 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Name, err = func(item stackitem.Item) (string, error) { - b, err := item.TryBytes() - if err != nil { - return "", err - } - if !utf8.Valid(b) { - return "", errors.New("not a UTF-8 string") - } - return string(b), nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Name: %w", err) - } - - index++ - res.Type, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Type: %w", err) - } - - return nil -} - -// itemToManagementPermission converts stack item into *ManagementPermission. -func itemToManagementPermission(item stackitem.Item, err error) (*ManagementPermission, error) { - if err != nil { - return nil, err - } - var res = new(ManagementPermission) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of ManagementPermission from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *ManagementPermission) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 2 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Contract, err = func(item stackitem.Item) (util.Uint160, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint160{}, err - } - u, err := util.Uint160DecodeBytesBE(b) - if err != nil { - return util.Uint160{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Contract: %w", err) - } - - index++ - res.Methods, err = func(item stackitem.Item) ([]string, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make([]string, len(arr)) - for i := range res { - res[i], err = func(item stackitem.Item) (string, error) { - b, err := item.TryBytes() - if err != nil { - return "", err - } - if !utf8.Valid(b) { - return "", errors.New("not a UTF-8 string") - } - return string(b), nil - }(arr[i]) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Methods: %w", err) - } - - return nil -} - // itemToNeofsRecord converts stack item into *NeofsRecord. func itemToNeofsRecord(item stackitem.Item, err error) (*NeofsRecord, error) { if err != nil { diff --git a/rpc/neofsid/rpcbinding.go b/rpc/neofsid/rpcbinding.go index b9f8a5ae..17f7d4ed 100644 --- a/rpc/neofsid/rpcbinding.go +++ b/rpc/neofsid/rpcbinding.go @@ -14,7 +14,6 @@ import ( "github.com/nspcc-dev/neo-go/pkg/util" "github.com/nspcc-dev/neo-go/pkg/vm/stackitem" "math/big" - "unicode/utf8" ) // CommonBallot is a contract-specific common.Ballot type used by its methods. @@ -24,126 +23,6 @@ type CommonBallot struct { Height *big.Int } -// LedgerBlock is a contract-specific ledger.Block type used by its methods. -type LedgerBlock struct { - Hash util.Uint256 - Version *big.Int - PrevHash util.Uint256 - MerkleRoot util.Uint256 - Timestamp *big.Int - Nonce *big.Int - Index *big.Int - NextConsensus util.Uint160 - TransactionsLength *big.Int -} - -// LedgerBlockSR is a contract-specific ledger.BlockSR type used by its methods. -type LedgerBlockSR struct { - Hash util.Uint256 - Version *big.Int - PrevHash util.Uint256 - MerkleRoot util.Uint256 - Timestamp *big.Int - Nonce *big.Int - Index *big.Int - NextConsensus util.Uint160 - TransactionsLength *big.Int - PrevStateRoot util.Uint256 -} - -// LedgerTransaction is a contract-specific ledger.Transaction type used by its methods. -type LedgerTransaction struct { - Hash util.Uint256 - Version *big.Int - Nonce *big.Int - Sender util.Uint160 - SysFee *big.Int - NetFee *big.Int - ValidUntilBlock *big.Int - Script []byte -} - -// LedgerTransactionSigner is a contract-specific ledger.TransactionSigner type used by its methods. -type LedgerTransactionSigner struct { - Account util.Uint160 - Scopes *big.Int - AllowedContracts []util.Uint160 - AllowedGroups keys.PublicKeys - Rules []*LedgerWitnessRule -} - -// LedgerWitnessCondition is a contract-specific ledger.WitnessCondition type used by its methods. -type LedgerWitnessCondition struct { - Type *big.Int - Value any -} - -// LedgerWitnessRule is a contract-specific ledger.WitnessRule type used by its methods. -type LedgerWitnessRule struct { - Action *big.Int - Condition *LedgerWitnessCondition -} - -// ManagementABI is a contract-specific management.ABI type used by its methods. -type ManagementABI struct { - Methods []*ManagementMethod - Events []*ManagementEvent -} - -// ManagementContract is a contract-specific management.Contract type used by its methods. -type ManagementContract struct { - ID *big.Int - UpdateCounter *big.Int - Hash util.Uint160 - NEF []byte - Manifest *ManagementManifest -} - -// ManagementEvent is a contract-specific management.Event type used by its methods. -type ManagementEvent struct { - Name string - Params []*ManagementParameter -} - -// ManagementGroup is a contract-specific management.Group type used by its methods. -type ManagementGroup struct { - PublicKey *keys.PublicKey - Signature []byte -} - -// ManagementManifest is a contract-specific management.Manifest type used by its methods. -type ManagementManifest struct { - Name string - Groups []*ManagementGroup - Features map[string]string - SupportedStandards []string - ABI *ManagementABI - Permissions []*ManagementPermission - Trusts []util.Uint160 - Extra any -} - -// ManagementMethod is a contract-specific management.Method type used by its methods. -type ManagementMethod struct { - Name string - Params []*ManagementParameter - ReturnType *big.Int - Offset *big.Int - Safe bool -} - -// ManagementParameter is a contract-specific management.Parameter type used by its methods. -type ManagementParameter struct { - Name string - Type *big.Int -} - -// ManagementPermission is a contract-specific management.Permission type used by its methods. -type ManagementPermission struct { - Contract util.Uint160 - Methods []string -} - // NeofsidUserInfo is a contract-specific neofsid.UserInfo type used by its methods. type NeofsidUserInfo struct { Keys [][]byte @@ -334,1208 +213,6 @@ func (res *CommonBallot) FromStackItem(item stackitem.Item) error { return nil } -// itemToLedgerBlock converts stack item into *LedgerBlock. -func itemToLedgerBlock(item stackitem.Item, err error) (*LedgerBlock, error) { - if err != nil { - return nil, err - } - var res = new(LedgerBlock) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of LedgerBlock from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *LedgerBlock) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 9 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Hash, err = func(item stackitem.Item) (util.Uint256, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint256{}, err - } - u, err := util.Uint256DecodeBytesBE(b) - if err != nil { - return util.Uint256{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Hash: %w", err) - } - - index++ - res.Version, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Version: %w", err) - } - - index++ - res.PrevHash, err = func(item stackitem.Item) (util.Uint256, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint256{}, err - } - u, err := util.Uint256DecodeBytesBE(b) - if err != nil { - return util.Uint256{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field PrevHash: %w", err) - } - - index++ - res.MerkleRoot, err = func(item stackitem.Item) (util.Uint256, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint256{}, err - } - u, err := util.Uint256DecodeBytesBE(b) - if err != nil { - return util.Uint256{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field MerkleRoot: %w", err) - } - - index++ - res.Timestamp, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Timestamp: %w", err) - } - - index++ - res.Nonce, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Nonce: %w", err) - } - - index++ - res.Index, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Index: %w", err) - } - - index++ - res.NextConsensus, err = func(item stackitem.Item) (util.Uint160, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint160{}, err - } - u, err := util.Uint160DecodeBytesBE(b) - if err != nil { - return util.Uint160{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field NextConsensus: %w", err) - } - - index++ - res.TransactionsLength, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field TransactionsLength: %w", err) - } - - return nil -} - -// itemToLedgerBlockSR converts stack item into *LedgerBlockSR. -func itemToLedgerBlockSR(item stackitem.Item, err error) (*LedgerBlockSR, error) { - if err != nil { - return nil, err - } - var res = new(LedgerBlockSR) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of LedgerBlockSR from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *LedgerBlockSR) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 10 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Hash, err = func(item stackitem.Item) (util.Uint256, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint256{}, err - } - u, err := util.Uint256DecodeBytesBE(b) - if err != nil { - return util.Uint256{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Hash: %w", err) - } - - index++ - res.Version, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Version: %w", err) - } - - index++ - res.PrevHash, err = func(item stackitem.Item) (util.Uint256, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint256{}, err - } - u, err := util.Uint256DecodeBytesBE(b) - if err != nil { - return util.Uint256{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field PrevHash: %w", err) - } - - index++ - res.MerkleRoot, err = func(item stackitem.Item) (util.Uint256, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint256{}, err - } - u, err := util.Uint256DecodeBytesBE(b) - if err != nil { - return util.Uint256{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field MerkleRoot: %w", err) - } - - index++ - res.Timestamp, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Timestamp: %w", err) - } - - index++ - res.Nonce, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Nonce: %w", err) - } - - index++ - res.Index, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Index: %w", err) - } - - index++ - res.NextConsensus, err = func(item stackitem.Item) (util.Uint160, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint160{}, err - } - u, err := util.Uint160DecodeBytesBE(b) - if err != nil { - return util.Uint160{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field NextConsensus: %w", err) - } - - index++ - res.TransactionsLength, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field TransactionsLength: %w", err) - } - - index++ - res.PrevStateRoot, err = func(item stackitem.Item) (util.Uint256, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint256{}, err - } - u, err := util.Uint256DecodeBytesBE(b) - if err != nil { - return util.Uint256{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field PrevStateRoot: %w", err) - } - - return nil -} - -// itemToLedgerTransaction converts stack item into *LedgerTransaction. -func itemToLedgerTransaction(item stackitem.Item, err error) (*LedgerTransaction, error) { - if err != nil { - return nil, err - } - var res = new(LedgerTransaction) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of LedgerTransaction from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *LedgerTransaction) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 8 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Hash, err = func(item stackitem.Item) (util.Uint256, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint256{}, err - } - u, err := util.Uint256DecodeBytesBE(b) - if err != nil { - return util.Uint256{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Hash: %w", err) - } - - index++ - res.Version, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Version: %w", err) - } - - index++ - res.Nonce, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Nonce: %w", err) - } - - index++ - res.Sender, err = func(item stackitem.Item) (util.Uint160, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint160{}, err - } - u, err := util.Uint160DecodeBytesBE(b) - if err != nil { - return util.Uint160{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Sender: %w", err) - } - - index++ - res.SysFee, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field SysFee: %w", err) - } - - index++ - res.NetFee, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field NetFee: %w", err) - } - - index++ - res.ValidUntilBlock, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field ValidUntilBlock: %w", err) - } - - index++ - res.Script, err = arr[index].TryBytes() - if err != nil { - return fmt.Errorf("field Script: %w", err) - } - - return nil -} - -// itemToLedgerTransactionSigner converts stack item into *LedgerTransactionSigner. -func itemToLedgerTransactionSigner(item stackitem.Item, err error) (*LedgerTransactionSigner, error) { - if err != nil { - return nil, err - } - var res = new(LedgerTransactionSigner) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of LedgerTransactionSigner from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *LedgerTransactionSigner) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 5 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Account, err = func(item stackitem.Item) (util.Uint160, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint160{}, err - } - u, err := util.Uint160DecodeBytesBE(b) - if err != nil { - return util.Uint160{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Account: %w", err) - } - - index++ - res.Scopes, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Scopes: %w", err) - } - - index++ - res.AllowedContracts, err = func(item stackitem.Item) ([]util.Uint160, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make([]util.Uint160, len(arr)) - for i := range res { - res[i], err = func(item stackitem.Item) (util.Uint160, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint160{}, err - } - u, err := util.Uint160DecodeBytesBE(b) - if err != nil { - return util.Uint160{}, err - } - return u, nil - }(arr[i]) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field AllowedContracts: %w", err) - } - - index++ - res.AllowedGroups, err = func(item stackitem.Item) (keys.PublicKeys, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make(keys.PublicKeys, len(arr)) - for i := range res { - res[i], err = func(item stackitem.Item) (*keys.PublicKey, error) { - b, err := item.TryBytes() - if err != nil { - return nil, err - } - k, err := keys.NewPublicKeyFromBytes(b, elliptic.P256()) - if err != nil { - return nil, err - } - return k, nil - }(arr[i]) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field AllowedGroups: %w", err) - } - - index++ - res.Rules, err = func(item stackitem.Item) ([]*LedgerWitnessRule, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make([]*LedgerWitnessRule, len(arr)) - for i := range res { - res[i], err = itemToLedgerWitnessRule(arr[i], nil) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Rules: %w", err) - } - - return nil -} - -// itemToLedgerWitnessCondition converts stack item into *LedgerWitnessCondition. -func itemToLedgerWitnessCondition(item stackitem.Item, err error) (*LedgerWitnessCondition, error) { - if err != nil { - return nil, err - } - var res = new(LedgerWitnessCondition) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of LedgerWitnessCondition from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *LedgerWitnessCondition) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 2 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Type, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Type: %w", err) - } - - index++ - res.Value, err = arr[index].Value(), error(nil) - if err != nil { - return fmt.Errorf("field Value: %w", err) - } - - return nil -} - -// itemToLedgerWitnessRule converts stack item into *LedgerWitnessRule. -func itemToLedgerWitnessRule(item stackitem.Item, err error) (*LedgerWitnessRule, error) { - if err != nil { - return nil, err - } - var res = new(LedgerWitnessRule) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of LedgerWitnessRule from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *LedgerWitnessRule) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 2 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Action, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Action: %w", err) - } - - index++ - res.Condition, err = itemToLedgerWitnessCondition(arr[index], nil) - if err != nil { - return fmt.Errorf("field Condition: %w", err) - } - - return nil -} - -// itemToManagementABI converts stack item into *ManagementABI. -func itemToManagementABI(item stackitem.Item, err error) (*ManagementABI, error) { - if err != nil { - return nil, err - } - var res = new(ManagementABI) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of ManagementABI from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *ManagementABI) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 2 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Methods, err = func(item stackitem.Item) ([]*ManagementMethod, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make([]*ManagementMethod, len(arr)) - for i := range res { - res[i], err = itemToManagementMethod(arr[i], nil) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Methods: %w", err) - } - - index++ - res.Events, err = func(item stackitem.Item) ([]*ManagementEvent, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make([]*ManagementEvent, len(arr)) - for i := range res { - res[i], err = itemToManagementEvent(arr[i], nil) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Events: %w", err) - } - - return nil -} - -// itemToManagementContract converts stack item into *ManagementContract. -func itemToManagementContract(item stackitem.Item, err error) (*ManagementContract, error) { - if err != nil { - return nil, err - } - var res = new(ManagementContract) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of ManagementContract from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *ManagementContract) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 5 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.ID, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field ID: %w", err) - } - - index++ - res.UpdateCounter, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field UpdateCounter: %w", err) - } - - index++ - res.Hash, err = func(item stackitem.Item) (util.Uint160, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint160{}, err - } - u, err := util.Uint160DecodeBytesBE(b) - if err != nil { - return util.Uint160{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Hash: %w", err) - } - - index++ - res.NEF, err = arr[index].TryBytes() - if err != nil { - return fmt.Errorf("field NEF: %w", err) - } - - index++ - res.Manifest, err = itemToManagementManifest(arr[index], nil) - if err != nil { - return fmt.Errorf("field Manifest: %w", err) - } - - return nil -} - -// itemToManagementEvent converts stack item into *ManagementEvent. -func itemToManagementEvent(item stackitem.Item, err error) (*ManagementEvent, error) { - if err != nil { - return nil, err - } - var res = new(ManagementEvent) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of ManagementEvent from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *ManagementEvent) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 2 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Name, err = func(item stackitem.Item) (string, error) { - b, err := item.TryBytes() - if err != nil { - return "", err - } - if !utf8.Valid(b) { - return "", errors.New("not a UTF-8 string") - } - return string(b), nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Name: %w", err) - } - - index++ - res.Params, err = func(item stackitem.Item) ([]*ManagementParameter, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make([]*ManagementParameter, len(arr)) - for i := range res { - res[i], err = itemToManagementParameter(arr[i], nil) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Params: %w", err) - } - - return nil -} - -// itemToManagementGroup converts stack item into *ManagementGroup. -func itemToManagementGroup(item stackitem.Item, err error) (*ManagementGroup, error) { - if err != nil { - return nil, err - } - var res = new(ManagementGroup) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of ManagementGroup from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *ManagementGroup) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 2 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.PublicKey, err = func(item stackitem.Item) (*keys.PublicKey, error) { - b, err := item.TryBytes() - if err != nil { - return nil, err - } - k, err := keys.NewPublicKeyFromBytes(b, elliptic.P256()) - if err != nil { - return nil, err - } - return k, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field PublicKey: %w", err) - } - - index++ - res.Signature, err = arr[index].TryBytes() - if err != nil { - return fmt.Errorf("field Signature: %w", err) - } - - return nil -} - -// itemToManagementManifest converts stack item into *ManagementManifest. -func itemToManagementManifest(item stackitem.Item, err error) (*ManagementManifest, error) { - if err != nil { - return nil, err - } - var res = new(ManagementManifest) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of ManagementManifest from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *ManagementManifest) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 8 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Name, err = func(item stackitem.Item) (string, error) { - b, err := item.TryBytes() - if err != nil { - return "", err - } - if !utf8.Valid(b) { - return "", errors.New("not a UTF-8 string") - } - return string(b), nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Name: %w", err) - } - - index++ - res.Groups, err = func(item stackitem.Item) ([]*ManagementGroup, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make([]*ManagementGroup, len(arr)) - for i := range res { - res[i], err = itemToManagementGroup(arr[i], nil) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Groups: %w", err) - } - - index++ - res.Features, err = func(item stackitem.Item) (map[string]string, error) { - m, ok := item.Value().([]stackitem.MapElement) - if !ok { - return nil, fmt.Errorf("%s is not a map", item.Type().String()) - } - res := make(map[string]string) - for i := range m { - k, err := func(item stackitem.Item) (string, error) { - b, err := item.TryBytes() - if err != nil { - return "", err - } - if !utf8.Valid(b) { - return "", errors.New("not a UTF-8 string") - } - return string(b), nil - }(m[i].Key) - if err != nil { - return nil, fmt.Errorf("key %d: %w", i, err) - } - v, err := func(item stackitem.Item) (string, error) { - b, err := item.TryBytes() - if err != nil { - return "", err - } - if !utf8.Valid(b) { - return "", errors.New("not a UTF-8 string") - } - return string(b), nil - }(m[i].Value) - if err != nil { - return nil, fmt.Errorf("value %d: %w", i, err) - } - res[k] = v - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Features: %w", err) - } - - index++ - res.SupportedStandards, err = func(item stackitem.Item) ([]string, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make([]string, len(arr)) - for i := range res { - res[i], err = func(item stackitem.Item) (string, error) { - b, err := item.TryBytes() - if err != nil { - return "", err - } - if !utf8.Valid(b) { - return "", errors.New("not a UTF-8 string") - } - return string(b), nil - }(arr[i]) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field SupportedStandards: %w", err) - } - - index++ - res.ABI, err = itemToManagementABI(arr[index], nil) - if err != nil { - return fmt.Errorf("field ABI: %w", err) - } - - index++ - res.Permissions, err = func(item stackitem.Item) ([]*ManagementPermission, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make([]*ManagementPermission, len(arr)) - for i := range res { - res[i], err = itemToManagementPermission(arr[i], nil) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Permissions: %w", err) - } - - index++ - res.Trusts, err = func(item stackitem.Item) ([]util.Uint160, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make([]util.Uint160, len(arr)) - for i := range res { - res[i], err = func(item stackitem.Item) (util.Uint160, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint160{}, err - } - u, err := util.Uint160DecodeBytesBE(b) - if err != nil { - return util.Uint160{}, err - } - return u, nil - }(arr[i]) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Trusts: %w", err) - } - - index++ - res.Extra, err = arr[index].Value(), error(nil) - if err != nil { - return fmt.Errorf("field Extra: %w", err) - } - - return nil -} - -// itemToManagementMethod converts stack item into *ManagementMethod. -func itemToManagementMethod(item stackitem.Item, err error) (*ManagementMethod, error) { - if err != nil { - return nil, err - } - var res = new(ManagementMethod) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of ManagementMethod from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *ManagementMethod) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 5 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Name, err = func(item stackitem.Item) (string, error) { - b, err := item.TryBytes() - if err != nil { - return "", err - } - if !utf8.Valid(b) { - return "", errors.New("not a UTF-8 string") - } - return string(b), nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Name: %w", err) - } - - index++ - res.Params, err = func(item stackitem.Item) ([]*ManagementParameter, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make([]*ManagementParameter, len(arr)) - for i := range res { - res[i], err = itemToManagementParameter(arr[i], nil) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Params: %w", err) - } - - index++ - res.ReturnType, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field ReturnType: %w", err) - } - - index++ - res.Offset, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Offset: %w", err) - } - - index++ - res.Safe, err = arr[index].TryBool() - if err != nil { - return fmt.Errorf("field Safe: %w", err) - } - - return nil -} - -// itemToManagementParameter converts stack item into *ManagementParameter. -func itemToManagementParameter(item stackitem.Item, err error) (*ManagementParameter, error) { - if err != nil { - return nil, err - } - var res = new(ManagementParameter) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of ManagementParameter from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *ManagementParameter) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 2 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Name, err = func(item stackitem.Item) (string, error) { - b, err := item.TryBytes() - if err != nil { - return "", err - } - if !utf8.Valid(b) { - return "", errors.New("not a UTF-8 string") - } - return string(b), nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Name: %w", err) - } - - index++ - res.Type, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Type: %w", err) - } - - return nil -} - -// itemToManagementPermission converts stack item into *ManagementPermission. -func itemToManagementPermission(item stackitem.Item, err error) (*ManagementPermission, error) { - if err != nil { - return nil, err - } - var res = new(ManagementPermission) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of ManagementPermission from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *ManagementPermission) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 2 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Contract, err = func(item stackitem.Item) (util.Uint160, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint160{}, err - } - u, err := util.Uint160DecodeBytesBE(b) - if err != nil { - return util.Uint160{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Contract: %w", err) - } - - index++ - res.Methods, err = func(item stackitem.Item) ([]string, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make([]string, len(arr)) - for i := range res { - res[i], err = func(item stackitem.Item) (string, error) { - b, err := item.TryBytes() - if err != nil { - return "", err - } - if !utf8.Valid(b) { - return "", errors.New("not a UTF-8 string") - } - return string(b), nil - }(arr[i]) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Methods: %w", err) - } - - return nil -} - // itemToNeofsidUserInfo converts stack item into *NeofsidUserInfo. func itemToNeofsidUserInfo(item stackitem.Item, err error) (*NeofsidUserInfo, error) { if err != nil { diff --git a/rpc/netmap/rpcbinding.go b/rpc/netmap/rpcbinding.go index 55806a05..e593e7a1 100644 --- a/rpc/netmap/rpcbinding.go +++ b/rpc/netmap/rpcbinding.go @@ -14,7 +14,6 @@ import ( "github.com/nspcc-dev/neo-go/pkg/util" "github.com/nspcc-dev/neo-go/pkg/vm/stackitem" "math/big" - "unicode/utf8" ) // CommonBallot is a contract-specific common.Ballot type used by its methods. @@ -29,126 +28,6 @@ type CommonIRNode struct { PublicKey *keys.PublicKey } -// LedgerBlock is a contract-specific ledger.Block type used by its methods. -type LedgerBlock struct { - Hash util.Uint256 - Version *big.Int - PrevHash util.Uint256 - MerkleRoot util.Uint256 - Timestamp *big.Int - Nonce *big.Int - Index *big.Int - NextConsensus util.Uint160 - TransactionsLength *big.Int -} - -// LedgerBlockSR is a contract-specific ledger.BlockSR type used by its methods. -type LedgerBlockSR struct { - Hash util.Uint256 - Version *big.Int - PrevHash util.Uint256 - MerkleRoot util.Uint256 - Timestamp *big.Int - Nonce *big.Int - Index *big.Int - NextConsensus util.Uint160 - TransactionsLength *big.Int - PrevStateRoot util.Uint256 -} - -// LedgerTransaction is a contract-specific ledger.Transaction type used by its methods. -type LedgerTransaction struct { - Hash util.Uint256 - Version *big.Int - Nonce *big.Int - Sender util.Uint160 - SysFee *big.Int - NetFee *big.Int - ValidUntilBlock *big.Int - Script []byte -} - -// LedgerTransactionSigner is a contract-specific ledger.TransactionSigner type used by its methods. -type LedgerTransactionSigner struct { - Account util.Uint160 - Scopes *big.Int - AllowedContracts []util.Uint160 - AllowedGroups keys.PublicKeys - Rules []*LedgerWitnessRule -} - -// LedgerWitnessCondition is a contract-specific ledger.WitnessCondition type used by its methods. -type LedgerWitnessCondition struct { - Type *big.Int - Value any -} - -// LedgerWitnessRule is a contract-specific ledger.WitnessRule type used by its methods. -type LedgerWitnessRule struct { - Action *big.Int - Condition *LedgerWitnessCondition -} - -// ManagementABI is a contract-specific management.ABI type used by its methods. -type ManagementABI struct { - Methods []*ManagementMethod - Events []*ManagementEvent -} - -// ManagementContract is a contract-specific management.Contract type used by its methods. -type ManagementContract struct { - ID *big.Int - UpdateCounter *big.Int - Hash util.Uint160 - NEF []byte - Manifest *ManagementManifest -} - -// ManagementEvent is a contract-specific management.Event type used by its methods. -type ManagementEvent struct { - Name string - Params []*ManagementParameter -} - -// ManagementGroup is a contract-specific management.Group type used by its methods. -type ManagementGroup struct { - PublicKey *keys.PublicKey - Signature []byte -} - -// ManagementManifest is a contract-specific management.Manifest type used by its methods. -type ManagementManifest struct { - Name string - Groups []*ManagementGroup - Features map[string]string - SupportedStandards []string - ABI *ManagementABI - Permissions []*ManagementPermission - Trusts []util.Uint160 - Extra any -} - -// ManagementMethod is a contract-specific management.Method type used by its methods. -type ManagementMethod struct { - Name string - Params []*ManagementParameter - ReturnType *big.Int - Offset *big.Int - Safe bool -} - -// ManagementParameter is a contract-specific management.Parameter type used by its methods. -type ManagementParameter struct { - Name string - Type *big.Int -} - -// ManagementPermission is a contract-specific management.Permission type used by its methods. -type ManagementPermission struct { - Contract util.Uint160 - Methods []string -} - // NetmapNode is a contract-specific netmap.Node type used by its methods. type NetmapNode struct { BLOB []byte @@ -713,1208 +592,6 @@ func (res *CommonIRNode) FromStackItem(item stackitem.Item) error { return nil } -// itemToLedgerBlock converts stack item into *LedgerBlock. -func itemToLedgerBlock(item stackitem.Item, err error) (*LedgerBlock, error) { - if err != nil { - return nil, err - } - var res = new(LedgerBlock) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of LedgerBlock from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *LedgerBlock) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 9 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Hash, err = func(item stackitem.Item) (util.Uint256, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint256{}, err - } - u, err := util.Uint256DecodeBytesBE(b) - if err != nil { - return util.Uint256{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Hash: %w", err) - } - - index++ - res.Version, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Version: %w", err) - } - - index++ - res.PrevHash, err = func(item stackitem.Item) (util.Uint256, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint256{}, err - } - u, err := util.Uint256DecodeBytesBE(b) - if err != nil { - return util.Uint256{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field PrevHash: %w", err) - } - - index++ - res.MerkleRoot, err = func(item stackitem.Item) (util.Uint256, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint256{}, err - } - u, err := util.Uint256DecodeBytesBE(b) - if err != nil { - return util.Uint256{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field MerkleRoot: %w", err) - } - - index++ - res.Timestamp, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Timestamp: %w", err) - } - - index++ - res.Nonce, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Nonce: %w", err) - } - - index++ - res.Index, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Index: %w", err) - } - - index++ - res.NextConsensus, err = func(item stackitem.Item) (util.Uint160, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint160{}, err - } - u, err := util.Uint160DecodeBytesBE(b) - if err != nil { - return util.Uint160{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field NextConsensus: %w", err) - } - - index++ - res.TransactionsLength, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field TransactionsLength: %w", err) - } - - return nil -} - -// itemToLedgerBlockSR converts stack item into *LedgerBlockSR. -func itemToLedgerBlockSR(item stackitem.Item, err error) (*LedgerBlockSR, error) { - if err != nil { - return nil, err - } - var res = new(LedgerBlockSR) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of LedgerBlockSR from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *LedgerBlockSR) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 10 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Hash, err = func(item stackitem.Item) (util.Uint256, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint256{}, err - } - u, err := util.Uint256DecodeBytesBE(b) - if err != nil { - return util.Uint256{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Hash: %w", err) - } - - index++ - res.Version, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Version: %w", err) - } - - index++ - res.PrevHash, err = func(item stackitem.Item) (util.Uint256, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint256{}, err - } - u, err := util.Uint256DecodeBytesBE(b) - if err != nil { - return util.Uint256{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field PrevHash: %w", err) - } - - index++ - res.MerkleRoot, err = func(item stackitem.Item) (util.Uint256, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint256{}, err - } - u, err := util.Uint256DecodeBytesBE(b) - if err != nil { - return util.Uint256{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field MerkleRoot: %w", err) - } - - index++ - res.Timestamp, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Timestamp: %w", err) - } - - index++ - res.Nonce, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Nonce: %w", err) - } - - index++ - res.Index, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Index: %w", err) - } - - index++ - res.NextConsensus, err = func(item stackitem.Item) (util.Uint160, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint160{}, err - } - u, err := util.Uint160DecodeBytesBE(b) - if err != nil { - return util.Uint160{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field NextConsensus: %w", err) - } - - index++ - res.TransactionsLength, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field TransactionsLength: %w", err) - } - - index++ - res.PrevStateRoot, err = func(item stackitem.Item) (util.Uint256, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint256{}, err - } - u, err := util.Uint256DecodeBytesBE(b) - if err != nil { - return util.Uint256{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field PrevStateRoot: %w", err) - } - - return nil -} - -// itemToLedgerTransaction converts stack item into *LedgerTransaction. -func itemToLedgerTransaction(item stackitem.Item, err error) (*LedgerTransaction, error) { - if err != nil { - return nil, err - } - var res = new(LedgerTransaction) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of LedgerTransaction from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *LedgerTransaction) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 8 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Hash, err = func(item stackitem.Item) (util.Uint256, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint256{}, err - } - u, err := util.Uint256DecodeBytesBE(b) - if err != nil { - return util.Uint256{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Hash: %w", err) - } - - index++ - res.Version, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Version: %w", err) - } - - index++ - res.Nonce, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Nonce: %w", err) - } - - index++ - res.Sender, err = func(item stackitem.Item) (util.Uint160, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint160{}, err - } - u, err := util.Uint160DecodeBytesBE(b) - if err != nil { - return util.Uint160{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Sender: %w", err) - } - - index++ - res.SysFee, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field SysFee: %w", err) - } - - index++ - res.NetFee, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field NetFee: %w", err) - } - - index++ - res.ValidUntilBlock, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field ValidUntilBlock: %w", err) - } - - index++ - res.Script, err = arr[index].TryBytes() - if err != nil { - return fmt.Errorf("field Script: %w", err) - } - - return nil -} - -// itemToLedgerTransactionSigner converts stack item into *LedgerTransactionSigner. -func itemToLedgerTransactionSigner(item stackitem.Item, err error) (*LedgerTransactionSigner, error) { - if err != nil { - return nil, err - } - var res = new(LedgerTransactionSigner) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of LedgerTransactionSigner from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *LedgerTransactionSigner) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 5 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Account, err = func(item stackitem.Item) (util.Uint160, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint160{}, err - } - u, err := util.Uint160DecodeBytesBE(b) - if err != nil { - return util.Uint160{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Account: %w", err) - } - - index++ - res.Scopes, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Scopes: %w", err) - } - - index++ - res.AllowedContracts, err = func(item stackitem.Item) ([]util.Uint160, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make([]util.Uint160, len(arr)) - for i := range res { - res[i], err = func(item stackitem.Item) (util.Uint160, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint160{}, err - } - u, err := util.Uint160DecodeBytesBE(b) - if err != nil { - return util.Uint160{}, err - } - return u, nil - }(arr[i]) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field AllowedContracts: %w", err) - } - - index++ - res.AllowedGroups, err = func(item stackitem.Item) (keys.PublicKeys, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make(keys.PublicKeys, len(arr)) - for i := range res { - res[i], err = func(item stackitem.Item) (*keys.PublicKey, error) { - b, err := item.TryBytes() - if err != nil { - return nil, err - } - k, err := keys.NewPublicKeyFromBytes(b, elliptic.P256()) - if err != nil { - return nil, err - } - return k, nil - }(arr[i]) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field AllowedGroups: %w", err) - } - - index++ - res.Rules, err = func(item stackitem.Item) ([]*LedgerWitnessRule, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make([]*LedgerWitnessRule, len(arr)) - for i := range res { - res[i], err = itemToLedgerWitnessRule(arr[i], nil) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Rules: %w", err) - } - - return nil -} - -// itemToLedgerWitnessCondition converts stack item into *LedgerWitnessCondition. -func itemToLedgerWitnessCondition(item stackitem.Item, err error) (*LedgerWitnessCondition, error) { - if err != nil { - return nil, err - } - var res = new(LedgerWitnessCondition) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of LedgerWitnessCondition from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *LedgerWitnessCondition) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 2 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Type, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Type: %w", err) - } - - index++ - res.Value, err = arr[index].Value(), error(nil) - if err != nil { - return fmt.Errorf("field Value: %w", err) - } - - return nil -} - -// itemToLedgerWitnessRule converts stack item into *LedgerWitnessRule. -func itemToLedgerWitnessRule(item stackitem.Item, err error) (*LedgerWitnessRule, error) { - if err != nil { - return nil, err - } - var res = new(LedgerWitnessRule) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of LedgerWitnessRule from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *LedgerWitnessRule) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 2 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Action, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Action: %w", err) - } - - index++ - res.Condition, err = itemToLedgerWitnessCondition(arr[index], nil) - if err != nil { - return fmt.Errorf("field Condition: %w", err) - } - - return nil -} - -// itemToManagementABI converts stack item into *ManagementABI. -func itemToManagementABI(item stackitem.Item, err error) (*ManagementABI, error) { - if err != nil { - return nil, err - } - var res = new(ManagementABI) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of ManagementABI from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *ManagementABI) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 2 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Methods, err = func(item stackitem.Item) ([]*ManagementMethod, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make([]*ManagementMethod, len(arr)) - for i := range res { - res[i], err = itemToManagementMethod(arr[i], nil) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Methods: %w", err) - } - - index++ - res.Events, err = func(item stackitem.Item) ([]*ManagementEvent, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make([]*ManagementEvent, len(arr)) - for i := range res { - res[i], err = itemToManagementEvent(arr[i], nil) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Events: %w", err) - } - - return nil -} - -// itemToManagementContract converts stack item into *ManagementContract. -func itemToManagementContract(item stackitem.Item, err error) (*ManagementContract, error) { - if err != nil { - return nil, err - } - var res = new(ManagementContract) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of ManagementContract from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *ManagementContract) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 5 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.ID, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field ID: %w", err) - } - - index++ - res.UpdateCounter, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field UpdateCounter: %w", err) - } - - index++ - res.Hash, err = func(item stackitem.Item) (util.Uint160, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint160{}, err - } - u, err := util.Uint160DecodeBytesBE(b) - if err != nil { - return util.Uint160{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Hash: %w", err) - } - - index++ - res.NEF, err = arr[index].TryBytes() - if err != nil { - return fmt.Errorf("field NEF: %w", err) - } - - index++ - res.Manifest, err = itemToManagementManifest(arr[index], nil) - if err != nil { - return fmt.Errorf("field Manifest: %w", err) - } - - return nil -} - -// itemToManagementEvent converts stack item into *ManagementEvent. -func itemToManagementEvent(item stackitem.Item, err error) (*ManagementEvent, error) { - if err != nil { - return nil, err - } - var res = new(ManagementEvent) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of ManagementEvent from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *ManagementEvent) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 2 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Name, err = func(item stackitem.Item) (string, error) { - b, err := item.TryBytes() - if err != nil { - return "", err - } - if !utf8.Valid(b) { - return "", errors.New("not a UTF-8 string") - } - return string(b), nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Name: %w", err) - } - - index++ - res.Params, err = func(item stackitem.Item) ([]*ManagementParameter, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make([]*ManagementParameter, len(arr)) - for i := range res { - res[i], err = itemToManagementParameter(arr[i], nil) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Params: %w", err) - } - - return nil -} - -// itemToManagementGroup converts stack item into *ManagementGroup. -func itemToManagementGroup(item stackitem.Item, err error) (*ManagementGroup, error) { - if err != nil { - return nil, err - } - var res = new(ManagementGroup) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of ManagementGroup from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *ManagementGroup) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 2 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.PublicKey, err = func(item stackitem.Item) (*keys.PublicKey, error) { - b, err := item.TryBytes() - if err != nil { - return nil, err - } - k, err := keys.NewPublicKeyFromBytes(b, elliptic.P256()) - if err != nil { - return nil, err - } - return k, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field PublicKey: %w", err) - } - - index++ - res.Signature, err = arr[index].TryBytes() - if err != nil { - return fmt.Errorf("field Signature: %w", err) - } - - return nil -} - -// itemToManagementManifest converts stack item into *ManagementManifest. -func itemToManagementManifest(item stackitem.Item, err error) (*ManagementManifest, error) { - if err != nil { - return nil, err - } - var res = new(ManagementManifest) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of ManagementManifest from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *ManagementManifest) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 8 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Name, err = func(item stackitem.Item) (string, error) { - b, err := item.TryBytes() - if err != nil { - return "", err - } - if !utf8.Valid(b) { - return "", errors.New("not a UTF-8 string") - } - return string(b), nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Name: %w", err) - } - - index++ - res.Groups, err = func(item stackitem.Item) ([]*ManagementGroup, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make([]*ManagementGroup, len(arr)) - for i := range res { - res[i], err = itemToManagementGroup(arr[i], nil) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Groups: %w", err) - } - - index++ - res.Features, err = func(item stackitem.Item) (map[string]string, error) { - m, ok := item.Value().([]stackitem.MapElement) - if !ok { - return nil, fmt.Errorf("%s is not a map", item.Type().String()) - } - res := make(map[string]string) - for i := range m { - k, err := func(item stackitem.Item) (string, error) { - b, err := item.TryBytes() - if err != nil { - return "", err - } - if !utf8.Valid(b) { - return "", errors.New("not a UTF-8 string") - } - return string(b), nil - }(m[i].Key) - if err != nil { - return nil, fmt.Errorf("key %d: %w", i, err) - } - v, err := func(item stackitem.Item) (string, error) { - b, err := item.TryBytes() - if err != nil { - return "", err - } - if !utf8.Valid(b) { - return "", errors.New("not a UTF-8 string") - } - return string(b), nil - }(m[i].Value) - if err != nil { - return nil, fmt.Errorf("value %d: %w", i, err) - } - res[k] = v - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Features: %w", err) - } - - index++ - res.SupportedStandards, err = func(item stackitem.Item) ([]string, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make([]string, len(arr)) - for i := range res { - res[i], err = func(item stackitem.Item) (string, error) { - b, err := item.TryBytes() - if err != nil { - return "", err - } - if !utf8.Valid(b) { - return "", errors.New("not a UTF-8 string") - } - return string(b), nil - }(arr[i]) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field SupportedStandards: %w", err) - } - - index++ - res.ABI, err = itemToManagementABI(arr[index], nil) - if err != nil { - return fmt.Errorf("field ABI: %w", err) - } - - index++ - res.Permissions, err = func(item stackitem.Item) ([]*ManagementPermission, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make([]*ManagementPermission, len(arr)) - for i := range res { - res[i], err = itemToManagementPermission(arr[i], nil) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Permissions: %w", err) - } - - index++ - res.Trusts, err = func(item stackitem.Item) ([]util.Uint160, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make([]util.Uint160, len(arr)) - for i := range res { - res[i], err = func(item stackitem.Item) (util.Uint160, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint160{}, err - } - u, err := util.Uint160DecodeBytesBE(b) - if err != nil { - return util.Uint160{}, err - } - return u, nil - }(arr[i]) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Trusts: %w", err) - } - - index++ - res.Extra, err = arr[index].Value(), error(nil) - if err != nil { - return fmt.Errorf("field Extra: %w", err) - } - - return nil -} - -// itemToManagementMethod converts stack item into *ManagementMethod. -func itemToManagementMethod(item stackitem.Item, err error) (*ManagementMethod, error) { - if err != nil { - return nil, err - } - var res = new(ManagementMethod) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of ManagementMethod from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *ManagementMethod) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 5 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Name, err = func(item stackitem.Item) (string, error) { - b, err := item.TryBytes() - if err != nil { - return "", err - } - if !utf8.Valid(b) { - return "", errors.New("not a UTF-8 string") - } - return string(b), nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Name: %w", err) - } - - index++ - res.Params, err = func(item stackitem.Item) ([]*ManagementParameter, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make([]*ManagementParameter, len(arr)) - for i := range res { - res[i], err = itemToManagementParameter(arr[i], nil) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Params: %w", err) - } - - index++ - res.ReturnType, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field ReturnType: %w", err) - } - - index++ - res.Offset, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Offset: %w", err) - } - - index++ - res.Safe, err = arr[index].TryBool() - if err != nil { - return fmt.Errorf("field Safe: %w", err) - } - - return nil -} - -// itemToManagementParameter converts stack item into *ManagementParameter. -func itemToManagementParameter(item stackitem.Item, err error) (*ManagementParameter, error) { - if err != nil { - return nil, err - } - var res = new(ManagementParameter) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of ManagementParameter from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *ManagementParameter) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 2 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Name, err = func(item stackitem.Item) (string, error) { - b, err := item.TryBytes() - if err != nil { - return "", err - } - if !utf8.Valid(b) { - return "", errors.New("not a UTF-8 string") - } - return string(b), nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Name: %w", err) - } - - index++ - res.Type, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Type: %w", err) - } - - return nil -} - -// itemToManagementPermission converts stack item into *ManagementPermission. -func itemToManagementPermission(item stackitem.Item, err error) (*ManagementPermission, error) { - if err != nil { - return nil, err - } - var res = new(ManagementPermission) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of ManagementPermission from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *ManagementPermission) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 2 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Contract, err = func(item stackitem.Item) (util.Uint160, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint160{}, err - } - u, err := util.Uint160DecodeBytesBE(b) - if err != nil { - return util.Uint160{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Contract: %w", err) - } - - index++ - res.Methods, err = func(item stackitem.Item) ([]string, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make([]string, len(arr)) - for i := range res { - res[i], err = func(item stackitem.Item) (string, error) { - b, err := item.TryBytes() - if err != nil { - return "", err - } - if !utf8.Valid(b) { - return "", errors.New("not a UTF-8 string") - } - return string(b), nil - }(arr[i]) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Methods: %w", err) - } - - return nil -} - // itemToNetmapNode converts stack item into *NetmapNode. func itemToNetmapNode(item stackitem.Item, err error) (*NetmapNode, error) { if err != nil { diff --git a/rpc/nns/rpcbinding.go b/rpc/nns/rpcbinding.go index 09eaef40..f4fba461 100644 --- a/rpc/nns/rpcbinding.go +++ b/rpc/nns/rpcbinding.go @@ -4,12 +4,10 @@ package nns import ( - "crypto/elliptic" "errors" "fmt" "github.com/google/uuid" "github.com/nspcc-dev/neo-go/pkg/core/transaction" - "github.com/nspcc-dev/neo-go/pkg/crypto/keys" "github.com/nspcc-dev/neo-go/pkg/neorpc/result" "github.com/nspcc-dev/neo-go/pkg/rpcclient/nep11" "github.com/nspcc-dev/neo-go/pkg/rpcclient/unwrap" @@ -20,133 +18,6 @@ import ( "unicode/utf8" ) -// CommonBallot is a contract-specific common.Ballot type used by its methods. -type CommonBallot struct { - ID []byte - Voters keys.PublicKeys - Height *big.Int -} - -// LedgerBlock is a contract-specific ledger.Block type used by its methods. -type LedgerBlock struct { - Hash util.Uint256 - Version *big.Int - PrevHash util.Uint256 - MerkleRoot util.Uint256 - Timestamp *big.Int - Nonce *big.Int - Index *big.Int - NextConsensus util.Uint160 - TransactionsLength *big.Int -} - -// LedgerBlockSR is a contract-specific ledger.BlockSR type used by its methods. -type LedgerBlockSR struct { - Hash util.Uint256 - Version *big.Int - PrevHash util.Uint256 - MerkleRoot util.Uint256 - Timestamp *big.Int - Nonce *big.Int - Index *big.Int - NextConsensus util.Uint160 - TransactionsLength *big.Int - PrevStateRoot util.Uint256 -} - -// LedgerTransaction is a contract-specific ledger.Transaction type used by its methods. -type LedgerTransaction struct { - Hash util.Uint256 - Version *big.Int - Nonce *big.Int - Sender util.Uint160 - SysFee *big.Int - NetFee *big.Int - ValidUntilBlock *big.Int - Script []byte -} - -// LedgerTransactionSigner is a contract-specific ledger.TransactionSigner type used by its methods. -type LedgerTransactionSigner struct { - Account util.Uint160 - Scopes *big.Int - AllowedContracts []util.Uint160 - AllowedGroups keys.PublicKeys - Rules []*LedgerWitnessRule -} - -// LedgerWitnessCondition is a contract-specific ledger.WitnessCondition type used by its methods. -type LedgerWitnessCondition struct { - Type *big.Int - Value any -} - -// LedgerWitnessRule is a contract-specific ledger.WitnessRule type used by its methods. -type LedgerWitnessRule struct { - Action *big.Int - Condition *LedgerWitnessCondition -} - -// ManagementABI is a contract-specific management.ABI type used by its methods. -type ManagementABI struct { - Methods []*ManagementMethod - Events []*ManagementEvent -} - -// ManagementContract is a contract-specific management.Contract type used by its methods. -type ManagementContract struct { - ID *big.Int - UpdateCounter *big.Int - Hash util.Uint160 - NEF []byte - Manifest *ManagementManifest -} - -// ManagementEvent is a contract-specific management.Event type used by its methods. -type ManagementEvent struct { - Name string - Params []*ManagementParameter -} - -// ManagementGroup is a contract-specific management.Group type used by its methods. -type ManagementGroup struct { - PublicKey *keys.PublicKey - Signature []byte -} - -// ManagementManifest is a contract-specific management.Manifest type used by its methods. -type ManagementManifest struct { - Name string - Groups []*ManagementGroup - Features map[string]string - SupportedStandards []string - ABI *ManagementABI - Permissions []*ManagementPermission - Trusts []util.Uint160 - Extra any -} - -// ManagementMethod is a contract-specific management.Method type used by its methods. -type ManagementMethod struct { - Name string - Params []*ManagementParameter - ReturnType *big.Int - Offset *big.Int - Safe bool -} - -// ManagementParameter is a contract-specific management.Parameter type used by its methods. -type ManagementParameter struct { - Name string - Type *big.Int -} - -// ManagementPermission is a contract-specific management.Permission type used by its methods. -type ManagementPermission struct { - Contract util.Uint160 - Methods []string -} - // NnsNameState is a contract-specific nns.NameState type used by its methods. type NnsNameState struct { Owner util.Uint160 @@ -489,1277 +360,6 @@ func (c *Contract) UpdateSOAUnsigned(name string, email string, refresh *big.Int return c.actor.MakeUnsignedCall(c.hash, "updateSOA", nil, name, email, refresh, retry, expire, ttl) } -// itemToCommonBallot converts stack item into *CommonBallot. -func itemToCommonBallot(item stackitem.Item, err error) (*CommonBallot, error) { - if err != nil { - return nil, err - } - var res = new(CommonBallot) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of CommonBallot from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *CommonBallot) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 3 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.ID, err = arr[index].TryBytes() - if err != nil { - return fmt.Errorf("field ID: %w", err) - } - - index++ - res.Voters, err = func(item stackitem.Item) (keys.PublicKeys, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make(keys.PublicKeys, len(arr)) - for i := range res { - res[i], err = func(item stackitem.Item) (*keys.PublicKey, error) { - b, err := item.TryBytes() - if err != nil { - return nil, err - } - k, err := keys.NewPublicKeyFromBytes(b, elliptic.P256()) - if err != nil { - return nil, err - } - return k, nil - }(arr[i]) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Voters: %w", err) - } - - index++ - res.Height, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Height: %w", err) - } - - return nil -} - -// itemToLedgerBlock converts stack item into *LedgerBlock. -func itemToLedgerBlock(item stackitem.Item, err error) (*LedgerBlock, error) { - if err != nil { - return nil, err - } - var res = new(LedgerBlock) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of LedgerBlock from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *LedgerBlock) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 9 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Hash, err = func(item stackitem.Item) (util.Uint256, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint256{}, err - } - u, err := util.Uint256DecodeBytesBE(b) - if err != nil { - return util.Uint256{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Hash: %w", err) - } - - index++ - res.Version, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Version: %w", err) - } - - index++ - res.PrevHash, err = func(item stackitem.Item) (util.Uint256, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint256{}, err - } - u, err := util.Uint256DecodeBytesBE(b) - if err != nil { - return util.Uint256{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field PrevHash: %w", err) - } - - index++ - res.MerkleRoot, err = func(item stackitem.Item) (util.Uint256, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint256{}, err - } - u, err := util.Uint256DecodeBytesBE(b) - if err != nil { - return util.Uint256{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field MerkleRoot: %w", err) - } - - index++ - res.Timestamp, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Timestamp: %w", err) - } - - index++ - res.Nonce, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Nonce: %w", err) - } - - index++ - res.Index, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Index: %w", err) - } - - index++ - res.NextConsensus, err = func(item stackitem.Item) (util.Uint160, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint160{}, err - } - u, err := util.Uint160DecodeBytesBE(b) - if err != nil { - return util.Uint160{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field NextConsensus: %w", err) - } - - index++ - res.TransactionsLength, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field TransactionsLength: %w", err) - } - - return nil -} - -// itemToLedgerBlockSR converts stack item into *LedgerBlockSR. -func itemToLedgerBlockSR(item stackitem.Item, err error) (*LedgerBlockSR, error) { - if err != nil { - return nil, err - } - var res = new(LedgerBlockSR) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of LedgerBlockSR from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *LedgerBlockSR) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 10 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Hash, err = func(item stackitem.Item) (util.Uint256, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint256{}, err - } - u, err := util.Uint256DecodeBytesBE(b) - if err != nil { - return util.Uint256{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Hash: %w", err) - } - - index++ - res.Version, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Version: %w", err) - } - - index++ - res.PrevHash, err = func(item stackitem.Item) (util.Uint256, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint256{}, err - } - u, err := util.Uint256DecodeBytesBE(b) - if err != nil { - return util.Uint256{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field PrevHash: %w", err) - } - - index++ - res.MerkleRoot, err = func(item stackitem.Item) (util.Uint256, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint256{}, err - } - u, err := util.Uint256DecodeBytesBE(b) - if err != nil { - return util.Uint256{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field MerkleRoot: %w", err) - } - - index++ - res.Timestamp, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Timestamp: %w", err) - } - - index++ - res.Nonce, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Nonce: %w", err) - } - - index++ - res.Index, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Index: %w", err) - } - - index++ - res.NextConsensus, err = func(item stackitem.Item) (util.Uint160, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint160{}, err - } - u, err := util.Uint160DecodeBytesBE(b) - if err != nil { - return util.Uint160{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field NextConsensus: %w", err) - } - - index++ - res.TransactionsLength, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field TransactionsLength: %w", err) - } - - index++ - res.PrevStateRoot, err = func(item stackitem.Item) (util.Uint256, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint256{}, err - } - u, err := util.Uint256DecodeBytesBE(b) - if err != nil { - return util.Uint256{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field PrevStateRoot: %w", err) - } - - return nil -} - -// itemToLedgerTransaction converts stack item into *LedgerTransaction. -func itemToLedgerTransaction(item stackitem.Item, err error) (*LedgerTransaction, error) { - if err != nil { - return nil, err - } - var res = new(LedgerTransaction) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of LedgerTransaction from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *LedgerTransaction) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 8 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Hash, err = func(item stackitem.Item) (util.Uint256, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint256{}, err - } - u, err := util.Uint256DecodeBytesBE(b) - if err != nil { - return util.Uint256{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Hash: %w", err) - } - - index++ - res.Version, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Version: %w", err) - } - - index++ - res.Nonce, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Nonce: %w", err) - } - - index++ - res.Sender, err = func(item stackitem.Item) (util.Uint160, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint160{}, err - } - u, err := util.Uint160DecodeBytesBE(b) - if err != nil { - return util.Uint160{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Sender: %w", err) - } - - index++ - res.SysFee, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field SysFee: %w", err) - } - - index++ - res.NetFee, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field NetFee: %w", err) - } - - index++ - res.ValidUntilBlock, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field ValidUntilBlock: %w", err) - } - - index++ - res.Script, err = arr[index].TryBytes() - if err != nil { - return fmt.Errorf("field Script: %w", err) - } - - return nil -} - -// itemToLedgerTransactionSigner converts stack item into *LedgerTransactionSigner. -func itemToLedgerTransactionSigner(item stackitem.Item, err error) (*LedgerTransactionSigner, error) { - if err != nil { - return nil, err - } - var res = new(LedgerTransactionSigner) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of LedgerTransactionSigner from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *LedgerTransactionSigner) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 5 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Account, err = func(item stackitem.Item) (util.Uint160, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint160{}, err - } - u, err := util.Uint160DecodeBytesBE(b) - if err != nil { - return util.Uint160{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Account: %w", err) - } - - index++ - res.Scopes, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Scopes: %w", err) - } - - index++ - res.AllowedContracts, err = func(item stackitem.Item) ([]util.Uint160, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make([]util.Uint160, len(arr)) - for i := range res { - res[i], err = func(item stackitem.Item) (util.Uint160, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint160{}, err - } - u, err := util.Uint160DecodeBytesBE(b) - if err != nil { - return util.Uint160{}, err - } - return u, nil - }(arr[i]) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field AllowedContracts: %w", err) - } - - index++ - res.AllowedGroups, err = func(item stackitem.Item) (keys.PublicKeys, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make(keys.PublicKeys, len(arr)) - for i := range res { - res[i], err = func(item stackitem.Item) (*keys.PublicKey, error) { - b, err := item.TryBytes() - if err != nil { - return nil, err - } - k, err := keys.NewPublicKeyFromBytes(b, elliptic.P256()) - if err != nil { - return nil, err - } - return k, nil - }(arr[i]) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field AllowedGroups: %w", err) - } - - index++ - res.Rules, err = func(item stackitem.Item) ([]*LedgerWitnessRule, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make([]*LedgerWitnessRule, len(arr)) - for i := range res { - res[i], err = itemToLedgerWitnessRule(arr[i], nil) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Rules: %w", err) - } - - return nil -} - -// itemToLedgerWitnessCondition converts stack item into *LedgerWitnessCondition. -func itemToLedgerWitnessCondition(item stackitem.Item, err error) (*LedgerWitnessCondition, error) { - if err != nil { - return nil, err - } - var res = new(LedgerWitnessCondition) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of LedgerWitnessCondition from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *LedgerWitnessCondition) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 2 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Type, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Type: %w", err) - } - - index++ - res.Value, err = arr[index].Value(), error(nil) - if err != nil { - return fmt.Errorf("field Value: %w", err) - } - - return nil -} - -// itemToLedgerWitnessRule converts stack item into *LedgerWitnessRule. -func itemToLedgerWitnessRule(item stackitem.Item, err error) (*LedgerWitnessRule, error) { - if err != nil { - return nil, err - } - var res = new(LedgerWitnessRule) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of LedgerWitnessRule from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *LedgerWitnessRule) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 2 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Action, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Action: %w", err) - } - - index++ - res.Condition, err = itemToLedgerWitnessCondition(arr[index], nil) - if err != nil { - return fmt.Errorf("field Condition: %w", err) - } - - return nil -} - -// itemToManagementABI converts stack item into *ManagementABI. -func itemToManagementABI(item stackitem.Item, err error) (*ManagementABI, error) { - if err != nil { - return nil, err - } - var res = new(ManagementABI) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of ManagementABI from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *ManagementABI) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 2 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Methods, err = func(item stackitem.Item) ([]*ManagementMethod, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make([]*ManagementMethod, len(arr)) - for i := range res { - res[i], err = itemToManagementMethod(arr[i], nil) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Methods: %w", err) - } - - index++ - res.Events, err = func(item stackitem.Item) ([]*ManagementEvent, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make([]*ManagementEvent, len(arr)) - for i := range res { - res[i], err = itemToManagementEvent(arr[i], nil) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Events: %w", err) - } - - return nil -} - -// itemToManagementContract converts stack item into *ManagementContract. -func itemToManagementContract(item stackitem.Item, err error) (*ManagementContract, error) { - if err != nil { - return nil, err - } - var res = new(ManagementContract) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of ManagementContract from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *ManagementContract) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 5 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.ID, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field ID: %w", err) - } - - index++ - res.UpdateCounter, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field UpdateCounter: %w", err) - } - - index++ - res.Hash, err = func(item stackitem.Item) (util.Uint160, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint160{}, err - } - u, err := util.Uint160DecodeBytesBE(b) - if err != nil { - return util.Uint160{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Hash: %w", err) - } - - index++ - res.NEF, err = arr[index].TryBytes() - if err != nil { - return fmt.Errorf("field NEF: %w", err) - } - - index++ - res.Manifest, err = itemToManagementManifest(arr[index], nil) - if err != nil { - return fmt.Errorf("field Manifest: %w", err) - } - - return nil -} - -// itemToManagementEvent converts stack item into *ManagementEvent. -func itemToManagementEvent(item stackitem.Item, err error) (*ManagementEvent, error) { - if err != nil { - return nil, err - } - var res = new(ManagementEvent) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of ManagementEvent from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *ManagementEvent) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 2 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Name, err = func(item stackitem.Item) (string, error) { - b, err := item.TryBytes() - if err != nil { - return "", err - } - if !utf8.Valid(b) { - return "", errors.New("not a UTF-8 string") - } - return string(b), nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Name: %w", err) - } - - index++ - res.Params, err = func(item stackitem.Item) ([]*ManagementParameter, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make([]*ManagementParameter, len(arr)) - for i := range res { - res[i], err = itemToManagementParameter(arr[i], nil) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Params: %w", err) - } - - return nil -} - -// itemToManagementGroup converts stack item into *ManagementGroup. -func itemToManagementGroup(item stackitem.Item, err error) (*ManagementGroup, error) { - if err != nil { - return nil, err - } - var res = new(ManagementGroup) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of ManagementGroup from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *ManagementGroup) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 2 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.PublicKey, err = func(item stackitem.Item) (*keys.PublicKey, error) { - b, err := item.TryBytes() - if err != nil { - return nil, err - } - k, err := keys.NewPublicKeyFromBytes(b, elliptic.P256()) - if err != nil { - return nil, err - } - return k, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field PublicKey: %w", err) - } - - index++ - res.Signature, err = arr[index].TryBytes() - if err != nil { - return fmt.Errorf("field Signature: %w", err) - } - - return nil -} - -// itemToManagementManifest converts stack item into *ManagementManifest. -func itemToManagementManifest(item stackitem.Item, err error) (*ManagementManifest, error) { - if err != nil { - return nil, err - } - var res = new(ManagementManifest) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of ManagementManifest from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *ManagementManifest) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 8 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Name, err = func(item stackitem.Item) (string, error) { - b, err := item.TryBytes() - if err != nil { - return "", err - } - if !utf8.Valid(b) { - return "", errors.New("not a UTF-8 string") - } - return string(b), nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Name: %w", err) - } - - index++ - res.Groups, err = func(item stackitem.Item) ([]*ManagementGroup, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make([]*ManagementGroup, len(arr)) - for i := range res { - res[i], err = itemToManagementGroup(arr[i], nil) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Groups: %w", err) - } - - index++ - res.Features, err = func(item stackitem.Item) (map[string]string, error) { - m, ok := item.Value().([]stackitem.MapElement) - if !ok { - return nil, fmt.Errorf("%s is not a map", item.Type().String()) - } - res := make(map[string]string) - for i := range m { - k, err := func(item stackitem.Item) (string, error) { - b, err := item.TryBytes() - if err != nil { - return "", err - } - if !utf8.Valid(b) { - return "", errors.New("not a UTF-8 string") - } - return string(b), nil - }(m[i].Key) - if err != nil { - return nil, fmt.Errorf("key %d: %w", i, err) - } - v, err := func(item stackitem.Item) (string, error) { - b, err := item.TryBytes() - if err != nil { - return "", err - } - if !utf8.Valid(b) { - return "", errors.New("not a UTF-8 string") - } - return string(b), nil - }(m[i].Value) - if err != nil { - return nil, fmt.Errorf("value %d: %w", i, err) - } - res[k] = v - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Features: %w", err) - } - - index++ - res.SupportedStandards, err = func(item stackitem.Item) ([]string, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make([]string, len(arr)) - for i := range res { - res[i], err = func(item stackitem.Item) (string, error) { - b, err := item.TryBytes() - if err != nil { - return "", err - } - if !utf8.Valid(b) { - return "", errors.New("not a UTF-8 string") - } - return string(b), nil - }(arr[i]) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field SupportedStandards: %w", err) - } - - index++ - res.ABI, err = itemToManagementABI(arr[index], nil) - if err != nil { - return fmt.Errorf("field ABI: %w", err) - } - - index++ - res.Permissions, err = func(item stackitem.Item) ([]*ManagementPermission, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make([]*ManagementPermission, len(arr)) - for i := range res { - res[i], err = itemToManagementPermission(arr[i], nil) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Permissions: %w", err) - } - - index++ - res.Trusts, err = func(item stackitem.Item) ([]util.Uint160, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make([]util.Uint160, len(arr)) - for i := range res { - res[i], err = func(item stackitem.Item) (util.Uint160, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint160{}, err - } - u, err := util.Uint160DecodeBytesBE(b) - if err != nil { - return util.Uint160{}, err - } - return u, nil - }(arr[i]) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Trusts: %w", err) - } - - index++ - res.Extra, err = arr[index].Value(), error(nil) - if err != nil { - return fmt.Errorf("field Extra: %w", err) - } - - return nil -} - -// itemToManagementMethod converts stack item into *ManagementMethod. -func itemToManagementMethod(item stackitem.Item, err error) (*ManagementMethod, error) { - if err != nil { - return nil, err - } - var res = new(ManagementMethod) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of ManagementMethod from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *ManagementMethod) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 5 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Name, err = func(item stackitem.Item) (string, error) { - b, err := item.TryBytes() - if err != nil { - return "", err - } - if !utf8.Valid(b) { - return "", errors.New("not a UTF-8 string") - } - return string(b), nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Name: %w", err) - } - - index++ - res.Params, err = func(item stackitem.Item) ([]*ManagementParameter, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make([]*ManagementParameter, len(arr)) - for i := range res { - res[i], err = itemToManagementParameter(arr[i], nil) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Params: %w", err) - } - - index++ - res.ReturnType, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field ReturnType: %w", err) - } - - index++ - res.Offset, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Offset: %w", err) - } - - index++ - res.Safe, err = arr[index].TryBool() - if err != nil { - return fmt.Errorf("field Safe: %w", err) - } - - return nil -} - -// itemToManagementParameter converts stack item into *ManagementParameter. -func itemToManagementParameter(item stackitem.Item, err error) (*ManagementParameter, error) { - if err != nil { - return nil, err - } - var res = new(ManagementParameter) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of ManagementParameter from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *ManagementParameter) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 2 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Name, err = func(item stackitem.Item) (string, error) { - b, err := item.TryBytes() - if err != nil { - return "", err - } - if !utf8.Valid(b) { - return "", errors.New("not a UTF-8 string") - } - return string(b), nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Name: %w", err) - } - - index++ - res.Type, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Type: %w", err) - } - - return nil -} - -// itemToManagementPermission converts stack item into *ManagementPermission. -func itemToManagementPermission(item stackitem.Item, err error) (*ManagementPermission, error) { - if err != nil { - return nil, err - } - var res = new(ManagementPermission) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of ManagementPermission from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *ManagementPermission) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 2 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Contract, err = func(item stackitem.Item) (util.Uint160, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint160{}, err - } - u, err := util.Uint160DecodeBytesBE(b) - if err != nil { - return util.Uint160{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Contract: %w", err) - } - - index++ - res.Methods, err = func(item stackitem.Item) ([]string, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make([]string, len(arr)) - for i := range res { - res[i], err = func(item stackitem.Item) (string, error) { - b, err := item.TryBytes() - if err != nil { - return "", err - } - if !utf8.Valid(b) { - return "", errors.New("not a UTF-8 string") - } - return string(b), nil - }(arr[i]) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Methods: %w", err) - } - - return nil -} - // itemToNnsNameState converts stack item into *NnsNameState. func itemToNnsNameState(item stackitem.Item, err error) (*NnsNameState, error) { if err != nil { diff --git a/rpc/processing/rpcbinding.go b/rpc/processing/rpcbinding.go index 25b79536..2bd27b3f 100644 --- a/rpc/processing/rpcbinding.go +++ b/rpc/processing/rpcbinding.go @@ -4,146 +4,13 @@ package processing import ( - "crypto/elliptic" - "errors" - "fmt" "github.com/nspcc-dev/neo-go/pkg/core/transaction" - "github.com/nspcc-dev/neo-go/pkg/crypto/keys" "github.com/nspcc-dev/neo-go/pkg/neorpc/result" "github.com/nspcc-dev/neo-go/pkg/rpcclient/unwrap" "github.com/nspcc-dev/neo-go/pkg/util" - "github.com/nspcc-dev/neo-go/pkg/vm/stackitem" "math/big" - "unicode/utf8" ) -// CommonBallot is a contract-specific common.Ballot type used by its methods. -type CommonBallot struct { - ID []byte - Voters keys.PublicKeys - Height *big.Int -} - -// LedgerBlock is a contract-specific ledger.Block type used by its methods. -type LedgerBlock struct { - Hash util.Uint256 - Version *big.Int - PrevHash util.Uint256 - MerkleRoot util.Uint256 - Timestamp *big.Int - Nonce *big.Int - Index *big.Int - NextConsensus util.Uint160 - TransactionsLength *big.Int -} - -// LedgerBlockSR is a contract-specific ledger.BlockSR type used by its methods. -type LedgerBlockSR struct { - Hash util.Uint256 - Version *big.Int - PrevHash util.Uint256 - MerkleRoot util.Uint256 - Timestamp *big.Int - Nonce *big.Int - Index *big.Int - NextConsensus util.Uint160 - TransactionsLength *big.Int - PrevStateRoot util.Uint256 -} - -// LedgerTransaction is a contract-specific ledger.Transaction type used by its methods. -type LedgerTransaction struct { - Hash util.Uint256 - Version *big.Int - Nonce *big.Int - Sender util.Uint160 - SysFee *big.Int - NetFee *big.Int - ValidUntilBlock *big.Int - Script []byte -} - -// LedgerTransactionSigner is a contract-specific ledger.TransactionSigner type used by its methods. -type LedgerTransactionSigner struct { - Account util.Uint160 - Scopes *big.Int - AllowedContracts []util.Uint160 - AllowedGroups keys.PublicKeys - Rules []*LedgerWitnessRule -} - -// LedgerWitnessCondition is a contract-specific ledger.WitnessCondition type used by its methods. -type LedgerWitnessCondition struct { - Type *big.Int - Value any -} - -// LedgerWitnessRule is a contract-specific ledger.WitnessRule type used by its methods. -type LedgerWitnessRule struct { - Action *big.Int - Condition *LedgerWitnessCondition -} - -// ManagementABI is a contract-specific management.ABI type used by its methods. -type ManagementABI struct { - Methods []*ManagementMethod - Events []*ManagementEvent -} - -// ManagementContract is a contract-specific management.Contract type used by its methods. -type ManagementContract struct { - ID *big.Int - UpdateCounter *big.Int - Hash util.Uint160 - NEF []byte - Manifest *ManagementManifest -} - -// ManagementEvent is a contract-specific management.Event type used by its methods. -type ManagementEvent struct { - Name string - Params []*ManagementParameter -} - -// ManagementGroup is a contract-specific management.Group type used by its methods. -type ManagementGroup struct { - PublicKey *keys.PublicKey - Signature []byte -} - -// ManagementManifest is a contract-specific management.Manifest type used by its methods. -type ManagementManifest struct { - Name string - Groups []*ManagementGroup - Features map[string]string - SupportedStandards []string - ABI *ManagementABI - Permissions []*ManagementPermission - Trusts []util.Uint160 - Extra any -} - -// ManagementMethod is a contract-specific management.Method type used by its methods. -type ManagementMethod struct { - Name string - Params []*ManagementParameter - ReturnType *big.Int - Offset *big.Int - Safe bool -} - -// ManagementParameter is a contract-specific management.Parameter type used by its methods. -type ManagementParameter struct { - Name string - Type *big.Int -} - -// ManagementPermission is a contract-specific management.Permission type used by its methods. -type ManagementPermission struct { - Contract util.Uint160 - Methods []string -} - // Invoker is used by ContractReader to call various safe methods. type Invoker interface { Call(contract util.Uint160, operation string, params ...any) (*result.Invoke, error) @@ -215,1274 +82,3 @@ func (c *Contract) UpdateTransaction(script []byte, manifest []byte, data any) ( func (c *Contract) UpdateUnsigned(script []byte, manifest []byte, data any) (*transaction.Transaction, error) { return c.actor.MakeUnsignedCall(c.hash, "update", nil, script, manifest, data) } - -// itemToCommonBallot converts stack item into *CommonBallot. -func itemToCommonBallot(item stackitem.Item, err error) (*CommonBallot, error) { - if err != nil { - return nil, err - } - var res = new(CommonBallot) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of CommonBallot from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *CommonBallot) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 3 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.ID, err = arr[index].TryBytes() - if err != nil { - return fmt.Errorf("field ID: %w", err) - } - - index++ - res.Voters, err = func(item stackitem.Item) (keys.PublicKeys, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make(keys.PublicKeys, len(arr)) - for i := range res { - res[i], err = func(item stackitem.Item) (*keys.PublicKey, error) { - b, err := item.TryBytes() - if err != nil { - return nil, err - } - k, err := keys.NewPublicKeyFromBytes(b, elliptic.P256()) - if err != nil { - return nil, err - } - return k, nil - }(arr[i]) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Voters: %w", err) - } - - index++ - res.Height, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Height: %w", err) - } - - return nil -} - -// itemToLedgerBlock converts stack item into *LedgerBlock. -func itemToLedgerBlock(item stackitem.Item, err error) (*LedgerBlock, error) { - if err != nil { - return nil, err - } - var res = new(LedgerBlock) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of LedgerBlock from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *LedgerBlock) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 9 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Hash, err = func(item stackitem.Item) (util.Uint256, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint256{}, err - } - u, err := util.Uint256DecodeBytesBE(b) - if err != nil { - return util.Uint256{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Hash: %w", err) - } - - index++ - res.Version, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Version: %w", err) - } - - index++ - res.PrevHash, err = func(item stackitem.Item) (util.Uint256, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint256{}, err - } - u, err := util.Uint256DecodeBytesBE(b) - if err != nil { - return util.Uint256{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field PrevHash: %w", err) - } - - index++ - res.MerkleRoot, err = func(item stackitem.Item) (util.Uint256, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint256{}, err - } - u, err := util.Uint256DecodeBytesBE(b) - if err != nil { - return util.Uint256{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field MerkleRoot: %w", err) - } - - index++ - res.Timestamp, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Timestamp: %w", err) - } - - index++ - res.Nonce, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Nonce: %w", err) - } - - index++ - res.Index, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Index: %w", err) - } - - index++ - res.NextConsensus, err = func(item stackitem.Item) (util.Uint160, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint160{}, err - } - u, err := util.Uint160DecodeBytesBE(b) - if err != nil { - return util.Uint160{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field NextConsensus: %w", err) - } - - index++ - res.TransactionsLength, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field TransactionsLength: %w", err) - } - - return nil -} - -// itemToLedgerBlockSR converts stack item into *LedgerBlockSR. -func itemToLedgerBlockSR(item stackitem.Item, err error) (*LedgerBlockSR, error) { - if err != nil { - return nil, err - } - var res = new(LedgerBlockSR) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of LedgerBlockSR from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *LedgerBlockSR) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 10 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Hash, err = func(item stackitem.Item) (util.Uint256, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint256{}, err - } - u, err := util.Uint256DecodeBytesBE(b) - if err != nil { - return util.Uint256{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Hash: %w", err) - } - - index++ - res.Version, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Version: %w", err) - } - - index++ - res.PrevHash, err = func(item stackitem.Item) (util.Uint256, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint256{}, err - } - u, err := util.Uint256DecodeBytesBE(b) - if err != nil { - return util.Uint256{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field PrevHash: %w", err) - } - - index++ - res.MerkleRoot, err = func(item stackitem.Item) (util.Uint256, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint256{}, err - } - u, err := util.Uint256DecodeBytesBE(b) - if err != nil { - return util.Uint256{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field MerkleRoot: %w", err) - } - - index++ - res.Timestamp, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Timestamp: %w", err) - } - - index++ - res.Nonce, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Nonce: %w", err) - } - - index++ - res.Index, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Index: %w", err) - } - - index++ - res.NextConsensus, err = func(item stackitem.Item) (util.Uint160, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint160{}, err - } - u, err := util.Uint160DecodeBytesBE(b) - if err != nil { - return util.Uint160{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field NextConsensus: %w", err) - } - - index++ - res.TransactionsLength, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field TransactionsLength: %w", err) - } - - index++ - res.PrevStateRoot, err = func(item stackitem.Item) (util.Uint256, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint256{}, err - } - u, err := util.Uint256DecodeBytesBE(b) - if err != nil { - return util.Uint256{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field PrevStateRoot: %w", err) - } - - return nil -} - -// itemToLedgerTransaction converts stack item into *LedgerTransaction. -func itemToLedgerTransaction(item stackitem.Item, err error) (*LedgerTransaction, error) { - if err != nil { - return nil, err - } - var res = new(LedgerTransaction) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of LedgerTransaction from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *LedgerTransaction) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 8 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Hash, err = func(item stackitem.Item) (util.Uint256, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint256{}, err - } - u, err := util.Uint256DecodeBytesBE(b) - if err != nil { - return util.Uint256{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Hash: %w", err) - } - - index++ - res.Version, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Version: %w", err) - } - - index++ - res.Nonce, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Nonce: %w", err) - } - - index++ - res.Sender, err = func(item stackitem.Item) (util.Uint160, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint160{}, err - } - u, err := util.Uint160DecodeBytesBE(b) - if err != nil { - return util.Uint160{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Sender: %w", err) - } - - index++ - res.SysFee, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field SysFee: %w", err) - } - - index++ - res.NetFee, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field NetFee: %w", err) - } - - index++ - res.ValidUntilBlock, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field ValidUntilBlock: %w", err) - } - - index++ - res.Script, err = arr[index].TryBytes() - if err != nil { - return fmt.Errorf("field Script: %w", err) - } - - return nil -} - -// itemToLedgerTransactionSigner converts stack item into *LedgerTransactionSigner. -func itemToLedgerTransactionSigner(item stackitem.Item, err error) (*LedgerTransactionSigner, error) { - if err != nil { - return nil, err - } - var res = new(LedgerTransactionSigner) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of LedgerTransactionSigner from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *LedgerTransactionSigner) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 5 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Account, err = func(item stackitem.Item) (util.Uint160, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint160{}, err - } - u, err := util.Uint160DecodeBytesBE(b) - if err != nil { - return util.Uint160{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Account: %w", err) - } - - index++ - res.Scopes, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Scopes: %w", err) - } - - index++ - res.AllowedContracts, err = func(item stackitem.Item) ([]util.Uint160, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make([]util.Uint160, len(arr)) - for i := range res { - res[i], err = func(item stackitem.Item) (util.Uint160, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint160{}, err - } - u, err := util.Uint160DecodeBytesBE(b) - if err != nil { - return util.Uint160{}, err - } - return u, nil - }(arr[i]) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field AllowedContracts: %w", err) - } - - index++ - res.AllowedGroups, err = func(item stackitem.Item) (keys.PublicKeys, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make(keys.PublicKeys, len(arr)) - for i := range res { - res[i], err = func(item stackitem.Item) (*keys.PublicKey, error) { - b, err := item.TryBytes() - if err != nil { - return nil, err - } - k, err := keys.NewPublicKeyFromBytes(b, elliptic.P256()) - if err != nil { - return nil, err - } - return k, nil - }(arr[i]) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field AllowedGroups: %w", err) - } - - index++ - res.Rules, err = func(item stackitem.Item) ([]*LedgerWitnessRule, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make([]*LedgerWitnessRule, len(arr)) - for i := range res { - res[i], err = itemToLedgerWitnessRule(arr[i], nil) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Rules: %w", err) - } - - return nil -} - -// itemToLedgerWitnessCondition converts stack item into *LedgerWitnessCondition. -func itemToLedgerWitnessCondition(item stackitem.Item, err error) (*LedgerWitnessCondition, error) { - if err != nil { - return nil, err - } - var res = new(LedgerWitnessCondition) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of LedgerWitnessCondition from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *LedgerWitnessCondition) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 2 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Type, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Type: %w", err) - } - - index++ - res.Value, err = arr[index].Value(), error(nil) - if err != nil { - return fmt.Errorf("field Value: %w", err) - } - - return nil -} - -// itemToLedgerWitnessRule converts stack item into *LedgerWitnessRule. -func itemToLedgerWitnessRule(item stackitem.Item, err error) (*LedgerWitnessRule, error) { - if err != nil { - return nil, err - } - var res = new(LedgerWitnessRule) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of LedgerWitnessRule from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *LedgerWitnessRule) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 2 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Action, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Action: %w", err) - } - - index++ - res.Condition, err = itemToLedgerWitnessCondition(arr[index], nil) - if err != nil { - return fmt.Errorf("field Condition: %w", err) - } - - return nil -} - -// itemToManagementABI converts stack item into *ManagementABI. -func itemToManagementABI(item stackitem.Item, err error) (*ManagementABI, error) { - if err != nil { - return nil, err - } - var res = new(ManagementABI) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of ManagementABI from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *ManagementABI) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 2 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Methods, err = func(item stackitem.Item) ([]*ManagementMethod, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make([]*ManagementMethod, len(arr)) - for i := range res { - res[i], err = itemToManagementMethod(arr[i], nil) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Methods: %w", err) - } - - index++ - res.Events, err = func(item stackitem.Item) ([]*ManagementEvent, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make([]*ManagementEvent, len(arr)) - for i := range res { - res[i], err = itemToManagementEvent(arr[i], nil) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Events: %w", err) - } - - return nil -} - -// itemToManagementContract converts stack item into *ManagementContract. -func itemToManagementContract(item stackitem.Item, err error) (*ManagementContract, error) { - if err != nil { - return nil, err - } - var res = new(ManagementContract) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of ManagementContract from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *ManagementContract) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 5 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.ID, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field ID: %w", err) - } - - index++ - res.UpdateCounter, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field UpdateCounter: %w", err) - } - - index++ - res.Hash, err = func(item stackitem.Item) (util.Uint160, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint160{}, err - } - u, err := util.Uint160DecodeBytesBE(b) - if err != nil { - return util.Uint160{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Hash: %w", err) - } - - index++ - res.NEF, err = arr[index].TryBytes() - if err != nil { - return fmt.Errorf("field NEF: %w", err) - } - - index++ - res.Manifest, err = itemToManagementManifest(arr[index], nil) - if err != nil { - return fmt.Errorf("field Manifest: %w", err) - } - - return nil -} - -// itemToManagementEvent converts stack item into *ManagementEvent. -func itemToManagementEvent(item stackitem.Item, err error) (*ManagementEvent, error) { - if err != nil { - return nil, err - } - var res = new(ManagementEvent) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of ManagementEvent from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *ManagementEvent) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 2 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Name, err = func(item stackitem.Item) (string, error) { - b, err := item.TryBytes() - if err != nil { - return "", err - } - if !utf8.Valid(b) { - return "", errors.New("not a UTF-8 string") - } - return string(b), nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Name: %w", err) - } - - index++ - res.Params, err = func(item stackitem.Item) ([]*ManagementParameter, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make([]*ManagementParameter, len(arr)) - for i := range res { - res[i], err = itemToManagementParameter(arr[i], nil) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Params: %w", err) - } - - return nil -} - -// itemToManagementGroup converts stack item into *ManagementGroup. -func itemToManagementGroup(item stackitem.Item, err error) (*ManagementGroup, error) { - if err != nil { - return nil, err - } - var res = new(ManagementGroup) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of ManagementGroup from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *ManagementGroup) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 2 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.PublicKey, err = func(item stackitem.Item) (*keys.PublicKey, error) { - b, err := item.TryBytes() - if err != nil { - return nil, err - } - k, err := keys.NewPublicKeyFromBytes(b, elliptic.P256()) - if err != nil { - return nil, err - } - return k, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field PublicKey: %w", err) - } - - index++ - res.Signature, err = arr[index].TryBytes() - if err != nil { - return fmt.Errorf("field Signature: %w", err) - } - - return nil -} - -// itemToManagementManifest converts stack item into *ManagementManifest. -func itemToManagementManifest(item stackitem.Item, err error) (*ManagementManifest, error) { - if err != nil { - return nil, err - } - var res = new(ManagementManifest) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of ManagementManifest from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *ManagementManifest) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 8 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Name, err = func(item stackitem.Item) (string, error) { - b, err := item.TryBytes() - if err != nil { - return "", err - } - if !utf8.Valid(b) { - return "", errors.New("not a UTF-8 string") - } - return string(b), nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Name: %w", err) - } - - index++ - res.Groups, err = func(item stackitem.Item) ([]*ManagementGroup, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make([]*ManagementGroup, len(arr)) - for i := range res { - res[i], err = itemToManagementGroup(arr[i], nil) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Groups: %w", err) - } - - index++ - res.Features, err = func(item stackitem.Item) (map[string]string, error) { - m, ok := item.Value().([]stackitem.MapElement) - if !ok { - return nil, fmt.Errorf("%s is not a map", item.Type().String()) - } - res := make(map[string]string) - for i := range m { - k, err := func(item stackitem.Item) (string, error) { - b, err := item.TryBytes() - if err != nil { - return "", err - } - if !utf8.Valid(b) { - return "", errors.New("not a UTF-8 string") - } - return string(b), nil - }(m[i].Key) - if err != nil { - return nil, fmt.Errorf("key %d: %w", i, err) - } - v, err := func(item stackitem.Item) (string, error) { - b, err := item.TryBytes() - if err != nil { - return "", err - } - if !utf8.Valid(b) { - return "", errors.New("not a UTF-8 string") - } - return string(b), nil - }(m[i].Value) - if err != nil { - return nil, fmt.Errorf("value %d: %w", i, err) - } - res[k] = v - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Features: %w", err) - } - - index++ - res.SupportedStandards, err = func(item stackitem.Item) ([]string, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make([]string, len(arr)) - for i := range res { - res[i], err = func(item stackitem.Item) (string, error) { - b, err := item.TryBytes() - if err != nil { - return "", err - } - if !utf8.Valid(b) { - return "", errors.New("not a UTF-8 string") - } - return string(b), nil - }(arr[i]) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field SupportedStandards: %w", err) - } - - index++ - res.ABI, err = itemToManagementABI(arr[index], nil) - if err != nil { - return fmt.Errorf("field ABI: %w", err) - } - - index++ - res.Permissions, err = func(item stackitem.Item) ([]*ManagementPermission, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make([]*ManagementPermission, len(arr)) - for i := range res { - res[i], err = itemToManagementPermission(arr[i], nil) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Permissions: %w", err) - } - - index++ - res.Trusts, err = func(item stackitem.Item) ([]util.Uint160, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make([]util.Uint160, len(arr)) - for i := range res { - res[i], err = func(item stackitem.Item) (util.Uint160, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint160{}, err - } - u, err := util.Uint160DecodeBytesBE(b) - if err != nil { - return util.Uint160{}, err - } - return u, nil - }(arr[i]) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Trusts: %w", err) - } - - index++ - res.Extra, err = arr[index].Value(), error(nil) - if err != nil { - return fmt.Errorf("field Extra: %w", err) - } - - return nil -} - -// itemToManagementMethod converts stack item into *ManagementMethod. -func itemToManagementMethod(item stackitem.Item, err error) (*ManagementMethod, error) { - if err != nil { - return nil, err - } - var res = new(ManagementMethod) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of ManagementMethod from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *ManagementMethod) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 5 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Name, err = func(item stackitem.Item) (string, error) { - b, err := item.TryBytes() - if err != nil { - return "", err - } - if !utf8.Valid(b) { - return "", errors.New("not a UTF-8 string") - } - return string(b), nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Name: %w", err) - } - - index++ - res.Params, err = func(item stackitem.Item) ([]*ManagementParameter, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make([]*ManagementParameter, len(arr)) - for i := range res { - res[i], err = itemToManagementParameter(arr[i], nil) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Params: %w", err) - } - - index++ - res.ReturnType, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field ReturnType: %w", err) - } - - index++ - res.Offset, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Offset: %w", err) - } - - index++ - res.Safe, err = arr[index].TryBool() - if err != nil { - return fmt.Errorf("field Safe: %w", err) - } - - return nil -} - -// itemToManagementParameter converts stack item into *ManagementParameter. -func itemToManagementParameter(item stackitem.Item, err error) (*ManagementParameter, error) { - if err != nil { - return nil, err - } - var res = new(ManagementParameter) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of ManagementParameter from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *ManagementParameter) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 2 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Name, err = func(item stackitem.Item) (string, error) { - b, err := item.TryBytes() - if err != nil { - return "", err - } - if !utf8.Valid(b) { - return "", errors.New("not a UTF-8 string") - } - return string(b), nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Name: %w", err) - } - - index++ - res.Type, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Type: %w", err) - } - - return nil -} - -// itemToManagementPermission converts stack item into *ManagementPermission. -func itemToManagementPermission(item stackitem.Item, err error) (*ManagementPermission, error) { - if err != nil { - return nil, err - } - var res = new(ManagementPermission) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of ManagementPermission from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *ManagementPermission) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 2 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Contract, err = func(item stackitem.Item) (util.Uint160, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint160{}, err - } - u, err := util.Uint160DecodeBytesBE(b) - if err != nil { - return util.Uint160{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Contract: %w", err) - } - - index++ - res.Methods, err = func(item stackitem.Item) ([]string, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make([]string, len(arr)) - for i := range res { - res[i], err = func(item stackitem.Item) (string, error) { - b, err := item.TryBytes() - if err != nil { - return "", err - } - if !utf8.Valid(b) { - return "", errors.New("not a UTF-8 string") - } - return string(b), nil - }(arr[i]) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Methods: %w", err) - } - - return nil -} diff --git a/rpc/proxy/rpcbinding.go b/rpc/proxy/rpcbinding.go index 49859235..60465b11 100644 --- a/rpc/proxy/rpcbinding.go +++ b/rpc/proxy/rpcbinding.go @@ -4,146 +4,13 @@ package proxy import ( - "crypto/elliptic" - "errors" - "fmt" "github.com/nspcc-dev/neo-go/pkg/core/transaction" - "github.com/nspcc-dev/neo-go/pkg/crypto/keys" "github.com/nspcc-dev/neo-go/pkg/neorpc/result" "github.com/nspcc-dev/neo-go/pkg/rpcclient/unwrap" "github.com/nspcc-dev/neo-go/pkg/util" - "github.com/nspcc-dev/neo-go/pkg/vm/stackitem" "math/big" - "unicode/utf8" ) -// CommonBallot is a contract-specific common.Ballot type used by its methods. -type CommonBallot struct { - ID []byte - Voters keys.PublicKeys - Height *big.Int -} - -// LedgerBlock is a contract-specific ledger.Block type used by its methods. -type LedgerBlock struct { - Hash util.Uint256 - Version *big.Int - PrevHash util.Uint256 - MerkleRoot util.Uint256 - Timestamp *big.Int - Nonce *big.Int - Index *big.Int - NextConsensus util.Uint160 - TransactionsLength *big.Int -} - -// LedgerBlockSR is a contract-specific ledger.BlockSR type used by its methods. -type LedgerBlockSR struct { - Hash util.Uint256 - Version *big.Int - PrevHash util.Uint256 - MerkleRoot util.Uint256 - Timestamp *big.Int - Nonce *big.Int - Index *big.Int - NextConsensus util.Uint160 - TransactionsLength *big.Int - PrevStateRoot util.Uint256 -} - -// LedgerTransaction is a contract-specific ledger.Transaction type used by its methods. -type LedgerTransaction struct { - Hash util.Uint256 - Version *big.Int - Nonce *big.Int - Sender util.Uint160 - SysFee *big.Int - NetFee *big.Int - ValidUntilBlock *big.Int - Script []byte -} - -// LedgerTransactionSigner is a contract-specific ledger.TransactionSigner type used by its methods. -type LedgerTransactionSigner struct { - Account util.Uint160 - Scopes *big.Int - AllowedContracts []util.Uint160 - AllowedGroups keys.PublicKeys - Rules []*LedgerWitnessRule -} - -// LedgerWitnessCondition is a contract-specific ledger.WitnessCondition type used by its methods. -type LedgerWitnessCondition struct { - Type *big.Int - Value any -} - -// LedgerWitnessRule is a contract-specific ledger.WitnessRule type used by its methods. -type LedgerWitnessRule struct { - Action *big.Int - Condition *LedgerWitnessCondition -} - -// ManagementABI is a contract-specific management.ABI type used by its methods. -type ManagementABI struct { - Methods []*ManagementMethod - Events []*ManagementEvent -} - -// ManagementContract is a contract-specific management.Contract type used by its methods. -type ManagementContract struct { - ID *big.Int - UpdateCounter *big.Int - Hash util.Uint160 - NEF []byte - Manifest *ManagementManifest -} - -// ManagementEvent is a contract-specific management.Event type used by its methods. -type ManagementEvent struct { - Name string - Params []*ManagementParameter -} - -// ManagementGroup is a contract-specific management.Group type used by its methods. -type ManagementGroup struct { - PublicKey *keys.PublicKey - Signature []byte -} - -// ManagementManifest is a contract-specific management.Manifest type used by its methods. -type ManagementManifest struct { - Name string - Groups []*ManagementGroup - Features map[string]string - SupportedStandards []string - ABI *ManagementABI - Permissions []*ManagementPermission - Trusts []util.Uint160 - Extra any -} - -// ManagementMethod is a contract-specific management.Method type used by its methods. -type ManagementMethod struct { - Name string - Params []*ManagementParameter - ReturnType *big.Int - Offset *big.Int - Safe bool -} - -// ManagementParameter is a contract-specific management.Parameter type used by its methods. -type ManagementParameter struct { - Name string - Type *big.Int -} - -// ManagementPermission is a contract-specific management.Permission type used by its methods. -type ManagementPermission struct { - Contract util.Uint160 - Methods []string -} - // Invoker is used by ContractReader to call various safe methods. type Invoker interface { Call(contract util.Uint160, operation string, params ...any) (*result.Invoke, error) @@ -215,1274 +82,3 @@ func (c *Contract) UpdateTransaction(script []byte, manifest []byte, data any) ( func (c *Contract) UpdateUnsigned(script []byte, manifest []byte, data any) (*transaction.Transaction, error) { return c.actor.MakeUnsignedCall(c.hash, "update", nil, script, manifest, data) } - -// itemToCommonBallot converts stack item into *CommonBallot. -func itemToCommonBallot(item stackitem.Item, err error) (*CommonBallot, error) { - if err != nil { - return nil, err - } - var res = new(CommonBallot) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of CommonBallot from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *CommonBallot) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 3 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.ID, err = arr[index].TryBytes() - if err != nil { - return fmt.Errorf("field ID: %w", err) - } - - index++ - res.Voters, err = func(item stackitem.Item) (keys.PublicKeys, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make(keys.PublicKeys, len(arr)) - for i := range res { - res[i], err = func(item stackitem.Item) (*keys.PublicKey, error) { - b, err := item.TryBytes() - if err != nil { - return nil, err - } - k, err := keys.NewPublicKeyFromBytes(b, elliptic.P256()) - if err != nil { - return nil, err - } - return k, nil - }(arr[i]) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Voters: %w", err) - } - - index++ - res.Height, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Height: %w", err) - } - - return nil -} - -// itemToLedgerBlock converts stack item into *LedgerBlock. -func itemToLedgerBlock(item stackitem.Item, err error) (*LedgerBlock, error) { - if err != nil { - return nil, err - } - var res = new(LedgerBlock) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of LedgerBlock from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *LedgerBlock) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 9 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Hash, err = func(item stackitem.Item) (util.Uint256, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint256{}, err - } - u, err := util.Uint256DecodeBytesBE(b) - if err != nil { - return util.Uint256{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Hash: %w", err) - } - - index++ - res.Version, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Version: %w", err) - } - - index++ - res.PrevHash, err = func(item stackitem.Item) (util.Uint256, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint256{}, err - } - u, err := util.Uint256DecodeBytesBE(b) - if err != nil { - return util.Uint256{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field PrevHash: %w", err) - } - - index++ - res.MerkleRoot, err = func(item stackitem.Item) (util.Uint256, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint256{}, err - } - u, err := util.Uint256DecodeBytesBE(b) - if err != nil { - return util.Uint256{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field MerkleRoot: %w", err) - } - - index++ - res.Timestamp, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Timestamp: %w", err) - } - - index++ - res.Nonce, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Nonce: %w", err) - } - - index++ - res.Index, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Index: %w", err) - } - - index++ - res.NextConsensus, err = func(item stackitem.Item) (util.Uint160, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint160{}, err - } - u, err := util.Uint160DecodeBytesBE(b) - if err != nil { - return util.Uint160{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field NextConsensus: %w", err) - } - - index++ - res.TransactionsLength, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field TransactionsLength: %w", err) - } - - return nil -} - -// itemToLedgerBlockSR converts stack item into *LedgerBlockSR. -func itemToLedgerBlockSR(item stackitem.Item, err error) (*LedgerBlockSR, error) { - if err != nil { - return nil, err - } - var res = new(LedgerBlockSR) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of LedgerBlockSR from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *LedgerBlockSR) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 10 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Hash, err = func(item stackitem.Item) (util.Uint256, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint256{}, err - } - u, err := util.Uint256DecodeBytesBE(b) - if err != nil { - return util.Uint256{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Hash: %w", err) - } - - index++ - res.Version, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Version: %w", err) - } - - index++ - res.PrevHash, err = func(item stackitem.Item) (util.Uint256, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint256{}, err - } - u, err := util.Uint256DecodeBytesBE(b) - if err != nil { - return util.Uint256{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field PrevHash: %w", err) - } - - index++ - res.MerkleRoot, err = func(item stackitem.Item) (util.Uint256, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint256{}, err - } - u, err := util.Uint256DecodeBytesBE(b) - if err != nil { - return util.Uint256{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field MerkleRoot: %w", err) - } - - index++ - res.Timestamp, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Timestamp: %w", err) - } - - index++ - res.Nonce, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Nonce: %w", err) - } - - index++ - res.Index, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Index: %w", err) - } - - index++ - res.NextConsensus, err = func(item stackitem.Item) (util.Uint160, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint160{}, err - } - u, err := util.Uint160DecodeBytesBE(b) - if err != nil { - return util.Uint160{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field NextConsensus: %w", err) - } - - index++ - res.TransactionsLength, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field TransactionsLength: %w", err) - } - - index++ - res.PrevStateRoot, err = func(item stackitem.Item) (util.Uint256, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint256{}, err - } - u, err := util.Uint256DecodeBytesBE(b) - if err != nil { - return util.Uint256{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field PrevStateRoot: %w", err) - } - - return nil -} - -// itemToLedgerTransaction converts stack item into *LedgerTransaction. -func itemToLedgerTransaction(item stackitem.Item, err error) (*LedgerTransaction, error) { - if err != nil { - return nil, err - } - var res = new(LedgerTransaction) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of LedgerTransaction from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *LedgerTransaction) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 8 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Hash, err = func(item stackitem.Item) (util.Uint256, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint256{}, err - } - u, err := util.Uint256DecodeBytesBE(b) - if err != nil { - return util.Uint256{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Hash: %w", err) - } - - index++ - res.Version, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Version: %w", err) - } - - index++ - res.Nonce, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Nonce: %w", err) - } - - index++ - res.Sender, err = func(item stackitem.Item) (util.Uint160, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint160{}, err - } - u, err := util.Uint160DecodeBytesBE(b) - if err != nil { - return util.Uint160{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Sender: %w", err) - } - - index++ - res.SysFee, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field SysFee: %w", err) - } - - index++ - res.NetFee, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field NetFee: %w", err) - } - - index++ - res.ValidUntilBlock, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field ValidUntilBlock: %w", err) - } - - index++ - res.Script, err = arr[index].TryBytes() - if err != nil { - return fmt.Errorf("field Script: %w", err) - } - - return nil -} - -// itemToLedgerTransactionSigner converts stack item into *LedgerTransactionSigner. -func itemToLedgerTransactionSigner(item stackitem.Item, err error) (*LedgerTransactionSigner, error) { - if err != nil { - return nil, err - } - var res = new(LedgerTransactionSigner) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of LedgerTransactionSigner from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *LedgerTransactionSigner) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 5 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Account, err = func(item stackitem.Item) (util.Uint160, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint160{}, err - } - u, err := util.Uint160DecodeBytesBE(b) - if err != nil { - return util.Uint160{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Account: %w", err) - } - - index++ - res.Scopes, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Scopes: %w", err) - } - - index++ - res.AllowedContracts, err = func(item stackitem.Item) ([]util.Uint160, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make([]util.Uint160, len(arr)) - for i := range res { - res[i], err = func(item stackitem.Item) (util.Uint160, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint160{}, err - } - u, err := util.Uint160DecodeBytesBE(b) - if err != nil { - return util.Uint160{}, err - } - return u, nil - }(arr[i]) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field AllowedContracts: %w", err) - } - - index++ - res.AllowedGroups, err = func(item stackitem.Item) (keys.PublicKeys, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make(keys.PublicKeys, len(arr)) - for i := range res { - res[i], err = func(item stackitem.Item) (*keys.PublicKey, error) { - b, err := item.TryBytes() - if err != nil { - return nil, err - } - k, err := keys.NewPublicKeyFromBytes(b, elliptic.P256()) - if err != nil { - return nil, err - } - return k, nil - }(arr[i]) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field AllowedGroups: %w", err) - } - - index++ - res.Rules, err = func(item stackitem.Item) ([]*LedgerWitnessRule, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make([]*LedgerWitnessRule, len(arr)) - for i := range res { - res[i], err = itemToLedgerWitnessRule(arr[i], nil) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Rules: %w", err) - } - - return nil -} - -// itemToLedgerWitnessCondition converts stack item into *LedgerWitnessCondition. -func itemToLedgerWitnessCondition(item stackitem.Item, err error) (*LedgerWitnessCondition, error) { - if err != nil { - return nil, err - } - var res = new(LedgerWitnessCondition) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of LedgerWitnessCondition from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *LedgerWitnessCondition) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 2 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Type, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Type: %w", err) - } - - index++ - res.Value, err = arr[index].Value(), error(nil) - if err != nil { - return fmt.Errorf("field Value: %w", err) - } - - return nil -} - -// itemToLedgerWitnessRule converts stack item into *LedgerWitnessRule. -func itemToLedgerWitnessRule(item stackitem.Item, err error) (*LedgerWitnessRule, error) { - if err != nil { - return nil, err - } - var res = new(LedgerWitnessRule) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of LedgerWitnessRule from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *LedgerWitnessRule) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 2 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Action, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Action: %w", err) - } - - index++ - res.Condition, err = itemToLedgerWitnessCondition(arr[index], nil) - if err != nil { - return fmt.Errorf("field Condition: %w", err) - } - - return nil -} - -// itemToManagementABI converts stack item into *ManagementABI. -func itemToManagementABI(item stackitem.Item, err error) (*ManagementABI, error) { - if err != nil { - return nil, err - } - var res = new(ManagementABI) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of ManagementABI from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *ManagementABI) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 2 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Methods, err = func(item stackitem.Item) ([]*ManagementMethod, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make([]*ManagementMethod, len(arr)) - for i := range res { - res[i], err = itemToManagementMethod(arr[i], nil) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Methods: %w", err) - } - - index++ - res.Events, err = func(item stackitem.Item) ([]*ManagementEvent, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make([]*ManagementEvent, len(arr)) - for i := range res { - res[i], err = itemToManagementEvent(arr[i], nil) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Events: %w", err) - } - - return nil -} - -// itemToManagementContract converts stack item into *ManagementContract. -func itemToManagementContract(item stackitem.Item, err error) (*ManagementContract, error) { - if err != nil { - return nil, err - } - var res = new(ManagementContract) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of ManagementContract from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *ManagementContract) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 5 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.ID, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field ID: %w", err) - } - - index++ - res.UpdateCounter, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field UpdateCounter: %w", err) - } - - index++ - res.Hash, err = func(item stackitem.Item) (util.Uint160, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint160{}, err - } - u, err := util.Uint160DecodeBytesBE(b) - if err != nil { - return util.Uint160{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Hash: %w", err) - } - - index++ - res.NEF, err = arr[index].TryBytes() - if err != nil { - return fmt.Errorf("field NEF: %w", err) - } - - index++ - res.Manifest, err = itemToManagementManifest(arr[index], nil) - if err != nil { - return fmt.Errorf("field Manifest: %w", err) - } - - return nil -} - -// itemToManagementEvent converts stack item into *ManagementEvent. -func itemToManagementEvent(item stackitem.Item, err error) (*ManagementEvent, error) { - if err != nil { - return nil, err - } - var res = new(ManagementEvent) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of ManagementEvent from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *ManagementEvent) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 2 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Name, err = func(item stackitem.Item) (string, error) { - b, err := item.TryBytes() - if err != nil { - return "", err - } - if !utf8.Valid(b) { - return "", errors.New("not a UTF-8 string") - } - return string(b), nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Name: %w", err) - } - - index++ - res.Params, err = func(item stackitem.Item) ([]*ManagementParameter, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make([]*ManagementParameter, len(arr)) - for i := range res { - res[i], err = itemToManagementParameter(arr[i], nil) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Params: %w", err) - } - - return nil -} - -// itemToManagementGroup converts stack item into *ManagementGroup. -func itemToManagementGroup(item stackitem.Item, err error) (*ManagementGroup, error) { - if err != nil { - return nil, err - } - var res = new(ManagementGroup) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of ManagementGroup from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *ManagementGroup) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 2 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.PublicKey, err = func(item stackitem.Item) (*keys.PublicKey, error) { - b, err := item.TryBytes() - if err != nil { - return nil, err - } - k, err := keys.NewPublicKeyFromBytes(b, elliptic.P256()) - if err != nil { - return nil, err - } - return k, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field PublicKey: %w", err) - } - - index++ - res.Signature, err = arr[index].TryBytes() - if err != nil { - return fmt.Errorf("field Signature: %w", err) - } - - return nil -} - -// itemToManagementManifest converts stack item into *ManagementManifest. -func itemToManagementManifest(item stackitem.Item, err error) (*ManagementManifest, error) { - if err != nil { - return nil, err - } - var res = new(ManagementManifest) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of ManagementManifest from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *ManagementManifest) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 8 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Name, err = func(item stackitem.Item) (string, error) { - b, err := item.TryBytes() - if err != nil { - return "", err - } - if !utf8.Valid(b) { - return "", errors.New("not a UTF-8 string") - } - return string(b), nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Name: %w", err) - } - - index++ - res.Groups, err = func(item stackitem.Item) ([]*ManagementGroup, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make([]*ManagementGroup, len(arr)) - for i := range res { - res[i], err = itemToManagementGroup(arr[i], nil) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Groups: %w", err) - } - - index++ - res.Features, err = func(item stackitem.Item) (map[string]string, error) { - m, ok := item.Value().([]stackitem.MapElement) - if !ok { - return nil, fmt.Errorf("%s is not a map", item.Type().String()) - } - res := make(map[string]string) - for i := range m { - k, err := func(item stackitem.Item) (string, error) { - b, err := item.TryBytes() - if err != nil { - return "", err - } - if !utf8.Valid(b) { - return "", errors.New("not a UTF-8 string") - } - return string(b), nil - }(m[i].Key) - if err != nil { - return nil, fmt.Errorf("key %d: %w", i, err) - } - v, err := func(item stackitem.Item) (string, error) { - b, err := item.TryBytes() - if err != nil { - return "", err - } - if !utf8.Valid(b) { - return "", errors.New("not a UTF-8 string") - } - return string(b), nil - }(m[i].Value) - if err != nil { - return nil, fmt.Errorf("value %d: %w", i, err) - } - res[k] = v - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Features: %w", err) - } - - index++ - res.SupportedStandards, err = func(item stackitem.Item) ([]string, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make([]string, len(arr)) - for i := range res { - res[i], err = func(item stackitem.Item) (string, error) { - b, err := item.TryBytes() - if err != nil { - return "", err - } - if !utf8.Valid(b) { - return "", errors.New("not a UTF-8 string") - } - return string(b), nil - }(arr[i]) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field SupportedStandards: %w", err) - } - - index++ - res.ABI, err = itemToManagementABI(arr[index], nil) - if err != nil { - return fmt.Errorf("field ABI: %w", err) - } - - index++ - res.Permissions, err = func(item stackitem.Item) ([]*ManagementPermission, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make([]*ManagementPermission, len(arr)) - for i := range res { - res[i], err = itemToManagementPermission(arr[i], nil) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Permissions: %w", err) - } - - index++ - res.Trusts, err = func(item stackitem.Item) ([]util.Uint160, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make([]util.Uint160, len(arr)) - for i := range res { - res[i], err = func(item stackitem.Item) (util.Uint160, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint160{}, err - } - u, err := util.Uint160DecodeBytesBE(b) - if err != nil { - return util.Uint160{}, err - } - return u, nil - }(arr[i]) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Trusts: %w", err) - } - - index++ - res.Extra, err = arr[index].Value(), error(nil) - if err != nil { - return fmt.Errorf("field Extra: %w", err) - } - - return nil -} - -// itemToManagementMethod converts stack item into *ManagementMethod. -func itemToManagementMethod(item stackitem.Item, err error) (*ManagementMethod, error) { - if err != nil { - return nil, err - } - var res = new(ManagementMethod) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of ManagementMethod from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *ManagementMethod) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 5 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Name, err = func(item stackitem.Item) (string, error) { - b, err := item.TryBytes() - if err != nil { - return "", err - } - if !utf8.Valid(b) { - return "", errors.New("not a UTF-8 string") - } - return string(b), nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Name: %w", err) - } - - index++ - res.Params, err = func(item stackitem.Item) ([]*ManagementParameter, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make([]*ManagementParameter, len(arr)) - for i := range res { - res[i], err = itemToManagementParameter(arr[i], nil) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Params: %w", err) - } - - index++ - res.ReturnType, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field ReturnType: %w", err) - } - - index++ - res.Offset, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Offset: %w", err) - } - - index++ - res.Safe, err = arr[index].TryBool() - if err != nil { - return fmt.Errorf("field Safe: %w", err) - } - - return nil -} - -// itemToManagementParameter converts stack item into *ManagementParameter. -func itemToManagementParameter(item stackitem.Item, err error) (*ManagementParameter, error) { - if err != nil { - return nil, err - } - var res = new(ManagementParameter) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of ManagementParameter from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *ManagementParameter) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 2 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Name, err = func(item stackitem.Item) (string, error) { - b, err := item.TryBytes() - if err != nil { - return "", err - } - if !utf8.Valid(b) { - return "", errors.New("not a UTF-8 string") - } - return string(b), nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Name: %w", err) - } - - index++ - res.Type, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Type: %w", err) - } - - return nil -} - -// itemToManagementPermission converts stack item into *ManagementPermission. -func itemToManagementPermission(item stackitem.Item, err error) (*ManagementPermission, error) { - if err != nil { - return nil, err - } - var res = new(ManagementPermission) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of ManagementPermission from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *ManagementPermission) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 2 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Contract, err = func(item stackitem.Item) (util.Uint160, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint160{}, err - } - u, err := util.Uint160DecodeBytesBE(b) - if err != nil { - return util.Uint160{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Contract: %w", err) - } - - index++ - res.Methods, err = func(item stackitem.Item) ([]string, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make([]string, len(arr)) - for i := range res { - res[i], err = func(item stackitem.Item) (string, error) { - b, err := item.TryBytes() - if err != nil { - return "", err - } - if !utf8.Valid(b) { - return "", errors.New("not a UTF-8 string") - } - return string(b), nil - }(arr[i]) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Methods: %w", err) - } - - return nil -} diff --git a/rpc/reputation/rpcbinding.go b/rpc/reputation/rpcbinding.go index 6d5383b8..e86c75ca 100644 --- a/rpc/reputation/rpcbinding.go +++ b/rpc/reputation/rpcbinding.go @@ -14,7 +14,6 @@ import ( "github.com/nspcc-dev/neo-go/pkg/util" "github.com/nspcc-dev/neo-go/pkg/vm/stackitem" "math/big" - "unicode/utf8" ) // CommonBallot is a contract-specific common.Ballot type used by its methods. @@ -24,126 +23,6 @@ type CommonBallot struct { Height *big.Int } -// LedgerBlock is a contract-specific ledger.Block type used by its methods. -type LedgerBlock struct { - Hash util.Uint256 - Version *big.Int - PrevHash util.Uint256 - MerkleRoot util.Uint256 - Timestamp *big.Int - Nonce *big.Int - Index *big.Int - NextConsensus util.Uint160 - TransactionsLength *big.Int -} - -// LedgerBlockSR is a contract-specific ledger.BlockSR type used by its methods. -type LedgerBlockSR struct { - Hash util.Uint256 - Version *big.Int - PrevHash util.Uint256 - MerkleRoot util.Uint256 - Timestamp *big.Int - Nonce *big.Int - Index *big.Int - NextConsensus util.Uint160 - TransactionsLength *big.Int - PrevStateRoot util.Uint256 -} - -// LedgerTransaction is a contract-specific ledger.Transaction type used by its methods. -type LedgerTransaction struct { - Hash util.Uint256 - Version *big.Int - Nonce *big.Int - Sender util.Uint160 - SysFee *big.Int - NetFee *big.Int - ValidUntilBlock *big.Int - Script []byte -} - -// LedgerTransactionSigner is a contract-specific ledger.TransactionSigner type used by its methods. -type LedgerTransactionSigner struct { - Account util.Uint160 - Scopes *big.Int - AllowedContracts []util.Uint160 - AllowedGroups keys.PublicKeys - Rules []*LedgerWitnessRule -} - -// LedgerWitnessCondition is a contract-specific ledger.WitnessCondition type used by its methods. -type LedgerWitnessCondition struct { - Type *big.Int - Value any -} - -// LedgerWitnessRule is a contract-specific ledger.WitnessRule type used by its methods. -type LedgerWitnessRule struct { - Action *big.Int - Condition *LedgerWitnessCondition -} - -// ManagementABI is a contract-specific management.ABI type used by its methods. -type ManagementABI struct { - Methods []*ManagementMethod - Events []*ManagementEvent -} - -// ManagementContract is a contract-specific management.Contract type used by its methods. -type ManagementContract struct { - ID *big.Int - UpdateCounter *big.Int - Hash util.Uint160 - NEF []byte - Manifest *ManagementManifest -} - -// ManagementEvent is a contract-specific management.Event type used by its methods. -type ManagementEvent struct { - Name string - Params []*ManagementParameter -} - -// ManagementGroup is a contract-specific management.Group type used by its methods. -type ManagementGroup struct { - PublicKey *keys.PublicKey - Signature []byte -} - -// ManagementManifest is a contract-specific management.Manifest type used by its methods. -type ManagementManifest struct { - Name string - Groups []*ManagementGroup - Features map[string]string - SupportedStandards []string - ABI *ManagementABI - Permissions []*ManagementPermission - Trusts []util.Uint160 - Extra any -} - -// ManagementMethod is a contract-specific management.Method type used by its methods. -type ManagementMethod struct { - Name string - Params []*ManagementParameter - ReturnType *big.Int - Offset *big.Int - Safe bool -} - -// ManagementParameter is a contract-specific management.Parameter type used by its methods. -type ManagementParameter struct { - Name string - Type *big.Int -} - -// ManagementPermission is a contract-specific management.Permission type used by its methods. -type ManagementPermission struct { - Contract util.Uint160 - Methods []string -} - // Invoker is used by ContractReader to call various safe methods. type Invoker interface { Call(contract util.Uint160, operation string, params ...any) (*result.Invoke, error) @@ -333,1205 +212,3 @@ func (res *CommonBallot) FromStackItem(item stackitem.Item) error { return nil } - -// itemToLedgerBlock converts stack item into *LedgerBlock. -func itemToLedgerBlock(item stackitem.Item, err error) (*LedgerBlock, error) { - if err != nil { - return nil, err - } - var res = new(LedgerBlock) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of LedgerBlock from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *LedgerBlock) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 9 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Hash, err = func(item stackitem.Item) (util.Uint256, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint256{}, err - } - u, err := util.Uint256DecodeBytesBE(b) - if err != nil { - return util.Uint256{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Hash: %w", err) - } - - index++ - res.Version, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Version: %w", err) - } - - index++ - res.PrevHash, err = func(item stackitem.Item) (util.Uint256, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint256{}, err - } - u, err := util.Uint256DecodeBytesBE(b) - if err != nil { - return util.Uint256{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field PrevHash: %w", err) - } - - index++ - res.MerkleRoot, err = func(item stackitem.Item) (util.Uint256, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint256{}, err - } - u, err := util.Uint256DecodeBytesBE(b) - if err != nil { - return util.Uint256{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field MerkleRoot: %w", err) - } - - index++ - res.Timestamp, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Timestamp: %w", err) - } - - index++ - res.Nonce, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Nonce: %w", err) - } - - index++ - res.Index, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Index: %w", err) - } - - index++ - res.NextConsensus, err = func(item stackitem.Item) (util.Uint160, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint160{}, err - } - u, err := util.Uint160DecodeBytesBE(b) - if err != nil { - return util.Uint160{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field NextConsensus: %w", err) - } - - index++ - res.TransactionsLength, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field TransactionsLength: %w", err) - } - - return nil -} - -// itemToLedgerBlockSR converts stack item into *LedgerBlockSR. -func itemToLedgerBlockSR(item stackitem.Item, err error) (*LedgerBlockSR, error) { - if err != nil { - return nil, err - } - var res = new(LedgerBlockSR) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of LedgerBlockSR from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *LedgerBlockSR) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 10 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Hash, err = func(item stackitem.Item) (util.Uint256, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint256{}, err - } - u, err := util.Uint256DecodeBytesBE(b) - if err != nil { - return util.Uint256{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Hash: %w", err) - } - - index++ - res.Version, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Version: %w", err) - } - - index++ - res.PrevHash, err = func(item stackitem.Item) (util.Uint256, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint256{}, err - } - u, err := util.Uint256DecodeBytesBE(b) - if err != nil { - return util.Uint256{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field PrevHash: %w", err) - } - - index++ - res.MerkleRoot, err = func(item stackitem.Item) (util.Uint256, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint256{}, err - } - u, err := util.Uint256DecodeBytesBE(b) - if err != nil { - return util.Uint256{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field MerkleRoot: %w", err) - } - - index++ - res.Timestamp, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Timestamp: %w", err) - } - - index++ - res.Nonce, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Nonce: %w", err) - } - - index++ - res.Index, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Index: %w", err) - } - - index++ - res.NextConsensus, err = func(item stackitem.Item) (util.Uint160, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint160{}, err - } - u, err := util.Uint160DecodeBytesBE(b) - if err != nil { - return util.Uint160{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field NextConsensus: %w", err) - } - - index++ - res.TransactionsLength, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field TransactionsLength: %w", err) - } - - index++ - res.PrevStateRoot, err = func(item stackitem.Item) (util.Uint256, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint256{}, err - } - u, err := util.Uint256DecodeBytesBE(b) - if err != nil { - return util.Uint256{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field PrevStateRoot: %w", err) - } - - return nil -} - -// itemToLedgerTransaction converts stack item into *LedgerTransaction. -func itemToLedgerTransaction(item stackitem.Item, err error) (*LedgerTransaction, error) { - if err != nil { - return nil, err - } - var res = new(LedgerTransaction) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of LedgerTransaction from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *LedgerTransaction) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 8 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Hash, err = func(item stackitem.Item) (util.Uint256, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint256{}, err - } - u, err := util.Uint256DecodeBytesBE(b) - if err != nil { - return util.Uint256{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Hash: %w", err) - } - - index++ - res.Version, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Version: %w", err) - } - - index++ - res.Nonce, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Nonce: %w", err) - } - - index++ - res.Sender, err = func(item stackitem.Item) (util.Uint160, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint160{}, err - } - u, err := util.Uint160DecodeBytesBE(b) - if err != nil { - return util.Uint160{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Sender: %w", err) - } - - index++ - res.SysFee, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field SysFee: %w", err) - } - - index++ - res.NetFee, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field NetFee: %w", err) - } - - index++ - res.ValidUntilBlock, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field ValidUntilBlock: %w", err) - } - - index++ - res.Script, err = arr[index].TryBytes() - if err != nil { - return fmt.Errorf("field Script: %w", err) - } - - return nil -} - -// itemToLedgerTransactionSigner converts stack item into *LedgerTransactionSigner. -func itemToLedgerTransactionSigner(item stackitem.Item, err error) (*LedgerTransactionSigner, error) { - if err != nil { - return nil, err - } - var res = new(LedgerTransactionSigner) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of LedgerTransactionSigner from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *LedgerTransactionSigner) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 5 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Account, err = func(item stackitem.Item) (util.Uint160, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint160{}, err - } - u, err := util.Uint160DecodeBytesBE(b) - if err != nil { - return util.Uint160{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Account: %w", err) - } - - index++ - res.Scopes, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Scopes: %w", err) - } - - index++ - res.AllowedContracts, err = func(item stackitem.Item) ([]util.Uint160, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make([]util.Uint160, len(arr)) - for i := range res { - res[i], err = func(item stackitem.Item) (util.Uint160, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint160{}, err - } - u, err := util.Uint160DecodeBytesBE(b) - if err != nil { - return util.Uint160{}, err - } - return u, nil - }(arr[i]) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field AllowedContracts: %w", err) - } - - index++ - res.AllowedGroups, err = func(item stackitem.Item) (keys.PublicKeys, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make(keys.PublicKeys, len(arr)) - for i := range res { - res[i], err = func(item stackitem.Item) (*keys.PublicKey, error) { - b, err := item.TryBytes() - if err != nil { - return nil, err - } - k, err := keys.NewPublicKeyFromBytes(b, elliptic.P256()) - if err != nil { - return nil, err - } - return k, nil - }(arr[i]) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field AllowedGroups: %w", err) - } - - index++ - res.Rules, err = func(item stackitem.Item) ([]*LedgerWitnessRule, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make([]*LedgerWitnessRule, len(arr)) - for i := range res { - res[i], err = itemToLedgerWitnessRule(arr[i], nil) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Rules: %w", err) - } - - return nil -} - -// itemToLedgerWitnessCondition converts stack item into *LedgerWitnessCondition. -func itemToLedgerWitnessCondition(item stackitem.Item, err error) (*LedgerWitnessCondition, error) { - if err != nil { - return nil, err - } - var res = new(LedgerWitnessCondition) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of LedgerWitnessCondition from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *LedgerWitnessCondition) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 2 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Type, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Type: %w", err) - } - - index++ - res.Value, err = arr[index].Value(), error(nil) - if err != nil { - return fmt.Errorf("field Value: %w", err) - } - - return nil -} - -// itemToLedgerWitnessRule converts stack item into *LedgerWitnessRule. -func itemToLedgerWitnessRule(item stackitem.Item, err error) (*LedgerWitnessRule, error) { - if err != nil { - return nil, err - } - var res = new(LedgerWitnessRule) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of LedgerWitnessRule from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *LedgerWitnessRule) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 2 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Action, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Action: %w", err) - } - - index++ - res.Condition, err = itemToLedgerWitnessCondition(arr[index], nil) - if err != nil { - return fmt.Errorf("field Condition: %w", err) - } - - return nil -} - -// itemToManagementABI converts stack item into *ManagementABI. -func itemToManagementABI(item stackitem.Item, err error) (*ManagementABI, error) { - if err != nil { - return nil, err - } - var res = new(ManagementABI) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of ManagementABI from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *ManagementABI) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 2 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Methods, err = func(item stackitem.Item) ([]*ManagementMethod, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make([]*ManagementMethod, len(arr)) - for i := range res { - res[i], err = itemToManagementMethod(arr[i], nil) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Methods: %w", err) - } - - index++ - res.Events, err = func(item stackitem.Item) ([]*ManagementEvent, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make([]*ManagementEvent, len(arr)) - for i := range res { - res[i], err = itemToManagementEvent(arr[i], nil) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Events: %w", err) - } - - return nil -} - -// itemToManagementContract converts stack item into *ManagementContract. -func itemToManagementContract(item stackitem.Item, err error) (*ManagementContract, error) { - if err != nil { - return nil, err - } - var res = new(ManagementContract) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of ManagementContract from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *ManagementContract) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 5 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.ID, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field ID: %w", err) - } - - index++ - res.UpdateCounter, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field UpdateCounter: %w", err) - } - - index++ - res.Hash, err = func(item stackitem.Item) (util.Uint160, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint160{}, err - } - u, err := util.Uint160DecodeBytesBE(b) - if err != nil { - return util.Uint160{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Hash: %w", err) - } - - index++ - res.NEF, err = arr[index].TryBytes() - if err != nil { - return fmt.Errorf("field NEF: %w", err) - } - - index++ - res.Manifest, err = itemToManagementManifest(arr[index], nil) - if err != nil { - return fmt.Errorf("field Manifest: %w", err) - } - - return nil -} - -// itemToManagementEvent converts stack item into *ManagementEvent. -func itemToManagementEvent(item stackitem.Item, err error) (*ManagementEvent, error) { - if err != nil { - return nil, err - } - var res = new(ManagementEvent) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of ManagementEvent from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *ManagementEvent) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 2 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Name, err = func(item stackitem.Item) (string, error) { - b, err := item.TryBytes() - if err != nil { - return "", err - } - if !utf8.Valid(b) { - return "", errors.New("not a UTF-8 string") - } - return string(b), nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Name: %w", err) - } - - index++ - res.Params, err = func(item stackitem.Item) ([]*ManagementParameter, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make([]*ManagementParameter, len(arr)) - for i := range res { - res[i], err = itemToManagementParameter(arr[i], nil) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Params: %w", err) - } - - return nil -} - -// itemToManagementGroup converts stack item into *ManagementGroup. -func itemToManagementGroup(item stackitem.Item, err error) (*ManagementGroup, error) { - if err != nil { - return nil, err - } - var res = new(ManagementGroup) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of ManagementGroup from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *ManagementGroup) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 2 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.PublicKey, err = func(item stackitem.Item) (*keys.PublicKey, error) { - b, err := item.TryBytes() - if err != nil { - return nil, err - } - k, err := keys.NewPublicKeyFromBytes(b, elliptic.P256()) - if err != nil { - return nil, err - } - return k, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field PublicKey: %w", err) - } - - index++ - res.Signature, err = arr[index].TryBytes() - if err != nil { - return fmt.Errorf("field Signature: %w", err) - } - - return nil -} - -// itemToManagementManifest converts stack item into *ManagementManifest. -func itemToManagementManifest(item stackitem.Item, err error) (*ManagementManifest, error) { - if err != nil { - return nil, err - } - var res = new(ManagementManifest) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of ManagementManifest from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *ManagementManifest) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 8 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Name, err = func(item stackitem.Item) (string, error) { - b, err := item.TryBytes() - if err != nil { - return "", err - } - if !utf8.Valid(b) { - return "", errors.New("not a UTF-8 string") - } - return string(b), nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Name: %w", err) - } - - index++ - res.Groups, err = func(item stackitem.Item) ([]*ManagementGroup, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make([]*ManagementGroup, len(arr)) - for i := range res { - res[i], err = itemToManagementGroup(arr[i], nil) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Groups: %w", err) - } - - index++ - res.Features, err = func(item stackitem.Item) (map[string]string, error) { - m, ok := item.Value().([]stackitem.MapElement) - if !ok { - return nil, fmt.Errorf("%s is not a map", item.Type().String()) - } - res := make(map[string]string) - for i := range m { - k, err := func(item stackitem.Item) (string, error) { - b, err := item.TryBytes() - if err != nil { - return "", err - } - if !utf8.Valid(b) { - return "", errors.New("not a UTF-8 string") - } - return string(b), nil - }(m[i].Key) - if err != nil { - return nil, fmt.Errorf("key %d: %w", i, err) - } - v, err := func(item stackitem.Item) (string, error) { - b, err := item.TryBytes() - if err != nil { - return "", err - } - if !utf8.Valid(b) { - return "", errors.New("not a UTF-8 string") - } - return string(b), nil - }(m[i].Value) - if err != nil { - return nil, fmt.Errorf("value %d: %w", i, err) - } - res[k] = v - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Features: %w", err) - } - - index++ - res.SupportedStandards, err = func(item stackitem.Item) ([]string, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make([]string, len(arr)) - for i := range res { - res[i], err = func(item stackitem.Item) (string, error) { - b, err := item.TryBytes() - if err != nil { - return "", err - } - if !utf8.Valid(b) { - return "", errors.New("not a UTF-8 string") - } - return string(b), nil - }(arr[i]) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field SupportedStandards: %w", err) - } - - index++ - res.ABI, err = itemToManagementABI(arr[index], nil) - if err != nil { - return fmt.Errorf("field ABI: %w", err) - } - - index++ - res.Permissions, err = func(item stackitem.Item) ([]*ManagementPermission, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make([]*ManagementPermission, len(arr)) - for i := range res { - res[i], err = itemToManagementPermission(arr[i], nil) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Permissions: %w", err) - } - - index++ - res.Trusts, err = func(item stackitem.Item) ([]util.Uint160, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make([]util.Uint160, len(arr)) - for i := range res { - res[i], err = func(item stackitem.Item) (util.Uint160, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint160{}, err - } - u, err := util.Uint160DecodeBytesBE(b) - if err != nil { - return util.Uint160{}, err - } - return u, nil - }(arr[i]) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Trusts: %w", err) - } - - index++ - res.Extra, err = arr[index].Value(), error(nil) - if err != nil { - return fmt.Errorf("field Extra: %w", err) - } - - return nil -} - -// itemToManagementMethod converts stack item into *ManagementMethod. -func itemToManagementMethod(item stackitem.Item, err error) (*ManagementMethod, error) { - if err != nil { - return nil, err - } - var res = new(ManagementMethod) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of ManagementMethod from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *ManagementMethod) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 5 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Name, err = func(item stackitem.Item) (string, error) { - b, err := item.TryBytes() - if err != nil { - return "", err - } - if !utf8.Valid(b) { - return "", errors.New("not a UTF-8 string") - } - return string(b), nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Name: %w", err) - } - - index++ - res.Params, err = func(item stackitem.Item) ([]*ManagementParameter, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make([]*ManagementParameter, len(arr)) - for i := range res { - res[i], err = itemToManagementParameter(arr[i], nil) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Params: %w", err) - } - - index++ - res.ReturnType, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field ReturnType: %w", err) - } - - index++ - res.Offset, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Offset: %w", err) - } - - index++ - res.Safe, err = arr[index].TryBool() - if err != nil { - return fmt.Errorf("field Safe: %w", err) - } - - return nil -} - -// itemToManagementParameter converts stack item into *ManagementParameter. -func itemToManagementParameter(item stackitem.Item, err error) (*ManagementParameter, error) { - if err != nil { - return nil, err - } - var res = new(ManagementParameter) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of ManagementParameter from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *ManagementParameter) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 2 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Name, err = func(item stackitem.Item) (string, error) { - b, err := item.TryBytes() - if err != nil { - return "", err - } - if !utf8.Valid(b) { - return "", errors.New("not a UTF-8 string") - } - return string(b), nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Name: %w", err) - } - - index++ - res.Type, err = arr[index].TryInteger() - if err != nil { - return fmt.Errorf("field Type: %w", err) - } - - return nil -} - -// itemToManagementPermission converts stack item into *ManagementPermission. -func itemToManagementPermission(item stackitem.Item, err error) (*ManagementPermission, error) { - if err != nil { - return nil, err - } - var res = new(ManagementPermission) - err = res.FromStackItem(item) - return res, err -} - -// FromStackItem retrieves fields of ManagementPermission from the given -// [stackitem.Item] or returns an error if it's not possible to do to so. -func (res *ManagementPermission) FromStackItem(item stackitem.Item) error { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return errors.New("not an array") - } - if len(arr) != 2 { - return errors.New("wrong number of structure elements") - } - - var ( - index = -1 - err error - ) - index++ - res.Contract, err = func(item stackitem.Item) (util.Uint160, error) { - b, err := item.TryBytes() - if err != nil { - return util.Uint160{}, err - } - u, err := util.Uint160DecodeBytesBE(b) - if err != nil { - return util.Uint160{}, err - } - return u, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Contract: %w", err) - } - - index++ - res.Methods, err = func(item stackitem.Item) ([]string, error) { - arr, ok := item.Value().([]stackitem.Item) - if !ok { - return nil, errors.New("not an array") - } - res := make([]string, len(arr)) - for i := range res { - res[i], err = func(item stackitem.Item) (string, error) { - b, err := item.TryBytes() - if err != nil { - return "", err - } - if !utf8.Valid(b) { - return "", errors.New("not a UTF-8 string") - } - return string(b), nil - }(arr[i]) - if err != nil { - return nil, fmt.Errorf("item %d: %w", i, err) - } - } - return res, nil - }(arr[index]) - if err != nil { - return fmt.Errorf("field Methods: %w", err) - } - - return nil -}