-1

I'm building a C# HID library for the Trezor. It's working well. I'm able to get past the pin entry and retrieve my xpub, and I can get addresses. However, none of the addresses that are getting returned match any of my addresses in the Trezor wallet website.

You can see the HID doco here: https://doc.satoshilabs.com/trezor-tech/api-workflows.html#passphrase-meta-workflow

This is not really a C# question. Rather, it's a general question for any Trezor HID developers. The big problem is that if I pass a HDNodePathType message as the Multisig property of the GetAddress method, I get the error "Can't encode address'". What do I need to pass with the GetAddress message to get a valid address?

Here is an Android repo: https://github.com/trezor/trezor-android

Juan Leni
  • 6,982
  • 5
  • 55
  • 87
Christian Findlay
  • 6,770
  • 5
  • 51
  • 103
  • Typically, the path to your first address should be 4 bytes longer than the path to your `xpub`. Typical `xpub` path would be `m/44'/0'/0'`, where as a typical address path would look like `m/44'/0'/0'/0/0` . And `xpub` creates the full address as something like `xpub/0/0` . So... are you certain that ensure the paths you are creating on C# are byte identical to the ones in the website. The easiest low-level Trezor testing can be done using the python library. – Dan Dec 07 '18 at 17:04
  • Also... ensure that you can get an address that matches the web-wallet using the python library. Then work on debugging the paths you feed into python to try to match the paths you get in C#. One concievable bug would be an unwanted string escape of the char `'` or `/`. I assume this has been checked. BTW... You can `load-device` with an `xprv` key. This may help in correlating path and xpub. – Dan Dec 07 '18 at 17:04

1 Answers1

0

This problem has now been resolved. Trezor.Net has a working example can be cloned here.

Here is the code for getting an address:

public async Task<string> GetAddressAsync(IAddressPath addressPath, bool isPublicKey, bool display, AddressType addressType, InputScriptType inputScriptType, string coinName)
{
    try
    {
        var path = addressPath.ToArray();

        if (isPublicKey)
        {
            var publicKey = await SendMessageAsync<PublicKey, GetPublicKey>(new GetPublicKey { CoinName = coinName, AddressNs = path, ShowDisplay = display, ScriptType = inputScriptType });
            return publicKey.Xpub;
        }
        else
        {
            switch (addressType)
            {
                case AddressType.Bitcoin:

                    //Ultra hack to deal with a coin name change in Firmware Version 1.6.2
                    if ((Features.MajorVersion <= 1 && Features.MinorVersion < 6) && coinName == "Bgold")
                    {
                        coinName = "Bitcoin Gold";
                    }

                    return (await SendMessageAsync<Address, GetAddress>(new GetAddress { ShowDisplay = display, AddressNs = path, CoinName = coinName, ScriptType = inputScriptType })).address;

                case AddressType.Ethereum:

                    var ethereumAddress = await SendMessageAsync<EthereumAddress, EthereumGetAddress>(new EthereumGetAddress { ShowDisplay = display, AddressNs = path });

                    var sb = new StringBuilder();
                    foreach (var b in ethereumAddress.Address)
                    {
                        sb.Append(b.ToString("X2").ToLower());
                    }

                    var hexString = sb.ToString();

                    return $"0x{hexString}";
                default:
                    throw new NotImplementedException();
            }
        }
    }
    catch (Exception ex)
    {
        Logger?.Log("Error Getting Trezor Address", LogSection, ex, LogLevel.Error);
        throw;
    }
}
Christian Findlay
  • 6,770
  • 5
  • 51
  • 103