網(wǎng)站被劫持怎么辦百度官方版
報(bào)這種錯(cuò)誤:一般在生成private key前面添加"0x"即可解決。我就是在私鑰前面添加了"0x"解決了。
在學(xué)習(xí)web3時(shí),使用助詞生成的私鑰,然后由私鑰導(dǎo)出keystore就報(bào)錯(cuò):
ERROR
Invalid Private Key, Not a valid string or uint8Array
InvalidPrivateKeyError: Invalid Private Key, Not a valid string or uint8Arrayat 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)
開始報(bào)錯(cuò)以為是自己沒開梯子,但我用之前寫的demo是能查詢到eth賬戶余額,于是根據(jù)報(bào)錯(cuò)信息懷疑是生成的privatekey不對(duì),我照著學(xué)習(xí)的視頻檢查了代碼跟它一樣沒問題呀,然后我在生成的私鑰前面添加了"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');
搞定。