ethers.js/index.js

101 lines
3.3 KiB
JavaScript
Raw Normal View History

2016-07-25 10:55:16 +03:00
'use strict';
2016-07-14 20:43:15 +03:00
var rlp = require('rlp');
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');
var providers = require('./lib/providers.js');
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-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-07-16 06:47:35 +03:00
// http://ethereum.stackexchange.com/questions/760/how-is-the-address-of-an-ethereum-contract-computed
2016-07-22 00:06:42 +03:00
utils.defineProperty(exportUtils, 'getContractAddress', function(transaction) {
return SigningKey.getAddress('0x' + utils.sha3(rlp.encode([
2016-08-05 04:37:15 +03:00
utils.hexOrBuffer(SigningKey.getAddress(transaction.from)),
2016-07-16 06:47:35 +03:00
utils.hexOrBuffer(utils.hexlify(transaction.nonce, 'nonce'))
2016-07-22 00:06:42 +03:00
])).slice(12).toString('hex'));
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-03 00:46:20 +03:00
//utils.defineProperty(Wallet, 'getAddress', SigningKey.getAddress);
//utils.defineProperty(Wallet, 'getIcapAddress', SigningKey.getIcapAddress);
2016-07-14 20:43:15 +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));
});
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);
});
});
});
utils.defineProperty(Wallet.prototype, 'encrypt', function(password, options, progressCallback) {
if (typeof(options) === 'function' && !progressCallback) {
progressCallback = options;
options = {};
}
if (progressCallback && typeof(progressCallback) !== 'function') {
throw new Error('invalid callback');
}
return secretStorage.encrypt(this.privateKey, password, options, progressCallback);
});
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
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
});
});
utils.defineProperty(Wallet, 'providers', providers);
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;