Skip to content

Commit

Permalink
Add pigeon (#94)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrei-21 authored Feb 23, 2024
1 parent 67e3c7e commit 0f40319
Show file tree
Hide file tree
Showing 8 changed files with 226 additions and 3 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ members = [
"crow",
"graphql",
"honey-badger",
"squirrel",
"parrot",
"pigeon",
"squirrel",
]

resolver = "2"
7 changes: 7 additions & 0 deletions graphql/schemas/operations.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -157,3 +157,10 @@ mutation ReportPaymentTelemetry($telemetryId: String!, $events: PaymentTelemetry
payFailed
}
}

mutation AssignLightningAddress {
assign_lightning_address {
address
assignedAt
}
}
117 changes: 117 additions & 0 deletions graphql/schemas/schema_wallet_read.graphql
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Updated at 2024-02-23_11:14:36
# Hasura Role: WALLET_READ

schema {
query: query_root
mutation: mutation_root
Expand Down Expand Up @@ -65,6 +68,11 @@ type GetTermsConditionsStatusResponse {
version: Int!
}

type LightningAddressResponse {
address: String!
assignedAt: DateTime!
}

type MigrationBalanceResponse {
balanceAmountSat: BigInteger!
}
Expand Down Expand Up @@ -584,6 +592,59 @@ enum cursor_ordering {
DESC
}

"""
columns and relationships of "lightning_address"
"""
type lightning_address {
address: String!
assignedAt: timestamptz!
}

"""
Boolean expression to filter rows from the table "lightning_address". All fields are combined with a logical 'AND'.
"""
input lightning_address_bool_exp {
_and: [lightning_address_bool_exp!]
_not: lightning_address_bool_exp
_or: [lightning_address_bool_exp!]
address: String_comparison_exp
assignedAt: timestamptz_comparison_exp
}

"""Ordering options when selecting data from "lightning_address"."""
input lightning_address_order_by {
address: order_by
assignedAt: order_by
}

"""
select columns of table "lightning_address"
"""
enum lightning_address_select_column {
"""column name"""
address

"""column name"""
assignedAt
}

"""
Streaming cursor of the table "lightning_address"
"""
input lightning_address_stream_cursor_input {
"""Stream column input with initial value"""
initial_value: lightning_address_stream_cursor_value_input!

"""cursor ordering"""
ordering: cursor_ordering
}

"""Initial value of the column from where the streaming should start"""
input lightning_address_stream_cursor_value_input {
address: String
assignedAt: timestamptz
}

"""mutation root"""
type mutation_root {
"""
Expand Down Expand Up @@ -612,6 +673,7 @@ type mutation_root {
): accepted_terms_conditions
accept_terms_conditions(args: ServiceProviderInputInput): AcceptTermsResponse
accept_wallet_acl_by_pk(pk_columns: AcceptWalletPkRequestInput!): WalletAcl
assign_lightning_address: LightningAddressResponse
create_backup(encryptedBackup: String!, schemaName: String!, schemaVersion: String!): CreateBackupResponse
hide_topup(id: String!): String

Expand Down Expand Up @@ -762,6 +824,27 @@ type query_root {
currencyCode: String!
): currency
get_terms_conditions_status(args: GetTermsConditionsStatusInputInput): GetTermsConditionsStatusResponse

"""
fetch data from the table: "lightning_address"
"""
lightning_address(
"""distinct select on columns"""
distinct_on: [lightning_address_select_column!]

"""limit the number of rows returned"""
limit: Int

"""skip the first n rows. Use only with order_by"""
offset: Int

"""sort the rows by one or more columns"""
order_by: [lightning_address_order_by!]

"""filter the rows returned"""
where: lightning_address_bool_exp
): [lightning_address!]!
lightning_address_service_version: String
migration_balance(nodePubKey: String): MigrationBalanceResponse
notification_service_version: String
payment_service_version: String
Expand Down Expand Up @@ -966,6 +1049,40 @@ type subscription_root {
where: currency_bool_exp
): [currency!]!

"""
fetch data from the table: "lightning_address"
"""
lightning_address(
"""distinct select on columns"""
distinct_on: [lightning_address_select_column!]

"""limit the number of rows returned"""
limit: Int

"""skip the first n rows. Use only with order_by"""
offset: Int

"""sort the rows by one or more columns"""
order_by: [lightning_address_order_by!]

"""filter the rows returned"""
where: lightning_address_bool_exp
): [lightning_address!]!

"""
fetch data from the table in a streaming manner: "lightning_address"
"""
lightning_address_stream(
"""maximum number of rows returned in a single batch"""
batch_size: Int!

"""cursor to stream the results returned by the query"""
cursor: [lightning_address_stream_cursor_input]!

"""filter the rows returned"""
where: lightning_address_bool_exp
): [lightning_address!]!

"""
fetch data from the table: "token"
"""
Expand Down
4 changes: 2 additions & 2 deletions graphql/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ pub fn build_async_client(access_token: Option<&str>) -> Result<reqwest::Client>

pub fn post_blocking<Query: graphql_client::GraphQLQuery>(
client: &Client,
backend_url: &String,
backend_url: &str,
variables: Query::Variables,
) -> Result<Query::ResponseData> {
let response = match post_graphql_blocking::<Query, _>(client, backend_url, variables) {
Expand All @@ -83,7 +83,7 @@ pub fn post_blocking<Query: graphql_client::GraphQLQuery>(

pub async fn post<Query: graphql_client::GraphQLQuery>(
client: &reqwest::Client,
backend_url: &String,
backend_url: &str,
variables: Query::Variables,
) -> Result<Query::ResponseData> {
let response = match post_graphql::<Query, _>(client, backend_url, variables).await {
Expand Down
8 changes: 8 additions & 0 deletions graphql/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,3 +173,11 @@ pub struct RecoverBackup;
response_derives = "Debug"
)]
pub struct ReportPaymentTelemetry;

#[derive(GraphQLQuery)]
#[graphql(
schema_path = "schemas/schema_wallet_read.graphql",
query_path = "schemas/operations.graphql",
response_derives = "Debug"
)]
pub struct AssignLightningAddress;
14 changes: 14 additions & 0 deletions pigeon/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "pigeon"
version = "0.1.0"
edition = "2021"

[dependencies]
graphql = { path = "../graphql" }
honey-badger = { path = "../honey-badger" }

[dev-dependencies]
bitcoin = { version = "0.30.1" }
ctor = "0.2.0"
simplelog = { version ="0.12.0", features = ["test"] }
tokio = { version = "1.32.0" }
20 changes: 20 additions & 0 deletions pigeon/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use graphql::perro::OptionToError;
use graphql::schema::{assign_lightning_address, AssignLightningAddress};
use graphql::{build_async_client, post};
use honey_badger::asynchronous::Auth;

pub async fn assign_lightning_address(backend_url: &str, auth: &Auth) -> graphql::Result<String> {
let token = auth.query_token().await?;
let client = build_async_client(Some(&token))?;
let data = post::<AssignLightningAddress>(
&client,
backend_url,
assign_lightning_address::Variables {},
)
.await?;
let address = data
.assign_lightning_address
.ok_or_permanent_failure("Unexpected backend response: empty")?
.address;
Ok(address)
}
56 changes: 56 additions & 0 deletions pigeon/tests/integration_tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
use bitcoin::Network;
use honey_badger::asynchronous::Auth;
use honey_badger::secrets::{derive_keys, generate_keypair, generate_mnemonic};
use honey_badger::AuthLevel;
use pigeon::assign_lightning_address;
use simplelog::TestLogger;
use std::env;
use std::sync::Once;

static INIT_LOGGER_ONCE: Once = Once::new();

#[cfg(test)]
#[ctor::ctor]
fn init() {
INIT_LOGGER_ONCE.call_once(|| {
TestLogger::init(simplelog::LevelFilter::Info, simplelog::Config::default()).unwrap();
});
}

#[tokio::test]
async fn test_assigning_lightning_address() {
let (backend_url, auth) = build_client();
let address = assign_lightning_address(&backend_url, &auth).await.unwrap();
println!("Assigned address is: {address}");
assert!(!address.is_empty());
let address_from_another_call = assign_lightning_address(&backend_url, &auth).await.unwrap();
assert_eq!(address, address_from_another_call);

let (backend_url, another_auth) = build_client();
let address_for_another_user = assign_lightning_address(&backend_url, &another_auth)
.await
.unwrap();
assert_ne!(address, address_for_another_user);
}

fn build_client() -> (String, Auth) {
println!("Generating keys ...");
let mnemonic = generate_mnemonic();
println!("mnemonic: {mnemonic:?}");
let wallet_keys = derive_keys(Network::Testnet, mnemonic).wallet_keypair;
let auth_keys = generate_keypair();

let auth = Auth::new(
get_backend_url(),
AuthLevel::Pseudonymous,
wallet_keys,
auth_keys,
)
.unwrap();

(get_backend_url(), auth)
}

fn get_backend_url() -> String {
env::var("GRAPHQL_API_URL").expect("GRAPHQL_API_URL environment variable is not set")
}

0 comments on commit 0f40319

Please sign in to comment.