如何在没有私钥更新元数据的情况下从前端签署 solana web3 交易?

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

我使用 Metaplex 将其元数据设置为已创建的令牌。 前端是 Angular,我使用的钱包连接器是@solana/wallet-adapter-phantom。

下面的代码正在尝试更新代币元数据,以便能够更改代币名称、图像、符号……按照文档中的描述使用 PDA:https://docs.metaplex.com/programs/token-metadata/概述

    async createTokenMetadata(
    connection: Connection,
    metaplex: Metaplex,
    mint: PublicKey, // mint address
    user: Keypair,
    name: string,
    symbol: string,
    description: string,
    image_url: string
  ) {
    console.log('create token metadata');
    const l = image_url.split('/').length;
    const image_name = image_url.split('/')[l - 1];

    const file = toMetaplexFile(image_url, image_name);

    const imageUri = await metaplex.storage().upload(file);
    console.log('Image URI:', imageUri);

    // Upload metadata and get metadata uri (off chain metadata)
    const { uri } = await metaplex.nfts().uploadMetadata({
      name: name,
      description: description,
      image: imageUri,
    });
    console.log('Metadata URI:', uri);

    // Get metadata account address
    const metadataPDA = metaplex.nfts().pdas().metadata({ mint });

    // Onchain metadata format
    const tokenMetadata = {
      name: name,
      symbol: symbol,
      uri: uri,
      sellerFeeBasisPoints: 0,
      creators: null,
      collection: null,
      uses: null,
    } as DataV2;

    // Transaction to create metadata account
    const transaction = new Transaction().add(
      createCreateMetadataAccountV2Instruction(
        {
          metadata: metadataPDA,
          mint: mint,
          mintAuthority: user.publicKey,
          payer: user.publicKey,
          updateAuthority: user.publicKey,
        },
        {
          createMetadataAccountArgsV2: {
            data: tokenMetadata,
            isMutable: true,
          },
        }
      )
    );


    // Sing Transaction
    const {
      context: { slot: minContextSlot },
      value: { blockhash, lastValidBlockHeight },
    } = await connection.getLatestBlockhashAndContext();

    transaction.feePayer = this.phantomWalletAdapterService.getWalletAdapter().publicKey!;
    transaction.recentBlockhash = blockhash;
    transaction.lastValidBlockHeight = lastValidBlockHeight;

    const signed = await this.phantomWalletAdapterService.walletAdapter.signTransaction(transaction);

    const sendTransaction = await connection.sendRawTransaction(signed.serialize())
    // -> Here the code breaks on the frontend with this error:
    // Error: Signature verification failed
    // at Transaction.serialize 


    /* if I use this method instead, throws the error of the below image */
 //       const sendTransaction = 
 //   this.phantomWalletAdapterService.walletAdapter.sendTransaction(signed, connection, {
 //   minContextSlot,
 //   skipPreflight: true,
 //   preflightCommitment: 'processed',
});

    console.log(`Create Metadata Account: ${sendTransaction}`);

   const result = await connection.confirmTransaction({ blockhash, lastValidBlockHeight, signature });    

    console.log(`Result of the transaction: ${result}`);

        return result;
  }

我怎样才能在没有私钥的情况下从前端签署交易?

phantomjs web3js solana phantom-wallet
© www.soinside.com 2019 - 2024. All rights reserved.