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

feat: support evm address #668

Merged
merged 21 commits into from
Nov 9, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
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 docs/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
- [Structs and enums](./types/custom_types.md)
- [String](./types/string.md)
- [Bits256](./types/bits256.md)
- [EvmAddress](./types/evm_address.md)
- [Vectors](./types/vectors.md)
- [API](./getting-started/api.md)
- [Debugging](./debugging/debugging.md)
Expand Down
15 changes: 15 additions & 0 deletions docs/src/types/evm_address.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# EvmAddress

In the Rust SDK, evm addresses can be represented with the 'EvmAddress' type. Its definition matches with the Sway standard library type with the same name and will be converted accordingly when interacting with contracts:
MujkicA marked this conversation as resolved.
Show resolved Hide resolved

```rust,ignore
{{#include ../../../packages/fuels-core/src/types/bits_256.rs:evm_address}}
```

Here's an example:

```rust,ignore
{{#include ../../../packages/fuels/tests/bindings.rs:evm_address_arg}}
```

> **Note:** when creating an `EvmAddress` from `Bits256`, the first 12 bytes will be cleared because an evm address is only 20 bytes long.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Explain the choice of clearing the leftmost ones. If it is a deliberate choice (ie not based on spec), then this choice should be stated clearly ("We chose the convention of clearing the leftmost bytes").

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The choice was made based on the Sway implementation of the same type. I find this a bit tricky to comment since we had a task to specifically remove all mentions of sway.

25 changes: 18 additions & 7 deletions packages/fuels-core/src/types/bits_256.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ impl Tokenizable for Bits256 {
match token {
Token::B256(data) => Ok(Bits256(data)),
_ => Err(Error::InvalidData(format!(
"Bits256 cannot be constructed from token {token}"
"EvmAddress could not be constructed from the given token. Reason: Expected Struct(B256) got: {token:?}"
MujkicA marked this conversation as resolved.
Show resolved Hide resolved
))),
}
}
Expand Down Expand Up @@ -68,6 +68,7 @@ impl EvmAddress {

impl From<Bits256> for EvmAddress {
fn from(b256: Bits256) -> Self {
// An evm address is only 20 bytes
let value = Bits256(Self::clear_12_bytes(b256.0));
MujkicA marked this conversation as resolved.
Show resolved Hide resolved

Self { value }
Expand Down Expand Up @@ -124,7 +125,7 @@ mod tests {

#[test]
fn test_from_token_b256() -> Result<(), Error> {
let data = [0u8; 32];
let data = [1u8; 32];
let token = Token::B256(data);

let bits256 = Bits256::from_token(token)?;
Expand All @@ -136,7 +137,7 @@ mod tests {

#[test]
fn test_into_token_b256() {
let data = [0u8; 32];
let data = [1u8; 32];
let bits256 = Bits256(data);

let token = bits256.into_token();
Expand Down Expand Up @@ -177,23 +178,33 @@ mod tests {

#[test]
fn test_from_token_evm_addr() -> Result<(), Error> {
let data = [0u8; 32];
let data = [1u8; 32];
let token = Token::Struct(vec![Token::B256(data)]);

let evm_address = EvmAddress::from_token(token)?;

assert_eq!(evm_address.value.0, data);
let expected_data = [
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1,
];

assert_eq!(evm_address.value.0, expected_data);

Ok(())
}

#[test]
fn test_into_token_evm_addr() {
let data = [0u8; 32];
let data = [1u8; 32];
let evm_address = EvmAddress::from(Bits256(data));

let token = evm_address.into_token();

assert_eq!(token, Token::Struct(vec![Token::B256(data)]));
let expected_data = [
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1,
];

assert_eq!(token, Token::Struct(vec![Token::B256(expected_data)]));
}
}