Coder Social home page Coder Social logo

ledger-dotnet-api's Introduction

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.

ledger-dotnet-api's People

Contributors

danielcrenna avatar dependabot[bot] avatar mbalous avatar melbournedeveloper avatar nicolasdorier avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

ledger-dotnet-api's Issues

Ethereum app not working with api

Hi there.

I've noticed that the api runs into errors when trying to run the sample code with the Ethereum app opened instead of Bitcoin.

First off, I noticed that LedgerClient.GetHIDLedgers() returns a 0 length IEnumerable when the "Browser Support" setting is enabled on the Ledger Ethereum App. When Browser Support is disabled, I do end up getting a proper result with LedgerClient.GetHIDLedgers().First().

However, any call I make after that gets me a LedgerWalletException: INS not supported. Even a simple call where I can't really make any mistakes, such as ledger.GetFirmwareVersion() runs into the same exception.

When running this same code on the Bitcoin app for example, I get the proper firmware version and don't run into any exceptions.

This was tested using the latest firmware version of the Ledger.

Allow Unsafe Code

This library allows unsafe code:

image

This will be a problem for deploying apps to stores like the Windows Store, and Google Play store.

Unsafe code can be used to increase performance, but it allows direct access to memory, so many analyzers and so on may consider the library to be malware because it is not known what the app may be doing to memory. As a general rule unsafe code should only be used when there are performance issues with device communication, but seeing that device communication is not generally an issue because of the small amounts of transfer, I don't think there will be performance issues.

Signing tx with 3+ outputs fails

Hi, I forked the project and modified the CanSignTransactionStandardModeConcurrently in NanoSTests to have 3 outputs.

In test CanSignMultipleTransactionOutputs at ferib@8440518

When I sign the transaction, I see my Ledger X device prompt the suspicious path a couple of times, but that's it. I do not get any prompt to verify/sign each input & output. This problem only seems to happen when the output is 3 or more. e.g.:

// NOTE: having 3+ will promt for suspicious path, but won't sign anything :( 
            spending.Outputs.Add(new TxOut(Money.Coins(1m), BitcoinAddress.Create("bc1qc8h9tmkejfzzky79euxx5acmv9xthmcnk9df0m", Network.Main)));
            spending.Outputs.Add(new TxOut(Money.Coins(1m), BitcoinAddress.Create("bc1qve2w630azhahrhtmu047prnjjzxy2rymtd06na", Network.Main)));
            spending.Outputs.Add(new TxOut(Money.Coins(1m), BitcoinAddress.Create("bc1qdj59eexd2ggf4qa7u4n3fx9anurs5ad92d2jp3", Network.Main)));

When I comment some out, it all works fine

// NOTE: having 3+ will promt for suspicious path, but won't sign anything :( 
           spending.Outputs.Add(new TxOut(Money.Coins(1m), BitcoinAddress.Create("bc1qc8h9tmkejfzzky79euxx5acmv9xthmcnk9df0m", Network.Main)));
           //spending.Outputs.Add(new TxOut(Money.Coins(1m), BitcoinAddress.Create("bc1qve2w630azhahrhtmu047prnjjzxy2rymtd06na", Network.Main)));
           //spending.Outputs.Add(new TxOut(Money.Coins(1m), BitcoinAddress.Create("bc1qdj59eexd2ggf4qa7u4n3fx9anurs5ad92d2jp3", Network.Main)));

Exception when authenticating

I am trying to use the u2f API but I get command not allowed every 2nd call
eg.

            var appId = new AppId(Encoders.Hex.DecodeData("d2e42c173c857991d5e1b6c81f3e07cbb9d5f57431fe41997c9445c14ce61ec4"));
            var challenge = Encoders.Hex.DecodeData("e6425678fbd7d3d8e311fbfb1db8d26c37cf9f16ac81c95848998a76ce3d3768");
            U2FClient u2f = U2FClient.GetHIDU2F().First();


            // Accept registration
            var reg = u2f.Register(challenge, appId);


            // Accept registration
            try
            {
                u2f.Authenticate(challenge, appId, reg.KeyHandle);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                //            LedgerWallet.LedgerWalletException: Command not allowed: Conditions of use not satisfied
                //                at LedgerWallet.LedgerClientBase.Throw(Int32 sw)
                //            at LedgerWallet.LedgerClientBase.< ExchangeApdus > d__14.MoveNext()
                //                -- - End of stack trace from previous location where exception was thrown ---
                //                at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
                //            at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
                //            at LedgerWallet.U2F.U2FClient.< AuthenticateAsync > d__10.MoveNext()
                //                -- - End of stack trace from previous location where exception was thrown ---
                //                at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
                //            at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
                //            at LedgerWallet.U2F.U2FClient.Authenticate(Byte[] challenge, AppId applicationId, KeyHandle keyHandle, CancellationToken cancellationToken)
                //            at Ledger.Program.Main(String[] args) in C: \Users\Keza\Documents\Visual Studio 2017\Projects\Ledger\Ledger\Program.cs:line 28
            }

            var login = u2f.Authenticate(challenge, appId, reg.KeyHandle);

            Console.WriteLine(login);
            //            LedgerWallet.U2F.U2FAuthenticationResponse
            Console.WriteLine(login.UserPresence);
            //            False
            Console.ReadLine();

