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 blake2b to wasm bindings #288

Merged
merged 2 commits into from Jun 21, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions bindings/wasm/docs/api-reference.md
Expand Up @@ -148,7 +148,7 @@
* [.transactionURL(message_id)](#Client+transactionURL) ⇒ <code>string</code>
* [.publishDocument(document)](#Client+publishDocument) ⇒ <code>Promise.&lt;any&gt;</code>
* [.publishDiff(message_id, value)](#Client+publishDiff) ⇒ <code>Promise.&lt;any&gt;</code>
* [.readDocument(did)](#Client+readDocument) ⇒ <code>Promise.&lt;any&gt;</code>
* [.resolve(did)](#Client+resolve) ⇒ <code>Promise.&lt;any&gt;</code>
* [.checkCredential(data)](#Client+checkCredential) ⇒ <code>Promise.&lt;any&gt;</code>
* [.checkPresentation(data)](#Client+checkPresentation) ⇒ <code>Promise.&lt;any&gt;</code>
* _static_
Expand Down Expand Up @@ -212,9 +212,9 @@ Publishes a `DocumentDiff` to the Tangle.
| message_id | <code>string</code> |
| value | <code>any</code> |

<a name="Client+readDocument"></a>
<a name="Client+resolve"></a>

### client.readDocument(did) ⇒ <code>Promise.&lt;any&gt;</code>
### client.resolve(did) ⇒ <code>Promise.&lt;any&gt;</code>
**Kind**: instance method of [<code>Client</code>](#Client)

| Param | Type |
Expand Down
1 change: 1 addition & 0 deletions bindings/wasm/src/crypto/digest.rs
Expand Up @@ -7,6 +7,7 @@ use wasm_bindgen::prelude::*;
#[derive(Clone, Copy, Debug)]
pub enum Digest {
Sha256 = 1,
Blake2b256 = 2,
}

impl Default for Digest {
Expand Down
10 changes: 10 additions & 0 deletions bindings/wasm/src/crypto/key_collection.rs
Expand Up @@ -3,6 +3,7 @@

use identity::core::decode_b58;
use identity::core::encode_b58;
use identity::crypto::merkle_key::Blake2b256;
use identity::crypto::merkle_key::Sha256;
use identity::crypto::merkle_tree::Proof;
use identity::crypto::KeyCollection as KeyCollection_;
Expand Down Expand Up @@ -77,6 +78,7 @@ impl KeyCollection {
pub fn merkle_root(&self, digest: Digest) -> String {
match digest {
Digest::Sha256 => encode_b58(self.0.merkle_root::<Sha256>().as_slice()),
Digest::Blake2b256 => encode_b58(self.0.merkle_root::<Blake2b256>().as_slice()),
}
}

Expand All @@ -89,6 +91,14 @@ impl KeyCollection {
None => return None,
};

Some(encode_b58(&proof.encode()))
}
Digest::Blake2b256 => {
let proof: Proof<Blake2b256> = match self.0.merkle_proof(index) {
Some(proof) => proof,
None => return None,
};

Some(encode_b58(&proof.encode()))
}
}
Expand Down
10 changes: 9 additions & 1 deletion bindings/wasm/src/wasm_verification_method.rs
@@ -1,7 +1,9 @@
// Copyright 2020-2021 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

use identity::crypto::merkle_key::Blake2b256;
use identity::crypto::merkle_key::Sha256;
use identity::iota::IotaDID;
use identity::iota::IotaVerificationMethod;
use wasm_bindgen::prelude::*;

Expand Down Expand Up @@ -41,8 +43,14 @@ impl WasmVerificationMethod {
keys: &KeyCollection,
tag: Option<String>,
) -> Result<WasmVerificationMethod, JsValue> {
let did: IotaDID = did.0.clone();
let tag: Option<&str> = tag.as_deref();

match digest {
Digest::Sha256 => IotaVerificationMethod::create_merkle_key::<Sha256, _>(did.0.clone(), &keys.0, tag.as_deref())
Digest::Sha256 => IotaVerificationMethod::create_merkle_key::<Sha256, _>(did, &keys.0, tag)
.map_err(err)
.map(Self),
Digest::Blake2b256 => IotaVerificationMethod::create_merkle_key::<Blake2b256, _>(did, &keys.0, tag)
.map_err(err)
.map(Self),
}
Expand Down