aboutsummaryrefslogtreecommitdiffstats
path: root/signer/core/stdioui.go
blob: 342488a707c551dac7e51307d780224df78f5ccd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// Copyright 2018 The go-ethereum Authors
// This file is part of go-ethereum.
//
// go-ethereum is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// go-ethereum is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
//

package core

import (
    "context"
    "sync"

    "github.com/dexon-foundation/dexon/internal/ethapi"
    "github.com/dexon-foundation/dexon/log"
    "github.com/dexon-foundation/dexon/rpc"
)

type StdIOUI struct {
    client rpc.Client
    mu     sync.Mutex
}

func NewStdIOUI() *StdIOUI {
    log.Info("NewStdIOUI")
    client, err := rpc.DialContext(context.Background(), "stdio://")
    if err != nil {
        log.Crit("Could not create stdio client", "err", err)
    }
    return &StdIOUI{client: *client}
}

// dispatch sends a request over the stdio
func (ui *StdIOUI) dispatch(serviceMethod string, args interface{}, reply interface{}) error {
    err := ui.client.Call(&reply, serviceMethod, args)
    if err != nil {
        log.Info("Error", "exc", err.Error())
    }
    return err
}

func (ui *StdIOUI) ApproveTx(request *SignTxRequest) (SignTxResponse, error) {
    var result SignTxResponse
    err := ui.dispatch("ApproveTx", request, &result)
    return result, err
}

func (ui *StdIOUI) ApproveSignData(request *SignDataRequest) (SignDataResponse, error) {
    var result SignDataResponse
    err := ui.dispatch("ApproveSignData", request, &result)
    return result, err
}

func (ui *StdIOUI) ApproveExport(request *ExportRequest) (ExportResponse, error) {
    var result ExportResponse
    err := ui.dispatch("ApproveExport", request, &result)
    return result, err
}

func (ui *StdIOUI) ApproveImport(request *ImportRequest) (ImportResponse, error) {
    var result ImportResponse
    err := ui.dispatch("ApproveImport", request, &result)
    return result, err
}

func (ui *StdIOUI) ApproveListing(request *ListRequest) (ListResponse, error) {
    var result ListResponse
    err := ui.dispatch("ApproveListing", request, &result)
    return result, err
}

func (ui *StdIOUI) ApproveNewAccount(request *NewAccountRequest) (NewAccountResponse, error) {
    var result NewAccountResponse
    err := ui.dispatch("ApproveNewAccount", request, &result)
    return result, err
}

func (ui *StdIOUI) ShowError(message string) {
    err := ui.dispatch("ShowError", &Message{message}, nil)
    if err != nil {
        log.Info("Error calling 'ShowError'", "exc", err.Error(), "msg", message)
    }
}

func (ui *StdIOUI) ShowInfo(message string) {
    err := ui.dispatch("ShowInfo", Message{message}, nil)
    if err != nil {
        log.Info("Error calling 'ShowInfo'", "exc", err.Error(), "msg", message)
    }
}
func (ui *StdIOUI) OnApprovedTx(tx ethapi.SignTransactionResult) {
    err := ui.dispatch("OnApprovedTx", tx, nil)
    if err != nil {
        log.Info("Error calling 'OnApprovedTx'", "exc", err.Error(), "tx", tx)
    }
}

func (ui *StdIOUI) OnSignerStartup(info StartupInfo) {
    err := ui.dispatch("OnSignerStartup", info, nil)
    if err != nil {
        log.Info("Error calling 'OnSignerStartup'", "exc", err.Error(), "info", info)
    }
}
func (ui *StdIOUI) OnInputRequired(info UserInputRequest) (UserInputResponse, error) {
    var result UserInputResponse
    err := ui.dispatch("OnInputRequired", info, &result)
    if err != nil {
        log.Info("Error calling 'OnInputRequired'", "exc", err.Error(), "info", info)
    }
    return result, err
}