-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from wbobeirne/wo/channel-api
- Loading branch information
Showing
4 changed files
with
112 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,34 @@ | ||
``` | ||
# MutinyNet Faucet API | ||
|
||
1. Copy `.env.sample` to `.env.local` and fill it out with bitcoind and lnd connection info | ||
2. Run `cargo build && cargo start` | ||
|
||
## Endpoint examples | ||
|
||
```sh | ||
curl -X POST \ | ||
http://localhost:3001/api/onchain \ | ||
-H 'Content-Type: application/json' \ | ||
-d '{"sats":10000,"address":"bcrt1..."}' | ||
``` | ||
|
||
``` | ||
```sh | ||
curl -X POST \ | ||
http://localhost:3001/api/lightning \ | ||
-H 'Content-Type: application/json' \ | ||
-d '{"bolt11": "..."}' | ||
``` | ||
|
||
``` | ||
```sh | ||
curl -X POST \ | ||
http://localhost:3001/api/bolt11 \ | ||
-H 'Content-Type: application/json' \ | ||
-d '{"amount_sats": 1234}' | ||
``` | ||
``` | ||
|
||
```sh | ||
curl -X POST \ | ||
http://localhost:3001/api/channel \ | ||
-H 'Content-Type: application/json' \ | ||
-d '{"capacity": 2468,"push_amount": 1234,"pubkey":"023...","host":"127.0.0.1:9735"}' | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
use serde::{Deserialize, Serialize}; | ||
|
||
use std::sync::{Arc, Mutex}; | ||
use tonic_openssl_lnd::lnrpc::{self, channel_point}; | ||
|
||
use crate::{AppState, MAX_SEND_AMOUNT}; | ||
|
||
#[derive(Clone, Deserialize)] | ||
pub struct ChannelRequest { | ||
capacity: i64, | ||
push_amount: i64, | ||
pubkey: String, | ||
host: Option<String>, | ||
} | ||
|
||
#[derive(Clone, Serialize)] | ||
pub struct ChannelResponse { | ||
pub txid: String, | ||
} | ||
|
||
pub async fn open_channel( | ||
state: Arc<Mutex<AppState>>, | ||
payload: ChannelRequest, | ||
) -> anyhow::Result<String> { | ||
if payload.capacity > MAX_SEND_AMOUNT.try_into().unwrap() { | ||
anyhow::bail!("max capacity is 10,000,000"); | ||
} | ||
if payload.push_amount < 0 { | ||
anyhow::bail!("push_amount must be positive"); | ||
} | ||
if payload.push_amount > payload.capacity { | ||
anyhow::bail!("push_amount must be less than or equal to capacity"); | ||
} | ||
|
||
let node_pubkey_result = hex::decode(payload.pubkey.clone()); | ||
let node_pubkey = match node_pubkey_result { | ||
Ok(pubkey) => pubkey, | ||
Err(e) => anyhow::bail!("invalid pubkey: {}", e), | ||
}; | ||
|
||
let channel_point = { | ||
let mut lightning_client = state | ||
.clone() | ||
.lock() | ||
.map_err(|_| anyhow::anyhow!("failed to get lock"))? | ||
.lightning_client | ||
.clone(); | ||
|
||
if let Some(host) = payload.host { | ||
lightning_client | ||
.connect_peer(lnrpc::ConnectPeerRequest { | ||
addr: Some(lnrpc::LightningAddress { | ||
pubkey: payload.pubkey.clone(), | ||
host, | ||
}), | ||
..Default::default() | ||
}) | ||
.await | ||
.ok(); | ||
} | ||
|
||
lightning_client | ||
.open_channel_sync(lnrpc::OpenChannelRequest { | ||
node_pubkey, | ||
local_funding_amount: payload.capacity, | ||
push_sat: payload.push_amount, | ||
..Default::default() | ||
}) | ||
.await? | ||
.into_inner() | ||
}; | ||
|
||
let txid = match channel_point.funding_txid { | ||
Some(channel_point::FundingTxid::FundingTxidBytes(bytes)) => hex::encode(bytes), | ||
Some(channel_point::FundingTxid::FundingTxidStr(string)) => string, | ||
None => anyhow::bail!("failed to open channel"), | ||
}; | ||
|
||
Ok(txid) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters