|
|
|
@ -18,6 +18,7 @@ package console |
|
|
|
|
|
|
|
|
|
import ( |
|
|
|
|
"encoding/json" |
|
|
|
|
"errors" |
|
|
|
|
"fmt" |
|
|
|
|
"io" |
|
|
|
|
"reflect" |
|
|
|
@ -77,18 +78,18 @@ func (b *bridge) NewAccount(call jsre.Call) (goja.Value, error) { |
|
|
|
|
return nil, err |
|
|
|
|
} |
|
|
|
|
if password != confirm { |
|
|
|
|
return nil, fmt.Errorf("passwords don't match!") |
|
|
|
|
return nil, errors.New("passwords don't match!") |
|
|
|
|
} |
|
|
|
|
// A single string password was specified, use that
|
|
|
|
|
case len(call.Arguments) == 1 && call.Argument(0).ToString() != nil: |
|
|
|
|
password = call.Argument(0).ToString().String() |
|
|
|
|
default: |
|
|
|
|
return nil, fmt.Errorf("expected 0 or 1 string argument") |
|
|
|
|
return nil, errors.New("expected 0 or 1 string argument") |
|
|
|
|
} |
|
|
|
|
// Password acquired, execute the call and return
|
|
|
|
|
newAccount, callable := goja.AssertFunction(getJeth(call.VM).Get("newAccount")) |
|
|
|
|
if !callable { |
|
|
|
|
return nil, fmt.Errorf("jeth.newAccount is not callable") |
|
|
|
|
return nil, errors.New("jeth.newAccount is not callable") |
|
|
|
|
} |
|
|
|
|
ret, err := newAccount(goja.Null(), call.VM.ToValue(password)) |
|
|
|
|
if err != nil { |
|
|
|
@ -102,7 +103,7 @@ func (b *bridge) NewAccount(call jsre.Call) (goja.Value, error) { |
|
|
|
|
func (b *bridge) OpenWallet(call jsre.Call) (goja.Value, error) { |
|
|
|
|
// Make sure we have a wallet specified to open
|
|
|
|
|
if call.Argument(0).ToObject(call.VM).ClassName() != "String" { |
|
|
|
|
return nil, fmt.Errorf("first argument must be the wallet URL to open") |
|
|
|
|
return nil, errors.New("first argument must be the wallet URL to open") |
|
|
|
|
} |
|
|
|
|
wallet := call.Argument(0) |
|
|
|
|
|
|
|
|
@ -115,7 +116,7 @@ func (b *bridge) OpenWallet(call jsre.Call) (goja.Value, error) { |
|
|
|
|
// Open the wallet and return if successful in itself
|
|
|
|
|
openWallet, callable := goja.AssertFunction(getJeth(call.VM).Get("openWallet")) |
|
|
|
|
if !callable { |
|
|
|
|
return nil, fmt.Errorf("jeth.openWallet is not callable") |
|
|
|
|
return nil, errors.New("jeth.openWallet is not callable") |
|
|
|
|
} |
|
|
|
|
val, err := openWallet(goja.Null(), wallet, passwd) |
|
|
|
|
if err == nil { |
|
|
|
@ -198,7 +199,7 @@ func (b *bridge) readPassphraseAndReopenWallet(call jsre.Call) (goja.Value, erro |
|
|
|
|
} |
|
|
|
|
openWallet, callable := goja.AssertFunction(getJeth(call.VM).Get("openWallet")) |
|
|
|
|
if !callable { |
|
|
|
|
return nil, fmt.Errorf("jeth.openWallet is not callable") |
|
|
|
|
return nil, errors.New("jeth.openWallet is not callable") |
|
|
|
|
} |
|
|
|
|
return openWallet(goja.Null(), wallet, call.VM.ToValue(input)) |
|
|
|
|
} |
|
|
|
@ -219,7 +220,7 @@ func (b *bridge) readPinAndReopenWallet(call jsre.Call) (goja.Value, error) { |
|
|
|
|
} |
|
|
|
|
openWallet, callable := goja.AssertFunction(getJeth(call.VM).Get("openWallet")) |
|
|
|
|
if !callable { |
|
|
|
|
return nil, fmt.Errorf("jeth.openWallet is not callable") |
|
|
|
|
return nil, errors.New("jeth.openWallet is not callable") |
|
|
|
|
} |
|
|
|
|
return openWallet(goja.Null(), wallet, call.VM.ToValue(input)) |
|
|
|
|
} |
|
|
|
@ -230,13 +231,13 @@ func (b *bridge) readPinAndReopenWallet(call jsre.Call) (goja.Value, error) { |
|
|
|
|
// the RPC call.
|
|
|
|
|
func (b *bridge) UnlockAccount(call jsre.Call) (goja.Value, error) { |
|
|
|
|
if len(call.Arguments) < 1 { |
|
|
|
|
return nil, fmt.Errorf("usage: unlockAccount(account, [ password, duration ])") |
|
|
|
|
return nil, errors.New("usage: unlockAccount(account, [ password, duration ])") |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
account := call.Argument(0) |
|
|
|
|
// Make sure we have an account specified to unlock.
|
|
|
|
|
if goja.IsUndefined(account) || goja.IsNull(account) || account.ExportType().Kind() != reflect.String { |
|
|
|
|
return nil, fmt.Errorf("first argument must be the account to unlock") |
|
|
|
|
return nil, errors.New("first argument must be the account to unlock") |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// If password is not given or is the null value, prompt the user for it.
|
|
|
|
@ -250,7 +251,7 @@ func (b *bridge) UnlockAccount(call jsre.Call) (goja.Value, error) { |
|
|
|
|
passwd = call.VM.ToValue(input) |
|
|
|
|
} else { |
|
|
|
|
if call.Argument(1).ExportType().Kind() != reflect.String { |
|
|
|
|
return nil, fmt.Errorf("password must be a string") |
|
|
|
|
return nil, errors.New("password must be a string") |
|
|
|
|
} |
|
|
|
|
passwd = call.Argument(1) |
|
|
|
|
} |
|
|
|
@ -259,7 +260,7 @@ func (b *bridge) UnlockAccount(call jsre.Call) (goja.Value, error) { |
|
|
|
|
duration := goja.Null() |
|
|
|
|
if !goja.IsUndefined(call.Argument(2)) && !goja.IsNull(call.Argument(2)) { |
|
|
|
|
if !isNumber(call.Argument(2)) { |
|
|
|
|
return nil, fmt.Errorf("unlock duration must be a number") |
|
|
|
|
return nil, errors.New("unlock duration must be a number") |
|
|
|
|
} |
|
|
|
|
duration = call.Argument(2) |
|
|
|
|
} |
|
|
|
@ -267,7 +268,7 @@ func (b *bridge) UnlockAccount(call jsre.Call) (goja.Value, error) { |
|
|
|
|
// Send the request to the backend and return.
|
|
|
|
|
unlockAccount, callable := goja.AssertFunction(getJeth(call.VM).Get("unlockAccount")) |
|
|
|
|
if !callable { |
|
|
|
|
return nil, fmt.Errorf("jeth.unlockAccount is not callable") |
|
|
|
|
return nil, errors.New("jeth.unlockAccount is not callable") |
|
|
|
|
} |
|
|
|
|
return unlockAccount(goja.Null(), account, passwd, duration) |
|
|
|
|
} |
|
|
|
@ -277,7 +278,7 @@ func (b *bridge) UnlockAccount(call jsre.Call) (goja.Value, error) { |
|
|
|
|
// jeth.sign) with it to actually execute the RPC call.
|
|
|
|
|
func (b *bridge) Sign(call jsre.Call) (goja.Value, error) { |
|
|
|
|
if nArgs := len(call.Arguments); nArgs < 2 { |
|
|
|
|
return nil, fmt.Errorf("usage: sign(message, account, [ password ])") |
|
|
|
|
return nil, errors.New("usage: sign(message, account, [ password ])") |
|
|
|
|
} |
|
|
|
|
var ( |
|
|
|
|
message = call.Argument(0) |
|
|
|
@ -286,10 +287,10 @@ func (b *bridge) Sign(call jsre.Call) (goja.Value, error) { |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
if goja.IsUndefined(message) || message.ExportType().Kind() != reflect.String { |
|
|
|
|
return nil, fmt.Errorf("first argument must be the message to sign") |
|
|
|
|
return nil, errors.New("first argument must be the message to sign") |
|
|
|
|
} |
|
|
|
|
if goja.IsUndefined(account) || account.ExportType().Kind() != reflect.String { |
|
|
|
|
return nil, fmt.Errorf("second argument must be the account to sign with") |
|
|
|
|
return nil, errors.New("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
|
|
|
|
@ -301,13 +302,13 @@ func (b *bridge) Sign(call jsre.Call) (goja.Value, error) { |
|
|
|
|
} |
|
|
|
|
passwd = call.VM.ToValue(input) |
|
|
|
|
} else if passwd.ExportType().Kind() != reflect.String { |
|
|
|
|
return nil, fmt.Errorf("third argument must be the password to unlock the account") |
|
|
|
|
return nil, errors.New("third argument must be the password to unlock the account") |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Send the request to the backend and return
|
|
|
|
|
sign, callable := goja.AssertFunction(getJeth(call.VM).Get("sign")) |
|
|
|
|
if !callable { |
|
|
|
|
return nil, fmt.Errorf("jeth.sign is not callable") |
|
|
|
|
return nil, errors.New("jeth.sign is not callable") |
|
|
|
|
} |
|
|
|
|
return sign(goja.Null(), message, account, passwd) |
|
|
|
|
} |
|
|
|
@ -315,11 +316,11 @@ func (b *bridge) Sign(call jsre.Call) (goja.Value, error) { |
|
|
|
|
// Sleep will block the console for the specified number of seconds.
|
|
|
|
|
func (b *bridge) Sleep(call jsre.Call) (goja.Value, error) { |
|
|
|
|
if nArgs := len(call.Arguments); nArgs < 1 { |
|
|
|
|
return nil, fmt.Errorf("usage: sleep(<number of seconds>)") |
|
|
|
|
return nil, errors.New("usage: sleep(<number of seconds>)") |
|
|
|
|
} |
|
|
|
|
sleepObj := call.Argument(0) |
|
|
|
|
if goja.IsUndefined(sleepObj) || goja.IsNull(sleepObj) || !isNumber(sleepObj) { |
|
|
|
|
return nil, fmt.Errorf("usage: sleep(<number of seconds>)") |
|
|
|
|
return nil, errors.New("usage: sleep(<number of seconds>)") |
|
|
|
|
} |
|
|
|
|
sleep := sleepObj.ToFloat() |
|
|
|
|
time.Sleep(time.Duration(sleep * float64(time.Second))) |
|
|
|
@ -336,17 +337,17 @@ func (b *bridge) SleepBlocks(call jsre.Call) (goja.Value, error) { |
|
|
|
|
) |
|
|
|
|
nArgs := len(call.Arguments) |
|
|
|
|
if nArgs == 0 { |
|
|
|
|
return nil, fmt.Errorf("usage: sleepBlocks(<n blocks>[, max sleep in seconds])") |
|
|
|
|
return nil, errors.New("usage: sleepBlocks(<n blocks>[, max sleep in seconds])") |
|
|
|
|
} |
|
|
|
|
if nArgs >= 1 { |
|
|
|
|
if goja.IsNull(call.Argument(0)) || goja.IsUndefined(call.Argument(0)) || !isNumber(call.Argument(0)) { |
|
|
|
|
return nil, fmt.Errorf("expected number as first argument") |
|
|
|
|
return nil, errors.New("expected number as first argument") |
|
|
|
|
} |
|
|
|
|
blocks = call.Argument(0).ToInteger() |
|
|
|
|
} |
|
|
|
|
if nArgs >= 2 { |
|
|
|
|
if goja.IsNull(call.Argument(1)) || goja.IsUndefined(call.Argument(1)) || !isNumber(call.Argument(1)) { |
|
|
|
|
return nil, fmt.Errorf("expected number as second argument") |
|
|
|
|
return nil, errors.New("expected number as second argument") |
|
|
|
|
} |
|
|
|
|
sleep = call.Argument(1).ToInteger() |
|
|
|
|
} |
|
|
|
@ -421,7 +422,7 @@ func (b *bridge) Send(call jsre.Call) (goja.Value, error) { |
|
|
|
|
JSON := call.VM.Get("JSON").ToObject(call.VM) |
|
|
|
|
parse, callable := goja.AssertFunction(JSON.Get("parse")) |
|
|
|
|
if !callable { |
|
|
|
|
return nil, fmt.Errorf("JSON.parse is not a function") |
|
|
|
|
return nil, errors.New("JSON.parse is not a function") |
|
|
|
|
} |
|
|
|
|
resultVal, err := parse(goja.Null(), call.VM.ToValue(string(result))) |
|
|
|
|
if err != nil { |
|
|
|
|