以太坊Geth通过私钥导入新地址到钱包步骤(3种方法)

一:

通过Geth客户端导入私钥:

  1. Open TextEdit

  2. Paste key into TextEdit without any extra characters or quotations

  3. Save the file as pk.txt to your Desktop

  4. Open Terminal, run command:

    geth account import ~/Desktop/pk.txt
    
  5. After successful import, delete the file from your desktop.

.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)

,输入 personal.listAccounts 查看是否导入成功

二:

通过Web3导入私钥

web3.shh.addPrivateKey

根据给定的私钥生成密钥对,并在保存后返回其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(privateKey);

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){...}
}
 



原文地址:https://www.cnblogs.com/x-poior/p/11010058.html