# all files / packages/web3-eth-accounts/src/ index.js

81.11% Statements 146/180
70.43% Branches 81/115
77.78% Functions 21/27
81.11% Lines 146/180

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
    390
    391
    392
    393
    394
    395
    396
    397

    1×
    1×
    1×
    1×
    1×
    1×
    1×
    1×
    1×
    1×

    1×
    84×

    1×

    1569×
    325×

    1569×

    1×
    1460×

    1460×
    14×

    1460×

    1460×
    100×

    1460×

    1×
    1130×

    1×
    330×

    1×
    20×

    1×

    20×

    20×

    20×
    20×
    20×
    20×

    20×

    20×

    20×
    6×

    14×

    14×

    14×

    14×

    1×
    4×

    1×
    24×
    24×
    24×

    1×

    16×
    16×
    16×
    16×

    1×

    20×
    4×

    16×
    6×

    16×
    4×

    12×

    1×

    104×

    104×

    104×

    104×
    104×
    104×
    103×

    103×
    1×
    1×

    1×

    1×

    104×

    104×
    104×

    104×
    104×

    104×

    1×

    100×

    100×
    100×
    100×

    100×
    100×
    100×

    100×

    100×

    100×
    100×
    100×
    100×

    100×
    100×

    100×

    100×

    100×

    1×
    1569×
    1569×
    1569×

    1×
    6×
    30×

    6×

    1×

    56×
    50×

    56×
    52×
    52×

    52×
    52×
    52×

    52×

    52×

    4×

    1×
    26×

    26×

    26×
    26×

    26×
    26×

    26×
    26×

    26×

    26×

    1×
    2×
    2×
    20×

    2×

    1×

    1×

    1×

    1×

    1×
    1×
    1×

    1×
     /*
     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 _ = require("underscore");
    var Promise = require('bluebird');
    var Account = require("eth-lib/src/account");
    var Hash = require("eth-lib/src/hash");
    var RLP = require("eth-lib/src/rlp");
    var crypto = require('crypto');
    var scryptsy = require('scrypt.js');
    var uuid = require('uuid');
    var utils = require('web3-utils');
    var helpers = require('web3-core-helpers');

    var isNot = function(value) {
        return (_.isUndefined(value) || _.isNull(value));
    };

    var Accounts = function Accounts(eth) {

        if (eth) {
            this.eth = (eth.eth) ? eth.eth : eth;
        }
        this.wallet = new Wallet(this);
    };

    Accounts.prototype._addAccountFunctions = function (account) {
        var _this = this;

        // add sign functions
        account.signTransaction = function signTransaction(tx, callback) {
            return _this.signTransaction(tx, account.privateKey, callback);
        };
        account.sign = function sign(data) {
            return _this.sign(data, account.privateKey);
        };

        account.encrypt = function encrypt(password, options) {
            return _this.encrypt(account.privateKey, password, options);
        };

        return account;
    };

    Accounts.prototype.create = function create(entropy) {
        return this._addAccountFunctions(Account.create(entropy || utils.randomHex(32)));
    };

    Accounts.prototype.privateKeyToAccount = function privateKeyToAccount(privateKey) {
        return this._addAccountFunctions(Account.fromPrivate(privateKey));
    };

    Accounts.prototype.signTransaction = function signTransaction(tx, privateKey, callback) {
        var _this = this;

        function signed (tx) {

            Iif (!tx.gas && !tx.gasLimit) {
                throw new Error('"gas" is missing');
            }

            var transaction = {
                nonce: utils.numberToHex(tx.nonce),
                to: tx.to ? helpers.formatters.inputAddressFormatter(tx.to) : '0x',
                data: tx.data || '0x',
                value: tx.value ? utils.numberToHex(tx.value) : "0x",
                gas: utils.numberToHex(tx.gasLimit || tx.gas),
                gasPrice: utils.numberToHex(tx.gasPrice),
                chainId: utils.numberToHex(tx.chainId)
            };

            var hash = Hash.keccak256(Account.transactionSigningData(transaction));
            var rawTransaction = Account.signTransaction(transaction, privateKey);
            var values = RLP.decode(rawTransaction);
            var result = {
                messageHash: hash,
                v: values[6],
                r: values[7],
                s: values[8],
                rawTransaction: rawTransaction
            };
            Iif (_.isFunction(callback)) {
                callback(null, result);
            }
            return result;
        }

        // Returns synchronously if nonce, chainId and price are provided
        if (tx.nonce !== undefined && tx.chainId !== undefined && tx.gasPrice !== undefined) {
            return signed(tx);
        }

        Iif (!_this || !_this.eth || !_this.eth.net) {
            return Promise.reject(new Error('The Eth package is set bound. Please set using "accounts.eth = eth", or provide "nonce", "chainId" and "gasPrice" in the transaction yourself.'));
        }

        // Otherwise, get the missing info from the Ethereum Node
        return Promise.all([
            isNot(tx.chainId) ? _this.eth.net.getId() : tx.chainId,
            isNot(tx.gasPrice) ? _this.eth.getGasPrice() : tx.gasPrice,
            isNot(tx.nonce) ? _this.eth.getTransactionCount(_this.privateKeyToAccount(privateKey).address) : tx.nonce
        ]).then(function (args) {
            Iif (isNot(args[0]) || isNot(args[1]) || isNot(args[2])) {
                throw new Error('One of the values "chainId", "gasPrice", or "nonce" couldn\'t be fetched: '+ JSON.stringify(args));
            }
            return signed(_.extend(tx, {chainId: args[0], gasPrice: args[1], nonce: args[2]}));
        });
    };

    Accounts.prototype.recoverTransaction = function recoverTransaction(rawTx) {
        return Account.recoverTransaction(rawTx);
    };

    Accounts.prototype.hashMessage = function hashMessage(data) {
        var message = utils.isHex(data) ? utils.hexToUtf8(data) : data;
        var ethMessage = "\x19Ethereum Signed Message:\n" + message.length + message;
        return Hash.keccak256s(ethMessage);
    };

    Accounts.prototype.sign = function sign(data, privateKey) {

        var hash = this.hashMessage(data);
        var signature = Account.sign(hash, privateKey);
        var vrs = Account.decodeSignature(signature);
        return {
            message: data,
            messageHash: hash,
            v: vrs[0],
            r: vrs[1],
            s: vrs[2],
            signature: signature
        };
    };

    Accounts.prototype.recover = function recover(hash, signature) {

        if (_.isObject(hash)) {
            return this.recover(hash.messageHash, Account.encodeSignature([hash.v, hash.r, hash.s]));
        }

        if (!utils.isHex(hash)) {
            hash = this.hashMessage(hash);
        }

        if (arguments.length === 4) {
            return this.recover(hash, Account.encodeSignature([].slice.call(arguments, 1, 4))); // v, r, s
        }
        return Account.recover(hash, signature);
    };

    // Taken from https://github.com/ethereumjs/ethereumjs-wallet
    Accounts.prototype.decrypt = function (v3Keystore, password, nonStrict) {
        /* jshint maxcomplexity: 10 */

        Iif(!_.isString(password)) {
            throw new Error('No password given.');
        }

        var json = (_.isObject(v3Keystore)) ? v3Keystore : JSON.parse(nonStrict ? v3Keystore.toLowerCase() : v3Keystore);

        Iif (json.version !== 3) {
            throw new Error('Not a valid V3 wallet');
        }

        var derivedKey;
        var kdfparams;
        if (json.crypto.kdf === 'scrypt') {
            kdfparams = json.crypto.kdfparams;

            // FIXME: support progress reporting callback
            derivedKey = scryptsy(new Buffer(password), new Buffer(kdfparams.salt, 'hex'), kdfparams.n, kdfparams.r, kdfparams.p, kdfparams.dklen);
        } else Eif (json.crypto.kdf === 'pbkdf2') {
            kdfparams = json.crypto.kdfparams;

            Iif (kdfparams.prf !== 'hmac-sha256') {
                throw new Error('Unsupported parameters to PBKDF2');
            }

            derivedKey = crypto.pbkdf2Sync(new Buffer(password), new Buffer(kdfparams.salt, 'hex'), kdfparams.c, kdfparams.dklen, 'sha256');
        } else {
            throw new Error('Unsupported key derivation scheme');
        }

        var ciphertext = new Buffer(json.crypto.ciphertext, 'hex');

        var mac = utils.sha3(Buffer.concat([ derivedKey.slice(16, 32), ciphertext ])).replace('0x','');
        Iif (mac !== json.crypto.mac) {
            throw new Error('Key derivation failed - possibly wrong password');
        }

        var decipher = crypto.createDecipheriv(json.crypto.cipher, derivedKey.slice(0, 16), new Buffer(json.crypto.cipherparams.iv, 'hex'));
        var seed = '0x'+ Buffer.concat([ decipher.update(ciphertext), decipher.final() ]).toString('hex');

        return this.privateKeyToAccount(seed);
    };

    Accounts.prototype.encrypt = function (privateKey, password, options) {
        /* jshint maxcomplexity: 20 */
        var account = this.privateKeyToAccount(privateKey);

        options = options || {};
        var salt = options.salt || crypto.randomBytes(32);
        var iv = options.iv || crypto.randomBytes(16);

        var derivedKey;
        var kdf = options.kdf || 'scrypt';
        var kdfparams = {
            dklen: options.dklen || 32,
            salt: salt.toString('hex')
        };

        Iif (kdf === 'pbkdf2') {
            kdfparams.c = options.c || 262144;
            kdfparams.prf = 'hmac-sha256';
            derivedKey = crypto.pbkdf2Sync(new Buffer(password), salt, kdfparams.c, kdfparams.dklen, 'sha256');
        } else Eif (kdf === 'scrypt') {
            // FIXME: support progress reporting callback
            kdfparams.n = options.n || 8192; // 2048 4096 8192 16384
            kdfparams.r = options.r || 8;
            kdfparams.p = options.p || 1;
            derivedKey = scryptsy(new Buffer(password), salt, kdfparams.n, kdfparams.r, kdfparams.p, kdfparams.dklen);
        } else {
            throw new Error('Unsupported kdf');
        }

        var cipher = crypto.createCipheriv(options.cipher || 'aes-128-ctr', derivedKey.slice(0, 16), iv);
        Iif (!cipher) {
            throw new Error('Unsupported cipher');
        }

        var ciphertext = Buffer.concat([ cipher.update(new Buffer(account.privateKey.replace('0x',''), 'hex')), cipher.final() ]);

        var mac = utils.sha3(Buffer.concat([ derivedKey.slice(16, 32), new Buffer(ciphertext, 'hex') ])).replace('0x','');

        return {
            version: 3,
            id: uuid.v4({ random: options.uuid || crypto.randomBytes(16) }),
            address: account.address.toLowerCase().replace('0x',''),
            crypto: {
                ciphertext: ciphertext.toString('hex'),
                cipherparams: {
                    iv: iv.toString('hex')
                },
                cipher: options.cipher || 'aes-128-ctr',
                kdf: kdf,
                kdfparams: kdfparams,
                mac: mac.toString('hex')
            }
        };
    };

    // Note: this is trying to follow closely the specs on
    // http://web3js.readthedocs.io/en/1.0/web3-eth-accounts.html

    function Wallet(accounts) {
        this.length = 0;
        this._accounts = accounts;
        this.defaultKeyName = "web3js_wallet";
    }

    Wallet.prototype.create = function (numberOfAccounts, entropy) {
        for (var i = 0; i < numberOfAccounts; ++i) {
            this.add(this._accounts.create(entropy).privateKey);
        }
        return this;
    };

    Wallet.prototype.add = function (account) {

        if (_.isString(account)) {
            account = this._accounts.privateKeyToAccount(account);
        }
        if (!this[account.address]) {
            account = this._accounts.privateKeyToAccount(account.privateKey);
            account.index = this.length;

            this[this.length] = account;
            this[account.address] = account;
            this[account.address.toLowerCase()] = account;

            this.length++;

            return account;
        } else {
            return this[account.address];
        }
    };

    Wallet.prototype.remove = function (addressOrIndex) {
        var account = this[addressOrIndex];

        Eif (account) {
            // address
            this[account.address].privateKey = null;
            delete this[account.address];
            // address lowercase
            this[account.address.toLowerCase()].privateKey = null;
            delete this[account.address.toLowerCase()];
            // index
            this[account.index].privateKey = null;
            delete this[account.index];

            this.length--;

            return true;
        } else {
            return false;
        }
    };

    Wallet.prototype.clear = function () {
        var length = this.length;
        for (var i = 0; i < length; i++) {
            this.remove(i);
        }

        return this;
    };

    Wallet.prototype.encrypt = function (password, options) {
        var accounts = [];
        for (var i = 0; i < this.length; i++) {
            accounts[i] = this[i].encrypt(password, options);
        }
        return accounts;
    };

    Wallet.prototype.decrypt = function (encryptedWallet, password) {
        var _this = this;

        encryptedWallet.forEach(function (keystore) {
            var account = _this._accounts.decrypt(keystore, password);

            if (account) {
                _this.add(account);
            } else {
                throw new Error('Couldn\'t decrypt accounts. Password wrong?');
            }
        });

        return this;
    };

    Wallet.prototype.save = function (password, keyName) {
        localStorage.setItem(keyName || this.defaultKeyName, JSON.stringify(this.encrypt(password)));

        return true;
    };

    Wallet.prototype.load = function (password, keyName) {
        var keystore = localStorage.getItem(keyName || this.defaultKeyName);

        if (keystore) {
            try {
                keystore = JSON.parse(keystore);
            } catch(e) {

            }
        }

        return this.decrypt(keystore || [], password);
    };

    Eif (typeof localStorage === 'undefined') {
        delete Wallet.prototype.save;
        delete Wallet.prototype.load;
    }

    module.exports = Accounts;