2016-07-25 10:55:16 +03:00
|
|
|
'use strict';
|
|
|
|
|
2016-08-02 02:24:52 +03:00
|
|
|
var scrypt = require('scrypt-js');
|
2016-07-14 20:43:15 +03:00
|
|
|
|
2016-07-16 06:47:35 +03:00
|
|
|
var Contract = require('./lib/contract.js');
|
2016-08-04 02:36:44 +03:00
|
|
|
var providers = require('./lib/providers.js');
|
2016-07-23 10:27:14 +03:00
|
|
|
var secretStorage = require('./lib/secret-storage.js');
|
2016-07-27 09:53:40 +03:00
|
|
|
var Randomish = require('./lib/randomish.js');
|
2016-07-22 00:06:42 +03:00
|
|
|
var SigningKey = require('./lib/signing-key.js');
|
|
|
|
var Wallet = require('./lib/wallet.js');
|
2016-08-24 02:35:19 +03:00
|
|
|
var units = require('./lib/units.js');
|
2016-07-14 20:43:15 +03:00
|
|
|
|
2016-07-16 06:47:35 +03:00
|
|
|
var utils = require('./lib/utils.js');
|
|
|
|
var BN = utils.BN;
|
2016-07-14 20:43:15 +03:00
|
|
|
|
|
|
|
|
2016-07-16 06:47:35 +03:00
|
|
|
var exportUtils = {};
|
2016-07-22 00:06:42 +03:00
|
|
|
utils.defineProperty(Wallet, 'utils', exportUtils);
|
2016-07-14 20:43:15 +03:00
|
|
|
|
2016-07-22 00:06:42 +03:00
|
|
|
utils.defineProperty(exportUtils, 'BN', BN);
|
|
|
|
utils.defineProperty(exportUtils, 'Buffer', Buffer);
|
2016-07-14 20:43:15 +03:00
|
|
|
|
2016-07-22 00:06:42 +03:00
|
|
|
utils.defineProperty(exportUtils, 'sha3', utils.sha3);
|
|
|
|
utils.defineProperty(exportUtils, 'sha256', utils.sha256);
|
2016-07-14 20:43:15 +03:00
|
|
|
|
2016-08-24 05:06:26 +03:00
|
|
|
utils.defineProperty(exportUtils, 'getContractAddress', utils.getContractAddress);
|
2016-07-14 20:43:15 +03:00
|
|
|
|
2016-07-22 00:06:42 +03:00
|
|
|
module.exports = Wallet;
|
2016-07-16 06:47:35 +03:00
|
|
|
|
|
|
|
|
2016-08-02 02:24:52 +03:00
|
|
|
utils.defineProperty(Wallet, 'etherSymbol', '\uD835\uDF63');
|
|
|
|
|
2016-08-24 02:35:19 +03:00
|
|
|
utils.defineProperty(Wallet, 'formatEther', units.formatEther);
|
|
|
|
utils.defineProperty(Wallet, 'parseEther', units.parseEther);
|
2016-08-04 02:36:44 +03:00
|
|
|
|
2016-08-24 03:12:12 +03:00
|
|
|
utils.defineProperty(Wallet, 'getAddress', utils.getAddress);
|
|
|
|
utils.defineProperty(Wallet, 'getIcapAddress', utils.getIcapAddress);
|
2016-07-14 20:43:15 +03:00
|
|
|
|
2016-07-23 10:27:14 +03:00
|
|
|
utils.defineProperty(Wallet, 'isCrowdsaleWallet', secretStorage.isCrowdsaleWallet);
|
|
|
|
utils.defineProperty(Wallet, 'isValidWallet', secretStorage.isValidWallet);
|
|
|
|
|
|
|
|
utils.defineProperty(Wallet, 'decryptCrowdsale', function(json, password) {
|
|
|
|
return new Wallet(secretStorage.decryptCrowdsale(json, password));
|
|
|
|
});
|
|
|
|
|
2016-08-16 06:04:02 +03:00
|
|
|
utils.defineProperty(Wallet, 'decrypt', function(json, password, progressCallback) {
|
|
|
|
if (progressCallback && typeof(progressCallback) !== 'function') {
|
|
|
|
throw new Error('invalid callback');
|
|
|
|
}
|
|
|
|
|
|
|
|
return new Promise(function(resolve, reject) {
|
|
|
|
secretStorage.decrypt(json, password, progressCallback).then(function(signingKey) {
|
|
|
|
resolve(new Wallet(signingKey));
|
|
|
|
}, function(error) {
|
|
|
|
reject(error);
|
|
|
|
});
|
2016-07-23 10:27:14 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-08-16 06:04:02 +03:00
|
|
|
utils.defineProperty(Wallet.prototype, 'encrypt', function(password, options, progressCallback) {
|
|
|
|
if (typeof(options) === 'function' && !progressCallback) {
|
|
|
|
progressCallback = options;
|
2016-07-23 10:27:14 +03:00
|
|
|
options = {};
|
|
|
|
}
|
2016-08-16 06:04:02 +03:00
|
|
|
if (progressCallback && typeof(progressCallback) !== 'function') {
|
|
|
|
throw new Error('invalid callback');
|
|
|
|
}
|
2016-07-23 10:27:14 +03:00
|
|
|
|
2016-08-16 06:04:02 +03:00
|
|
|
return secretStorage.encrypt(this.privateKey, password, options, progressCallback);
|
2016-07-23 10:27:14 +03:00
|
|
|
});
|
|
|
|
|
2016-08-16 06:04:02 +03:00
|
|
|
utils.defineProperty(Wallet, 'summonBrainWallet', function(username, password, progressCallback) {
|
|
|
|
if (progressCallback && typeof(progressCallback) !== 'function') {
|
|
|
|
throw new Error('invalid callback');
|
|
|
|
}
|
2016-08-02 02:24:52 +03:00
|
|
|
|
2016-08-16 06:04:02 +03:00
|
|
|
return new Promise(function(resolve, reject) {
|
|
|
|
scrypt(password, username, (1 << 18), 8, 1, 32, function(error, progress, key) {
|
|
|
|
if (error) {
|
|
|
|
reject(error);
|
|
|
|
} else if (key) {
|
|
|
|
resolve(new Wallet(new Buffer(key)));
|
|
|
|
} else if (progressCallback) {
|
|
|
|
progressCallback(progress);
|
|
|
|
}
|
|
|
|
});
|
2016-08-02 02:24:52 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2016-08-04 02:36:44 +03:00
|
|
|
utils.defineProperty(Wallet, 'providers', providers);
|
|
|
|
|
2016-11-22 01:46:36 +03:00
|
|
|
utils.defineProperty(Wallet, 'Contract', Contract);
|
|
|
|
utils.defineProperty(Wallet, 'Interface', Contract.Interface);
|
2016-08-04 02:36:44 +03:00
|
|
|
|
2016-07-27 09:53:40 +03:00
|
|
|
utils.defineProperty(Wallet, 'randomish', new Randomish());
|
|
|
|
|
2016-07-14 20:43:15 +03:00
|
|
|
module.exports = Wallet;
|