Skip to content

Commit

Permalink
Implement encoding for multicoins (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
Antony1060 committed Nov 23, 2023
1 parent a5a0273 commit 4fcdc30
Show file tree
Hide file tree
Showing 28 changed files with 850 additions and 38 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/
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
Original file line number Diff line number Diff line change
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"]
6 changes: 3 additions & 3 deletions src/gateway/endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ 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),
}

Expand Down
24 changes: 16 additions & 8 deletions src/gateway/resolution.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use std::sync::Arc;

use ethers::{abi::Token, providers::namehash, types::H160, utils::keccak256};
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 @@ -56,20 +58,26 @@ impl UnresolvedQuery<'_> {
vec![Token::String(value)]
}
ResolverFunctionCall::AddrMultichain(_bf, chain) => {
info!(name = self.name, chain = chain, "Resolution Address Multichain");
info!(
name = self.name,
chain = chain,
"Resolution Address Multichain"
);

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 = value.as_bytes().to_vec();
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
13 changes: 10 additions & 3 deletions src/http.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use std::{net::SocketAddr, sync::Arc};
use std::{env, net::SocketAddr, sync::Arc};

use crate::state::GlobalState;
use axum::{
routing::{get, post},
Router, Server,
};
use tower_http::cors::CorsLayer;
use tracing::{debug, info};

use crate::state::GlobalState;

/// Starts the HTTP Server
pub async fn serve(state: GlobalState) {
info!("Starting webserver");
Expand All @@ -24,7 +25,13 @@ pub async fn serve(state: GlobalState) {
.with_state(Arc::new(state))
.layer(CorsLayer::very_permissive());

let addr = SocketAddr::from(([0, 0, 0, 0], 3000));
let addr = SocketAddr::from((
[0, 0, 0, 0],
env::var("PORT")
.unwrap_or("3000".to_string())
.parse::<u16>()
.expect("port should fit in u16"),
));
debug!("Listening on {}", addr);

Server::bind(&addr)
Expand Down
24 changes: 18 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,33 @@ 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};

mod http;
pub mod ccip;
pub mod state;
pub mod utils;
pub mod gateway;
pub mod database;
pub mod gateway;
mod http;
pub mod multicoin;
pub mod selfservice;
pub mod state;
pub mod utils;

#[tokio::main]
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
Loading

0 comments on commit 4fcdc30

Please sign in to comment.