Coder Social home page Coder Social logo

melbournedeveloper / trezor.net Goto Github PK

View Code? Open in Web Editor NEW
43.0 6.0 13.0 2.53 MB

Cross platform C# library for talking to the Trezor hardwarewallet

License: MIT License

C# 100.00%
trezor uwp trezor-hardwarewallets android cryptocurrency hid csharp bitcoin ethereum bitcoin-wallet

trezor.net's Introduction

Trezor.Net

Cross Platform C# Library for the Trezor Cryptocurrency Hardwarewallet

This library allows you to communicate with both Trezor hardwarewallets in the same way that the Trezor browser wallet app communicates with them. It can be used to build apps that send or receive crypto currencies like Bitcoin in a secure way.

Currently supports: .NET Framework, .NET Core, Android, UWP , See MacOS and Linux Support

Would you like to contribute?

Quick Start

  • Clone the repo and open the solution
  • There is a console sample, Xamarin Forms sample and unit tests for UWP, and .NET
  • Compile one of the unit test apps, run the UWP/Android Xamarin Forms apps or,
  • Go to Test->Windows->Text Explorer in Visual Studio
  • Run one of the unit tests in the pane.
  • Note that the UWP unit test has a UI for entering the pin. Please read instructions there.

All Trezor messages are in the Trezor.Net.Contracts namespace. To implement them, you need to call SendMessageAsync

NuGet: Install-Package Trezor.Net

Example:

public async Task<string> GetAddressAsync()
{
    //Register the factory for creating Usb devices. Trezor One Firmware 1.7.x, 1.8.x / Trezor Model T 2.1.x
    WindowsUsbDeviceFactory.Register();
    //Register the factory for creating Hid devices. Trezor One Firmware 1.6.x
    WindowsHidDeviceFactory.Register();

    var trezorManagerBroker = new TrezorManagerBroker(GetPin, 2000, new DefaultCoinUtility());

    var trezorManager =  await trezorManagerBroker.WaitForFirstTrezorAsync();

    var bip44AddressPath = AddressPathBase.Parse<BIP44AddressPath>("m/49'/0'/0'/0/0");

    return await trezorManager.GetAddressAsync(bip44AddressPath, false, true);
}

Contact

The community needs your help! Unit tests, integration tests, more app integrations and bug fixes please! Check out the Issues section.

Donate

All my libraries are open source and free. Your donations will contribute to making sure that these libraries keep up with the latest firmware, functions are implemented, and the quality is maintained.

Coin Address
Bitcoin 33LrG1p81kdzNUHoCnsYGj6EHRprTKWu3U
Ethereum 0x7ba0ea9975ac0efb5319886a287dcf5eecd3038e

Based On

Library Description
Hardwarewallets.Net This library is part of the Hardwarewallets.Net suite. It is aimed toward putting a set of common C# interfaces, and utilities that will work with all hardwarewallets.
Hid.Net, Usb.Net Trezor.Net communicates with the devices via the Hid.Net and Usb.Net libraries. You can see the repo for this library here.

See Also

Library Description
KeepKey.Net KeepKey Hardwarewallet Library
Ledger.Net Ledger Hardwarewallet Library
Ledger .NET API A similar Ledger library
Ledger Bitcoin App Bitcoin wallet application for Ledger Blue and Nano S
Ledger Ethereum App Ethereum wallet application for Ledger Blue and Nano S

Hardfolio - Store App Production Usage

https://play.google.com/store/apps/details?id=com.Hardfolio (Android)

https://www.microsoft.com/en-au/p/hardfolio/9p8xx70n5d2j (UWP)

trezor.net's People

Contributors

matejcik avatar melbournedeveloper avatar mtleliever avatar raystonn avatar www 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

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

trezor.net's Issues

Model T Support / Trezor One Firmware 1.7.1

I think it would be easy to implement Model T support but I'm poor, so if you have a spare one, send it my way!

Otherwise, could someone please run the unit tests with a model T?

Unit Testing

There is a sample app which behaves as a unit test, but it's not a proper unit test. The problem with Trezor is that it requires you to look at the screen and use human intervention to type the pin, so no unit test can really run in isolation (well not without a web cam and OCR of the Trezor screen...)

Still, unit tests are better than a console app.

Ethereum transaction signing issues

In the current state of the Trezor.Net library, there are some very strange results when attempting to sign an Ethereum transaction.

This is the code I am using to try and sign a transaction:

