/
proof_upload.go
143 lines (122 loc) · 3.97 KB
/
proof_upload.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package controller
import (
"crypto/ecdsa"
"encoding/base64"
"net/http"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
"github.com/nextdotid/proof_server/model"
"github.com/nextdotid/proof_server/types"
"github.com/nextdotid/proof_server/util"
mycrypto "github.com/nextdotid/proof_server/util/crypto"
"github.com/nextdotid/proof_server/util/sqs"
"github.com/nextdotid/proof_server/validator"
"golang.org/x/xerrors"
)
type ProofUploadRequest struct {
Action types.Action `json:"action"`
Platform types.Platform `json:"platform"`
Identity string `json:"identity"`
ProofLocation string `json:"proof_location"`
PublicKey string `json:"public_key"`
Uuid string `json:"uuid"`
CreatedAt string `json:"created_at"`
Extra ProofUploadRequestExtra `json:"extra"`
}
type ProofUploadRequestExtra struct {
Signature string `json:"signature"`
EthereumWalletSignature string `json:"wallet_signature"`
}
func proofUpload(c *gin.Context) {
req := ProofUploadRequest{}
err := c.BindJSON(&req)
if err != nil {
errorResp(c, 400, xerrors.Errorf("parse request failed: %w", err))
return
}
pubkey, err := mycrypto.StringToPubkey(req.PublicKey)
if err != nil {
errorResp(c, 400, xerrors.Errorf("%w", err))
return
}
previous_pc, err := model.ProofChainFindLatest(mycrypto.CompressedPubkeyHex(pubkey))
if err != nil {
errorResp(c, 500, xerrors.Errorf("internal database error"))
return
}
validator, err := validateProof(req, previous_pc, pubkey)
if err != nil {
errorResp(c, 400, xerrors.Errorf("%w", err))
return
}
if err = applyUpload(&validator); err != nil {
errorResp(c, 400, xerrors.Errorf("%w", err))
return
}
if err = triggerArweave(model.MarshalPersona(pubkey)); err != nil {
// Do not errorResp here, since it is a tolerable error.
l.Warnf("error sending arweave upload message: %v", err)
}
c.JSON(http.StatusCreated, gin.H{})
}
func validateProof(req ProofUploadRequest, prev *model.ProofChain, pubkey *ecdsa.PublicKey) (validator.Base, error) {
prev_signature := ""
if prev != nil {
prev_signature = prev.Signature
}
performer_factory, ok := validator.PlatformFactories[req.Platform]
if !ok {
return validator.Base{}, xerrors.Errorf("platform not supported: %s", string(req.Platform))
}
created_at, err := util.TimestampStringToTime(req.CreatedAt)
if err != nil {
return validator.Base{}, xerrors.Errorf("error when parsing created_at: %s not recognized", req.CreatedAt)
}
parsed_uuid, err := uuid.Parse(req.Uuid)
if err != nil {
return validator.Base{}, xerrors.Errorf("error when parsing uuid: %s not recognized", req.Uuid)
}
base := validator.Base{
Platform: req.Platform,
Previous: prev_signature,
Action: req.Action,
Pubkey: pubkey,
Identity: req.Identity,
ProofLocation: req.ProofLocation,
CreatedAt: created_at,
Uuid: parsed_uuid,
}
if req.Extra.Signature != "" || req.Platform == types.Platforms.Ethereum {
extra := map[string]string{}
extra["wallet_signature"] = req.Extra.EthereumWalletSignature
base.Extra = extra
persona_sig, err := base64.StdEncoding.DecodeString(req.Extra.Signature)
if err != nil {
return validator.Base{}, xerrors.Errorf("error when decoding persona signature: %w", err)
}
base.Signature = persona_sig
}
performer := performer_factory(&base)
return base, performer.Validate()
}
func applyUpload(validator *validator.Base) error {
pc, err := model.ProofChainCreateFromValidator(validator)
if err != nil {
return xerrors.Errorf("%w", err)
}
err = pc.Apply()
if err != nil {
return xerrors.Errorf("%w", err)
}
return nil
}
func triggerArweave(persona string) error {
msg := types.QueueMessage{
Action: types.QueueActions.ArweaveUpload,
Persona: persona,
}
if err := sqs.Send(msg); err != nil {
return xerrors.Errorf("error sending message to queue: %w", err)
}
return nil
}