From 3074d6f76f1efe247cc21afc383acc935f2f6219 Mon Sep 17 00:00:00 2001 From: Bryan Stitt Date: Sat, 24 Sep 2022 07:18:33 +0000 Subject: [PATCH] left pad addresses --- web3_proxy/src/bin/web3_proxy_cli/create_user.rs | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/web3_proxy/src/bin/web3_proxy_cli/create_user.rs b/web3_proxy/src/bin/web3_proxy_cli/create_user.rs index 243d829a..82355a18 100644 --- a/web3_proxy/src/bin/web3_proxy_cli/create_user.rs +++ b/web3_proxy/src/bin/web3_proxy_cli/create_user.rs @@ -39,21 +39,19 @@ impl CreateUserSubCommand { // TODO: would be nice to use the fixed array instead of a Vec in the entities // take a simple String. If it starts with 0x, parse as address. otherwise convert ascii to hex - // TODO: there has to be a cleaner way to convert a String to a [u8; 20] let address = if self.address.starts_with("0x") { let address = self.address.parse::
()?; address.to_fixed_bytes().into() } else { - let bytes = self.address.as_bytes(); + // left pad and truncate the string + let address = &format!("{:\x00>20}", self.address)[0..20]; - let mut address: [u8; 20] = [0; 20]; + // convert the string to bytes + let bytes = address.as_bytes(); - let len = 20.min(bytes.len()); - - address[..len].copy_from_slice(&bytes[..len]); - - address.into() + // convert the slice to a Vec + bytes.try_into().unwrap() }; // TODO: get existing or create a new one