Skip to content

Commit

Permalink
Merge 4cce383 into c5f0073
Browse files Browse the repository at this point in the history
  • Loading branch information
Oliver E. Anderson committed Sep 27, 2022
2 parents c5f0073 + 4cce383 commit e840057
Show file tree
Hide file tree
Showing 19 changed files with 66 additions and 53 deletions.
2 changes: 1 addition & 1 deletion bindings/wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ crate-type = ["cdylib", "rlib"]
[dependencies]
async-trait = { version = "0.1", default-features = false }
# Ensure bee-block always matches the version used by iota-client.
bee-block = { version = "1.0.0-beta.7", default-features = false, features = ["dto"] }
bee-block = { version = "1.0", default-features = false, features = ["dto"] }
console_error_panic_hook = { version = "0.1" }
futures = { version = "0.3" }
js-sys = { version = "0.3" }
Expand Down
4 changes: 2 additions & 2 deletions examples/0_basic/1_update_did.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@ async fn main() -> anyhow::Result<()> {

// Because the size of the DID document increased, we have to increase the allocated storage deposit.
// This increases the deposit amount to the new minimum.
let rent_structure: RentStructure = client.get_rent_structure().await?;
let rent_structure: RentStructure = client.get_rent_structure()?;
let alias_output: AliasOutput = AliasOutputBuilder::from(&alias_output)
.with_minimum_storage_deposit(rent_structure)
.finish()?;
.finish(client.get_token_supply()?)?;

// Publish the updated Alias Output.
let updated: IotaDocument = client.publish_did_output(&secret_manager, alias_output).await?;
Expand Down
8 changes: 4 additions & 4 deletions examples/0_basic/3_deactivate_did.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ async fn main() -> anyhow::Result<()> {
let deactivated_output: AliasOutput = client.deactivate_did_output(&did).await?;

// Optional: reduce and reclaim the storage deposit, sending the tokens to the state controller.
let rent_structure = client.get_rent_structure().await?;
let rent_structure = client.get_rent_structure()?;
let deactivated_output = AliasOutputBuilder::from(&deactivated_output)
.with_minimum_storage_deposit(rent_structure)
.finish()?;
.finish(client.get_token_supply()?)?;

// Publish the deactivated DID document.
let _ = client.publish_did_output(&secret_manager, deactivated_output).await?;
Expand All @@ -58,10 +58,10 @@ async fn main() -> anyhow::Result<()> {
let reactivated_output: AliasOutput = client.update_did_output(document.clone()).await?;

// Increase the storage deposit to the minimum again, if it was reclaimed during deactivation.
let rent_structure = client.get_rent_structure().await?;
let rent_structure = client.get_rent_structure()?;
let reactivated_output = AliasOutputBuilder::from(&reactivated_output)
.with_minimum_storage_deposit(rent_structure)
.finish()?;
.finish(client.get_token_supply()?)?;
client.publish_did_output(&secret_manager, reactivated_output).await?;

// Resolve the reactivated DID document.
Expand Down
2 changes: 1 addition & 1 deletion examples/0_basic/4_delete_did.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ async fn main() -> anyhow::Result<()> {
let error: Error = client.resolve_did(&did).await.unwrap_err();
assert!(matches!(
error,
identity_iota::iota::Error::DIDResolutionError(iota_client::Error::NotFound)
identity_iota::iota::Error::DIDResolutionError(iota_client::Error::NotFound(..))
));

Ok(())
Expand Down
6 changes: 3 additions & 3 deletions examples/1_advanced/0_did_controls_did.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ async fn main() -> anyhow::Result<()> {
let (_, company_did): (Address, IotaDID) = create_did(&client, &mut secret_manager).await?;

// Get the current byte costs and network name.
let rent_structure: RentStructure = client.get_rent_structure().await?;
let rent_structure: RentStructure = client.get_rent_structure()?;
let network_name: NetworkName = client.network_name().await?;

// Construct a new DID document for the subsidiary.
Expand All @@ -74,7 +74,7 @@ async fn main() -> anyhow::Result<()> {
.add_immutable_feature(IssuerFeature::new(AliasAddress::new(AliasId::from(&company_did)).into()).into())
// Adding the issuer feature means we have to recalculate the required storage deposit.
.with_minimum_storage_deposit(rent_structure.clone())
.finish()?;
.finish(client.get_token_supply()?)?;

// Publish the subsidiary's DID.
let mut subsidiary_document: IotaDocument = client.publish_did_output(&secret_manager, subsidiary_alias).await?;
Expand All @@ -99,7 +99,7 @@ async fn main() -> anyhow::Result<()> {
let subsidiary_alias: AliasOutput = client.update_did_output(subsidiary_document).await?;
let subsidiary_alias: AliasOutput = AliasOutputBuilder::from(&subsidiary_alias)
.with_minimum_storage_deposit(rent_structure.clone())
.finish()?;
.finish(client.get_token_supply()?)?;

// Publish the updated subsidiary's DID.
//
Expand Down
6 changes: 3 additions & 3 deletions examples/1_advanced/1_did_issues_nft.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ async fn main() -> anyhow::Result<()> {
let (_, manufacturer_did): (Address, IotaDID) = create_did(&client, &mut secret_manager).await?;

// Get the current byte cost.
let rent_structure: RentStructure = client.get_rent_structure().await?;
let rent_structure: RentStructure = client.get_rent_structure()?;

// Create a Digital Product Passport NFT issued by the manufacturer.
let product_passport_nft: NftOutput =
Expand All @@ -72,7 +72,7 @@ async fn main() -> anyhow::Result<()> {
.add_immutable_feature(Feature::Metadata(MetadataFeature::new(
b"Digital Product Passport Metadata".to_vec(),
)?))
.finish()?;
.finish(client.get_token_supply()?)?;

// Publish the NFT.
let block: Block = client
Expand All @@ -97,7 +97,7 @@ async fn main() -> anyhow::Result<()> {
// Fetch the NFT Output.
let nft_output_id: OutputId = client.nft_output_id(nft_id).await?;
let output_response: OutputResponse = client.get_output(&nft_output_id).await?;
let output: Output = Output::try_from(&output_response.output)?;
let output: Output = Output::try_from_dto(&output_response.output, client.get_token_supply()?)?;

// Extract the issuer of the NFT.
let nft_output: NftOutput = if let Output::Nft(nft_output) = output {
Expand Down
6 changes: 3 additions & 3 deletions examples/1_advanced/2_nft_owns_did.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@ async fn main() -> anyhow::Result<()> {
let address: Address = get_address_with_funds(&client, &mut secret_manager, FAUCET_ENDPOINT).await?;

// Get the current byte cost.
let rent_structure: RentStructure = client.get_rent_structure().await?;
let rent_structure: RentStructure = client.get_rent_structure()?;

// Create the car NFT with an Ed25519 address as the unlock condition.
let car_nft: NftOutput = NftOutputBuilder::new_with_minimum_storage_deposit(rent_structure.clone(), NftId::null())?
.add_unlock_condition(UnlockCondition::Address(AddressUnlockCondition::new(address)))
.finish()?;
.finish(client.get_token_supply()?)?;

// Publish the NFT output.
let block: Block = client
Expand Down Expand Up @@ -118,7 +118,7 @@ async fn main() -> anyhow::Result<()> {
let car_nft_id: &NftId = car_nft_address.nft_id();
let output_id: OutputId = client.nft_output_id(*car_nft_id).await?;
let output_response: OutputResponse = client.get_output(&output_id).await?;
let output: Output = Output::try_from(&output_response.output)?;
let output: Output = Output::try_from_dto(&output_response.output, client.get_token_supply()?)?;

let car_nft: NftOutput = if let Output::Nft(nft_output) = output {
nft_output
Expand Down
13 changes: 7 additions & 6 deletions examples/1_advanced/3_did_issues_tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ async fn main() -> anyhow::Result<()> {
// Create a new DID for the authority.
let (_, authority_did): (Address, IotaDID) = create_did(&client, &mut secret_manager).await?;

let rent_structure: RentStructure = client.get_rent_structure().await?;
let rent_structure: RentStructure = client.get_rent_structure()?;

// We want to update the foundry counter of the authority's Alias Output, so we create an
// updated version of the output. We pass in the previous document,
Expand All @@ -75,7 +75,7 @@ async fn main() -> anyhow::Result<()> {
// We will add one foundry to this Alias Output.
let authority_alias_output = AliasOutputBuilder::from(&authority_alias_output)
.with_foundry_counter(1)
.finish()?;
.finish(client.get_token_supply()?)?;

// Create a token foundry that represents carbon credits.
let token_scheme = TokenScheme::Simple(SimpleTokenScheme::new(
Expand All @@ -100,7 +100,7 @@ async fn main() -> anyhow::Result<()> {
.add_unlock_condition(UnlockCondition::ImmutableAliasAddress(
ImmutableAliasAddressUnlockCondition::new(AliasAddress::new(AliasId::from(&authority_did))),
))
.finish()?;
.finish(client.get_token_supply()?)?;

let carbon_credits_foundry_id: FoundryId = carbon_credits_foundry.id();

Expand All @@ -120,7 +120,8 @@ async fn main() -> anyhow::Result<()> {
// Get the latest output that contains the foundry.
let foundry_output_id: OutputId = client.foundry_output_id(carbon_credits_foundry_id).await?;
let carbon_credits_foundry: OutputResponse = client.get_output(&foundry_output_id).await?;
let carbon_credits_foundry: Output = Output::try_from(&carbon_credits_foundry.output)?;
let carbon_credits_foundry: Output =
Output::try_from_dto(&carbon_credits_foundry.output, client.get_token_supply()?)?;

let carbon_credits_foundry: FoundryOutput = if let Output::Foundry(foundry_output) = carbon_credits_foundry {
foundry_output
Expand Down Expand Up @@ -164,15 +165,15 @@ async fn main() -> anyhow::Result<()> {
Address::Alias(AliasAddress::new(*authority_alias_id)),
tomorrow,
)?))
.finish()?;
.finish(client.get_token_supply()?)?;

// Reduce the carbon credits in the foundry by the amount that is sent to the company.
let carbon_credits_foundry = FoundryOutputBuilder::from(&carbon_credits_foundry)
.with_native_tokens(vec![NativeToken::new(
carbon_credits_foundry.token_id(),
U256::from(499_000u32),
)?])
.finish()?;
.finish(client.get_token_supply()?)?;

// Publish the output, transferring the carbon credits.
let block: Block = client
Expand Down
2 changes: 1 addition & 1 deletion examples/1_advanced/4_key_exchange.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ async fn main() -> anyhow::Result<()> {
.context("failed to get address with funds")?;

let network: NetworkName = client.network_name().await?;
let rent_structure: RentStructure = client.get_rent_structure().await?;
let rent_structure: RentStructure = client.get_rent_structure()?;

// Alice creates and publishes their DID Document.
let (alice_did, alice_x25519): (IotaDID, KeyPair) = {
Expand Down
4 changes: 2 additions & 2 deletions examples/1_advanced/5_alias_output_history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,10 @@ async fn main() -> anyhow::Result<()> {

// Increase the storage deposit and publish the update.
let alias_output: AliasOutput = client.update_did_output(document.clone()).await?;
let rent_structure: RentStructure = client.get_rent_structure().await?;
let rent_structure: RentStructure = client.get_rent_structure()?;
let alias_output: AliasOutput = AliasOutputBuilder::from(&alias_output)
.with_minimum_storage_deposit(rent_structure)
.finish()?;
.finish(client.get_token_supply()?)?;
client.publish_did_output(&secret_manager, alias_output).await?;
}

Expand Down
2 changes: 1 addition & 1 deletion examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ publish = false
anyhow = "1.0.62"
did-key = { version = "0.1.1", default-features = false }
identity_iota = { path = "../identity_iota" }
iota-client = { version = "2.0.0-beta.3", default-features = false, features = ["tls", "stronghold"] }
iota-client = { version = "2.0", default-features = false, features = ["tls", "stronghold"] }
primitive-types = "0.11.1"
rand = "0.8.5"
tokio = { version = "1.20.1", default-features = false, features = ["rt"] }
Expand Down
13 changes: 4 additions & 9 deletions examples/utils/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,9 @@ pub async fn get_address_with_funds(
) -> anyhow::Result<Address> {
let address: Address = get_address(client, stronghold).await?;

request_faucet_funds(
client,
address,
client.get_bech32_hrp().await?.as_str(),
faucet_endpoint,
)
.await
.context("failed to request faucet funds")?;
request_faucet_funds(client, address, client.get_bech32_hrp()?.as_str(), faucet_endpoint)
.await
.context("failed to request faucet funds")?;

Ok(address)
}
Expand Down Expand Up @@ -152,7 +147,7 @@ async fn get_address_balance(client: &Client, address: &str) -> anyhow::Result<u

let mut total_amount = 0;
for output_response in outputs_responses {
let output = Output::try_from(&output_response.output)?;
let output = Output::try_from_dto(&output_response.output, client.get_token_supply()?)?;
total_amount += output.amount();
}

Expand Down
2 changes: 1 addition & 1 deletion identity_iota/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ identity_resolver = { version = "=0.7.0-alpha.1", path = "../identity_resolver",

[dev-dependencies]
anyhow = "1.0.64"
iota-client = { version = "2.0.0-beta.3", default-features = false, features = ["tls", "stronghold"] }
iota-client = { version = "2.0", default-features = false, features = ["tls", "stronghold"] }
tokio = { version = "1.17.0", features = ["full"] }

[features]
Expand Down
4 changes: 2 additions & 2 deletions identity_iota_core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ description = "An IOTA Ledger integration for the IOTA DID Method."

[dependencies]
# Ensure bee-block always matches the version used by iota-client.
bee-block = { version = "1.0.0-beta.7", default-features = false, features = ["std"], optional = true }
bee-block = { version = "1.0", default-features = false, features = ["std"], optional = true }
identity_core = { version = "=0.7.0-alpha.1", path = "../identity_core", default-features = false }
identity_credential = { version = "=0.7.0-alpha.1", path = "../identity_credential", default-features = false, features = ["validator"] }
identity_did = { version = "=0.7.0-alpha.1", path = "../identity_did", default-features = false }

async-trait = { version = "0.1.56", default-features = false, optional = true }
futures = { version = "0.3" }
iota-client = { version = "2.0.0-beta.3", default-features = false, features = ["tls"], optional = true }
iota-client = { version = "2.0", default-features = false, features = ["tls"], optional = true }
num-derive = { version = "0.3", default-features = false }
num-traits = { version = "0.2", default-features = false, features = ["std"] }
once_cell = { version = "1", default-features = false, features = ["std"] }
Expand Down
17 changes: 12 additions & 5 deletions identity_iota_core/src/client/identity_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ pub trait IotaIdentityClient {
async fn get_alias_output(&self, alias_id: AliasId) -> Result<(OutputId, AliasOutput)>;

/// Return the rent structure of the network, indicating the byte costs for outputs.
async fn get_rent_structure(&self) -> Result<RentStructure>;
fn get_rent_structure(&self) -> Result<RentStructure>;

/// Gets the token supply of the node we're connecting to.
fn get_token_supply(&self) -> Result<u64>;
}

/// An extension trait that provides helper functions for publication
Expand Down Expand Up @@ -65,7 +68,7 @@ pub trait IotaIdentityClientExt: IotaIdentityClient {
let rent_structure: RentStructure = if let Some(rent) = rent_structure {
rent
} else {
self.get_rent_structure().await?
self.get_rent_structure()?
};

AliasOutputBuilder::new_with_minimum_storage_deposit(rent_structure, AliasId::null())
Expand All @@ -80,7 +83,7 @@ pub trait IotaIdentityClientExt: IotaIdentityClient {
.add_unlock_condition(UnlockCondition::GovernorAddress(GovernorAddressUnlockCondition::new(
address,
)))
.finish()
.finish(self.get_token_supply()?)
.map_err(Error::AliasOutputBuildError)
}

Expand All @@ -105,7 +108,9 @@ pub trait IotaIdentityClientExt: IotaIdentityClient {
alias_output_builder = alias_output_builder.with_alias_id(id);
}

alias_output_builder.finish().map_err(Error::AliasOutputBuildError)
alias_output_builder
.finish(self.get_token_supply()?)
.map_err(Error::AliasOutputBuildError)
}

/// Removes the DID document from the state metadata of its Alias Output,
Expand All @@ -132,7 +137,9 @@ pub trait IotaIdentityClientExt: IotaIdentityClient {
alias_output_builder = alias_output_builder.with_alias_id(alias_id);
}

alias_output_builder.finish().map_err(Error::AliasOutputBuildError)
alias_output_builder
.finish(self.get_token_supply()?)
.map_err(Error::AliasOutputBuildError)
}

/// Resolve a [`IotaDocument`]. Returns an empty, deactivated document if the state metadata
Expand Down
19 changes: 12 additions & 7 deletions identity_iota_core/src/client/iota_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ impl IotaClientExt for Client {
.map_err(Error::BasicOutputBuildError)?
.with_native_tokens(alias_output.native_tokens().clone())
.add_unlock_condition(UnlockCondition::Address(AddressUnlockCondition::new(address)))
.finish_output()
.finish_output(self.get_token_supply().map_err(Error::TokenSupplyError)?)
.map_err(Error::BasicOutputBuildError)?;

let block: Block = self
Expand All @@ -111,13 +111,17 @@ impl IotaClientExt for Client {
#[cfg_attr(not(feature = "send-sync-client-ext"), async_trait::async_trait(?Send))]
impl IotaIdentityClient for Client {
async fn get_network_hrp(&self) -> Result<String> {
self.get_bech32_hrp().await.map_err(Error::DIDResolutionError)
self.get_bech32_hrp().map_err(Error::DIDResolutionError)
}

async fn get_alias_output(&self, id: AliasId) -> Result<(OutputId, AliasOutput)> {
let output_id: OutputId = self.alias_output_id(id).await.map_err(Error::DIDResolutionError)?;
let output_response: OutputResponse = self.get_output(&output_id).await.map_err(Error::DIDResolutionError)?;
let output: Output = Output::try_from(&output_response.output).map_err(Error::OutputConversionError)?;
let output: Output = Output::try_from_dto(
&output_response.output,
<Self as IotaIdentityClient>::get_token_supply(self)?,
)
.map_err(Error::OutputConversionError)?;

if let Output::Alias(alias_output) = output {
Ok((output_id, alias_output))
Expand All @@ -126,10 +130,11 @@ impl IotaIdentityClient for Client {
}
}

async fn get_rent_structure(&self) -> Result<RentStructure> {
Client::get_rent_structure(self)
.await
.map_err(|err| Error::DIDUpdateError("get_rent_structure failed", Some(err)))
fn get_rent_structure(&self) -> Result<RentStructure> {
Client::get_rent_structure(self).map_err(|err| Error::DIDUpdateError("get_rent_structure failed", Some(err)))
}
fn get_token_supply(&self) -> Result<u64> {
self.get_token_supply().map_err(Error::TokenSupplyError)
}
}

Expand Down
4 changes: 3 additions & 1 deletion identity_iota_core/src/document/iota_document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,7 @@ impl TryMethod for IotaDocument {

#[cfg(test)]
mod tests {
use bee_block::protocol::ProtocolParameters;
use identity_core::common::Timestamp;
use identity_core::convert::FromJson;
use identity_core::convert::ToJson;
Expand Down Expand Up @@ -752,6 +753,7 @@ mod tests {

#[test]
fn test_unpack_empty() {
let mock_token_supply: u64 = ProtocolParameters::default().token_supply();
let controller_did: IotaDID = valid_did();

// VALID: unpack empty, deactivated document.
Expand All @@ -766,7 +768,7 @@ mod tests {
.add_unlock_condition(UnlockCondition::GovernorAddress(GovernorAddressUnlockCondition::new(
Address::Alias(AliasAddress::new(AliasId::from(&controller_did))),
)))
.finish()
.finish(mock_token_supply)
.unwrap();
let document: IotaDocument = IotaDocument::unpack_from_output(&did, &alias_output, true).unwrap();
assert_eq!(document.id(), &did);
Expand Down
Loading

0 comments on commit e840057

Please sign in to comment.