使用 javascript 创建 Ton 钱包时出现问题

问题描述 投票:0回答:1

我在使用 tonweb 库创建 Ton 钱包时遇到问题。 我尝试先生成私钥并从私钥获取公钥,然后从公钥获取地址钱包。 但地址钱包并不依赖于私钥!

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);
})();
javascript telegram ton
1个回答
0
投票

根据

tweetnacl
文档,不建议使用
fromSeed
函数。我已更改您的代码以仅使用推荐的
nacl.sign.keyPair()
函数,并且它可以按预期工作:

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);
})();

输出是 64 字节私钥,而不是之前的 32 字节私钥实现。

© www.soinside.com 2019 - 2024. All rights reserved.