@paragon-wallet/novo-simple-keyring
Version:
A simple standard interface for a series of Novo private keys.
133 lines (132 loc) • 5.59 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SimpleKeyring = void 0;
const novo = __importStar(require("@paragon-wallet/novocore-lib"));
const events_1 = require("events");
const type = "Simple Key Pair";
class SimpleKeyring extends events_1.EventEmitter {
constructor(opts) {
super();
this.type = type;
this.network = "mainnet";
this.wallets = [];
if (opts) {
this.deserialize(opts);
}
}
serialize() {
return __awaiter(this, void 0, void 0, function* () {
return this.wallets.map((privateKey) => privateKey.toWIF());
});
}
deserialize(opts) {
return __awaiter(this, void 0, void 0, function* () {
let wifs = opts;
this.wallets = wifs.map((wif) => novo.PrivateKey.fromWIF(wif));
});
}
addAccounts(n = 1) {
return __awaiter(this, void 0, void 0, function* () {
const newWallets = [];
for (let i = 0; i < n; i++) {
newWallets.push(novo.PrivateKey.fromRandom());
}
this.wallets = this.wallets.concat(newWallets);
const hexWallets = newWallets.map(({ publicKey }) => publicKey.toAddress(this.network).toString());
return hexWallets;
});
}
getAccounts() {
return __awaiter(this, void 0, void 0, function* () {
return this.wallets.map(({ publicKey }) => publicKey.toAddress(this.network).toString());
});
}
signTransaction(address, tx) {
return __awaiter(this, void 0, void 0, function* () {
const privKey = this._getPrivateKeyFor(address);
const sigtype = novo.crypto.Signature.SIGHASH_ALL;
tx.inputs.forEach((input, inputIndex) => {
const sig = new novo.Transaction.Signature({
publicKey: privKey.toPublicKey(),
prevTxId: input.prevTxId,
outputIndex: input.outputIndex,
inputIndex,
signature: novo.Transaction.Sighash.sign(tx, privKey, sigtype, inputIndex, input.output.script, input.output.satoshisBN),
sigtype,
});
input.setScript(novo.Script.buildPublicKeyHashIn(sig.publicKey, sig.signature.toDER(), sig.sigtype));
});
return tx;
});
}
signMessage(address, message) {
return __awaiter(this, void 0, void 0, function* () {
const privKey = this._getPrivateKeyFor(address);
const sig = novo.Message.sign(message, privKey);
return sig;
});
}
verifyMessage(withAccount, message, sig) {
return __awaiter(this, void 0, void 0, function* () {
return novo.Message.verify(message, withAccount, sig);
});
}
_getPrivateKeyFor(address) {
if (!address) {
throw new Error("Must specify address.");
}
const wallet = this._getWalletForAccount(address);
return wallet;
}
exportAccount(address) {
return __awaiter(this, void 0, void 0, function* () {
const wallet = this._getWalletForAccount(address);
return wallet.toString();
});
}
removeAccount(address) {
if (!this.wallets
.map(({ publicKey }) => publicKey.toAddress(this.network).toString())
.includes(address)) {
throw new Error(`Address ${address} not found in this keyring`);
}
this.wallets = this.wallets.filter(({ publicKey }) => publicKey.toAddress(this.network).toString() !== address);
}
_getWalletForAccount(address) {
let wallet = this.wallets.find(({ publicKey }) => publicKey.toAddress(this.network).toString() == address);
if (!wallet) {
throw new Error("Simple Keyring - Unable to find matching address.");
}
return wallet;
}
}
exports.SimpleKeyring = SimpleKeyring;
SimpleKeyring.type = type;