Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
P
plugin
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
JIRA
JIRA
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
link33
plugin
Commits
6d72a861
Commit
6d72a861
authored
Jan 04, 2019
by
heyubin
Committed by
vipwzw
Jan 05, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add by hyb for issues248
parent
4dfc0689
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
216 additions
and
2 deletions
+216
-2
js.go
plugin/dapp/js/cmd/js.go
+160
-0
doc.go
plugin/dapp/js/doc.go
+53
-0
plugin.go
plugin/dapp/js/plugin.go
+3
-2
No files found.
plugin/dapp/js/cmd/js.go
0 → 100644
View file @
6d72a861
// Copyright Fuzamei Corp. 2018 All Rights Reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package
cmd
import
(
"fmt"
"io/ioutil"
"os"
"github.com/33cn/chain33/rpc/jsonclient"
rpctypes
"github.com/33cn/chain33/rpc/types"
"github.com/33cn/chain33/types"
jsty
"github.com/33cn/plugin/plugin/dapp/js/types"
"github.com/33cn/plugin/plugin/dapp/js/types/jsproto"
//"github.com/gojson"
"github.com/spf13/cobra"
)
//JavaScriptCmd :
func
JavaScriptCmd
()
*
cobra
.
Command
{
cmd
:=
&
cobra
.
Command
{
Use
:
"jsvm"
,
Short
:
"Java Script VM contract"
,
Args
:
cobra
.
MinimumNArgs
(
1
),
}
cmd
.
AddCommand
(
JavaScriptCreateCmd
(),
JavaScriptCallCmd
(),
JavaScriptQueryCmd
(),
)
return
cmd
}
// JavaScriptCreateCmd :
func
JavaScriptCreateCmd
()
*
cobra
.
Command
{
cmd
:=
&
cobra
.
Command
{
Use
:
"create"
,
Short
:
"create java script contract"
,
Run
:
createJavaScriptContract
,
}
createJavaScriptContractFlags
(
cmd
)
return
cmd
}
func
createJavaScriptContractFlags
(
cmd
*
cobra
.
Command
)
{
cmd
.
Flags
()
.
StringP
(
"code"
,
"c"
,
""
,
"path of js file,it must always be in utf-8."
)
cmd
.
MarkFlagRequired
(
"code"
)
cmd
.
Flags
()
.
StringP
(
"name"
,
"n"
,
""
,
"contract name"
)
cmd
.
MarkFlagRequired
(
"name"
)
}
func
createJavaScriptContract
(
cmd
*
cobra
.
Command
,
args
[]
string
)
{
rpcLaddr
,
_
:=
cmd
.
Flags
()
.
GetString
(
"rpc_laddr"
)
patch
,
_
:=
cmd
.
Flags
()
.
GetString
(
"code"
)
name
,
_
:=
cmd
.
Flags
()
.
GetString
(
"name"
)
codestr
,
err
:=
ioutil
.
ReadFile
(
patch
)
if
err
!=
nil
{
fmt
.
Fprintln
(
os
.
Stderr
,
err
)
return
}
create
:=
&
jsproto
.
Create
{
Code
:
string
(
codestr
),
Name
:
name
,
}
params
:=
&
rpctypes
.
CreateTxIn
{
Execer
:
jsty
.
JsX
,
ActionName
:
"Create"
,
Payload
:
types
.
MustPBToJSON
(
create
),
}
var
res
string
ctx
:=
jsonclient
.
NewRPCCtx
(
rpcLaddr
,
"Chain33.CreateTransaction"
,
params
,
&
res
)
ctx
.
RunWithoutMarshal
()
}
// JavaScriptCallCmd :
func
JavaScriptCallCmd
()
*
cobra
.
Command
{
cmd
:=
&
cobra
.
Command
{
Use
:
"call"
,
Short
:
"call java script contract"
,
Run
:
callJavaScript
,
}
callJavaScriptFlags
(
cmd
)
return
cmd
}
func
callJavaScriptFlags
(
cmd
*
cobra
.
Command
)
{
cmd
.
Flags
()
.
StringP
(
"name"
,
"n"
,
""
,
"java script contract name"
)
cmd
.
MarkFlagRequired
(
"name"
)
cmd
.
Flags
()
.
StringP
(
"funcname"
,
"f"
,
""
,
"java script contract funcname"
)
cmd
.
MarkFlagRequired
(
"funcname"
)
cmd
.
Flags
()
.
StringP
(
"args"
,
"a"
,
""
,
"json str of args"
)
}
func
callJavaScript
(
cmd
*
cobra
.
Command
,
args
[]
string
)
{
rpcLaddr
,
_
:=
cmd
.
Flags
()
.
GetString
(
"rpc_laddr"
)
name
,
_
:=
cmd
.
Flags
()
.
GetString
(
"name"
)
funcname
,
_
:=
cmd
.
Flags
()
.
GetString
(
"funcname"
)
input
,
_
:=
cmd
.
Flags
()
.
GetString
(
"args"
)
call
:=
&
jsproto
.
Call
{
Name
:
name
,
Funcname
:
funcname
,
Args
:
input
,
}
params
:=
&
rpctypes
.
CreateTxIn
{
Execer
:
"user."
+
jsty
.
JsX
+
"."
+
name
,
ActionName
:
"Call"
,
Payload
:
types
.
MustPBToJSON
(
call
),
}
var
res
string
ctx
:=
jsonclient
.
NewRPCCtx
(
rpcLaddr
,
"Chain33.CreateTransaction"
,
params
,
&
res
)
ctx
.
RunWithoutMarshal
()
}
//JavaScriptQueryCmd :
func
JavaScriptQueryCmd
()
*
cobra
.
Command
{
cmd
:=
&
cobra
.
Command
{
Use
:
"query"
,
Short
:
"query java script contract"
,
Run
:
queryJavaScript
,
}
queryJavaScriptFlags
(
cmd
)
return
cmd
}
func
queryJavaScriptFlags
(
cmd
*
cobra
.
Command
)
{
cmd
.
Flags
()
.
StringP
(
"name"
,
"n"
,
""
,
"java script contract name"
)
cmd
.
MarkFlagRequired
(
"name"
)
cmd
.
Flags
()
.
StringP
(
"funcname"
,
"f"
,
""
,
"java script contract funcname"
)
cmd
.
MarkFlagRequired
(
"funcname"
)
cmd
.
Flags
()
.
StringP
(
"args"
,
"a"
,
""
,
"json str of args"
)
}
func
queryJavaScript
(
cmd
*
cobra
.
Command
,
args
[]
string
)
{
rpcLaddr
,
_
:=
cmd
.
Flags
()
.
GetString
(
"rpc_laddr"
)
name
,
_
:=
cmd
.
Flags
()
.
GetString
(
"name"
)
funcname
,
_
:=
cmd
.
Flags
()
.
GetString
(
"funcname"
)
input
,
_
:=
cmd
.
Flags
()
.
GetString
(
"args"
)
var
params
rpctypes
.
Query4Jrpc
var
rep
interface
{}
req
:=
&
jsproto
.
Call
{
Name
:
name
,
Funcname
:
funcname
,
Args
:
input
,
}
params
.
Execer
=
jsty
.
JsX
params
.
FuncName
=
"Query"
params
.
Payload
=
types
.
MustPBToJSON
(
req
)
rep
=
&
jsproto
.
QueryResult
{}
ctx
:=
jsonclient
.
NewRPCCtx
(
rpcLaddr
,
"Chain33.Query"
,
params
,
rep
)
ctx
.
Run
()
}
plugin/dapp/js/doc.go
0 → 100644
View file @
6d72a861
// Copyright Fuzamei Corp. 2018 All Rights Reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package
js
/*
Java Script VM contract
cli 命令行
Available Commands:
call call java script contract
create create java script contract
query query java script contract
cli jsvm create
Flags:
-c, --code string path of js file,it must always be in utf-8.
-h, --help help for create
-n, --name string contract name
cli jsvm call
Flags:
-a, --args string json str of args
-f, --funcname string java script contract funcname
-h, --help help for call
-n, --name string java script contract name
cli jsvm query
Flags:
-a, --args string json str of args
-f, --funcname string java script contract funcname
-h, --help help for query
-n, --name string java script contract name
测试步骤:
第一步:创建钱包
cli seed save -p heyubin -s "voice leisure mechanic tape cluster grunt receive joke nurse between monkey lunch save useful cruise"
cli wallet unlock -p heyubin
cli account import_key -l miner -k CC38546E9E659D15E6B4893F0AB32A06D103931A8230B0BDE71459D2B27D6944
第二步:创建名为test的js合约,合约代码使用test.js代码文件(必须是utf-8格式)
cli send jsvm create -c "../plugin/dapp/js/executor/test.js" -n test -k 14KEKbYtKKQm4wMthSK9J4La4nAiidGozt
第三步:调用test合约的hello函数
cli send jsvm call -a "{\"hello\": \"world\"}" -f hello -n test -k 14KEKbYtKKQm4wMthSK9J4La4nAiidGozt
第四步:query test合约hello函数
cli jsvm query -a "{\"hello\": \"world\"}" -f hello -n test
*/
plugin/dapp/js/plugin.go
View file @
6d72a861
package
unfreeze
package
js
import
(
"github.com/33cn/chain33/pluginmgr"
"github.com/33cn/plugin/plugin/dapp/js/cmd"
"github.com/33cn/plugin/plugin/dapp/js/executor"
ptypes
"github.com/33cn/plugin/plugin/dapp/js/types"
)
...
...
@@ -11,7 +12,7 @@ func init() {
Name
:
ptypes
.
JsX
,
ExecName
:
executor
.
GetName
(),
Exec
:
executor
.
Init
,
Cmd
:
nil
,
Cmd
:
cmd
.
JavaScriptCmd
,
RPC
:
nil
,
})
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment