scorechain-sdk
Version:
SDK for the Scorechain API
52 lines • 2.88 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.proofOfAuthenticityVerifierAdapterForAxios = exports.proofOfAuthenticityVerifier = void 0;
const axios_1 = require("axios");
const crypto_1 = __importDefault(require("crypto"));
const ProofOfAuthenticityError_1 = require("../types/errors/ProofOfAuthenticityError");
const fs_1 = require("fs");
const path_1 = __importDefault(require("path"));
const VERIFICATION_CONSTANT = "verified";
/**
* @description Scorechain provide a proof of authenticity conducted using JWT and asymetrical encryption of the payload.
* The following function will help you with veryfying this proof of authenticity.
*/
function proofOfAuthenticityVerifier(data, signature, publicKey, headerServerTimestamp) {
try {
const payload = JSON.stringify({ data, timestamp: headerServerTimestamp });
const verification = crypto_1.default.createVerify("RSA-SHA256").update(payload).verify(publicKey, signature, "hex");
if (!verification) {
throw new ProofOfAuthenticityError_1.ProofOfOriginError();
}
return VERIFICATION_CONSTANT;
}
catch (_) {
throw new ProofOfAuthenticityError_1.ProofOfOriginError();
}
}
exports.proofOfAuthenticityVerifier = proofOfAuthenticityVerifier;
/**
* @description Scorechain provide a proof of authenticity conducted using JWT and asymetrical encryption of the payload.
* The following function is an adapter from the last function specificially for query realised with axios.
*/
function proofOfAuthenticityVerifierAdapterForAxios(axiosCallResponse) {
var _a, _b, _c;
const publicKey = (0, fs_1.readFileSync)(path_1.default.join(__dirname, "../../scorechainPublicKey", "public.pem")).toString("utf-8");
if (axiosCallResponse instanceof axios_1.AxiosError) {
const payload = (_a = axiosCallResponse.response) === null || _a === void 0 ? void 0 : _a.data;
const token = (_b = axiosCallResponse.response) === null || _b === void 0 ? void 0 : _b.headers["x-signature"];
const headerServerTimestamp = (_c = axiosCallResponse.response) === null || _c === void 0 ? void 0 : _c.headers["x-server-time"];
proofOfAuthenticityVerifier(payload, token, publicKey, headerServerTimestamp);
}
else {
const payload = axiosCallResponse.data;
const token = axiosCallResponse.headers["x-signature"];
const headerServerTimestamp = axiosCallResponse.headers["x-server-time"];
proofOfAuthenticityVerifier(payload, token, publicKey, headerServerTimestamp);
}
}
exports.proofOfAuthenticityVerifierAdapterForAxios = proofOfAuthenticityVerifierAdapterForAxios;
//# sourceMappingURL=proofOfAuthenticityVerifier.js.map