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');

搞定。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值