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 restricted and implicit address types #1229

Merged
merged 46 commits into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
7e89965
Add restricted and implicit address types
Sep 13, 2023
a95fab3
comments and derive
Sep 13, 2023
efe62c5
nit
Sep 13, 2023
5a4e121
Merge branch '2.0' into feat/address-types
Sep 13, 2023
bd28696
no_std
Sep 13, 2023
0fdbb67
Merge branch '2.0' into feat/address-types
Sep 14, 2023
8b42ff6
Merge branch '2.0' into feat/address-types
Sep 14, 2023
a4b4ae5
Merge branch '2.0' into feat/address-types
Sep 15, 2023
370a7f5
Merge branch '2.0' into feat/address-types
Sep 15, 2023
d8ec789
Merge branch '2.0' into feat/address-types
Sep 18, 2023
9179b92
Merge branch '2.0' into feat/address-types
Sep 19, 2023
aa60d43
Merge branch '2.0' into feat/address-types
thibault-martinez Sep 24, 2023
4742d27
Split out ImplicitAccountCreationAddress
thibault-martinez Sep 24, 2023
6e2e860
Split out Restricted
thibault-martinez Sep 24, 2023
ef3427d
Rename to RestrictedAddress
thibault-martinez Sep 24, 2023
37e72b3
Merge branch '2.0' into feat/address-types
thibault-martinez Sep 26, 2023
6fe6f1e
Merge branch '2.0' into feat/address-types
thibault-martinez Sep 26, 2023
9a854f4
Tests in tests
thibault-martinez Sep 26, 2023
a28ce3d
Merge branch '2.0' into feat/address-types
Sep 27, 2023
8f64435
Update restricted address and remove Copy derives
Sep 27, 2023
e7435b7
Merge branch '2.0' into feat/address-types
Sep 27, 2023
59b764a
fix bech32 tests
Sep 27, 2023
2b2e4d0
Clippy
thibault-martinez Sep 28, 2023
36e39aa
Apply suggestions from code review
thibault-martinez Sep 28, 2023
be03d05
remove clones
Sep 28, 2023
0f92318
little cleanup
Sep 28, 2023
a99d6df
Merge branch '2.0' into feat/address-types
Sep 29, 2023
6274556
Nit
thibault-martinez Oct 1, 2023
83db39e
Split and clean tests
thibault-martinez Oct 1, 2023
9bc4317
PR suggestions
Oct 2, 2023
f70e0b7
Merge branch '2.0' into feat/address-types
Oct 2, 2023
c0490a3
Merge branch '2.0' into feat/address-types
Oct 3, 2023
a3c577d
Refactor address capabilities
Oct 3, 2023
958dbaa
Use prefixed box for capabilities
Oct 3, 2023
8d89c1a
renames
Oct 3, 2023
7bfc658
clippy
Oct 3, 2023
7f73583
Merge branch '2.0' into feat/address-types
Oct 4, 2023
14a2022
docs
Oct 4, 2023
e253d2b
Remove Result/Option
thibault-martinez Oct 5, 2023
9ff8b92
Merge branch '2.0' into feat/address-types
Oct 5, 2023
477f6d2
Update sdk/src/client/utils.rs
Oct 5, 2023
825fbff
Revert some stuff
thibault-martinez Oct 5, 2023
91b7ffe
Missed changes
thibault-martinez Oct 5, 2023
7716258
PR suggestions and no_std
Oct 6, 2023
f5532eb
Merge branch '2.0' into feat/address-types
Oct 6, 2023
f03a45d
Merge branch '2.0' into feat/address-types
thibault-martinez Oct 10, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions sdk/src/client/api/block_builder/input_selection/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ impl InputSelection {
AccountTransition::State,
))),
Address::Nft(nft_address) => Ok(Some(Requirement::Nft(*nft_address.nft_id()))),
_ => todo!("What do we do here?"),
kwek20 marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ impl InputSelection {
Err(e) => Err(e),
}
}
_ => todo!("What do we do here?"),
}
}
}
1 change: 1 addition & 0 deletions sdk/src/client/secret/ledger_nano.rs
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,7 @@ fn merge_unlocks(
merged_unlocks.push(Unlock::Reference(ReferenceUnlock::new(*block_index as u16)?));
}
Address::Nft(_nft) => merged_unlocks.push(Unlock::Nft(NftUnlock::new(*block_index as u16)?)),
_ => todo!("What do we do here?"),
kwek20 marked this conversation as resolved.
Show resolved Hide resolved
},
None => {
// We can only sign ed25519 addresses and block_indexes needs to contain the account or nft
Expand Down
1 change: 1 addition & 0 deletions sdk/src/client/secret/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,7 @@ where
blocks.push(Unlock::Reference(ReferenceUnlock::new(*block_index as u16)?));
}
Address::Nft(_nft) => blocks.push(Unlock::Nft(NftUnlock::new(*block_index as u16)?)),
_ => todo!("What do we do here?"),
},
None => {
// We can only sign ed25519 addresses and block_indexes needs to contain the account or nft
Expand Down
1 change: 1 addition & 0 deletions sdk/src/client/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ pub fn bech32_to_hex(bech32: impl ConvertTo<Bech32Address>) -> Result<String> {
Address::Ed25519(ed) => ed.to_string(),
Address::Account(account) => account.to_string(),
Address::Nft(nft) => nft.to_string(),
_ => todo!("What do we do here?"),
kwek20 marked this conversation as resolved.
Show resolved Hide resolved
})
}

