Skip to content

Commit

Permalink
Fix build warnings and fix MoveType implementation for IotaVerifiable…
Browse files Browse the repository at this point in the history
…Credential

All tests running successfully for the first time now
  • Loading branch information
chrisgitiota committed Jan 7, 2025
1 parent f348ff7 commit c1e4cbf
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 49 deletions.
24 changes: 4 additions & 20 deletions identity_iota_core/src/rebased/assets/public_available_vc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// SPDX-License-Identifier: Apache-2.0

use std::ops::Deref;
use std::str::FromStr;

use anyhow::Context as _;
use identity_credential::credential::Credential;
Expand All @@ -11,32 +10,17 @@ use identity_credential::credential::JwtCredential;
use identity_jose::jwt::JwtHeader;
use identity_jose::jwu;
use identity_iota_interaction::types::base_types::ObjectID;
use identity_iota_interaction::types::TypeTag;
use identity_iota_interaction::IotaKeySignature;
use identity_iota_interaction::{IotaKeySignature, IotaVerifiableCredential};
use itertools::Itertools;
use secret_storage::Signer;
use serde::Deserialize;
use serde::Serialize;

use crate::rebased::client::IdentityClient;
use crate::rebased::client::IdentityClientReadOnly;
use crate::rebased::transaction::TransactionInternal;
use identity_iota_interaction::MoveType;

use super::AuthenticatedAsset;
use super::AuthenticatedAssetBuilder;

#[derive(Debug, Clone, Serialize, Deserialize)]
struct IotaVerifiableCredential {
data: Vec<u8>,
}

impl MoveType for IotaVerifiableCredential {
fn move_type(package: ObjectID) -> TypeTag {
TypeTag::from_str(&format!("{package}::public_vc::PublicVc")).expect("valid utf8")
}
}

/// A publicly available verifiable credential.
#[derive(Debug, Clone)]
pub struct PublicAvailableVC {
Expand All @@ -59,7 +43,7 @@ impl PublicAvailableVC {

/// Get the JWT of the credential.
pub fn jwt(&self) -> Jwt {
String::from_utf8(self.asset.content().data.clone())
String::from_utf8(self.asset.content().data().clone())
.map(Jwt::new)
.expect("JWT is valid UTF8")
}
Expand All @@ -74,7 +58,7 @@ impl PublicAvailableVC {
{
let jwt_bytes = String::from(jwt).into_bytes();
let credential = parse_jwt_credential(&jwt_bytes)?;
let asset = AuthenticatedAssetBuilder::new(IotaVerifiableCredential { data: jwt_bytes })
let asset = AuthenticatedAssetBuilder::new(IotaVerifiableCredential::new(jwt_bytes))
.transferable(false)
.mutable(true)
.deletable(true)
Expand All @@ -99,7 +83,7 @@ impl PublicAvailableVC {
}

fn try_from_asset(asset: AuthenticatedAsset<IotaVerifiableCredential>) -> Result<Self, anyhow::Error> {
let credential = parse_jwt_credential(&asset.content().data)?;
let credential = parse_jwt_credential(&asset.content().data())?;
Ok(Self { asset, credential })
}
}
Expand Down
22 changes: 19 additions & 3 deletions identity_iota_core/src/rebased/migration/alias.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,26 @@ pub async fn get_alias(client: &IdentityClientReadOnly, object_id: ObjectID) ->
}
}



cfg_if::cfg_if! {
if #[cfg(target_arch = "wasm32")] {
// Add wasm32 compatible migrate() function wrapper here
} else {
use crate::rebased::transaction::Transaction;
impl UnmigratedAlias {
/// Returns a transaction that when executed migrates a legacy `Alias`
/// containing a DID Document to a new [`OnChainIdentity`].
pub async fn migrate(self, client: &IdentityClientReadOnly)
-> Result<impl Transaction<Output = OnChainIdentity>, Error> {
self.migrate_internal(client).await
}
}
}
}

