Skip to content
Snippets Groups Projects
Commit 441e2051 authored by Igor Markin's avatar Igor Markin
Browse files

Implement prepare script

parent 474c45bf
No related branches found
No related tags found
1 merge request!38Implement prepare script
Showing
with 1 addition and 616 deletions
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.VERIFICATION_ERROR = void 0;
exports.VERIFICATION_ERROR = "VERIFICATION_ERROR";
class VerificationError extends Error {
constructor(message) {
super(message);
this.name = exports.VERIFICATION_ERROR;
}
}
exports.default = VerificationError;
declare const EventEmitter: any;
import { BlockData, MerkleTree, StatusData, TxData, VerificationData } from "../../types";
import { AxiosError } from "axios";
export declare const TRANSACTION_RETRIEVED = "TRANSACTION_RETRIEVED";
export declare const MERKLE_TREE_VERIFIED = "MERKLE_TREE_VERIFIED";
export declare const BLOCK_DATA_RETRIEVED = "BLOCK_DATA_RETRIEVED";
declare class VerificationService extends EventEmitter {
private _aeternityService;
private _cloudflareService;
constructor(cdnUrl?: string, cdnBucket?: string, aeternityNodeUrl?: string, aeternityCompilerUrl?: string, aeternityContractBytecode?: string);
getTransaction(hash: string): Promise<TxData>;
getBlockData(blockHeight: number): Promise<BlockData>;
verifyStatusData(statusData: StatusData): Promise<VerificationData>;
getMerkleTreeForTransaction(txData: TxData): Promise<MerkleTree>;
verifyMerkleTreeNode(nodeToVerify: string, merkleTree: MerkleTree): Promise<boolean>;
catchVerificationError(error: AxiosError, verificationFailureMessage: string): void;
}
export default VerificationService;
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.BLOCK_DATA_RETRIEVED = exports.MERKLE_TREE_VERIFIED = exports.TRANSACTION_RETRIEVED = void 0;
const index_1 = require("../../index");
const EventEmitter = require("eventemitter2");
const merkletreejs_1 = require("merkletreejs");
const AeternityService_1 = require("../AeternityService");
const CloudflareService_1 = require("../CloudflareService");
const VerificationError_1 = require("./VerificationError");
exports.TRANSACTION_RETRIEVED = "TRANSACTION_RETRIEVED";
exports.MERKLE_TREE_VERIFIED = "MERKLE_TREE_VERIFIED";
exports.BLOCK_DATA_RETRIEVED = "BLOCK_DATA_RETRIEVED";
class VerificationService extends EventEmitter {
constructor(cdnUrl, cdnBucket, aeternityNodeUrl, aeternityCompilerUrl, aeternityContractBytecode) {
super();
this._aeternityService = null;
this._cloudflareService = null;
this._aeternityService = new AeternityService_1.default(aeternityNodeUrl, aeternityCompilerUrl, aeternityContractBytecode);
this._cloudflareService = new CloudflareService_1.default(cdnUrl, cdnBucket);
}
getTransaction(hash) {
return __awaiter(this, void 0, void 0, function* () {
return this._aeternityService.getTxDataByHash(hash).catch((err) => {
this.catchVerificationError(err, `Error getting transaction`);
return null;
});
});
}
getBlockData(blockHeight) {
return __awaiter(this, void 0, void 0, function* () {
return this._aeternityService.getBlockData(blockHeight).catch((e) => {
this.catchVerificationError(e, `Error retrieving block`);
return null;
});
});
}
verifyStatusData(statusData) {
return __awaiter(this, void 0, void 0, function* () {
let verificationError;
let blockData;
let merkleTreeDetails;
let txData;
try {
txData = yield this.getTransaction(statusData.transactionHash);
this.emit(exports.TRANSACTION_RETRIEVED, txData);
const merkleTree = yield this.getMerkleTreeForTransaction(txData);
const statusVerified = yield this.verifyMerkleTreeNode(statusData.statusRaw, merkleTree);
merkleTreeDetails = {
verified: statusVerified,
treeSize: merkleTree.nodes.length,
statusPosition: merkleTree.nodes.indexOf(statusData.statusRaw) + 1,
rootHash: merkleTree.rootHash,
};
this.emit(exports.MERKLE_TREE_VERIFIED, merkleTreeDetails);
blockData = yield this.getBlockData(txData.blockHeight);
this.emit(exports.BLOCK_DATA_RETRIEVED, blockData);
}
catch (e) {
if (e instanceof VerificationError_1.default) {
verificationError = e.message;
}
else {
throw e;
}
}
return {
statusData,
merkleTreeDetails,
blockData,
verificationError,
txData,
};
});
}
getMerkleTreeForTransaction(txData) {
var _a;
return __awaiter(this, void 0, void 0, function* () {
try {
const txMerkleTreeData = yield this._aeternityService.decodeContractCallData((_a = txData.tx) === null || _a === void 0 ? void 0 : _a.callData);
const merkleTreeNodes = yield this._cloudflareService.getMerkleTree(txMerkleTreeData.merkleeTreeFileName);
return {
nodes: merkleTreeNodes,
rootHash: txMerkleTreeData.rootNodeHash,
};
}
catch (e) {
this.catchVerificationError(e, `Error extracting Merkle tree from transaction.`);
}
});
}
verifyMerkleTreeNode(nodeToVerify, merkleTree) {
return __awaiter(this, void 0, void 0, function* () {
const leaves = yield Promise.all(merkleTree.nodes.map((x) => __awaiter(this, void 0, void 0, function* () { return yield index_1.CryptoService.SHA256(x); })));
const tree = new merkletreejs_1.MerkleTree(leaves);
const leaf = yield index_1.CryptoService.SHA256(nodeToVerify);
const proof = tree.getProof(Buffer.from(leaf), merkleTree.nodes.indexOf(nodeToVerify));
const verified = tree.verify(proof, Buffer.from(leaf), Buffer.from(merkleTree.rootHash, "base64"));
if (!verified) {
throw new VerificationError_1.default(`Merkle tree not verified.`);
}
return verified;
});
}
catchVerificationError(error, verificationFailureMessage) {
if (error.response) {
throw new VerificationError_1.default(verificationFailureMessage);
}
else {
throw error;
}
}
}
VerificationService.TRANSACTION_RETRIEVED = exports.TRANSACTION_RETRIEVED;
VerificationService.MERKLE_TREE_VERIFIED = exports.MERKLE_TREE_VERIFIED;
VerificationService.BLOCK_DATA_RETRIEVED = exports.BLOCK_DATA_RETRIEVED;
exports.default = VerificationService;
import VerificationService from "./VerificationService";
export default VerificationService;
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const VerificationService_1 = require("./VerificationService");
exports.default = VerificationService_1.default;
This diff is collapsed.
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
export declare const arrayBufferToBase64: (buffer: ArrayBuffer) => string;
export declare const base64ToArrayBuffer: (base64: string) => ArrayBuffer;
export declare const ensureBase64: (data: string | ArrayBuffer) => string;
export declare const ensureArrayBuffer: (data: string | ArrayBuffer) => ArrayBuffer;
export declare const ensureUint8Array: (data: string | ArrayBuffer | Uint8Array) => Uint8Array;
export declare const compressData: (binary: string | ArrayBuffer) => ArrayBuffer;
export declare const decompressData: (binary: string | ArrayBuffer) => ArrayBuffer;
export declare const arrayBufferToHex: (buffer: ArrayBuffer) => string;
export declare const escapeHtmlString: (string: string) => string;
declare const _default: {
compressData: (binary: string | ArrayBuffer) => ArrayBuffer;
decompressData: (binary: string | ArrayBuffer) => ArrayBuffer;
arrayBufferToHex: (buffer: ArrayBuffer) => string;
arrayBufferToBase64: (buffer: ArrayBuffer) => string;
base64ToArrayBuffer: (base64: string) => ArrayBuffer;
ensureUint8Array: (data: string | ArrayBuffer | Uint8Array) => Uint8Array;
ensureArrayBuffer: (data: string | ArrayBuffer) => ArrayBuffer;
ensureBase64: (data: string | ArrayBuffer) => string;
escapeHtmlString: (string: string) => string;
};
export default _default;
This diff is collapsed.
declare const _default: {
putQrCodeOnChromakeyTemplate: (qrCodeImageBase64: string, templateImageBase64: string, placeholderWidth: number, placeholderHeight: number, scale?: number) => Promise<string>;
};
export default _default;
This diff is collapsed.
/**
* Utility implementing Rabin-Karp algorithm
* https://en.wikipedia.org/wiki/Rabin%E2%80%93Karp_algorithm
*/
declare const _default: {
getRabinFingerprint: (string: string) => string;
searchFingerprintInText: (text: string, keywordHashHexString: string, size: number) => number;
/**
* @deprecated Will be deleted in version 1.0.1. Use getRabinFingerprint instead.
*/
getRollingHash: (string: string) => string;
/**
* @deprecated Will be deleted in version 1.0.1. Use searchFingerprintInText instead.
*/
searchHashInText: (text: string, keywordHashHexString: string, size: number) => number;
};
export default _default;
This diff is collapsed.
export declare const removeSpacesAndLinebreaks: (s: string) => string;
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.removeSpacesAndLinebreaks = void 0;
exports.removeSpacesAndLinebreaks = (s) => {
const regexNewlines = new RegExp(/[\r\n\v]+/g);
const regexSpaces = new RegExp(/\s+|\u200B/g);
return s.replace(regexNewlines, "").replace(regexSpaces, "");
};
......@@ -22,6 +22,7 @@
"typescript": "^4.0.2"
},
"scripts": {
"prepare": "yarn build",
"start": "tsc --watch",
"build": "tsc",
"test": "jest",
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment