ethers.js/index.js

85 lines
2.9 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 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([
utils.hexOrBuffer(utils.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-07-22 00:06:42 +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, callback) {
if (typeof(callback) !== 'function') { throw new Error('invalid callback'); }
secretStorage.decrypt(json, password, function(error, signingKey, progress) {
if (signingKey) {
return callback(error, new Wallet(signingKey), progress);
}
return callback(error, signingKey, progress);
});
});
utils.defineProperty(Wallet.prototype, 'encrypt', function(password, options, callback) {
if (typeof(options) === 'function' && !callback) {
callback = options;
options = {};
}
if (typeof(callback) !== 'function') { throw new Error('invalid callback'); }
secretStorage.encrypt(this.privateKey, password, options, callback);
});
2016-08-02 02:24:52 +03:00
utils.defineProperty(Wallet, 'summonBrainWallet', function(username, password, callback) {
if (typeof(callback) !== 'function') { throw new Error('invalid callback'); }
scrypt(password, username, (1 << 18), 8, 1, 32, function(error, progress, key) {
if (key) {
return callback(error, new Wallet(new Buffer(key)), 1);
} else {
return callback(error, null, progress);
}
});
});
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;