This package makes implementing a Web3 Wallets Login possible in few lines of code!
The protocol requires two endpoints to issue a token. The first call provides the client with a verifiable random challenge. While the second call verifies the challenge signature then issues a token that can then be used either on or off-chain.
import (
"github.com/samyfodil/web3auth"
"github.com/samyfodil/web3auth/key"
)
var (
issuer web3auth.Issuer
)
func init() {
sk, _ := key.New("HEX-OF-PRIVATE-KEY")
issuer = web3auth.New().Issuer(
sk,
7*24*time.Hour,
)
}
This call will generate a challenge. In this example I'm using a Taubyte dFunc.
//export auth_wallet_init
func authWalletInit(e event.Event) uint32 {
h, err := e.HTTP()
if err != nil {
return 1
}
defer func() {
if err != nil {
h.Write(web3auth.MarshaledInitReplyError(err))
}
}()
body, err := io.ReadAll(h.Body())
if err != nil {
return 1
}
msg := &proto.InitMessage{}
err = msg.UnmarshalJSON(body)
if err != nil {
return 1
}
res := issuer0.Challenge(msg)
resBytes, err := res.MarshalJSON()
if err != nil {
return 1
}
h.Write(resBytes)
h.Return(200)
return 0
}
This call verifies the wallet.
//export auth_wallet
func authWallet(e event.Event) uint32 {
h, err := e.HTTP()
if err != nil {
return 1
}
defer func() {
if err != nil {
h.Write(web3auth.MarshaledReplyError(err))
}
}()
body, err := io.ReadAll(h.Body())
if err != nil {
return 1
}
msg := &proto.Message{}
err = msg.UnmarshalJSON(body)
if err != nil {
return 1
}
res := issuer1.Issue(msg)
resData, _ := res.MarshalJSON()
h.Write(resData)
return 0
}
var (
verifier web3auth.Verifier
)
func init() {
pk, _ := key.New("HEX-OF-PUBLIC-KEY")
verifier = web3auth.New().Verifier(pk)
}
err := verifier.Validate("token")
- Samy Fodil @samyfodil