967570b472975d770f7d36c02321c5890731889d2796add97547d3d63e64c552.json raw
1 {"ast":null,"code":"var __awaiter = this && this.__awaiter || function (thisArg, _arguments, P, generator) {\n function adopt(value) {\n return value instanceof P ? value : new P(function (resolve) {\n resolve(value);\n });\n }\n return new (P || (P = Promise))(function (resolve, reject) {\n function fulfilled(value) {\n try {\n step(generator.next(value));\n } catch (e) {\n reject(e);\n }\n }\n function rejected(value) {\n try {\n step(generator[\"throw\"](value));\n } catch (e) {\n reject(e);\n }\n }\n function step(result) {\n result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected);\n }\n step((generator = generator.apply(thisArg, _arguments || [])).next());\n });\n};\nvar __generator = this && this.__generator || function (thisArg, body) {\n var _ = {\n label: 0,\n sent: function () {\n if (t[0] & 1) throw t[1];\n return t[1];\n },\n trys: [],\n ops: []\n },\n f,\n y,\n t,\n g;\n return g = {\n next: verb(0),\n \"throw\": verb(1),\n \"return\": verb(2)\n }, typeof Symbol === \"function\" && (g[Symbol.iterator] = function () {\n return this;\n }), g;\n function verb(n) {\n return function (v) {\n return step([n, v]);\n };\n }\n function step(op) {\n if (f) throw new TypeError(\"Generator is already executing.\");\n while (g && (g = 0, op[0] && (_ = 0)), _) try {\n if (f = 1, y && (t = op[0] & 2 ? y[\"return\"] : op[0] ? y[\"throw\"] || ((t = y[\"return\"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;\n if (y = 0, t) op = [op[0] & 2, t.value];\n switch (op[0]) {\n case 0:\n case 1:\n t = op;\n break;\n case 4:\n _.label++;\n return {\n value: op[1],\n done: false\n };\n case 5:\n _.label++;\n y = op[1];\n op = [0];\n continue;\n case 7:\n op = _.ops.pop();\n _.trys.pop();\n continue;\n default:\n if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {\n _ = 0;\n continue;\n }\n if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {\n _.label = op[1];\n break;\n }\n if (op[0] === 6 && _.label < t[1]) {\n _.label = t[1];\n t = op;\n break;\n }\n if (t && _.label < t[2]) {\n _.label = t[2];\n _.ops.push(op);\n break;\n }\n if (t[2]) _.ops.pop();\n _.trys.pop();\n continue;\n }\n op = body.call(thisArg, _);\n } catch (e) {\n op = [6, e];\n y = 0;\n } finally {\n f = t = 0;\n }\n if (op[0] & 5) throw op[1];\n return {\n value: op[0] ? op[1] : void 0,\n done: true\n };\n }\n};\nimport { ConnectionManager } from './WSConnection.js';\nimport { MeltQuoteState } from './model/types/index.js';\nimport request from './request.js';\nimport { isObj, joinUrls, sanitizeUrl } from './utils.js';\nimport { handleMeltQuoteResponseDeprecated } from './legacy/nut-05.js';\nimport { handleMintQuoteResponseDeprecated } from './legacy/nut-04.js';\nimport { handleMintInfoContactFieldDeprecated } from './legacy/nut-06.js';\n/**\n * Class represents Cashu Mint API. This class contains Lower level functions that are implemented by CashuWallet.\n */\nvar CashuMint = /** @class */function () {\n /**\n * @param _mintUrl requires mint URL to create this object\n * @param _customRequest if passed, use custom request implementation for network communication with the mint\n */\n function CashuMint(_mintUrl, _customRequest) {\n this._mintUrl = _mintUrl;\n this._customRequest = _customRequest;\n this._mintUrl = sanitizeUrl(_mintUrl);\n this._customRequest = _customRequest;\n }\n Object.defineProperty(CashuMint.prototype, \"mintUrl\", {\n get: function () {\n return this._mintUrl;\n },\n enumerable: false,\n configurable: true\n });\n /**\n * fetches mints info at the /info endpoint\n * @param mintUrl\n * @param customRequest\n */\n CashuMint.getInfo = function (mintUrl, customRequest) {\n return __awaiter(this, void 0, void 0, function () {\n var requestInstance, response, data;\n return __generator(this, function (_a) {\n switch (_a.label) {\n case 0:\n requestInstance = customRequest || request;\n return [4 /*yield*/, requestInstance({\n endpoint: joinUrls(mintUrl, '/v1/info')\n })];\n case 1:\n response = _a.sent();\n data = handleMintInfoContactFieldDeprecated(response);\n return [2 /*return*/, data];\n }\n });\n });\n };\n /**\n * fetches mints info at the /info endpoint\n */\n CashuMint.prototype.getInfo = function () {\n return __awaiter(this, void 0, void 0, function () {\n return __generator(this, function (_a) {\n return [2 /*return*/, CashuMint.getInfo(this._mintUrl, this._customRequest)];\n });\n });\n };\n /**\n * Performs a swap operation with ecash inputs and outputs.\n * @param mintUrl\n * @param swapPayload payload containing inputs and outputs\n * @param customRequest\n * @returns signed outputs\n */\n CashuMint.swap = function (mintUrl, swapPayload, customRequest) {\n var _a;\n return __awaiter(this, void 0, void 0, function () {\n var requestInstance, data;\n return __generator(this, function (_b) {\n switch (_b.label) {\n case 0:\n requestInstance = customRequest || request;\n return [4 /*yield*/, requestInstance({\n endpoint: joinUrls(mintUrl, '/v1/swap'),\n method: 'POST',\n requestBody: swapPayload\n })];\n case 1:\n data = _b.sent();\n if (!isObj(data) || !Array.isArray(data === null || data === void 0 ? void 0 : data.signatures)) {\n throw new Error((_a = data.detail) !== null && _a !== void 0 ? _a : 'bad response');\n }\n return [2 /*return*/, data];\n }\n });\n });\n };\n /**\n * Performs a swap operation with ecash inputs and outputs.\n * @param swapPayload payload containing inputs and outputs\n * @returns signed outputs\n */\n CashuMint.prototype.swap = function (swapPayload) {\n return __awaiter(this, void 0, void 0, function () {\n return __generator(this, function (_a) {\n return [2 /*return*/, CashuMint.swap(this._mintUrl, swapPayload, this._customRequest)];\n });\n });\n };\n /**\n * Requests a new mint quote from the mint.\n * @param mintUrl\n * @param mintQuotePayload Payload for creating a new mint quote\n * @param customRequest\n * @returns the mint will create and return a new mint quote containing a payment request for the specified amount and unit\n */\n CashuMint.createMintQuote = function (mintUrl, mintQuotePayload, customRequest) {\n return __awaiter(this, void 0, void 0, function () {\n var requestInstance, response, data;\n return __generator(this, function (_a) {\n switch (_a.label) {\n case 0:\n requestInstance = customRequest || request;\n return [4 /*yield*/, requestInstance({\n endpoint: joinUrls(mintUrl, '/v1/mint/quote/bolt11'),\n method: 'POST',\n requestBody: mintQuotePayload\n })];\n case 1:\n response = _a.sent();\n data = handleMintQuoteResponseDeprecated(response);\n return [2 /*return*/, data];\n }\n });\n });\n };\n /**\n * Requests a new mint quote from the mint.\n * @param mintQuotePayload Payload for creating a new mint quote\n * @returns the mint will create and return a new mint quote containing a payment request for the specified amount and unit\n */\n CashuMint.prototype.createMintQuote = function (mintQuotePayload) {\n return __awaiter(this, void 0, void 0, function () {\n return __generator(this, function (_a) {\n return [2 /*return*/, CashuMint.createMintQuote(this._mintUrl, mintQuotePayload, this._customRequest)];\n });\n });\n };\n /**\n * Gets an existing mint quote from the mint.\n * @param mintUrl\n * @param quote Quote ID\n * @param customRequest\n * @returns the mint will create and return a Lightning invoice for the specified amount\n */\n CashuMint.checkMintQuote = function (mintUrl, quote, customRequest) {\n return __awaiter(this, void 0, void 0, function () {\n var requestInstance, response, data;\n return __generator(this, function (_a) {\n switch (_a.label) {\n case 0:\n requestInstance = customRequest || request;\n return [4 /*yield*/, requestInstance({\n endpoint: joinUrls(mintUrl, '/v1/mint/quote/bolt11', quote),\n method: 'GET'\n })];\n case 1:\n response = _a.sent();\n data = handleMintQuoteResponseDeprecated(response);\n return [2 /*return*/, data];\n }\n });\n });\n };\n /**\n * Gets an existing mint quote from the mint.\n * @param quote Quote ID\n * @returns the mint will create and return a Lightning invoice for the specified amount\n */\n CashuMint.prototype.checkMintQuote = function (quote) {\n return __awaiter(this, void 0, void 0, function () {\n return __generator(this, function (_a) {\n return [2 /*return*/, CashuMint.checkMintQuote(this._mintUrl, quote, this._customRequest)];\n });\n });\n };\n /**\n * Mints new tokens by requesting blind signatures on the provided outputs.\n * @param mintUrl\n * @param mintPayload Payload containing the outputs to get blind signatures on\n * @param customRequest\n * @returns serialized blinded signatures\n */\n CashuMint.mint = function (mintUrl, mintPayload, customRequest) {\n return __awaiter(this, void 0, void 0, function () {\n var requestInstance, data;\n return __generator(this, function (_a) {\n switch (_a.label) {\n case 0:\n requestInstance = customRequest || request;\n return [4 /*yield*/, requestInstance({\n endpoint: joinUrls(mintUrl, '/v1/mint/bolt11'),\n method: 'POST',\n requestBody: mintPayload\n })];\n case 1:\n data = _a.sent();\n if (!isObj(data) || !Array.isArray(data === null || data === void 0 ? void 0 : data.signatures)) {\n throw new Error('bad response');\n }\n return [2 /*return*/, data];\n }\n });\n });\n };\n /**\n * Mints new tokens by requesting blind signatures on the provided outputs.\n * @param mintPayload Payload containing the outputs to get blind signatures on\n * @returns serialized blinded signatures\n */\n CashuMint.prototype.mint = function (mintPayload) {\n return __awaiter(this, void 0, void 0, function () {\n return __generator(this, function (_a) {\n return [2 /*return*/, CashuMint.mint(this._mintUrl, mintPayload, this._customRequest)];\n });\n });\n };\n /**\n * Requests a new melt quote from the mint.\n * @param mintUrl\n * @param MeltQuotePayload\n * @returns\n */\n CashuMint.createMeltQuote = function (mintUrl, meltQuotePayload, customRequest) {\n return __awaiter(this, void 0, void 0, function () {\n var requestInstance, response, data;\n return __generator(this, function (_a) {\n switch (_a.label) {\n case 0:\n requestInstance = customRequest || request;\n return [4 /*yield*/, requestInstance({\n endpoint: joinUrls(mintUrl, '/v1/melt/quote/bolt11'),\n method: 'POST',\n requestBody: meltQuotePayload\n })];\n case 1:\n response = _a.sent();\n data = handleMeltQuoteResponseDeprecated(response);\n if (!isObj(data) || typeof (data === null || data === void 0 ? void 0 : data.amount) !== 'number' || typeof (data === null || data === void 0 ? void 0 : data.fee_reserve) !== 'number' || typeof (data === null || data === void 0 ? void 0 : data.quote) !== 'string') {\n throw new Error('bad response');\n }\n return [2 /*return*/, data];\n }\n });\n });\n };\n /**\n * Requests a new melt quote from the mint.\n * @param MeltQuotePayload\n * @returns\n */\n CashuMint.prototype.createMeltQuote = function (meltQuotePayload) {\n return __awaiter(this, void 0, void 0, function () {\n return __generator(this, function (_a) {\n return [2 /*return*/, CashuMint.createMeltQuote(this._mintUrl, meltQuotePayload, this._customRequest)];\n });\n });\n };\n /**\n * Gets an existing melt quote.\n * @param mintUrl\n * @param quote Quote ID\n * @returns\n */\n CashuMint.checkMeltQuote = function (mintUrl, quote, customRequest) {\n return __awaiter(this, void 0, void 0, function () {\n var requestInstance, response, data;\n return __generator(this, function (_a) {\n switch (_a.label) {\n case 0:\n requestInstance = customRequest || request;\n return [4 /*yield*/, requestInstance({\n endpoint: joinUrls(mintUrl, '/v1/melt/quote/bolt11', quote),\n method: 'GET'\n })];\n case 1:\n response = _a.sent();\n data = handleMeltQuoteResponseDeprecated(response);\n if (!isObj(data) || typeof (data === null || data === void 0 ? void 0 : data.amount) !== 'number' || typeof (data === null || data === void 0 ? void 0 : data.fee_reserve) !== 'number' || typeof (data === null || data === void 0 ? void 0 : data.quote) !== 'string' || typeof (data === null || data === void 0 ? void 0 : data.state) !== 'string' || !Object.values(MeltQuoteState).includes(data.state)) {\n throw new Error('bad response');\n }\n return [2 /*return*/, data];\n }\n });\n });\n };\n /**\n * Gets an existing melt quote.\n * @param quote Quote ID\n * @returns\n */\n CashuMint.prototype.checkMeltQuote = function (quote) {\n return __awaiter(this, void 0, void 0, function () {\n return __generator(this, function (_a) {\n return [2 /*return*/, CashuMint.checkMeltQuote(this._mintUrl, quote, this._customRequest)];\n });\n });\n };\n /**\n * Requests the mint to pay for a Bolt11 payment request by providing ecash as inputs to be spent. The inputs contain the amount and the fee_reserves for a Lightning payment. The payload can also contain blank outputs in order to receive back overpaid Lightning fees.\n * @param mintUrl\n * @param meltPayload\n * @param customRequest\n * @returns\n */\n CashuMint.melt = function (mintUrl, meltPayload, customRequest) {\n return __awaiter(this, void 0, void 0, function () {\n var requestInstance, response, data;\n return __generator(this, function (_a) {\n switch (_a.label) {\n case 0:\n requestInstance = customRequest || request;\n return [4 /*yield*/, requestInstance({\n endpoint: joinUrls(mintUrl, '/v1/melt/bolt11'),\n method: 'POST',\n requestBody: meltPayload\n })];\n case 1:\n response = _a.sent();\n data = handleMeltQuoteResponseDeprecated(response);\n if (!isObj(data) || typeof (data === null || data === void 0 ? void 0 : data.state) !== 'string' || !Object.values(MeltQuoteState).includes(data.state)) {\n throw new Error('bad response');\n }\n return [2 /*return*/, data];\n }\n });\n });\n };\n /**\n * Ask mint to perform a melt operation. This pays a lightning invoice and destroys tokens matching its amount + fees\n * @param meltPayload\n * @returns\n */\n CashuMint.prototype.melt = function (meltPayload) {\n return __awaiter(this, void 0, void 0, function () {\n return __generator(this, function (_a) {\n return [2 /*return*/, CashuMint.melt(this._mintUrl, meltPayload, this._customRequest)];\n });\n });\n };\n /**\n * Checks if specific proofs have already been redeemed\n * @param mintUrl\n * @param checkPayload\n * @param customRequest\n * @returns redeemed and unredeemed ordered list of booleans\n */\n CashuMint.check = function (mintUrl, checkPayload, customRequest) {\n return __awaiter(this, void 0, void 0, function () {\n var requestInstance, data;\n return __generator(this, function (_a) {\n switch (_a.label) {\n case 0:\n requestInstance = customRequest || request;\n return [4 /*yield*/, requestInstance({\n endpoint: joinUrls(mintUrl, '/v1/checkstate'),\n method: 'POST',\n requestBody: checkPayload\n })];\n case 1:\n data = _a.sent();\n if (!isObj(data) || !Array.isArray(data === null || data === void 0 ? void 0 : data.states)) {\n throw new Error('bad response');\n }\n return [2 /*return*/, data];\n }\n });\n });\n };\n /**\n * Get the mints public keys\n * @param mintUrl\n * @param keysetId optional param to get the keys for a specific keyset. If not specified, the keys from all active keysets are fetched\n * @param customRequest\n * @returns\n */\n CashuMint.getKeys = function (mintUrl, keysetId, customRequest) {\n return __awaiter(this, void 0, void 0, function () {\n var requestInstance, data;\n return __generator(this, function (_a) {\n switch (_a.label) {\n case 0:\n // backwards compatibility for base64 encoded keyset ids\n if (keysetId) {\n // make the keysetId url safe\n keysetId = keysetId.replace(/\\//g, '_').replace(/\\+/g, '-');\n }\n requestInstance = customRequest || request;\n return [4 /*yield*/, requestInstance({\n endpoint: keysetId ? joinUrls(mintUrl, '/v1/keys', keysetId) : joinUrls(mintUrl, '/v1/keys')\n })];\n case 1:\n data = _a.sent();\n if (!isObj(data) || !Array.isArray(data.keysets)) {\n throw new Error('bad response');\n }\n return [2 /*return*/, data];\n }\n });\n });\n };\n /**\n * Get the mints public keys\n * @param keysetId optional param to get the keys for a specific keyset. If not specified, the keys from all active keysets are fetched\n * @returns the mints public keys\n */\n CashuMint.prototype.getKeys = function (keysetId, mintUrl) {\n return __awaiter(this, void 0, void 0, function () {\n var allKeys;\n return __generator(this, function (_a) {\n switch (_a.label) {\n case 0:\n return [4 /*yield*/, CashuMint.getKeys(mintUrl || this._mintUrl, keysetId, this._customRequest)];\n case 1:\n allKeys = _a.sent();\n return [2 /*return*/, allKeys];\n }\n });\n });\n };\n /**\n * Get the mints keysets in no specific order\n * @param mintUrl\n * @param customRequest\n * @returns all the mints past and current keysets.\n */\n CashuMint.getKeySets = function (mintUrl, customRequest) {\n return __awaiter(this, void 0, void 0, function () {\n var requestInstance;\n return __generator(this, function (_a) {\n requestInstance = customRequest || request;\n return [2 /*return*/, requestInstance({\n endpoint: joinUrls(mintUrl, '/v1/keysets')\n })];\n });\n });\n };\n /**\n * Get the mints keysets in no specific order\n * @returns all the mints past and current keysets.\n */\n CashuMint.prototype.getKeySets = function () {\n return __awaiter(this, void 0, void 0, function () {\n return __generator(this, function (_a) {\n return [2 /*return*/, CashuMint.getKeySets(this._mintUrl, this._customRequest)];\n });\n });\n };\n /**\n * Checks if specific proofs have already been redeemed\n * @param checkPayload\n * @returns redeemed and unredeemed ordered list of booleans\n */\n CashuMint.prototype.check = function (checkPayload) {\n return __awaiter(this, void 0, void 0, function () {\n return __generator(this, function (_a) {\n return [2 /*return*/, CashuMint.check(this._mintUrl, checkPayload, this._customRequest)];\n });\n });\n };\n CashuMint.restore = function (mintUrl, restorePayload, customRequest) {\n return __awaiter(this, void 0, void 0, function () {\n var requestInstance, data;\n return __generator(this, function (_a) {\n switch (_a.label) {\n case 0:\n requestInstance = customRequest || request;\n return [4 /*yield*/, requestInstance({\n endpoint: joinUrls(mintUrl, '/v1/restore'),\n method: 'POST',\n requestBody: restorePayload\n })];\n case 1:\n data = _a.sent();\n if (!isObj(data) || !Array.isArray(data === null || data === void 0 ? void 0 : data.outputs) || !Array.isArray(data === null || data === void 0 ? void 0 : data.promises)) {\n throw new Error('bad response');\n }\n return [2 /*return*/, data];\n }\n });\n });\n };\n CashuMint.prototype.restore = function (restorePayload) {\n return __awaiter(this, void 0, void 0, function () {\n return __generator(this, function (_a) {\n return [2 /*return*/, CashuMint.restore(this._mintUrl, restorePayload, this._customRequest)];\n });\n });\n };\n /**\n * Tries to establish a websocket connection with the websocket mint url according to NUT-17\n */\n CashuMint.prototype.connectWebSocket = function () {\n return __awaiter(this, void 0, void 0, function () {\n var mintUrl, wsSegment, e_1;\n return __generator(this, function (_a) {\n switch (_a.label) {\n case 0:\n if (!this.ws) return [3 /*break*/, 2];\n return [4 /*yield*/, this.ws.ensureConnection()];\n case 1:\n _a.sent();\n return [3 /*break*/, 6];\n case 2:\n mintUrl = new URL(this._mintUrl);\n wsSegment = 'v1/ws';\n if (mintUrl.pathname) {\n if (mintUrl.pathname.endsWith('/')) {\n mintUrl.pathname += wsSegment;\n } else {\n mintUrl.pathname += '/' + wsSegment;\n }\n }\n this.ws = ConnectionManager.getInstance().getConnection(\"\".concat(mintUrl.protocol === 'https:' ? 'wss' : 'ws', \"://\").concat(mintUrl.host).concat(mintUrl.pathname));\n _a.label = 3;\n case 3:\n _a.trys.push([3, 5,, 6]);\n return [4 /*yield*/, this.ws.connect()];\n case 4:\n _a.sent();\n return [3 /*break*/, 6];\n case 5:\n e_1 = _a.sent();\n console.log(e_1);\n throw new Error('Failed to connect to WebSocket...');\n case 6:\n return [2 /*return*/];\n }\n });\n });\n };\n /**\n * Closes a websocket connection\n */\n CashuMint.prototype.disconnectWebSocket = function () {\n if (this.ws) {\n this.ws.close();\n }\n };\n Object.defineProperty(CashuMint.prototype, \"webSocketConnection\", {\n get: function () {\n return this.ws;\n },\n enumerable: false,\n configurable: true\n });\n return CashuMint;\n}();\nexport { CashuMint };\n//# sourceMappingURL=CashuMint.js.map","map":null,"metadata":{},"sourceType":"module","externalDependencies":[]}