UNPKG

cardano-web3-utils

Version:
83 lines (82 loc) 4.04 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (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; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.testUtils = exports.authUtils = exports.Web3AuthenticationError = exports.Web3Authentication = void 0; var auth_utils_1 = require("./auth-utils"); var Web3Authentication = /** @class */ (function () { function Web3Authentication(expirationTimeSpan, hostname) { this.expirationTimeSpan = expirationTimeSpan; this.hostname = hostname; } Web3Authentication.prototype.authenticate = function (uri, action, key, signature, options) { var payload = (0, auth_utils_1.getPayload)(signature); if (!(0, auth_utils_1.checkExpiration)(payload, (options === null || options === void 0 ? void 0 : options.expirationTimeSpan) ? options.expirationTimeSpan : this.expirationTimeSpan)) { throw new Web3AuthenticationError('Expired timestamp', 401); } if (action !== payload.action) { throw new Web3AuthenticationError('Invalid action', 401); } if (this.hostname + uri !== payload.uri) { throw new Web3AuthenticationError('Invalid uri', 401); } if (!(0, auth_utils_1.verifyCoseSign1Signature)(key, signature)) { throw new Web3AuthenticationError('Invalid signature', 401); } if (!(0, auth_utils_1.verifyCoseSign1Address)(key, signature)) { throw new Web3AuthenticationError("Public key doesn't match the provided address", 401); } return { payload: payload, walletAddress: (0, auth_utils_1.getCoseSign1Bech32Address)(signature) }; }; return Web3Authentication; }()); exports.Web3Authentication = Web3Authentication; var Web3AuthenticationError = /** @class */ (function (_super) { __extends(Web3AuthenticationError, _super); function Web3AuthenticationError(message, httpErrorCode) { var _this = _super.call(this, message) || this; _this.httpErrorCode = httpErrorCode; _this.name = 'Web3AuthenticationError'; return _this; } return Web3AuthenticationError; }(Error)); exports.Web3AuthenticationError = Web3AuthenticationError; exports.authUtils = __importStar(require("./auth-utils")); exports.testUtils = __importStar(require("./test-utils"));