1 | "use strict";
|
2 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
3 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
4 | };
|
5 | Object.defineProperty(exports, "__esModule", { value: true });
|
6 | const url_1 = __importDefault(require("url"));
|
7 | const bitcoinjs_lib_1 = require("bitcoinjs-lib");
|
8 | const config_1 = require("./config");
|
9 | const logger_1 = require("./logger");
|
10 | exports.BLOCKSTACK_HANDLER = 'blockstack';
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | function nextYear() {
|
16 | return new Date(new Date().setFullYear(new Date().getFullYear() + 1));
|
17 | }
|
18 | exports.nextYear = nextYear;
|
19 | function nextMonth() {
|
20 | return new Date(new Date().setMonth(new Date().getMonth() + 1));
|
21 | }
|
22 | exports.nextMonth = nextMonth;
|
23 | function nextHour() {
|
24 | return new Date(new Date().setHours(new Date().getHours() + 1));
|
25 | }
|
26 | exports.nextHour = nextHour;
|
27 |
|
28 |
|
29 |
|
30 |
|
31 | function 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 | }
|
41 | exports.updateQueryStringParameter = updateQueryStringParameter;
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 | function 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 | }
|
68 | exports.isLaterVersion = isLaterVersion;
|
69 | function 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 | }
|
89 | exports.hexStringToECPair = hexStringToECPair;
|
90 | function 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 | }
|
99 | exports.ecPairToHexString = ecPairToHexString;
|
100 | function ecPairToAddress(keyPair) {
|
101 | return bitcoinjs_lib_1.address.toBase58Check(bitcoinjs_lib_1.crypto.hash160(keyPair.publicKey), keyPair.network.pubKeyHash);
|
102 | }
|
103 | exports.ecPairToAddress = ecPairToAddress;
|
104 |
|
105 |
|
106 |
|
107 |
|
108 | function makeUUID4() {
|
109 | let d = new Date().getTime();
|
110 | if (typeof performance !== 'undefined' && typeof performance.now === 'function') {
|
111 | d += performance.now();
|
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 | }
|
119 | exports.makeUUID4 = makeUUID4;
|
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 | function 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 | }
|
141 | exports.isSameOriginAbsoluteUrl = isSameOriginAbsoluteUrl;
|
142 |
|
143 |
|
144 |
|
145 |
|
146 |
|
147 |
|
148 |
|
149 |
|
150 | function 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 | }
|
159 | exports.checkWindowAPI = checkWindowAPI;
|
160 |
|
\ | No newline at end of file |