createMint 函数调用,无需在客户端公开密钥对

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

我想要一个从前端创建 spl 令牌的解决方案,因为我看到许多解决方案的密钥对在前端公开或生成随机密钥对,但当我切换到带或不带锚的幻影钱包连接时,没有结果。功能不仅仅是一个展示品。而且没有任何用处。

我想要一个关于与带锚或不带锚的幻影钱包连接的解决方案工作解决方案,不会在前端暴露密钥对或在前端随机生成密钥对..需要最佳解决方案

solana-web3js spl anchor-solana
1个回答
0
投票

您可以使用幻影钱包在前端创建铸币账户。

try {
      const { solana } = window;
      if (!solana || !solana.isPhantom)
        throw new Error("Phantom wallet not found");

      if (!walletConnected) {
        throw new Error("Wallet is not connected");
      }
      const connection = new Connection(clusterApiUrl("devnet"), "confirmed");
      const phantomPublicKey = new PublicKey(publicKey!);
      const mintAccount = Keypair.generate();
      const tx = new Transaction();
      const lamports = await getMinimumBalanceForRentExemptMint(connection);
      tx.instructions = [
        SystemProgram.createAccount({
          fromPubkey: phantomPublicKey,
          newAccountPubkey: mintAccount.publicKey,
          lamports,
          space: MINT_SIZE,
          programId: TOKEN_PROGRAM_ID,
        }),
        createInitializeMint2Instruction(
          mintAccount.publicKey,
          6,
          phantomPublicKey,
          phantomPublicKey,
          TOKEN_PROGRAM_ID
        ),
      ];
      const blockhash = (await connection.getLatestBlockhash()).blockhash;
      tx.recentBlockhash = blockhash;
      tx.feePayer = phantomPublicKey;
      const signedTx = await solana.signAndSendTransaction(tx);

      // Check if signature exists
      const { signature } = signedTx;
      console.log("Signature:", signature);
      const transactionConfirmation = await connection.confirmTransaction(
        signature
      );
      console.log("Transaction confirmed", transactionConfirmation);
    } catch (error) {
      console.error("Transaction failed:", error.message || error);
    }
  };
© www.soinside.com 2019 - 2024. All rights reserved.