From 8d49a45008bd84fcee31270749c6447525c2aa97 Mon Sep 17 00:00:00 2001 From: Daniel Whitenack Date: Tue, 21 Jun 2016 09:34:38 -0500 Subject: [PATCH] return public key on create account --- src/Godeps/Godeps.json | 5 + src/gethdep.go | 14 +- src/library.go | 8 +- src/vendor/github.com/pkg/errors/.gitignore | 24 ++ src/vendor/github.com/pkg/errors/.travis.yml | 10 + src/vendor/github.com/pkg/errors/LICENSE | 24 ++ src/vendor/github.com/pkg/errors/README.md | 52 ++++ src/vendor/github.com/pkg/errors/errors.go | 241 +++++++++++++++++++ 8 files changed, 371 insertions(+), 7 deletions(-) create mode 100644 src/vendor/github.com/pkg/errors/.gitignore create mode 100644 src/vendor/github.com/pkg/errors/.travis.yml create mode 100644 src/vendor/github.com/pkg/errors/LICENSE create mode 100644 src/vendor/github.com/pkg/errors/README.md create mode 100644 src/vendor/github.com/pkg/errors/errors.go diff --git a/src/Godeps/Godeps.json b/src/Godeps/Godeps.json index e8b9757cc..f9465e2ee 100644 --- a/src/Godeps/Godeps.json +++ b/src/Godeps/Godeps.json @@ -327,6 +327,11 @@ "ImportPath": "github.com/peterh/liner", "Rev": "72909af234e0e355af10d0ace679446a6c5d7ec3" }, + { + "ImportPath": "github.com/pkg/errors", + "Comment": "v0.4.0", + "Rev": "d814416a46cbb066b728cfff58d30a986bc9ddbe" + }, { "ImportPath": "github.com/rcrowley/go-metrics", "Rev": "eeba7bd0dd01ace6e690fa833b3f22aaec29af43" diff --git a/src/gethdep.go b/src/gethdep.go index a0bba2c85..01d4fae8d 100644 --- a/src/gethdep.go +++ b/src/gethdep.go @@ -5,7 +5,9 @@ import ( "fmt" "github.com/ethereum/go-ethereum/accounts" + "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/node" + errextra "github.com/pkg/errors" ) var ( @@ -14,7 +16,7 @@ var ( ) // createAccount creates an internal geth account -func createAccount(password, keydir string) (string, error) { +func createAccount(password, keydir string) (string, string, error) { var sync *[]node.Service w := true @@ -22,11 +24,17 @@ func createAccount(password, keydir string) (string, error) { account, err := accman.NewAccount(password, w) if err != nil { - return "", err + return "", "", errextra.Wrap(err, "Account manager could not create the account") } address := fmt.Sprintf("{%x}", account.Address) - return address, nil + key, err := crypto.LoadECDSA(account.File) + if err != nil { + return address, "", errextra.Wrap(err, "Could not load the key") + } + pubKey := string(crypto.FromECDSAPub(&key.PublicKey)) + + return address, pubKey, nil } diff --git a/src/library.go b/src/library.go index 910af9b77..5b24300a8 100644 --- a/src/library.go +++ b/src/library.go @@ -7,15 +7,15 @@ import ( ) //export doCreateAccount -func doCreateAccount(password, keydir *C.char) (*C.char, C.int) { +func doCreateAccount(password, keydir *C.char) (*C.char, *C.char, C.int) { // This is equivalent to creating an account from the command line, // just modified to handle the function arg passing - address, err := createAccount(C.GoString(password), C.GoString(keydir)) + address, pubKey, err := createAccount(C.GoString(password), C.GoString(keydir)) if err != nil { fmt.Fprintln(os.Stderr, err) - return C.CString(""), -1 + return C.CString(""), C.CString(""), -1 } - return C.CString(address), 0 + return C.CString(address), C.CString(pubKey), 0 } // export doStartNode diff --git a/src/vendor/github.com/pkg/errors/.gitignore b/src/vendor/github.com/pkg/errors/.gitignore new file mode 100644 index 000000000..daf913b1b --- /dev/null +++ b/src/vendor/github.com/pkg/errors/.gitignore @@ -0,0 +1,24 @@ +# Compiled Object files, Static and Dynamic libs (Shared Objects) +*.o +*.a +*.so + +# Folders +_obj +_test + +# Architecture specific extensions/prefixes +*.[568vq] +[568vq].out + +*.cgo1.go +*.cgo2.c +_cgo_defun.c +_cgo_gotypes.go +_cgo_export.* + +_testmain.go + +*.exe +*.test +*.prof diff --git a/src/vendor/github.com/pkg/errors/.travis.yml b/src/vendor/github.com/pkg/errors/.travis.yml new file mode 100644 index 000000000..13f087a7d --- /dev/null +++ b/src/vendor/github.com/pkg/errors/.travis.yml @@ -0,0 +1,10 @@ +language: go +go_import_path: github.com/pkg/errors +go: + - 1.4.3 + - 1.5.4 + - 1.6.1 + - tip + +script: + - go test -v ./... diff --git a/src/vendor/github.com/pkg/errors/LICENSE b/src/vendor/github.com/pkg/errors/LICENSE new file mode 100644 index 000000000..fafcaafdc --- /dev/null +++ b/src/vendor/github.com/pkg/errors/LICENSE @@ -0,0 +1,24 @@ +Copyright (c) 2015, Dave Cheney +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +* Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + +* Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + diff --git a/src/vendor/github.com/pkg/errors/README.md b/src/vendor/github.com/pkg/errors/README.md new file mode 100644 index 000000000..ab0701981 --- /dev/null +++ b/src/vendor/github.com/pkg/errors/README.md @@ -0,0 +1,52 @@ +# errors [![Travis-CI](https://travis-ci.org/pkg/errors.svg)](https://travis-ci.org/pkg/errors) [![GoDoc](https://godoc.org/github.com/pkg/errors?status.svg)](http://godoc.org/github.com/pkg/errors) [![Report card](https://goreportcard.com/badge/github.com/pkg/errors)](https://goreportcard.com/report/github.com/pkg/errors) + +Package errors provides simple error handling primitives. + +The traditional error handling idiom in Go is roughly akin to +```go +if err != nil { + return err +} +``` +which applied recursively up the call stack results in error reports without context or debugging information. The errors package allows programmers to add context to the failure path in their code in a way that does not destroy the original value of the error. + +## Adding context to an error + +The errors.Wrap function returns a new error that adds context to the original error. For example +```go +_, err := ioutil.ReadAll(r) +if err != nil { + return errors.Wrap(err, "read failed") +} +``` +In addition, `errors.Wrap` records the file and line where it was called, allowing the programmer to retrieve the path to the original error. + +## Retrieving the cause of an error + +Using `errors.Wrap` constructs a stack of errors, adding context to the preceding error. Depending on the nature of the error it may be necessary to recurse the operation of errors.Wrap to retrieve the original error for inspection. Any error value which implements this interface can be inspected by `errors.Cause`. +```go +type causer interface { + Cause() error +} +``` +`errors.Cause` will recursively retrieve the topmost error which does not implement `causer`, which is assumed to be the original cause. For example: +```go +switch err := errors.Cause(err).(type) { +case *MyError: + // handle specifically +default: + // unknown error +} +``` + +Would you like to know more? Read the [blog post](http://dave.cheney.net/2016/04/27/dont-just-check-errors-handle-them-gracefully). + +## Contributing + +We welcome pull requests, bug fixes and issue reports. With that said, the bar for adding new symbols to this package is intentionally set high. + +Before proposing a change, please discuss your change by raising an issue. + +## Licence + +MIT diff --git a/src/vendor/github.com/pkg/errors/errors.go b/src/vendor/github.com/pkg/errors/errors.go new file mode 100644 index 000000000..254c6dd06 --- /dev/null +++ b/src/vendor/github.com/pkg/errors/errors.go @@ -0,0 +1,241 @@ +// Package errors provides simple error handling primitives. +// +// The traditional error handling idiom in Go is roughly akin to +// +// if err != nil { +// return err +// } +// +// which applied recursively up the call stack results in error reports +// without context or debugging information. The errors package allows +// programmers to add context to the failure path in their code in a way +// that does not destroy the original value of the error. +// +// Adding context to an error +// +// The errors.Wrap function returns a new error that adds context to the +// original error. For example +// +// _, err := ioutil.ReadAll(r) +// if err != nil { +// return errors.Wrap(err, "read failed") +// } +// +// In addition, errors.Wrap records the file and line where it was called, +// allowing the programmer to retrieve the path to the original error. +// +// Retrieving the cause of an error +// +// Using errors.Wrap constructs a stack of errors, adding context to the +// preceding error. Depending on the nature of the error it may be necessary +// to reverse the operation of errors.Wrap to retrieve the original error +// for inspection. Any error value which implements this interface +// +// type causer interface { +// Cause() error +// } +// +// can be inspected by errors.Cause. errors.Cause will recursively retrieve +// the topmost error which does not implement causer, which is assumed to be +// the original cause. For example: +// +// switch err := errors.Cause(err).(type) { +// case *MyError: +// // handle specifically +// default: +// // unknown error +// } +package errors + +import ( + "errors" + "fmt" + "io" + "runtime" + "strings" +) + +// location represents a program counter that +// implements the Location() method. +type location uintptr + +func (l location) Location() (string, int) { + pc := uintptr(l) - 1 + fn := runtime.FuncForPC(pc) + if fn == nil { + return "unknown", 0 + } + + file, line := fn.FileLine(pc) + + // Here we want to get the source file path relative to the compile time + // GOPATH. As of Go 1.6.x there is no direct way to know the compiled + // GOPATH at runtime, but we can infer the number of path segments in the + // GOPATH. We note that fn.Name() returns the function name qualified by + // the import path, which does not include the GOPATH. Thus we can trim + // segments from the beginning of the file path until the number of path + // separators remaining is one more than the number of path separators in + // the function name. For example, given: + // + // GOPATH /home/user + // file /home/user/src/pkg/sub/file.go + // fn.Name() pkg/sub.Type.Method + // + // We want to produce: + // + // pkg/sub/file.go + // + // From this we can easily see that fn.Name() has one less path separator + // than our desired output. We count separators from the end of the file + // path until it finds two more than in the function name and then move + // one character forward to preserve the initial path segment without a + // leading separator. + const sep = "/" + goal := strings.Count(fn.Name(), sep) + 2 + i := len(file) + for n := 0; n < goal; n++ { + i = strings.LastIndex(file[:i], sep) + if i == -1 { + // not enough separators found, set i so that the slice expression + // below leaves file unmodified + i = -len(sep) + break + } + } + // get back to 0 or trim the leading separator + file = file[i+len(sep):] + + return file, line +} + +// New returns an error that formats as the given text. +func New(text string) error { + pc, _, _, _ := runtime.Caller(1) + return struct { + error + location + }{ + errors.New(text), + location(pc), + } +} + +type cause struct { + cause error + message string +} + +func (c cause) Error() string { return c.Message() + ": " + c.Cause().Error() } +func (c cause) Cause() error { return c.cause } +func (c cause) Message() string { return c.message } + +// Errorf formats according to a format specifier and returns the string +// as a value that satisfies error. +func Errorf(format string, args ...interface{}) error { + pc, _, _, _ := runtime.Caller(1) + return struct { + error + location + }{ + fmt.Errorf(format, args...), + location(pc), + } +} + +// Wrap returns an error annotating the cause with message. +// If cause is nil, Wrap returns nil. +func Wrap(cause error, message string) error { + if cause == nil { + return nil + } + pc, _, _, _ := runtime.Caller(1) + return wrap(cause, message, pc) +} + +// Wrapf returns an error annotating the cause with the format specifier. +// If cause is nil, Wrapf returns nil. +func Wrapf(cause error, format string, args ...interface{}) error { + if cause == nil { + return nil + } + pc, _, _, _ := runtime.Caller(1) + return wrap(cause, fmt.Sprintf(format, args...), pc) +} + +func wrap(err error, msg string, pc uintptr) error { + return struct { + cause + location + }{ + cause{ + cause: err, + message: msg, + }, + location(pc), + } +} + +type causer interface { + Cause() error +} + +// Cause returns the underlying cause of the error, if possible. +// An error value has a cause if it implements the following +// interface: +// +// type Causer interface { +// Cause() error +// } +// +// If the error does not implement Cause, the original error will +// be returned. If the error is nil, nil will be returned without further +// investigation. +func Cause(err error) error { + for err != nil { + cause, ok := err.(causer) + if !ok { + break + } + err = cause.Cause() + } + return err +} + +// Fprint prints the error to the supplied writer. +// If the error implements the Causer interface described in Cause +// Print will recurse into the error's cause. +// If the error implements the inteface: +// +// type Location interface { +// Location() (file string, line int) +// } +// +// Print will also print the file and line of the error. +// If err is nil, nothing is printed. +func Fprint(w io.Writer, err error) { + type location interface { + Location() (string, int) + } + type message interface { + Message() string + } + + for err != nil { + if err, ok := err.(location); ok { + file, line := err.Location() + fmt.Fprintf(w, "%s:%d: ", file, line) + } + switch err := err.(type) { + case message: + fmt.Fprintln(w, err.Message()) + default: + fmt.Fprintln(w, err.Error()) + } + + cause, ok := err.(causer) + if !ok { + break + } + err = cause.Cause() + } +}