Skip to content

Commit

Permalink
Merge pull request #29 from 0xPolygonID/feat/amoy-update
Browse files Browse the repository at this point in the history
Make golang demo working. Serve static files for both examples. Updat…
  • Loading branch information
Kolezhniuk authored Apr 17, 2024
2 parents de37f7b + 1236e18 commit 8077fbe
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 14 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
# tutorial-examples

## Verifier integration demo

1. JS example
- `cd verifier-integration/js`
- `npm install`
- `node index.js`
2. Go example
- `cd verifier-integration/go`
- `go run index.go`

For additional info please visit [Polygon ID tutorial](https://devs.polygonid.com/docs/quick-start-demo)
49 changes: 36 additions & 13 deletions verifier-integration/go/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,39 @@ import (
"io"
"log"
"net/http"
"os"
"strconv"
"time"

"github.com/ethereum/go-ethereum/common"
circuits "github.com/iden3/go-circuits/v2"
auth "github.com/iden3/go-iden3-auth/v2"

"github.com/iden3/go-iden3-auth/v2/loaders"
"github.com/iden3/go-iden3-auth/v2/pubsignals"
"github.com/iden3/go-iden3-auth/v2/state"
"github.com/iden3/iden3comm/v2/protocol"
)

const VerificationKeyPath = "verification_key.json"

type KeyLoader struct {
Dir string
}

// Load keys from embedded FS
func (m KeyLoader) Load(id circuits.CircuitID) ([]byte, error) {
return os.ReadFile(fmt.Sprintf("%s/%v/%s", m.Dir, id, VerificationKeyPath))
}

func main() {
fs := http.FileServer(http.Dir("../static"))
http.Handle("/", fs)
http.HandleFunc("/api/sign-in", GetAuthRequest)
http.HandleFunc("/api/callback", Callback)
http.ListenAndServe(":8080", nil)
log.Println("Starting server at port 8080")
if err := http.ListenAndServe(":8080", nil); err != nil {
log.Fatal(err)
}
}

// Create a map to store the auth requests and their session IDs
Expand Down Expand Up @@ -76,12 +92,17 @@ func GetAuthRequest(w http.ResponseWriter, r *http.Request) {

// Callback works with sign-in callbacks
func Callback(w http.ResponseWriter, r *http.Request) {

fmt.Println("callback")
// Get session ID from request
sessionID := r.URL.Query().Get("sessionId")

// get JWZ token params from the post request
tokenBytes, _ := io.ReadAll(r.Body)
tokenBytes, err := io.ReadAll(r.Body)

if err != nil {
log.Println(err)
return
}

// Add Polygon Mumbai RPC node endpoint - needed to read on-chain state
ethURL := "AMOY_RPC_URL"
Expand All @@ -98,10 +119,9 @@ func Callback(w http.ResponseWriter, r *http.Request) {
authRequest := requestMap[sessionID]

// print authRequest
fmt.Println(authRequest)
log.Println(authRequest)

// load the verification key
var verificationKeyloader = &loaders.FSKeyLoader{Dir: keyDIR}
var verificationKeyLoader = &KeyLoader{Dir: keyDIR}
resolver := state.ETHResolver{
RPCUrl: ethURL,
ContractAddress: common.HexToAddress(contractAddress),
Expand All @@ -112,7 +132,7 @@ func Callback(w http.ResponseWriter, r *http.Request) {
}

// EXECUTE VERIFICATION
verifier, err := auth.NewVerifier(verificationKeyloader, resolvers, auth.WithIPFSGateway("https://ipfs.io"))
verifier, err := auth.NewVerifier(verificationKeyLoader, resolvers, auth.WithIPFSGateway("https://ipfs.io"))
if err != nil {
log.Println(err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError)
Expand All @@ -129,13 +149,16 @@ func Callback(w http.ResponseWriter, r *http.Request) {
return
}

userID := authResponse.From

messageBytes := []byte("User with ID " + userID + " Successfully authenticated")
//marshal auth resp
messageBytes, err := json.Marshal(authResponse)
if err != nil {
log.Println(err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "application/json")
w.Write(messageBytes)

return
log.Println("verification passed")
}
3 changes: 2 additions & 1 deletion verifier-integration/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const getRawBody = require("raw-body");
const app = express();
const port = 8080;

app.use(express.static("static"));
app.use(express.static("../static"));

app.get("/api/sign-in", (req, res) => {
console.log("get Auth Request");
Expand Down Expand Up @@ -75,6 +75,7 @@ async function callback(req, res) {
// get JWZ token params from the post request
const raw = await getRawBody(req);
const tokenStr = raw.toString().trim();
console.log(tokenStr);

const ethURL = "<AMOY_URL>";
const contractAddress = "0x1a4cC30f2aA0377b0c3bc9848766D90cb4404124";
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 8077fbe

Please sign in to comment.