summaryrefslogtreecommitdiff
path: root/daemon/kmd/client/requests.go
blob: dbd6cf69af0351bf3b5865b8fd571b484c09c278 (plain)
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Copyright (C) 2019-2023 Algorand, Inc.
// This file is part of go-algorand
//
// go-algorand is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// go-algorand is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with go-algorand.  If not, see <https://www.gnu.org/licenses/>.

package client

import (
	"bytes"
	"fmt"
	"net/http"

	v1 "github.com/algorand/go-algorand/daemon/kmd/api/v1"
	"github.com/algorand/go-algorand/daemon/kmd/lib/kmdapi"
	"github.com/algorand/go-algorand/protocol"
)

// DoV1Request accepts a request from kmdapi/requests and
func (kcl KMDClient) DoV1Request(req kmdapi.APIV1Request, resp kmdapi.APIV1Response) error {
	var body []byte

	// Get the path and method for this request type
	reqPath, reqMethod, err := getPathAndMethod(req)
	if err != nil {
		return err
	}

	// Encode the request
	body = protocol.EncodeJSON(req)
	fullPath := fmt.Sprintf("http://%s/%s", kcl.address, reqPath)
	hreq, err := http.NewRequest(reqMethod, fullPath, bytes.NewReader(body))
	if err != nil {
		return err
	}

	// Add the auth token
	hreq.Header.Add(v1.KMDTokenHeader, kcl.apiToken)

	// Send the request
	hresp, err := kcl.httpClient.Do(hreq)
	if err != nil {
		return err
	}

	// Decode the response object
	decoder := protocol.NewJSONDecoder(hresp.Body)
	err = decoder.Decode(resp)
	hresp.Body.Close()
	if err != nil {
		return err
	}

	// Check if this was an error response
	err = resp.GetError()
	if err != nil {
		return err
	}

	return nil
}

// getPathAndMethod infers the request path and method from the request type
func getPathAndMethod(req kmdapi.APIV1Request) (reqPath string, reqMethod string, err error) {
	switch req.(type) {
	default:
		err = fmt.Errorf("unknown request type")
	case kmdapi.VersionsRequest:
		reqPath = "versions"
		reqMethod = "GET"
	case kmdapi.APIV1GETWalletsRequest:
		reqPath = "v1/wallets"
		reqMethod = "GET"
	case kmdapi.APIV1POSTWalletRequest:
		reqPath = "v1/wallet"
		reqMethod = "POST"
	case kmdapi.APIV1POSTWalletInitRequest:
		reqPath = "v1/wallet/init"
		reqMethod = "POST"
	case kmdapi.APIV1POSTWalletReleaseRequest:
		reqPath = "v1/wallet/release"
		reqMethod = "POST"
	case kmdapi.APIV1POSTWalletRenewRequest:
		reqPath = "v1/wallet/renew"
		reqMethod = "POST"
	case kmdapi.APIV1POSTWalletRenameRequest:
		reqPath = "v1/wallet/rename"
		reqMethod = "POST"
	case kmdapi.APIV1POSTWalletInfoRequest:
		reqPath = "v1/wallet/info"
		reqMethod = "POST"
	case kmdapi.APIV1POSTMasterKeyExportRequest:
		reqPath = "v1/master-key/export"
		reqMethod = "POST"
	case kmdapi.APIV1POSTKeyImportRequest:
		reqPath = "v1/key/import"
		reqMethod = "POST"
	case kmdapi.APIV1POSTKeyExportRequest:
		reqPath = "v1/key/export"
		reqMethod = "POST"
	case kmdapi.APIV1POSTKeyRequest:
		reqPath = "v1/key"
		reqMethod = "POST"
	case kmdapi.APIV1DELETEKeyRequest:
		reqPath = "v1/key"
		reqMethod = "DELETE"
	case kmdapi.APIV1POSTKeyListRequest:
		reqPath = "v1/key/list"
		reqMethod = "POST"
	case kmdapi.APIV1POSTProgramSignRequest:
		reqPath = "v1/program/sign"
		reqMethod = "POST"
	case kmdapi.APIV1POSTTransactionSignRequest:
		reqPath = "v1/transaction/sign"
		reqMethod = "POST"
	case kmdapi.APIV1POSTMultisigListRequest:
		reqPath = "v1/multisig/list"
		reqMethod = "POST"
	case kmdapi.APIV1POSTMultisigImportRequest:
		reqPath = "v1/multisig/import"
		reqMethod = "POST"
	case kmdapi.APIV1POSTMultisigExportRequest:
		reqPath = "v1/multisig/export"
		reqMethod = "POST"
	case kmdapi.APIV1POSTMultisigTransactionSignRequest:
		reqPath = "v1/multisig/sign"
		reqMethod = "POST"
	case kmdapi.APIV1POSTMultisigProgramSignRequest:
		reqPath = "v1/multisig/signprogram"
		reqMethod = "POST"
	case kmdapi.APIV1DELETEMultisigRequest:
		reqPath = "v1/multisig"
		reqMethod = "DELETE"
	}
	return
}