forked from CyberMiles/travis
-
Notifications
You must be signed in to change notification settings - Fork 3
/
account.go
80 lines (69 loc) · 2.2 KB
/
account.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package commons
import (
"fmt"
"github.com/CyberMiles/travis/sdk"
"github.com/CyberMiles/travis/utils"
"github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/accounts/keystore"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/state"
"math"
"os"
"path/filepath"
"time"
)
const (
// Ethereum default keystore directory
datadirDefaultKeyStore = "keystore"
)
var (
emHome = os.ExpandEnv("$HOME/.travis")
)
func MakeAccountManager() (*accounts.Manager, string, error) {
scryptN := keystore.StandardScryptN
scryptP := keystore.StandardScryptP
keydir := filepath.Join(emHome, datadirDefaultKeyStore)
ephemeral := keydir
if err := os.MkdirAll(keydir, 0700); err != nil {
return nil, "", err
}
// Assemble the account manager and supported backends
backends := []accounts.Backend{
keystore.NewKeyStore(keydir, scryptN, scryptP),
}
return accounts.NewManager(backends...), ephemeral, nil
}
// fetchKeystore retrives the encrypted keystore from the account manager.
func fetchKeystore(am *accounts.Manager) *keystore.KeyStore {
return am.Backends(keystore.KeyStoreType)[0].(*keystore.KeyStore)
}
func UnlockAccount(am *accounts.Manager, addr common.Address, password string, duration *uint64) (bool, error) {
const max = uint64(time.Duration(math.MaxInt64) / time.Second)
var d time.Duration
if duration == nil {
d = 300 * time.Second
} else if *duration > max {
return false, fmt.Errorf("unlock duration too large")
} else {
d = time.Duration(*duration) * time.Second
}
err := fetchKeystore(am).TimedUnlock(accounts.Account{Address: addr}, password, d)
return err == nil, err
}
func Transfer(from, to common.Address, amount sdk.Int) error {
utils.StateChangeQueue = append(utils.StateChangeQueue, utils.StateChangeObject{
From: from, To: to, Amount: amount})
return nil
}
func TransferWithReactor(from, to common.Address, amount sdk.Int, reactor utils.StateChangeReactor) error {
utils.StateChangeQueue = append(utils.StateChangeQueue, utils.StateChangeObject{
from,
to,
amount,
reactor,
})
return nil
}
func GetBalance(state *state.StateDB, addr common.Address) (sdk.Int, error) {
return sdk.NewIntFromBigInt(state.GetBalance(addr)), nil
}