diff --git a/accounts/abi/abi.go b/accounts/abi/abi.go index 205dc300b0ef..f69ef4d60ce3 100644 --- a/accounts/abi/abi.go +++ b/accounts/abi/abi.go @@ -20,6 +20,7 @@ import ( "encoding/json" "fmt" "io" + "bytes" ) // The ABI holds information about a contract's context and available @@ -137,3 +138,14 @@ func (abi *ABI) UnmarshalJSON(data []byte) error { return nil } + +// methodById looks up a method by the 4-byte id +// returns nil if none found +func (abi *ABI) MethodById(sigdata []byte) *Method { + for _, method := range abi.Methods{ + if bytes.Equal(method.Id(), sigdata) { + return &method + } + } + return nil +} \ No newline at end of file diff --git a/accounts/abi/event.go b/accounts/abi/event.go index 44ed7b8df256..812d602c5bfb 100644 --- a/accounts/abi/event.go +++ b/accounts/abi/event.go @@ -75,7 +75,7 @@ func (e Event) tupleUnpack(v interface{}, output []byte) error { // need to move this up because they read sequentially j += input.Type.Size } - marshalledValue, err := toGoType((i+j)*32, input.Type, output) + marshalledValue, err := ToGoType((i+j)*32, input.Type, output) if err != nil { return err } @@ -126,7 +126,7 @@ func (e Event) singleUnpack(v interface{}, output []byte) error { value := valueOf.Elem() - marshalledValue, err := toGoType(0, e.Inputs[0].Type, output) + marshalledValue, err := ToGoType(0, e.Inputs[0].Type, output) if err != nil { return err } diff --git a/accounts/abi/method.go b/accounts/abi/method.go index d8838e9ed68f..7b7525ec9ddf 100644 --- a/accounts/abi/method.go +++ b/accounts/abi/method.go @@ -99,7 +99,7 @@ func (method Method) tupleUnpack(v interface{}, output []byte) error { // need to move this up because they read sequentially j += toUnpack.Type.Size } - marshalledValue, err := toGoType((i+j)*32, toUnpack.Type, output) + marshalledValue, err := ToGoType((i+j)*32, toUnpack.Type, output) if err != nil { return err } @@ -146,7 +146,7 @@ func (method Method) singleUnpack(v interface{}, output []byte) error { value := valueOf.Elem() - marshalledValue, err := toGoType(0, method.Outputs[0].Type, output) + marshalledValue, err := ToGoType(0, method.Outputs[0].Type, output) if err != nil { return err } diff --git a/accounts/abi/unpack.go b/accounts/abi/unpack.go index 57732797b6e1..e1828dfae16f 100644 --- a/accounts/abi/unpack.go +++ b/accounts/abi/unpack.go @@ -127,7 +127,7 @@ func forEachUnpack(t Type, output []byte, start, size int) (interface{}, error) if t.Elem.T == ArrayTy && j != 0 { i = start + t.Elem.Size*32*j } - inter, err := toGoType(i, *t.Elem, output) + inter, err := ToGoType(i, *t.Elem, output) if err != nil { return nil, err } @@ -139,9 +139,9 @@ func forEachUnpack(t Type, output []byte, start, size int) (interface{}, error) return refSlice.Interface(), nil } -// toGoType parses the output bytes and recursively assigns the value of these bytes +// ToGoType parses the output bytes and recursively assigns the value of these bytes // into a go type with accordance with the ABI spec. -func toGoType(index int, t Type, output []byte) (interface{}, error) { +func ToGoType(index int, t Type, output []byte) (interface{}, error) { if index+32 > len(output) { return nil, fmt.Errorf("abi: cannot marshal in to go type: length insufficient %d require %d", len(output), index+32) } diff --git a/accounts/usbwallet/hub.go b/accounts/usbwallet/hub.go index 61fc98ccc80e..640320bc9143 100644 --- a/accounts/usbwallet/hub.go +++ b/accounts/usbwallet/hub.go @@ -127,7 +127,7 @@ func (hub *Hub) refreshWallets() { // breaking the Ledger protocol if that is waiting for user confirmation. This // is a bug acknowledged at Ledger, but it won't be fixed on old devices so we // need to prevent concurrent comms ourselves. The more elegant solution would - // be to ditch enumeration in favor of hutplug events, but that don't work yet + // be to ditch enumeration in favor of hotplug events, but that don't work yet // on Windows so if we need to hack it anyway, this is more elegant for now. hub.commsLock.Lock() if hub.commsPend > 0 { // A confirmation is pending, don't refresh diff --git a/accounts/usbwallet/wallet.go b/accounts/usbwallet/wallet.go index 8b3b5a522402..6cef6e0fb02a 100644 --- a/accounts/usbwallet/wallet.go +++ b/accounts/usbwallet/wallet.go @@ -99,7 +99,7 @@ type wallet struct { // // As such, a hardware wallet needs two locks to function correctly. A state // lock can be used to protect the wallet's software-side internal state, which - // must not be held exlusively during hardware communication. A communication + // must not be held exclusively during hardware communication. A communication // lock can be used to achieve exclusive access to the device itself, this one // however should allow "skipping" waiting for operations that might want to // use the device, but can live without too (e.g. account self-derivation). diff --git a/common/types.go b/common/types.go index fdc67480c234..918f78bab570 100644 --- a/common/types.go +++ b/common/types.go @@ -23,6 +23,7 @@ import ( "math/rand" "reflect" + "encoding/json" "github.com/ethereum/go-ethereum/common/hexutil" "github.com/ethereum/go-ethereum/crypto/sha3" ) @@ -240,3 +241,56 @@ func (a *UnprefixedAddress) UnmarshalText(input []byte) error { func (a UnprefixedAddress) MarshalText() ([]byte, error) { return []byte(hex.EncodeToString(a[:])), nil } + +// MixedcaseAddress retains the original string, which may or may not be +// correctly checksummed +// +// TODO! Should we really keep both addr and original, or _only_ original, and +// always calculate addr on the fly? That would reduce the possibilities for errors +// if some caller modifies the original at some point. NB: If we do so, we should still +// parse the addr in UnmarshalJSON to ensure that the format is correct, e.g. correct size and +// hex-encoded and such +type MixedcaseAddress struct { + addr Address + original string +} +// NewMixedcaseAddress constructor (mainly for testing) +func NewMixedcaseAddress(addr Address) MixedcaseAddress { + return MixedcaseAddress{addr: addr, original: addr.Hex()} +} + +// UnmarshalJSON parses MixedcaseAddress +func (ma *MixedcaseAddress) UnmarshalJSON(input []byte) error { + if err := hexutil.UnmarshalFixedJSON(addressT, input, ma.addr[:]); err != nil { + return err + } + return json.Unmarshal(input, &ma.original) +} + +// MarshalJSON marshals the original value +func (ma *MixedcaseAddress) MarshalJSON() ([]byte, error) { + return json.Marshal(ma.original) +} + +// Address returns the address +func (ma *MixedcaseAddress) Address() Address { + return ma.addr +} + +// String implements fmt.Stringer +func (ma *MixedcaseAddress) String() string { + if ma.ValidChecksum() { + return fmt.Sprintf("%s [chksum ok]", ma.original) + } + return fmt.Sprintf("%s [chksum INVALID]", ma.original) +} + +// ValidChecksum returns true if the address has valid checksum +func (ma *MixedcaseAddress) ValidChecksum() bool { + return ma.original == ma.addr.Hex() +} + +// Original returns the mixed-case input string +func (ma *MixedcaseAddress) Original() string { + return ma.original +} diff --git a/common/types_test.go b/common/types_test.go index db636812ce8a..9e0c5be3ad2e 100644 --- a/common/types_test.go +++ b/common/types_test.go @@ -18,6 +18,7 @@ package common import ( "encoding/json" + "math/big" "strings" "testing" @@ -149,3 +150,46 @@ func BenchmarkAddressHex(b *testing.B) { testAddr.Hex() } } + +func TestMixedcaseAccount_Address(t *testing.T) { + + // https://github.com/ethereum/EIPs/blob/master/EIPS/eip-55.md + // Note: 0X{checksum_addr} is not valid according to spec above + + var res []struct { + A MixedcaseAddress + Valid bool + } + if err := json.Unmarshal([]byte(`[ + {"A" : "0xae967917c465db8578ca9024c205720b1a3651A9", "Valid": false}, + {"A" : "0xAe967917c465db8578ca9024c205720b1a3651A9", "Valid": true}, + {"A" : "0XAe967917c465db8578ca9024c205720b1a3651A9", "Valid": false}, + {"A" : "0x1111111111111111111112222222222223333323", "Valid": true} + ]`), &res); err != nil { + t.Fatal(err) + } + + for _, r := range res { + if got := r.A.ValidChecksum(); got != r.Valid { + t.Errorf("Expected checksum %v, got checksum %v, input %v", r.Valid, got, r.A.String()) + } + } + + //These should throw exceptions: + var r2 []MixedcaseAddress + for _, r := range []string{ + `["0x11111111111111111111122222222222233333"]`, // Too short + `["0x111111111111111111111222222222222333332"]`, // Too short + `["0x11111111111111111111122222222222233333234"]`, // Too long + `["0x111111111111111111111222222222222333332344"]`, // Too long + `["1111111111111111111112222222222223333323"]`, // Missing 0x + `["x1111111111111111111112222222222223333323"]`, // Missing 0 + `["0xG111111111111111111112222222222223333323"]`, //Non-hex + } { + if err := json.Unmarshal([]byte(r), &r2); err == nil { + t.Errorf("Expected failure, input %v", r) + } + + } + +} diff --git a/rpc/client.go b/rpc/client.go index 8aa84ec98279..68745c6cbec1 100644 --- a/rpc/client.go +++ b/rpc/client.go @@ -33,6 +33,7 @@ import ( "time" "github.com/ethereum/go-ethereum/log" + "os" ) var ( @@ -171,6 +172,8 @@ func DialContext(ctx context.Context, rawurl string) (*Client, error) { return DialHTTP(rawurl) case "ws", "wss": return DialWebsocket(ctx, rawurl, "") + case "stdio": + return DialStdIO(ctx) case "": return DialIPC(ctx, rawurl) default: @@ -178,13 +181,51 @@ func DialContext(ctx context.Context, rawurl string) (*Client, error) { } } +type StdIOConn struct{} + +func (io StdIOConn) Read(b []byte) (n int, err error) { + return os.Stdin.Read(b) +} + +func (io StdIOConn) Write(b []byte) (n int, err error) { + return os.Stdout.Write(b) +} + +func (io StdIOConn) Close() error { + return nil +} + +func (io StdIOConn) LocalAddr() net.Addr { + return &net.UnixAddr{Name: "stdio", Net: "stdio"} +} + +func (io StdIOConn) RemoteAddr() net.Addr { + return &net.UnixAddr{Name: "stdio", Net: "stdio"} +} + +func (io StdIOConn) SetDeadline(t time.Time) error { + return &net.OpError{Op: "set", Net: "stdio", Source: nil, Addr: nil, Err: errors.New("deadline not supported")} +} + +func (io StdIOConn) SetReadDeadline(t time.Time) error { + return &net.OpError{Op: "set", Net: "stdio", Source: nil, Addr: nil, Err: errors.New("deadline not supported")} +} + +func (io StdIOConn) SetWriteDeadline(t time.Time) error { + return &net.OpError{Op: "set", Net: "stdio", Source: nil, Addr: nil, Err: errors.New("deadline not supported")} +} +func DialStdIO(ctx context.Context) (*Client, error) { + return newClient(ctx, func(_ context.Context) (net.Conn, error) { + return StdIOConn{}, nil + }) +} + func newClient(initctx context.Context, connectFunc func(context.Context) (net.Conn, error)) (*Client, error) { conn, err := connectFunc(initctx) if err != nil { return nil, err } _, isHTTP := conn.(*httpConn) - c := &Client{ writeConn: conn, isHTTP: isHTTP, @@ -524,13 +565,13 @@ func (c *Client) dispatch(conn net.Conn) { } case err := <-c.readErr: - log.Debug(fmt.Sprintf("<-readErr: %v", err)) + log.Debug("<-readErr", "err", err) c.closeRequestOps(err) conn.Close() reading = false case newconn := <-c.reconnected: - log.Debug(fmt.Sprintf("<-reconnected: (reading=%t) %v", reading, conn.RemoteAddr())) + log.Debug("<-reconnected", "reading", reading, "remote", conn.RemoteAddr()) if reading { // Wait for the previous read loop to exit. This is a rare case. conn.Close() @@ -587,7 +628,7 @@ func (c *Client) closeRequestOps(err error) { func (c *Client) handleNotification(msg *jsonrpcMessage) { if !strings.HasSuffix(msg.Method, notificationMethodSuffix) { - log.Debug(fmt.Sprint("dropping non-subscription message: ", msg)) + log.Debug("dropping non-subscription message", "msg", msg) return } var subResult struct { @@ -595,7 +636,7 @@ func (c *Client) handleNotification(msg *jsonrpcMessage) { Result json.RawMessage `json:"result"` } if err := json.Unmarshal(msg.Params, &subResult); err != nil { - log.Debug(fmt.Sprint("dropping invalid subscription message: ", msg)) + log.Debug("dropping invalid subscription message", "msg", msg) return } if c.subs[subResult.ID] != nil { @@ -606,7 +647,7 @@ func (c *Client) handleNotification(msg *jsonrpcMessage) { func (c *Client) handleResponse(msg *jsonrpcMessage) { op := c.respWait[string(msg.ID)] if op == nil { - log.Debug(fmt.Sprintf("unsolicited response %v", msg)) + log.Debug("unsolicited response", "msg", msg) return } delete(c.respWait, string(msg.ID)) diff --git a/rpc/http.go b/rpc/http.go index a26559b1221f..3b779a1c0f21 100644 --- a/rpc/http.go +++ b/rpc/http.go @@ -159,11 +159,16 @@ func (srv *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) { // All checks passed, create a codec that reads direct from the request body // untilEOF and writes the response to w and order the server to process a // single request. + ctx := context.Background() + ctx = context.WithValue(ctx, "remote", r.RemoteAddr) + ctx = context.WithValue(ctx, "scheme", r.Proto) + ctx = context.WithValue(ctx, "local", r.Host) + codec := NewJSONCodec(&httpReadWriteNopCloser{r.Body, w}) defer codec.Close() w.Header().Set("content-type", contentType) - srv.ServeSingleRequest(codec, OptionMethodInvocation) + srv.ServeSingleRequest(codec, OptionMethodInvocation, ctx) } // validateRequest returns a non-zero response code and error message if the diff --git a/rpc/server.go b/rpc/server.go index 30c288349e86..1c63efd354e5 100644 --- a/rpc/server.go +++ b/rpc/server.go @@ -27,6 +27,7 @@ import ( "github.com/ethereum/go-ethereum/log" "gopkg.in/fatih/set.v0" + "time" ) const MetadataApi = "rpc" @@ -42,6 +43,19 @@ const ( OptionSubscriptions = 1 << iota // support pub sub ) +// RPCLogger is an interface for logging rpc requests and responses +type RPCLogger interface { + Store(record *RPCInvocationRecord) +} + +// RPCInvocationRecord captures an rpc request and response, for audit logging +type RPCInvocationRecord struct { + Method string + Args []string + Time time.Time + Response string +} + // NewServer will create a new server instance with no registered handlers. func NewServer() *Server { server := &Server{ @@ -58,6 +72,10 @@ func NewServer() *Server { return server } +func (s *Server) SetAuditLogger(logger RPCLogger) { + s.auditlog = &logger +} + // RPCService gives meta information about the server. // e.g. gives information about the loaded modules. type RPCService struct { @@ -125,7 +143,7 @@ func (s *Server) RegisterName(name string, rcvr interface{}) error { // If singleShot is true it will process a single request, otherwise it will handle // requests until the codec returns an error when reading a request (in most cases // an EOF). It executes requests in parallel when singleShot is false. -func (s *Server) serveRequest(codec ServerCodec, singleShot bool, options CodecOption) error { +func (s *Server) serveRequest(codec ServerCodec, singleShot bool, options CodecOption, ctx context.Context) error { var pend sync.WaitGroup defer func() { @@ -140,7 +158,8 @@ func (s *Server) serveRequest(codec ServerCodec, singleShot bool, options CodecO s.codecsMu.Unlock() }() - ctx, cancel := context.WithCancel(context.Background()) + // ctx, cancel := context.WithCancel(context.Background()) + ctx, cancel := context.WithCancel(ctx) defer cancel() // if the codec supports notification include a notifier that callbacks can use @@ -215,14 +234,14 @@ func (s *Server) serveRequest(codec ServerCodec, singleShot bool, options CodecO // stopped. In either case the codec is closed. func (s *Server) ServeCodec(codec ServerCodec, options CodecOption) { defer codec.Close() - s.serveRequest(codec, false, options) + s.serveRequest(codec, false, options, context.Background()) } // ServeSingleRequest reads and processes a single RPC request from the given codec. It will not // close the codec unless a non-recoverable error has occurred. Note, this method will return after // a single request has been processed! -func (s *Server) ServeSingleRequest(codec ServerCodec, options CodecOption) { - s.serveRequest(codec, true, options) +func (s *Server) ServeSingleRequest(codec ServerCodec, options CodecOption, ctx context.Context) { + s.serveRequest(codec, true, options, ctx) } // Stop will stop reading new requests, wait for stopPendingRequestTimeout to allow pending requests to finish, @@ -253,6 +272,20 @@ func (s *Server) createSubscription(ctx context.Context, c ServerCodec, req *ser return reply[0].Interface().(*Subscription).ID, nil } +// vstring 'resolves' values into string-values +func vstring(v reflect.Value) string { + // Resolve pointers (optional args) + if v.Kind() == reflect.Ptr && !v.IsNil() { + v = v.Elem() + } + if v.CanInterface() { + if str, ok := v.Interface().(fmt.Stringer); ok { + return str.String() + } + } + return v.String() +} + // handle executes a request and returns the response from the callback. func (s *Server) handle(ctx context.Context, codec ServerCodec, req *serverRequest) (interface{}, func()) { if req.err != nil { @@ -312,14 +345,22 @@ func (s *Server) handle(ctx context.Context, codec ServerCodec, req *serverReque if len(reply) == 0 { return codec.CreateResponse(req.id, nil), nil } + record := RPCInvocationRecord{Method: req.callb.method.Name} + if s.auditlog != nil { + for _, a := range req.args { + record.Args = append(record.Args, vstring(a)) + } + defer (*s.auditlog).Store(&record) + } if req.callb.errPos >= 0 { // test if method returned an error if !reply[req.callb.errPos].IsNil() { e := reply[req.callb.errPos].Interface().(error) - res := codec.CreateErrorResponse(&req.id, &callbackError{e.Error()}) - return res, nil + record.Response = e.Error() + return codec.CreateErrorResponse(&req.id, &callbackError{e.Error()}), nil } } + record.Response = vstring(reply[0]) return codec.CreateResponse(req.id, reply[0].Interface()), nil } diff --git a/rpc/types.go b/rpc/types.go index f2375604ed95..d9c0e2b4218f 100644 --- a/rpc/types.go +++ b/rpc/types.go @@ -74,6 +74,7 @@ type Server struct { run int32 codecsMu sync.Mutex codecs *set.Set + auditlog *RPCLogger } // rpcRequest represents a raw incoming RPC request