novusnota / tonutils-dart

Comprehensive Dart SDK for interacting with TON Blockchain. When combined with Flutter can be used for any popular platform out there!
https://pub.dev/packages/tonutils
Apache License 2.0
18 stars 2 forks source link

Mnemonics to keypair makes screen lag #12

Open Milimeter opened 3 weeks ago

Milimeter commented 3 weeks ago

keyPair = Mnemonic.toKeyPair(mnemonics);

This makes the screen lag for like 6 seconds. Why

Milimeter commented 3 weeks ago

im using a 12 word multiseed wallet.

Everything works though. Just this is the issue

novusnota commented 3 weeks ago

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 :)

Milimeter commented 3 weeks ago

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 commented 3 weeks ago

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 commented 1 week ago

Could you perhaps add the workaround to the docs too?