Skip to content

Commit

Permalink
Free and paid signatures
Browse files Browse the repository at this point in the history
  • Loading branch information
TonyGiorgio committed Mar 12, 2024
1 parent 70e849b commit 886ea44
Show file tree
Hide file tree
Showing 12 changed files with 265 additions and 71 deletions.
1 change: 1 addition & 0 deletions .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ DATABASE_URL=postgres://localhost/hermes
FM_DB_PATH=./.fedimint-test-dir
NSEC=
DOMAIN_URL=
AUTH_PK=
#HERMES_PORT=8080
113 changes: 109 additions & 4 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 @@ -21,6 +21,7 @@ fedimint-core = "0.2.2"
fedimint-client = "0.2.2"
fedimint-wallet-client = "0.2.2"
fedimint-mint-client = "0.2.2"
bls12_381 = { version = "0.7.1", features = [ "zeroize", "groups" ] }
fedimint-ln-client = "0.2.2"
futures = "0.3.28"
url = "2.5.0"
Expand All @@ -41,6 +42,7 @@ tokio = { version = "1.12.0", features = ["full"] }
tower-http = { version = "0.4.0", features = ["cors"] }
lazy-regex = "3.1.0"
multimint = { git = "https://github.com/Kodylow/multimint", rev = "00df9d34f0244d0200eee4d285094b22b34cf38b" }
names = "0.14.0"

[dev-dependencies]
mockall = "0.11.2"
Expand Down
2 changes: 1 addition & 1 deletion justfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ test-integration:
cargo test --features integration-tests

run:
cargo run
RUST_LOG=debug cargo run

release:
cargo run --release
Expand Down
8 changes: 8 additions & 0 deletions migrations/2024-02-20-210617_user_info/up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ CREATE TABLE app_user (
federation_invite_code VARCHAR(255) NOT NULL
);

CREATE INDEX idx_app_user_unblinded_msg ON app_user (unblinded_msg);
CREATE INDEX idx_app_user_name ON app_user (name);

