rpc: add (*Client).GetDesignatedByRole method
This commit is contained in:
parent
1e649bc9a0
commit
10fb86c0b2
2 changed files with 80 additions and 0 deletions
|
@ -3,9 +3,14 @@ package client
|
|||
// Various non-policy things from native contracts.
|
||||
|
||||
import (
|
||||
"crypto/elliptic"
|
||||
"fmt"
|
||||
|
||||
"github.com/nspcc-dev/neo-go/pkg/core/native/nativenames"
|
||||
"github.com/nspcc-dev/neo-go/pkg/core/native/noderoles"
|
||||
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
||||
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
|
||||
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
||||
)
|
||||
|
||||
// GetOraclePrice invokes `getPrice` method on a native Oracle contract.
|
||||
|
@ -34,3 +39,54 @@ func (c *Client) GetGasPerBlock() (int64, error) {
|
|||
}
|
||||
return c.invokeNativeGetMethod(neoHash, "getGasPerBlock")
|
||||
}
|
||||
|
||||
// GetDesignatedByRole invokes `getDesignatedByRole` method on a native RoleManagement contract.
|
||||
func (c *Client) GetDesignatedByRole(role noderoles.Role, index uint32) (keys.PublicKeys, error) {
|
||||
rmHash, err := c.GetNativeContractHash(nativenames.Designation)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to get native RoleManagement hash: %w", err)
|
||||
}
|
||||
result, err := c.InvokeFunction(rmHash, "getDesignatedByRole", []smartcontract.Parameter{
|
||||
{
|
||||
Type: smartcontract.IntegerType,
|
||||
Value: int64(role),
|
||||
},
|
||||
{
|
||||
Type: smartcontract.IntegerType,
|
||||
Value: int64(index),
|
||||
},
|
||||
}, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
err = getInvocationError(result)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("`getDesignatedByRole`: %w", err)
|
||||
}
|
||||
return topPublicKeysFromStack(result.Stack)
|
||||
}
|
||||
|
||||
// topPublicKeysFromStack returns the top array of public keys from stack.
|
||||
func topPublicKeysFromStack(st []stackitem.Item) (keys.PublicKeys, error) {
|
||||
index := len(st) - 1 // top stack element is last in the array
|
||||
var (
|
||||
pks keys.PublicKeys
|
||||
err error
|
||||
)
|
||||
items, ok := st[index].Value().([]stackitem.Item)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("invalid stack item type: %s", st[index].Type())
|
||||
}
|
||||
pks = make(keys.PublicKeys, len(items))
|
||||
for i, item := range items {
|
||||
val, ok := item.Value().([]byte)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("invalid array element #%d: %s", i, item.Type())
|
||||
}
|
||||
pks[i], err = keys.NewPublicKeyFromBytes(val, elliptic.P256())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return pks, nil
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue