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

add fix for exists operator #70

Merged
merged 1 commit into from
Mar 25, 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
14 changes: 13 additions & 1 deletion pubsignals/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/json"
"fmt"
"math/big"
"reflect"
"strconv"

"github.com/iden3/go-circuits/v2"
Expand Down Expand Up @@ -159,7 +160,7 @@ func ParseQueryMetadata(ctx context.Context, propertyQuery PropertyQuery, ldCont
}
}
if propertyQuery.Operator == circuits.EXISTS {
query.Values, err = transformQueryValueToBigInts(ctx, propertyQuery.OperatorValue, ld.XSDBoolean) // TODO: refactor
query.Values, err = transformExistsOperatorValueToBigInt(ctx, propertyQuery.OperatorValue)
} else {
query.Values, err = transformQueryValueToBigInts(ctx, propertyQuery.OperatorValue, query.Datatype)
}
Expand Down Expand Up @@ -221,6 +222,17 @@ func transformQueryValueToBigInts(_ context.Context, value any, ldType string) (
return []*big.Int{hashValue}, err
}

func transformExistsOperatorValueToBigInt(_ context.Context, value any) (out []*big.Int, err error) {
t := reflect.TypeOf(value).Kind()
if t != reflect.Bool {
return nil, errors.New("only boolean value is supported for operator $exists")
}
val := new(big.Int)
if value == true {
val.SetInt64(1)
}
return []*big.Int{val}, err
}
func isPositiveInteger(v interface{}) bool {
number, err := strconv.ParseFloat(fmt.Sprintf("%v", v), 64)
if err != nil {
Expand Down
53 changes: 53 additions & 0 deletions pubsignals/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,60 @@ func TestCheckRequest_Success(t *testing.T) {
})
}
}
func TestCheckRequestV3_Success(t *testing.T) {
now := time.Now().Unix()
tests := []struct {
name string
query Query
pubSig *CircuitOutputs
vp json.RawMessage
loader *mockJSONLDSchemaLoader
}{
{
name: "Check merklized query, exists operator",
query: Query{
AllowedIssuers: []string{"*"},
CredentialSubject: map[string]interface{}{
"countryCode": map[string]interface{}{
"$exists": true,
},
},
Context: "https://raw.githubusercontent.com/iden3/claim-schema-vocab/main/schemas/json-ld/kyc-v3.json-ld",
Type: "KYCCountryOfResidenceCredential",
},
pubSig: &CircuitOutputs{
IssuerID: &issuerID,
ClaimSchema: utils.CreateSchemaHash([]byte("https://raw.githubusercontent.com/iden3/claim-schema-vocab/main/schemas/json-ld/kyc-v3.json-ld#KYCCountryOfResidenceCredential")),
ClaimPathKey: func() *big.Int {
v, _ := big.NewInt(0).SetString("17002437119434618783545694633038537380726339994244684348913844923422470806844", 10)
return v
}(),
Operator: 11,
Value: func() []*big.Int {
v, _ := circuits.PrepareCircuitArrayValues([]*big.Int{big.NewInt(1)}, 64)
return v
}(),
Merklized: 1,
IsRevocationChecked: 1,
ValueArraySize: 1,
Timestamp: now,
},
loader: &mockJSONLDSchemaLoader{
schemas: map[string]string{
"https://raw.githubusercontent.com/iden3/claim-schema-vocab/main/schemas/json-ld/kyc-v3.json-ld": loadSchema("kyc-v3.json-ld"),
},
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := tt.query.Check(context.Background(), tt.loader, tt.pubSig, tt.vp, circuits.AtomicQueryV3CircuitID)
require.NoError(t, err)
tt.loader.assert(t)
})
}
}
func TestCheckRequest_SelectiveDisclosure_Error(t *testing.T) {
now := time.Now().Unix()
durationMin, _ := time.ParseDuration("-1m")
Expand Down
Loading