The is a .NET library to build application relying on the ledger Nano S.
LedgerClient
class for the Ledger Bitcoin App.U2FClient
class for the U2F Ledger App.Support only Windows for the time being. Supporting other plateform is theorically possible if you can compile hidapi library by yourself.
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.