Giter Site home page Giter Site logo

nlightning's Introduction

NLightning

NLightning is a library to connect, synchronize, send and receive payments with the lightning network.

Current implementation uses bitcoind (with -txindex) as backend.

โš  NLightning is in an early development stage, do not use with real funds!

Features:

  • DNS bootstrap
  • Download and synchronize network view
  • Open channels
  • Mutual and unilateral closing of channels
  • Handle penalty and remote unilateral close
  • Send and receive payments
  • SPV wallet support (BIP 157, BIP 158)
  • Backup channels
  • Support watchtowers

Supported platforms:

  • .NET Core (Linux, MacOS, Windows)

Not yet supported:

  • Xamarin iOS
  • Xamarin Android

Dependencies:

  • NBitcoin
  • EFCore
  • Portable.BouncyCastle
  • System.Reactive
  • Microsoft Extensions (Logging)
  • DnsClient
  • xUnit, Moq

Usage

Configuration

ECKeyPair localLightningKey = new ECKeyPair("<node private key>", true);
ECKeyPair walletKey = new ECKeyPair("<wallet private key>", true);

LightningNode node = new LightningNode(localLightningKey, walletKey, NetworkParameters.BitcoinTestnet);

node.ConfigureServices(services =>
{
    // by default NLightning loads the configuration from a nlightning.json file located in the working directory.
    // if you want to load your own configuration instead add it to the services:
    // services.AddSingleton(new ConfigurationBuilder()
    //                       .SetBasePath(Directory.GetCurrentDirectory())
    //                       .AddJsonFile("my-config.json", true, true));
        
    // we use bitcoind (with -txindex) as backend
    services.AddSingleton<IBlockchainClientService, BitcoindClientService>();
    
    // configure logging
    services.AddLogging(logging => logging.SetMinimumLevel(LogLevel.Debug)
                                          .AddConsole(options => options.IncludeScopes = false)
                                          .AddDebug()
                                          .AddFilter("Microsoft.EntityFrameworkCore", LogLevel.Warning));
    
    // configure network db (gossip data)
    // we support sqlite, but others should work as well
    services.AddDbContext<NetworkPersistenceContext>(options => options.UseSqlite("Data Source=network.db"));
    
    // configure local db (channel and peer data) 
    // private keys are stored here
    services.AddDbContext<LocalPersistenceContext>(options => options.UseSqlite("Data Source=local.db"));
});

// Start all services.
node.Initialize();

Configuration Entities: BitcoindClientConfiguration, BlockchainConfiguration, ChannelConfiguration, NetworkViewConfiguration, PeerConfiguration

Connect to a node

IPeerService peerService = node.Services.GetService<IPeerService>();
NodeAddress nodeAddress = NodeAddress.Parse("<publickey@ip:port>");
IPeer peer = peerService.AddPeer(nodeAddress, persist: true, reconnect: true);

Open a channel

var channelEstablishmentService = node.Services.GetService<IChannelEstablishmentService>();
channelEstablishmentService.SuccessProvider.Subscribe(peerAndChannel =>
{
    // $"Opened a channel (ID: {peerAndChannel.Channel.ChannelId} with peer {peerAndChannel.Peer.NodeAddress}";  
});

// Open a channel with a funding of 25000 Satoshis
channelEstablishmentService.OpenChannel(peer, 25000);

Get all active channels and updates

IChannelService channelService = node.Services.GetService<IChannelService>();
var activeChannels = channelService.Channels.Where(c => c.Active);
var channelStateService = node.Services.GetService<IChannelStateService>();
channelStateService.ChannelActiveStateChangedProvider
    .Where(c => c.Active)
    .Subscribe(channel =>
    {
        // Channel is active
    });

Close a channel

IChannelCloseService closeService = node.Services.GetService<IChannelCloseService>();

// mutual close, if peer is not available do an unilateral close:
closeService.Close(channel, unilateralCloseOnUnavailability: true);

// unilateral close:
closeService.UnilateralClose(channel);

Get all channels and nodes in the network

var networkViewService = node.Services.GetService<INetworkViewService>();
var networkChannels = networkViewService.View.GetChannels();
var networkNodes = networkViewService.View.GetNodes();

nlightning's People

Contributors

dmsch avatar knocte avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.