2015-03-06 02:43:34 +00:00
|
|
|
package natspec
|
|
|
|
|
|
|
|
import (
|
2015-03-06 17:54:26 +00:00
|
|
|
"github.com/obscuren/otto"
|
2015-03-06 02:43:34 +00:00
|
|
|
"io/ioutil"
|
|
|
|
)
|
|
|
|
|
|
|
|
type NatSpec struct {
|
2015-03-06 17:54:26 +00:00
|
|
|
jsvm *otto.Otto
|
2015-03-06 02:43:34 +00:00
|
|
|
}
|
|
|
|
|
2015-03-06 17:54:26 +00:00
|
|
|
func NewNATSpec(transaction string) (self *NatSpec, err error) {
|
2015-03-06 02:43:34 +00:00
|
|
|
|
|
|
|
self = new(NatSpec)
|
2015-03-06 17:54:26 +00:00
|
|
|
self.jsvm = otto.New()
|
2015-03-06 02:43:34 +00:00
|
|
|
code, err := ioutil.ReadFile("natspec.js")
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-03-06 17:54:26 +00:00
|
|
|
_, err = self.jsvm.Run(string(code))
|
2015-03-06 02:43:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2015-03-06 17:54:26 +00:00
|
|
|
_, err = self.jsvm.Run("var natspec = require('natspec');")
|
2015-03-06 02:43:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-03-06 17:54:26 +00:00
|
|
|
self.jsvm.Run("var transaction = " + transaction + ";")
|
2015-03-06 02:43:34 +00:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *NatSpec) SetDescription(desc string) (err error) {
|
|
|
|
|
2015-03-06 17:54:26 +00:00
|
|
|
_, err = self.jsvm.Run("var expression = \"" + desc + "\";")
|
2015-03-06 02:43:34 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *NatSpec) SetABI(abi string) (err error) {
|
|
|
|
|
2015-03-06 17:54:26 +00:00
|
|
|
_, err = self.jsvm.Run("var abi = " + abi + ";")
|
2015-03-06 02:43:34 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *NatSpec) SetMethod(method string) (err error) {
|
|
|
|
|
2015-03-06 17:54:26 +00:00
|
|
|
_, err = self.jsvm.Run("var method = '" + method + "';")
|
2015-03-06 02:43:34 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *NatSpec) Parse() string {
|
|
|
|
|
2015-03-06 17:54:26 +00:00
|
|
|
self.jsvm.Run("var call = {method: method,abi: abi,transaction: transaction};")
|
|
|
|
value, err := self.jsvm.Run("natspec.evaluateExpression(expression, call);")
|
2015-03-06 02:43:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return err.Error()
|
|
|
|
}
|
|
|
|
return value.String()
|
|
|
|
|
|
|
|
}
|