-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
ocr2_keys_controller.go
125 lines (109 loc) · 4.17 KB
/
ocr2_keys_controller.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
114
115
116
117
118
119
120
121
122
123
124
125
package web
import (
"io"
"net/http"
"github.com/gin-gonic/gin"
"github.com/pkg/errors"
"github.com/smartcontractkit/chainlink/v2/core/logger/audit"
"github.com/smartcontractkit/chainlink/v2/core/services/chainlink"
"github.com/smartcontractkit/chainlink/v2/core/services/keystore/chaintype"
"github.com/smartcontractkit/chainlink/v2/core/web/presenters"
)
// OCRKeysController manages OCR key bundles
type OCR2KeysController struct {
App chainlink.Application
}
// Index lists OCR2 key bundles
// Example:
// "GET <application>/keys/ocr"
func (ocr2kc *OCR2KeysController) Index(c *gin.Context) {
ekbs, err := ocr2kc.App.GetKeyStore().OCR2().GetAll()
if err != nil {
jsonAPIError(c, http.StatusInternalServerError, err)
return
}
jsonAPIResponse(c, presenters.NewOCR2KeysBundleResources(ekbs), "offChainReporting2KeyBundle")
}
// Create and return an OCR2 key bundle
// Example:
// "POST <application>/keys/ocr"
func (ocr2kc *OCR2KeysController) Create(c *gin.Context) {
chainType := chaintype.ChainType(c.Param("chainType"))
key, err := ocr2kc.App.GetKeyStore().OCR2().Create(chainType)
if errors.Is(errors.Cause(err), chaintype.ErrInvalidChainType) {
jsonAPIError(c, http.StatusBadRequest, err)
return
}
if err != nil {
jsonAPIError(c, http.StatusInternalServerError, err)
return
}
ocr2kc.App.GetAuditLogger().Audit(audit.OCR2KeyBundleCreated, map[string]interface{}{
"ocr2KeyID": key.ID(),
"ocr2KeyChainType": key.ChainType(),
"ocr2KeyConfigEncryptionPublicKey": key.ConfigEncryptionPublicKey(),
"ocr2KeyOffchainPublicKey": key.OffchainPublicKey(),
"ocr2KeyMaxSignatureLength": key.MaxSignatureLength(),
"ocr2KeyPublicKey": key.PublicKey(),
})
jsonAPIResponse(c, presenters.NewOCR2KeysBundleResource(key), "offChainReporting2KeyBundle")
}
// Delete an OCR2 key bundle
// Example:
// "DELETE <application>/keys/ocr/:keyID"
func (ocr2kc *OCR2KeysController) Delete(c *gin.Context) {
id := c.Param("keyID")
key, err := ocr2kc.App.GetKeyStore().OCR2().Get(id)
if err != nil {
jsonAPIError(c, http.StatusNotFound, err)
return
}
err = ocr2kc.App.GetKeyStore().OCR2().Delete(id)
if err != nil {
jsonAPIError(c, http.StatusInternalServerError, err)
return
}
ocr2kc.App.GetAuditLogger().Audit(audit.OCR2KeyBundleDeleted, map[string]interface{}{"id": id})
jsonAPIResponse(c, presenters.NewOCR2KeysBundleResource(key), "offChainReporting2KeyBundle")
}
// Import imports an OCR2 key bundle
// Example:
// "Post <application>/keys/ocr/import"
func (ocr2kc *OCR2KeysController) Import(c *gin.Context) {
defer ocr2kc.App.GetLogger().ErrorIfFn(c.Request.Body.Close, "Error closing Import request body")
bytes, err := io.ReadAll(c.Request.Body)
if err != nil {
jsonAPIError(c, http.StatusBadRequest, err)
return
}
oldPassword := c.Query("oldpassword")
keyBundle, err := ocr2kc.App.GetKeyStore().OCR2().Import(bytes, oldPassword)
if err != nil {
jsonAPIError(c, http.StatusInternalServerError, err)
return
}
ocr2kc.App.GetAuditLogger().Audit(audit.OCR2KeyBundleImported, map[string]interface{}{
"ocr2KeyID": keyBundle.ID(),
"ocr2KeyChainType": keyBundle.ChainType(),
"ocr2KeyConfigEncryptionPublicKey": keyBundle.ConfigEncryptionPublicKey(),
"ocr2KeyOffchainPublicKey": keyBundle.OffchainPublicKey(),
"ocr2KeyMaxSignatureLength": keyBundle.MaxSignatureLength(),
"ocr2KeyPublicKey": keyBundle.PublicKey(),
})
jsonAPIResponse(c, presenters.NewOCR2KeysBundleResource(keyBundle), "offChainReporting2KeyBundle")
}
// Export exports an OCR2 key bundle
// Example:
// "Post <application>/keys/ocr/export"
func (ocr2kc *OCR2KeysController) Export(c *gin.Context) {
defer ocr2kc.App.GetLogger().ErrorIfFn(c.Request.Body.Close, "Error closing Export response body")
stringID := c.Param("ID")
newPassword := c.Query("newpassword")
bytes, err := ocr2kc.App.GetKeyStore().OCR2().Export(stringID, newPassword)
if err != nil {
jsonAPIError(c, http.StatusInternalServerError, err)
return
}
ocr2kc.App.GetAuditLogger().Audit(audit.OCR2KeyBundleExported, map[string]interface{}{"keyID": stringID})
c.Data(http.StatusOK, MediaType, bytes)
}