[TestMethod]
public async Task SignEthereumTransaction()
{
    await GetAndInitialize();

    //Note: these are not reasonable values. They should not be used for a transaction. Looking for a better example here...
    var txMessage = new EthereumSignTx
    {
        Nonce = 0.ToHexBytes(),
        GasPrice = 1000000000.ToHexBytes(),
        GasLimit = 21000.ToHexBytes(),
        To = "689c56aef474df92d44a1b70850f808488f9769c".ToHexBytes(),
        Value = 100000000000000.ToHexBytes(),
        AddressNs = ManagerHelpers.GetAddressPath(false, 0, false, 0, 60),
        ChainId = 4
    };

    var transaction = await TrezorManager.SendMessageAsync<EthereumTxRequest, EthereumSignTx>(txMessage);

    Assert.AreEqual(transaction.SignatureR.Length, 32);
    Assert.AreEqual(transaction.SignatureS.Length, 32);
}

The transaction signing goes wrong in a few different areas.

Firstly, when attempting to sign a transaction on the Rinkeby network through https://mycrypto.com, we can see the output on our device as something related to the following:

Send 1.0 tEth to 0x689c56aef474df92d44a1b70850f808488f9769c?

After confirming this, we get the next message:

Really send 1.0 tEth paying up to 0.000021 tEth for gas?

When attempting to sign the same transaction through Trezor.Net, we get the something similar to the following:

Send message to 0x689c56aef474df92d44a1b70850f808488f97600

After confirming this, we'll get this next message:

Really send message paying up to 0.000021 UNKN for gas?

The first bit of strange behavior is with the fact that even though we are sending an EthereumSignTx, the Trezor is interpreting it as signing a message through EthereumSignMessage. The text 'Send Message' should only really appear if we are signing a message through EthereumSignMessage.

The second bit of strange behavior is seen in the actual address. The address where the message is being sent to the device in its entirety it seems. Sometimes only the last few characters of the address will be '0', sometimes it will be many more.

The last bit of strange behavior is that the Trezor doesn't seem to recognize the ChainCode that we have entered, and therefore states we are trying pay the transaction fee with currency UNKN.

I'm not really sure where the issue lies in all of this, but clearly something is up.

Get Latest Protobuffers

Use Marc Gravell's protobuf generator to pump out the latest messages from the Trezor definition files.

Abstracting HID transport

It would be nice if TrezorManager did not depend on IHidDevice, but by a similar class than Ledger API (similar to ILedgerTransport)

I would like to use this with my custom made transport for BTCPay. (Where the browser is connected to Trezor, and I want to pilot it from my backend via websocket)

Usb.Net / Trezor - Error Code 121 - ERROR_SEM_TIMEOUT

Every second or third time you connect to the Trezor you get an Error Code 121 on one of the calls. This usually happens with GetDescriptor. If you remove any calls to GetDescriptor, it will just hang. This is also happening with WinUSBNet. I'm not sure if this is a Trezor specific problem, or something that both Usb.Net and WinUSBNet are both doing wrong...

Is there some handle or something not being disposed?

After latest Trezor firmware update, not possible to sign TX.


Edited by Christian

@ep0x is correct. The latest version of the firmware is incompatible with the current version of Trezor.Net

I've tried a few times to get it working. You can see my very poor attempt here:

//This code is converted from Trezor.py here:

Which comes from a python example here:
https://github.com/trezor/python-trezor/blob/2813522b05cef4e0e545a101f8b3559a3183b45b/trezorlib/btc.py#L161

If you want to have a go at this, I recommend joining the Trezor dev community on gitter.

This is some documentation about signing transactions:
https://wiki.trezor.io/Developers_guide:Message_Workflows#SignTx


We have problem to sign tx for both models.
Far as i inspected issue, i found :
While sending TxRequest to Trezor One, new requirement is needed such as TxRequest.RequestType.Txmeta ( TXMETA).

Also while doing debugging in our service we noticed when we send TxRequest to device, before device ask us for TXMETA data, now in request.Details.TxHash is not empty like before. There is new things happening now that we need to change.

Now we need to verify prevHas or something. I did a bit inspection in https://github.com/trezor/trezor-firmware/blob/82c0c403677a14bde56c1a6b440a46593a57267d/python/src/trezorlib/btc.py and i found out there is changes in code for txmeta and tx.bin.output added python: Add get_ownership_id() and get_ownership_proof() to trezorlib.

In TrezorLib, inputs value(amount) is not mandatory now if i understand python correctly...

