Skip to content

Commit

Permalink
Merge pull request #8 from jeje/feat/gateway-lib
Browse files Browse the repository at this point in the history
feat(lib): introduced ENS gateway library
  • Loading branch information
jeje committed Jun 28, 2023
2 parents c22eebe + 56927de commit 56b06a1
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 45 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ jobs:
- name: "Upload Binaries"
uses: "taiki-e/upload-rust-binary-action@v1"
with:
bin: "offchain-resolver-gateway"
bin: "ens-gateway"
target: ${{ matrix.target }}
archive: $bin-${{ matrix.target }}
ref: refs/tags/v${{ needs.get-tag.outputs.pkg-version }}
Expand Down
42 changes: 21 additions & 21 deletions Cargo.lock

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

12 changes: 10 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
[package]
name = "offchain-resolver-gateway"
version = "0.1.1"
name = "ens-offchain-resolver-gateway"
version = "0.1.2"
edition = "2021"

[lib]
name = "ens_gateway_server"
path = "src/lib.rs"

[[bin]]
name = "ens-gateway"
path = "src/main.rs"

[dependencies]
ccip-read-server = "0.1.0"

Expand Down
10 changes: 5 additions & 5 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ WORKDIR /opt/
RUN apk add curl protoc musl-dev gzip git

# offchain-resolver-gateway
RUN curl -sLO https://github.com/jeje/ens-offchain-resolver-gateway-rs/releases/latest/download/offchain-resolver-gateway-x86_64-unknown-linux-musl.tar.gz \
&& tar -xvf offchain-resolver-gateway-x86_64-unknown-linux-musl.tar.gz \
&& chmod +x offchain-resolver-gateway
RUN curl -sLO https://github.com/jeje/ens-offchain-resolver-gateway-rs/releases/latest/download/ens-gateway-x86_64-unknown-linux-musl.tar.gz \
&& tar -xvf ens-gateway-x86_64-unknown-linux-musl.tar.gz \
&& chmod +x ens-gateway

#########################################################

FROM alpine:3.16.2

RUN apk add tmux

COPY --from=builder /opt/offchain-resolver-gateway /opt/offchain-resolver-gateway
COPY --from=builder /opt/ens-gateway /opt/ens-gateway

RUN chown -R root:root /opt/

Expand All @@ -24,4 +24,4 @@ ENV TTL 300
ENV LISTEN_IP 0.0.0.0
ENV LISTEN_PORT 8080

ENTRYPOINT [ "/opt/offchain-resolver-gateway" ]
ENTRYPOINT [ "/opt/ens-gateway" ]
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,15 @@ Precompiled ENS gateways are available in [releases page](https://github.com/jej

A Docker image is also available: https://hub.docker.com/r/jeje/ens-offchain-resolver-gateway-rs

Lastly a library is provided to ease implementation of custom gateways without duplicating much code.
A good sample is the [default implementation provided](src/main.rs).

### CLI Usage

```
ENS Offchain Gateway server answering requests from CCIP-READ protocol (aka ERC-3668)
Usage: offchain-resolver-gateway [OPTIONS] --privatekey <VALUE> <--json <FILE>>
Usage: ens-gateway [OPTIONS] --privatekey <VALUE> <--json <FILE>>
Options:
-k, --privatekey <VALUE> private key of the wallet allowed to sign offchain ENS record results [env: PRIVATE_KEY]
Expand Down
9 changes: 8 additions & 1 deletion src/errors.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
use thiserror::Error;

/// ENS gateway errors
#[derive(Error, Debug)]
pub enum GatewayErrors {
#[error("invalid dns name")]
/// Invalid ENS name error
#[error("invalid ENS name")]
InvalidName,
/// ABI error
#[error("ABI error {0}")]
Abi(#[from] ethers::contract::AbiError),
/// ABI Parsing error
#[error("ABI parse error {0}")]
AbiParse(#[from] ethers::abi::ParseError),
/// CCIP-Read server error
#[error("CCIP Read error {0}")]
CCIPReadMiddleware(#[from] ccip_read_server::CCIPReadMiddlewareError),
/// Unknown ENS record type error
#[error("unknown record type")]
UnknownRecordType,
/// Invalid signature error
#[error("signature error")]
Signature(#[from] ethers::types::SignatureError),
}
18 changes: 18 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//! ENS offchain resolver gateway framework

/// Various offchain database storage options
pub mod db;
/// Errors
pub mod errors;
/// Gateway framework
pub mod gateway;
/// Utils
pub mod utils;

use ethers::prelude::abigen;

abigen!(
Resolver,
"./res/Resolver.json",
event_derives(serde::Deserialize, serde::Serialize)
);
16 changes: 2 additions & 14 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use crate::db::{Database, JsonDatabase};
use crate::gateway::Gateway;
use clap::{arg, command, value_parser, ArgGroup};
use color_eyre::Report;
use ethers::prelude::abigen;
use ens_gateway_server::db::{Database, JsonDatabase};
use ens_gateway_server::gateway::Gateway;
use ethers::signers::{LocalWallet, Signer};
use eyre::Result;
use std::env;
Expand All @@ -13,17 +12,6 @@ use tracing::info;
use tracing_subscriber::prelude::*;
use tracing_subscriber::EnvFilter;

mod db;
mod errors;
mod gateway;
mod utils;

abigen!(
Resolver,
"./res/Resolver.json",
event_derives(serde::Deserialize, serde::Serialize)
);

#[tokio::main]
async fn main() -> Result<(), Report> {
let matches = command!()
Expand Down
4 changes: 4 additions & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::errors::GatewayErrors;
use ethers::abi::{AbiEncode, Token};
use ethers::types::Signature;

/// Decode DNS name from call data, parsing ENS labels
pub fn decode_dns_name(dns_name: &Token) -> Result<String, GatewayErrors> {
if let Some(dns_name) = dns_name.clone().into_bytes() {
let mut labels = vec![];
Expand All @@ -20,6 +21,9 @@ pub fn decode_dns_name(dns_name: &Token) -> Result<String, GatewayErrors> {
}
}

/// Compute `yParityAndS` from a signature
///
/// EIP-2098: <https://eips.ethereum.org/EIPS/eip-2098>
pub fn compact_y_parity_and_s(sig: &Signature) -> Result<Vec<u8>, GatewayErrors> {
let mut y_parity_and_s = sig.s.encode();
if sig.recovery_id()?.is_y_odd() {
Expand Down

0 comments on commit 56b06a1

Please sign in to comment.