Expand Down
36 changes: 36 additions & 0 deletions sdk/src/types/block/address/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use core::str::FromStr;

use derive_more::{AsRef, Deref, From};

use super::Restricted;
use crate::types::block::{output::AccountId, Error};

/// An account address.
Expand Down Expand Up @@ -37,6 +38,12 @@ impl AccountAddress {
}
}

impl Restricted<AccountAddress> {
/// The [`Address`](crate::types::block::address::Address) kind of a
/// [`RestrictedAccountAddress`](Restricted<AccountAddress>).
pub const KIND: u8 = 9;
}

impl FromStr for AccountAddress {
type Err = Error;

Expand All @@ -62,6 +69,7 @@ mod dto {
use serde::{Deserialize, Serialize};

use super::*;
use crate::types::block::address::dto::RestrictedDto;

/// Describes an account address.
#[derive(Serialize, Deserialize)]
Expand All @@ -88,4 +96,32 @@ mod dto {
}

impl_serde_typed_dto!(AccountAddress, AccountAddressDto, "account address");

impl From<&Restricted<AccountAddress>> for RestrictedDto<AccountAddressDto> {
fn from(value: &Restricted<AccountAddress>) -> Self {
Self {
address: AccountAddressDto {
kind: Restricted::<AccountAddress>::KIND,
account_id: value.address.0,
},
allowed_capabilities: value.allowed_capabilities.into_iter().map(|c| c.0).collect(),
}
}
}

impl From<RestrictedDto<AccountAddressDto>> for Restricted<AccountAddress> {
fn from(value: RestrictedDto<AccountAddressDto>) -> Self {
let mut res = Self::new(AccountAddress::from(value.address));
if let Some(allowed_capabilities) = value.allowed_capabilities.first() {
res = res.with_allowed_capabilities(*allowed_capabilities);
}
res
}
}

impl_serde_typed_dto!(
Restricted<AccountAddress>,
RestrictedDto<AccountAddressDto>,
"restricted account address"
);
}
90 changes: 87 additions & 3 deletions sdk/src/types/block/address/ed25519.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
use core::str::FromStr;

use crypto::signatures::ed25519::PublicKey;
use derive_more::{AsRef, Deref, From};
use derive_more::{AsRef, Deref, Display, From, FromStr};
use packable::Packable;

use super::Restricted;
use crate::types::block::Error;

/// An Ed25519 address.
#[derive(Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash, From, AsRef, Deref, packable::Packable)]
#[derive(Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash, From, AsRef, Deref, Packable)]
#[as_ref(forward)]
pub struct Ed25519Address([u8; Self::LENGTH]);

Expand All @@ -26,6 +28,29 @@ impl Ed25519Address {
}
}

