-
Notifications
You must be signed in to change notification settings - Fork 0
/
account.go
94 lines (82 loc) · 2.13 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package whisper
import (
"errors"
"fmt"
"io/ioutil"
"os"
"strings"
"github.com/golang/glog"
)
func AccountId(url string) (string, error) {
// try reading from account file, if not set or invalid, will generate and overwrite
// generate own
gen := func() (string, error) {
empty := make([]interface{}, 0)
if id, err := WhisperSend(nil, url, NEW_IDENTITY, empty, 3); err != nil {
return "", err
} else {
switch s := interface{}(id).(type) {
case WhisperRPCIncomingMsgSingleStr:
glog.V(3).Infof("Returned message from whisper generate id: %v", s.Result)
if s.Result == "" {
return "", errors.New("No returned whisper id from generate call, cannot proceed")
}
return s.Result, nil
default:
return "", fmt.Errorf("Unexpected return type: %T", id)
}
}
}
whisperFile := os.Getenv("CMTN_WHISPER_ADDRESS_PATH")
var file *os.File
var err error
if _, err = os.Stat(whisperFile); os.IsNotExist(err) {
file, err = os.Create(whisperFile)
if err != nil {
return "", err
}
} else {
file, err = os.OpenFile(whisperFile, os.O_RDWR, 0600)
if err != nil {
return "", err
}
}
defer file.Close()
var id string
if err != nil {
return "", err
} else {
if data, err := ioutil.ReadFile(file.Name()); err != nil {
return "", err
} else {
id = strings.Trim(string(data), "\n\r ")
glog.V(2).Infof("Read whisper id from file: %v", id)
// check the id
if in, err := WhisperSend(nil, url, CHECK_IDENTITY, WrapParam(id), 5); err != nil {
return "", err
} else {
switch s := interface{}(in).(type) {
case WhisperRPCIncomingMsgSingleBool:
glog.V(3).Infof("Returned message from whisper identity check: %v", s.Result)
if !s.Result {
glog.V(3).Infof("Client returned false for check identity call, generating own")
newId, err := gen()
if err != nil {
return "", err
}
// attempt to write it
_, err = file.Write([]byte(newId))
if err != nil {
return "", err
} else {
return newId, nil
}
}
return id, nil
default:
return "", fmt.Errorf("Unexpected type: %T", in)
}
}
}
}
}