-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.go
113 lines (93 loc) · 2.59 KB
/
plugin.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// Copyright (c) 2017-2019 The Bitum developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package main
import (
"encoding/hex"
"encoding/json"
"fmt"
"net/http"
"strconv"
"github.com/bitum-project/politeia/bitumplugin"
pd "github.com/bitum-project/politeia/politeiad/api/v1"
"github.com/bitum-project/politeia/util"
)
// PluginSetting is a structure that holds key/value pairs of a plugin setting.
type PluginSetting struct {
Key string // Name of setting
Value string // Value of setting
}
// Plugin describes a plugin and its settings.
type Plugin struct {
ID string // Identifier
Version string // Version
Settings []PluginSetting // Settings
}
// getBestBlock asks the bitum plugin what the current best block is.
func (p *politeiawww) getBestBlock() (uint64, error) {
challenge, err := util.Random(pd.ChallengeSize)
if err != nil {
return 0, err
}
pc := pd.PluginCommand{
Challenge: hex.EncodeToString(challenge),
ID: bitumplugin.ID,
Command: bitumplugin.CmdBestBlock,
CommandID: bitumplugin.CmdBestBlock,
Payload: "",
}
responseBody, err := p.makeRequest(http.MethodPost,
pd.PluginCommandRoute, pc)
if err != nil {
return 0, err
}
var reply pd.PluginCommandReply
err = json.Unmarshal(responseBody, &reply)
if err != nil {
return 0, fmt.Errorf("Could not unmarshal "+
"PluginCommandReply: %v", err)
}
// Verify the challenge.
err = util.VerifyChallenge(p.cfg.Identity, challenge, reply.Response)
if err != nil {
return 0, err
}
bestBlock, err := strconv.ParseUint(reply.Payload, 10, 64)
if err != nil {
return 0, err
}
return bestBlock, nil
}
// getPluginInventory obtains the politeiad plugin inventory.
func (p *politeiawww) getPluginInventory() ([]Plugin, error) {
log.Tracef("getPluginInventory")
// Setup politeiad request
challenge, err := util.Random(pd.ChallengeSize)
if err != nil {
return nil, err
}
pi := pd.PluginInventory{
Challenge: hex.EncodeToString(challenge),
}
// Send politeiad request
responseBody, err := p.makeRequest(http.MethodPost,
pd.PluginInventoryRoute, pi)
if err != nil {
return nil, fmt.Errorf("makeRequest: %v", err)
}
// Handle response
var reply pd.PluginInventoryReply
err = json.Unmarshal(responseBody, &reply)
if err != nil {
return nil, err
}
err = util.VerifyChallenge(p.cfg.Identity, challenge, reply.Response)
if err != nil {
return nil, err
}
plugins := make([]Plugin, 0, len(reply.Plugins))
for _, v := range reply.Plugins {
plugins = append(plugins, convertPluginFromPD(v))
}
return plugins, nil
}