add: wrapper integration
This commit is contained in:
parent
502cd51a66
commit
490fa548bb
8 changed files with 121 additions and 0 deletions
7
contract/README.md
Normal file
7
contract/README.md
Normal file
|
@ -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` в свой проект и использовать его.
|
5
contract/config.yml
Normal file
5
contract/config.yml
Normal file
|
@ -0,0 +1,5 @@
|
|||
name: "Password storage"
|
||||
sourceurl: https://git.frostfs.info/Web3N3/neowolves
|
||||
safemethods: ["checkUser"]
|
||||
supportedstandards: []
|
||||
events: []
|
5
contract/go.mod
Normal file
5
contract/go.mod
Normal file
|
@ -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
|
2
contract/go.sum
Normal file
2
contract/go.sum
Normal file
|
@ -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=
|
1
contract/manifest.json
Normal file
1
contract/manifest.json
Normal file
|
@ -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}
|
23
contract/passtorage.go
Normal file
23
contract/passtorage.go
Normal file
|
@ -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")
|
||||
}
|
||||
}
|
BIN
contract/passtorage.nef
Normal file
BIN
contract/passtorage.nef
Normal file
Binary file not shown.
78
contract/wrapper.go
Normal file
78
contract/wrapper.go
Normal file
|
@ -0,0 +1,78 @@
|
|||
// Code generated by neo-go contract generate-rpcwrapper --manifest <file.json> --out <file.go> [--hash <hash>] [--config <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)
|
||||
}
|
Loading…
Reference in a new issue