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
According to the
tweetnacl
documentation, using thefromSeed
function is not recommended. I’ve changed your code to simply use the recommendednacl.sign.keyPair()
function and it works as desired:The output is a 64-byte private key, as opposed to the previous implementation of a 32-byte private key.
You can also try this to generate with mnemonic
@Aaron Meese‘s answer is correct.
But I am adding this since you asked specifically that you need
32-byte
ofprivateKey
. By default,nacl.sign.keyPair()
generates64-byte
of Combined Private Key.In case, you REALLY want to stick with
32-byte
ofprivateKey
, You have toslice
it from the Combined Private Key.