2023-12-05 09:12:35 +00:00
|
|
|
package contract
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"math/big"
|
|
|
|
|
|
|
|
policycontract "git.frostfs.info/TrueCloudLab/frostfs-contract/policy"
|
|
|
|
policyclient "git.frostfs.info/TrueCloudLab/frostfs-contract/rpcclient/policy"
|
2023-12-08 07:44:13 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/internal/frostfs/policy"
|
2023-12-07 14:13:11 +00:00
|
|
|
frostfsutil "git.frostfs.info/TrueCloudLab/frostfs-s3-gw/internal/frostfs/util"
|
2023-12-21 14:57:12 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
|
2023-12-05 09:12:35 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/rpcclient"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/rpcclient/actor"
|
2023-12-21 14:57:12 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/rpcclient/notary"
|
2023-12-07 14:13:11 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
2023-12-05 09:12:35 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/wallet"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Client struct {
|
|
|
|
actor *actor.Actor
|
|
|
|
policyContract *policyclient.Contract
|
|
|
|
}
|
|
|
|
|
|
|
|
type Config struct {
|
|
|
|
// RPCAddress is an endpoint to connect to neo rpc.
|
|
|
|
RPCAddress string
|
|
|
|
|
|
|
|
// Contract is hash of contract or its name in NNS.
|
|
|
|
Contract string
|
|
|
|
|
2023-12-21 14:57:12 +00:00
|
|
|
// ProxyContract is hash of proxy contract or its name in NNS to interact with policy.
|
|
|
|
ProxyContract string
|
|
|
|
|
2023-12-05 09:12:35 +00:00
|
|
|
// Key is used to interact with policy contract.
|
|
|
|
// If this is nil than random key will be generated.
|
|
|
|
Key *keys.PrivateKey
|
|
|
|
}
|
|
|
|
|
2023-12-08 07:44:13 +00:00
|
|
|
var _ policy.Contract = (*Client)(nil)
|
2023-12-05 09:12:35 +00:00
|
|
|
|
|
|
|
// New creates new Policy contract wrapper.
|
|
|
|
func New(ctx context.Context, cfg Config) (*Client, error) {
|
2023-12-07 14:13:11 +00:00
|
|
|
contractHash, err := frostfsutil.ResolveContractHash(cfg.Contract, cfg.RPCAddress)
|
2023-12-05 09:12:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("resolve frostfs contract hash: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
key := cfg.Key
|
|
|
|
if key == nil {
|
|
|
|
if key, err = keys.NewPrivateKey(); err != nil {
|
|
|
|
return nil, fmt.Errorf("generate anon private key for policy: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rpcCli, err := rpcclient.New(ctx, cfg.RPCAddress, rpcclient.Options{})
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("create policy rpc client: %w", err)
|
|
|
|
}
|
|
|
|
|
2023-12-21 14:57:12 +00:00
|
|
|
proxyContractHash, err := frostfsutil.ResolveContractHash(cfg.ProxyContract, cfg.RPCAddress)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("resolve frostfs contract hash: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
act, err := actor.New(rpcCli, getSigners(key, proxyContractHash, contractHash))
|
2023-12-05 09:12:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("create new actor: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Client{
|
|
|
|
actor: act,
|
|
|
|
policyContract: policyclient.New(act, contractHash),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2023-12-21 14:57:12 +00:00
|
|
|
func getSigners(key *keys.PrivateKey, proxyHash, contractHash util.Uint160) []actor.SignerAccount {
|
|
|
|
acc := wallet.NewAccountFromPrivateKey(key)
|
|
|
|
|
|
|
|
return []actor.SignerAccount{
|
|
|
|
{
|
|
|
|
Signer: transaction.Signer{
|
|
|
|
Account: proxyHash,
|
|
|
|
Scopes: transaction.CustomContracts,
|
|
|
|
AllowedContracts: []util.Uint160{contractHash},
|
|
|
|
},
|
|
|
|
Account: notary.FakeContractAccount(proxyHash),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Signer: transaction.Signer{
|
|
|
|
Account: acc.Contract.ScriptHash(),
|
|
|
|
Scopes: transaction.CalledByEntry,
|
|
|
|
},
|
|
|
|
Account: acc,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-08 07:44:13 +00:00
|
|
|
func (c *Client) AddChain(kind policycontract.Kind, entity string, name []byte, chain []byte) (util.Uint256, uint32, error) {
|
|
|
|
return c.policyContract.AddChain(big.NewInt(int64(kind)), entity, name, chain)
|
2023-12-05 09:12:35 +00:00
|
|
|
}
|
|
|
|
|
2023-12-08 07:44:13 +00:00
|
|
|
func (c *Client) GetChain(kind policycontract.Kind, entity string, name []byte) ([]byte, error) {
|
|
|
|
return c.policyContract.GetChain(big.NewInt(int64(kind)), entity, name)
|
2023-12-05 09:12:35 +00:00
|
|
|
}
|
|
|
|
|
2023-12-08 07:44:13 +00:00
|
|
|
func (c *Client) RemoveChain(kind policycontract.Kind, entity string, name []byte) (util.Uint256, uint32, error) {
|
|
|
|
return c.policyContract.RemoveChain(big.NewInt(int64(kind)), entity, name)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) ListChains(kind policycontract.Kind, entity string, name []byte) ([][]byte, error) {
|
|
|
|
items, err := c.policyContract.ListChainsByPrefix(big.NewInt(int64(kind)), entity, name)
|
2023-12-05 09:12:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-12-08 07:44:13 +00:00
|
|
|
res := make([][]byte, len(items))
|
2023-12-05 09:12:35 +00:00
|
|
|
for i, item := range items {
|
2023-12-08 07:44:13 +00:00
|
|
|
res[i], err = item.TryBytes()
|
2023-12-05 09:12:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2023-12-08 07:44:13 +00:00
|
|
|
func (c *Client) Wait(tx util.Uint256, vub uint32, err error) error {
|
|
|
|
_, err = c.actor.Wait(tx, vub, err)
|
|
|
|
return err
|
2023-12-05 09:12:35 +00:00
|
|
|
}
|