integrated ethjs-accounts

This commit is contained in:
Fabian Vogelsteller 2017-03-24 11:37:24 +01:00
parent 71e6b34081
commit 5e24628dbd
No known key found for this signature in database
GPG Key ID: E51EADA77F1A4124
9 changed files with 284 additions and 7 deletions

View File

@ -5,7 +5,7 @@
web3.eth.abi
=========
The ``web3.eth.abi`` functions let you de- and encode ABI parameters for function calls.
The ``web3.eth.abi`` functions let you de- and encode parameters to ABI (Application Binary Interface) for function calls to the EVM (Ethereum Virtual Machine).

207
docs/web3-eth-accounts.rst Normal file
View File

@ -0,0 +1,207 @@
.. _eth-accounts:
=========
web3.eth.accounts
=========
The ``web3.eth.accounts`` contains functions to generate Ethereum accounts and sign transactions.
------------------------------------------------------------------------------
generate
=====================
.. code-block:: javascript
web3.eth.accounts.generate([entropy]);
Generates an account object with private key and public key.
----------
Parameters
----------
1. ``entropy`` - ``String`` (optional): A random strong to increase entropy. If igven it should be at least 32 characters. If none is given a random string will be generated using :ref:`randomhex <randomhex>`.
.. _eth-accounts-generate-return:
-------
Returns
-------
``Object`` - The account object with the following structure:
- ``address`` - ``string``: The account address.
- ``publicKey`` - ``string``: The accounts public key.
- ``privateKey`` - ``string``: The accounts private key. This should never be shared or stored unencrypted in localstorage! Also make sure to ``null`` the memory after usage.
-------
Example
-------
.. code-block:: javascript
web3.eth.accounts.generate('2435@#@#@±±±±!!!!678543213456764321§34567543213456785432134567');
> {
address: "0xF2CD2AA0c7926743B1D4310b2BC984a0a453c3d4",
publicKey: "0x0b9f65726c43d486229d0a44f27edb53a0e4b141350ceaa8f7a12c893e5b0385b3b25b35b1a0b85d39e2b7e8f1b407f776f0fc765be04683dea4697a3c603a46"
privateKey: "0xd7325de5c2c1cf0009fac77d3d04a9c004b038883446b065871bc3e831dcd098",
}
web3.eth.accounts.generate();
> {
address: "0xb8CE9ab6943e0eCED004cDe8e3bBed6568B2Fa01",
publicKey: "0xbb1846722a4c27e71196e1a44611ee7174276a6c51c4830fb810cac64b0725f217cb8783625a809d1303adeeec2cf036ab74098a77a6b7f1003486e173b29aa7"
privateKey: "0x348ce564d427a3311b6536bbcff9390d69395b06ed6c486954e971d960fe8709",
}
------------------------------------------------------------------------------
privateToPublic
=====================
.. code-block:: javascript
web3.eth.accounts.privateToPublic(privateKey);
Gets the public key from a private key.
----------
Parameters
----------
1. ``privateKey`` - ``String``: The private key to convert.
-------
Returns
-------
``String`` - The accounts public key.
-------
Example
-------
.. code-block:: javascript
web3.eth.accounts.privateToPublic('0x348ce564d427a3311b6536bbcff9390d69395b06ed6c486954e971d960fe8709');
> "0xbb1846722a4c27e71196e1a44611ee7174276a6c51c4830fb810cac64b0725f217cb8783625a809d1303adeeec2cf036ab74098a77a6b7f1003486e173b29aa7"
web3.eth.accounts.privateToPublic('348ce564d427a3311b6536bbcff9390d69395b06ed6c486954e971d960fe8709');
> "0xbb1846722a4c27e71196e1a44611ee7174276a6c51c4830fb810cac64b0725f217cb8783625a809d1303adeeec2cf036ab74098a77a6b7f1003486e173b29aa7"
------------------------------------------------------------------------------
privateToAddress
=====================
.. code-block:: javascript
web3.eth.accounts.privateToAddress(privateKey);
Gets the Ethereum address from a private key.
----------
Parameters
----------
1. ``privateKey`` - ``String``: The private key to convert.
-------
Returns
-------
``String`` - The accounts address.
-------
Example
-------
.. code-block:: javascript
web3.eth.accounts.privateToAddress('0x348ce564d427a3311b6536bbcff9390d69395b06ed6c486954e971d960fe8709');
> "0xb8CE9ab6943e0eCED004cDe8e3bBed6568B2Fa01"
web3.eth.accounts.privateToAddress('348ce564d427a3311b6536bbcff9390d69395b06ed6c486954e971d960fe8709');
> "0xb8CE9ab6943e0eCED004cDe8e3bBed6568B2Fa01"
------------------------------------------------------------------------------
privateToAccount
=====================
.. code-block:: javascript
web3.eth.accounts.privateToAccount(privateKey);
Gets a account object from a private key.
----------
Parameters
----------
1. ``privateKey`` - ``String``: The private key to convert.
-------
Returns
-------
``Object`` - The account object with the :ref:`structure seen here <eth-accounts-generate-return>`.
-------
Example
-------
.. code-block:: javascript
web3.eth.accounts.privateToAccount('0x348ce564d427a3311b6536bbcff9390d69395b06ed6c486954e971d960fe8709');
> "0xb8CE9ab6943e0eCED004cDe8e3bBed6568B2Fa01"
web3.eth.accounts.privateToAccount('348ce564d427a3311b6536bbcff9390d69395b06ed6c486954e971d960fe8709');
> "0xb8CE9ab6943e0eCED004cDe8e3bBed6568B2Fa01"
------------------------------------------------------------------------------
publicToAddress
=====================
.. code-block:: javascript
web3.eth.accounts.publicToAddress(publicKey);
Gets an Ethereum address from a public key.
----------
Parameters
----------
1. ``publicKey`` - ``String``: The public key to convert.
-------
Returns
-------
``String`` - The Ethereum address.
-------
Example
-------
.. code-block:: javascript
web3.eth.accounts.publicToAddress('0x7195981eaa1ccf18c6d2e15ca5c5bc6ad97f7f8e3505005f9ad12fc68a02ded647f95b9cacf71a2a99f96371c6133dfd3d4486493d9159d49a7faae7c5793c24');
> "0xF0109fC8DF283027b6285cc889F5aA624EaC1F55"
web3.eth.accounts.publicToAddress('7195981eaa1ccf18c6d2e15ca5c5bc6ad97f7f8e3505005f9ad12fc68a02ded647f95b9cacf71a2a99f96371c6133dfd3d4486493d9159d49a7faae7c5793c24');
> "0xF0109fC8DF283027b6285cc889F5aA624EaC1F55"
------------------------------------------------------------------------------

