在web3.eth.accounts包含了这些功能产生以太坊账户和标志的交易和数据。
注意
此软件包尚未经过审核,可能不安全。采取预防措施以正确清理内存,安全存储私钥,并在生产中使用前正确测试交易接收和发送功能!
要独立使用此软件包,请使用:
var Accounts = require('web3-eth-accounts');
//传入eth或web3包是必要的,以允许自动检索chainId,gasPrice和nonce
//for accounts.signTransaction()。
var accounts = new Accounts('ws://localhost:8546');
创造
web3.eth.accounts.create([entropy]);
使用私钥和公钥生成帐户对象。
参数
- entropy- String(可选):增加熵的随机字符串。如果给定它应该至少32个字符。如果没有给出,将使用randomhex生成随机字符串。
返回
Object - 具有以下结构的帐户对象:
• address- string:帐户地址。
• privateKey- string:帐户私钥。永远不应该在localstorage中以未加密的方式共享或存储!null使用后请确保内存。
• signTransaction(tx [, callback])- Function:签署交易的功能。有关更多信息,请参见web3.eth.accounts.signTransaction()。
• sign(data)- Function:签署交易的功能。有关更多信息,请参见web3.eth.accounts.sign()。
示例
web3.eth.accounts.create();
> {
address: "0xb8CE9ab6943e0eCED004cDe8e3bBed6568B2Fa01",
privateKey: "0x348ce564d427a3311b6536bbcff9390d69395b06ed6c486954e971d960fe8709",
signTransaction: function(tx){...},
sign: function(data){...},
encrypt: function(password){...}
}
web3.eth.accounts.create('2435@#@#@±±±±!!!!678543213456764321§34567543213456785432134567');
> {
address: "0xF2CD2AA0c7926743B1D4310b2BC984a0a453c3d4",
privateKey: "0xd7325de5c2c1cf0009fac77d3d04a9c004b038883446b065871bc3e831dcd098",
signTransaction: function(tx){...},
sign: function(data){...},
encrypt: function(password){...}
}
web3.eth.accounts.create(web3.utils.randomHex(32));
> {
address: "0xe78150FaCD36E8EB00291e251424a0515AA1FF05",
privateKey: "0xcc505ee6067fba3f6fc2050643379e190e087aeffe5d958ab9f2f3ed3800fa4e",
signTransaction: function(tx){...},
sign: function(data){...},
encrypt: function(password){...}
}
privateKeyToAccount
web3.eth.accounts.privateKeyToAccount(privateKey);
从私钥创建帐户对象。
参数
- privateKey- String:要转换的私钥。
返回
Object- 具有此处所示结构的帐户对象。
示例
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){...}
}
signTransaction
web3.eth.accounts.signTransaction(tx, privateKey [, callback]);
使用给定的私钥签署以太坊交易。
参数
- tx - Object: The transaction object as follows:
• nonce - String: (optional) The nonce to use when signing this transaction. Default will use web3.eth.getTransactionCount().
• chainId - String: (optional) The chain id to use when signing this transaction. Default will use web3.eth.net.getId().
• to - String: (optional) The recevier of the transaction, can be empty when deploying a contract.
• data - String: (optional) The call data of the transaction, can be empty for simple value transfers.
• value - String: (optional) The value of the transaction in wei.
• gasPrice - String: (optional) The gas price set by this transaction, if empty, it will use web3.eth.gasPrice()
• gas - String: The gas provided by the transaction. - privateKey- String:用于签名的私钥。
- callback- Function:(可选)可选回调,返回错误对象作为第一个参数,结果作为秒。
返回
Promise返回Object:签