Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

XSWD permission changes and tests #176

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions cmd/dero-wallet-cli/easymenu_post_open.go
Original file line number Diff line number Diff line change
Expand Up @@ -521,12 +521,18 @@ func handle_easymenu_post_open_command(l *readline.Instance, line string) (proce
break
}

// NewXSWDServer default behavior is to Ask permission for all requests
xswd_server = xswd.NewXSWDServer(wallet, func(ad *xswd.ApplicationData) bool {
// TODO inform if it was already or not, and with permissions inside
// xswd logger informs if app is requesting permissions upon connection or if app is already connected
return ReadStringXSWDPrompt(l, ad.OnClose, fmt.Sprintf("Allow application %s (%s) to access your wallet (y/N): ", ad.Name, ad.Url), []string{"Y", "N"}) == "Y"
}, func(ad *xswd.ApplicationData, r *jrpc2.Request) xswd.Permission {
return AskPermissionForRequest(l, ad, r)
})
// check if start was successful
time.Sleep(time.Second)
if !xswd_server.IsRunning() {
xswd_server = nil
}
case "17":
if xswd_server == nil {
logger.Error(nil, "XSWD server is not running")
Expand All @@ -535,7 +541,14 @@ func handle_easymenu_post_open_command(l *readline.Instance, line string) (proce
apps := xswd_server.GetApplications()
logger.Info(fmt.Sprintf("XSWD Applications (%d):", len(apps)))
for _, app := range apps {
logger.Info("Application", "id", app.Id, "name", app.Name, "description", app.Description, "url", app.Url, "permissions", app.Permissions)
logger.Info("Application", "id", app.Id, "name", app.Name, "description", app.Description, "url", app.Url)
for name, perm := range app.Permissions {
logger.Info(fmt.Sprintf("Permission %s", app.Name), name, perm)
}

for event, sub := range app.RegisteredEvents {
logger.Info(fmt.Sprintf("Subscribed %s", app.Name), string(event), sub)
}
}

default:
Expand Down
9 changes: 8 additions & 1 deletion cmd/dero-wallet-cli/prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -861,7 +861,14 @@ func ReadStringXSWDPrompt(l *readline.Instance, onClose chan bool, prompt string
for !validValue {
go func() {
line, err := l.ReadPasswordWithConfig(conf)
if err != nil {
if err == readline.ErrInterrupt {
if len(line) == 0 {
logger.Info("Ctrl-C received, Exiting")
os.Exit(0)
}
} else if err == io.EOF {
os.Exit(0)
} else if err != nil {
logger.Error(err, "Error reading input")
}
value := strings.ToUpper(string(line))
Expand Down
2 changes: 1 addition & 1 deletion cmd/simulator/wallets.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func register_wallets(chain *blockchain.Blockchain) {

if v, ok := globals.Arguments["--use-xswd"]; ok && v.(bool) {
// XSWD server accept everything by default
xswd.NewXSWDServerWithPort(wallet_ports_xswd_start+i, wallets[i], func(app *xswd.ApplicationData) bool {
xswd.NewXSWDServerWithPort(wallet_ports_xswd_start+i, wallets[i], false, func(app *xswd.ApplicationData) bool {
return true
}, func(app *xswd.ApplicationData, request *jrpc2.Request) xswd.Permission {
return xswd.Allow
Expand Down
58 changes: 53 additions & 5 deletions walletapi/xswd/methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ package xswd

import (
"context"
"fmt"
"strings"

"github.com/deroproject/derohe/rpc"
"github.com/deroproject/derohe/walletapi"
"github.com/deroproject/derohe/walletapi/rpcserver"
)

Expand All @@ -15,6 +18,11 @@ type Subscribe_Params struct {
Event rpc.EventType `json:"event"`
}

type Signature_Result struct {
Signer string `json:"signer"`
Message string `json:"message"`
}

func HasMethod(ctx context.Context, p HasMethod_Params) bool {
w := rpcserver.FromContext(ctx)
xswd := w.Extra["xswd"].(*XSWD)
Expand Down Expand Up @@ -50,9 +58,49 @@ func Unsubscribe(ctx context.Context, p Subscribe_Params) bool {
return true
}

// TODO WIP sign data
func SignData(ctx context.Context, p string) string {
// w := rpcserver.FromContext(ctx)
// xswd := w.Extra["xswd"].(*XSWD)
return "WIP"
// SignData returned as DERO signed message
func SignData(ctx context.Context, p []byte) (signed []byte, err error) {
w := rpcserver.FromContext(ctx)
xswd := w.Extra["xswd"].(*XSWD)
if xswd.wallet == nil {
err = fmt.Errorf("XSWD could not sign data")
return
}

signed = xswd.wallet.SignData(p)

return
}

// CheckSignature of DERO signed message
func CheckSignature(ctx context.Context, p []byte) (result Signature_Result, err error) {
w := rpcserver.FromContext(ctx)
xswd := w.Extra["xswd"].(*XSWD)
if xswd.wallet == nil {
err = fmt.Errorf("XSWD could not check signature")
return
}

var address *rpc.Address
var messageBytes []byte
address, messageBytes, err = xswd.wallet.CheckSignature(p)
if err != nil {
return
}

result.Signer = address.String()
result.Message = strings.TrimSpace(string(messageBytes))

return
}

// GetDaemon endpoint from connected wallet
func GetDaemon(ctx context.Context) (result string, err error) {
if walletapi.Daemon_Endpoint_Active != "" {
result = walletapi.Daemon_Endpoint_Active
} else {
err = fmt.Errorf("XSWD could not get daemon endpoint from wallet")
}

return
}
Loading