Skip to content
This repository has been archived by the owner on Feb 14, 2024. It is now read-only.

LedgerHQ/ledger-dotnet-api

Repository files navigation

Ledger Wallet Client API

NuGet

The is a .NET library to build application relying on the ledger Nano S.

  • Use the LedgerClient class for the Ledger Bitcoin App.
  • Use the U2FClient class for the U2F Ledger App.
  • Use a custom transport protocol to talk to your ledger (Example: having a HTTP proxy to talk to a remote ledger connected to your server)
  • You can easily build your own client for your custom App.
  • Support Segwit

Support only Windows for the time being. Supporting other plateform is theorically possible if you can compile hidapi library by yourself.

How to use ?

  1. Reference the nuget package in your project.
  2. Plug your ledger
  3. Open Bitcoin app

Then you can easily sign:

var ledger = LedgerClient.GetHIDLedgers().First();

var walletPubKey = ledger.GetWalletPubKey(new KeyPath("1'/0"));
var address1 = walletPubKey.Address.ScriptPubKey;
var walletPubKey2 = ledger.GetWalletPubKey(new KeyPath("1'/0"));
var address2 =walletPubKey2.Address.ScriptPubKey;

var changeAddress = (BitcoinAddress)ledger.GetWalletPubKey(new KeyPath("1'/1")).Address;

Transaction funding = new Transaction();
funding.AddInput(Network.Main.GetGenesis().Transactions[0].Inputs[0]);
funding.Outputs.Add(new TxOut(Money.Coins(1.1m), address1));
funding.Outputs.Add(new TxOut(Money.Coins(1.0m), address1));
funding.Outputs.Add(new TxOut(Money.Coins(1.2m), address2));

var coins = funding.Outputs.AsCoins();

var spending = new Transaction();
spending.LockTime = 1;
spending.Inputs.AddRange(coins.Select(o => new TxIn(o.Outpoint, Script.Empty)));
spending.Inputs[0].Sequence = 1;
spending.Outputs.Add(new TxOut(Money.Coins(0.5m), BitcoinAddress.Create("15sYbVpRh6dyWycZMwPdxJWD4xbfxReeHe")));
spending.Outputs.Add(new TxOut(Money.Coins(0.8m), changeAddress));
spending.Outputs.Add(new TxOut(Money.Zero, TxNullDataTemplate.Instance.GenerateScriptPubKey(new byte[] { 1, 2 })));


var requests = new SignatureRequest[]{
	new SignatureRequest()
	{
		InputCoin = new Coin(funding, 0),
		InputTransaction = funding,
		KeyPath = new KeyPath("1'/0")
	},
	new SignatureRequest()
	{
		InputCoin = new Coin(funding, 1),
		InputTransaction = funding,
		KeyPath = new KeyPath("1'/0")
	},
	new SignatureRequest()
	{
		InputCoin = new Coin(funding, 2),
		InputTransaction = funding,
		KeyPath = new KeyPath("1'/0")
	},
};

//should show 0.5 and 2.0 btc in fee
var signed = ledger.SignTransaction(requests, spending, new KeyPath("1'/1"));
Console.WriteLine(signed);
foreach(var req in requests)
{
    Console.WriteLine(req.Signature);
}

You can check the tests NanoSTests and U2FTests for additional informations.