Skip to content

Commit

Permalink
Fix getting country code from pub signals
Browse files Browse the repository at this point in the history
  • Loading branch information
artemskriabin committed Sep 11, 2024
1 parent c671954 commit f93f793
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
9 changes: 6 additions & 3 deletions internal/service/requests/verify_passport.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,19 @@ func NewVerifyPassport(r *http.Request) (req resources.VerifyPassportRequest, er
// ExtractCountry extracts country code from the proof, converting decimal UTF-8
// code to ISO 3166-1 alpha-3 code.
func ExtractCountry(proof zkptypes.ZKProof) (string, error) {
if len(proof.PubSignals) <= int(zk.Citizenship) {
if len(proof.PubSignals) <= zk.Indexes(zk.GlobalPassport)[zk.Citizenship] {
return "", val.Errors{"country_code": val.ErrLengthTooShort}.Filter()
}

b, ok := new(big.Int).SetString(proof.PubSignals[zk.Citizenship], 10)
getter := zk.PubSignalGetter{Signals: proof.PubSignals, ProofType: zk.GlobalPassport}
code := getter.Get(zk.Citizenship)

b, ok := new(big.Int).SetString(code, 10)
if !ok {
b = new(big.Int)
}

code := string(b.Bytes())
code = string(b.Bytes())

return code, val.Errors{"country_code": val.Validate(code, val.Required, is.CountryCode3)}.Filter()
}
40 changes: 40 additions & 0 deletions internal/service/requests/verify_passport_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package requests

import (
zkptypes "github.com/iden3/go-rapidsnark/types"
"github.com/stretchr/testify/assert"
"testing"
)

func TestExtractCountry(t *testing.T) {
p := zkptypes.ZKProof{
PubSignals: []string{"9072791962122059526608165338435582217583998249448445715859811865975207897521",
"0",
"0",
"0",
"0",
"0",
"5589842",
"0",
"0",
"211985299740800702300256033401632392934377086534111448880928528431996790315",
"6334216257887790546056750444971451489366652820049478976520311646899632086040",
"6657866160187480897185968992500348980038797861763249572960971022605721484835",
"23073",
"55199728742705",
"0",
"1726052791",
"0",
"10",
"52983525027888",
"53009295421745",
"55199728742705",
"52983525027888",
"52983525027888",
},
}

code, err := ExtractCountry(p)
assert.NoError(t, err)
assert.Equal(t, "UKR", code)
}

0 comments on commit f93f793

Please sign in to comment.