-
-func readIntFromFile(path string) (int, error) {
- data, err := ioutil.ReadFile(path)
- if err != nil {
- return 0, err
- }
- val, err := strconv.Atoi(strings.TrimRight(string(data), "\n"))
- if err != nil {
- return 0, err
- }
- return val, nil
-}
-
-// Get peer related configuration.
-func (id *PeerId) Conf() *PeerConf {
- if dummyConf != nil {
- return dummyConf
- }
- conf := PeerConf{Id: id, NoiseEnable: false, CPR: 0}
- peerPath := path.Join(PeersPath, id.String())
-
- verPath := path.Join(peerPath, "verifier")
- keyData, err := ioutil.ReadFile(verPath)
- if err != nil {
- log.Println("Unable to read verifier:", verPath)
- return nil
- }
- if len(keyData) < ed25519.PublicKeySize*2 {
- log.Println("Verifier must be 64 hex characters long:", verPath)
- return nil
- }
- keyDecoded, err := hex.DecodeString(string(keyData[:ed25519.PublicKeySize*2]))
- if err != nil {
- log.Println("Unable to decode the key:", err.Error(), verPath)
- return nil
- }
- conf.DSAPub = new([ed25519.PublicKeySize]byte)
- copy(conf.DSAPub[:], keyDecoded)
-
- timeout := TimeoutDefault
- if val, err := readIntFromFile(path.Join(peerPath, "timeout")); err == nil {
- timeout = val
- }
- conf.Timeout = time.Second * time.Duration(timeout)
-
- if val, err := readIntFromFile(path.Join(peerPath, "noise")); err == nil && val == 1 {
- conf.NoiseEnable = true
- }
- if val, err := readIntFromFile(path.Join(peerPath, "cpr")); err == nil {
- conf.CPR = val
- }
- return &conf
-}
-
-// Decode identification string.
-// It must be 32 hexadecimal characters long.
-func IDDecode(raw string) (*PeerId, error) {
- if len(raw) != IDSize*2 {
- return nil, errors.New("ID must be 32 characters long")
- }
- idDecoded, err := hex.DecodeString(raw)
- if err != nil {
- return nil, errors.New("ID must contain hexadecimal characters only")
- }
- idP := new([IDSize]byte)
- copy(idP[:], idDecoded)
- id := PeerId(*idP)
- return &id, nil
-}