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

actions: Add ledger backend functions. #3869

Merged
merged 1 commit into from
Sep 18, 2023

Conversation

JoeGruffins
Copy link
Member

part of #3865

@JoeGruffins

This comment was marked as outdated.

@JoeGruffins
Copy link
Member Author

I changed the helper method a bit so need to fix the tests then should be good.

@JoeGruffins JoeGruffins force-pushed the addledgeractions branch 3 times, most recently from 6e1d68c to aed9703 Compare June 2, 2023 08:29
@JoeGruffins JoeGruffins marked this pull request as ready for review June 2, 2023 08:36
Comment on lines 63 to 65
// bitshift right (>>>) does not seem to throw away the lower half, so
// subtracting.
const upper = n - lower;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wait, this doesnt work. hmm..

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Division with the max uint32 works correct? What is the correct syntax to bitshift right though? Is it not n>>32 or n>>>32?

Not using Buffer.writeUint64LE as this version of Buffer does not seem to have it.

app/helpers/ledger.js Outdated Show resolved Hide resolved
app/main_dev/ledger.js Outdated Show resolved Hide resolved
@JoeGruffins
Copy link
Member Author

@matheusd ok, I'll look into it...

@JoeGruffins
Copy link
Member Author

JoeGruffins commented Jun 23, 2023

@matheusd it looks like we might need electron/electron#36289

We are using whats in the package.json "electron": "18.3.7"? If so that release was tagged on Aug 4, 2022 so I guess it is not included. Not sure but will try updating the electron version as little as possible and see if it works.

And of course it breaks if I try to update.

@JoeGruffins
Copy link
Member Author

JoeGruffins commented Jun 25, 2023

Definitely working with electron v23.3.8. I guess I will put up a pr increasing the version and see what happens. #3884

@JoeGruffins
Copy link
Member Author

Now using "@ledgerhq/hw-transport-webusb" and everything looks alot better. Glad we took this route.

app/main.development.js Outdated Show resolved Hide resolved
// TODO: Enable on mainnet.
const isTestnet = true;
try {
const payload = await getAddress("44'/42'/0'");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of btc.getWalletPublicKey() (which is what this call cashes out into), maybe you could use btc.getWalletXpub()?

I haven't thoroughly checked the correctness of the next line's call to pubkeyToHDPubkey(), but I'd be very afraid a mistake in there is going to render the resulting wallet either unusable or incompatible to other software, making recovery of the funds using other software impossible.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Totally missed getWalletXpub, thank you. Now using that. It uses a sha256 checksum so replacing that.

Copy link
Member

@matheusd matheusd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dependencies look ok to me now!

@JoeGruffins
Copy link
Member Author

Comment on lines +15 to +18
// fixPubKeyChecksum replaces the sha256 checksum, or last four bytes, of a
// pubkey with a blake256 checksum.
export function fixPubKeyChecksum(pubKey) {
const buff = bs58.decode(pubKey).slice(0, -4);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will make it into a valid xpub, ok.

However, this means you can't go from seed to wallet using the standard decred software or my BIP0039 -> raw seed tool.

My concern here is that, if the user (say) gets its ledger stolen and wants to withdraw all funds as fast as possible using only software, there will be no fully compatible software to do so, because this particular path from mnemonic seed -> raw seed bytes -> account is not replicated on any software.

At the very least we'll need a tool that can go from the ledger mnemonic seed to either a raw seed or account xprivs that can be imported into decrediton/dcrwallet so that we can accurately recreate the wallet in software only if needed.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

However, this means you can't go from seed to wallet using the standard decred software or my BIP0039 -> raw seed tool.

Where was the tool again? In the decred repo? I'm unsure why it would not work but I trust you are correct.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@matheusd I was able to use the tool at https://github.com/matheusd/bip39-to-dcr-seed to restore a decred wallet from seed.

I made a mainnet wallet with decrediton and ledger. Send it a small amount of funds. After restoring using your tool, the seed words, and dcrwallet I was able to access the funds. It works!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I should mention that the tool will not work on testnet because the ledger testnet app forces cointype 42' although dcrwallet wants 1'. This is fine for mainnet though.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change "44'/42'/0'" to "44'/1'/0'"? Ledger will give me the error "Ledger device: Internal error, please report (0x6f15)".

@JoeGruffins
Copy link
Member Author

Copy link
Member

@matheusd matheusd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've been testing this for bit in both Linux and Windows, and while there are a few testing nits, this is good to go as a first pass implementation.

This is currently disabled in mainnet, so we'll keep iterating on it.

Comment on lines +87 to +90
if (numOuts > 2) {
throw "more than two outputs is not expected";
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's ok if we don't support more than two outputs in the first version, but we really should eventually support it (unless ledger itself limits this somehow).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

buff.set(writeUint16LE(out.version), i);
i += 2;
// TODO: Clean this up for production? Should use smarter logic to get varInts?
buff[i] = out.script.length; // varInt for 23/25 bytes
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, this won't be a problem for standard outputs, because the varints for 23/25 bytes is itself, but could be an issue in the future.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah. Leaving for now...

@alexlyp alexlyp merged commit 50de9f1 into decred:master Sep 18, 2023
1 check passed
alexlyp pushed a commit that referenced this pull request Sep 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants