示例#1
0
文件: jeth.go 项目: efaysal/etherapis
// NewAccount asks the user for the password and than executes the jeth.newAccount callback in the jsre
func (self *Jeth) NewAccount(call otto.FunctionCall) (response otto.Value) {
	if len(call.ArgumentList) == 0 {
		passwd, err := PromptPassword("Passphrase: ", true)
		if err != nil {
			return otto.FalseValue()
		}
		passwd2, err := PromptPassword("Repeat passphrase: ", true)
		if err != nil {
			return otto.FalseValue()
		}

		if passwd != passwd2 {
			fmt.Println("Passphrases don't match")
			return otto.FalseValue()
		}

		cmd := fmt.Sprintf("jeth.newAccount('%s')", passwd)
		if val, err := call.Otto.Run(cmd); err == nil {
			return val
		}
	} else {
		fmt.Println("New account doesn't expect argument(s), you will be prompted for a password")
	}

	return otto.FalseValue()
}
示例#2
0
文件: jeth.go 项目: efaysal/etherapis
// UnlockAccount asks the user for the password and than executes the jeth.UnlockAccount callback in the jsre
func (self *Jeth) UnlockAccount(call otto.FunctionCall) (response otto.Value) {
	var cmd, account, passwd string
	timeout := int64(300)
	var ok bool

	if len(call.ArgumentList) == 0 {
		fmt.Println("expected address of account to unlock")
		return otto.FalseValue()
	}

	if len(call.ArgumentList) >= 1 {
		if accountExport, err := call.Argument(0).Export(); err == nil {
			if account, ok = accountExport.(string); ok {
				if len(call.ArgumentList) == 1 {
					fmt.Printf("Unlock account %s\n", account)
					passwd, err = PromptPassword("Passphrase: ", true)
					if err != nil {
						return otto.FalseValue()
					}
				}
			}
		}
	}
	if len(call.ArgumentList) >= 2 {
		if passwdExport, err := call.Argument(1).Export(); err == nil {
			passwd, _ = passwdExport.(string)
		}
	}

	if len(call.ArgumentList) >= 3 {
		if timeoutExport, err := call.Argument(2).Export(); err == nil {
			timeout, _ = timeoutExport.(int64)
		}
	}

	cmd = fmt.Sprintf("jeth.unlockAccount('%s', '%s', %d)", account, passwd, timeout)
	if val, err := call.Otto.Run(cmd); err == nil {
		return val
	}

	return otto.FalseValue()
}
示例#3
0
文件: jsre.go 项目: efaysal/etherapis
// loadScript executes a JS script from inside the currently executing JS code.
func (self *JSRE) loadScript(call otto.FunctionCall) otto.Value {
	file, err := call.Argument(0).ToString()
	if err != nil {
		// TODO: throw exception
		return otto.FalseValue()
	}
	file = common.AbsolutePath(self.assetPath, file)
	source, err := ioutil.ReadFile(file)
	if err != nil {
		// TODO: throw exception
		return otto.FalseValue()
	}
	if _, err := compileAndRun(call.Otto, file, source); err != nil {
		// TODO: throw exception
		fmt.Println("err:", err)
		return otto.FalseValue()
	}
	// TODO: return evaluation result
	return otto.TrueValue()
}
示例#4
0
文件: jeth.go 项目: efaysal/etherapis
// SleepBlocks will wait for a specified number of new blocks or max for a
// given of seconds. sleepBlocks(nBlocks[, maxSleep]).
func (self *Jeth) SleepBlocks(call otto.FunctionCall) (response otto.Value) {
	nBlocks := int64(0)
	maxSleep := int64(9999999999999999) // indefinitely

	nArgs := len(call.ArgumentList)

	if nArgs == 0 {
		throwJSExeception("usage: sleepBlocks(<n blocks>[, max sleep in seconds])")
	}

	if nArgs >= 1 {
		if call.Argument(0).IsNumber() {
			nBlocks, _ = call.Argument(0).ToInteger()
		} else {
			throwJSExeception("expected number as first argument")
		}
	}

	if nArgs >= 2 {
		if call.Argument(1).IsNumber() {
			maxSleep, _ = call.Argument(1).ToInteger()
		} else {
			throwJSExeception("expected number as second argument")
		}
	}

	// go through the console, this will allow web3 to call the appropriate
	// callbacks if a delayed response or notification is received.
	currentBlockNr := func() int64 {
		result, err := call.Otto.Run("eth.blockNumber")
		if err != nil {
			throwJSExeception(err.Error())
		}
		blockNr, err := result.ToInteger()
		if err != nil {
			throwJSExeception(err.Error())
		}
		return blockNr
	}

	targetBlockNr := currentBlockNr() + nBlocks
	deadline := time.Now().Add(time.Duration(maxSleep) * time.Second)

	for time.Now().Before(deadline) {
		if currentBlockNr() >= targetBlockNr {
			return otto.TrueValue()
		}
		time.Sleep(time.Second)
	}

	return otto.FalseValue()
}