Skip to content

Commit 2999358

Browse files
authored
Merge pull request #9 from threefoldtech/add-documentation-for-how-to-use-registrar
add documentations for registrar
2 parents bb83aa4 + 7bc318a commit 2999358

File tree

7 files changed

+704
-331
lines changed

7 files changed

+704
-331
lines changed

.goreleaser.yaml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,32 @@ builds:
1818
- -X github.com/threefoldtech/tfgrid4-sdk-go/node-registrar/cmd.version={{.Tag}}
1919
- -X github.com/threefoldtech/tfgrid-sdk-go/node-registrar/cmd.commit={{.Commit}}
2020

21+
- dir: ./node-registrar/tools/account
22+
env:
23+
- CGO_ENABLED=0
24+
goos:
25+
- linux
26+
- windows
27+
- darwin
28+
binary: new-account
29+
id: new-account
30+
31+
ignore:
32+
- goos: windows
33+
34+
- dir: ./node-registrar/tools/farm
35+
env:
36+
- CGO_ENABLED=0
37+
goos:
38+
- linux
39+
- windows
40+
- darwin
41+
binary: new-farm
42+
id: new-farm
43+
44+
ignore:
45+
- goos: windows
46+
2147

2248
archives:
2349
- format: tar.gz

node-registrar/docs/README.md