CREATE TABLE invoice (
id SERIAL PRIMARY KEY,
federation_id VARCHAR(64) NOT NULL,
Expand All @@ -17,9 +20,14 @@ CREATE TABLE invoice (
state INTEGER NOT NULL DEFAULT 0
);

CREATE INDEX idx_invoice_state ON invoice (state);
CREATE INDEX idx_invoice_op_id ON invoice (op_id);

CREATE TABLE zaps
(
id INTEGER NOT NULL PRIMARY KEY references invoice (id),
request TEXT NOT NULL,
event_id VARCHAR(64)
);

CREATE INDEX idx_zaps_event_id ON zaps (event_id);
6 changes: 3 additions & 3 deletions src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::models::{
#[cfg_attr(test, automock)]
pub(crate) trait DBConnection {
fn check_name_available(&self, name: String) -> anyhow::Result<bool>;
fn check_token_not_spent(&self, msg: String) -> anyhow::Result<bool>;
fn get_user_by_token(&self, msg: String) -> anyhow::Result<Option<AppUser>>;
fn insert_new_user(&self, name: NewAppUser) -> anyhow::Result<AppUser>;
fn get_pending_invoices(&self) -> anyhow::Result<Vec<Invoice>>;
fn insert_new_invoice(&self, invoice: NewInvoice) -> anyhow::Result<Invoice>;
Expand All @@ -36,9 +36,9 @@ impl DBConnection for PostgresConnection {
AppUser::check_available_name(conn, name)
}

fn check_token_not_spent(&self, msg: String) -> anyhow::Result<bool> {
fn get_user_by_token(&self, msg: String) -> anyhow::Result<Option<AppUser>> {
let conn = &mut self.db.get()?;
AppUser::check_token_not_spent(conn, msg)
AppUser::get_by_token(conn, msg)
}

fn insert_new_user(&self, new_user: NewAppUser) -> anyhow::Result<AppUser> {
Expand Down
6 changes: 4 additions & 2 deletions src/lnurlp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,15 +188,17 @@ mod tests_integration {
let nostr = nostr_sdk::Client::new(&nostr_sk);

// create blind signer
let signer = BlindSigner::derive(&[0u8; 32], 0, 0);
let free_signer = BlindSigner::derive(&[0u8; 32], 0, 0);
let paid_signer = BlindSigner::derive(&[0u8; 32], 0, 0);

let mock_mm = Arc::new(mock_mm);
let state = State {
db: db.clone(),
mm: mock_mm,
secp: Secp256k1::new(),
nostr,
auth_pk: signer.pk,
free_pk: free_signer.pk,
paid_pk: paid_signer.pk,
domain: "http://hello.com".to_string(),
};

Expand Down
32 changes: 25 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use axum::http::{request::Parts, HeaderValue, Method, StatusCode, Uri};
use axum::routing::get;
use axum::{extract::DefaultBodyLimit, routing::post};
use axum::{http, Extension, Router, TypedHeader};
use bls12_381::G2Affine;
use log::{error, info};
use nostr_sdk::nostr::{key::FromSkStr, Keys};
use secp256k1::{All, Secp256k1};
Expand Down Expand Up @@ -58,7 +59,8 @@ pub struct State {
pub secp: Secp256k1<All>,
pub nostr: nostr_sdk::Client,
pub domain: String,
pub auth_pk: AggregatePublicKey,
pub free_pk: AggregatePublicKey,
pub paid_pk: AggregatePublicKey,
}

#[tokio::main]
Expand All @@ -82,9 +84,24 @@ async fn main() -> anyhow::Result<()> {
.await
.expect("should set up mints");

let auth_pk = std::env::var("AUTH_PK").expect("AUTH_PK must be set");
// no from_str impl so just decode from serde
let auth_pk: AggregatePublicKey = serde_json::from_str(&auth_pk).expect("Invalid AUTH_PK");
let free_pk = std::env::var("FREE_PK").expect("FREE_PK must be set");
let paid_pk = std::env::var("PAID_PK").expect("PAID_PK must be set");
let free_pk: AggregatePublicKey = AggregatePublicKey(
G2Affine::from_compressed(
hex::decode(&free_pk).expect("Invalid key hex")[..]
.try_into()
.expect("Invalid key byte key"),
)
.expect("Invalid FREE_PK"),
);
let paid_pk: AggregatePublicKey = AggregatePublicKey(
G2Affine::from_compressed(
hex::decode(&paid_pk).expect("Invalid key hex")[..]
.try_into()
.expect("Invalid key byte key"),
)
.expect("Invalid PAID_PK"),
);

// nostr
let nostr_nsec_str = std::env::var("NSEC").expect("FM_DB_PATH must be set");
Expand All @@ -108,7 +125,8 @@ async fn main() -> anyhow::Result<()> {
secp,
nostr,
domain,
auth_pk,
free_pk,
paid_pk,
};

// spawn a task to check for previous pending invoices
Expand Down Expand Up @@ -138,8 +156,8 @@ async fn main() -> anyhow::Result<()> {

let server_router = Router::new()
.route("/health-check", get(health_check))
.route("/check-username/:username", get(check_username))
.route("/register", post(register_route))
.route("/v1/check-username/:username", get(check_username))
.route("/v1/register", post(register_route))
.route("/.well-known/nostr.json", get(well_known_nip5_route))
.route(
"/.well-known/lnurlp/:username",
Expand Down
7 changes: 3 additions & 4 deletions src/models/app_user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,11 @@ impl AppUser {
== 0)
}

pub fn check_token_not_spent(conn: &mut PgConnection, msg: String) -> anyhow::Result<bool> {
pub fn get_by_token(conn: &mut PgConnection, msg: String) -> anyhow::Result<Option<AppUser>> {
Ok(app_user::table
.filter(app_user::unblinded_msg.eq(msg))
.count()
.get_result::<i64>(conn)?
== 0)
.first::<AppUser>(conn)
.optional()?)
}

pub fn get_by_pubkey(
Expand Down
Loading

0 comments on commit 886ea44

Please sign in to comment.