Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Failed to sign PSBT using ledger #64

Closed
thomaseizinger opened this issue Aug 11, 2020 · 19 comments
Closed

Failed to sign PSBT using ledger #64

thomaseizinger opened this issue Aug 11, 2020 · 19 comments
Labels
bug Something isn't working ledger Ledger-related issues

Comments

@thomaseizinger
Copy link
Contributor

I've been building a PoC for using this lib to sign PSBTs and I've run into an issue that I can't resolve.

My PoC is here: https://github.com/thomaseizinger/rust-ledger-poc
You can run the shell script with a ledger plugged in and it should error with:

Error: failed to get signatures from device

Caused by:
    Ledger device: APDU Response error `Code 6985 ([APDU_CODE_CONDITIONS_NOT_SATISFIED] Conditions of use not satisfied)`

Any ideas of what could be the issue?

@prestwich
Copy link
Member

What version of the ledger firmware and ledger Bitcoin app are you on? There have been some recent changes to the necessary apdus

@thomaseizinger
Copy link
Contributor Author

thomaseizinger commented Aug 11, 2020

I was on 1.5.5 and upgraded to 1.6.0 yesterday, however that didn't change anything. Same error in both cases.

@prestwich
Copy link
Member

are you signing witness or legacy inputs? My legacy support is spotty at the moment

@thomaseizinger
Copy link
Contributor Author

thomaseizinger commented Aug 11, 2020

I skimmed the APDU code and tried to cross reference with the spec but I didn't get very far.

Some things I noticed:

@thomaseizinger
Copy link
Contributor Author

thomaseizinger commented Aug 11, 2020

are you signing witness or legacy inputs? My legacy support is spotty at the moment

All native segwit unless I messed it up somewhere :D
But the inputs are all funded to bech32 addresses and I am spending to bech32 as well.

@prestwich
Copy link
Member

0x80 is usually a continuation flag. I'll try to take some time tomorrow to dive into this

@thomaseizinger
Copy link
Contributor Author

thomaseizinger commented Aug 11, 2020

I tried to set the PoC up in a "nice" way :)
You should be able to run it (see the included shell script) end to end with the ledger plugged in if that helps with anything!

@prestwich
Copy link
Member

The psbt crate has a basic ledger signer that I have run a few times. It's overdue for a refactor to deprecate the txin witness (see #44)

@thomaseizinger
Copy link
Contributor Author

Ah I missed that one, I was using the psbt stuff from rust-bitcoin 😅

Will give that a go and see if it works!

@prestwich
Copy link
Member

its probably riddled with bugs 👌

@thomaseizinger
Copy link
Contributor Author

I've pushed a branch here: https://github.com/thomaseizinger/rust-ledger-poc/tree/use-bitcoins-psbt

Unfortunately, I can't even parse the psbt from base64 😬
It fails with: Error: failed to fill whole buffer

That is the psbt I used:

cHNidP8BAHECAAAAAeBANSdI+VT5VJvVfchN4UEUniZ5cfeucBkBuoA475wjAAAAAAD+////AgDh9QUAAAAAFgAU7gEhvO/VGbeMDvk2DeqaTVkRQh8AERAkAQAAABYAFCQ8xyUkB4v4DqmV7T6aVADqs8M5AAAAAAABAR8A8gUqAQAAABYAFO4BIbzv1Rm3jA75Ng3qmk1ZEUIfIgYDbXrhM7lpiaTJhxwJSplsX1r33gCcoD9xL4wEteLypE8YRwNsJ1QAAIABAACAAAAAgAAAAAAAAAAAACICA2164TO5aYmkyYccCUqZbF9a994AnKA/cS+MBLXi8qRPGEcDbCdUAACAAQAAgAAAAIAAAAAAAAAAAAAiAgONam8JJOdoEr/jubocGRelQAnn2NfLVM7jLliPK0n8KBhHA2wnVAAAgAEAAIAAAACAAQAAAAAAAAAA`

I also needed to fix another compile error 😅 #65

@prestwich prestwich added the bug Something isn't working label Aug 11, 2020
@prestwich
Copy link
Member

failed to fill whole buffer

this would be a (de)serialization issue. I have narrowed it down to an issue with the input bip32 derivation deserialization

@prestwich
Copy link
Member

prestwich commented Aug 11, 2020

the schema was passing an incorrect length to the KeyDerivation deserialization. Fixed in 5668e6a

@prestwich
Copy link
Member

I'll check your example routines when I get a second.

Typically the 6985 error is triggered here: Code. It often means there was some missing information and a signature was requested before the device was ready to produce it.

@thomaseizinger
Copy link
Contributor Author

I'll check your example routines when I get a second.

Typically the 6985 error is triggered here: Code. It often means there was some missing information and a signature was requested before the device was ready to produce it.

Thanks! I also stumbled over that code path in the source code but I couldn't really conclude on what to do differently as a result 😅

I will try again with the fix when I get around to it :)

@prestwich
Copy link
Member

okay, I started a new job so progress on this issue has been slow. I pushed a branch that fixes osx compilation errors afaict

@thomaseizinger
Copy link
Contributor Author

Congratulations! :)

That is okay, we ended up going back to using JavaScript and the ledgerjs package for what we are currently trying to achieve. I am still curious on what the issue is though!

Note that I also opened LedgerHQ/app-bitcoin#164 although looking at the other issues, the Ledger team doesn't seem to give too much attention to the OSS community 🙃

@prestwich
Copy link
Member

if you're in JS, the WASM target uses the ledger JS transports and the PSBT should be able to sign with it. I still need to check your example and see where the bug is

@prestwich
Copy link
Member

dropping this here for more digging

out.log

@prestwich prestwich added the ledger Ledger-related issues label Aug 22, 2020
@prestwich prestwich added this to the v1.0.0 milestone Aug 25, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working ledger Ledger-related issues
Projects
None yet
Development

No branches or pull requests

2 participants