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

How do I get TON's private key and public key? #11

Open
ronnieedev opened this issue Feb 21, 2025 · 2 comments
Open

How do I get TON's private key and public key? #11

ronnieedev opened this issue Feb 21, 2025 · 2 comments

Comments

@ronnieedev
Copy link

ronnieedev commented Feb 21, 2025

Hi, I don't have enough knowledge about blockchain.

Can I get TON's public key and private key only with the package function here??
And TonSeed Generator takes about a minute to generate.

@mrtnetwork
Copy link
Owner

Hi, I don't have enough knowledge about blockchain.

Can I get TON's public key and private key only with the package function here?? And TonSeed Generator takes about a minute to

Hi, TON uses PBKDF2 to generate the seed from the mnemonic. If the mnemonic fails validation checks, a new one will be automatically generated. This process may repeat multiple times until a valid mnemonic is created.

  final mnemonic = TonMnemonicGenerator().fromWordsNumber(12);
  final seed = TonSeedGenerator(mnemonic).generate();
  final key = Bip44.fromSeed(seed, Bip44Coins.tonMainnet);
  final privateKey = key.privateKey;
  final publicKey = privateKey.publicKey;

@ronnieedev
Copy link
Author

ronnieedev commented Feb 21, 2025

@mrtnetwork Thank you
I knew what the problem was.
If you know anything, can you help me?
I need to convert EVM 12 words mnemonic to TON mnemonic and make an address
I have an encoder, but it's not the feature I want.

E/flutter (17293): [ERROR:flutter/runtime/dart_vm_initializer.cc(40)] Unhandled Exception: MnemonicException(Invalid Ton mnemonic.)

Future<Map<String, dynamic>> _generateBIP44Ton(
    List<String> mnemonic,
    Bip44Coins coinType,
  ) async {
    final tonAddrEncoder = TonAddrEncoder();
    final bip39MnemonicDecoder = Bip39MnemonicDecoder();

    final entropyBytes = bip39MnemonicDecoder.decode(mnemonic.join(' '));
    final Uint8List expandedEntropy = Uint8List.fromList(
      QuickCrypto.sha512Hash(entropyBytes).sublist(0, 32),
    );

    final tonMnemonic = TonMnemonicGenerator().fromEntropy(expandedEntropy);

    final seed = TonSeedGenerator(
      tonMnemonic,
    ).generate(
      validateTonMnemonic: true,
    );
    final wallet = Bip44.fromSeed(seed, coinType);
    final privateKey = wallet.privateKey;
    final publicKey = privateKey.publicKey;

    final parsedAddress = tonAddrEncoder.encodeKey(
      publicKey.compressed,
      Bip44Conf.tonMainnet.addrParams,
    );
    final parsedPrivateKey = privateKey.privKey.toHex(prefix: '0x');

    return {
      "privateKey": parsedPrivateKey,
      "address": parsedAddress,
    };
  }

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

No branches or pull requests

2 participants