skip to Main Content

I have problem to create Ton wallet with tonweb library.
I tried to generate private key first and get publickey from private key , then get address wallet from public key.
But the Address wallet is not depended on Private key!

const TonWeb = require("tonweb");
const nacl = require("tweetnacl");

(async () => {
  const tonwebInstance = new TonWeb();

  // Generate a random 32-byte private key
  const privateKey = nacl.randomBytes(32);
  const privateKeyHex = Buffer.from(privateKey).toString("hex");

  // Create a key pair from the private key
  const keyPair = nacl.sign.keyPair.fromSeed(privateKey);

  // Extract the public key from the key pair
  const publicKey = keyPair.publicKey;
  const publicKeyHex = Buffer.from(publicKey).toString("hex");

  // Create a wallet using the public key as Uint8Array
  const wallet = tonwebInstance.wallet.create({
    publicKey: publicKey,
  });

  // Get the wallet address
  const walletAddress = (await wallet.getAddress()).toString(true, true, true);

  console.log("Private key (hex):", privateKeyHex);
  console.log("Public key (hex):", publicKeyHex);
  console.log("Wallet address:", walletAddress);
})();

3

Answers


  1. According to the tweetnacl documentation, using the fromSeed function is not recommended. I’ve changed your code to simply use the recommended nacl.sign.keyPair() function and it works as desired:

    const TonWeb = require("tonweb");
    const nacl = require("tweetnacl");
    
    (async () => {
      const tonwebInstance = new TonWeb();
    
      // Create a key pair
      const keyPair = nacl.sign.keyPair();
    
      // Extract the public key from the key pair
      const publicKey = keyPair.publicKey;
      const publicKeyHex = Buffer.from(publicKey).toString("hex");
    
      // Extract the private key from the key pair
      const privateKey = keyPair.secretKey;
      const privateKeyHex = Buffer.from(privateKey).toString("hex");
    
      // Create a wallet using the public key as Uint8Array
      const wallet = tonwebInstance.wallet.create({publicKey});
    
      // Get the wallet address
      const walletAddress = (await wallet.getAddress()).toString(true, true, true);
    
      console.log("Wallet address:", walletAddress);
      console.log("Public key (hex):", publicKeyHex);
      console.log("Private key (hex):", privateKeyHex);
    })();
    

    The output is a 64-byte private key, as opposed to the previous implementation of a 32-byte private key.

    Login or Signup to reply.
  2. You can also try this to generate with mnemonic

    const createWallet = async () => {
        const tonweb = new TonWeb();
        const words = await generateMnemonic();
        const seed = await mnemonicToSeed(words);
        const keyPair = TonWeb.utils.nacl.sign.keyPair.fromSeed(seed);
        console.log(TonWeb.utils.bytesToHex(keyPair.secretKey));
    
        const WalletClass = tonweb.wallet.all.v3R2;
        const wallet = new WalletClass(tonweb.provider, {
            publicKey: keyPair.publicKey
        });
        const address = await wallet.getAddress();
        console.log(address.toString(true, true, false)); // print address in format to display in UI
    }
    
    Login or Signup to reply.
  3. @Aaron Meese‘s answer is correct.

    But I am adding this since you asked specifically that you need 32-byte of privateKey. By default, nacl.sign.keyPair() generates 64-byte of Combined Private Key.

    Combined Private Key (64-byte) = Seed (32-byte) + Public Key (32-byte)

    In case, you REALLY want to stick with 32-byte of privateKey, You have to slice it from the Combined Private Key.

    const combinedPrivateKey = keyPair.secretKey; // Seed + Public Key
    
    // Sling first 32 bytes which is Seed that can be used as Private Key
    const privateKey = combinedPrivateKey.slice(0, 32);
    const privateKeyHex = Buffer.from(privateKey).toString("hex");
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search