Fix stronghold.ts
@@ -14,8 +14,11 @@ use identity::account_storage::Storage;
use identity::crypto::PrivateKey;
use identity::crypto::PublicKey;
use identity::iota_core::IotaDID;
+use js_sys::Array;
use js_sys::Promise;
+use js_sys::Uint8Array;
use wasm_bindgen::prelude::*;
+use wasm_bindgen::JsCast;
use wasm_bindgen_futures::JsFuture;
use crate::account::identity::WasmChainState;
@@ -94,10 +97,7 @@ impl Storage for WasmStorage {
async fn key_new(&self, did: &IotaDID, location: &KeyLocation) -> AccountStorageResult<PublicKey> {
let promise: Promise = Promise::resolve(&self.key_new(did.clone().into(), location.clone().into()));
let result: JsValueResult = JsFuture::from(promise).await.into();
- let public_key: Vec<u8> = result
- .account_err()?
- .into_serde()
- .map_err(|err| AccountStorageError::SerializationError(err.to_string()))?;
+ let public_key: Vec<u8> = result.account_err().map(uint8array_to_bytes)??;
Ok(public_key.into())
}
@@ -114,10 +114,7 @@ impl Storage for WasmStorage {
private_key.as_ref().to_vec(),
));
let result: JsValueResult = JsFuture::from(promise).await.into();
- let public_key: Vec<u8> = result
- .account_err()?
- .into_serde()
- .map_err(|err| AccountStorageError::SerializationError(err.to_string()))?;
+ let public_key: Vec<u8> = result.account_err().map(uint8array_to_bytes)??;
Ok(public_key.into())
}
@@ -125,10 +122,7 @@ impl Storage for WasmStorage {
async fn key_get(&self, did: &IotaDID, location: &KeyLocation) -> AccountStorageResult<PublicKey> {
let promise: Promise = Promise::resolve(&self.key_get(did.clone().into(), location.clone().into()));
let result: JsValueResult = JsFuture::from(promise).await.into();
- let public_key: Vec<u8> = result
- .account_err()?
- .into_serde()
- .map_err(|err| AccountStorageError::SerializationError(err.to_string()))?;
+ let public_key: Vec<u8> = result.account_err().map(uint8array_to_bytes)??;
Ok(public_key.into())
}
@@ -249,3 +243,15 @@ interface Storage {
/** Removes the keys and any state for the identity specified by `did`.*/
purge: (did: DID) => Promise<void>;
}"#;
+
+fn uint8array_to_bytes(value: JsValue) -> AccountStorageResult<Vec<u8>> {
+ if !JsCast::is_instance_of::<Uint8Array>(&value) {
+ return Err(AccountStorageError::SerializationError(
+ "expected Uint8Array".to_owned(),
+ ));
+ }
+ let array_js_value = JsValue::from(Array::from(&value));
+ array_js_value
+ .into_serde()
+ .map_err(|e| AccountStorageError::SerializationError(e.to_string()))
+}
