2018-06-13 22:39:39 +03:00
|
|
|
'use strict';
|
2018-06-18 12:42:41 +03:00
|
|
|
var __extends = (this && this.__extends) || (function () {
|
|
|
|
var extendStatics = Object.setPrototypeOf ||
|
|
|
|
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
|
|
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
|
|
|
|
return function (d, b) {
|
|
|
|
extendStatics(d, b);
|
|
|
|
function __() { this.constructor = d; }
|
|
|
|
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
|
|
};
|
|
|
|
})();
|
2018-06-13 22:39:39 +03:00
|
|
|
var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
|
if (mod && mod.__esModule) return mod;
|
|
|
|
var result = {};
|
|
|
|
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
|
|
|
|
result["default"] = mod;
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
2018-06-15 11:18:17 +03:00
|
|
|
var hdnode_1 = require("./hdnode");
|
|
|
|
var secretStorage = __importStar(require("./secret-storage"));
|
|
|
|
var signing_key_1 = require("./signing-key");
|
2018-06-17 23:47:28 +03:00
|
|
|
var bytes_1 = require("../utils/bytes");
|
2018-06-18 12:42:41 +03:00
|
|
|
var hash_1 = require("../utils/hash");
|
2018-06-13 22:39:39 +03:00
|
|
|
var keccak256_1 = require("../utils/keccak256");
|
2018-06-17 23:32:57 +03:00
|
|
|
var properties_1 = require("../utils/properties");
|
2018-06-13 22:39:39 +03:00
|
|
|
var random_bytes_1 = require("../utils/random-bytes");
|
2018-06-17 23:32:57 +03:00
|
|
|
var transaction_1 = require("../utils/transaction");
|
2018-06-13 22:39:39 +03:00
|
|
|
var errors = __importStar(require("../utils/errors"));
|
2018-06-18 12:42:41 +03:00
|
|
|
var Signer = /** @class */ (function () {
|
|
|
|
function Signer() {
|
|
|
|
}
|
|
|
|
return Signer;
|
|
|
|
}());
|
|
|
|
exports.Signer = Signer;
|
|
|
|
var Wallet = /** @class */ (function (_super) {
|
|
|
|
__extends(Wallet, _super);
|
2018-06-13 22:39:39 +03:00
|
|
|
function Wallet(privateKey, provider) {
|
2018-06-18 12:42:41 +03:00
|
|
|
var _this = _super.call(this) || this;
|
|
|
|
errors.checkNew(_this, Wallet);
|
2018-06-13 22:39:39 +03:00
|
|
|
// Make sure we have a valid signing key
|
|
|
|
if (privateKey instanceof signing_key_1.SigningKey) {
|
2018-06-18 12:42:41 +03:00
|
|
|
properties_1.defineReadOnly(_this, 'signingKey', privateKey);
|
2018-06-13 22:39:39 +03:00
|
|
|
}
|
|
|
|
else {
|
2018-06-18 12:42:41 +03:00
|
|
|
properties_1.defineReadOnly(_this, 'signingKey', new signing_key_1.SigningKey(privateKey));
|
2018-06-13 22:39:39 +03:00
|
|
|
}
|
2018-06-18 12:42:41 +03:00
|
|
|
properties_1.defineReadOnly(_this, 'provider', provider);
|
|
|
|
return _this;
|
2018-06-13 22:39:39 +03:00
|
|
|
}
|
2018-06-19 09:12:57 +03:00
|
|
|
Object.defineProperty(Wallet.prototype, "address", {
|
|
|
|
get: function () { return this.signingKey.address; },
|
|
|
|
enumerable: true,
|
|
|
|
configurable: true
|
|
|
|
});
|
|
|
|
Object.defineProperty(Wallet.prototype, "mnemonic", {
|
|
|
|
get: function () { return this.signingKey.mnemonic; },
|
|
|
|
enumerable: true,
|
|
|
|
configurable: true
|
|
|
|
});
|
|
|
|
Object.defineProperty(Wallet.prototype, "path", {
|
|
|
|
get: function () { return this.signingKey.mnemonic; },
|
|
|
|
enumerable: true,
|
|
|
|
configurable: true
|
|
|
|
});
|
|
|
|
Object.defineProperty(Wallet.prototype, "privateKey", {
|
|
|
|
get: function () { return this.signingKey.privateKey; },
|
|
|
|
enumerable: true,
|
|
|
|
configurable: true
|
|
|
|
});
|
|
|
|
/**
|
|
|
|
* Create a new instance of this Wallet connected to provider.
|
|
|
|
*/
|
2018-06-18 12:42:41 +03:00
|
|
|
Wallet.prototype.connect = function (provider) {
|
|
|
|
return new Wallet(this.signingKey, provider);
|
2018-06-13 22:39:39 +03:00
|
|
|
};
|
|
|
|
Wallet.prototype.getAddress = function () {
|
|
|
|
return Promise.resolve(this.address);
|
|
|
|
};
|
2018-06-18 12:42:41 +03:00
|
|
|
Wallet.prototype.sign = function (transaction) {
|
|
|
|
var _this = this;
|
|
|
|
return properties_1.resolveProperties(transaction).then(function (tx) {
|
2018-06-26 04:02:20 +03:00
|
|
|
return transaction_1.serialize(tx, _this.signingKey.signDigest.bind(_this.signingKey));
|
2018-06-18 12:42:41 +03:00
|
|
|
});
|
|
|
|
};
|
|
|
|
Wallet.prototype.signMessage = function (message) {
|
|
|
|
return Promise.resolve(bytes_1.joinSignature(this.signingKey.signDigest(hash_1.hashMessage(message))));
|
|
|
|
};
|
2018-06-13 22:39:39 +03:00
|
|
|
Wallet.prototype.getBalance = function (blockTag) {
|
|
|
|
if (!this.provider) {
|
|
|
|
throw new Error('missing provider');
|
|
|
|
}
|
|
|
|
return this.provider.getBalance(this.address, blockTag);
|
|
|
|
};
|
|
|
|
Wallet.prototype.getTransactionCount = function (blockTag) {
|
|
|
|
if (!this.provider) {
|
|
|
|
throw new Error('missing provider');
|
|
|
|
}
|
|
|
|
return this.provider.getTransactionCount(this.address, blockTag);
|
|
|
|
};
|
|
|
|
Wallet.prototype.sendTransaction = function (transaction) {
|
|
|
|
if (!this.provider) {
|
|
|
|
throw new Error('missing provider');
|
|
|
|
}
|
|
|
|
if (!transaction || typeof (transaction) !== 'object') {
|
|
|
|
throw new Error('invalid transaction object');
|
|
|
|
}
|
2018-06-17 23:32:57 +03:00
|
|
|
var tx = properties_1.shallowCopy(transaction);
|
|
|
|
if (tx.to != null) {
|
|
|
|
tx.to = this.provider.resolveName(tx.to);
|
2018-06-13 22:39:39 +03:00
|
|
|
}
|
2018-06-17 23:32:57 +03:00
|
|
|
if (tx.gasLimit == null) {
|
2018-06-25 01:47:36 +03:00
|
|
|
tx.from = this.getAddress();
|
2018-06-18 12:42:41 +03:00
|
|
|
tx.gasLimit = this.provider.estimateGas(tx);
|
2018-06-13 22:39:39 +03:00
|
|
|
}
|
2018-06-17 23:32:57 +03:00
|
|
|
if (tx.gasPrice == null) {
|
2018-06-18 12:42:41 +03:00
|
|
|
tx.gasPrice = this.provider.getGasPrice();
|
2018-06-13 22:39:39 +03:00
|
|
|
}
|
2018-06-17 23:32:57 +03:00
|
|
|
if (tx.nonce == null) {
|
|
|
|
tx.nonce = this.getTransactionCount();
|
2018-06-13 22:39:39 +03:00
|
|
|
}
|
2018-06-17 23:32:57 +03:00
|
|
|
if (tx.chainId == null) {
|
|
|
|
tx.chainId = this.provider.getNetwork().then(function (network) { return network.chainId; });
|
2018-06-13 22:39:39 +03:00
|
|
|
}
|
2018-06-22 03:41:09 +03:00
|
|
|
return this.provider.sendTransaction(this.sign(tx));
|
2018-06-13 22:39:39 +03:00
|
|
|
};
|
|
|
|
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');
|
|
|
|
}
|
|
|
|
if (!options) {
|
|
|
|
options = {};
|
|
|
|
}
|
|
|
|
if (this.mnemonic) {
|
|
|
|
// Make sure we don't accidentally bubble the mnemonic up the call-stack
|
|
|
|
var safeOptions = {};
|
|
|
|
for (var key in options) {
|
|
|
|
safeOptions[key] = options[key];
|
|
|
|
}
|
|
|
|
options = safeOptions;
|
|
|
|
// Set the mnemonic and path
|
|
|
|
options.mnemonic = this.mnemonic;
|
|
|
|
options.path = this.path;
|
|
|
|
}
|
|
|
|
return secretStorage.encrypt(this.privateKey, password, options, progressCallback);
|
|
|
|
};
|
2018-06-19 09:12:57 +03:00
|
|
|
/**
|
|
|
|
* Static methods to create Wallet instances.
|
|
|
|
*/
|
2018-06-13 22:39:39 +03:00
|
|
|
Wallet.createRandom = function (options) {
|
|
|
|
var entropy = random_bytes_1.randomBytes(16);
|
|
|
|
if (!options) {
|
|
|
|
options = {};
|
|
|
|
}
|
|
|
|
if (options.extraEntropy) {
|
2018-06-17 23:47:28 +03:00
|
|
|
entropy = bytes_1.arrayify(keccak256_1.keccak256(bytes_1.concat([entropy, options.extraEntropy])).substring(0, 34));
|
2018-06-13 22:39:39 +03:00
|
|
|
}
|
2018-06-21 03:29:54 +03:00
|
|
|
var mnemonic = hdnode_1.entropyToMnemonic(entropy, options.locale);
|
|
|
|
return Wallet.fromMnemonic(mnemonic, options.path, options.locale);
|
2018-06-13 22:39:39 +03:00
|
|
|
};
|
2018-06-22 09:18:19 +03:00
|
|
|
Wallet.fromEncryptedJson = function (json, password, progressCallback) {
|
2018-07-03 23:44:05 +03:00
|
|
|
if (secretStorage.isCrowdsaleWallet(json)) {
|
|
|
|
try {
|
|
|
|
var privateKey = secretStorage.decryptCrowdsale(json, password);
|
|
|
|
return Promise.resolve(new Wallet(privateKey));
|
2018-06-13 22:39:39 +03:00
|
|
|
}
|
2018-07-03 23:44:05 +03:00
|
|
|
catch (error) {
|
|
|
|
return Promise.reject(error);
|
2018-06-13 22:39:39 +03:00
|
|
|
}
|
2018-07-03 23:44:05 +03:00
|
|
|
}
|
|
|
|
else if (secretStorage.isValidWallet(json)) {
|
|
|
|
return secretStorage.decrypt(json, password, progressCallback).then(function (signingKey) {
|
|
|
|
return new Wallet(signingKey);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return Promise.reject('invalid wallet JSON');
|
2018-06-13 22:39:39 +03:00
|
|
|
};
|
2018-06-21 03:29:54 +03:00
|
|
|
Wallet.fromMnemonic = function (mnemonic, path, wordlist) {
|
2018-06-13 22:39:39 +03:00
|
|
|
if (!path) {
|
2018-06-18 12:42:41 +03:00
|
|
|
path = hdnode_1.defaultPath;
|
2018-06-13 22:39:39 +03:00
|
|
|
}
|
2018-06-21 03:29:54 +03:00
|
|
|
return new Wallet(hdnode_1.fromMnemonic(mnemonic, wordlist).derivePath(path));
|
2018-06-13 22:39:39 +03:00
|
|
|
};
|
2018-06-19 09:12:57 +03:00
|
|
|
/**
|
|
|
|
* Determine if this is an encryped JSON wallet.
|
|
|
|
*/
|
|
|
|
Wallet.isEncryptedWallet = function (json) {
|
|
|
|
return (secretStorage.isValidWallet(json) || secretStorage.isCrowdsaleWallet(json));
|
|
|
|
};
|
|
|
|
/**
|
|
|
|
* Verify a signed message, returning the address of the signer.
|
|
|
|
*/
|
2018-06-18 12:42:41 +03:00
|
|
|
Wallet.verifyMessage = function (message, signature) {
|
|
|
|
signature = bytes_1.hexlify(signature);
|
|
|
|
if (signature.length != 132) {
|
|
|
|
throw new Error('invalid signature');
|
|
|
|
}
|
|
|
|
var digest = hash_1.hashMessage(message);
|
|
|
|
var recoveryParam = parseInt(signature.substring(130), 16);
|
|
|
|
if (recoveryParam >= 27) {
|
|
|
|
recoveryParam -= 27;
|
|
|
|
}
|
|
|
|
if (recoveryParam < 0) {
|
|
|
|
throw new Error('invalid signature');
|
|
|
|
}
|
|
|
|
return signing_key_1.recoverAddress(digest, {
|
|
|
|
r: signature.substring(0, 66),
|
|
|
|
s: '0x' + signature.substring(66, 130),
|
|
|
|
recoveryParam: recoveryParam
|
|
|
|
});
|
|
|
|
};
|
2018-06-13 22:39:39 +03:00
|
|
|
return Wallet;
|
2018-06-18 12:42:41 +03:00
|
|
|
}(Signer));
|
2018-06-13 22:39:39 +03:00
|
|
|
exports.Wallet = Wallet;
|