Skip to content

Commit

Permalink
Merge branch 'frontier/natspec' of https://github.com/ethersphere/go-…
Browse files Browse the repository at this point in the history
…ethereum into ethersphere-frontier/natspec
  • Loading branch information
obscuren committed Apr 20, 2015
2 parents 76025cc + 093a910 commit 36ec42e
Show file tree
Hide file tree
Showing 18 changed files with 5,095 additions and 72 deletions.
18 changes: 13 additions & 5 deletions cmd/geth/js.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ import (
"strings"

"github.com/ethereum/go-ethereum/cmd/utils"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/common/docserver"
"github.com/ethereum/go-ethereum/common/natspec"
"github.com/ethereum/go-ethereum/eth"
re "github.com/ethereum/go-ethereum/jsre"
"github.com/ethereum/go-ethereum/rpc"
Expand Down Expand Up @@ -139,10 +140,17 @@ var net = web3.net;
js.re.Eval(globalRegistrar + "registrar = new GlobalRegistrar(\"" + globalRegistrarAddr + "\");")
}

func (self *jsre) ConfirmTransaction(tx *types.Transaction) bool {
p := fmt.Sprintf("Confirm Transaction %v\n[y/n] ", tx)
answer, _ := self.Prompt(p)
return strings.HasPrefix(strings.Trim(answer, " "), "y")
var ds, _ = docserver.New(utils.JSpathFlag.String())

func (self *jsre) ConfirmTransaction(tx string) bool {
if self.ethereum.NatSpec {
notice := natspec.GetNotice(self.xeth, tx, ds)
fmt.Println(notice)
answer, _ := self.Prompt("Confirm Transaction\n[y/n] ")
return strings.HasPrefix(strings.Trim(answer, " "), "y")
} else {
return true
}
}

func (self *jsre) UnlockAccount(addr []byte) bool {
Expand Down
1 change: 1 addition & 0 deletions cmd/geth/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ JavaScript API. See https://github.com/ethereum/go-ethereum/wiki/Javascipt-Conso
utils.MinerThreadsFlag,
utils.MiningEnabledFlag,
utils.NATFlag,
utils.NatspecEnabledFlag,
utils.NodeKeyFileFlag,
utils.NodeKeyHexFlag,
utils.RPCEnabledFlag,
Expand Down
5 changes: 5 additions & 0 deletions cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ var (
Name: "identity",
Usage: "node name",
}
NatspecEnabledFlag = cli.BoolFlag{
Name: "natspec",
Usage: "Enable NatSpec confirmation notice",
}

// miner settings
MinerThreadsFlag = cli.IntFlag{
Expand Down Expand Up @@ -268,6 +272,7 @@ func MakeEthConfig(clientID, version string, ctx *cli.Context) *eth.Config {
MaxPeers: ctx.GlobalInt(MaxPeersFlag.Name),
Port: ctx.GlobalString(ListenPortFlag.Name),
NAT: GetNAT(ctx),
NatSpec: ctx.GlobalBool(NatspecEnabledFlag.Name),
NodeKey: GetNodeKey(ctx),
Shh: true,
Dial: true,
Expand Down
17 changes: 17 additions & 0 deletions common/bytes.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,23 @@ func Hex2Bytes(str string) []byte {
return h
}

func Hex2BytesFixed(str string, flen int) []byte {

h, _ := hex.DecodeString(str)
if len(h) == flen {
return h
} else {
if len(h) > flen {
return h[len(h)-flen : len(h)]
} else {
hh := make([]byte, flen)
copy(hh[flen-len(h):flen], h[:])
return hh
}
}

}

func StringToByteFunc(str string, cb func(str string) []byte) (ret []byte) {
if len(str) > 1 && str[0:2] == "0x" && !strings.Contains(str, "\n") {
ret = Hex2Bytes(str[2:])
Expand Down
82 changes: 82 additions & 0 deletions common/docserver/docserver.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package docserver

import (
"fmt"
"io/ioutil"
"net/http"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
)

// http://golang.org/pkg/net/http/#RoundTripper
var (
schemes = map[string]func(*DocServer) http.RoundTripper{
// Simple File server from local disk file:///etc/passwd :)
"file": fileServerOnDocRoot,
}
)

func fileServerOnDocRoot(ds *DocServer) http.RoundTripper {
return http.NewFileTransport(http.Dir(ds.DocRoot))
}

type DocServer struct {
*http.Transport
DocRoot string
}

func New(docRoot string) (self *DocServer, err error) {
self = &DocServer{
Transport: &http.Transport{},
DocRoot: docRoot,
}
err = self.RegisterProtocols(schemes)
return
}

// Clients should be reused instead of created as needed. Clients are safe for concurrent use by multiple goroutines.

// A Client is higher-level than a RoundTripper (such as Transport) and additionally handles HTTP details such as cookies and redirects.

func (self *DocServer) Client() *http.Client {
return &http.Client{
Transport: self,
}
}

func (self *DocServer) RegisterProtocols(schemes map[string]func(*DocServer) http.RoundTripper) (err error) {
for scheme, rtf := range schemes {
self.RegisterProtocol(scheme, rtf(self))
}
return
}

func (self *DocServer) GetAuthContent(uri string, hash common.Hash) (content []byte, err error) {
// retrieve content
resp, err := self.Client().Get(uri)
defer func() {
if resp != nil {
resp.Body.Close()
}
}()
if err != nil {
return
}
content, err = ioutil.ReadAll(resp.Body)
if err != nil {
return
}

// check hash to authenticate content
hashbytes := crypto.Sha3(content)
var chash common.Hash
copy(chash[:], hashbytes)
if chash != hash {
content = nil
err = fmt.Errorf("content hash mismatch")
}

return

}
38 changes: 38 additions & 0 deletions common/docserver/docserver_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package docserver

import (
"io/ioutil"
"os"
"testing"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
)

func TestGetAuthContent(t *testing.T) {
text := "test"
hash := common.Hash{}
copy(hash[:], crypto.Sha3([]byte(text)))
ioutil.WriteFile("/tmp/test.content", []byte(text), os.ModePerm)

ds, err := New("/tmp/")
content, err := ds.GetAuthContent("file:///test.content", hash)
if err != nil {
t.Errorf("no error expected, got %v", err)
}
if string(content) != text {
t.Errorf("incorrect content. expected %v, got %v", text, string(content))
}

hash = common.Hash{}
content, err = ds.GetAuthContent("file:///test.content", hash)
expected := "content hash mismatch"
if err == nil {
t.Errorf("expected error, got nothing")
} else {
if err.Error() != expected {
t.Errorf("expected error '%s' got '%v'", expected, err)
}
}

}
Loading

0 comments on commit 36ec42e

Please sign in to comment.