This commit is contained in:
Caesar Chad 2018-06-19 13:42:03 +00:00 committed by GitHub
commit d5a6e36b13
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -29,7 +29,7 @@ import (
"github.com/robertkrimen/otto" "github.com/robertkrimen/otto"
) )
// bridge is a collection of JavaScript utility methods to bride the .js runtime // bridge is a collection of JavaScript utility methods to bridge the .js runtime
// environment and the Go RPC connection backing the remote method calls. // environment and the Go RPC connection backing the remote method calls.
type bridge struct { type bridge struct {
client *rpc.Client // RPC client to execute Ethereum requests through client *rpc.Client // RPC client to execute Ethereum requests through
@ -37,7 +37,7 @@ type bridge struct {
printer io.Writer // Output writer to serialize any display strings to printer io.Writer // Output writer to serialize any display strings to
} }
// newBridge creates a new JavaScript wrapper around an RPC client. // newBridge creates a new JavaScript wrapper around an RPC client
func newBridge(client *rpc.Client, prompter UserPrompter, printer io.Writer) *bridge { func newBridge(client *rpc.Client, prompter UserPrompter, printer io.Writer) *bridge {
return &bridge{ return &bridge{
client: client, client: client,
@ -93,12 +93,15 @@ func (b *bridge) OpenWallet(call otto.FunctionCall) (response otto.Value) {
} }
wallet := call.Argument(0) wallet := call.Argument(0)
// If password is not given or is the null value, set the password to
// blank which is digestible by otto/JavaScript.
var passwd otto.Value var passwd otto.Value
if call.Argument(1).IsUndefined() || call.Argument(1).IsNull() { if call.Argument(1).IsUndefined() || call.Argument(1).IsNull() {
passwd, _ = otto.ToValue("") passwd, _ = otto.ToValue("")
} else { } else {
passwd = call.Argument(1) passwd = call.Argument(1)
} }
// Open the wallet and return if successful in itself // Open the wallet and return if successful in itself
val, err := call.Otto.Call("jeth.openWallet", nil, wallet, passwd) val, err := call.Otto.Call("jeth.openWallet", nil, wallet, passwd)
if err == nil { if err == nil {
@ -154,7 +157,7 @@ func (b *bridge) UnlockAccount(call otto.FunctionCall) (response otto.Value) {
} }
passwd = call.Argument(1) passwd = call.Argument(1)
} }
// Third argument is the duration how long the account must be unlocked. // Third argument is the duration how long the account must be unlocked
duration := otto.NullValue() duration := otto.NullValue()
if call.Argument(2).IsDefined() && !call.Argument(2).IsNull() { if call.Argument(2).IsDefined() && !call.Argument(2).IsNull() {
if !call.Argument(2).IsNumber() { if !call.Argument(2).IsNumber() {
@ -180,6 +183,7 @@ func (b *bridge) Sign(call otto.FunctionCall) (response otto.Value) {
passwd = call.Argument(2) passwd = call.Argument(2)
) )
// Make sure the first and second arguments are strings
if !message.IsString() { if !message.IsString() {
throwJSException("first argument must be the message to sign") throwJSException("first argument must be the message to sign")
} }
@ -187,7 +191,7 @@ func (b *bridge) Sign(call otto.FunctionCall) (response otto.Value) {
throwJSException("second argument must be the account to sign with") throwJSException("second argument must be the account to sign with")
} }
// if the password is not given or null ask the user and ensure password is a string // If the password is not given or null ask the user and ensure password is a string
if passwd.IsUndefined() || passwd.IsNull() { if passwd.IsUndefined() || passwd.IsNull() {
fmt.Fprintf(b.printer, "Give password for account %s\n", account) fmt.Fprintf(b.printer, "Give password for account %s\n", account)
if input, err := b.prompter.PromptPassword("Passphrase: "); err != nil { if input, err := b.prompter.PromptPassword("Passphrase: "); err != nil {
@ -271,12 +275,12 @@ func (b *bridge) SleepBlocks(call otto.FunctionCall) (response otto.Value) {
} }
type jsonrpcCall struct { type jsonrpcCall struct {
ID int64 Id int64
Method string Method string
Params []interface{} Params []interface{}
} }
// Send implements the web3 provider "send" method. // Send implements the web3 provider "send" method
func (b *bridge) Send(call otto.FunctionCall) (response otto.Value) { func (b *bridge) Send(call otto.FunctionCall) (response otto.Value) {
// Remarshal the request into a Go value. // Remarshal the request into a Go value.
JSON, _ := call.Otto.Object("JSON") JSON, _ := call.Otto.Object("JSON")
@ -304,7 +308,7 @@ func (b *bridge) Send(call otto.FunctionCall) (response otto.Value) {
resps, _ := call.Otto.Object("new Array()") resps, _ := call.Otto.Object("new Array()")
for _, req := range reqs { for _, req := range reqs {
resp, _ := call.Otto.Object(`({"jsonrpc":"2.0"})`) resp, _ := call.Otto.Object(`({"jsonrpc":"2.0"})`)
resp.Set("id", req.ID) resp.Set("id", req.Id)
var result json.RawMessage var result json.RawMessage
err = b.client.Call(&result, req.Method, req.Params...) err = b.client.Call(&result, req.Method, req.Params...)
switch err := err.(type) { switch err := err.(type) {