def from_json(json_dict):
    def make_input(vin):
        i = messages.TxInputType()
        if "coinbase" in vin:
            i.prev_hash = b"\0" * 32
            i.prev_index = 0xFFFFFFFF  # signed int -1
            i.script_sig = bytes.fromhex(vin["coinbase"])
            i.sequence = vin["sequence"]

        else:
            i.prev_hash = bytes.fromhex(vin["txid"])
            i.prev_index = vin["vout"]
            i.script_sig = bytes.fromhex(vin["scriptSig"]["hex"])
            i.sequence = vin["sequence"]

        return i

    def make_bin_output(vout):
        o = messages.TxOutputBinType()
        o.amount = int(Decimal(vout["value"]) * (10 ** 8))
        o.script_pubkey = bytes.fromhex(vout["scriptPubKey"]["hex"])
        return o

    t = messages.TransactionType()
    t.version = json_dict["version"]
    t.lock_time = json_dict.get("locktime")
    t.inputs = [make_input(vin) for vin in json_dict["vin"]]
    t.bin_outputs = [make_bin_output(vout) for vout in json_dict["vout"]]
    return t

About TX_META

  
def copy_tx_meta(tx):
        tx_copy = messages.TransactionType(**tx)
        # clear fields
        tx_copy.inputs_cnt = len(tx.inputs)
        tx_copy.inputs = []
        tx_copy.outputs_cnt = len(tx.bin_outputs or tx.outputs)
        tx_copy.outputs = []
        tx_copy.bin_outputs = []
        tx_copy.extra_data_len = len(tx.extra_data or b"")
        tx_copy.extra_data = None
        return tx_copy


if res.request_type == R.TXMETA:
            msg = copy_tx_meta(current_tx)
            res = client.call(messages.TxAck(tx=msg))

And here is trick with request for TXOUTPUT. Now he is checking if there is request.Details.TxHash and if is has is presented then bin_outputs must be set...

elif res.request_type == R.TXOUTPUT:
            msg = messages.TransactionType()
            if res.details.tx_hash:
                msg.bin_outputs = [current_tx.bin_outputs[res.details.request_index]]
            else:
                msg.outputs = [current_tx.outputs[res.details.request_index]]

            res = client.call(messages.TxAck(tx=msg))


In Trezor Model T there is error in firmware message when you try to sign TX.

I am willing to help but idk from where to start...
I noticed there is a lot problems with new firmware for many wallets and services like Wasabi and BtcPayServer, but idk did he fixed issue, he used HWI for his Wasabi project.
I am willing to help if i can in anyway.
It would be awesome if we can join all together and maintain one Lib for Trezor instead 5 different libs at once and having the same issue...

I'd be glad if we could join this project, our respected colleagues like @NicolasDorier and @lontivero @bitcoinbrisbane
Any help are welcome!

Thank you in advance.

Model T UWP Support

Support for Model T can be achieved via LibUsbDotNet (see Windows unit tests, and #4).

However, there is currently no library to fill the hole for UWP. This is because the Model T does not support Hid. A raw Usb implementation needs to be written for UWP. Here is an issue logged in Usb.Net regarding this problem:
https://github.com/MelbourneDeveloper/Usb.Net/issues/2

TrezorManagerBroker

Leverage Hid.Net/Usb.Net 's new DeviceListener functionality to listen for connections and automatically handle connection/disconnection.

Remove ByteBuffer

This is a conversion from Android Java and needs to go in the bin

Android Usb.Net NuGet Dependency

Android doesn't depend on Usb.Net

There should be a platform independent dependency for Usb.Net so that Android downloads it.

Trezor.Net with LibUsb getting an error while reading response from a device

Trezor.Net does not work properly with LibUsb on either Windows or Linux (mono) because the behavior of IDevice.ReadAsync differs from the native implementation, resulting in an error:
An error occurred while attempting to read the message from the device. The last written message was a ButtonAck. In the first chunk of data the first byte was not 63the second byte was not 35the third byte was not 35

When using LibUsb, the ReadAsync method does not wait for data to be received and immediately returns a response with BytesTransferred property value of zero.

I can create a PR for either Trezor.Net or/and Device.Net.LibUsb (probably this would be a better approach) if that makes sense given the project is stalled.

Info

Platform: Windows / Linux(mono)
Device.Net 4.2.1
Device.Net.LibUsb 4.2.1
LibUsbDotNet 2.2.29
Trezor.Net 5.0.0-beta

Trezor.Sample stuck at Device.ReadAsync in TrezorManagerBase.cs

hi author
I still stuck at Device.ReadAsync in TrezorManagerBase.cs with follow information
device: Trezor One
firmware: latest
I just run Trezor.Net.Sample, the program hang at "Waiting for Trezor... Please plug it in if it is not connected"
I have plugged it in, and when i use trezorctl or Trezor Bridge to check, both of tools works. The device is ok.

In the Trezor.Net.Sample program, i change netcoreapp2.0 to netcoreapp 3.1

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.