2015-03-05 20:43:34 -06:00
|
|
|
package natspec
|
|
|
|
|
|
|
|
import (
|
2015-03-31 01:28:12 -05:00
|
|
|
"bytes"
|
2015-03-30 18:49:38 -05:00
|
|
|
"encoding/json"
|
2015-03-08 08:09:13 -05:00
|
|
|
"fmt"
|
2015-03-20 07:22:01 -05:00
|
|
|
"github.com/robertkrimen/otto"
|
2015-03-30 18:49:38 -05:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2015-03-31 10:02:55 -05:00
|
|
|
"github.com/ethereum/go-ethereum/common/docserver"
|
|
|
|
"github.com/ethereum/go-ethereum/common/resolver"
|
2015-03-30 18:49:38 -05:00
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
2015-03-31 01:28:12 -05:00
|
|
|
"github.com/ethereum/go-ethereum/xeth"
|
2015-03-05 20:43:34 -06:00
|
|
|
)
|
|
|
|
|
2015-03-30 18:49:38 -05:00
|
|
|
type abi2method map[[8]byte]*method
|
|
|
|
|
2015-03-05 20:43:34 -06:00
|
|
|
type NatSpec struct {
|
2015-03-30 18:49:38 -05:00
|
|
|
jsvm *otto.Otto
|
|
|
|
userDocJson, abiDocJson []byte
|
|
|
|
userDoc userDoc
|
2015-03-31 01:28:12 -05:00
|
|
|
tx, data string
|
2015-03-30 18:49:38 -05:00
|
|
|
// abiDoc abiDoc
|
2015-03-05 20:43:34 -06:00
|
|
|
}
|
|
|
|
|
2015-04-08 05:35:02 -05:00
|
|
|
func getFallbackNotice(comment, tx string) string {
|
|
|
|
|
|
|
|
return "About to submit transaction (" + comment + "): " + tx
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetNotice(xeth *xeth.XEth, tx string, http *docserver.DocServer) (notice string) {
|
|
|
|
|
|
|
|
ns, err := New(xeth, tx, http)
|
|
|
|
if err != nil {
|
|
|
|
if ns == nil {
|
|
|
|
return getFallbackNotice("no NatSpec info found for contract", tx)
|
|
|
|
} else {
|
|
|
|
return getFallbackNotice("invalid NatSpec info", tx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
notice, err2 := ns.Notice()
|
|
|
|
|
|
|
|
if err2 != nil {
|
2015-04-13 07:35:41 -05:00
|
|
|
return getFallbackNotice("NatSpec notice error \""+err2.Error()+"\"", tx)
|
2015-04-08 05:35:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-03-31 10:02:55 -05:00
|
|
|
func New(xeth *xeth.XEth, tx string, http *docserver.DocServer) (self *NatSpec, err error) {
|
2015-03-31 01:28:12 -05:00
|
|
|
|
|
|
|
// extract contract address from tx
|
|
|
|
|
|
|
|
var obj map[string]json.RawMessage
|
|
|
|
err = json.Unmarshal([]byte(tx), &obj)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var tmp []map[string]string
|
|
|
|
err = json.Unmarshal(obj["params"], &tmp)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
contractAddress := tmp[0]["to"]
|
|
|
|
|
|
|
|
// retrieve contract hash from state
|
|
|
|
if !xeth.IsContract(contractAddress) {
|
|
|
|
err = fmt.Errorf("NatSpec error: contract not found")
|
|
|
|
return
|
|
|
|
}
|
2015-04-07 04:50:17 -05:00
|
|
|
codehex := xeth.CodeAt(contractAddress)
|
2015-04-08 05:35:02 -05:00
|
|
|
codeHash := common.BytesToHash(crypto.Sha3(common.Hex2Bytes(codehex[2:])))
|
2015-03-31 10:02:55 -05:00
|
|
|
// parse out host/domain
|
|
|
|
|
|
|
|
// set up nameresolver with natspecreg + urlhint contract addresses
|
|
|
|
res := resolver.New(
|
|
|
|
xeth,
|
2015-04-03 10:37:59 -05:00
|
|
|
resolver.URLHintContractAddress,
|
2015-04-07 04:50:17 -05:00
|
|
|
resolver.HashRegContractAddress,
|
2015-03-31 10:02:55 -05:00
|
|
|
)
|
|
|
|
|
2015-04-07 04:50:17 -05:00
|
|
|
// resolve host via HashReg/UrlHint Resolver
|
|
|
|
uri, hash, err := res.KeyToUrl(codeHash)
|
2015-03-31 10:02:55 -05:00
|
|
|
if err != nil {
|
|
|
|
return
|
2015-03-31 01:28:12 -05:00
|
|
|
}
|
|
|
|
|
2015-03-31 10:02:55 -05:00
|
|
|
// get content via http client and authenticate content using hash
|
|
|
|
content, err := http.GetAuthContent(uri, hash)
|
|
|
|
if err != nil {
|
|
|
|
return
|
2015-03-31 01:28:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// get abi, userdoc
|
|
|
|
var obj2 map[string]json.RawMessage
|
|
|
|
err = json.Unmarshal(content, &obj2)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
abi := []byte(obj2["abi"])
|
|
|
|
userdoc := []byte(obj2["userdoc"])
|
|
|
|
|
|
|
|
self, err = NewWithDocs(abi, userdoc, tx)
|
|
|
|
return
|
2015-03-30 18:49:38 -05:00
|
|
|
}
|
2015-03-05 20:43:34 -06:00
|
|
|
|
2015-03-31 01:28:12 -05:00
|
|
|
func NewWithDocs(abiDocJson, userDocJson []byte, tx string) (self *NatSpec, err error) {
|
|
|
|
|
|
|
|
var obj map[string]json.RawMessage
|
|
|
|
err = json.Unmarshal([]byte(tx), &obj)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var tmp []map[string]string
|
|
|
|
err = json.Unmarshal(obj["params"], &tmp)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
data := tmp[0]["data"]
|
2015-03-30 18:49:38 -05:00
|
|
|
|
|
|
|
self = &NatSpec{
|
|
|
|
jsvm: otto.New(),
|
|
|
|
abiDocJson: abiDocJson,
|
|
|
|
userDocJson: userDocJson,
|
2015-03-31 01:28:12 -05:00
|
|
|
tx: tx,
|
|
|
|
data: data,
|
2015-03-30 18:49:38 -05:00
|
|
|
}
|
2015-03-05 20:43:34 -06:00
|
|
|
|
2015-04-01 06:29:16 -05:00
|
|
|
// load and require natspec js (but it is meant to be protected environment)
|
2015-03-08 08:09:13 -05:00
|
|
|
_, err = self.jsvm.Run(natspecJS)
|
2015-03-05 20:43:34 -06:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2015-03-06 11:54:26 -06:00
|
|
|
_, err = self.jsvm.Run("var natspec = require('natspec');")
|
2015-03-05 20:43:34 -06:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-03-30 18:49:38 -05:00
|
|
|
err = json.Unmarshal(userDocJson, &self.userDoc)
|
|
|
|
// err = parseAbiJson(abiDocJson, &self.abiDoc)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// type abiDoc []method
|
|
|
|
|
|
|
|
// type method struct {
|
|
|
|
// Name string `json:name`
|
|
|
|
// Inputs []input `json:inputs`
|
|
|
|
// abiKey [8]byte
|
|
|
|
// }
|
|
|
|
|
|
|
|
// type input struct {
|
|
|
|
// Name string `json:name`
|
|
|
|
// Type string `json:type`
|
|
|
|
// }
|
|
|
|
|
2015-04-01 06:29:16 -05:00
|
|
|
// json skeleton for abi doc (contract method definitions)
|
2015-03-30 18:49:38 -05:00
|
|
|
type method struct {
|
|
|
|
Notice string `json:notice`
|
|
|
|
name string
|
|
|
|
}
|
|
|
|
|
|
|
|
type userDoc struct {
|
|
|
|
Methods map[string]*method `json:methods`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *NatSpec) makeAbi2method(abiKey [8]byte) (meth *method) {
|
|
|
|
for signature, m := range self.userDoc.Methods {
|
|
|
|
name := strings.Split(signature, "(")[0]
|
|
|
|
hash := []byte(common.Bytes2Hex(crypto.Sha3([]byte(signature))))
|
|
|
|
var key [8]byte
|
|
|
|
copy(key[:], hash[:8])
|
2015-03-31 01:28:12 -05:00
|
|
|
if bytes.Equal(key[:], abiKey[:]) {
|
2015-03-30 18:49:38 -05:00
|
|
|
meth = m
|
2015-03-31 01:28:12 -05:00
|
|
|
meth.name = name
|
|
|
|
return
|
2015-03-30 18:49:38 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-03-31 01:28:12 -05:00
|
|
|
func (self *NatSpec) Notice() (notice string, err error) {
|
2015-03-30 18:49:38 -05:00
|
|
|
var abiKey [8]byte
|
2015-03-31 01:28:12 -05:00
|
|
|
if len(self.data) < 10 {
|
|
|
|
err = fmt.Errorf("Invalid transaction data")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
copy(abiKey[:], self.data[2:10])
|
2015-03-30 18:49:38 -05:00
|
|
|
meth := self.makeAbi2method(abiKey)
|
2015-04-07 04:50:17 -05:00
|
|
|
|
2015-03-30 18:49:38 -05:00
|
|
|
if meth == nil {
|
|
|
|
err = fmt.Errorf("abi key %x does not match any method %v")
|
|
|
|
return
|
|
|
|
}
|
2015-03-31 01:28:12 -05:00
|
|
|
notice, err = self.noticeForMethod(self.tx, meth.name, meth.Notice)
|
2015-03-05 20:43:34 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-03-30 18:49:38 -05:00
|
|
|
func (self *NatSpec) noticeForMethod(tx string, name, expression string) (notice string, err error) {
|
2015-04-07 04:50:17 -05:00
|
|
|
|
2015-03-30 18:49:38 -05:00
|
|
|
if _, err = self.jsvm.Run("var transaction = " + tx + ";"); err != nil {
|
2015-03-08 08:09:13 -05:00
|
|
|
return "", fmt.Errorf("natspec.js error setting transaction: %v", err)
|
|
|
|
}
|
2015-03-05 20:43:34 -06:00
|
|
|
|
2015-03-30 18:49:38 -05:00
|
|
|
if _, err = self.jsvm.Run("var abi = " + string(self.abiDocJson) + ";"); err != nil {
|
2015-03-08 08:09:13 -05:00
|
|
|
return "", fmt.Errorf("natspec.js error setting abi: %v", err)
|
|
|
|
}
|
2015-03-05 20:43:34 -06:00
|
|
|
|
2015-03-30 18:49:38 -05:00
|
|
|
if _, err = self.jsvm.Run("var method = '" + name + "';"); err != nil {
|
2015-03-08 08:09:13 -05:00
|
|
|
return "", fmt.Errorf("natspec.js error setting method: %v", err)
|
|
|
|
}
|
2015-03-05 20:43:34 -06:00
|
|
|
|
2015-03-08 08:09:13 -05:00
|
|
|
if _, err = self.jsvm.Run("var expression = \"" + expression + "\";"); err != nil {
|
|
|
|
return "", fmt.Errorf("natspec.js error setting expression: %v", err)
|
|
|
|
}
|
2015-03-05 20:43:34 -06:00
|
|
|
|
2015-03-06 11:54:26 -06:00
|
|
|
self.jsvm.Run("var call = {method: method,abi: abi,transaction: transaction};")
|
|
|
|
value, err := self.jsvm.Run("natspec.evaluateExpression(expression, call);")
|
2015-03-05 20:43:34 -06:00
|
|
|
if err != nil {
|
2015-03-08 08:09:13 -05:00
|
|
|
return "", fmt.Errorf("natspec.js error evaluating expression: %v", err)
|
|
|
|
}
|
|
|
|
evalError := "Natspec evaluation failed, wrong input params"
|
|
|
|
if value.String() == evalError {
|
|
|
|
return "", fmt.Errorf("natspec.js error evaluating expression: wrong input params in expression '%s'", expression)
|
2015-03-05 20:43:34 -06:00
|
|
|
}
|
2015-03-08 08:09:13 -05:00
|
|
|
if len(value.String()) == 0 {
|
|
|
|
return "", fmt.Errorf("natspec.js error evaluating expression")
|
|
|
|
}
|
|
|
|
|
|
|
|
return value.String(), nil
|
2015-03-05 20:43:34 -06:00
|
|
|
|
|
|
|
}
|