View File

@ -79,6 +79,22 @@ personal
For ``web3.eth.personal`` see the :ref:`personal reference documentation <eth-personal>`
------------------------------------------------------------------------------
accounts
=====================
For ``web3.eth.accounts`` see the :ref:`accounts reference documentation <eth-accounts>`
------------------------------------------------------------------------------
abi
=====================
For ``web3.eth.abi`` see the :ref:`abi reference documentation <eth-abi>`
------------------------------------------------------------------------------

View File

@ -19,7 +19,6 @@
"coveralls": "^2.11.2",
"crypto-js": "^3.1.4",
"del": ">=2.0.2",
"ethjs-abi": "^0.1.8",
"exorcist": "^0.4.0",
"google-closure-compiler": "^20170218.0.0",
"gulp": ">=3.9.0",

View File

@ -17,6 +17,7 @@
"web3-net": "^1.0.0",
"web3-utils": "^1.0.0",
"ethjs-account": "^0.1.0",
"randomhex": "^0.1.5",
"underscore": "^1.8.3"
}
}

View File

@ -0,0 +1,53 @@
/*
This file is part of web3.js.
web3.js is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
web3.js is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with web3.js. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @file accounts.js
* @author Fabian Vogelsteller <fabian@ethereum.org>
* @date 2017
*/
"use strict";
var randomhex = require('randomhex');
var accounts = require('ethjs-account');
module.exports = {
generate: function generate(randomString) {
if(!randomString) {
randomString = randomhex(32);
}
return accounts.generate(randomString);
},
privateToAddress: function privateToAddress(privKey) {
privKey = '0x'+ privKey.replace(/^0x/i,'');
return accounts.privateToAccount(privKey).address;
},
privateToPublic: function privateToPublic(privKey) {
privKey = '0x'+ privKey.replace(/^0x/i,'');
return '0x'+ accounts.privateToPublic(privKey).toString('hex');
},
privateToAccount: function privateToAccount(privKey) {
privKey = '0x'+ privKey.replace(/^0x/i,'');
return '0x'+ accounts.privateToAccount(privKey);
},
publicToAddress: function publicToAddress(pubKey) {
pubKey = new Buffer(pubKey.replace(/^0x/i,''), 'hex');
return accounts.publicToAddress(pubKey);
}
};

View File

@ -15,7 +15,7 @@
along with web3.js. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @file guessChain.js
* @file getNetworkType.js
* @author Fabian Vogelsteller <fabian@ethereum.org>
* @date 2017
*/

View File

@ -34,8 +34,7 @@ var Personal = require('web3-eth-personal');
var abi = require('web3-eth-abi');
var Contract = require('web3-eth-contract');
var Iban = require('web3-eth-iban');
var accounts = require('ethjs-account');
var accounts = require('./accounts.js');
@ -81,6 +80,9 @@ var Eth = function Eth() {
// add net
this.net = new Net(this.currentProvider);
// add accounts
this.accounts = accounts;
// add personal
this.personal = new Personal(this.currentProvider);

View File

@ -49,11 +49,10 @@ var providers = {
var Web3 = function Web3() {
this.version = version.version;
// sets _requestmanager etc
core.packageInit(this, arguments);
this.version = version.version;
this.providers = providers;