Skip to content

Commit 87090c8

Browse files
committed
adding chain authentication
1 parent f331312 commit 87090c8

File tree

7 files changed

+791
-27
lines changed

7 files changed

+791
-27
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,6 @@ erebrus-linux-x64
3535
# bin folder
3636
/bin
3737

38-
env.text
38+
env.text
39+
D:/jjsahdd/server.json
40+
D:/jjsahdd/wg0.conf

.sample-env

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ REGION=EU
88

99
# PASETO Specifications
1010
PASETO_EXPIRATION_IN_HOURS=168
11-
AUTH_EULA=I Accept the Erebrus Terms of Service https://erebrus.io/terms.html for accessing the application. Challenge ID:
11+
AUTH_EULA=I Accept the Erebrus Terms of Service https://erebrus.io/terms.html for accessing the application.
1212
SIGNED_BY=Erebrus
1313
FOOTER=Erebrus 2024
1414

api/v1/authenticate/authenticate.go

Lines changed: 88 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package authenticate
22

33
import (
4+
"fmt"
45
"net/http"
56
"os"
67

78
"github.com/NetSepio/erebrus/api/v1/authenticate/challengeid"
89
"github.com/NetSepio/erebrus/util/pkg/auth"
910
"github.com/NetSepio/erebrus/util/pkg/claims"
10-
"github.com/NetSepio/erebrus/util/pkg/cryptosign"
11+
"github.com/NetSepio/gateway/util/pkg/logwrapper"
12+
"github.com/TheLazarusNetwork/go-helpers/httpo"
1113

1214
"github.com/gin-gonic/gin"
1315
log "github.com/sirupsen/logrus"
@@ -38,27 +40,95 @@ func authenticate(c *gin.Context) {
3840
}
3941
userAuthEULA := os.Getenv("AUTH_EULA")
4042
message := userAuthEULA + req.ChallengeId
41-
walletAddress, isCorrect, err := cryptosign.CheckSign(req.Signature, req.ChallengeId, message)
43+
// walletAddress, isCorrect, err := cryptosign.CheckSign(req.Signature, req.ChallengeId, message)
4244

43-
if err == cryptosign.ErrFlowIdNotFound {
44-
log.WithFields(log.Fields{
45-
"err": err,
46-
}).Error("FlowId Not Found")
47-
errResponse := ErrAuthenticate(err.Error())
48-
c.JSON(http.StatusNotFound, errResponse)
49-
return
50-
}
45+
// if err == cryptosign.ErrChallangeIdNotFound {
46+
// log.WithFields(log.Fields{
47+
// "err": err,
48+
// }).Error("FlowId Not Found")
49+
// errResponse := ErrAuthenticate(err.Error())
50+
// c.JSON(http.StatusNotFound, errResponse)
51+
// return
52+
// }
5153

52-
if err != nil {
53-
log.WithFields(log.Fields{
54-
"err": err,
55-
}).Error("failed to CheckSignature")
56-
errResponse := ErrAuthenticate(err.Error())
57-
c.JSON(http.StatusInternalServerError, errResponse)
58-
return
54+
// if err != nil {
55+
// log.WithFields(log.Fields{
56+
// "err": err,
57+
// }).Error("failed to CheckSignature")
58+
// errResponse := ErrAuthenticate(err.Error())
59+
// c.JSON(http.StatusInternalServerError, errResponse)
60+
// return
61+
// }
62+
63+
var (
64+
isCorrect bool
65+
// userId string
66+
walletAddr string
67+
)
68+
69+
switch req.ChainName {
70+
case "EVM", "PEAQ":
71+
userAuthEULA := userAuthEULA
72+
message := userAuthEULA + req.ChallengeId
73+
walletAddr, isCorrect, err = CheckSignEth(req.Signature, req.ChallengeId, message)
74+
75+
if err == ErrChallangeIdNotFound {
76+
httpo.NewErrorResponse(http.StatusNotFound, "Challange Id not found")
77+
return
78+
}
79+
80+
if err != nil {
81+
logwrapper.Errorf("failed to CheckSignature, error %v", err.Error())
82+
httpo.NewErrorResponse(http.StatusInternalServerError, "Unexpected error occurred").SendD(c)
83+
return
84+
}
85+
86+
case "APTOS":
87+
userAuthEULA := userAuthEULA
88+
message := fmt.Sprintf("APTOS\nmessage: %v\nnonce: %v", userAuthEULA, req.ChallengeId)
89+
walletAddr, isCorrect, err = CheckSign(req.Signature, req.ChallengeId, message, req.PubKey)
90+
91+
if err == ErrChallangeIdNotFound {
92+
httpo.NewErrorResponse(http.StatusNotFound, "Challange Id not found")
93+
return
94+
}
95+
96+
if err != nil {
97+
logwrapper.Errorf("failed to CheckSignature, error %v", err.Error())
98+
httpo.NewErrorResponse(http.StatusInternalServerError, "Unexpected error occurred").SendD(c)
99+
return
100+
}
101+
102+
case "SUI":
103+
walletAddr, isCorrect, err = CheckSignSui(req.Signature, req.ChallengeId)
104+
105+
if err == ErrChallangeIdNotFound {
106+
httpo.NewErrorResponse(http.StatusNotFound, "Challange Id not found")
107+
return
108+
}
109+
110+
if err != nil {
111+
logwrapper.Errorf("failed to CheckSignature, error %v", err.Error())
112+
httpo.NewErrorResponse(http.StatusInternalServerError, "Unexpected error occurred").SendD(c)
113+
return
114+
}
115+
116+
case "SOLANA":
117+
walletAddr, isCorrect, err = CheckSignSol(req.Signature, req.ChallengeId, message, req.PubKey)
118+
119+
if err == ErrChallangeIdNotFound {
120+
httpo.NewErrorResponse(http.StatusNotFound, "Challange Id not found")
121+
return
122+
}
123+
124+
if err != nil {
125+
logwrapper.Errorf("failed to CheckSignature, error %v", err.Error())
126+
httpo.NewErrorResponse(http.StatusInternalServerError, "Unexpected error occurred").SendD(c)
127+
return
128+
}
59129
}
60130
if isCorrect {
61-
customClaims := claims.New(walletAddress)
131+
customClaims := claims.New(walletAddr)
62132
pasetoToken, err := auth.GenerateTokenPaseto(customClaims)
63133
if err != nil {
64134
log.WithFields(log.Fields{

api/v1/authenticate/challengeid/challengid.go

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package challengeid
22

33
import (
44
"encoding/hex"
5+
"math/big"
56
"net/http"
67
"os"
78
"regexp"
@@ -61,17 +62,19 @@ func GetChallengeId(c *gin.Context) {
6162

6263
if err := ValidateAddress(chainName, walletAddress); err != nil {
6364

64-
info := " pass chain name between SOLANA, PEAQ, APTOS, SUI, ECLIPSE"
65+
info := "chain name = " + chainName + "; pass chain name between SOLANA, PEAQ, APTOS, SUI, ECLIPSE, EVM"
6566

6667
switch err {
6768
case ErrInvalidChain:
6869
log.WithFields(log.Fields{"err": ErrInvalidChain}).Error("failed to create client")
6970
response := core.MakeErrorResponse(http.StatusNotAcceptable, ErrInvalidChain.Error()+info, nil, nil, nil)
7071
c.JSON(http.StatusNotAcceptable, response)
72+
return
7173
case ErrInvalidAddress:
7274
log.WithFields(log.Fields{"err": ErrInvalidAddress}).Error("failed to create client")
7375
response := core.MakeErrorResponse(http.StatusNotAcceptable, ErrInvalidAddress.Error(), nil, nil, nil)
7476
c.JSON(http.StatusNotAcceptable, response)
77+
return
7578
}
7679
return
7780
}
@@ -117,6 +120,7 @@ func GenerateChallengeId(walletAddress string, chainName string) (string, error)
117120
var dbdata MemoryDB
118121
dbdata.WalletAddress = walletAddress
119122
dbdata.Timestamp = time.Now()
123+
dbdata.ChainName = chainName
120124
Data = map[string]MemoryDB{
121125
challengeId: dbdata,
122126
}
@@ -126,10 +130,14 @@ func GenerateChallengeId(walletAddress string, chainName string) (string, error)
126130
// ValidateAddress validates a wallet address for the specified blockchain
127131
func ValidateAddress(chain, address string) error {
128132
// Convert chain name to lowercase for case-insensitive comparison
129-
chain = strings.ToLower(chain)
133+
// chain = strings.ToLower(chain)
130134

131135
switch chain {
132-
case "SOLANA", "SOL", "ECLIPSE":
136+
case "EVM":
137+
if !ValidateAddressEtherium(address) {
138+
return ErrInvalidAddress
139+
}
140+
case "SOLANA", "ECLIPSE":
133141
if !ValidateSolanaAddress(address) {
134142
return ErrInvalidAddress
135143
}
@@ -139,7 +147,7 @@ func ValidateAddress(chain, address string) error {
139147
return ErrInvalidAddress
140148
}
141149

142-
case "APTOS", "APT":
150+
case "APTOS":
143151
if !ValidateAptosAddress(address) {
144152
return ErrInvalidAddress
145153
}
@@ -201,3 +209,11 @@ func ValidateSuiAddress(address string) bool {
201209
_, err := hex.DecodeString(address)
202210
return err == nil
203211
}
212+
213+
func ValidateAddressEtherium(address string) bool {
214+
if len(address) != 42 || !strings.HasPrefix(address, "0x") {
215+
return false
216+
}
217+
_, isValid := big.NewInt(0).SetString(address[2:], 16)
218+
return isValid
219+
}

0 commit comments

Comments
 (0)