Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/upload meme v2 #2315

Merged
merged 2 commits into from
Jan 2, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 75 additions & 1 deletion handlers/meme.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,73 @@ func GetMemeChallenge() db.MemeChallenge {
return memeChallenge
}

type SignChallengeBody struct {
Message string `json:"message"`
}

type V2SignChallengeResponse struct {
Sig string `json:"sig"`
}

func SignChallenge(challenge string) db.RelaySignerResponse {

if config.V2BotUrl != "" {
url := fmt.Sprintf("%s/sign_base64", config.V2BotUrl)
client := &http.Client{}

challengeBody := SignChallengeBody{
Message: challenge,
}

jsonBody, _ := json.Marshal(challengeBody)

req, err := http.NewRequest(http.MethodPost, url, bytes.NewBuffer(jsonBody))
req.Header.Set("x-admin-token", config.V2BotToken)
req.Header.Set("Content-Type", "application/json")
res, _ := client.Do(req)

if err != nil {
log.Printf("[Sign Challenge for V2] Request Failed: %s", err)
return db.RelaySignerResponse{
Success: false,
Response: db.SignerResponse(db.SignerResponse{Sig: ""}),
}
}

defer res.Body.Close()

body, err := io.ReadAll(res.Body)

if err != nil {
log.Printf("[Sign Challenge for V2] Reading sign challenge response body failed: %s", err)
return db.RelaySignerResponse{
Success: false,
Response: db.SignerResponse(db.SignerResponse{Sig: ""}),
}
}

v2SignChallengeResponse := V2SignChallengeResponse{}

// Unmarshal result
err = json.Unmarshal(body, &v2SignChallengeResponse)

if err != nil {
log.Printf("[Sign Challenge for V2] Unmarshalling response body failed: %s", err)
return db.RelaySignerResponse{
Success: false,
Response: db.SignerResponse(db.SignerResponse{Sig: ""}),
}
}

signerResponse := db.RelaySignerResponse{
Success: true,
Response: db.SignerResponse(v2SignChallengeResponse),
}

return signerResponse

}

url := fmt.Sprintf("%s/signer/%s", config.RelayUrl, challenge)

client := &http.Client{}
Expand Down Expand Up @@ -142,10 +208,18 @@ func SignChallenge(challenge string) db.RelaySignerResponse {
func GetMemeToken(id string, sig string) (string, db.MemeTokenSuccess) {
memeUrl := fmt.Sprintf("%s/verify", config.MemeUrl)

var pubkey string

if config.V2BotUrl != "" {
pubkey = config.GetV2ContactKey()
} else {
pubkey = config.RelayNodeKey
}

formData := url.Values{
"id": {id},
"sig": {sig},
"pubkey": {config.RelayNodeKey},
"pubkey": {pubkey},
}

res, err := http.PostForm(memeUrl, formData)
Expand Down
Loading