@paragon-wallet/novo-simple-keyring
Version:
A simple standard interface for a series of Novo private keys.
302 lines (249 loc) • 9.12 kB
JavaScript
import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
import _inheritsLoose from "@babel/runtime/helpers/esm/inheritsLoose";
import _regeneratorRuntime from "@babel/runtime/regenerator";
import * as novo from "@paragon-wallet/novocore-lib";
import { EventEmitter } from "events";
var type = "Simple Key Pair";
export var SimpleKeyring = /*#__PURE__*/function (_EventEmitter) {
_inheritsLoose(SimpleKeyring, _EventEmitter);
function SimpleKeyring(opts) {
var _this;
_this = _EventEmitter.call(this) || this;
_this.type = type;
_this.network = "mainnet";
_this.wallets = [];
if (opts) {
_this.deserialize(opts);
}
return _this;
}
var _proto = SimpleKeyring.prototype;
_proto.serialize = /*#__PURE__*/function () {
var _serialize = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
return _regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
return _context.abrupt("return", this.wallets.map(function (privateKey) {
return privateKey.toWIF();
}));
case 1:
case "end":
return _context.stop();
}
}
}, _callee, this);
}));
function serialize() {
return _serialize.apply(this, arguments);
}
return serialize;
}();
_proto.deserialize = /*#__PURE__*/function () {
var _deserialize = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2(opts) {
var wifs;
return _regeneratorRuntime.wrap(function _callee2$(_context2) {
while (1) {
switch (_context2.prev = _context2.next) {
case 0:
wifs = opts;
this.wallets = wifs.map(function (wif) {
return novo.PrivateKey.fromWIF(wif);
});
case 2:
case "end":
return _context2.stop();
}
}
}, _callee2, this);
}));
function deserialize(_x) {
return _deserialize.apply(this, arguments);
}
return deserialize;
}();
_proto.addAccounts = /*#__PURE__*/function () {
var _addAccounts = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee3(n) {
var _this2 = this;
var newWallets, i, hexWallets;
return _regeneratorRuntime.wrap(function _callee3$(_context3) {
while (1) {
switch (_context3.prev = _context3.next) {
case 0:
if (n === void 0) {
n = 1;
}
newWallets = [];
for (i = 0; i < n; i++) {
newWallets.push(novo.PrivateKey.fromRandom());
}
this.wallets = this.wallets.concat(newWallets);
hexWallets = newWallets.map(function (_ref) {
var publicKey = _ref.publicKey;
return publicKey.toAddress(_this2.network).toString();
});
return _context3.abrupt("return", hexWallets);
case 6:
case "end":
return _context3.stop();
}
}
}, _callee3, this);
}));
function addAccounts(_x2) {
return _addAccounts.apply(this, arguments);
}
return addAccounts;
}();
_proto.getAccounts = /*#__PURE__*/function () {
var _getAccounts = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee4() {
var _this3 = this;
return _regeneratorRuntime.wrap(function _callee4$(_context4) {
while (1) {
switch (_context4.prev = _context4.next) {
case 0:
return _context4.abrupt("return", this.wallets.map(function (_ref2) {
var publicKey = _ref2.publicKey;
return publicKey.toAddress(_this3.network).toString();
}));
case 1:
case "end":
return _context4.stop();
}
}
}, _callee4, this);
}));
function getAccounts() {
return _getAccounts.apply(this, arguments);
}
return getAccounts;
}();
_proto.signTransaction = /*#__PURE__*/function () {
var _signTransaction = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee5(address, tx) {
var privKey, sigtype;
return _regeneratorRuntime.wrap(function _callee5$(_context5) {
while (1) {
switch (_context5.prev = _context5.next) {
case 0:
privKey = this._getPrivateKeyFor(address);
sigtype = novo.crypto.Signature.SIGHASH_ALL;
tx.inputs.forEach(function (input, inputIndex) {
var sig = new novo.Transaction.Signature({
publicKey: privKey.toPublicKey(),
prevTxId: input.prevTxId,
outputIndex: input.outputIndex,
inputIndex: inputIndex,
signature: novo.Transaction.Sighash.sign(tx, privKey, sigtype, inputIndex, input.output.script, input.output.satoshisBN),
sigtype: sigtype
});
input.setScript(novo.Script.buildPublicKeyHashIn(sig.publicKey, sig.signature.toDER(), sig.sigtype));
});
return _context5.abrupt("return", tx);
case 4:
case "end":
return _context5.stop();
}
}
}, _callee5, this);
}));
function signTransaction(_x3, _x4) {
return _signTransaction.apply(this, arguments);
}
return signTransaction;
}();
_proto.signMessage = /*#__PURE__*/function () {
var _signMessage = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee6(address, message) {
var privKey, sig;
return _regeneratorRuntime.wrap(function _callee6$(_context6) {
while (1) {
switch (_context6.prev = _context6.next) {
case 0:
privKey = this._getPrivateKeyFor(address);
sig = novo.Message.sign(message, privKey);
return _context6.abrupt("return", sig);
case 3:
case "end":
return _context6.stop();
}
}
}, _callee6, this);
}));
function signMessage(_x5, _x6) {
return _signMessage.apply(this, arguments);
}
return signMessage;
}();
_proto.verifyMessage = /*#__PURE__*/function () {
var _verifyMessage = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee7(withAccount, message, sig) {
return _regeneratorRuntime.wrap(function _callee7$(_context7) {
while (1) {
switch (_context7.prev = _context7.next) {
case 0:
return _context7.abrupt("return", novo.Message.verify(message, withAccount, sig));
case 1:
case "end":
return _context7.stop();
}
}
}, _callee7);
}));
function verifyMessage(_x7, _x8, _x9) {
return _verifyMessage.apply(this, arguments);
}
return verifyMessage;
}();
_proto._getPrivateKeyFor = function _getPrivateKeyFor(address) {
if (!address) {
throw new Error("Must specify address.");
}
var wallet = this._getWalletForAccount(address);
return wallet;
};
_proto.exportAccount = /*#__PURE__*/function () {
var _exportAccount = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee8(address) {
var wallet;
return _regeneratorRuntime.wrap(function _callee8$(_context8) {
while (1) {
switch (_context8.prev = _context8.next) {
case 0:
wallet = this._getWalletForAccount(address);
return _context8.abrupt("return", wallet.toString());
case 2:
case "end":
return _context8.stop();
}
}
}, _callee8, this);
}));
function exportAccount(_x10) {
return _exportAccount.apply(this, arguments);
}
return exportAccount;
}();
_proto.removeAccount = function removeAccount(address) {
var _this4 = this;
if (!this.wallets.map(function (_ref3) {
var publicKey = _ref3.publicKey;
return publicKey.toAddress(_this4.network).toString();
}).includes(address)) {
throw new Error("Address " + address + " not found in this keyring");
}
this.wallets = this.wallets.filter(function (_ref4) {
var publicKey = _ref4.publicKey;
return publicKey.toAddress(_this4.network).toString() !== address;
});
};
_proto._getWalletForAccount = function _getWalletForAccount(address) {
var _this5 = this;
var wallet = this.wallets.find(function (_ref5) {
var publicKey = _ref5.publicKey;
return publicKey.toAddress(_this5.network).toString() == address;
});
if (!wallet) {
throw new Error("Simple Keyring - Unable to find matching address.");
}
return wallet;
};
return SimpleKeyring;
}(EventEmitter);
SimpleKeyring.type = type;