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

Mnemonics to keypair makes screen lag #12

Open
Milimeter opened this issue Aug 22, 2024 · 5 comments
Open

Mnemonics to keypair makes screen lag #12

Milimeter opened this issue Aug 22, 2024 · 5 comments
Labels
bug Something isn't working

Comments

@Milimeter
Copy link

keyPair = Mnemonic.toKeyPair(mnemonics);

This makes the screen lag for like 6 seconds. Why

@Milimeter
Copy link
Author

im using a 12 word multiseed wallet.

Everything works though. Just this is the issue

@novusnota
Copy link
Owner

Thanks for reporting, that's a known issue though. One way to cope with it right now is to wrap it in an async call or put in another isolate process.

Going into the future this slow and blocking behavior has to be addressed, I'll consider the options :)

@novusnota novusnota added the bug Something isn't working label Aug 22, 2024
@Milimeter
Copy link
Author

i am trying to use the compute function but I have a error:
ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: Invalid argument(s): Illegal argument in isolate message: object is unsendable - Library:'dart:async' Class: _AsBroadcastStream@4048458 (see restrictions listed at SendPort.send() documentation for more information)
this is my code:

keyPair =
       await compute<List<String>, ton.KeyPair>(generateKeyPair, mnemonics);
ton.KeyPair generateKeyPair(List<String> mnemonics) {
   return ton.Mnemonic.toKeyPair(mnemonics);
 }

can you provide a working snippet, please

@Milimeter
Copy link
Author

Hi @novusnota
I found a workaround to generate the keypair by deriving the private key and then constructing the keypair from it. Here’s the integration I used:

final seed = bip39.mnemonicToSeed(mnemonic);
final wallet = bip32.BIP32.fromSeed(seed);
final pathWallet = wallet.derivePath('m/44'/607'/0'/0/0');
Uint8List privateKey32 = pathWallet.privateKey!;

// Create a 64-byte private key by hashing the private key and appending
Uint8List privateKey64 = Uint8List.fromList(privateKey32 + sha256.convert(privateKey32).bytes);

// Use this instead if you are using a 24-word seed phrase to generate your private key
// Uint8List privateKey64 = Uint8List.fromList(privateKey);

keyPair = ton.KeyPair.fromPrivateKey(privateKey64);
log("public key: ${keyPair.publicKey}");
log("private key: ${keyPair.privateKey}");

In this approach, I attempt to create a 64-byte private key by combining a 32-byte private key (privateKey32) with the SHA-256 hash of that same private key. The privateKey32 is derived from the BIP32 process, which typically produces a 32-byte private key suitable for various blockchain applications like Ethereum. However, this key is not directly suitable for TON, which requires a 64-byte private key.

The idea behind this approach is to generate a 64-byte sequence, which is necessary for some cryptographic libraries or blockchains, including TON. Since the original private key derived from BIP32 is only 32 bytes long, I appended its hash to extend it to the required 64 bytes.

While this method successfully creates a 64-byte private key, it's important to note that it may result in a different public key and address, as the original cryptographic relationship between the private key and public key is altered.

@Milimeter
Copy link
Author

Could you perhaps add the workaround to the docs too?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants