Skip to content

Commit

Permalink
Add a new function PLGNNewGenesisID similar to PLGNCalculateGenesisID…
Browse files Browse the repository at this point in the history
…, but that accepts configuration and registers custom DID methods.
  • Loading branch information
olomix committed Feb 21, 2024
1 parent b848380 commit 1e0d12c
Show file tree
Hide file tree
Showing 9 changed files with 329 additions and 69 deletions.
49 changes: 23 additions & 26 deletions cmd/polygonid/polygonid.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,62 +220,59 @@ func PLGNAuthV2InputsMarshal(jsonResponse **C.char, in *C.char,
func PLGNCalculateGenesisID(jsonResponse **C.char, in *C.char,
status **C.PLGNStatus) bool {

_, cancel := logAPITime()
ctx, cancel := logAPITime()
defer cancel()

var req struct {
ClaimsTreeRoot *jsonIntStr `json:"claimsTreeRoot"`
Blockchain core.Blockchain `json:"blockchain"`
Network core.NetworkID `json:"network"`
}

if in == nil {
maybeCreateStatus(status, C.PLGNSTATUSCODE_NIL_POINTER,
"pointer to request is nil")
return false
}

err := json.Unmarshal([]byte(C.GoString(in)), &req)
inStr := C.GoString(in)
resp, err := c_polygonid.NewGenesysID(ctx, c_polygonid.EnvConfig{},
[]byte(inStr))
if err != nil {
maybeCreateStatus(status, C.PLGNSTATUSCODE_ERROR, err.Error())
return false
}

state, err := merkletree.HashElems(req.ClaimsTreeRoot.Int(),
merkletree.HashZero.BigInt(), merkletree.HashZero.BigInt())
respB, err := json.Marshal(resp)
if err != nil {
maybeCreateStatus(status, C.PLGNSTATUSCODE_ERROR, err.Error())
return false
}

typ, err := core.BuildDIDType(core.DIDMethodPolygonID, req.Blockchain,
req.Network)
if err != nil {
maybeCreateStatus(status, C.PLGNSTATUSCODE_ERROR, err.Error())
*jsonResponse = C.CString(string(respB))
return true
}

//export PLGNNewGenesisID
func PLGNNewGenesisID(jsonResponse **C.char, in *C.char, cfg *C.char,
status **C.PLGNStatus) bool {

ctx, cancel := logAPITime()
defer cancel()

if in == nil {
maybeCreateStatus(status, C.PLGNSTATUSCODE_NIL_POINTER,
"pointer to request is nil")
return false
}

coreID, err := core.NewIDFromIdenState(typ, state.BigInt())
envCfg, err := createEnvConfig(cfg)
if err != nil {
maybeCreateStatus(status, C.PLGNSTATUSCODE_ERROR, err.Error())
return false
}

did, err := core.ParseDIDFromID(*coreID)
if err != nil {
inStr := C.GoString(in)
resp, err := c_polygonid.NewGenesysID(ctx, envCfg, []byte(inStr))
if in == nil {
maybeCreateStatus(status, C.PLGNSTATUSCODE_ERROR, err.Error())
return false
}

resp := struct {
DID string `json:"did"`
ID string `json:"id"`
IDAsInt string `json:"idAsInt"`
}{
DID: did.String(),
ID: coreID.String(),
IDAsInt: coreID.BigInt().String(),
}
respB, err := json.Marshal(resp)
if err != nil {
maybeCreateStatus(status, C.PLGNSTATUSCODE_ERROR, err.Error())
Expand Down
24 changes: 23 additions & 1 deletion env_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,33 @@ func NewEnvConfigFromJSON(in []byte) (EnvConfig, error) {
return cfg, fmt.Errorf("unable to parse json config: %w", err)
}

if len(cfg.ChainConfigs) == 0 {
if len(cfg.DIDMethods) == 0 {
return cfg, nil
}

err = registerDIDMethods(cfg.DIDMethods)
if err != nil {
return cfg, err
}

var zeroAddr common.Address
for _, didMethod := range cfg.DIDMethods {
chainIDCfg, ok := cfg.ChainConfigs[didMethod.ChainID]
if !ok {
return cfg, fmt.Errorf("no chain config found for chain ID %v",
didMethod.ChainID)
}
if chainIDCfg.RPCUrl == "" {
return cfg, fmt.Errorf("no RPC URL found for chain ID %v",
didMethod.ChainID)
}
if chainIDCfg.StateContractAddr == zeroAddr {
return cfg, fmt.Errorf(
"no state contract address found for chain ID %v",
didMethod.ChainID)
}
}

return cfg, err
}

Expand Down
125 changes: 83 additions & 42 deletions examples/json_functions_tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
// GoUint8 is a C bool type
typedef GoUint8(*FN)(char**, char*, PLGNStatus**);

// FN2 is a generic function that accept configuration compared to FN function
typedef GoUint8(*FN2)(char**, char*, char*, PLGNStatus**);

// if return is false, test is not passed
typedef bool(*JSProcess)(cJSON *);

Expand Down Expand Up @@ -33,50 +36,58 @@ remove_timestamp_field(cJSON *obj) {
typedef struct _TEST {
char *in;
char *out;
char *cfg;
FN fn;
FN2 fn2;
JSProcess resultPostprocessFn;
} TEST;

TEST testCases[] = {
{
.in = "testdata/create_claim_in.json",
.out = "testdata/create_claim_out.json",
.fn = &PLGNCreateClaim
.in = "testdata/create_claim_in.json",
.out = "testdata/create_claim_out.json",
.fn = &PLGNCreateClaim
},
{
.in = "testdata/create_claim_all_fields_1_in.json",
.out = "testdata/create_claim_all_fields_1_out.json",
.fn = &PLGNCreateClaim
},
{
.in = "testdata/create_claim_all_fields_1_in.json",
.out = "testdata/create_claim_all_fields_1_out.json",
.fn = &PLGNCreateClaim
.in = "testdata/create_claim_all_fields_2_in.json",
.out = "testdata/create_claim_all_fields_2_out.json",
.fn = &PLGNCreateClaim
},
{
.in = "testdata/create_claim_all_fields_2_in.json",
.out = "testdata/create_claim_all_fields_2_out.json",
.fn = &PLGNCreateClaim
.in = "testdata/auth_v2_inputs_in.json",
.out = "testdata/auth_v2_inputs_out.json",
.fn = &PLGNAuthV2InputsMarshal
},
{
.in = "testdata/auth_v2_inputs_in.json",
.out = "testdata/auth_v2_inputs_out.json",
.fn = &PLGNAuthV2InputsMarshal
.in = "testdata/calculate_genesis_id_in.json",
.out = "testdata/calculate_genesis_id_out.json",
.fn = &PLGNCalculateGenesisID
},
{
.in = "testdata/calculate_genesis_id_in.json",
.out = "testdata/calculate_genesis_id_out.json",
.fn = &PLGNCalculateGenesisID
.in = "testdata/id_to_int_in.json",
.out = "testdata/id_to_int_out.json",
.fn = &PLGNIDToInt
},
{
.in = "testdata/id_to_int_in.json",
.out = "testdata/id_to_int_out.json",
.fn = &PLGNIDToInt
.in = "testdata/proof_from_smart_contract_in.json",
.out = "testdata/proof_from_smart_contract_out.json",
.fn = &PLGNProofFromSmartContract
},
{
.in = "testdata/proof_from_smart_contract_in.json",
.out = "testdata/proof_from_smart_contract_out.json",
.fn = &PLGNProofFromSmartContract
.in = "testdata/profile_id_in.json",
.out = "testdata/profile_id_out.json",
.fn = &PLGNProfileID
},
{
.in = "testdata/profile_id_in.json",
.out = "testdata/profile_id_out.json",
.fn = &PLGNProfileID
.in = "testdata/new_genesis_id_in.json",
.cfg = "testdata/new_genesis_id_cfg.json",
.out = "testdata/new_genesis_id_out.json",
.fn2 = &PLGNNewGenesisID
}
// timestamp is different on each call, so we can't just compare output for equality
// this test is failed because ec2-34-243-185-133.eu-west-1.compute.amazonaws.com:8888 is down
Expand All @@ -90,7 +101,7 @@ TEST testCases[] = {

bool
json_equal(const char *want, const char *actual,
JSProcess resultPostprocessFn) {
JSProcess resultPostprocessFn) {
cJSON *wantJson = NULL;
cJSON *actualJson = NULL;
wantJson = cJSON_Parse(want);
Expand All @@ -115,31 +126,62 @@ json_equal(const char *want, const char *actual,

// return 0 on success or non-0 on error
int
run_test(char *in, char *out, FN fn,
JSProcess resultPostprocessFn) {
run_test(TEST tc) {
int ret_val = 0;
char *resp = NULL;
PLGNStatus *status = NULL;
char *want_output = NULL;
char *input = NULL;
char *cfg = NULL;

if (tc.fn == NULL && tc.fn2 == NULL) {
printf("functions are NULL\n");
ret_val = 1;
goto cleanup;
}

char *input = read_file(in);
if (tc.fn != NULL && tc.fn2 != NULL) {
printf("functions are not NULL\n");
ret_val = 1;
goto cleanup;
}

input = read_file(tc.in);
if (!input) {
ret_val = 1;
goto cleanup;
}
char *want_output = read_file(out);

want_output = read_file(tc.out);
if (!want_output) {
ret_val = 1;
goto cleanup;
}

bool ok = fn(&resp, input, &status);
bool ok = {0};

if (tc.fn != NULL) {

ok = tc.fn(&resp, input, &status);

} else {

cfg = read_file(tc.cfg);
if (!cfg) {
ret_val = 1;
goto cleanup;
}

ok = tc.fn2(&resp, input, cfg, &status);
}

if (!ok) {
consume_status(status, "Error marshaling input");
ret_val = 1;
goto cleanup;
}

ok = json_equal(want_output, resp, resultPostprocessFn);
ok = json_equal(want_output, resp, tc.resultPostprocessFn);
if (!ok) {
ret_val = 1;
fprintf(stderr, "result is not equal to expected output\n\ngot: %s\n\nwant: %s\n\n",
Expand All @@ -148,13 +190,13 @@ run_test(char *in, char *out, FN fn,

cleanup:
if (input) {
free(input);
free(input);
}
if (resp) {
free(resp);
free(resp);
}
if (want_output) {
free(want_output);
free(want_output);
}
return ret_val;
}
Expand All @@ -163,14 +205,13 @@ int
main() {
int ret_val = 0;
for(int i = 0; i < sizeof(testCases)/sizeof(TEST); i++) {
int r = run_test(testCases[i].in, testCases[i].out, testCases[i].fn,
testCases[i].resultPostprocessFn);
if (r != 0) {
ret_val = r;
printf("FAILED: %s => %s\n", testCases[i].in, testCases[i].out);
} else {
printf("OK: %s => %s\n", testCases[i].in, testCases[i].out);
}
int r = run_test(testCases[i]);
if (r != 0) {
ret_val = r;
printf("FAILED: %s => %s\n", testCases[i].in, testCases[i].out);
} else {
printf("OK: %s => %s\n", testCases[i].in, testCases[i].out);
}
}
return ret_val;
}
18 changes: 18 additions & 0 deletions examples/testdata/new_genesis_id_cfg.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"chainConfigs": {
"59140": {
"rpcUrl": "http://localhost:8545",
"stateContractAddr": "0xEA9aF2088B4a9770fC32A12fD42E61BDD317E655"
}
},
"didMethods": [
{
"name": "polygonid",
"blockchain": "linea",
"network": "testnet",
"networkFlag": "0b01000011",
"methodByte": "0b00000010",
"chainID": "59140"
}
]
}
5 changes: 5 additions & 0 deletions examples/testdata/new_genesis_id_in.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"claimsTreeRoot":"16306276920027997118951972513784102597349518910734830865369546877495436692483",
"blockchain":"linea",
"network":"testnet"
}
5 changes: 5 additions & 0 deletions examples/testdata/new_genesis_id_out.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"did": "did:polygonid:linea:testnet:31Akw5AB2xBrwqmbDUA2XoSGCfTepz52q9jmFE4mXA",
"id": "31Akw5AB2xBrwqmbDUA2XoSGCfTepz52q9jmFE4mXA",
"idAsInt": "24460059377712687587111979692736628604804094576108957842967948238113620738"
}
Loading

0 comments on commit 1e0d12c

Please sign in to comment.