2017-05-16 12:09:52 +00:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"reflect"
|
|
|
|
"runtime/debug"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/accounts"
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2018-01-26 05:59:21 +00:00
|
|
|
"github.com/pborman/uuid"
|
2017-08-10 13:35:58 +00:00
|
|
|
"github.com/status-im/status-go/geth/log"
|
2017-05-16 12:09:52 +00:00
|
|
|
"github.com/status-im/status-go/static"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// MessageIDKey is a key for message ID
|
|
|
|
// This ID is required to track from which chat a given send transaction request is coming.
|
|
|
|
MessageIDKey = contextKey("message_id")
|
|
|
|
)
|
|
|
|
|
|
|
|
type contextKey string // in order to make sure that our context key does not collide with keys from other packages
|
|
|
|
|
|
|
|
// errors
|
|
|
|
var (
|
|
|
|
ErrInvalidAccountAddressOrKey = errors.New("cannot parse address or key to valid account address")
|
|
|
|
)
|
|
|
|
|
|
|
|
// ParseAccountString parses hex encoded string and returns is as accounts.Account.
|
|
|
|
func ParseAccountString(account string) (accounts.Account, error) {
|
|
|
|
// valid address, convert to account
|
|
|
|
if common.IsHexAddress(account) {
|
|
|
|
return accounts.Account{Address: common.HexToAddress(account)}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return accounts.Account{}, ErrInvalidAccountAddressOrKey
|
|
|
|
}
|
|
|
|
|
|
|
|
// FromAddress converts account address from string to common.Address.
|
|
|
|
// The function is useful to format "From" field of send transaction struct.
|
|
|
|
func FromAddress(accountAddress string) common.Address {
|
|
|
|
from, err := ParseAccountString(accountAddress)
|
|
|
|
if err != nil {
|
|
|
|
return common.Address{}
|
|
|
|
}
|
|
|
|
|
|
|
|
return from.Address
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToAddress converts account address from string to *common.Address.
|
|
|
|
// The function is useful to format "To" field of send transaction struct.
|
|
|
|
func ToAddress(accountAddress string) *common.Address {
|
|
|
|
to, err := ParseAccountString(accountAddress)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return &to.Address
|
|
|
|
}
|
|
|
|
|
2017-11-07 17:46:11 +00:00
|
|
|
// ImportTestAccount imports keystore from static resources, see "static/keys" folder
|
2017-05-16 12:09:52 +00:00
|
|
|
func ImportTestAccount(keystoreDir, accountFile string) error {
|
|
|
|
// make sure that keystore folder exists
|
|
|
|
if _, err := os.Stat(keystoreDir); os.IsNotExist(err) {
|
2018-01-17 16:46:21 +00:00
|
|
|
os.MkdirAll(keystoreDir, os.ModePerm) // nolint: errcheck, gas
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
dst := filepath.Join(keystoreDir, accountFile)
|
2017-11-07 17:46:11 +00:00
|
|
|
err := ioutil.WriteFile(dst, static.MustAsset("keys/"+accountFile), 0644)
|
|
|
|
if err != nil {
|
|
|
|
log.Warn("cannot copy test account PK", "error", err)
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
|
|
|
|
2017-11-07 17:46:11 +00:00
|
|
|
return err
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// PanicAfter throws panic() after waitSeconds, unless abort channel receives notification
|
|
|
|
func PanicAfter(waitSeconds time.Duration, abort chan struct{}, desc string) {
|
|
|
|
go func() {
|
|
|
|
select {
|
|
|
|
case <-abort:
|
|
|
|
return
|
|
|
|
case <-time.After(waitSeconds):
|
|
|
|
panic("whatever you were doing takes toooo long: " + desc)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
// MessageIDFromContext returns message id from context (if exists)
|
|
|
|
func MessageIDFromContext(ctx context.Context) string {
|
|
|
|
if ctx == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
if messageID, ok := ctx.Value(MessageIDKey).(string); ok {
|
|
|
|
return messageID
|
|
|
|
}
|
|
|
|
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
// ParseJSONArray parses JSON array into Go array of string
|
|
|
|
func ParseJSONArray(items string) ([]string, error) {
|
|
|
|
var parsedItems []string
|
|
|
|
err := json.Unmarshal([]byte(items), &parsedItems)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return parsedItems, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fatalf is used to halt the execution.
|
|
|
|
// When called the function prints stack end exits.
|
|
|
|
// Failure is logged into both StdErr and StdOut.
|
|
|
|
func Fatalf(reason interface{}, args ...interface{}) {
|
|
|
|
// decide on output stream
|
|
|
|
w := io.MultiWriter(os.Stdout, os.Stderr)
|
2018-01-17 16:46:21 +00:00
|
|
|
outf, _ := os.Stdout.Stat() // nolint: gas
|
|
|
|
errf, _ := os.Stderr.Stat() // nolint: gas
|
2017-05-16 12:09:52 +00:00
|
|
|
if outf != nil && errf != nil && os.SameFile(outf, errf) {
|
|
|
|
w = os.Stderr
|
|
|
|
}
|
|
|
|
|
|
|
|
// find out whether error or string has been passed as a reason
|
|
|
|
r := reflect.ValueOf(reason)
|
|
|
|
if r.Kind() == reflect.String {
|
2018-02-04 23:07:47 +00:00
|
|
|
fmt.Fprintf(w, "Fatal Failure: %v\n%v\n", reason.(string), args) //nolint: gas
|
2017-05-16 12:09:52 +00:00
|
|
|
} else {
|
2018-01-17 16:46:21 +00:00
|
|
|
fmt.Fprintf(w, "Fatal Failure: %v\n", reason.(error)) //nolint: gas
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
debug.PrintStack()
|
|
|
|
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2018-01-26 05:59:21 +00:00
|
|
|
|
|
|
|
// CreateTransaction returns a transaction object.
|
|
|
|
func CreateTransaction(ctx context.Context, args SendTxArgs) *QueuedTx {
|
|
|
|
return &QueuedTx{
|
|
|
|
ID: QueuedTxID(uuid.New()),
|
|
|
|
Context: ctx,
|
|
|
|
Args: args,
|
2018-01-05 20:58:17 +00:00
|
|
|
Result: make(chan TransactionResult, 1),
|
2018-01-26 05:59:21 +00:00
|
|
|
}
|
|
|
|
}
|