Skip to content

Commit

Permalink
fix(js): update react-native bindings for hyperledger#226
Browse files Browse the repository at this point in the history
Signed-off-by: Ariel Gentile <[email protected]>
  • Loading branch information
genaris committed Aug 12, 2023
1 parent 75bffca commit 25bf545
Show file tree
Hide file tree
Showing 9 changed files with 100 additions and 41 deletions.
22 changes: 16 additions & 6 deletions include/README.md
Original file line number Diff line number Diff line change
@@ -1,22 +1,32 @@
_Generating the C header:_

1. Install [cbindgen](https://github.com/eqrion/cbindgen/)
1. Install [cargo expand](https://github.com/dtolnay/cargo-expand)
1. use `nightly` and not `stable
- `rustup default nightly`

1. use `nightly` instead of `stable`

```sh
rustup default nightly
```
> **Note**: If you run into _'unknown feature'_ issues by using latest nightly, force it to 1.72.0 by executing: `rustup default nightly-2023-06-15`
2. Install [cbindgen](https://github.com/eqrion/cbindgen/)

```sh
cargo install cbindgen
```

Generate the header file:
3. Install [cargo expand](https://github.com/dtolnay/cargo-expand)

```sh
cargo install cargo-expand
```

4. Generate the header file:

```sh
cbindgen --config include/cbindgen.toml --crate anoncreds --output include/libanoncreds.h
```

Copy to React Native:
5. Copy to React Native:

```sh
cp include/libanoncreds.h wrappers/javascript/anoncreds-react-native/cpp/include/
Expand Down
20 changes: 12 additions & 8 deletions include/libanoncreds.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

/* Generated with cbindgen:0.24.3 */
/* Generated with cbindgen:0.24.5 */

/* Warning, this file is autogenerated by cbindgen. Don't modify this manually. */

Expand Down Expand Up @@ -317,19 +317,21 @@ ErrorCode anoncreds_create_presentation(ObjectHandle pres_req,

ErrorCode anoncreds_create_revocation_registry_def(ObjectHandle cred_def,
FfiStr cred_def_id,
FfiStr issuer_id,
FfiStr _issuer_id,
FfiStr tag,
FfiStr rev_reg_type,
int64_t max_cred_num,
FfiStr tails_dir_path,
ObjectHandle *reg_def_p,
ObjectHandle *reg_def_private_p);

ErrorCode anoncreds_create_revocation_status_list(FfiStr rev_reg_def_id,
ErrorCode anoncreds_create_revocation_status_list(ObjectHandle cred_def,
FfiStr rev_reg_def_id,
ObjectHandle rev_reg_def,
FfiStr issuer_id,
int64_t timestamp,
ObjectHandle reg_rev_priv,
FfiStr _issuer_id,
int8_t issuance_by_default,
int64_t timestamp,
ObjectHandle *rev_status_list_p);

ErrorCode anoncreds_create_schema(FfiStr schema_name,
Expand Down Expand Up @@ -415,11 +417,13 @@ ErrorCode anoncreds_set_default_logger(void);
*/
void anoncreds_string_free(char *s);

ErrorCode anoncreds_update_revocation_status_list(int64_t timestamp,
struct FfiList_i32 issued,
struct FfiList_i32 revoked,
ErrorCode anoncreds_update_revocation_status_list(ObjectHandle cred_def,
ObjectHandle rev_reg_def,
ObjectHandle rev_reg_priv,
ObjectHandle rev_current_list,
struct FfiList_i32 issued,
struct FfiList_i32 revoked,
int64_t timestamp,
ObjectHandle *new_rev_status_list_p);

ErrorCode anoncreds_update_revocation_status_list_timestamp_only(int64_t timestamp,
Expand Down
8 changes: 6 additions & 2 deletions wrappers/javascript/anoncreds-nodejs/test/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -390,8 +390,6 @@ test('create and verify presentation passing only JSON objects as parameters', (
maximumCredentialNumber: 10,
})

const tailsPath = revocationRegistryDefinition.getTailsLocation()

const timeCreateRevStatusList = 12
const revocationStatusList = RevocationStatusList.create({
credentialDefinition,
Expand All @@ -403,6 +401,12 @@ test('create and verify presentation passing only JSON objects as parameters', (
timestamp: timeCreateRevStatusList,
})

revocationStatusList.update({
credentialDefinition,
revocationRegistryDefinition,
revocationRegistryDefinitionPrivate,
revoked: [1],
})
const credentialOffer = CredentialOffer.fromJson({
schema_id: 'mock:uri',
cred_def_id: 'mock:uri',
Expand Down
24 changes: 17 additions & 7 deletions wrappers/javascript/anoncreds-react-native/cpp/anoncreds.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -592,37 +592,47 @@ jsi::Value createOrUpdateRevocationState(jsi::Runtime &rt,
};

jsi::Value createRevocationStatusList(jsi::Runtime &rt, jsi::Object options) {
auto credentialDefinition =
jsiToValue<ObjectHandle>(rt, options, "credentialDefinition");
auto revocationRegistryDefinitionId =
jsiToValue<std::string>(rt, options, "revocationRegistryDefinitionId");
auto issuerId = jsiToValue<std::string>(rt, options, "issuerId");
auto revocationRegistryDefinition =
jsiToValue<ObjectHandle>(rt, options, "revocationRegistryDefinition");
auto revocationRegistryDefinitionPrivate =
jsiToValue<ObjectHandle>(rt, options, "revocationRegistryDefinitionPrivate");
auto issuerId = jsiToValue<std::string>(rt, options, "issuerId");
auto timestamp = jsiToValue<int64_t>(rt, options, "timestamp");
auto issuanceByDefault = jsiToValue<int8_t>(rt, options, "issuanceByDefault");

ObjectHandle out;

ErrorCode code = anoncreds_create_revocation_status_list(
revocationRegistryDefinitionId.c_str(), revocationRegistryDefinition,
credentialDefinition, revocationRegistryDefinitionId.c_str(),
revocationRegistryDefinition, revocationRegistryDefinitionPrivate,
issuerId.c_str(), timestamp, issuanceByDefault, &out);

return createReturnValue(rt, code, &out);
}

jsi::Value updateRevocationStatusList(jsi::Runtime &rt, jsi::Object options) {
auto timestamp = jsiToValue<int64_t>(rt, options, "timestamp");
auto issued = jsiToValue<FfiList_i32>(rt, options, "issued");
auto revoked = jsiToValue<FfiList_i32>(rt, options, "revoked");
auto credentialDefinition =
jsiToValue<ObjectHandle>(rt, options, "credentialDefinition");
auto revocationRegistryDefinition =
jsiToValue<ObjectHandle>(rt, options, "revocationRegistryDefinition");
auto revocationRegistryDefinitionPrivate =
jsiToValue<ObjectHandle>(rt, options, "revocationRegistryDefinitionPrivate");
auto revocationStatusList =
jsiToValue<ObjectHandle>(rt, options, "revocationStatusList");
auto issued = jsiToValue<FfiList_i32>(rt, options, "issued");
auto revoked = jsiToValue<FfiList_i32>(rt, options, "revoked");
auto timestamp = jsiToValue<int64_t>(rt, options, "timestamp");

ObjectHandle out;

ErrorCode code = anoncreds_update_revocation_status_list(
timestamp, issued, revoked, revocationRegistryDefinition,
revocationStatusList, &out);
credentialDefinition, revocationRegistryDefinition,
revocationRegistryDefinitionPrivate, revocationStatusList,
issued, revoked, timestamp, &out);

return createReturnValue(rt, code, &out);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

/* Generated with cbindgen:0.24.3 */
/* Generated with cbindgen:0.24.5 */

/* Warning, this file is autogenerated by cbindgen. Don't modify this manually. */

Expand Down Expand Up @@ -317,19 +317,21 @@ ErrorCode anoncreds_create_presentation(ObjectHandle pres_req,

ErrorCode anoncreds_create_revocation_registry_def(ObjectHandle cred_def,
FfiStr cred_def_id,
FfiStr issuer_id,
FfiStr _issuer_id,
FfiStr tag,
FfiStr rev_reg_type,
int64_t max_cred_num,
FfiStr tails_dir_path,
ObjectHandle *reg_def_p,
ObjectHandle *reg_def_private_p);

ErrorCode anoncreds_create_revocation_status_list(FfiStr rev_reg_def_id,
ErrorCode anoncreds_create_revocation_status_list(ObjectHandle cred_def,
FfiStr rev_reg_def_id,
ObjectHandle rev_reg_def,
FfiStr issuer_id,
int64_t timestamp,
ObjectHandle reg_rev_priv,
FfiStr _issuer_id,
int8_t issuance_by_default,
int64_t timestamp,
ObjectHandle *rev_status_list_p);

ErrorCode anoncreds_create_schema(FfiStr schema_name,
Expand Down Expand Up @@ -415,11 +417,13 @@ ErrorCode anoncreds_set_default_logger(void);
*/
void anoncreds_string_free(char *s);

ErrorCode anoncreds_update_revocation_status_list(int64_t timestamp,
struct FfiList_i32 issued,
struct FfiList_i32 revoked,
ErrorCode anoncreds_update_revocation_status_list(ObjectHandle cred_def,
ObjectHandle rev_reg_def,
ObjectHandle rev_reg_priv,
ObjectHandle rev_current_list,
struct FfiList_i32 issued,
struct FfiList_i32 revoked,
int64_t timestamp,
ObjectHandle *new_rev_status_list_p);

ErrorCode anoncreds_update_revocation_status_list_timestamp_only(int64_t timestamp,
Expand Down
10 changes: 7 additions & 3 deletions wrappers/javascript/anoncreds-react-native/src/NativeBindings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,23 @@ export interface NativeBindings {
}): ReturnObject<Handle>

createRevocationStatusList(options: {
credentialDefinition: Handle
revocationRegistryDefinitionId: string
revocationRegistryDefinition: Handle
revocationRegistryDefinitionPrivate: Handle
issuerId: string
timestamp?: number
issuanceByDefault: number
}): ReturnObject<Handle>

updateRevocationStatusList(options: {
timestamp?: number
issued?: number[]
revoked?: number[]
credentialDefinition: Handle
revocationRegistryDefinition: Handle
revocationRegistryDefinitionPrivate: Handle
currentRevocationStatusList: Handle
issued?: number[]
revoked?: number[]
timestamp?: number
}): ReturnObject<Handle>

updateRevocationStatusListTimestampOnly(options: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@ export class ReactNativeAnoncreds implements Anoncreds {
}

public createRevocationStatusList(options: {
credentialDefinition: ObjectHandle
revocationRegistryDefinitionId: string
revocationRegistryDefinition: ObjectHandle
revocationRegistryDefinitionPrivate: ObjectHandle
issuerId: string
timestamp?: number
issuanceByDefault: boolean
Expand All @@ -50,11 +52,13 @@ export class ReactNativeAnoncreds implements Anoncreds {
}

public updateRevocationStatusList(options: {
timestamp?: number
issued?: number[]
revoked?: number[]
credentialDefinition: ObjectHandle
revocationRegistryDefinition: ObjectHandle
revocationRegistryDefinitionPrivate: ObjectHandle
currentRevocationStatusList: ObjectHandle
issued?: number[]
revoked?: number[]
timestamp?: number
}): ObjectHandle {
const handle = this.handleError(this.anoncreds.updateRevocationStatusList(serializeArguments(options)))
return new ObjectHandle(handle)
Expand Down
8 changes: 5 additions & 3 deletions wrappers/javascript/anoncreds-shared/src/Anoncreds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,11 +157,13 @@ export interface Anoncreds {
}): ObjectHandle

updateRevocationStatusList(options: {
timestamp?: number
issued?: Array<number>
revoked?: Array<number>
credentialDefinition: ObjectHandle
revocationRegistryDefinition: ObjectHandle
revocationRegistryDefinitionPrivate: ObjectHandle
currentRevocationStatusList: ObjectHandle
issued?: Array<number>
revoked?: Array<number>
timestamp?: number
}): ObjectHandle

credentialGetAttribute(options: { objectHandle: ObjectHandle; name: string }): string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@ export type UpdateRevocationStatusListTimestampOptions = {
}

export type UpdateRevocationStatusListOptions = {
credentialDefinition: CredentialDefinition | JsonObject
revocationRegistryDefinition: RevocationRegistryDefinition | JsonObject
timestamp?: number
revocationRegistryDefinitionPrivate: RevocationRegistryDefinitionPrivate | JsonObject
issued?: Array<number>
revoked?: Array<number>
timestamp?: number
}

export class RevocationStatusList extends AnoncredsObject {
Expand Down Expand Up @@ -84,6 +86,11 @@ export class RevocationStatusList extends AnoncredsObject {
public update(options: UpdateRevocationStatusListOptions) {
const objectHandles: ObjectHandle[] = []
try {
const credentialDefinition =
options.credentialDefinition instanceof CredentialDefinition
? options.credentialDefinition.handle
: pushToArray(CredentialDefinition.fromJson(options.credentialDefinition).handle, objectHandles)

const revocationRegistryDefinition =
options.revocationRegistryDefinition instanceof RevocationRegistryDefinition
? options.revocationRegistryDefinition.handle
Expand All @@ -92,9 +99,19 @@ export class RevocationStatusList extends AnoncredsObject {
objectHandles
)

const revocationRegistryDefinitionPrivate =
options.revocationRegistryDefinitionPrivate instanceof RevocationRegistryDefinitionPrivate
? options.revocationRegistryDefinitionPrivate.handle
: pushToArray(
RevocationRegistryDefinitionPrivate.fromJson(options.revocationRegistryDefinitionPrivate).handle,
objectHandles
)

const updatedRevocationStatusList = anoncreds.updateRevocationStatusList({
...options,
credentialDefinition,
revocationRegistryDefinition,
revocationRegistryDefinitionPrivate,
currentRevocationStatusList: this.handle,
})

Expand Down

0 comments on commit 25bf545

Please sign in to comment.