1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
//! Incremental account ID building support.
use super::AccountId;
use crate::account::*;
use std::{convert::TryInto, mem};
/// Incremental account ID builder.
#[derive(Debug)]
pub struct Builder {
/// Current raw account ID value without depth stored.
raw: RawAccountId,
/// Current account ID depth.
depth: AccountIdDepth,
}
impl Builder {
/// Creates a new account ID builder to construct descendant account IDs from the given ID.
///
/// # Errors
///
/// Returns one of the following [`AccountIdError`] variants if builder creation fails:
///
/// - [`NotIssuance`] if `id` is not an issuance account.
///
/// # Examples
///
/// ```
/// use m10_sdk::account::{id, AccountId};
///
/// let id = AccountId::from_raw(0x0181_2345_0000_0000_0000_0000_0000_0002)?;
/// let builder = id::Builder::from_id(id)?;
/// # Ok::<_, Box<dyn std::error::Error>>(())
/// ```
///
/// [`AccountIdError`]: ../error/enum.AccountIdError.html
/// [`NotIssuance`]: ../error/enum.AccountIdError.html#variant.NotIssuance
pub fn from_id(id: AccountId) -> Result<Self, AccountIdError> {
if !id.is_issuance() {
Err(AccountIdError::NotIssuance)
} else {
Ok(Self {
raw: id.as_raw() - (id.as_raw() & ACCOUNT_ID_DEPTH_MASK as RawAccountId),
depth: id.depth(),
})
}
}
/// Creates a new account ID builder, starting with the given root account index.
///
/// # Errors
///
/// Returns one of the following [`AccountIdError`] variants if builder creation fails:
///
/// - [`IndexRange`] if `root_index` exceeds the maximum range supported for root account
/// indexes.
///
/// # Examples
///
/// ```
/// use m10_sdk::account::id;
///
/// let builder = id::Builder::from_root_account_index(3)?;
/// # Ok::<_, Box<dyn std::error::Error>>(())
/// ```
///
/// [`AccountIdError`]: ../error/enum.AccountIdError.html
/// [`IndexRange`]: ../error/enum.AccountIdError.html#variant.IndexRange
pub fn from_root_account_index<RootIndexT>(
root_index: RootIndexT,
) -> Result<Self, AccountIdError>
where
RootIndexT: TryInto<RootAccountIndex>,
AccountIdError: From<RootIndexT::Error>,
{
let root_index = root_index.try_into()?.as_raw();
Ok(Self {
raw: RawAccountId::from(root_index)
<< (mem::size_of::<RawAccountId>() * 8 - ROOT_ACCOUNT_INDEX_BIT_COUNT),
depth: 0,
})
}
/// Pushes a non-root issuance account onto the ID being built.
///
/// # Errors
///
/// Returns one of the following [`AccountIdError`] variants on failure:
///
/// - [`InvalidDepth`] if the new account depth would exceed [`AccountId::MAX_DEPTH`].
/// - [`IndexRange`] if `issuance_index` exceeds the maximum range supported for non-root
/// issuance account indexes.
///
/// # Examples
///
/// ```
/// use m10_sdk::account::id;
///
/// let mut builder = id::Builder::from_root_account_index(3)?;
/// builder.push(0x01_2345)?;
/// # Ok::<_, Box<dyn std::error::Error>>(())
/// ```
///
/// [`AccountIdError`]: ../error/enum.AccountIdError.html
/// [`InvalidDepth`]: ../error/enum.AccountIdError.html#variant.InvalidDepth
/// [`AccountId::MAX_DEPTH`]: struct.AccountId.html#associatedconstant.MAX_DEPTH
/// [`IndexRange`]: ../error/enum.AccountIdError.html#variant.IndexRange
pub fn push<IssuanceIndexT>(
&mut self,
issuance_index: IssuanceIndexT,
) -> Result<&mut Self, AccountIdError>
where
IssuanceIndexT: TryInto<IssuanceAccountIndex>,
AccountIdError: From<IssuanceIndexT::Error>,
{
let issuance_index = issuance_index.try_into()?.as_raw();
let depth = self.depth + 2;
if depth > AccountId::MAX_DEPTH {
Err(AccountIdError::InvalidDepth)
} else {
self.depth = depth;
self.raw |=
RawAccountId::from(issuance_index | (1 << ISSUANCE_ACCOUNT_INDEX_BIT_COUNT))
<< (mem::size_of::<RawAccountId>() * 8
- ROOT_ACCOUNT_INDEX_BIT_COUNT
- (ISSUANCE_ACCOUNT_INDEX_BIT_COUNT + 1) * usize::from(depth / 2));
Ok(self)
}
}
/// Returns the issuance account ID from the current builder state.
///
/// # Examples
///
/// ```
/// use m10_sdk::account::id;
///
/// let id = id::Builder::from_root_account_index(3)?.push(0x01_2345)?.issuance_id();
/// assert_eq!(id.as_raw(), 0x0381_2345_0000_0000_0000_0000_0000_0002);
/// # Ok::<_, Box<dyn std::error::Error>>(())
/// ```
pub fn issuance_id(&self) -> AccountId {
AccountId(self.raw | RawAccountId::from(self.depth))
}
/// Appends a leaf account index to the current ID and returns the resulting leaf account ID.
///
/// # Errors
///
/// Returns one of the following [`AccountIdError`] variants on failure:
///
/// - [`IndexRange`] if `leaf_index` exceeds the maximum range supported for non-root issuance
/// account indexes.
///
/// # Examples
///
/// ```
/// use m10_sdk::account::id;
///
/// let id = id::Builder::from_root_account_index(3)?.push(0x01_2345)?.leaf_id(0x0f_edcb_a987)?;
/// assert_eq!(id.as_raw(), 0x0381_2345_0fed_cba9_8700_0000_0000_0003);
/// # Ok::<_, Box<dyn std::error::Error>>(())
/// ```
///
/// [`AccountIdError`]: ../error/enum.AccountIdError.html
/// [`IndexRange`]: ../error/enum.AccountIdError.html#variant.IndexRange
pub fn leaf_id<LeafIndexT>(&self, leaf_index: LeafIndexT) -> Result<AccountId, AccountIdError>
where
LeafIndexT: TryInto<LeafAccountIndex>,
AccountIdError: From<LeafIndexT::Error>,
{
let leaf_index = leaf_index.try_into()?.as_raw();
let depth = self.depth + 1;
// We should never have a maximum depth that allows issuance accounts without leaf accounts.
debug_assert!(depth <= AccountId::MAX_DEPTH);
let raw = self.raw
| RawAccountId::from(leaf_index)
<< (mem::size_of::<RawAccountId>() * 8
- ROOT_ACCOUNT_INDEX_BIT_COUNT
- (ISSUANCE_ACCOUNT_INDEX_BIT_COUNT + 1) * usize::from(depth / 2)
- (LEAF_ACCOUNT_INDEX_BIT_COUNT + 1));
Ok(AccountId(raw | RawAccountId::from(depth)))
}
}