Lines changed: 322 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,322 @@
1+
# Node Registrar client
2+
3+
To be able to use the node registrar you can use the following scripts.
4+
5+
## Account Management
6+
7+
- **Create a Seed:**
8+
9+
```go
10+
// Generate new seed
11+
seed := make([]byte, 32)
12+
_, err := rand.Read(seed)
13+
if err != nil {
14+
panic(err)
15+
}
16+
17+
hexKey := hex.EncodeToString(seed)
18+
fmt.Println("New Seed (Hex):", hexKey)
19+
20+
```
21+
22+
- **Parse The Seed**
23+
24+
```go
25+
// Generate Key Pair
26+
privateKey := ed25519.NewKeyFromSeed(seed)
27+
publicKey := privateKey.Public().(ed25519.PublicKey)
28+
29+
fmt.Println("Private Key (Hex):", hex.EncodeToString(privateKey))
30+
fmt.Println("Public Key (Hex):", hex.EncodeToString(publicKey))
31+
32+
```
33+
34+
- **Create Account**
35+
36+
```go
37+
url, err := url.JoinPath(registrarURL, "accounts")
38+
if err != nil {
39+
panic(err)
40+
}
41+
42+
timestamp := time.Now().Unix()
43+
publicKeyBase64 := base64.StdEncoding.EncodeToString(publicKey)
44+
45+
challenge := []byte(fmt.Sprintf("%d:%v", timestamp, publicKeyBase64))
46+
signature := ed25519.Sign(privateKey, challenge)
47+
48+
data := map[string]any{
49+
"public_key": publicKey,
50+
"signature": signature,
51+
"timestamp": timestamp,
52+
"rmb_enc_key": rmbEncKey,
53+
"relays": relays,
54+
}
55+
56+
var body bytes.Buffer
57+
err = json.NewEncoder(&body).Encode(data)
58+
if err != nil {
59+
panic(err)
60+
}
61+
62+
resp, err := http.DefaultClient.Post(url, "application/json", &body)
63+
if err != nil {
64+
panic(err)
65+
}
66+
67+
if resp.StatusCode != http.StatusCreated {
68+
panic(fmt.Errorf("account not created successfully"))
69+
}
70+
71+
defer resp.Body.Close()
72+
73+
var account map[string]any
74+
err = json.NewDecoder(resp.Body).Decode(&account)
75+
76+
fmt.Println(account["twin_id"])
77+
```
78+
79+
- **Get Account:**
80+
81+
```go
82+
url, err := url.JoinPath(registrarURL, "accounts")
83+
if err != nil {
84+
panic(err)
85+
}
86+
87+
req, err := http.NewRequest("GET", url, nil)
88+
if err != nil {
89+
return
90+
}
91+
92+
q := req.URL.Query()
93+
q.Add("twin_id", fmt.Sprint(twinID))
94+
req.URL.RawQuery = q.Encode()
95+
96+
resp, err := http.DefaultClient.Do(req)
97+
if err != nil {
98+
panic(err)
99+
}
100+
defer resp.Body.Close()
101+
102+
if resp.StatusCode != http.StatusNotFound {
103+
panic(fmt.Errorf("status code not ok"))
104+
}
105+
106+
var account map[string]any
107+
err = json.NewDecoder(resp.Body).Decode(&account)
108+
fmt.Println(account)
109+
```
110+
111+
## Farm Management
112+
113+
- **Create a Farm:**
114+
115+
```go
116+
url, err := url.JoinPath(registrarURL, "farms")
117+
if err != nil {
118+
panic(err)
119+
}
120+
121+
data := map[string]any{
122+
"farm_name": farmName,
123+
"twin_id": twinID,
124+
"dedicated": dedicated,
125+
}
126+
127+
var body bytes.Buffer
128+
err = json.NewEncoder(&body).Encode(data)
129+
if err != nil {
130+
panic(err)
131+
}
132+
133+
req, err := http.NewRequest("POST", url, &body)
134+
if err != nil {
135+
panic(err)
136+
}
137+
138+
timestamp := time.Now().Unix()
139+
challenge := []byte(fmt.Sprintf("%d:%v", timestamp, twinID))
140+
signature := ed25519.Sign(privateKey, challenge)
141+
142+
authHeader := fmt.Sprintf(
143+
"%s:%s",
144+
base64.StdEncoding.EncodeToString(challenge),
145+
base64.StdEncoding.EncodeToString(signature),
146+
)
147+
req.Header.Set("X-Auth", authHeader)
148+
req.Header.Set("Content-Type", "application/json")
149+
150+
resp, err := http.DefaultClient.Do(req)
151+
if err != nil {
152+
panic(err)
153+
}
154+
155+
defer resp.Body.Close()
156+
157+
if resp.StatusCode != http.StatusCreated {
158+
panic(err)
159+
}
160+
161+
result := struct {
162+
FarmID uint64 `json:"farm_id"`
163+
}{}
164+
165+
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
166+
panic(err)
167+
}
168+
169+
fmt.Println(result.FarmID)
170+
```
171+
172+
- **Get Farm:**
173+
174+
```go
175+
url, err := url.JoinPath(registrarURL, "farms", fmt.Sprint(farmID))
176+
if err != nil {
177+
panic(err)
178+
}
179+
resp, err := http.DefaultClient.Get(url)
180+
if err != nil {
181+
panic(err)
182+
}
183+
184+
if resp.StatusCode != http.StatusOK {
185+
panic(err)
186+
}
187+
defer resp.Body.Close()
188+
189+
var result map[string]any
190+
err = json.NewDecoder(resp.Body).Decode(&result)
191+
if err != nil {
192+
panic(err)
193+
}
194+
195+
fmt.Println(result)
196+
```
197+
198+
## Zos Version Management
199+
200+
- **Get Zos Version:**
201+
202+
```go
203+
url, err := url.JoinPath(registrarURL, "zos", "version")
204+
if err != nil {
205+
panic(err)
206+
}
207+
208+
resp, err := http.DefaultClient.Get(url)
209+
if err != nil {
210+
panic(err)
211+
}
212+
213+
if resp.StatusCode != http.StatusOK {
214+
panic(err)
215+
}
216+
217+
defer resp.Body.Close()
218+
219+
var versionString string
220+
err = json.NewDecoder(resp.Body).Decode(&versionString)
221+
if err != nil {
222+
panic(err)
223+
}
224+
225+
versionBytes, err := base64.StdEncoding.DecodeString(versionString)
226+
if err != nil {
227+
panic(err)
228+
}
229+
230+
correctedJSON := strings.ReplaceAll(string(versionBytes), "'", "\"")
231+
232+
var version map[string]any
233+
err = json.NewDecoder(strings.NewReader(correctedJSON)).Decode(&version)
234+
if err != nil {
235+
panic(err)
236+
}
237+
238+
fmt.Println(version)
239+
```
240+
241+
- **Set Zos Version**
242+
To set zos version you need to have the seed to the admin account
243+
244+
```go
245+
url, err := url.JoinPath(registrarURL, "zos", "version")
246+
if err != nil {
247+
panic(err)
248+
}
249+
250+
v := "{'safe_to_upgrade': true, 'version':'v0.1.7'}"
251+
252+
version := struct {
253+
Version string `json:"version"`
254+
}{
255+
Version: base64.StdEncoding.EncodeToString([]byte(v)),
256+
}
257+
258+
body, err := json.Marshal(version)
259+
if err != nil {
260+
panic(err)
261+
}
262+
263+
// Create auth headers
264+
timestamp := time.Now().Unix()
265+
challenge := []byte(fmt.Sprintf("%d:%v", timestamp, twinID))
266+
signature := ed25519.Sign(privateKey, challenge)
267+
268+
req, err := http.NewRequest("PUT", url, bytes.NewReader(body))
269+
if err != nil {
270+
panic(err)
271+
}
272+
273+
// Set required headers
274+
authHeader := fmt.Sprintf(
275+
"%s:%s",
276+
base64.StdEncoding.EncodeToString(challenge),
277+
base64.StdEncoding.EncodeToString(signature),
278+
)
279+
req.Header.Set("X-Auth", authHeader)
280+
req.Header.Set("Content-Type", "application/json")
281+
282+
resp, err := http.DefaultClient.Do(req)
283+
if err != nil {
284+
panic(err)
285+
}
286+
defer resp.Body.Close()
287+
288+
if resp.StatusCode != http.StatusOK {
289+
panic(err)
290+
}
291+
292+
fmt.Println("version updated successfully: ", string(body))
293+
```
294+
295+
## Node Management
296+
297+
- **Get Node:**
298+
299+
```go
300+
url, err := url.JoinPath(registrarURL, "nodes", fmt.Sprint(nodeID))
301+
if err != nil {
302+
panic(err)
303+
}
304+
305+
resp, err := http.DefaultClient.Get(url)
306+
if err != nil {
307+
panic(err)
308+
}
309+
310+
if resp.StatusCode != http.StatusOK {
311+
panic(err)
312+
}
313+
defer resp.Body.Close()
314+
315+
var result map[string]any
316+
err = json.NewDecoder(resp.Body).Decode(&result)
317+
if err != nil {
318+
panic(err)
319+
}
320+
321+
fmt.Println(result)
322+
```

0 commit comments

Comments
 (0)