reading the u2f spec it seems to be the test of user presence returning false
what am I doing wrong?

Get Address

This may sound like a silly question, but which path does the Ledger Live app get the addresses from?

I've tried a few combinations in GetWalletPubKeyAsync, but none of the results match the addresses that I see in Ledger Live.

I tried "1'/0", and "0'/0" but neither of those match.

My assumption is that the Ledger app would use this for segwit addresses:
49'/0'/0'/ischange/index

Also, is it really necessary to get the full public key? Transfer between the Ledger and the computer is not encrypted, so the public key is exposed because other apps can sniff the transfer. It's not a security issue, but it is a privacy issue.

[Deleted]

Failing UUD register ot luad

// NOTE: having 3+ will promt for suspicious path, but won't sign anything :( 
            spending.Outputs.Add(new TxOut(Money.Coins(1m), BitcoinAddress.Create("bc1qc8h9tmkejfzzky79euxx5acmv9xthmcnk9df0m", Network.Main)));
            spending.Outputs.Add(new TxOut(Money.Coins(1m), BitcoinAddress.Create("bc1qve2w630azhahrhtmu047prnjjzxy2rymtd06na", Network.Main)));
            spending.Outputs.Add(new TxOut(Money.Coins(1m), BitcoinAddress.Create("bc1qdj59eexd2ggf4qa7u4n3fx9anurs5ad92d2jp3", Network.Main)));

Can legdet not accept more then 3 outputs? I get error after 2? plz need fix

LedgerWallet.LedgerWalletException : The dongle must be reinserted

Hi Nicolas

I am having LedgerWallet.LedgerWalletException : The dongle must be reinserted exception when i run the NanoSTests.CanSignTransactionStandardMode test method. I haven't modified test code and i am also using nano s ledger with latest firmware update.

i am also having LedgerWallet.LedgerWalletException : INS not supported on if i switch segwit parameter to false on first method call.

I simply connect my device and enter to Bitcoin wallet and run the test. It raise that exception without asking transaction confirmation.

Do you have an idea what can cause this issue ? You are not having exactly same issue on latest master commit, right ?

Check Current App

Is there a way to check what app is running on the device? More specifically which coin?

If not, is this a limitation of the firmware? Or is this just not implemented in C#?

Hardwarewallets.Net Feedback

Hey @NicolasDorier ,

Sorry about the lack of contribution lately. I'm still planning on finishing off the Android support, but I've been working on other projects and have very little time.

I'm currently working on https://github.com/MelbourneDeveloper/Hardwarewallets.Net

The idea of this library is on the base level to have a public interface that sits across all hardwarewallets and all coins on all platforms. Your library does not need to implement the public interface. I will include wrappers for the hardwarewallet libraries in the second tier of the library. The plan is to build up the interface bit by bit and slowly implement the interface for KeepKey, Trezor, and Ledger.

This is the main interface:
https://github.com/MelbourneDeveloper/Hardwarewallets.Net/blob/master/src/Hardwarewallets.Net.Base/IHardwarewalletManager.cs

You can already see the library in action with unit tests for Trezor and your library:
https://github.com/MelbourneDeveloper/Hardwarewallets.Net/tree/master/src/Hardwarewallets.Net.UnitTests

Basically just hoping to get your input on moving forward with this. I'd especially like your input on Bitcoin transaction signing. I'm hoping to also hear from @juanfranblanco on this. Please feel free to submit any pull requests if you think anything looks wrong.

Christian

Exchange MVC Projects Sign Transactions

Hi,

Is there any way to sign and create transaction on mvc server app when i use my ledger on my browser. i see these description but i dont know how we build up.

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)

Thanks,

Proper support for Altcoins?

Hi, I noticed the packges.config sets NBitcoin to 4.0.0.29?

I wanted to use NBitcoin.Altcoins and had my NBitcoin bumped up to the latest version as NBitcoin.Altcoins requires this. Doing so caused a lot of problems due to unimplemented methods.

Has there been any progress in the last couple of years to support a later version of NBitcoin? This would be great for altcoin support!

API for other cryptocurrencies

Hello,

Thank you for sharing the code, can you please also help with the code or documentation or way we can implement the same using API for the other available cryptocurrencies with ledger wallet.

I will look forward for your response as soon as possible from you as we are stuck with this since last 1 month and not able to find the solution.

MacOS/Linux Support

As far as I understand, it only builds on windows? Are there any plans to support other OS?

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.