Skip to content

Commit

Permalink
for operator set true/false values as 1/0, not hashes of 0 and 1 (#68)
Browse files Browse the repository at this point in the history
* for  operator set true/false values as 1/0, not hashes of 0 and 1

* fix TestUnpackOperatorWithArgs test
  • Loading branch information
olomix authored Mar 22, 2024
1 parent f02c65c commit b12e43a
Show file tree
Hide file tree
Showing 18 changed files with 390 additions and 16 deletions.
30 changes: 17 additions & 13 deletions inputs_sig.go
Original file line number Diff line number Diff line change
Expand Up @@ -1445,14 +1445,27 @@ func queryFromObjMerklized(ctx context.Context,
return out, verifiablePresentation, nil
}

var opDatatypeRedefine = map[int]string{
circuits.EXISTS: ld.XSDBoolean,
}

// Return int operator value by its name and arguments as big.Ints array.
func unpackOperatorWithArgs(opStr string, opValue any,
datatype string, hasher merklize.Hasher) (int, []*big.Int, error) {

op, ok := circuits.QueryOperators[opStr]
if !ok {
return 0, nil, errors.New("unknown operator")
}

if op == circuits.EXISTS {
var existsVal bool
existsVal, ok = opValue.(bool)
if !ok {
return 0, nil, errors.New("$exists operator value is not a boolean")
}
if existsVal {
return op, []*big.Int{big.NewInt(1)}, nil
}
return op, []*big.Int{big.NewInt(0)}, nil
}

hashFn := func(val any) (*big.Int, error) {
if hasher == nil {
return merklize.HashValue(datatype, val)
Expand All @@ -1461,15 +1474,6 @@ func unpackOperatorWithArgs(opStr string, opValue any,
}
}

op, ok := circuits.QueryOperators[opStr]
if !ok {
return 0, nil, errors.New("unknown operator")
}

if newDT, ok := opDatatypeRedefine[op]; ok {
datatype = newDT
}

var err error
valArr, isArr := opValue.([]any)
if isArr {
Expand Down
96 changes: 93 additions & 3 deletions inputs_sig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,88 @@ func TestPrepareInputs(t *testing.T) {
AtomicQueryV3OnChainInputsFromJson, nil, EnvConfig{}, "")
})

t.Run("AtomicQueryV3InputsFromJson__Sig_exists_true", func(t *testing.T) {
ipfsURL := os.Getenv("IPFS_URL")
if ipfsURL == "" {
t.Skip("IPFS_URL is not set")
}

defer preserveIPFSHttpCli()()

cid := uploadIPFSFile(t, ipfsURL, "testdata/ipfs_QmcvoKLc742CyVH2Cnw6X95b4c8VdABqNPvTyAHEeaK1aP.json")
require.Equal(t, "QmcvoKLc742CyVH2Cnw6X95b4c8VdABqNPvTyAHEeaK1aP", cid)

defer httpmock.MockHTTPClient(t, map[string]string{
`http://127.0.0.1:8545%%%{"jsonrpc":"2.0","id":1,"method":"eth_call","params":[{"from":"0x0000000000000000000000000000000000000000","input":"0xb4bdea55000e02195fa99cf8975171e88a411bff99da7548cd4576ba2d102cf77ec31202","to":"0x134b1be34911e39a8397ec6289782989729807a4"},"latest"]}`: "testdata/httpresp_eth_resp3.json",
"https://rhs-staging.polygonid.me/node/34f9500218a054d58347b848dae17d07602b9320143c79e2786ff3aa97254f29": "testdata/httpresp_rhs_34f9500218a054d58347b848dae17d07602b9320143c79e2786ff3aa97254f29.json",
"https://rhs-staging.polygonid.me/node/d469d1307ba23faae8eef13d90031c981e4d2c36977b0422e669e5ef7b891205": "testdata/httpresp_rhs_d469d1307ba23faae8eef13d90031c981e4d2c36977b0422e669e5ef7b891205.json",
"https://rhs-staging.polygonid.me/node/20b66d667fe760963e023546deaec3550e51cf371c5b03e9eb751b7455601225": "testdata/httpresp_rhs_20b66d667fe760963e023546deaec3550e51cf371c5b03e9eb751b7455601225.json",
"https://rhs-staging.polygonid.me/node/81e7bed726f2bd88390e9537975e88835094176ad1d350b716d9d3eaaa4da128": "testdata/httpresp_rhs_81e7bed726f2bd88390e9537975e88835094176ad1d350b716d9d3eaaa4da128.json",
"https://rhs-staging.polygonid.me/node/0aab94d5a794dbb99bcd1ab80e91fef9afb2f9d667244b04ab6820c77dabeb23": "testdata/httpresp_rhs_0aab94d5a794dbb99bcd1ab80e91fef9afb2f9d667244b04ab6820c77dabeb23.json",
"https://rhs-staging.polygonid.me/node/003177fe2a5c4a9356894d6abb4f0a6da2fb19095e47d2447127edb8f7d01729": "testdata/httpresp_rhs_003177fe2a5c4a9356894d6abb4f0a6da2fb19095e47d2447127edb8f7d01729.json",
"https://rhs-staging.polygonid.me/node/adb8edf5bffb7168171e33696399d8766a03802624225ce20005cf61753d0611": "testdata/httpresp_rhs_adb8edf5bffb7168171e33696399d8766a03802624225ce20005cf61753d0611.json",
"https://rhs-staging.polygonid.me/node/c5b8691380634bd9c0f928da490f684579a2b51de1ee52028b74d83f461d0208": "testdata/httpresp_rhs_c5b8691380634bd9c0f928da490f684579a2b51de1ee52028b74d83f461d0208.json",
"https://rhs-staging.polygonid.me/node/1aefa6dd321a42685112bf762d7dc17a6fb51e5521a819f6d5c8a09681932913": "testdata/httpresp_rhs_1aefa6dd321a42685112bf762d7dc17a6fb51e5521a819f6d5c8a09681932913.json",
"https://rhs-staging.polygonid.me/node/d2e4c97c4fc3e83521a8b7910765ac62a7171f9120be3c3b854d48cd510e6f0a": "testdata/httpresp_rhs_d2e4c97c4fc3e83521a8b7910765ac62a7171f9120be3c3b854d48cd510e6f0a.json",
}, httpmock.IgnoreUntouchedURLs())()

cfg := EnvConfig{
IPFSNodeURL: ipfsURL,
ChainConfigs: map[core.ChainID]ChainConfig{
80001: {
RPCUrl: "http://127.0.0.1:8545",
StateContractAddr: common.HexToAddress(
"0x134B1BE34911E39A8397ec6289782989729807a4"),
},
},
}

doTest(t, "atomic_query_v3_sig_exists_true_inputs.json",
"atomic_query_v3_sig_exists_true_output.json",
AtomicQueryV3InputsFromJson, nil, cfg, "")
})

t.Run("AtomicQueryV3InputsFromJson__Sig_exists_false", func(t *testing.T) {
ipfsURL := os.Getenv("IPFS_URL")
if ipfsURL == "" {
t.Skip("IPFS_URL is not set")
}

defer preserveIPFSHttpCli()()

cid := uploadIPFSFile(t, ipfsURL, "testdata/ipfs_QmcvoKLc742CyVH2Cnw6X95b4c8VdABqNPvTyAHEeaK1aP.json")
require.Equal(t, "QmcvoKLc742CyVH2Cnw6X95b4c8VdABqNPvTyAHEeaK1aP", cid)

defer httpmock.MockHTTPClient(t, map[string]string{
`http://127.0.0.1:8545%%%{"jsonrpc":"2.0","id":1,"method":"eth_call","params":[{"from":"0x0000000000000000000000000000000000000000","input":"0xb4bdea55000e02195fa99cf8975171e88a411bff99da7548cd4576ba2d102cf77ec31202","to":"0x134b1be34911e39a8397ec6289782989729807a4"},"latest"]}`: "testdata/httpresp_eth_resp3.json",
"https://rhs-staging.polygonid.me/node/34f9500218a054d58347b848dae17d07602b9320143c79e2786ff3aa97254f29": "testdata/httpresp_rhs_34f9500218a054d58347b848dae17d07602b9320143c79e2786ff3aa97254f29.json",
"https://rhs-staging.polygonid.me/node/d469d1307ba23faae8eef13d90031c981e4d2c36977b0422e669e5ef7b891205": "testdata/httpresp_rhs_d469d1307ba23faae8eef13d90031c981e4d2c36977b0422e669e5ef7b891205.json",
"https://rhs-staging.polygonid.me/node/20b66d667fe760963e023546deaec3550e51cf371c5b03e9eb751b7455601225": "testdata/httpresp_rhs_20b66d667fe760963e023546deaec3550e51cf371c5b03e9eb751b7455601225.json",
"https://rhs-staging.polygonid.me/node/81e7bed726f2bd88390e9537975e88835094176ad1d350b716d9d3eaaa4da128": "testdata/httpresp_rhs_81e7bed726f2bd88390e9537975e88835094176ad1d350b716d9d3eaaa4da128.json",
"https://rhs-staging.polygonid.me/node/0aab94d5a794dbb99bcd1ab80e91fef9afb2f9d667244b04ab6820c77dabeb23": "testdata/httpresp_rhs_0aab94d5a794dbb99bcd1ab80e91fef9afb2f9d667244b04ab6820c77dabeb23.json",
"https://rhs-staging.polygonid.me/node/003177fe2a5c4a9356894d6abb4f0a6da2fb19095e47d2447127edb8f7d01729": "testdata/httpresp_rhs_003177fe2a5c4a9356894d6abb4f0a6da2fb19095e47d2447127edb8f7d01729.json",
"https://rhs-staging.polygonid.me/node/adb8edf5bffb7168171e33696399d8766a03802624225ce20005cf61753d0611": "testdata/httpresp_rhs_adb8edf5bffb7168171e33696399d8766a03802624225ce20005cf61753d0611.json",
"https://rhs-staging.polygonid.me/node/c5b8691380634bd9c0f928da490f684579a2b51de1ee52028b74d83f461d0208": "testdata/httpresp_rhs_c5b8691380634bd9c0f928da490f684579a2b51de1ee52028b74d83f461d0208.json",
"https://rhs-staging.polygonid.me/node/1aefa6dd321a42685112bf762d7dc17a6fb51e5521a819f6d5c8a09681932913": "testdata/httpresp_rhs_1aefa6dd321a42685112bf762d7dc17a6fb51e5521a819f6d5c8a09681932913.json",
"https://rhs-staging.polygonid.me/node/d2e4c97c4fc3e83521a8b7910765ac62a7171f9120be3c3b854d48cd510e6f0a": "testdata/httpresp_rhs_d2e4c97c4fc3e83521a8b7910765ac62a7171f9120be3c3b854d48cd510e6f0a.json",
}, httpmock.IgnoreUntouchedURLs())()

cfg := EnvConfig{
IPFSNodeURL: ipfsURL,
ChainConfigs: map[core.ChainID]ChainConfig{
80001: {
RPCUrl: "http://127.0.0.1:8545",
StateContractAddr: common.HexToAddress(
"0x134B1BE34911E39A8397ec6289782989729807a4"),
},
},
}

doTest(t, "atomic_query_v3_sig_exists_false_inputs.json",
"atomic_query_v3_sig_exists_false_output.json",
AtomicQueryV3InputsFromJson, nil, cfg, "")
})

}

func TestEnvConfig_UnmarshalJSON(t *testing.T) {
Expand Down Expand Up @@ -1076,9 +1158,17 @@ func TestUnpackOperatorWithArgs(t *testing.T) {
merklize.PoseidonHasher{})
require.NoError(t, err)
require.Equal(t, circuits.EXISTS, op)
require.Equal(t,
[]*big.Int{bi("18586133768512220936620570745912940619677854269274689475585506675881198879027")},
vals)
require.Equal(t, []*big.Int{bi("1")}, vals)

op, vals, err = unpackOperatorWithArgs("$exists", false, ld.XSDString,
merklize.PoseidonHasher{})
require.NoError(t, err)
require.Equal(t, circuits.EXISTS, op)
require.Equal(t, []*big.Int{bi("0")}, vals)

_, _, err = unpackOperatorWithArgs("$exists", "true", ld.XSDString,
merklize.PoseidonHasher{})
require.EqualError(t, err, "$exists operator value is not a boolean")
}

func TestDescribeID(t *testing.T) {
Expand Down
133 changes: 133 additions & 0 deletions testdata/atomic_query_v3_sig_exists_false_inputs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
{
"id": "2qFxHT9MSaJCtebmGKumTwy8DwuVtuVc3uiUpd55TS",
"profileNonce": "309551087329242797528867494378504098668188146678372651733290522425766376653",
"claimSubjectProfileNonce": "0",
"authClaim": [
"80551937543569765027552589160822318028",
"0",
"4171449880171922179953000706311240232125830426242846899721508824209851687017",
"14500742764723028776959908343362583962908547001323698888484012726294687416457",
"15930428023331155902",
"0",
"0",
"0"
],
"authClaimIncMtp": {
"existence": true,
"siblings": []
},
"authClaimNonRevMtp": {
"existence": false,
"siblings": []
},
"gistProof": {
"root": "12418611251737804295569422141525196583413539330327786072828082443303703831517",
"proof": {
"existence": false,
"siblings": [
"18322679938522535919439058522861035997603473627212030853556452893422350344203",
"11942982736734986851948986145140231737283217100524366500004170081809651461080",
"1841452540740171976664611240096314351730239924082525759975865533347106478455",
"21738338219567067283455727220430856103415382749223907128451252484348654064122",
"10248304658887589221414557525469308733702985288736333443429191477904243586231",
"16046686316613501716253972055459742344006727092099468833860978790162690293004",
"5885878964566214101846384796010217965011065654213067948135048732708021904037",
"15972022201407398657085806487990617724002507189259867505657830331767772246932",
"3313684918497731127871183729910051452832536838193931185491439071851740835083",
"8149689409002490831929693042282878874821042810679325231276761719174724844796",
"20622611059900858481715476496392728807539084000131724572061936302437113126066",
"6061141835026483142159727065797976934980399438249727448251635090707495787423"
]
}
},
"treeState": {
"state": "6860434661714791564964708878597544927702028603686661004182431934504324838115",
"claimsRoot": "13042327161192797453699947787027610043694323930159176304323111433681179984740",
"revocationRoot": "0",
"rootOfRoots": "0"
},
"verifiableCredentials": {
"id": "https://issuer-admin.polygonid.me/v1/credentials/1e1b6f1f-e852-11ee-9451-0242ac120007",
"@context": [
"https://www.w3.org/2018/credentials/v1",
"https://schema.iden3.io/core/jsonld/iden3proofs.jsonld",
"ipfs://QmcvoKLc742CyVH2Cnw6X95b4c8VdABqNPvTyAHEeaK1aP"
],
"type": [
"VerifiableCredential",
"types123"
],
"issuanceDate": "2024-03-22T13:43:08.514607928Z",
"credentialSubject": {
"id": "did:polygonid:polygon:mumbai:2qFxHT9MSaJCtebmGKumTwy8DwuVtuVc3uiUpd55TS",
"type": "types123",
"bol": true,
"double": 23.32,
"dt": "2024-03-16T15:42:38.640+02:00",
"string": "nice"
},
"credentialStatus": {
"id": "https://rhs-staging.polygonid.me/node?state=34f9500218a054d58347b848dae17d07602b9320143c79e2786ff3aa97254f29",
"revocationNonce": 625851252,
"type": "Iden3ReverseSparseMerkleTreeProof",
"statusIssuer": {
"id": "https://issuer-admin.polygonid.me/v1/credentials/revocation/status/625851252",
"revocationNonce": 625851252,
"type": "SparseMerkleTreeProof"
}
},
"issuer": "did:polygonid:polygon:mumbai:2qMLpQ5py1YzBTTuLEeX2yr6pDGQ7gyXAfygaPakzq",
"credentialSchema": {
"id": "ipfs://QmTRpn65HN3j6Y5ZC5WDU1orXnWPWMpoPr2qpep8eMCX6e",
"type": "JsonSchema2023"
},
"proof": [
{
"type": "BJJSignature2021",
"issuerData": {
"id": "did:polygonid:polygon:mumbai:2qMLpQ5py1YzBTTuLEeX2yr6pDGQ7gyXAfygaPakzq",
"state": {
"claimsTreeRoot": "68b46e3a40e2decd05fd3632885f3a754972d8ded5ef13e0f7afb36c35457817",
"value": "876586f318c37ef72c102dba7645cd4875da99ff1b418ae8715197f89ca95f19"
},
"authCoreClaim": "cca3371a6cb1b715004407e325bd993c0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005c49836bc7a21a1b444df0427abde89ece4905cdf451701f8802d0e0e551e723d5a39e30958a7e0221cfa67fcb5606499f44658f9bdd1400402f95bbc35339170000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"mtp": {
"existence": true,
"siblings": []
},
"credentialStatus": {
"id": "https://rhs-staging.polygonid.me/node?state=876586f318c37ef72c102dba7645cd4875da99ff1b418ae8715197f89ca95f19",
"revocationNonce": 0,
"type": "Iden3ReverseSparseMerkleTreeProof",
"statusIssuer": {
"id": "https://self-hosted-platform.polygonid.me/v1/did%3Apolygonid%3Apolygon%3Amumbai%3A2qMLpQ5py1YzBTTuLEeX2yr6pDGQ7gyXAfygaPakzq/claims/revocation/status/0",
"revocationNonce": 0,
"type": "SparseMerkleTreeProof",
"statusIssuer": null
}
}
},
"coreClaim": "8943e7ca773792800881fcd3054e13792200000000000000000000000000000002124ccc6ad45a53f2bf18c57a1d8dfe733612d65a22e8ab6f50de2a0f3d0d00aaeb2bab00d1ec73fb8ecde28f29d5bdda8265c8dee7578b91683777c9b37917000000000000000000000000000000000000000000000000000000000000000074bb4d2500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"signature": "5d803f48fab9446a92be0596141855637255ea7162e780a84698f2a066193b96aff0231f7468c01bddfd55c4b55c806e0c47b6602d7f36cf744071c115a13103"
}
]
},
"request": {
"id": 1711115116,
"circuitId": "credentialAtomicQueryV3-beta.1",
"query": {
"allowedIssuers": [
"*"
],
"context": "ipfs://QmcvoKLc742CyVH2Cnw6X95b4c8VdABqNPvTyAHEeaK1aP",
"type": "types123",
"credentialSubject": {
"bol": {
"$exists": false
}
}
}
},
"verifierId": "did:polygonid:polygon:mumbai:2qH7TstpRRJHXNN4o49Fu9H2Qismku8hQeUxDVrjqT",
"linkNonce": "0"
}
Loading

0 comments on commit b12e43a

Please sign in to comment.