impl UnmigratedAlias {
/// Returns a transaction that when executed migrates a legacy `Alias`
/// containing a DID Document to a new [`OnChainIdentity`].
pub async fn migrate(
pub(crate) async fn migrate_internal(
self,
client: &IdentityClientReadOnly,
) -> Result<impl TransactionInternal<Output = OnChainIdentity>, Error> {
Expand Down
30 changes: 17 additions & 13 deletions identity_iota_core/src/rebased/proposals/borrow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,22 @@ use super::UserDrivenTx;
cfg_if::cfg_if! {
if #[cfg(target_arch = "wasm32")] {
use iota_interaction_ts::NativeTsCodeBindingWrapper as Ptb;

pub trait IntentFnT: FnOnce(&mut Ptb, &HashMap<ObjectID, (Argument, IotaObjectData)>) {}
impl<T> IntentFnT for T where T: FnOnce(&mut Ptb, &HashMap<ObjectID, (Argument, IotaObjectData)>) {}
/// Instances of BorrowIntentFnT can be used as user-provided function to describe how
/// a borrowed assets shall be used.
pub trait BorrowIntentFnT: FnOnce(&mut Ptb, &HashMap<ObjectID, (Argument, IotaObjectData)>) {}
impl<T> BorrowIntentFnT for T where T: FnOnce(&mut Ptb, &HashMap<ObjectID, (Argument, IotaObjectData)>) {}
/// Boxed dynamic trait object of {@link BorrowIntentFnT}
#[allow(unreachable_pub)]
pub type IntentFn = Box<dyn IntentFnT + Send>;
pub type BorrowIntentFn = Box<dyn BorrowIntentFnT + Send>;
} else {
use identity_iota_interaction::types::programmable_transaction_builder::ProgrammableTransactionBuilder as Ptb;

pub trait IntentFnT: FnOnce(&mut Ptb, &HashMap<ObjectID, (Argument, IotaObjectData)>) {}
impl<T> IntentFnT for T where T: FnOnce(&mut Ptb, &HashMap<ObjectID, (Argument, IotaObjectData)>) {}
/// Instances of BorrowIntentFnT can be used as user-provided function to describe how
/// a borrowed assets shall be used.
pub trait BorrowIntentFnT: FnOnce(&mut Ptb, &HashMap<ObjectID, (Argument, IotaObjectData)>) {}
impl<T> BorrowIntentFnT for T where T: FnOnce(&mut Ptb, &HashMap<ObjectID, (Argument, IotaObjectData)>) {}
/// Boxed dynamic trait object of {@link BorrowIntentFnT}
#[allow(unreachable_pub)]
pub type IntentFn = Box<dyn IntentFnT + Send>;
pub type BorrowIntentFn = Box<dyn BorrowIntentFnT + Send>;
}
}

Expand All @@ -56,7 +60,7 @@ pub struct BorrowAction {
/// the borrowed assets shall be used.
pub struct BorrowActionWithIntent<F>
where
F: IntentFnT,
F: BorrowIntentFnT,
{
action: BorrowAction,
intent_fn: F,
Expand Down Expand Up @@ -167,7 +171,7 @@ impl<'i> UserDrivenTx<'i, BorrowAction> {
/// Defines how the borrowed assets should be used.
pub fn with_intent<F>(self, intent_fn: F) -> UserDrivenTx<'i, BorrowActionWithIntent<F>>
where
F: IntentFnT,
F: BorrowIntentFnT,
{
let UserDrivenTx {
identity,
Expand All @@ -183,8 +187,8 @@ impl<'i> UserDrivenTx<'i, BorrowAction> {
}

impl<'i> ProtoTransaction for UserDrivenTx<'i, BorrowAction> {
type Input = IntentFn;
type Tx = UserDrivenTx<'i, BorrowActionWithIntent<IntentFn>>;
type Input = BorrowIntentFn;
type Tx = UserDrivenTx<'i, BorrowActionWithIntent<BorrowIntentFn>>;

fn with(self, input: Self::Input) -> Self::Tx {
self.with_intent(input)
Expand All @@ -195,7 +199,7 @@ impl<'i> ProtoTransaction for UserDrivenTx<'i, BorrowAction> {
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
impl<F> TransactionInternal for UserDrivenTx<'_, BorrowActionWithIntent<F>>
where
F: IntentFnT + Send,
F: BorrowIntentFnT + Send,
{
type Output = ();
async fn execute_with_opt_gas_internal<S>(
Expand Down
30 changes: 17 additions & 13 deletions identity_iota_core/src/rebased/proposals/controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,22 @@ use super::UserDrivenTx;
cfg_if::cfg_if! {
if #[cfg(target_arch = "wasm32")] {
use iota_interaction_ts::NativeTsCodeBindingWrapper as Ptb;

pub trait IntentFnT: FnOnce(&mut Ptb, &Argument) {}
impl<T> IntentFnT for T where T: FnOnce(&mut Ptb, &Argument) {}
/// Instances of ControllerIntentFnT can be used as user-provided function to describe how
/// a borrowed identity's controller capability will be used.
pub trait ControllerIntentFnT: FnOnce(&mut Ptb, &Argument) {}
impl<T> ControllerIntentFnT for T where T: FnOnce(&mut Ptb, &Argument) {}
#[allow(unreachable_pub)]
pub type IntentFn = Box<dyn IntentFnT + Send>;
/// Boxed dynamic trait object of {@link ControllerIntentFnT}
pub type ControllerIntentFn = Box<dyn ControllerIntentFnT + Send>;
} else {
use identity_iota_interaction::types::programmable_transaction_builder::ProgrammableTransactionBuilder as Ptb;

pub trait IntentFnT: FnOnce(&mut Ptb, &Argument) {}
impl<T> IntentFnT for T where T: FnOnce(&mut Ptb, &Argument) {}
/// Instances of ControllerIntentFnT can be used as user-provided function to describe how
/// a borrowed identity's controller capability will be used.
pub trait ControllerIntentFnT: FnOnce(&mut Ptb, &Argument) {}
impl<T> ControllerIntentFnT for T where T: FnOnce(&mut Ptb, &Argument) {}
#[allow(unreachable_pub)]
pub type IntentFn = Box<dyn IntentFnT + Send>;
/// Boxed dynamic trait object of {@link ControllerIntentFnT}
pub type ControllerIntentFn = Box<dyn ControllerIntentFnT + Send>;
}
}

Expand All @@ -59,7 +63,7 @@ pub struct ControllerExecution {
/// the borrowed identity's controller capability will be used.
pub struct ControllerExecutionWithIntent<F>
where
F: IntentFnT,
F: ControllerIntentFnT,
{
action: ControllerExecution,
intent_fn: F,
Expand Down Expand Up @@ -151,7 +155,7 @@ impl<'i> UserDrivenTx<'i, ControllerExecution> {
/// Defines how the borrowed assets should be used.
pub fn with_intent<F>(self, intent_fn: F) -> UserDrivenTx<'i, ControllerExecutionWithIntent<F>>
where
F: IntentFnT,
F: ControllerIntentFnT,
{
let UserDrivenTx {
identity,
Expand All @@ -167,8 +171,8 @@ impl<'i> UserDrivenTx<'i, ControllerExecution> {
}

impl<'i> ProtoTransaction for UserDrivenTx<'i, ControllerExecution> {
type Input = IntentFn;
type Tx = UserDrivenTx<'i, ControllerExecutionWithIntent<IntentFn>>;
type Input = ControllerIntentFn;
type Tx = UserDrivenTx<'i, ControllerExecutionWithIntent<ControllerIntentFn>>;

fn with(self, input: Self::Input) -> Self::Tx {
self.with_intent(input)
Expand All @@ -179,7 +183,7 @@ impl<'i> ProtoTransaction for UserDrivenTx<'i, ControllerExecution> {
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
impl<F> TransactionInternal for UserDrivenTx<'_, ControllerExecutionWithIntent<F>>
where
F: IntentFnT + Send,
F: ControllerIntentFnT + Send,
{
type Output = ();
async fn execute_with_opt_gas_internal<S>(
Expand Down
4 changes: 4 additions & 0 deletions identity_iota_interaction/src/iota_verifiable_credential.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ pub struct IotaVerifiableCredential {
}

impl IotaVerifiableCredential {
pub fn new(data: Vec<u8>) -> IotaVerifiableCredential {
IotaVerifiableCredential { data }
}

pub fn data(&self) -> &Vec<u8> {
&self.data
}
Expand Down

0 comments on commit c1e4cbf

Please sign in to comment.