aboutsummaryrefslogtreecommitdiffstats
path: root/signer/rules
diff options
context:
space:
mode:
authorPéter Szilágyi <peterke@gmail.com>2019-07-02 19:01:47 +0800
committerGitHub <noreply@github.com>2019-07-02 19:01:47 +0800
commita0943b8932f2fcd28dc103689f904a3c75ea07a4 (patch)
treec94898e32a2a755d962ab78eb2cddbc09f0fc02b /signer/rules
parent6bf5555c4f79b8161b4cbedc19da9d29ca6e2305 (diff)
downloadgo-tangerine-a0943b8932f2fcd28dc103689f904a3c75ea07a4.tar
go-tangerine-a0943b8932f2fcd28dc103689f904a3c75ea07a4.tar.gz
go-tangerine-a0943b8932f2fcd28dc103689f904a3c75ea07a4.tar.bz2
go-tangerine-a0943b8932f2fcd28dc103689f904a3c75ea07a4.tar.lz
go-tangerine-a0943b8932f2fcd28dc103689f904a3c75ea07a4.tar.xz
go-tangerine-a0943b8932f2fcd28dc103689f904a3c75ea07a4.tar.zst
go-tangerine-a0943b8932f2fcd28dc103689f904a3c75ea07a4.zip
cmd/clef, signer: refresh tutorial, fix noticed issues (#19774)
* cmd/clef, signer: refresh tutorial, fix noticed issues * cmd/clef, signer: support removing stored keys (delpw + rules) * cmd/clef: polishes + Geth integration in the tutorial
Diffstat (limited to 'signer/rules')
-rw-r--r--signer/rules/rules.go18
-rw-r--r--signer/rules/rules_test.go51
2 files changed, 41 insertions, 28 deletions
diff --git a/signer/rules/rules.go b/signer/rules/rules.go
index 5ebffa3af..f731dac40 100644
--- a/signer/rules/rules.go
+++ b/signer/rules/rules.go
@@ -74,12 +74,28 @@ func (r *rulesetUI) execute(jsfunc string, jsarg interface{}) (otto.Value, error
// Instantiate a fresh vm engine every time
vm := otto.New()
+
// Set the native callbacks
consoleObj, _ := vm.Get("console")
consoleObj.Object().Set("log", consoleOutput)
consoleObj.Object().Set("error", consoleOutput)
- vm.Set("storage", r.storage)
+ vm.Set("storage", struct{}{})
+ storageObj, _ := vm.Get("storage")
+ storageObj.Object().Set("put", func(call otto.FunctionCall) otto.Value {
+ key, val := call.Argument(0).String(), call.Argument(1).String()
+ if val == "" {
+ r.storage.Del(key)
+ } else {
+ r.storage.Put(key, val)
+ }
+ return otto.NullValue()
+ })
+ storageObj.Object().Set("get", func(call otto.FunctionCall) otto.Value {
+ goval, _ := r.storage.Get(call.Argument(0).String())
+ jsval, _ := otto.ToValue(goval)
+ return jsval
+ })
// Load bootstrap libraries
script, err := vm.Compile("bignumber.js", BigNumber_JS)
if err != nil {
diff --git a/signer/rules/rules_test.go b/signer/rules/rules_test.go
index 19d9049c7..e1a23236c 100644
--- a/signer/rules/rules_test.go
+++ b/signer/rules/rules_test.go
@@ -301,23 +301,23 @@ func TestStorage(t *testing.T) {
js := `
function testStorage(){
- storage.Put("mykey", "myvalue")
- a = storage.Get("mykey")
+ storage.put("mykey", "myvalue")
+ a = storage.get("mykey")
- storage.Put("mykey", ["a", "list"]) // Should result in "a,list"
- a += storage.Get("mykey")
+ storage.put("mykey", ["a", "list"]) // Should result in "a,list"
+ a += storage.get("mykey")
- storage.Put("mykey", {"an": "object"}) // Should result in "[object Object]"
- a += storage.Get("mykey")
+ storage.put("mykey", {"an": "object"}) // Should result in "[object Object]"
+ a += storage.get("mykey")
- storage.Put("mykey", JSON.stringify({"an": "object"})) // Should result in '{"an":"object"}'
- a += storage.Get("mykey")
+ storage.put("mykey", JSON.stringify({"an": "object"})) // Should result in '{"an":"object"}'
+ a += storage.get("mykey")
- a += storage.Get("missingkey") //Missing keys should result in empty string
- storage.Put("","missing key==noop") // Can't store with 0-length key
- a += storage.Get("") // Should result in ''
+ a += storage.get("missingkey") //Missing keys should result in empty string
+ storage.put("","missing key==noop") // Can't store with 0-length key
+ a += storage.get("") // Should result in ''
var b = new BigNumber(2)
var c = new BigNumber(16)//"0xf0",16)
@@ -337,7 +337,6 @@ func TestStorage(t *testing.T) {
if err != nil {
t.Errorf("Unexpected error %v", err)
}
-
retval, err := v.ToString()
if err != nil {
@@ -369,7 +368,7 @@ const ExampleTxWindow = `
var windowstart = new Date().getTime() - window;
var txs = [];
- var stored = storage.Get('txs');
+ var stored = storage.get('txs');
if(stored != ""){
txs = JSON.parse(stored)
@@ -414,19 +413,18 @@ const ExampleTxWindow = `
var value = big(resp.tx.value)
var txs = []
// Load stored transactions
- var stored = storage.Get('txs');
+ var stored = storage.get('txs');
if(stored != ""){
txs = JSON.parse(stored)
}
// Add this to the storage
txs.push({tstamp: new Date().getTime(), value: value});
- storage.Put("txs", JSON.stringify(txs));
+ storage.put("txs", JSON.stringify(txs));
}
`
func dummyTx(value hexutil.Big) *core.SignTxRequest {
-
to, _ := mixAddr("000000000000000000000000000000000000dead")
from, _ := mixAddr("000000000000000000000000000000000000dead")
n := hexutil.Uint64(3)
@@ -448,28 +446,27 @@ func dummyTx(value hexutil.Big) *core.SignTxRequest {
Meta: core.Metadata{Remote: "remoteip", Local: "localip", Scheme: "inproc"},
}
}
-func dummyTxWithV(value uint64) *core.SignTxRequest {
+func dummyTxWithV(value uint64) *core.SignTxRequest {
v := big.NewInt(0).SetUint64(value)
h := hexutil.Big(*v)
return dummyTx(h)
}
+
func dummySigned(value *big.Int) *types.Transaction {
to := common.HexToAddress("000000000000000000000000000000000000dead")
gas := uint64(21000)
gasPrice := big.NewInt(2000000)
data := make([]byte, 0)
return types.NewTransaction(3, to, value, gas, gasPrice, data)
-
}
-func TestLimitWindow(t *testing.T) {
+func TestLimitWindow(t *testing.T) {
r, err := initRuleEngine(ExampleTxWindow)
if err != nil {
t.Errorf("Couldn't create evaluator %v", err)
return
}
-
// 0.3 ether: 429D069189E0000 wei
v := big.NewInt(0).SetBytes(common.Hex2Bytes("0429D069189E0000"))
h := hexutil.Big(*v)
@@ -496,7 +493,6 @@ func TestLimitWindow(t *testing.T) {
if resp.Approved {
t.Errorf("Expected check to resolve to 'Reject'")
}
-
}
// dontCallMe is used as a next-handler that does not want to be called - it invokes test failure
@@ -508,6 +504,7 @@ func (d *dontCallMe) OnInputRequired(info core.UserInputRequest) (core.UserInput
d.t.Fatalf("Did not expect next-handler to be called")
return core.UserInputResponse{}, nil
}
+
func (d *dontCallMe) RegisterUIServer(api *core.UIServerAPI) {
}
@@ -589,7 +586,7 @@ func TestSignData(t *testing.T) {
function ApproveSignData(r){
if( r.address.toLowerCase() == "0x694267f14675d7e1b9494fd8d72fefe1755710fa")
{
- if(r.message[0].value.indexOf("bazonk") >= 0){
+ if(r.messages[0].value.indexOf("bazonk") >= 0){
return "Approve"
}
return "Reject"
@@ -615,11 +612,11 @@ function ApproveSignData(r){
},
}
resp, err := r.ApproveSignData(&core.SignDataRequest{
- Address: *addr,
- Message: nvt,
- Hash: hash,
- Meta: core.Metadata{Remote: "remoteip", Local: "localip", Scheme: "inproc"},
- Rawdata: []byte(rawdata),
+ Address: *addr,
+ Messages: nvt,
+ Hash: hash,
+ Meta: core.Metadata{Remote: "remoteip", Local: "localip", Scheme: "inproc"},
+ Rawdata: []byte(rawdata),
})
if err != nil {
t.Fatalf("Unexpected error %v", err)