// MakeAddress converts an account specified directly as a hex encoded string or // a key index in the key store to an internal account representation. func MakeAddress(accman *accounts.Manager, account string) (accounts.Account, error) { // If the specified account is a valid address, return it if common.IsHexAddress(account) { return accounts.Account{Address: common.HexToAddress(account)}, nil } // Otherwise try to interpret the account as a keystore index index, err := strconv.Atoi(account) if err != nil { return accounts.Account{}, fmt.Errorf("invalid account address or index %q", account) } return accman.AccountByIndex(index) }
// MakeAddress converts an account specified directly as a hex encoded string or // a key index in the key store to an internal account representation. func MakeAddress(accman *accounts.Manager, account string) (a common.Address, err error) { // If the specified account is a valid address, return it if common.IsHexAddress(account) { return common.HexToAddress(account), nil } // Otherwise try to interpret the account as a keystore index index, err := strconv.Atoi(account) if err != nil { return a, fmt.Errorf("invalid account address or index %q", account) } hex, err := accman.AddressByIndex(index) if err != nil { return a, fmt.Errorf("can't get account #%d (%v)", index, err) } return common.HexToAddress(hex), nil }
// Transactions handles POST requests against an account endpoint to initiate // outbound value transfers to other accounts. func (a *api) Transactions(w http.ResponseWriter, r *http.Request) { params := mux.Vars(r) switch { case r.Method == "POST": // Parse and validate the transfer parameters sender := common.HexToAddress(params["address"]) recipient := r.FormValue("recipient") if !common.IsHexAddress(recipient) { log15.Warn("Invalid recipient", "recipient", recipient) http.Error(w, fmt.Sprintf("Invalid recipient: %s", recipient), http.StatusBadRequest) return } to := common.HexToAddress(recipient) amount := r.FormValue("amount") value, ok := new(big.Int).SetString(amount, 10) if !ok || value.Cmp(big.NewInt(0)) <= 0 { log15.Warn("Invalid amount", "amount", amount) http.Error(w, fmt.Sprintf("Invalid amount: %s", amount), http.StatusBadRequest) return } // Execute the value transfer and return an error or the transaction id id, err := a.eapis.Transfer(sender, to, value) if err != nil { log15.Error("Failed to execute transfer", "error", err) http.Error(w, err.Error(), http.StatusInternalServerError) return } w.Write([]byte(fmt.Sprintf("0x%x", id))) default: http.Error(w, "Unsupported method: "+r.Method, http.StatusMethodNotAllowed) } }
// Transact forms a transaction from the given arguments and submits it to the // transactio pool for execution. func (be *registryAPIBackend) Transact(fromStr, toStr, nonceStr, valueStr, gasStr, gasPriceStr, codeStr string) (string, error) { if len(toStr) > 0 && toStr != "0x" && !common.IsHexAddress(toStr) { return "", errors.New("invalid address") } var ( from = common.HexToAddress(fromStr) to = common.HexToAddress(toStr) value = common.Big(valueStr) gas *big.Int price *big.Int data []byte contractCreation bool ) if len(gasStr) == 0 { gas = big.NewInt(90000) } else { gas = common.Big(gasStr) } if len(gasPriceStr) == 0 { price = big.NewInt(10000000000000) } else { price = common.Big(gasPriceStr) } data = common.FromHex(codeStr) if len(toStr) == 0 { contractCreation = true } nonce := be.txPool.State().GetNonce(from) if len(nonceStr) != 0 { nonce = common.Big(nonceStr).Uint64() } var tx *types.Transaction if contractCreation { tx = types.NewContractCreation(nonce, value, gas, price, data) } else { tx = types.NewTransaction(nonce, to, value, gas, price, data) } acc := accounts.Account{from} signature, err := be.am.Sign(acc, tx.SigHash().Bytes()) if err != nil { return "", err } signedTx, err := tx.WithSignature(signature) if err != nil { return "", err } be.txPool.SetLocal(signedTx) if err := be.txPool.Add(signedTx); err != nil { return "", nil } if contractCreation { addr := crypto.CreateAddress(from, nonce) glog.V(logger.Info).Infof("Tx(%s) created: %s\n", signedTx.Hash().Hex(), addr.Hex()) } else { glog.V(logger.Info).Infof("Tx(%s) to: %s\n", signedTx.Hash().Hex(), tx.To().Hex()) } return signedTx.Hash().Hex(), nil }