/
registrationInfo.go
67 lines (63 loc) · 1.78 KB
/
registrationInfo.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
package account
import (
"fmt"
. "github.com/archoncloud/archoncloud-go/common"
"github.com/archoncloud/archoncloud-go/interfaces"
"github.com/pariz/gountries"
"github.com/pkg/errors"
"strings"
)
const RegistrationFileName = "registration.txt"
const RegistrationVersion = 2
func GetRegistrationInfo(regPath string) (*interfaces.RegistrationInfo, error) {
if regPath == "" {
regPath = DefaultToExecutable(RegistrationFileName)
}
r := interfaces.RegistrationInfo{
"USA",
1,
interfaces.EthereumValues{3.5, 1.0},
interfaces.NeoValues{0.002},
RegistrationVersion,
}
if !FileExists(regPath) {
// generate default and exit
err := SaveConfiguration(&r, regPath)
if err == nil {
err = fmt.Errorf("You need to first fill in %s, then try again\n", regPath)
} else {
err = fmt.Errorf("Cannot save default registration template %s\n", regPath)
}
return nil, err
}
var errMsgs []string
errP := fmt.Sprintf("in %s", regPath)
err := GetConfiguration(&r, regPath)
if err != nil {return nil, errors.WithMessage(err, errP)}
if r.PledgedGigaBytes <= 0.0 {
errMsgs = append(errMsgs,"pledged_giga_bytes must be larger than 0")
}
if r.Ethereum.EthStake <= 0.0 {
errMsgs = append(errMsgs,"Eth_stake must be larger than 0")
}
if r.Ethereum.WeiPerByte <= 0.0 {
errMsgs = append(errMsgs,"Wei_per_byte must be larger than 0")
}
if r.CountryA3 == "" {
r.CountryA3 = "USA"
} else {
query := gountries.New()
_, err := query.FindCountryByAlpha(r.CountryA3)
if err != nil {
errMsgs = append(errMsgs,"unknown country code " + r.CountryA3 + " (Must be ISO 3166-1 alpha-3)")
}
}
if r.Version < RegistrationVersion {
// New fields added
SaveConfiguration(&r, regPath)
}
if len(errMsgs) == 0 {
return &r, nil
}
return nil, errors.New(strings.Join(errMsgs, "\n\t"))
}