@walletpack/core
Version:
> TODO: description
149 lines (128 loc) • 4.56 kB
JavaScript
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _aesOop = _interopRequireDefault(require("aes-oop"));
var _Blockchains = require("./Blockchains");
var _IdGenerator = _interopRequireDefault(require("../util/IdGenerator"));
var _ExternalWallet = _interopRequireDefault(require("./hardware/ExternalWallet"));
var _StoreService = _interopRequireDefault(require("../services/utility/StoreService"));
var Keypair =
/*#__PURE__*/
function () {
function Keypair() {
var blockchains = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
(0, _classCallCheck2["default"])(this, Keypair);
this.id = _IdGenerator["default"].text(24);
this.name = '';
this.privateKey = '';
this.external = null;
this.fork = null;
this.publicKeys = [];
this.blockchains = blockchains;
this.createdAt = +new Date();
}
(0, _createClass2["default"])(Keypair, [{
key: "resetExternal",
value: function resetExternal() {
this.external["interface"].close();
this.external["interface"].open(); // this.external = ExternalWallet.fromJson(this.external);
}
}, {
key: "accounts",
value: function accounts() {
var _this = this;
var unique = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
var accounts = _StoreService["default"].get().state.scatter.keychain.accounts.filter(function (x) {
return x.keypairUnique === _this.unique();
});
if (!unique) return accounts;
return accounts.reduce(function (acc, account) {
if (!acc.find(function (x) {
return account.network().unique() === x.network().unique() && account.sendable() === x.sendable();
})) acc.push(account);
return acc;
}, []);
}
}, {
key: "enabledKey",
value: function enabledKey() {
var _this2 = this;
return this.publicKeys.find(function (x) {
return x.blockchain === _this2.blockchains[0];
});
}
}, {
key: "isUnique",
value: function isUnique() {
var _this3 = this;
return !_StoreService["default"].get().state.scatter.keychain.keypairs.find(function (x) {
return x.enabledKey().key === _this3.enabledKey().key;
});
}
}, {
key: "setName",
value: function setName() {
this.name = "".concat((0, _Blockchains.blockchainName)(this.enabledKey().blockchain), " Key - ").concat(new Date().toDateString(), " - ").concat(_IdGenerator["default"].text(4));
}
}, {
key: "unique",
value: function unique() {
return this.id;
}
}, {
key: "clone",
value: function clone() {
return Keypair.fromJson(JSON.parse(JSON.stringify(this)));
}
/***
* Checks whether a private key is encrypted
* @returns {boolean}
*/
}, {
key: "isEncrypted",
value: function isEncrypted() {
return typeof this.privateKey === 'string' && this.privateKey.length > 100;
}
/***
* Encrypts this Keypair's Private Key
* @param seed - The seed to encrypt with
*/
}, {
key: "encrypt",
value: function encrypt(seed) {
if (!this.isEncrypted()) this.privateKey = _aesOop["default"].encrypt(this.privateKey, seed);
}
/***
* Decrypts this Keypair's Private Key
* @param seed - The seed to decrypt with
*/
}, {
key: "decrypt",
value: function decrypt(seed) {
if (this.isEncrypted()) {
this.privateKey = _aesOop["default"].decrypt(this.privateKey, seed);
if ((0, _typeof2["default"])(this.privateKey) === 'object' && this.privateKey.hasOwnProperty('data')) this.privateKey = this.privateKey.data;
}
}
}], [{
key: "placeholder",
value: function placeholder(blockchains) {
return new Keypair(blockchains);
}
}, {
key: "fromJson",
value: function fromJson(json) {
var p = Object.assign(this.placeholder(), json);
if (json.hasOwnProperty('external') && !!json.external) p.external = _ExternalWallet["default"].fromJson(json.external);
return p;
}
}]);
return Keypair;
}();
exports["default"] = Keypair;