Skip to content

Commit

Permalink
WIP: connect VLS in local mode
Browse files Browse the repository at this point in the history
  • Loading branch information
devrandom committed Jun 27, 2022
1 parent ad4f320 commit de061f9
Show file tree
Hide file tree
Showing 10 changed files with 186 additions and 95 deletions.
19 changes: 10 additions & 9 deletions Cargo.lock

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

15 changes: 11 additions & 4 deletions senseicore/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,22 +46,29 @@ anyhow = "1.0"

[dependencies.lightning-signer-core]
git = "https://gitlab.com/lightning-signer/validating-lightning-signer.git"
rev = "b2f478ec263c5c9ecbd1795c5f234613cfa67ffa"
rev = "5a7f67dbf0245f35b61342d62b99ddedbc6243c8"
#path = "../../vls/lightning-signer-core"

[dependencies.lightning-signer-server]
git = "https://gitlab.com/lightning-signer/validating-lightning-signer.git"
rev = "5a7f67dbf0245f35b61342d62b99ddedbc6243c8"
#path = "../../vls/lightning-signer-server"
default_features = false
features = ["persist_kv_json"]

[dependencies.vls-protocol-signer]
git = "https://gitlab.com/lightning-signer/validating-lightning-signer.git"
rev = "b2f478ec263c5c9ecbd1795c5f234613cfa67ffa"
rev = "5a7f67dbf0245f35b61342d62b99ddedbc6243c8"
#path = "../../vls/vls-protocol-signer"

[dependencies.vls-protocol-client]
git = "https://gitlab.com/lightning-signer/validating-lightning-signer.git"
rev = "b2f478ec263c5c9ecbd1795c5f234613cfa67ffa"
rev = "5a7f67dbf0245f35b61342d62b99ddedbc6243c8"
#path = "../../vls/vls-protocol-client"

[dependencies.vls-proxy]
git = "https://gitlab.com/lightning-signer/validating-lightning-signer.git"
rev = "b2f478ec263c5c9ecbd1795c5f234613cfa67ffa"
rev = "5a7f67dbf0245f35b61342d62b99ddedbc6243c8"
#path = "../../vls/vls-proxy"
default-features = false
features = ["grpc"]
Expand Down
14 changes: 11 additions & 3 deletions senseicore/src/channels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ use crate::error::Error;
use crate::node::{connect_peer_if_necessary, parse_peer_addr, parse_pubkey, PeerManager};
use crate::services::node::OpenChannelRequest;
use crate::{chain::database::WalletDatabase, events::SenseiEvent, node::ChannelManager};
use bdk::{FeeRate, SignOptions};
use bdk::FeeRate;
use lightning::chain::chaininterface::ConfirmationTarget;
use rand::{thread_rng, Rng};
use std::sync::{Arc, Mutex};
use std::time::Duration;
use tokio::sync::broadcast;
use vls_protocol_client::{DynKeysInterface, SpendableKeysInterface};

pub struct EventFilter<F>
where
Expand All @@ -26,9 +27,11 @@ pub struct ChannelOpener {
event_receiver: broadcast::Receiver<SenseiEvent>,
broadcaster: Arc<SenseiBroadcaster>,
peer_manager: Arc<PeerManager>,
keys_manager: Arc<DynKeysInterface>,
}

