UNPKG

5.68 kBJavaScriptView Raw
1"use strict";
2var __importDefault = (this && this.__importDefault) || function (mod) {
3 return (mod && mod.__esModule) ? mod : { "default": mod };
4};
5Object.defineProperty(exports, "__esModule", { value: true });
6const url_1 = __importDefault(require("url"));
7const bitcoinjs_lib_1 = require("bitcoinjs-lib");
8const config_1 = require("./config");
9const logger_1 = require("./logger");
10exports.BLOCKSTACK_HANDLER = 'blockstack';
11/**
12 * Time
13 * @private
14 */
15function nextYear() {
16 return new Date(new Date().setFullYear(new Date().getFullYear() + 1));
17}
18exports.nextYear = nextYear;
19function nextMonth() {
20 return new Date(new Date().setMonth(new Date().getMonth() + 1));
21}
22exports.nextMonth = nextMonth;
23function nextHour() {
24 return new Date(new Date().setHours(new Date().getHours() + 1));
25}
26exports.nextHour = nextHour;
27/**
28 * Query Strings
29 * @private
30 */
31function updateQueryStringParameter(uri, key, value) {
32 const re = new RegExp(`([?&])${key}=.*?(&|$)`, 'i');
33 const separator = uri.indexOf('?') !== -1 ? '&' : '?';
34 if (uri.match(re)) {
35 return uri.replace(re, `$1${key}=${value}$2`);
36 }
37 else {
38 return `${uri}${separator}${key}=${value}`;
39 }
40}
41exports.updateQueryStringParameter = updateQueryStringParameter;
42/**
43 * Versioning
44 * @param {string} v1 - the left half of the version inequality
45 * @param {string} v2 - right half of the version inequality
46 * @returns {bool} iff v1 >= v2
47 * @private
48 */
49function isLaterVersion(v1, v2) {
50 if (v1 === undefined) {
51 v1 = '0.0.0';
52 }
53 if (v2 === undefined) {
54 v2 = '0.0.0';
55 }
56 const v1tuple = v1.split('.').map(x => parseInt(x, 10));
57 const v2tuple = v2.split('.').map(x => parseInt(x, 10));
58 for (let index = 0; index < v2.length; index++) {
59 if (index >= v1.length) {
60 v2tuple.push(0);
61 }
62 if (v1tuple[index] < v2tuple[index]) {
63 return false;
64 }
65 }
66 return true;
67}
68exports.isLaterVersion = isLaterVersion;
69function hexStringToECPair(skHex) {
70 const ecPairOptions = {
71 network: config_1.config.network.layer1,
72 compressed: true
73 };
74 if (skHex.length === 66) {
75 if (skHex.slice(64) !== '01') {
76 throw new Error('Improperly formatted private-key hex string. 66-length hex usually '
77 + 'indicates compressed key, but last byte must be == 1');
78 }
79 return bitcoinjs_lib_1.ECPair.fromPrivateKey(Buffer.from(skHex.slice(0, 64), 'hex'), ecPairOptions);
80 }
81 else if (skHex.length === 64) {
82 ecPairOptions.compressed = false;
83 return bitcoinjs_lib_1.ECPair.fromPrivateKey(Buffer.from(skHex, 'hex'), ecPairOptions);
84 }
85 else {
86 throw new Error('Improperly formatted private-key hex string: length should be 64 or 66.');
87 }
88}
89exports.hexStringToECPair = hexStringToECPair;
90function ecPairToHexString(secretKey) {
91 const ecPointHex = secretKey.privateKey.toString('hex');
92 if (secretKey.compressed) {
93 return `${ecPointHex}01`;
94 }
95 else {
96 return ecPointHex;
97 }
98}
99exports.ecPairToHexString = ecPairToHexString;
100function ecPairToAddress(keyPair) {
101 return bitcoinjs_lib_1.address.toBase58Check(bitcoinjs_lib_1.crypto.hash160(keyPair.publicKey), keyPair.network.pubKeyHash);
102}
103exports.ecPairToAddress = ecPairToAddress;
104/**
105 * UUIDs
106 * @private
107 */
108function makeUUID4() {
109 let d = new Date().getTime();
110 if (typeof performance !== 'undefined' && typeof performance.now === 'function') {
111 d += performance.now(); // use high-precision timer if available
112 }
113 return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
114 const r = (d + Math.random() * 16) % 16 | 0;
115 d = Math.floor(d / 16);
116 return (c === 'x' ? r : (r & 0x3 | 0x8)).toString(16);
117 });
118}
119exports.makeUUID4 = makeUUID4;
120/**
121 * Checks if both urls pass the same origin check & are absolute
122 * @param {[type]} uri1 first uri to check
123 * @param {[type]} uri2 second uri to check
124 * @return {Boolean} true if they pass the same origin check
125 * @private
126 */
127function isSameOriginAbsoluteUrl(uri1, uri2) {
128 const parsedUri1 = url_1.default.parse(uri1);
129 const parsedUri2 = url_1.default.parse(uri2);
130 const port1 = parseInt(parsedUri1.port || '0', 10) | 0 || (parsedUri1.protocol === 'https:' ? 443 : 80);
131 const port2 = parseInt(parsedUri2.port || '0', 10) | 0 || (parsedUri2.protocol === 'https:' ? 443 : 80);
132 const match = {
133 scheme: parsedUri1.protocol === parsedUri2.protocol,
134 hostname: parsedUri1.hostname === parsedUri2.hostname,
135 port: port1 === port2,
136 absolute: (uri1.includes('http://') || uri1.includes('https://'))
137 && (uri2.includes('http://') || uri2.includes('https://'))
138 };
139 return match.scheme && match.hostname && match.port && match.absolute;
140}
141exports.isSameOriginAbsoluteUrl = isSameOriginAbsoluteUrl;
142/**
143 * Runtime check for the existence of the global `window` object and the
144 * given API key (name) on `window`. Throws an error if either are not
145 * available in the current environment.
146 * @param fnDesc The function name to include in the thrown error and log.
147 * @param name The name of the key on the `window` object to check for.
148 * @hidden
149 */
150function checkWindowAPI(fnDesc, name) {
151 const api = typeof window !== 'undefined' && window[name];
152 if (!api) {
153 const errMsg = `\`${fnDesc}\` uses the \`window.${name}\` API which is `
154 + ' not available in the current environment.';
155 logger_1.Logger.error(errMsg);
156 throw new Error(errMsg);
157 }
158}
159exports.checkWindowAPI = checkWindowAPI;
160//# sourceMappingURL=utils.js.map
\No newline at end of file