forked from TrueCloudLab/frostfs-s3-gw
94 lines
2.8 KiB
Go
94 lines
2.8 KiB
Go
package contract
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-contract/commonclient"
|
|
"git.frostfs.info/TrueCloudLab/frostfs-contract/frostfsid/client"
|
|
frostfsutil "git.frostfs.info/TrueCloudLab/frostfs-s3-gw/internal/frostfs/util"
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
|
"github.com/nspcc-dev/neo-go/pkg/rpcclient"
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
|
"github.com/nspcc-dev/neo-go/pkg/wallet"
|
|
)
|
|
|
|
type FrostFSID struct {
|
|
cli *client.Client
|
|
}
|
|
|
|
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
|
|
|
|
// ProxyContract is hash of proxy contract or its name in NNS to interact with frostfsid.
|
|
ProxyContract string
|
|
|
|
// Key is used to interact with frostfsid contract.
|
|
// If this is nil than random key will be generated.
|
|
Key *keys.PrivateKey
|
|
|
|
// Waiter contains options for awaiting blockchain transactions
|
|
// submitted via the contract client.
|
|
Waiter commonclient.WaiterOptions
|
|
}
|
|
|
|
// New creates new FrostfsID contract wrapper that implements auth.FrostFSID interface.
|
|
func New(ctx context.Context, cfg Config) (*FrostFSID, error) {
|
|
contractHash, err := frostfsutil.ResolveContractHash(cfg.Contract, cfg.RPCAddress)
|
|
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 frostfsid: %w", err)
|
|
}
|
|
}
|
|
|
|
rpcCli, err := rpcclient.New(ctx, cfg.RPCAddress, rpcclient.Options{})
|
|
if err != nil {
|
|
return nil, fmt.Errorf("init rpc client: %w", err)
|
|
}
|
|
|
|
var opt client.Options
|
|
opt.ProxyContract, err = frostfsutil.ResolveContractHash(cfg.ProxyContract, cfg.RPCAddress)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("resolve frostfs contract hash: %w", err)
|
|
}
|
|
opt.Waiter = cfg.Waiter
|
|
|
|
cli, err := client.New(rpcCli, wallet.NewAccountFromPrivateKey(key), contractHash, opt)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("init frostfsid client: %w", err)
|
|
}
|
|
|
|
return &FrostFSID{
|
|
cli: cli,
|
|
}, nil
|
|
}
|
|
|
|
func (f *FrostFSID) GetSubjectExtended(userHash util.Uint160) (*client.SubjectExtended, error) {
|
|
return f.cli.GetSubjectExtended(userHash)
|
|
}
|
|
|
|
func (f *FrostFSID) GetSubjectKeyByName(namespace, name string) (*keys.PublicKey, error) {
|
|
return f.cli.GetSubjectKeyByName(namespace, name)
|
|
}
|
|
|
|
func (f *FrostFSID) CreateSubject(namespace string, key *keys.PublicKey) (util.Uint256, uint32, error) {
|
|
return f.cli.CreateSubject(namespace, key)
|
|
}
|
|
|
|
func (f *FrostFSID) SetSubjectName(key *keys.PublicKey, name string) (util.Uint256, uint32, error) {
|
|
return f.cli.SetSubjectName(key.GetScriptHash(), name)
|
|
}
|
|
|
|
func (f *FrostFSID) Wait(tx util.Uint256, vub uint32, err error) error {
|
|
_, err = f.cli.Wait(tx, vub, err)
|
|
return err
|
|
}
|