Attempted import error: 'Wallet' is not exported from '@project-serum/anchor'

Attempted import error: 'Wallet' is not exported from '@project-serum/anchor'

正在尝试导入钱包 class 但在运行时出现标题错误。这个问题显然应该在 v0.21+ 中得到修复,但它似乎在我的代码库中不起作用。

使用 nextjs v11.0.1 和@project-serum/anchor v0.23.0

来自 index.tsx

的相关片段
import { Provider, Program, Wallet } from '@project-serum/anchor';
import { Keypair } from '@solana/web3.js';

const Page = () => {
 const testWallet = new Wallet(Keypair.generate());
 return <div></div>;
}

以上代码片段在使用@project-serum/anchor v0.16.2

时有效

next.config.js

const path = require('path');
const withTM = require('next-transpile-modules')([
  '@blocto/sdk',
  '@project-serum/sol-wallet-adapter',
  '@solana/wallet-........,
]);

module.exports = withTM({
  target: 'serverless',
  distDir: 'build',
  trailingSlash: true,
  webpack5: false,
  webpack(config) {
    config.module.rules.push(
      {
        test: /\.svg$/,
        issuer: {
          test: /\.(js|ts)x?$/,
        },
        use: ['@svgr/webpack'],
      },
      {
        test: /\.png$/,
        issuer: {
          test: /\.(js|ts)x?$/,
        },
        use: ['file-loader'],
      },
      {
        test: /\.mjs$/,
        include: /node_modules/,
        type: 'javascript/auto',
      },
    );
    config.resolve.alias = {
      ...config.resolve.alias,
      assets: path.resolve(__dirname, './public/assets'),
    };
    config.node = {
      fs: 'empty',
    };

    return config;
  },
});

tsconfig.json

{
  "extends": "../../tsconfig.json",
  "compilerOptions": {
    "baseUrl": "./src",
    "module": "esnext",
    "paths": {
      "assets/*": ["./public/assets/*"],
      "@solana/*": ["./node_modules/@solana/*"]
    },
    "incremental": true
  },
  "exclude": ["node_modules"],
  "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"]
}

嗯,不确定这是否与我遇到的问题相同,但您可以尝试只 re-create 本地钱包类型并导入它。

对我来说是这样的

export class MyWallet implements Wallet {

    constructor(readonly payer: Keypair) {
        this.payer = payer
    }

    async signTransaction(tx: Transaction): Promise<Transaction> {
        tx.partialSign(this.payer);
        return tx;
    }

    async signAllTransactions(txs: Transaction[]): Promise<Transaction[]> {
        return txs.map((t) => {
            t.partialSign(this.payer);
            return t;
        });
    }

    get publicKey(): PublicKey {
        return this.payer.publicKey;
    }
}

我必须像这样在 react

中导入 Wallet
const { Wallet } = require("@project-serum/anchor");

使用"@project-serum/anchor": "^0.14.0",

import * as anchor from '@project-serum/anchor';

anchor.web3.Keypair.generate().publicKey
anchor.web3.Keypair.generate().secretKey

看起来,在您使用的那个版本中,Wallet 不可用,之前 web3@project-serum/anchor 的一部分。然后,他们将其移动到一个单独的包中 @solana/web3.js.

Solana 开发工具让我想起了 solidity 的开始。由于它是一项要求很高的新技术,所以变化很快

使用 NodeWallet 代替钱包

从'@project-serum/anchor'导入{提供商、程序、NodeWallet};