-
Notifications
You must be signed in to change notification settings - Fork 10
/
config.go
94 lines (85 loc) · 2.87 KB
/
config.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Copyright 2018 The cpchain authors
// This file is part of the cpchain library.
//
// The cpchain library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The cpchain library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the cpchain library. If not, see <http://www.gnu.org/licenses/>.
package config
import (
"crypto/ecdsa"
"os"
"path/filepath"
"bitbucket.org/cpchain/chain/accounts/keystore"
"bitbucket.org/cpchain/chain/api/cpclient"
"bitbucket.org/cpchain/chain/commons/log"
"bitbucket.org/cpchain/chain/configs"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
)
var (
endPoint = "http://localhost:8501"
keyStoreFilePath = "./chain/examples/cpchain/data/data1/keystore/"
// DefaultCPUDifficulty = uint64(19) // 1 cpu
DefaultCPUDifficulty = uint64(12) // 1 cpu
// DefaultMemoryDifficulty = uint64(9) // 16 G
DefaultMemoryDifficulty = uint64(6) // 2 G
DefaultCpuWorkTimeout = uint64(5)
DefaultMemoryWorkTimeout = uint64(5)
)
// overwrite from environment variables
func init() {
if val := os.Getenv("CPCHAIN_KEYSTORE_FILEPATH"); val != "" {
keyStoreFilePath = val
}
}
func SetConfig(ep, ksPath string) {
endPoint = ep
keyStoreFilePath = ksPath
}
func Connect(password string) (*cpclient.Client, error, *ecdsa.PrivateKey, *ecdsa.PublicKey, common.Address) {
ep, err := configs.ResolveUrl(endPoint)
if err != nil {
log.Fatal("unknown endpoint", "endpoint", endPoint, "err", err)
}
// Create client.
client, err := cpclient.Dial(ep)
if err != nil {
log.Fatal(err.Error())
}
// Open keystore file.
file, err := os.Open(keyStoreFilePath)
if err != nil {
log.Fatal(err.Error())
}
keyPath, err := filepath.Abs(filepath.Dir(file.Name()))
if err != nil {
log.Fatal(err.Error())
}
// Create keystore and get account.
kst := keystore.NewKeyStore(keyPath, 2, 1)
account := kst.Accounts()[0]
account, key, err := kst.GetDecryptedKey(account, password)
if err != nil {
log.Fatal(err.Error())
}
// Get private and public keys.
privateKey := key.PrivateKey
publicKey := privateKey.Public()
publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey)
if !ok {
log.Fatal("error casting public key to ECDSA")
}
// Get contractAddress.
fromAddress := crypto.PubkeyToAddress(*publicKeyECDSA)
// fmt.Println("from contractAddress:", fromAddress.Hex()) // 0xe94b7b6c5a0e526a4d97f9768ad6097bde25c62a
return client, err, privateKey, publicKeyECDSA, fromAddress
}