impl ChannelOpener {
#[allow(clippy::too_many_arguments)]
pub fn new(
node_id: String,
channel_manager: Arc<ChannelManager>,
Expand All @@ -37,6 +40,7 @@ impl ChannelOpener {
event_receiver: broadcast::Receiver<SenseiEvent>,
broadcaster: Arc<SenseiBroadcaster>,
peer_manager: Arc<PeerManager>,
keys_manager: Arc<DynKeysInterface>,
) -> Self {
Self {
node_id,
Expand All @@ -46,6 +50,7 @@ impl ChannelOpener {
event_receiver,
broadcaster,
peer_manager,
keys_manager,
}
}

Expand Down Expand Up @@ -185,6 +190,7 @@ impl ChannelOpener {
let wallet = self.wallet.lock().unwrap();

let mut tx_builder = wallet.build_tx();
tx_builder.version(2);
let fee_sats_per_1000_wu = self
.chain_manager
.fee_estimator
Expand Down Expand Up @@ -223,9 +229,11 @@ impl ChannelOpener {
return Err(Error::Bdk(e));
}

let (mut psbt, _tx_details) = tx_result.unwrap();
let (psbt, _tx_details) = tx_result.unwrap();

let _finalized = wallet.sign(&mut psbt, SignOptions::default()).unwrap();
// let _finalized = wallet.sign(&mut psbt, SignOptions::default()).unwrap();
let derivations = (0..psbt.inputs.len()).map(|_| 0).collect::<Vec<_>>();
let psbt = self.keys_manager.sign_from_wallet(&psbt, derivations);
let funding_tx = psbt.extract_tx();

let channels_to_open = requests_with_results
Expand Down
5 changes: 3 additions & 2 deletions senseicore/src/event_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,22 @@ use entity::sea_orm::ActiveValue;
use lightning::chain::chaininterface::BroadcasterInterface;
use lightning::routing::gossip::NodeId;
use lightning::{
chain::{chaininterface::ConfirmationTarget, keysinterface::KeysManager},
chain::chaininterface::ConfirmationTarget,
util::events::{Event, EventHandler, PaymentPurpose},
};
use rand::{thread_rng, Rng};
use std::sync::Mutex;
use std::{sync::Arc, time::Duration};
use tokio::runtime::Handle;
use tokio::sync::broadcast;
use vls_protocol_client::{DynKeysInterface, SpendableKeysInterface};

pub struct LightningNodeEventHandler {
pub node_id: String,
pub config: Arc<SenseiConfig>,
pub wallet: Arc<Mutex<bdk::Wallet<WalletDatabase>>>,
pub channel_manager: Arc<ChannelManager>,
pub keys_manager: Arc<KeysManager>,
pub keys_manager: Arc<DynKeysInterface>,
pub database: Arc<SenseiDatabase>,
pub chain_manager: Arc<SenseiChainManager>,
pub tokio_handle: Handle,
Expand Down
51 changes: 20 additions & 31 deletions senseicore/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use crate::services::node::{
OpenChannelResult, Peer, Utxo,
};
use crate::services::{PaginationRequest, PaginationResponse, PaymentsFilter};
use crate::signer::get_keys_manager;
use crate::utils::PagedVec;
use crate::{hex_utils, version};
use bdk::keys::ExtendedKey;
Expand All @@ -45,10 +46,10 @@ use bitcoin::hashes::hex::ToHex;
use bitcoin::hashes::sha256::Hash as Sha256;
use bitcoin::network::constants::Network;
use bitcoin::secp256k1::{PublicKey, Secp256k1};
use bitcoin::util::bip32::{ChildNumber, DerivationPath, ExtendedPrivKey};
use bitcoin::util::bip32::{ChildNumber, DerivationPath};
use bitcoin::BlockHash;
use lightning::chain::chainmonitor;
use lightning::chain::keysinterface::{InMemorySigner, KeysInterface, KeysManager, Recipient};
use lightning::chain::keysinterface::{KeysInterface, Recipient};
use lightning::chain::Watch;
use lightning::chain::{self, Filter};
use lightning::ln::channelmanager::{self, ChannelDetails, ChannelManager as LdkChannelManager};
Expand Down Expand Up @@ -85,6 +86,7 @@ use std::{convert::From, fmt};
use tokio::runtime::Handle;
use tokio::sync::broadcast;
use tokio::task::JoinHandle;
use vls_protocol_client::{DynKeysInterface, DynSigner};

#[derive(Serialize, Debug)]
pub struct LocalInvoice {
Expand Down Expand Up @@ -293,7 +295,7 @@ pub type GossipSync<P, G, A, L> =
lightning_background_processor::GossipSync<P, Arc<RapidGossipSync<G, L>>, G, A, L>;

pub type ChainMonitor = chainmonitor::ChainMonitor<
InMemorySigner,
DynSigner,
Arc<dyn Filter + Send + Sync>,
Arc<SenseiBroadcaster>,
Arc<SenseiFeeEstimator>,
Expand All @@ -304,7 +306,7 @@ pub type ChainMonitor = chainmonitor::ChainMonitor<
trait MustSized: Sized {}

pub type SimpleArcChannelManager<M, T, F, L> =
LdkChannelManager<InMemorySigner, Arc<M>, Arc<T>, Arc<KeysManager>, Arc<F>, Arc<L>>;
LdkChannelManager<DynSigner, Arc<M>, Arc<T>, Arc<DynKeysInterface>, Arc<F>, Arc<L>>;

pub type SimpleArcPeerManager<SD, M, T, F, L> = LdkPeerManager<
SD,
Expand Down Expand Up @@ -338,7 +340,7 @@ pub type InvoicePayer = payment::InvoicePayer<

#[allow(dead_code)]
pub type SyncableMonitor = (
ChannelMonitor<InMemorySigner>,
ChannelMonitor<DynSigner>,
Arc<SenseiBroadcaster>,
Arc<SenseiFeeEstimator>,
Arc<FilesystemLogger>,
Expand All @@ -347,6 +349,8 @@ pub type SyncableMonitor = (
pub type NetworkGraphMessageHandler =
P2PGossipSync<Arc<NetworkGraph>, Arc<dyn chain::Access + Send + Sync>, Arc<FilesystemLogger>>;

// FIXME remove
#[allow(unused)]
fn get_wpkh_descriptors_for_extended_key(
xkey: ExtendedKey,
network: Network,
Expand Down Expand Up @@ -408,7 +412,7 @@ pub struct LightningNode {
pub peer_manager: Arc<PeerManager>,
pub network_graph: Arc<NetworkGraph>,
pub network_graph_msg_handler: Arc<NetworkGraphMessageHandler>,
pub keys_manager: Arc<KeysManager>,
pub keys_manager: Arc<DynKeysInterface>,
pub logger: Arc<FilesystemLogger>,
pub invoice_payer: Arc<InvoicePayer>,
pub scorer: Arc<Mutex<Scorer>>,
Expand Down Expand Up @@ -533,14 +537,6 @@ impl LightningNode {
.map_err(|_e| Error::InvalidMacaroon)
}

pub fn get_node_pubkey_from_seed(seed: &[u8; 32]) -> String {
let secp_ctx = Secp256k1::new();
let keys_manager = KeysManager::new(seed, 0, 0);
let node_secret = keys_manager.get_node_secret(Recipient::Node).unwrap();
let node_pubkey = PublicKey::from_secret_key(&secp_ctx, &node_secret);
node_pubkey.to_string()
}

#[allow(clippy::too_many_arguments)]
pub async fn new(
config: Arc<SenseiConfig>,
Expand All @@ -563,23 +559,15 @@ impl LightningNode {
LightningNode::get_seed_for_node(id.clone(), passphrase.clone(), database.clone())
.await?;

let cur = SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)
let (manager, xpub) = get_keys_manager("local", network, data_dir.clone())
.await
.unwrap();
let keys_manager = Arc::new(DynKeysInterface::new(manager));

let keys_manager = Arc::new(KeysManager::new(&seed, cur.as_secs(), cur.subsec_nanos()));

let xprivkey = ExtendedPrivKey::new_master(network, &seed).unwrap();
let xkey = ExtendedKey::from(xprivkey);
let native_segwit_base_path = "m/84";
let account_number = 0;
let (receive_descriptor_template, change_descriptor_template) =
get_wpkh_descriptors_for_extended_key(
xkey,
network,
native_segwit_base_path,
account_number,
);
let secp = Secp256k1::new();
let pub0 = xpub.ckd_pub(&secp, ChildNumber::from(0)).unwrap().to_pub();
let receive_descriptor_template = bdk::descriptor!(wpkh(pub0)).unwrap();
let change_descriptor_template = bdk::descriptor!(wpkh(pub0)).unwrap();

let bdk_database = WalletDatabase::new(id.clone(), database.clone(), database.get_handle());
let wallet_database = bdk_database.clone();
Expand Down Expand Up @@ -1026,6 +1014,7 @@ impl LightningNode {
self.event_sender.subscribe(),
self.broadcaster.clone(),
self.peer_manager.clone(),
self.keys_manager.clone(),
);
opener.open_batch(requests).await
}
Expand Down Expand Up @@ -1081,12 +1070,12 @@ impl LightningNode {
Ok(())
}

async fn keysend<K: KeysInterface>(
async fn keysend(
&self,
invoice_payer: &InvoicePayer,
payee_pubkey: PublicKey,
amt_msat: u64,
keys: &K,
keys: &DynKeysInterface,
) -> Result<(), Error> {
let payment_preimage = keys.get_secure_random_bytes();

Expand Down
9 changes: 7 additions & 2 deletions senseicore/src/services/admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use crate::events::SenseiEvent;
use crate::network_graph::SenseiNetworkGraph;
use crate::{config::SenseiConfig, hex_utils, node::LightningNode, version};

use crate::signer::get_keys_manager;
use entity::node::{self, NodeRole};
use entity::sea_orm::{ActiveModelTrait, ActiveValue, EntityTrait};
use entity::{access_token, seconds_since_epoch};
Expand Down Expand Up @@ -563,9 +564,13 @@ impl AdminService {

// NODE DIRECTORY
let node_directory = format!("{}/{}/{}", self.data_dir, self.config.network, node_id);
fs::create_dir_all(node_directory)?;
fs::create_dir_all(node_directory.clone())?;

let (manager, _xpub) = get_keys_manager("local", self.config.network, node_directory)
.await
.unwrap();
// NODE SEED
// FIXME unused now
let seed = LightningNode::generate_seed();
let encrypted_seed = LightningNode::encrypt_seed(&seed, passphrase.as_bytes())?;

Expand All @@ -574,7 +579,7 @@ impl AdminService {
.get_seed_active_model(node_id.clone(), encrypted_seed);

// NODE PUBKEY
let node_pubkey = LightningNode::get_node_pubkey_from_seed(&seed);
let node_pubkey = manager.get_node_id().to_string();

// NODE MACAROON
let (macaroon, macaroon_id) = LightningNode::generate_macaroon(&seed, node_pubkey.clone())?;
Expand Down
Loading

0 comments on commit de061f9

Please sign in to comment.