Skip to content

Commit

Permalink
Implement multicoin encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
Antony1060 committed Nov 23, 2023
1 parent 02d4363 commit 67420c6
Show file tree
Hide file tree
Showing 27 changed files with 694 additions and 574 deletions.
108 changes: 106 additions & 2 deletions Cargo.lock

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

21 changes: 18 additions & 3 deletions Cargo.toml
Expand Up @@ -19,12 +19,27 @@ serde_json = "1.0.108"
thiserror = "1.0.50"
tokio = {version = "1", features = ["full"]}
tokio-postgres = "0.7.10"
tower-http = { version = "0.4.3", features = ["cors"] }
tower-http = { version = "0.4.4", features = ["cors"] }
tracing = "0.1.40"
tracing-subscriber = "0.3.18"
tracing-subscriber = { version = "0.3.18", features = ["env-filter"]}
lazy_static = { version = "1.4.0", features = [] }

# Multicoin encoding
bs58 = "0.5.0"
base32 = "0.4.0"
bech32 = "0.10.0-beta"
blake2 = "0.10.6"
sha2 = "0.10.8"
crc16 = "0.4.0"
ciborium = "0.2.1"
crc32fast = "1.3.2"

[dev-dependencies]
hex-literal = "0.4.1"

[features]
postgres = []
selfservice = []
eoa-auth = []
default = ["postgres", "selfservice", "eoa-auth"]
admin-auth = []
default = ["postgres", "selfservice", "eoa-auth", "admin-auth"]
14 changes: 11 additions & 3 deletions src/gateway/endpoint.rs
Expand Up @@ -24,16 +24,24 @@ pub async fn route(

#[derive(Debug, Error)]
pub enum CCIPEndpointError {
#[error("Invalid prefix {0}")]
#[error("Invalid prefix: {0}")]
DecodeError(#[from] super::payload::ResolverDecodeError),
#[error("Resolve error {0}")]
#[error("Resolve error: {0}")]
ResolveError(#[from] super::resolution::ResolveError),
#[error("Sign error {0}")]
#[error("Sign error: {0}")]
SignError(#[from] super::signing::SignError),
}

impl IntoResponse for CCIPEndpointError {
fn into_response(self) -> Response {
// TODO: remove
if let CCIPEndpointError::ResolveError(x) = &self {
if let super::resolution::ResolveError::Unparsable = x {
return (axum::http::StatusCode::UNPROCESSABLE_ENTITY, x.to_string())
.into_response();
}
}

GatewayResponse::Error(self.to_string()).into_response()
}
}
Expand Down
17 changes: 9 additions & 8 deletions src/gateway/resolution.rs
Expand Up @@ -2,9 +2,10 @@ use std::sync::Arc;

use ethers::{abi::Token, providers::namehash, utils::keccak256};
use thiserror::Error;
use tracing::info;
use tracing::{debug, info};

use crate::multicoin::cointype::coins::CoinType;
use crate::multicoin::encoding::MulticoinEncoder;
use crate::{ccip::lookup::ResolverFunctionCall, state::GlobalState};

use super::{payload::ResolveCCIPPostPayload, signing::UnsignedPayload};
Expand Down Expand Up @@ -65,18 +66,18 @@ impl UnresolvedQuery<'_> {

let hash = namehash(&self.name).to_fixed_bytes().to_vec();

let x = state.db.get_addresses(&hash, &[&chain.to_string()]).await;
let addresses = state.db.get_addresses(&hash, &[&chain.to_string()]).await;

let value = x
let value: &str = addresses
.get(&chain.to_string())
.to_owned()
.ok_or(ResolveError::NotFound)?
.clone()
.as_ref()
.ok_or(ResolveError::NotFound)?;

let bytes = CoinType::from(*chain as u32)
.encode(value)
.map_err(|_| ResolveError::Unparsable)?;
let bytes = CoinType::from(*chain as u32).encode(value).map_err(|err| {
debug!("error while trying to encode {}: {}", chain, err);
ResolveError::Unparsable
})?;

vec![Token::Bytes(bytes)]
}
Expand Down
15 changes: 13 additions & 2 deletions src/main.rs
Expand Up @@ -2,7 +2,8 @@ use std::{env, str::FromStr};

use dotenvy::dotenv;
use ethers::signers::{LocalWallet, Signer};
use tracing::info;
use tracing::{info, Level};
use tracing_subscriber::{EnvFilter, FmtSubscriber};

pub mod ccip;
pub mod database;
Expand All @@ -17,7 +18,17 @@ pub mod utils;
async fn main() {
dotenv().ok();

tracing_subscriber::fmt().init();
let filter = EnvFilter::new(format!("offchain_gateway={}", Level::DEBUG));

let subscriber = FmtSubscriber::builder()
// all spans/events with a level higher than TRACE (e.g, debug, info, warn, etc.)
// will be written to stdout.
.with_max_level(Level::DEBUG)
.with_env_filter(filter)
// completes the builder.
.finish();

tracing::subscriber::set_global_default(subscriber).expect("setting default subscriber failed");

let db = database::bootstrap().await;

Expand Down
8 changes: 0 additions & 8 deletions src/multicoin/cointype/slip44.rs
Expand Up @@ -4,9 +4,7 @@ use super::CoinType;

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SLIP44 {
Tezos,
Hedera,
Monero,
Ripple,
Solana,
Cardano,
Expand All @@ -16,10 +14,8 @@ pub enum SLIP44 {
Litecoin,
Dogecoin,
Ethereum,
Monacoin,
Polkadot,
Rootstock,
BitcoinCash,
EthereumClassic,
Other(U256),
}
Expand All @@ -31,16 +27,12 @@ impl From<u32> for SLIP44 {
2 => SLIP44::Litecoin,
3 => SLIP44::Dogecoin,
60 => SLIP44::Ethereum,
145 => SLIP44::BitcoinCash,
61 => SLIP44::EthereumClassic,
128 => SLIP44::Monero,
144 => SLIP44::Ripple,
148 => SLIP44::Stellar,
1729 => SLIP44::Tezos,
3030 => SLIP44::Hedera,
1815 => SLIP44::Cardano,
137 => SLIP44::Rootstock,
22 => SLIP44::Monacoin,
714 => SLIP44::Binance,
501 => SLIP44::Solana,
354 => SLIP44::Polkadot,
Expand Down

0 comments on commit 67420c6

Please sign in to comment.