@paragon-wallet/novo-hd-keyring
Version:
A simple standard interface for a seed phrase generated set of Novo accounts.
186 lines (185 loc) • 6.53 kB
JavaScript
"use strict";
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.HdKeyring = void 0;
const novo_simple_keyring_1 = require("@paragon-wallet/novo-simple-keyring");
const novo = __importStar(require("@paragon-wallet/novocore-lib"));
const hdPathString = "m/44'/0'/0'/0";
const type = "HD Key Tree";
class HdKeyring extends novo_simple_keyring_1.SimpleKeyring {
/* PUBLIC METHODS */
constructor(opts) {
super(null);
this.type = type;
this.mnemonic = null;
this.hdPath = hdPathString;
this.root = null;
this.wallets = [];
this._index2wallet = {};
this.activeIndexes = [];
this.page = 0;
this.perPage = 5;
this.deserialize(opts);
}
serialize() {
return __awaiter(this, void 0, void 0, function* () {
return {
mnemonic: this.mnemonic,
activeIndexes: this.activeIndexes,
hdPath: this.hdPath,
};
});
}
deserialize(_opts = {}) {
return __awaiter(this, void 0, void 0, function* () {
let opts = _opts;
this.wallets = [];
this.mnemonic = null;
this.root = null;
this.hdPath = opts.hdPath || hdPathString;
if (opts.mnemonic) {
this.initFromMnemonic(opts.mnemonic);
}
if (opts.activeIndexes) {
this.activeAccounts(opts.activeIndexes);
}
});
}
initFromMnemonic(mnemonic) {
this.mnemonic = mnemonic;
this._index2wallet = {};
this.hdWallet = novo.Mnemonic.fromString(mnemonic);
this.root = this.hdWallet
.toHDPrivateKey(this.phrase, this.network)
.deriveChild(this.hdPath);
}
addAccounts(numberOfAccounts = 1) {
if (!this.root) {
this.initFromMnemonic(novo.Mnemonic.fromRandom().toString());
}
let count = numberOfAccounts;
let currentIdx = 0;
const newWallets = [];
while (count) {
const [, wallet] = this._addressFromIndex(currentIdx);
if (this.wallets.includes(wallet)) {
currentIdx++;
}
else {
this.wallets.push(wallet);
newWallets.push(wallet);
this.activeIndexes.push(currentIdx);
count--;
}
}
const hexWallets = newWallets.map((w) => {
return w.toAddress(this.network).toString();
});
return Promise.resolve(hexWallets);
}
activeAccounts(indexes) {
const accounts = [];
for (const index of indexes) {
const [address, wallet] = this._addressFromIndex(index);
this.wallets.push(wallet);
this.activeIndexes.push(index);
accounts.push(address);
}
return accounts;
}
getFirstPage() {
this.page = 0;
return this.__getPage(1);
}
getNextPage() {
return this.__getPage(1);
}
getPreviousPage() {
return this.__getPage(-1);
}
getAddresses(start, end) {
const from = start;
const to = end;
const accounts = [];
for (let i = from; i < to; i++) {
const [address] = this._addressFromIndex(i);
accounts.push({
address,
index: i + 1,
});
}
return accounts;
}
__getPage(increment) {
return __awaiter(this, void 0, void 0, function* () {
this.page += increment;
if (!this.page || this.page <= 0) {
this.page = 1;
}
const from = (this.page - 1) * this.perPage;
const to = from + this.perPage;
const accounts = [];
for (let i = from; i < to; i++) {
const [address] = this._addressFromIndex(i);
accounts.push({
address,
index: i + 1,
});
}
return accounts;
});
}
getAccounts() {
return __awaiter(this, void 0, void 0, function* () {
return this.wallets.map((w) => {
return w.toAddress(this.network).toString();
});
});
}
getIndexByAddress(address) {
for (const key in this._index2wallet) {
if (this._index2wallet[key][0] === address) {
return Number(key);
}
}
return null;
}
_addressFromIndex(i) {
if (!this._index2wallet[i]) {
const child = this.root.deriveChild(i);
const privateKey = child.privateKey;
const address = privateKey.toAddress(this.network).toString();
this._index2wallet[i] = [address, privateKey];
}
return this._index2wallet[i];
}
}
exports.HdKeyring = HdKeyring;
HdKeyring.type = type;