From 490fa548bb96414dc0d85682cbc126ea1a13b73e Mon Sep 17 00:00:00 2001 From: krugarrr Date: Sun, 7 Jan 2024 22:14:47 +0300 Subject: [PATCH] add: wrapper integration --- contract/README.md | 7 ++++ contract/config.yml | 5 +++ contract/go.mod | 5 +++ contract/go.sum | 2 ++ contract/manifest.json | 1 + contract/passtorage.go | 23 ++++++++++++ contract/passtorage.nef | Bin 0 -> 275 bytes contract/wrapper.go | 78 ++++++++++++++++++++++++++++++++++++++++ 8 files changed, 121 insertions(+) create mode 100644 contract/README.md create mode 100644 contract/config.yml create mode 100644 contract/go.mod create mode 100644 contract/go.sum create mode 100644 contract/manifest.json create mode 100644 contract/passtorage.go create mode 100644 contract/passtorage.nef create mode 100644 contract/wrapper.go diff --git a/contract/README.md b/contract/README.md new file mode 100644 index 0000000..b675468 --- /dev/null +++ b/contract/README.md @@ -0,0 +1,7 @@ +## Как скомпилить +`neo-go contract compile -i passtorage.go -c config.yml -m manifest.json` + +## Как сгенерить обёртку +`neo-go contract generate-rpcwrapper -m manifest.json -o wrapper.go` +Можно добавить хеш скрипта через `--hash HASH` +Потом можете поместить `wrapper.go` в свой проект и использовать его. diff --git a/contract/config.yml b/contract/config.yml new file mode 100644 index 0000000..f57464b --- /dev/null +++ b/contract/config.yml @@ -0,0 +1,5 @@ +name: "Password storage" +sourceurl: https://git.frostfs.info/Web3N3/neowolves +safemethods: ["checkUser"] +supportedstandards: [] +events: [] diff --git a/contract/go.mod b/contract/go.mod new file mode 100644 index 0000000..99e976e --- /dev/null +++ b/contract/go.mod @@ -0,0 +1,5 @@ +module git.frostfs.info/Web3N3/neowolves/contract + +go 1.21.5 + +require github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20231229133344-d90169761595 diff --git a/contract/go.sum b/contract/go.sum new file mode 100644 index 0000000..e153659 --- /dev/null +++ b/contract/go.sum @@ -0,0 +1,2 @@ +github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20231229133344-d90169761595 h1:vWtkeZPpwlHV55W/PSTbRP8aH37UrbdTAOyB5J1xgDM= +github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20231229133344-d90169761595/go.mod h1:J/Mk6+nKeKSW4wygkZQFLQ6SkLOSGX5Ga0RuuuktEag= diff --git a/contract/manifest.json b/contract/manifest.json new file mode 100644 index 0000000..84a33ca --- /dev/null +++ b/contract/manifest.json @@ -0,0 +1 @@ +{"name":"Password storage","abi":{"methods":[{"name":"checkUser","offset":19,"parameters":[{"name":"login","type":"String"},{"name":"givenPassword","type":"Hash256"}],"returntype":"Void","safe":true},{"name":"createUser","offset":0,"parameters":[{"name":"login","type":"String"},{"name":"password","type":"Hash256"}],"returntype":"Void","safe":false}],"events":[]},"features":{},"groups":[],"permissions":[],"supportedstandards":[],"trusts":[],"extra":null} \ No newline at end of file diff --git a/contract/passtorage.go b/contract/passtorage.go new file mode 100644 index 0000000..99f1816 --- /dev/null +++ b/contract/passtorage.go @@ -0,0 +1,23 @@ +package main + +import ( + "github.com/nspcc-dev/neo-go/pkg/interop" + "github.com/nspcc-dev/neo-go/pkg/interop/storage" +) + +func CreateUser(login string, password interop.Hash256) { + ctx := storage.GetContext() + // NOTE: maybe we should handle case where user already exists + storage.Put(ctx, login, password) +} + +func CheckUser(login string, givenPassword interop.Hash256) { + ctx := storage.GetReadOnlyContext() + password := storage.Get(ctx, login).(interop.Hash256) + if password == nil { + panic("This user does not exist") + } + if !password.Equals(givenPassword) { + panic("Password hashes does not match") + } +} diff --git a/contract/passtorage.nef b/contract/passtorage.nef new file mode 100644 index 0000000000000000000000000000000000000000..4dbd4ff5f3b41e53c684f8df318f1bc6740cbde9 GIT binary patch literal 275 zcmeZsbu-RO&DTxO*EP^HG%(dOU?3T2W|Wi^6kF-*r)QSvr4{8Dm!uWzW#*;j>xZW% z8T%RQ11%`e&nZhS1{pmkoRP_K_P6wN1sN5U!H&=DC0ZQ9nVB5FZOMKF6bf*h6#K%^ z>!yJU%WZ?g%o}R5JQ5)pnZ*jF#i>OKDfy|z3VHb@3aJ&D#U)mmMOio0*eY)*F>|UY zWZh7ath}Kjn&ov<1E^V}5=u`8$|^B)tWs0vkqbyHE-ue6N>Rv2EY1KL1vf7@u_QUe K%0X?fkO2T?2~EWS literal 0 HcmV?d00001 diff --git a/contract/wrapper.go b/contract/wrapper.go new file mode 100644 index 0000000..bd2195f --- /dev/null +++ b/contract/wrapper.go @@ -0,0 +1,78 @@ +// Code generated by neo-go contract generate-rpcwrapper --manifest --out [--hash ] [--config ]; DO NOT EDIT. + +// Package passwordstorage contains RPC wrappers for Password storage contract. +package passwordstorage + +import ( + "github.com/nspcc-dev/neo-go/pkg/core/transaction" + "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" +) + +// Invoker is used by ContractReader to call various safe methods. +type Invoker interface { + Call(contract util.Uint160, operation string, params ...any) (*result.Invoke, error) +} + +// Actor is used by Contract to call state-changing methods. +type Actor interface { + Invoker + + MakeCall(contract util.Uint160, method string, params ...any) (*transaction.Transaction, error) + MakeRun(script []byte) (*transaction.Transaction, error) + MakeUnsignedCall(contract util.Uint160, method string, attrs []transaction.Attribute, params ...any) (*transaction.Transaction, error) + MakeUnsignedRun(script []byte, attrs []transaction.Attribute) (*transaction.Transaction, error) + SendCall(contract util.Uint160, method string, params ...any) (util.Uint256, uint32, error) + SendRun(script []byte) (util.Uint256, uint32, error) +} + +// ContractReader implements safe contract methods. +type ContractReader struct { + invoker Invoker + hash util.Uint160 +} + +// Contract implements all contract methods. +type Contract struct { + ContractReader + actor Actor + hash util.Uint160 +} + +// NewReader creates an instance of ContractReader using provided contract hash and the given Invoker. +func NewReader(invoker Invoker, hash util.Uint160) *ContractReader { + return &ContractReader{invoker, hash} +} + +// New creates an instance of Contract using provided contract hash and the given Actor. +func New(actor Actor, hash util.Uint160) *Contract { + return &Contract{ContractReader{actor, hash}, actor, hash} +} + +// CheckUser invokes `checkUser` method of contract. +func (c *ContractReader) CheckUser(login string, givenPassword util.Uint256) (*result.Invoke, error) { + c.invoker.Call(c.hash, "checkUser", login, givenPassword) +} + +// CreateUser creates a transaction invoking `createUser` method of the contract. +// This transaction is signed and immediately sent to the network. +// The values returned are its hash, ValidUntilBlock value and error if any. +func (c *Contract) CreateUser(login string, password util.Uint256) (util.Uint256, uint32, error) { + return c.actor.SendCall(c.hash, "createUser", login, password) +} + +// CreateUserTransaction creates a transaction invoking `createUser` method of the contract. +// This transaction is signed, but not sent to the network, instead it's +// returned to the caller. +func (c *Contract) CreateUserTransaction(login string, password util.Uint256) (*transaction.Transaction, error) { + return c.actor.MakeCall(c.hash, "createUser", login, password) +} + +// CreateUserUnsigned creates a transaction invoking `createUser` method of the contract. +// This transaction is not signed, it's simply returned to the caller. +// Any fields of it that do not affect fees can be changed (ValidUntilBlock, +// Nonce), fee values (NetworkFee, SystemFee) can be increased as well. +func (c *Contract) CreateUserUnsigned(login string, password util.Uint256) (*transaction.Transaction, error) { + return c.actor.MakeUnsignedCall(c.hash, "createUser", nil, login, password) +}