Skip to content

Commit e9dccba

Browse files
bitzoicK1-R1JoshuaBatty
authored
Add Bytes conversions for Address, ContractId, EvmAddress, B512 and AssetId (#6857)
## Description Several stack types did not convert from or into `Bytes`. These have been added in this PR for better interop and devex. ## Checklist - [x] I have linked to any relevant issues. - [x] I have commented my code, particularly in hard-to-understand areas. - [x] I have updated the documentation where relevant (API docs, the reference, and the Sway book). - [x] If my change requires substantial documentation changes, I have [requested support from the DevRel team](https://github.com/FuelLabs/devrel-requests/issues/new/choose) - [x] I have added tests that prove my fix is effective or that my feature works. - [x] I have added (or requested a maintainer to add) the necessary `Breaking*` or `New Feature` labels where relevant. - [x] I have done my best to ensure that my PR adheres to [the Fuel Labs Code Review Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md). - [x] I have requested a review from the relevant team or maintainers. --------- Co-authored-by: K1-R1 <77465250+K1-R1@users.noreply.github.com> Co-authored-by: Joshua Batty <joshpbatty@gmail.com>
1 parent 85c9602 commit e9dccba

File tree

10 files changed

+694
-10
lines changed

10 files changed

+694
-10
lines changed

sway-lib-std/src/address.sw

+60-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
//! The `Address` type used for interacting with addresses on the fuel network.
22
library;
33

4-
use ::convert::From;
4+
use ::convert::{From, Into, TryFrom};
55
use ::hash::{Hash, Hasher};
6+
use ::bytes::Bytes;
7+
use ::option::Option::{self, *};
68

79
/// The `Address` type, a struct wrapper around the inner `b256` value.
810
pub struct Address {
@@ -110,7 +112,7 @@ impl From<Address> for b256 {
110112
/// ```sway
111113
/// fn foo() {
112114
/// let address = Address::zero();
113-
/// let b256_data: b256 = address.into();
115+
/// let b256_data: b256 = b256::from(address);
114116
/// assert(b256_data == b256::zero());
115117
/// }
116118
/// ```
@@ -119,6 +121,62 @@ impl From<Address> for b256 {
119121
}
120122
}
121123

124+
impl TryFrom<Bytes> for Address {
125+
/// Casts raw `Bytes` data to an `Address`.
126+
///
127+
/// # Arguments
128+
///
129+
/// * `bytes`: [Bytes] - The raw `Bytes` data to be casted.
130+
///
131+
/// # Returns
132+
///
133+
/// * [Address] - The newly created `Address` from the raw `Bytes`.
134+
///
135+
/// # Examples
136+
///
137+
/// ```sway
138+
/// use std::bytes::Bytes;
139+
///
140+
/// fn foo(bytes: Bytes) {
141+
/// let result = Address::try_from(bytes);
142+
/// assert(result.is_some());
143+
/// let address = result.unwrap();
144+
/// }
145+
/// ```
146+
fn try_from(bytes: Bytes) -> Option<Self> {
147+
if bytes.len() != 32 {
148+
return None;
149+
}
150+
151+
Some(Self {
152+
bits: asm(ptr: bytes.ptr()) {
153+
ptr: b256
154+
},
155+
})
156+
}
157+
}
158+
159+
impl Into<Bytes> for Address {
160+
/// Casts an `Address` to raw `Bytes` data.
161+
///
162+
/// # Returns
163+
///
164+
/// * [Bytes] - The underlying raw `Bytes` data of the `Address`.
165+
///
166+
/// # Examples
167+
///
168+
/// ```sway
169+
/// fn foo() {
170+
/// let address = Address::zero();
171+
/// let bytes_data: Bytes = address.into()
172+
/// assert(bytes_data.len() == 32);
173+
/// }
174+
/// ```
175+
fn into(self) -> Bytes {
176+
Bytes::from(self.bits())
177+
}
178+
}
179+
122180
impl Hash for Address {
123181
fn hash(self, ref mut state: Hasher) {
124182
let Address { bits } = self;

sway-lib-std/src/asset_id.sw

+63-5
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ library;
33

44
use ::alias::SubId;
55
use ::contract_id::ContractId;
6-
use ::convert::From;
6+
use ::convert::{From, Into, TryFrom};
77
use ::hash::{Hash, Hasher};
8+
use ::bytes::Bytes;
9+
use ::option::Option::{self, *};
810

911
/// An AssetId is used for interacting with an asset on the network.
1012
///
@@ -71,7 +73,7 @@ impl AssetId {
7173
/// # Examples
7274
///
7375
/// ```sway
74-
/// use std::callframes::contract_id;
76+
/// use std::call_frames::contract_id;
7577
///
7678
/// fn foo() {
7779
/// let contract_id = contract_id();
@@ -109,7 +111,7 @@ impl AssetId {
109111
/// # Examples
110112
///
111113
/// ```sway
112-
/// use std::{callframes::contract_id, constants::DEFAULT_SUB_ID};
114+
/// use std::{call_frames::contract_id, constants::DEFAULT_SUB_ID};
113115
///
114116
/// fn foo() {
115117
/// let asset_id = AssetId::default();
@@ -236,12 +238,68 @@ impl From<AssetId> for b256 {
236238
///
237239
/// ```sway
238240
/// fn foo() {
239-
/// let asset_id = AssetId::b256::zero();
240-
/// let b256_data: b256 = asset_id.into();
241+
/// let asset_id = AssetId::zero();
242+
/// let b256_data: b256 = b256::from(asset_id);
241243
/// assert(b256_data == b256::zero());
242244
/// }
243245
/// ```
244246
fn from(id: AssetId) -> Self {
245247
id.bits()
246248
}
247249
}
250+
251+
impl TryFrom<Bytes> for AssetId {
252+
/// Casts raw `Bytes` data to an `AssetId`.
253+
///
254+
/// # Arguments
255+
///
256+
/// * `bytes`: [Bytes] - The raw `Bytes` data to be casted.
257+
///
258+
/// # Returns
259+
///
260+
/// * [AssetId] - The newly created `AssetId` from the raw `Bytes`.
261+
///
262+
/// # Examples
263+
///
264+
/// ```sway
265+
/// use std::bytes::Bytes;
266+
///
267+
/// fn foo(bytes: Bytes) {
268+
/// let result = AssetId::try_from(bytes);
269+
/// assert(result.is_some());
270+
/// let asset_id = result.unwrap();
271+
/// }
272+
/// ```
273+
fn try_from(bytes: Bytes) -> Option<Self> {
274+
if bytes.len() != 32 {
275+
return None;
276+
}
277+
278+
Some(Self {
279+
bits: asm(ptr: bytes.ptr()) {
280+
ptr: b256
281+
},
282+
})
283+
}
284+
}
285+
286+
impl Into<Bytes> for AssetId {
287+
/// Casts an `AssetId` to raw `Bytes` data.
288+
///
289+
/// # Returns
290+
///
291+
/// * [Bytes] - The underlying raw `Bytes` data of the `AssetId`.
292+
///
293+
/// # Examples
294+
///
295+
/// ```sway
296+
/// fn foo() {
297+
/// let asset_id = AssetId::zero();
298+
/// let bytes_data: Bytes = asset_id.into();
299+
/// assert(bytes_data.len() == 32);
300+
/// }
301+
/// ```
302+
fn into(self) -> Bytes {
303+
Bytes::from(self.bits())
304+
}
305+
}

sway-lib-std/src/b512.sw

+59-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
//! The `B512` type supports the usage of 64-byte values in Sway which are needed when working with public keys and signatures.
22
library;
33

4-
use ::convert::From;
4+
use ::convert::{From, Into, TryFrom};
5+
use ::bytes::Bytes;
6+
use ::option::Option::{self, *};
57

68
/// Stores two `b256`s in contiguous memory.
79
/// Guaranteed to be contiguous for use with ec-recover: `std::ecr::ec_recover`.
@@ -159,3 +161,59 @@ impl B512 {
159161
(self.bits)[0] == b256::zero() && (self.bits)[1] == b256::zero()
160162
}
161163
}
164+
165+
impl TryFrom<Bytes> for B512 {
166+
/// Casts raw `Bytes` data to an `B512`.
167+
///
168+
/// # Arguments
169+
///
170+
/// * `bytes`: [Bytes] - The raw `Bytes` data to be casted.
171+
///
172+
/// # Returns
173+
///
174+
/// * [B512] - The newly created `B512` from the raw `Bytes`.
175+
///
176+
/// # Examples
177+
///
178+
/// ```sway
179+
/// use std::bytes::Bytes;
180+
///
181+
/// fn foo(bytes: Bytes) {
182+
/// let result = B512::try_from(bytes);
183+
/// assert(result.is_some());
184+
/// let b512 = result.unwrap();
185+
/// }
186+
/// ```
187+
fn try_from(bytes: Bytes) -> Option<Self> {
188+
if bytes.len() != 64 {
189+
return None;
190+
}
191+
192+
Some(Self {
193+
bits: asm(ptr: bytes.ptr()) {
194+
ptr: [b256; 2]
195+
},
196+
})
197+
}
198+
}
199+
200+
impl Into<Bytes> for B512 {
201+
/// Casts an `B512` to raw `Bytes` data.
202+
///
203+
/// # Returns
204+
///
205+
/// * [Bytes] - The underlying raw `Bytes` data of the `B512`.
206+
///
207+
/// # Examples
208+
///
209+
/// ```sway
210+
/// fn foo() {
211+
/// let b512 = B512::from((b256::zero(), b256::zero()));
212+
/// let bytes_data: Bytes = b512.into()
213+
/// assert(bytes_data.len() == 64);
214+
/// }
215+
/// ```
216+
fn into(self) -> Bytes {
217+
Bytes::from(raw_slice::from_parts::<u8>(__addr_of(self.bits), 64))
218+
}
219+
}

sway-lib-std/src/contract_id.sw

+59-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
//! The `ContractId` type used for interacting with contracts on the fuel network.
22
library;
33

4-
use ::convert::From;
4+
use ::convert::{From, Into, TryFrom};
55
use ::hash::{Hash, Hasher};
6+
use ::bytes::Bytes;
7+
use ::option::Option::{self, *};
68

79
/// The `ContractId` type, a struct wrapper around the inner `b256` value.
810
pub struct ContractId {
@@ -80,6 +82,62 @@ impl From<ContractId> for b256 {
8082
}
8183
}
8284

85+
impl TryFrom<Bytes> for ContractId {
86+
/// Casts raw `Bytes` data to an `ContractId`.
87+
///
88+
/// # Arguments
89+
///
90+
/// * `bytes`: [Bytes] - The raw `Bytes` data to be casted.
91+
///
92+
/// # Returns
93+
///
94+
/// * [ContractId] - The newly created `ContractId` from the raw `Bytes`.
95+
///
96+
/// # Examples
97+
///
98+
/// ```sway
99+
/// use std::bytes::Bytes;
100+
///
101+
/// fn foo(bytes: Bytes) {
102+
/// let result = ContractId::try_from(bytes);
103+
/// assert(result.is_some());
104+
/// let contract_id = result.unwrap();
105+
/// }
106+
/// ```
107+
fn try_from(bytes: Bytes) -> Option<Self> {
108+
if bytes.len() != 32 {
109+
return None;
110+
}
111+
112+
Some(Self {
113+
bits: asm(ptr: bytes.ptr()) {
114+
ptr: b256
115+
},
116+
})
117+
}
118+
}
119+
120+
impl Into<Bytes> for ContractId {
121+
/// Casts an `ContractId` to raw `Bytes` data.
122+
///
123+
/// # Returns
124+
///
125+
/// * [Bytes] - The underlying raw `Bytes` data of the `ContractId`.
126+
///
127+
/// # Examples
128+
///
129+
/// ```sway
130+
/// fn foo() {
131+
/// let contract_id = ContractId::zero();
132+
/// let bytes_data: Bytes = contract_id.into()
133+
/// assert(bytes_data.len() == 32);
134+
/// }
135+
/// ```
136+
fn into(self) -> Bytes {
137+
Bytes::from(self.bits())
138+
}
139+
}
140+
83141
impl Hash for ContractId {
84142
fn hash(self, ref mut state: Hasher) {
85143
let Self { bits } = self;

0 commit comments

Comments
 (0)