-
Notifications
You must be signed in to change notification settings - Fork 0
/
account.go
executable file
·73 lines (63 loc) · 1.43 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
package account
import (
"encoding/hex"
"errors"
"fmt"
"github.com/ethereum/go-ethereum/crypto"
"io/ioutil"
"net/http"
"net/url"
"strings"
)
type Account struct {
Address string `json:"address"`
PrivKey string `json:"priv_key"`
}
//off line
func NewAccountOffLine() (string, string, error) {
key, err := crypto.GenerateKey()
if err != nil {
fmt.Println("Error: ", err.Error());
}
if key == nil {
return "","", errors.New("the key generated is nil")
}
address := crypto.PubkeyToAddress(key.PublicKey).Hex()
privKey := hex.EncodeToString(key.D.Bytes())
return address, privKey, nil
}
//on line
func NewAccountOnLine(reqUrl, proxy string) ([]byte, error) {
cli := &http.Client{}
data := url.Values{}
data.Set("proxy", proxy)
req, err := http.NewRequest("POST", reqUrl, strings.NewReader(data.Encode()))
if err != nil {
panic(err)
}
req.Body = ioutil.NopCloser(strings.NewReader(data.Encode()))
// set request content types
req.Header.Set("Content-Type", "x-www-form-urlencoded")
// request
rep, err := cli.Do(req)
if err != nil {
panic(err)
}
b, err := ioutil.ReadAll(rep.Body)
if err != nil {
return nil, err
}
return b, nil
}
func NewAccount() Account {
key, err := crypto.GenerateKey()
if err != nil {
fmt.Println("Error: ", err.Error());
}
address := crypto.PubkeyToAddress(key.PublicKey).Hex()
privKey := hex.EncodeToString(key.D.Bytes())
return Account{
Address: address,
PrivKey: privKey,
}
}