2015-03-05 20:43:34 -06:00
|
|
|
package natspec
|
|
|
|
|
|
|
|
import (
|
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"
|
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
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
|
|
|
|
methods abi2method
|
|
|
|
userDocJson, abiDocJson []byte
|
|
|
|
userDoc userDoc
|
|
|
|
// abiDoc abiDoc
|
2015-03-05 20:43:34 -06:00
|
|
|
}
|
|
|
|
|
2015-03-08 08:09:13 -05:00
|
|
|
// TODO: should initialise with abi and userdoc jsons
|
2015-03-08 08:17:20 -05:00
|
|
|
func New() (self *NatSpec, err error) {
|
2015-03-30 18:49:38 -05:00
|
|
|
// fetch abi, userdoc
|
|
|
|
abi := []byte("")
|
|
|
|
userdoc := []byte("")
|
|
|
|
return NewWithDocs(abi, userdoc)
|
|
|
|
}
|
2015-03-05 20:43:34 -06:00
|
|
|
|
2015-03-30 18:49:38 -05:00
|
|
|
func NewWithDocs(abiDocJson, userDocJson []byte) (self *NatSpec, err error) {
|
|
|
|
|
|
|
|
self = &NatSpec{
|
|
|
|
jsvm: otto.New(),
|
|
|
|
abiDocJson: abiDocJson,
|
|
|
|
userDocJson: userDocJson,
|
|
|
|
}
|
2015-03-05 20:43:34 -06:00
|
|
|
|
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`
|
|
|
|
// }
|
|
|
|
|
|
|
|
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) {
|
|
|
|
if self.methods != nil {
|
|
|
|
meth = self.methods[abiKey]
|
|
|
|
return
|
|
|
|
}
|
|
|
|
self.methods = make(abi2method)
|
|
|
|
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])
|
|
|
|
self.methods[key] = meth
|
|
|
|
meth.name = name
|
|
|
|
if key == abiKey {
|
|
|
|
meth = m
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *NatSpec) Notice(tx string, abi string) (notice string, err error) {
|
|
|
|
var abiKey [8]byte
|
|
|
|
copy(abiKey[:], []byte(abi)[:8])
|
|
|
|
meth := self.makeAbi2method(abiKey)
|
|
|
|
if meth == nil {
|
|
|
|
err = fmt.Errorf("abi key %x does not match any method %v")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
notice, err = self.noticeForMethod(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) {
|
|
|
|
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
|
|
|
|
|
|
|
}
|