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

solana added #168

Merged
merged 2 commits into from
May 6, 2024
Merged
Show file tree
Hide file tree
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
13 changes: 12 additions & 1 deletion api/v1/authenticate/authenticate.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,18 @@ func authenticate(c *gin.Context) {

}
if chain_symbol == "sol" {
isCorrect = true
walletAddr, userId, isCorrect, err = cryptosign.CheckSignSol(req.Signature, req.FlowId, req.Message, req.PubKey)

if err == cryptosign.ErrFlowIdNotFound {
httpo.NewErrorResponse(http.StatusNotFound, "Flow Id not found")
return
}

if err != nil {
logwrapper.Errorf("failed to CheckSignature, error %v", err.Error())
httpo.NewErrorResponse(http.StatusInternalServerError, "Unexpected error occured").SendD(c)
return
}
}
if isCorrect {
// update wallet address for that user_id
Expand Down
5 changes: 3 additions & 2 deletions api/v1/authenticate/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package authenticate

type AuthenticateRequest struct {
FlowId string `json:"flowId" binding:"required"`
Signature string `json:"signature" binding:"omitempty,hexadecimal,startswith=0x"`
PubKey string `json:"pubKey" binding:"omitempty,hexadecimal,startswith=0x"`
Signature string `json:"signature" binding:"omitempty,hexadecimal"`
PubKey string `json:"pubKey" binding:"omitempty"`
SignatureSui string `json:"signatureSui" binding:"omitempty"`
Message string `json:"message" binding:"omitempty"`
}
type AuthenticateRequestNoSign struct {
FlowId string `json:"flowId" binding:"required"`
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ require (
github.com/minio/blake2b-simd v0.0.0-20160723061019-3f5f724cb5b1 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/mr-tron/base58 v1.2.0
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rjeczalik/notify v0.9.1 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,8 @@ github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lN
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
github.com/mr-tron/base58 v1.2.0 h1:T/HDJBh4ZCPbU39/+c3rRvE0uKBQlU27+QI8LJ4t64o=
github.com/mr-tron/base58 v1.2.0/go.mod h1:BinMc/sQntlIE1frQmRFPUoPA1Zkr8VRgBdjWI2mNwc=
github.com/mschoch/smat v0.0.0-20160514031455-90eadee771ae/go.mod h1:qAyveg+e4CE+eKJXWVjKXM4ck2QobLqTDytGJbLLhJg=
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
github.com/naoina/go-stringutil v0.1.0/go.mod h1:XJ2SJL9jCtBh+P9q5btrd/Ylo8XwT/h1USek5+NqSA0=
Expand Down
30 changes: 30 additions & 0 deletions util/pkg/cryptosign/checksign.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cryptosign

import (
"crypto/ecdsa"
"crypto/ed25519"
"crypto/elliptic"
"encoding/base64"
"encoding/hex"
Expand All @@ -13,6 +14,7 @@ import (
"github.com/NetSepio/gateway/config/dbconfig"
"github.com/NetSepio/gateway/models"
"github.com/minio/blake2b-simd"
"github.com/mr-tron/base58"
"golang.org/x/crypto/nacl/sign"
"golang.org/x/crypto/sha3"
"gorm.io/gorm"
Expand Down Expand Up @@ -158,3 +160,31 @@ func CheckSignSui(signature string, flowId string) (string, string, bool, error)

return flowIdData.UserId, flowIdData.WalletAddress, true, nil
}

func CheckSignSol(signature string, flowId string, message string, pubKey string) (string, string, bool, error) {

db := dbconfig.GetDb()
bytes, err := base58.Decode(pubKey)
if err != nil {
return "", "", false, err
}
messageAsBytes := []byte(message)

signedMessageAsBytes, err := hex.DecodeString(signature)

if err != nil {

return "", "", false, err
}

var flowIdData models.FlowId
err = db.Model(&models.FlowId{}).Where("flow_id = ?", flowId).First(&flowIdData).Error
if errors.Is(err, gorm.ErrRecordNotFound) {
return "", "", false, err
}

ed25519.Verify(bytes, messageAsBytes, signedMessageAsBytes)

return flowIdData.WalletAddress, flowIdData.UserId, true, nil

}
Loading