Skip to content

Commit

Permalink
Add missing credential test, use record type (#1199)
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilippGackstatter authored Jul 7, 2023
1 parent 9e435e4 commit 09aaa80
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 11 deletions.
3 changes: 3 additions & 0 deletions bindings/wasm/src/common/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ extern "C" {
#[wasm_bindgen(typescript_type = "Map<string, any>")]
pub type MapStringAny;

#[wasm_bindgen(typescript_type = "Record<string, any>")]
pub type RecordStringAny;

#[wasm_bindgen(typescript_type = "number | number[]")]
pub type UOneOrManyNumber;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use identity_iota::credential::Credential;
use identity_iota::credential::Jwt;
use wasm_bindgen::prelude::*;

use crate::common::RecordStringAny;
use crate::credential::WasmCredential;
use crate::credential::WasmJwt;

Expand Down Expand Up @@ -46,14 +47,13 @@ impl WasmUnknownCredentialContainer {

/// Returns the contained value as JSON, if it can be converted, `undefined` otherwise.
#[wasm_bindgen(js_name = tryIntoRaw)]
pub fn try_into_raw(&self) -> JsValue {
let js_value: Option<JsValue> = match &self.0 {
UnknownCredential::Jwt(jwt) => JsValue::from_serde(jwt).ok(),
UnknownCredential::Credential(credential) => JsValue::from_serde(credential).ok(),
UnknownCredential::Other(object) => JsValue::from_serde(object).ok(),
};

js_value.unwrap_or_else(JsValue::undefined)
pub fn try_into_raw(&self) -> Option<RecordStringAny> {
match &self.0 {
UnknownCredential::Other(object) => JsValue::from_serde(object)
.map(|js_val| js_val.unchecked_into::<RecordStringAny>())
.ok(),
_ => None,
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ struct IJwtPresentationHelper {
#[typescript(
optional = false,
name = "verifiableCredential",
type = "Jwt | Credential | any | Array<Jwt | Credential | any>"
type = "Jwt | Credential | Record<string, any> | Array<Jwt | Credential | Record<string, any>>"
)]
verifiable_credential: OneOrMany<UnknownCredential>,
/// The entity that generated the presentation.
Expand Down
2 changes: 1 addition & 1 deletion bindings/wasm/tests/credentials.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ describe("Presentation", function() {

assert.deepStrictEqual(credentials[0].tryIntoJwt()?.toString(), credentialJwt.toString());
assert.deepStrictEqual(credentials[1].tryIntoCredential()?.toJSON(), unsignedVc.toJSON());
assert.deepStrictEqual(credentials[2].tryIntoRaw(), otherCredential);
assert.deepStrictEqual(credentials[2].tryIntoRaw()!, otherCredential);
});
});
});
51 changes: 50 additions & 1 deletion bindings/wasm/tests/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,57 @@ describe("#JwkStorageDocument", function() {
},
},
issuer: doc.id(),
issuanceDate: "2010-01-01T00:00:00Z",
issuanceDate: Timestamp.parse("2010-01-01T00:00:00Z"),
};

const credential = new Credential(credentialFields);
// Create the JWT
const credentialJwt = await doc.createCredentialJwt(
storage,
fragment,
credential,
new JwsSignatureOptions(),
);

// Check that the credentialJwt can be decoded and verified
let credentialValidator = new JwtCredentialValidator();
const credentialRetrieved = credentialValidator
.validate(
credentialJwt,
doc,
JwtCredentialValidationOptions.default(),
FailFast.FirstError,
)
.credential();
assert.deepStrictEqual(credentialRetrieved.toJSON(), credential.toJSON());

// Also check using our custom verifier
let credentialValidatorCustom = new JwtCredentialValidator(customVerifier);
const credentialRetrievedCustom = credentialValidatorCustom
.validate(
credentialJwt,
doc,
JwtCredentialValidationOptions.default(),
FailFast.AllErrors,
)
.credential();
// Check that customVerifer.verify was indeed called
assert.deepStrictEqual(customVerifier.verifications(), 2);
assert.deepStrictEqual(
credentialRetrievedCustom.toJSON(),
credential.toJSON(),
);

// Delete the method
const methodId = (method as VerificationMethod).id();
await doc.purgeMethod(storage, methodId); // Check that the method can no longer be resolved.
assert.deepStrictEqual(doc.resolveMethod(fragment), undefined);
// The storage should now be empty
assert.deepStrictEqual(
(storage.keyIdStorage() as KeyIdMemStore).count(),
0,
);
assert.deepStrictEqual((storage.keyStorage() as JwkMemStore).count(), 0);
});

it("JwtPresentation should work", async () => {
Expand Down

0 comments on commit 09aaa80

Please sign in to comment.