自学内容网 自学内容网

Invalid Private Key, Not a valid string or uint8Array

报这种错误:一般在生成private key前面添加"0x"即可解决。我就是在私钥前面添加了"0x"解决了。

在学习web3时,使用助词生成的私钥,然后由私钥导出keystore就报错:

ERROR
Invalid Private Key, Not a valid string or uint8Array
InvalidPrivateKeyError: Invalid Private Key, Not a valid string or uint8Array
    at parseAndValidatePrivateKey (webpack-internal:///./node_modules/web3-eth-accounts/lib/esm/account.js:132:11)
    at eval (webpack-internal:///./node_modules/web3-eth-accounts/lib/esm/account.js:523:32)
    at Generator.next (<anonymous>)
    at eval (webpack-internal:///./node_modules/web3-eth-accounts/lib/esm/account.js:66:67)
    at new Promise (<anonymous>)
    at __awaiter (webpack-internal:///./node_modules/web3-eth-accounts/lib/esm/account.js:48:10)
    at Object.encrypt (webpack-internal:///./node_modules/web3-eth-accounts/lib/esm/account.js:521:52)
    at genMnemonic (webpack-internal:///./node_modules/unplugin/dist/webpack/loaders/transform.js?unpluginName=unplugin-vue-components!./node_modules/babel-loader/lib/index.js??clonedRuleSet-40.use[0]!./node_modules/vue-loader/dist/index.js??ruleSet[0].use[0]!./src/components/AccountSys.vue?vue&type=script&setup=true&lang=js:46:42)

开始报错以为是自己没开梯子,但我用之前写的demo是能查询到eth账户余额,于是根据报错信息怀疑是生成的privatekey不对,我照着学习的视频检查了代码跟它一样没问题呀,然后我在生成的私钥前面添加了"0x"就可以了。

//获取钱包私钥
    const privateKey=wallet.getPrivateKey().toString("hex");    
const web3 = new Web3(
        Web3.givenProvider || 'wss://sepolia.infura.io/ws/v3/018c25a8b5ce4f23810c49f60478471f');
    const keystore = web3.eth.accounts.encrypt('0x'+privateKey,'111111');

搞定。


原文地址:https://blog.csdn.net/developerFBI/article/details/142419039

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!