Skip to content

Commit

Permalink
Merge pull request #30 from fedimint/back-to-objects
Browse files Browse the repository at this point in the history
  • Loading branch information
Kodylow authored Apr 19, 2024
2 parents 833e82e + cf20508 commit b5d358e
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 77 deletions.
12 changes: 6 additions & 6 deletions Cargo.lock

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

9 changes: 6 additions & 3 deletions fedimint-clientd/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "fedimint-clientd"
version = "0.3.1"
version = "0.3.2"
edition = "2021"
description = "A fedimint client daemon for server side applications to hold, use, and manage Bitcoin"
repository = "https://github.com/fedimint/fedimint-clientd"
Expand Down Expand Up @@ -31,13 +31,16 @@ tower-http = { version = "0.5.2", features = ["cors", "auth", "trace"] }
bitcoin = "0.29.2"
itertools = "0.12.0"
lnurl-rs = { version = "0.5.0", features = ["async"], default-features = false }
reqwest = { version = "0.12.3", features = ["json", "rustls-tls"], default-features = false }
reqwest = { version = "0.12.3", features = [
"json",
"rustls-tls",
], default-features = false }
lightning-invoice = { version = "0.26.0", features = ["serde"] }
bitcoin_hashes = "0.13.0"
time = { version = "0.3.25", features = ["formatting"] }
chrono = "0.4.31"
futures-util = "0.3.30"
clap = { version = "3", features = ["derive", "env"] }
multimint = { version = "0.3.0" }
multimint = { version = "0.3.2" }
# multimint = { path = "../multimint" }
axum-otel-metrics = "0.8.0"
1 change: 1 addition & 0 deletions fedimint-clientd/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub struct AppState {
impl AppState {
pub async fn new(fm_db_path: PathBuf) -> Result<Self> {
let clients = MultiMint::new(fm_db_path).await?;
clients.update_gateway_caches(true).await?;
Ok(Self {
multimint: clients,
cashu_mint: None,
Expand Down
2 changes: 1 addition & 1 deletion multimint/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "multimint"
version = "0.3.1"
version = "0.3.2"
edition = "2021"
description = "A library for managing fedimint clients across multiple federations"
license = "MIT"
Expand Down
14 changes: 14 additions & 0 deletions multimint/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ use fedimint_core::api::InviteCode;
use fedimint_core::config::{FederationId, FederationIdPrefix, JsonClientConfig};
use fedimint_core::db::Database;
use fedimint_core::Amount;
use fedimint_ln_client::LightningClientModule;
use fedimint_mint_client::MintClientModule;
use fedimint_wallet_client::WalletClientModule;
use tokio::sync::Mutex;
Expand Down Expand Up @@ -362,4 +363,17 @@ impl MultiMint {

Ok(info_map)
}

/// Update the gateway caches for all the lightning modules in the
/// multimint.
pub async fn update_gateway_caches(&self, apply_meta: bool) -> Result<()> {
let clients = self.clients.lock().await;

for (_, client) in clients.iter() {
let lightning_client = client.get_first_module::<LightningClientModule>();
lightning_client.update_gateway_cache(apply_meta).await?;
}

Ok(())
}
}
Binary file added wrappers/fedimint-ts/bun.lockb
Binary file not shown.
62 changes: 7 additions & 55 deletions wrappers/fedimint-ts/src/FedimintClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -392,18 +392,10 @@ export class FedimintClient {
* Creates a lightning invoice to receive payment via gateway
*/
createInvoice: async (
amountMsat: number,
description: string,
expiryTime?: number,
request: LightningInvoiceRequest,
gatewayId?: string,
federationId?: string
): Promise<LightningInvoiceResponse> => {
const request: LightningInvoiceRequest = {
amountMsat,
description,
expiryTime,
};

return await this.postWithGatewayIdAndFederationId<LightningInvoiceResponse>(
"/ln/invoice",
request,
Expand All @@ -425,22 +417,10 @@ export class FedimintClient {
* @param federationId - The ID of the federation to use for the invoice, if not provided will use the active federation ID
*/
createInvoiceForPubkeyTweak: async (
pubkey: string,
tweak: number,
amountMsat: number,
description: string,
expiryTime?: number,
request: LightningInvoiceExternalPubkeyTweakedRequest,
gatewayId?: string,
federationId?: string
): Promise<LightningInvoiceResponse> => {
const request: LightningInvoiceExternalPubkeyTweakedRequest = {
externalPubkey: pubkey,
tweak,
amountMsat,
description,
expiryTime,
};

return await this.postWithGatewayIdAndFederationId<LightningInvoiceExternalPubkeyTweakedResponse>(
"/ln/invoice-external-pubkey-tweaked",
request,
Expand All @@ -457,15 +437,9 @@ export class FedimintClient {
* @param federationId - The ID of the federation to claim the contracts in. Contracts are on specific federations so this is required.
*/
claimPubkeyTweakReceives: async (
privateKey: string,
tweaks: number[],
request: LightningClaimPubkeyTweakReceivesRequest,
federationId: string
): Promise<InfoResponse> => {
const request: LightningClaimPubkeyTweakReceivesRequest = {
privateKey,
tweaks,
};

return await this.postWithFederationId<InfoResponse>(
"/ln/claim-external-receive-tweaked",
request,
Expand Down Expand Up @@ -495,18 +469,10 @@ export class FedimintClient {
* Pays a lightning invoice or Lightningurl via a gateway
*/
pay: async (
paymentInfo: string,
amountMsat?: number,
LightningurlComment?: string,
request: LightningPayRequest,
gatewayId?: string,
federationId?: string
): Promise<LightningPayResponse> => {
const request: LightningPayRequest = {
paymentInfo,
amountMsat,
LightningurlComment,
};

return await this.postWithGatewayIdAndFederationId<LightningPayResponse>(
"/ln/pay",
request,
Expand All @@ -529,9 +495,7 @@ export class FedimintClient {
/**
* Decodes hex encoded binary ecash notes to json
*/
decodeNotes: async (
notes: string
): Promise<MintDecodeNotesResponse> => {
decodeNotes: async (notes: string): Promise<MintDecodeNotesResponse> => {
const request: MintDecodeNotesRequest = {
notes,
};
Expand Down Expand Up @@ -587,19 +551,9 @@ export class FedimintClient {
* @param federationId - The ID of the federation to spend the note in, if not provided will use the active federation ID
*/
spend: async (
amountMsat: number,
allowOverpay: boolean,
timeout: number,
includeInvite: boolean,
request: MintSpendRequest,
federationId?: string
): Promise<MintSpendResponse> => {
const request: MintSpendRequest = {
amountMsat,
allowOverpay,
timeout,
includeInvite,
};

return await this.postWithFederationId<MintSpendResponse>(
"/mint/spend",
request,
Expand Down Expand Up @@ -637,9 +591,7 @@ export class FedimintClient {
* Combines ecash notes into a single note string.
* @param notesVec - The notes to combine
*/
combine: async (
notesVec: string[]
): Promise<MintCombineResponse> => {
combine: async (notesVec: string[]): Promise<MintCombineResponse> => {
const request: MintCombineRequest = { notesVec };

return await this.post<MintCombineResponse>("/mint/combine", request);
Expand Down
56 changes: 44 additions & 12 deletions wrappers/fedimint-ts/tests/test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import dotenv from "dotenv";
import { randomBytes } from "crypto";
import * as secp256k1 from "secp256k1";
import { FedimintClientBuilder } from "../src";
import {
FedimintClientBuilder,
LightningClaimPubkeyReceiveTweakedRequest,
LightningInvoiceExternalPubkeyTweakedRequest,
LightningInvoiceRequest,
LightningPayRequest,
MintSpendRequest,
} from "../src";

dotenv.config();

Expand Down Expand Up @@ -98,29 +105,41 @@ async function main() {
logInputAndOutput({}, data);
// `/v2/ln/invoice`
logMethod("/v2/ln/invoice");
let lightningInvoiceRequest: LightningInvoiceRequest = {
amountMsat: 10000,
description: "test",
expiryTime: 3600,
};
let { operationId, invoice } = await fedimintClient.lightning.createInvoice(
10000,
"test"
lightningInvoiceRequest
);
logInputAndOutput(
{ amountMsat: 10000, description: "test" },
{ operationId, invoice }
);
// `/v2/ln/pay`
logMethod("/v2/ln/pay");
let payResponse = await fedimintClient.lightning.pay(invoice);
let lightningPayRequest: LightningPayRequest = {
paymentInfo: invoice,
};
let payResponse = await fedimintClient.lightning.pay(lightningPayRequest);
logInputAndOutput({ paymentInfo: invoice }, payResponse);
// `/v2/ln/await-invoice`
logMethod("/v2/ln/await-invoice");
data = await fedimintClient.lightning.awaitInvoice(operationId);
logInputAndOutput({ operationId }, data);
// `/v1/ln/invoice-external-pubkey-tweaked`
logMethod("/v1/ln/invoice-external-pubkey-tweaked");
let lightningInvoiceExternalPubkeyTweakedRequest: LightningInvoiceExternalPubkeyTweakedRequest =
{
externalPubkey: keyPair.publicKey,
tweak: 1,
amountMsat: 1000,
description: "test",
expiryTime: 3600,
};
data = await fedimintClient.lightning.createInvoiceForPubkeyTweak(
keyPair.publicKey,
1,
1000,
"test"
lightningInvoiceExternalPubkeyTweakedRequest
);
logInputAndOutput(
{
Expand All @@ -132,20 +151,33 @@ async function main() {
data
);
// pay the invoice
payResponse = await fedimintClient.lightning.pay(data.invoice);
let payRequest: LightningPayRequest = {
paymentInfo: data.invoice,
};
payResponse = await fedimintClient.lightning.pay(payRequest);
// `/v1/ln/claim-external-pubkey-tweaked`
logMethod("/v1/ln/claim-external-pubkey-tweaked");
let lightningClaimPubkeyTweakedRequest: LightningClaimPubkeyReceiveTweakedRequest =
{
privateKey: keyPair.privateKey,
tweaks: [1],
};
data = await fedimintClient.lightning.claimPubkeyTweakReceives(
keyPair.privateKey,
[1],
lightningClaimPubkeyTweakedRequest,
fedimintClient.getActiveFederationId()
);
logInputAndOutput({ privateKey: keyPair.privateKey, tweaks: [1] }, data);

// MINT METHODS
// `/v2/mint/spend`
logMethod("/v2/mint/spend");
let mintData = await fedimintClient.mint.spend(3000, true, 1000, false);
let spendRequest: MintSpendRequest = {
amountMsat: 3000,
allowOverpay: true,
timeout: 1000,
includeInvite: true,
};
let mintData = await fedimintClient.mint.spend(spendRequest);
logInputAndOutput(
{ amountMsat: 3000, allowOverpay: true, timeout: 1000 },
data
Expand Down

0 comments on commit b5d358e

Please sign in to comment.