feat_:implement connector requestPermissions api (#5566)
This commit is contained in:
parent
5e88d5b498
commit
0ecc266b1e
|
@ -34,6 +34,9 @@ func NewAPI(s *Service) *API {
|
|||
NetworkManager: s.nm,
|
||||
})
|
||||
|
||||
// Request permissions
|
||||
r.Register("wallet_requestPermissions", &commands.RequestPermissionsCommand{})
|
||||
|
||||
return &API{
|
||||
s: s,
|
||||
r: r,
|
||||
|
|
|
@ -0,0 +1,79 @@
|
|||
package commands
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
type RequestPermissionsCommand struct {
|
||||
}
|
||||
|
||||
type Permission struct {
|
||||
ParentCapability string `json:"parentCapability"`
|
||||
Date string `json:"date"`
|
||||
}
|
||||
|
||||
type PermissionsResponse struct {
|
||||
JSONRPC string `json:"jsonrpc"`
|
||||
ID int `json:"id"`
|
||||
Result []Permission `json:"result"`
|
||||
}
|
||||
|
||||
var (
|
||||
ErrNoRequestPermissionsParamsFound = errors.New("no request permission params found")
|
||||
ErrMultipleKeysFound = errors.New("Multiple methodNames found in request permissions params")
|
||||
ErrInvalidParamType = errors.New("Invalid parameter type")
|
||||
)
|
||||
|
||||
func (r *RPCRequest) getRequestPermissionsParam() (string, error) {
|
||||
if r.Params == nil || len(r.Params) == 0 {
|
||||
return "", ErrEmptyRPCParams
|
||||
}
|
||||
|
||||
paramMap, ok := r.Params[0].(map[string]interface{})
|
||||
if !ok {
|
||||
return "", ErrInvalidParamType
|
||||
}
|
||||
|
||||
if len(paramMap) > 1 {
|
||||
return "", ErrMultipleKeysFound
|
||||
}
|
||||
|
||||
for methodName := range paramMap {
|
||||
return methodName, nil
|
||||
}
|
||||
|
||||
return "", ErrNoRequestPermissionsParamsFound
|
||||
}
|
||||
|
||||
func (c *RequestPermissionsCommand) getPermissionResponse(methodName string) (string, error) {
|
||||
date := time.Now().UnixNano() / int64(time.Millisecond)
|
||||
|
||||
response := Permission{
|
||||
ParentCapability: methodName,
|
||||
Date: fmt.Sprintf("%d", date),
|
||||
}
|
||||
|
||||
responseJSON, err := json.Marshal(response)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to marshal response: %v", err)
|
||||
}
|
||||
|
||||
return string(responseJSON), nil
|
||||
}
|
||||
|
||||
func (c *RequestPermissionsCommand) Execute(request RPCRequest) (string, error) {
|
||||
err := request.Validate()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
methodName, err := request.getRequestPermissionsParam()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return c.getPermissionResponse(methodName)
|
||||
}
|
|
@ -0,0 +1,105 @@
|
|||
package commands
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestFailToRequestPermissionsWithMissingDAppFields(t *testing.T) {
|
||||
cmd := &RequestPermissionsCommand{}
|
||||
|
||||
// Missing DApp fields
|
||||
request, err := ConstructRPCRequest("wallet_requestPermissions", []interface{}{}, nil)
|
||||
assert.NoError(t, err)
|
||||
|
||||
result, err := cmd.Execute(request)
|
||||
assert.Equal(t, ErrRequestMissingDAppData, err)
|
||||
assert.Empty(t, result)
|
||||
}
|
||||
|
||||
func TestRequestPermissionsResponse(t *testing.T) {
|
||||
cmd := &RequestPermissionsCommand{}
|
||||
|
||||
testCases := []struct {
|
||||
name string
|
||||
params []interface{}
|
||||
expectedError error
|
||||
expectedCapability string
|
||||
}{
|
||||
{
|
||||
name: "Single valid key",
|
||||
params: []interface{}{
|
||||
map[string]interface{}{
|
||||
"eth_requestAccounts": struct{}{},
|
||||
},
|
||||
},
|
||||
expectedError: nil,
|
||||
expectedCapability: "eth_requestAccounts",
|
||||
},
|
||||
{
|
||||
name: "Single valid key",
|
||||
params: []interface{}{
|
||||
map[string]interface{}{
|
||||
"eth_accounts": struct{}{},
|
||||
},
|
||||
},
|
||||
expectedError: nil,
|
||||
expectedCapability: "eth_accounts",
|
||||
},
|
||||
{
|
||||
name: "Multiple keys",
|
||||
params: []interface{}{
|
||||
map[string]interface{}{
|
||||
"eth_requestAccounts": struct{}{},
|
||||
"eth_sendTransaction": struct{}{},
|
||||
},
|
||||
},
|
||||
expectedError: ErrMultipleKeysFound,
|
||||
expectedCapability: "",
|
||||
},
|
||||
{
|
||||
name: "No keys",
|
||||
params: []interface{}{
|
||||
map[string]interface{}{},
|
||||
},
|
||||
expectedError: ErrNoRequestPermissionsParamsFound,
|
||||
expectedCapability: "",
|
||||
},
|
||||
{
|
||||
name: "Nil params",
|
||||
params: nil,
|
||||
expectedError: ErrEmptyRPCParams,
|
||||
expectedCapability: "",
|
||||
},
|
||||
{
|
||||
name: "Invalid param type",
|
||||
params: []interface{}{
|
||||
"invalid_param_type",
|
||||
},
|
||||
expectedError: ErrInvalidParamType,
|
||||
expectedCapability: "",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
request, err := ConstructRPCRequest("wallet_requestPermissions", tc.params, &testDAppData)
|
||||
assert.NoError(t, err)
|
||||
|
||||
response, err := cmd.Execute(request)
|
||||
if tc.expectedError != nil {
|
||||
assert.Error(t, err)
|
||||
assert.Equal(t, err, tc.expectedError)
|
||||
} else {
|
||||
assert.NoError(t, err)
|
||||
|
||||
var permission Permission
|
||||
err = json.Unmarshal([]byte(response), &permission)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, permission.ParentCapability, tc.expectedCapability)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue