tnb-hd-wallet
Version:
A hd wallet that derives public and private keys from a 12 word mnemonic phrase with support
66 lines (65 loc) • 2.96 kB
JavaScript
;
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());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.TnbHdWallet = void 0;
const axios_1 = __importDefault(require("axios"));
const webHd_1 = require("./webHd");
class TnbHdWallet extends webHd_1.WebHdWallet {
constructor(seed, nodeUrl, configOptions = { addressGapLimit: 5 }) {
super(seed, "thenewboston", configOptions);
this.pvUrl = "";
this.getPvUrl = () => __awaiter(this, void 0, void 0, function* () {
if (!this.nodeUrl)
throw new Error("Did you add the nodeUrl parameter when initializing this HdWallet? Try 'HdWallet.thenewboston(mnemonic, nodeUrl)'");
const data = (yield axios_1.default.get(`${this.nodeUrl}/config`)).data;
const { protocol, ip_address, port } = data.primary_validator;
if (port) {
this.pvUrl = [protocol, "://", ip_address, ":", port].join("");
}
else {
this.pvUrl = [protocol, "://", ip_address].join("");
}
});
this.getBalanceLock = (accountNumber) => __awaiter(this, void 0, void 0, function* () {
if (!this.pvUrl) {
yield this.getPvUrl();
}
let data;
try {
data = (yield axios_1.default.get(`${this.pvUrl}/accounts/${accountNumber}/balance_lock`)).data;
}
catch (_a) {
this.getPvUrl();
data = (yield axios_1.default.get(`${this.pvUrl}/accounts/${accountNumber}/balance_lock`)).data;
}
return data.balance_lock;
});
if (nodeUrl) {
this.nodeUrl = nodeUrl;
}
else {
this.nodeUrl = null;
}
}
hasAddressBeenUsed(publicKey) {
return __awaiter(this, void 0, void 0, function* () {
return !!(yield this.getBalanceLock(publicKey));
});
}
formatAddress(address) {
delete address.chainCode;
return Object.assign(Object.assign({}, address), { publicKey: address.publicKey.slice(2) });
}
}
exports.TnbHdWallet = TnbHdWallet;