impl Restricted<Ed25519Address> {
/// The [`Address`](crate::types::block::address::Address) kind of a
/// [`RestrictedEd25519Address`](Restricted<Ed25519Address>).
pub const KIND: u8 = 1;
}

/// An implicit account creation address that can be used to transition an account.
#[derive(Copy, Clone, Debug, Display, Eq, PartialEq, Ord, PartialOrd, Hash, FromStr, AsRef, Deref, From, Packable)]
#[as_ref(forward)]
pub struct ImplicitAccountCreationAddress(Ed25519Address);
impl ImplicitAccountCreationAddress {
DaughterOfMars marked this conversation as resolved.
Show resolved Hide resolved
/// The [`Address`](crate::types::block::address::Address) kind of an [`ImplicitAccountCreationAddress`].
pub const KIND: u8 = 24;
/// The length of an [`ImplicitAccountCreationAddress`].
pub const LENGTH: usize = Ed25519Address::LENGTH;

/// Creates a new [`ImplicitAccountCreationAddress`].
#[inline(always)]
pub fn new(address: [u8; Self::LENGTH]) -> Self {
Self(Ed25519Address::new(address))
}
}

impl FromStr for Ed25519Address {
type Err = Error;

Expand All @@ -51,7 +76,7 @@ pub(crate) mod dto {
use serde::{Deserialize, Serialize};

use super::*;
use crate::utils::serde::prefix_hex_bytes;
use crate::{types::block::address::dto::RestrictedDto, utils::serde::prefix_hex_bytes};

/// Describes an Ed25519 address.
#[derive(Serialize, Deserialize)]
Expand Down Expand Up @@ -79,4 +104,63 @@ pub(crate) mod dto {
}

impl_serde_typed_dto!(Ed25519Address, Ed25519AddressDto, "ed25519 address");

impl From<&Restricted<Ed25519Address>> for RestrictedDto<Ed25519AddressDto> {
fn from(value: &Restricted<Ed25519Address>) -> Self {
Self {
address: Ed25519AddressDto {
kind: Restricted::<Ed25519Address>::KIND,
pub_key_hash: value.address.0,
},
allowed_capabilities: value.allowed_capabilities.into_iter().map(|c| c.0).collect(),
}
}
}

impl From<RestrictedDto<Ed25519AddressDto>> for Restricted<Ed25519Address> {
fn from(value: RestrictedDto<Ed25519AddressDto>) -> Self {
let mut res = Self::new(Ed25519Address::from(value.address));
if let Some(allowed_capabilities) = value.allowed_capabilities.first() {
res = res.with_allowed_capabilities(*allowed_capabilities);
}
res
}
}

impl_serde_typed_dto!(
Restricted<Ed25519Address>,
RestrictedDto<Ed25519AddressDto>,
"restricted ed25519 address"
);

/// Describes an Implicit Account Creation address.
#[derive(Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
struct ImplicitAccountCreationAddressDto {
#[serde(rename = "type")]
kind: u8,
#[serde(with = "prefix_hex_bytes")]
pub_key_hash: [u8; Ed25519Address::LENGTH],
}

impl From<&ImplicitAccountCreationAddress> for ImplicitAccountCreationAddressDto {
fn from(value: &ImplicitAccountCreationAddress) -> Self {
Self {
kind: ImplicitAccountCreationAddress::KIND,
pub_key_hash: value.0.0,
}
}
}

impl From<ImplicitAccountCreationAddressDto> for ImplicitAccountCreationAddress {
fn from(value: ImplicitAccountCreationAddressDto) -> Self {
Self::new(value.pub_key_hash)
}
}

impl_serde_typed_dto!(
ImplicitAccountCreationAddress,
ImplicitAccountCreationAddressDto,
"implicit account creation address"
);
}
Loading