Skip to content

Commit

Permalink
Partial bitcoin parsing (#19)
Browse files Browse the repository at this point in the history
Co-authored-by: Antonio F. Š <antony@v3x.email>
  • Loading branch information
lucemans and Antony1060 authored Sep 8, 2023
1 parent 7c47f74 commit 4fae5a2
Show file tree
Hide file tree
Showing 13 changed files with 219 additions and 59 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/target
.env
.idea/
58 changes: 30 additions & 28 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ thiserror = "1.0.48"
regex = "1.9.5"
rustls = "0.21.7"
bs58 = "0.5.0"
sha2 = "0.10.7"
digest = "0.10.7"
hex-literal = "0.4.1"
axum-macros = "0.3.8"
lazy_static = "1.4.0"
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ mod models;
mod routes;
mod state;
mod provider;
mod utils;

use dotenvy::dotenv;
use state::AppState;
Expand Down
46 changes: 16 additions & 30 deletions src/models/lookup/multicoin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ use ethers_core::{
use hex_literal::hex;
use tracing::info;

use crate::models::multicoin::cointype::{coins::CoinType, evm::ChainId, slip44::SLIP44};
use crate::models::multicoin::{
cointype::{coins::CoinType, evm::ChainId, slip44::SLIP44},
decoding::bitcoin::decode_btc,
};

use super::{ENSLookup, ENSLookupError};

Expand All @@ -28,8 +31,6 @@ impl ENSLookup for Multicoin {
}

fn decode(&self, data: &[u8]) -> Result<String, ENSLookupError> {
info!("Decoding: {:?}", data);

let decoded_abi = ethers_core::abi::decode(&[ParamType::Bytes], data)
.map_err(|_| ENSLookupError::AbiError)?;
let value = decoded_abi
Expand All @@ -46,13 +47,12 @@ impl ENSLookup for Multicoin {
// SLIP-044 Chain Address Decoding (see ensip-9)
CoinType::Slip44(slip44) => match slip44 {
// Bitcoin Decoding
SLIP44::Bitcoin => Ok(format!("btc:{}", bs58::encode(value).into_string())),
SLIP44::Bitcoin => Ok(decode_btc(value.as_slice()).unwrap()),
// Lightcoin Decoding
SLIP44::Litecoin => {
Err(ENSLookupError::Unknown(anyhow!(
"Litecoin Decoding Not Implemented"
)))
// Ok(format!("ltc:{}", bs58::encode(value).into_string()))
}

// Unsupported SLIP44 Chain
Expand All @@ -61,36 +61,22 @@ impl ENSLookup for Multicoin {
// Ok(format!("SLIP-{:?}", value))

// Unsupported
Err(ENSLookupError::Unsupported("Chain Not Supported".to_string()))
Err(ENSLookupError::Unsupported(
"Chain Not Supported".to_string(),
))
}
},
// Implement EVM Chain Address Decoding (mostly ChecksummedHex, sometimes ChecksummedHex(chainId)) (see ensip-11)
CoinType::Evm(evm) => match evm {
// TODO: EVM Exceptions go here
// ChainId::Ethereum => {
// // Verify length is 20 bytes
// if value.len() != 20 {
// // TODO: throw invalid length
// return Ok("Invalid Length".to_string());
// }

// let address = hex::encode(value);

// Ok(format!("0x{address}"))
// },

// Every EVM Chain
_ => {
// Verify length is 20 bytes
if value.len() != 20 {
// TODO: throw invalid length
return Ok("Invalid Length".to_string());
}
CoinType::Evm(_evm) => {
// Verify length is 20 bytes
if value.len() != 20 {
// TODO: throw invalid length
return Ok("Invalid Length".to_string());
}

let address = hex::encode(value);
let address = hex::encode(value);

Ok(format!("0x{address}"))
}
Ok(format!("0x{address}"))
},
}
}
Expand Down
Loading

0 comments on commit 4fae5a2

Please sign in to comment.