Exemplo n.º 1
0
// if the nonce is given, the addr is not needed
func checkCommon(addr, amtS, gasS, priceS string, seq uint64) (from common.Address, nonce uint64, amount, gas, price *big.Int, err error) {
	// resolve the big ints
	if amount, err = stringToBig(amtS); err != nil {
		err = fmt.Errorf("amt %s is bad hex: %v", amtS, err)
		return
	}
	if gas, err = stringToBig(gasS); err != nil {
		err = fmt.Errorf("gas %s is bad hex: %v", gasS, err)
		return
	}
	if price, err = stringToBig(priceS); err != nil {
		err = fmt.Errorf("price %s is bad hex: %v", priceS, err)
		return
	}

	// resolve the address
	if addr == "" {
		err = fmt.Errorf("--addr must be given")
		return
	}
	var addrBytes []byte
	addrBytes, err = hex.DecodeString(utils.StripHex(addr))
	if err != nil {
		err = fmt.Errorf("addr is bad hex: %v", err)
		return
	}
	from = common.BytesToAddress(addrBytes)

	// resolve the nonce (or fetch it)
	if seq == 0 {
		if EthClient.Host == "" {
			// NOTE this error only applies to ethtx, not other possible consumers of ethtx/core
			err = fmt.Errorf("input must specify a nonce with the --nonce flag or use --node-addr (or ETHTX_NODE_ADDR) to fetch the nonce from a node")
			return
		}

		var r interface{}
		// fetch block num
		r, err = EthClient.RequestResponse("eth", "blockNumber")
		if err != nil {
			err = fmt.Errorf("Error fetching block number: %v", err)
			return
		}
		// NOTE: both block num and account nonces are hex. (why?!)
		blockNum := utils.HexToInt(r.(string))

		r, err = EthClient.RequestResponse("eth", "getTransactionCount", addr, blockNum)
		if err != nil {
			err = fmt.Errorf("Error fetching account nonce: %v", err)
			return
		}

		nonce = uint64(utils.HexToInt(r.(string)))
	} else {
		nonce = seq
	}

	return
}
Exemplo n.º 2
0
func Send(fromAddr, toAddr, amtS, gasS, priceS string, nonce uint64) (*Transaction, error) {
	from, nonce, amt, gas, price, err := checkCommon(fromAddr, amtS, gasS, priceS, nonce)
	if err != nil {
		return nil, err
	}

	if toAddr == "" {
		return nil, fmt.Errorf("destination address must be given with --to flag")
	}

	toAddrBytes, err := hex.DecodeString(utils.StripHex(toAddr))
	if err != nil {
		return nil, fmt.Errorf("toAddr is bad hex: %v", err)
	}
	to := common.BytesToAddress(toAddrBytes)

	return NewTransaction(&to, &from, nonce, amt, gas, price, nil), nil
}
Exemplo n.º 3
0
func (self *testFrontend) UnlockAccount(acc []byte) bool {
	self.ethereum.AccountManager().Unlock(common.BytesToAddress(acc), "password")
	return true
}