經過Geth客戶端導入私鑰:javascript
Open TextEditjava
Paste key into TextEdit without any extra characters or quotationsweb
Save the file as pk.txt to your Desktopapi
Open Terminal, run command:cors
geth account import ~/Desktop/pk.txt
After successful import, delete the file from your desktop.dom
.\geth account import pk.txt,pk.txt放入沒有0x前綴的私鑰。按以前的命令重啓geth進入控制檯函數
(如:geth --testnet --rpcapi="db,eth,net,web3,personal,web3" --rpc --rpcaddr 0.0.0.0 --rpcport 8080 --rpccorsdomain "*" --verbosity 3 console --cache=4096)spa
,輸入 personal.listAccounts 查看是否導入成功3d
經過Web3導入私鑰code
根據給定的私鑰生成密鑰對,並在保存後返回其ID。
調用:
web3.shh.addPrivateKey(privateKey, [callback])
參數:
privateKey
:String - 要導入的私鑰,16進制字符串callback
:Function - 可選的回調函數,其第一個參數爲錯誤對象,第二個對象爲返回結果返回值:
String
- 成功時返回ID,失敗則返回錯誤信息
示例代碼:
web3.shh.addPrivateKey('0x8bda3abeb454847b515fa9b404cede50b1cc63cfdeddd4999d074284b4c21e15') .then(console.log); > "3e22b9ffc2387e18636e0a3d0c56b023264c16e78a2adcba1303cefc685e610f"
三:
web3.eth.accounts.privateKeyToAccount('0x348ce564d427a3311b6536bbcff9390d69395b06ed6c486954e971d960fe8709'); > { address: '0xb8CE9ab6943e0eCED004cDe8e3bBed6568B2Fa01', privateKey: '0x348ce564d427a3311b6536bbcff9390d69395b06ed6c486954e971d960fe8709', signTransaction: function(tx){...}, sign: function(data){...}, encrypt: function(password){...} } web3.eth.accounts.privateKeyToAccount('0x348ce564d427a3311b6536bbcff9390d69395b06ed6c486954e971d960fe8709'); > { address: '0xb8CE9ab6943e0eCED004cDe8e3bBed6568B2Fa01', privateKey: '0x348ce564d427a3311b6536bbcff9390d69395b06ed6c486954e971d960fe8709', signTransaction: function(tx){...}, sign: function(data){...}, encrypt: function(password){...} }