-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
ocr_keys_controller.go
112 lines (96 loc) · 3.48 KB
/
ocr_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
package web
import (
"io"
"net/http"
"github.com/gin-gonic/gin"
"github.com/smartcontractkit/chainlink/v2/core/logger/audit"
"github.com/smartcontractkit/chainlink/v2/core/services/chainlink"
"github.com/smartcontractkit/chainlink/v2/core/web/presenters"
)
// OCRKeysController manages OCR key bundles
type OCRKeysController struct {
App chainlink.Application
}
// Index lists OCR key bundles
// Example:
// "GET <application>/keys/ocr"
func (ocrkc *OCRKeysController) Index(c *gin.Context) {
ekbs, err := ocrkc.App.GetKeyStore().OCR().GetAll()
if err != nil {
jsonAPIError(c, http.StatusInternalServerError, err)
return
}
jsonAPIResponse(c, presenters.NewOCRKeysBundleResources(ekbs), "offChainReportingKeyBundle")
}
// Create and return an OCR key bundle
// Example:
// "POST <application>/keys/ocr"
func (ocrkc *OCRKeysController) Create(c *gin.Context) {
key, err := ocrkc.App.GetKeyStore().OCR().Create()
if err != nil {
jsonAPIError(c, http.StatusInternalServerError, err)
return
}
ocrkc.App.GetAuditLogger().Audit(audit.OCRKeyBundleCreated, map[string]interface{}{
"ocrKeyBundleID": key.ID(),
"ocrKeyBundlePublicKeyAddressOnChain": key.PublicKeyAddressOnChain(),
})
jsonAPIResponse(c, presenters.NewOCRKeysBundleResource(key), "offChainReportingKeyBundle")
}
// Delete an OCR key bundle
// Example:
// "DELETE <application>/keys/ocr/:keyID"
// "DELETE <application>/keys/ocr/:keyID?hard=true"
func (ocrkc *OCRKeysController) Delete(c *gin.Context) {
id := c.Param("keyID")
key, err := ocrkc.App.GetKeyStore().OCR().Get(id)
if err != nil {
jsonAPIError(c, http.StatusNotFound, err)
return
}
_, err = ocrkc.App.GetKeyStore().OCR().Delete(id)
if err != nil {
jsonAPIError(c, http.StatusInternalServerError, err)
return
}
ocrkc.App.GetAuditLogger().Audit(audit.OCRKeyBundleDeleted, map[string]interface{}{"id": id})
jsonAPIResponse(c, presenters.NewOCRKeysBundleResource(key), "offChainReportingKeyBundle")
}
// Import imports an OCR key bundle
// Example:
// "Post <application>/keys/ocr/import"
func (ocrkc *OCRKeysController) Import(c *gin.Context) {
defer ocrkc.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")
encryptedOCRKeyBundle, err := ocrkc.App.GetKeyStore().OCR().Import(bytes, oldPassword)
if err != nil {
jsonAPIError(c, http.StatusInternalServerError, err)
return
}
ocrkc.App.GetAuditLogger().Audit(audit.OCRKeyBundleImported, map[string]interface{}{
"OCRID": encryptedOCRKeyBundle.GetID(),
"OCRPublicKeyAddressOnChain": encryptedOCRKeyBundle.PublicKeyAddressOnChain(),
"OCRPublicKeyOffChain": encryptedOCRKeyBundle.PublicKeyOffChain(),
})
jsonAPIResponse(c, encryptedOCRKeyBundle, "offChainReportingKeyBundle")
}
// Export exports an OCR key bundle
// Example:
// "Post <application>/keys/ocr/export"
func (ocrkc *OCRKeysController) Export(c *gin.Context) {
defer ocrkc.App.GetLogger().ErrorIfFn(c.Request.Body.Close, "Error closing Export response body")
stringID := c.Param("ID")
newPassword := c.Query("newpassword")
bytes, err := ocrkc.App.GetKeyStore().OCR().Export(stringID, newPassword)
if err != nil {
jsonAPIError(c, http.StatusInternalServerError, err)
return
}
ocrkc.App.GetAuditLogger().Audit(audit.OCRKeyBundleExported, map[string]interface{}{"keyID": stringID})
c.Data(http.StatusOK, MediaType, bytes)
}