UNPKG

postchain-client

Version:

Client library for accessing a Postchain node through REST.

178 lines 11.4 kB
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()); }); }; import { encodeValue } from "../../../src/gtx/serialization"; import { createEncodedBlockHeaderArray, createMockIccfOpWithArgs, createRawGtx, getTestsClient, } from "../../common/setups"; import { gtvHash } from "../../../src/gtv"; import { rawGtxToGtx, toBuffer } from "../../../src/formatter"; import { getDigestToSign } from "../../../src/gtx/gtx"; import { clientConfiguredToD1, mockAnchoringTransaction, mockBuffer, mockConfirmationProof, mockGtx, mockStringDirectoryChainRid, mockThirtyTwoBytesBuffer, } from "../../common/mocks"; import * as iccfUtils from "../../../src/ICCF/utils"; import * as iccf from "../../../src/ICCF/IccfProofTxMaterialBuilder"; import * as blockchainClient from "../../../src/blockchainClient/blockchainClient"; const txToProveHashMismatch = Buffer.from("sample-tx-hash"); const sourceBlockchainRid = "FCD29927AA06E75547036C8860633AF2D8DF8FF6C78F947A2D7C816CB13DC209"; const targetBlockchainRid = "FCD29927AA06E75547036C8860633AF2D8DF8FF6C78F947A2D7C816CB13DC208"; const iccfTxSigners = [Buffer.from("iccf-pubKey1"), Buffer.from("iccf-pubKey2")]; const validAnchoringChainInfo = { anchoring_chain: Buffer.from("sample-anchoring-chain"), name: "anchoring chain", peers: [{ pubkey: Buffer.from("a"), api_url: "urls" }], }; let client; describe("iccfProofTxMaterialBuilder", () => { beforeAll(() => __awaiter(void 0, void 0, void 0, function* () { client = yield getTestsClient(); })); beforeEach(() => { jest.clearAllMocks(); }); afterEach(() => { jest.restoreAllMocks(); }); describe("createIccfProofTx", () => { it("returns iccfTx for intra-cluster operation", () => __awaiter(void 0, void 0, void 0, function* () { var _a, _b, _c; const rawGtx = createRawGtx("test", 0); const proofHash = gtvHash(rawGtx, 2); const confirmationProofResponse = { hash: proofHash, blockHeader: createEncodedBlockHeaderArray(), }; const gtx = rawGtxToGtx(rawGtx); const txToProveRID = getDigestToSign(gtx, 2); const txToProveSigners = []; jest.spyOn(blockchainClient, "createClient").mockResolvedValue(client); jest.spyOn(client, "getConfirmationProof").mockResolvedValue(mockConfirmationProof); jest .spyOn(iccfUtils, "fetchAndVerifyTransaction") .mockResolvedValue({ verifiedTx: mockGtx, verifiedTxHash: mockBuffer }); jest .spyOn(iccfUtils, "getClusterOfBlockchain") .mockResolvedValueOnce("source-cluster") .mockResolvedValueOnce("source-cluster"); jest .spyOn(iccfUtils, "composeProofTransactionObject") .mockReturnValue(createMockIccfOpWithArgs([ toBuffer(sourceBlockchainRid), proofHash, encodeValue(confirmationProofResponse), ])); const result = yield iccf.createIccfProofTx(clientConfiguredToD1, txToProveRID, proofHash, txToProveSigners, sourceBlockchainRid, targetBlockchainRid, iccfTxSigners, false); expect(result).toHaveProperty("iccfTx"); expect(result).not.toHaveProperty("verifiedTx"); expect(result.iccfTx.operations[0].name).toBe("iccf_proof"); const args = ((_c = (_b = (_a = result.iccfTx) === null || _a === void 0 ? void 0 : _a.operations) === null || _b === void 0 ? void 0 : _b[0]) === null || _c === void 0 ? void 0 : _c.args) || []; const expectedArgs = [ toBuffer(sourceBlockchainRid), proofHash, encodeValue(confirmationProofResponse), ]; expect(args).toStrictEqual(expectedArgs); })); it("returns iccfTx for intra-cluster operation, mismatching", () => __awaiter(void 0, void 0, void 0, function* () { var _d, _e, _f; const rawGtx = createRawGtx("test", 0); const proofHash = gtvHash(rawGtx, 2); const confirmationProofResponse = { hash: proofHash, blockHeader: createEncodedBlockHeaderArray(), }; const gtx = rawGtxToGtx(rawGtx); const txToProveRID = getDigestToSign(gtx, 2); const txToProveSigners = []; jest.spyOn(blockchainClient, "createClient").mockResolvedValue(client); jest.spyOn(client, "getConfirmationProof").mockResolvedValue(mockConfirmationProof); jest .spyOn(iccfUtils, "fetchAndVerifyTransaction") .mockResolvedValue({ verifiedTx: mockGtx, verifiedTxHash: mockBuffer }); jest .spyOn(iccfUtils, "getClusterOfBlockchain") .mockResolvedValueOnce("source-cluster") .mockResolvedValueOnce("source-cluster"); jest .spyOn(iccfUtils, "composeProofTransactionObject") .mockReturnValue(createMockIccfOpWithArgs([ toBuffer(sourceBlockchainRid), proofHash, encodeValue(confirmationProofResponse), ])); const result = yield iccf.createIccfProofTx(clientConfiguredToD1, txToProveRID, txToProveHashMismatch, txToProveSigners, sourceBlockchainRid, targetBlockchainRid, iccfTxSigners, false); expect(result).toHaveProperty("iccfTx"); expect(result).not.toHaveProperty("verifiedTx"); expect(result.iccfTx.operations[0].name).toBe("iccf_proof"); const args = ((_f = (_e = (_d = result.iccfTx) === null || _d === void 0 ? void 0 : _d.operations) === null || _e === void 0 ? void 0 : _e[0]) === null || _f === void 0 ? void 0 : _f.args) || []; const expectedArgs = [ toBuffer(sourceBlockchainRid), proofHash, encodeValue(confirmationProofResponse), ]; expect(args).toStrictEqual(expectedArgs); })); }); describe("getBlockAnchoringTransaction", () => { it("returns anchoringTransaction", () => __awaiter(void 0, void 0, void 0, function* () { jest.spyOn(iccfUtils, "calculateBlockRID").mockReturnValue(mockBuffer); jest .spyOn(clientConfiguredToD1, "getConfirmationProof") .mockResolvedValue(mockConfirmationProof); jest .spyOn(iccfUtils, "awaitGetAnchoringTransactionForBlockRid") .mockResolvedValue(mockAnchoringTransaction); const anchoringTransactionResult = yield iccf.getBlockAnchoringTransaction(clientConfiguredToD1, clientConfiguredToD1, mockThirtyTwoBytesBuffer); expect(anchoringTransactionResult).toStrictEqual(mockAnchoringTransaction); })); it("handles null response with confirmationProof", () => __awaiter(void 0, void 0, void 0, function* () { jest.spyOn(iccfUtils, "calculateBlockRID").mockReturnValue(mockBuffer); jest .spyOn(clientConfiguredToD1, "getConfirmationProof") .mockResolvedValue(mockConfirmationProof); jest.spyOn(iccfUtils, "awaitGetAnchoringTransactionForBlockRid").mockResolvedValue(null); const result = yield iccf.getBlockAnchoringTransaction(clientConfiguredToD1, clientConfiguredToD1, mockThirtyTwoBytesBuffer); expect(result).toBeNull(); })); it("throws an error if neither a txRid or TxProof is provided as input", () => __awaiter(void 0, void 0, void 0, function* () { yield expect(iccf.getBlockAnchoringTransaction(clientConfiguredToD1, clientConfiguredToD1)).rejects.toThrow(new Error("Missing a txRid or TxProof")); })); }); describe("getAnchoringClient", () => { it("returns a client when inserting a blockchain RID", () => __awaiter(void 0, void 0, void 0, function* () { jest.spyOn(iccfUtils, "getClusterOfBlockchain").mockResolvedValue("system"); jest.spyOn(iccfUtils, "getClusterInfo").mockResolvedValue(validAnchoringChainInfo); jest.spyOn(blockchainClient, "createClient").mockResolvedValue(clientConfiguredToD1); const result = yield iccf.getAnchoringClient(clientConfiguredToD1, mockStringDirectoryChainRid); expect(result).toHaveProperty("config"); })); it("returns a client when inserting a cluster name", () => __awaiter(void 0, void 0, void 0, function* () { jest.spyOn(iccfUtils, "getClusterOfBlockchain").mockResolvedValue("system"); jest.spyOn(iccfUtils, "getClusterInfo").mockResolvedValue(validAnchoringChainInfo); jest.spyOn(blockchainClient, "createClient").mockResolvedValue(clientConfiguredToD1); const result = yield iccf.getAnchoringClient(clientConfiguredToD1, undefined, "source-cluster"); expect(result).toHaveProperty("config"); })); it("throws an error if neither blockchainRID nor cluster name is provided", () => __awaiter(void 0, void 0, void 0, function* () { yield expect(iccf.getAnchoringClient(clientConfiguredToD1)).rejects.toThrow(new Error("Missing a dapp blockchainRid or cluster name")); })); it("throws an error if no cluster could be found", () => __awaiter(void 0, void 0, void 0, function* () { yield expect(iccf.getAnchoringClient(clientConfiguredToD1, mockStringDirectoryChainRid)).rejects.toBeInstanceOf(Error); yield expect(iccf.getAnchoringClient(clientConfiguredToD1, mockStringDirectoryChainRid)).rejects.toThrow("No cluster could be found"); })); it("throws an error if no cluster info could be found", () => __awaiter(void 0, void 0, void 0, function* () { yield expect(iccf.getAnchoringClient(clientConfiguredToD1, undefined, "source-cluster")).rejects.toThrow(new Error("Cluster info could not be found")); })); it("sets input client network settings as anchoring client network settings", () => __awaiter(void 0, void 0, void 0, function* () { jest.spyOn(iccfUtils, "getClusterOfBlockchain").mockResolvedValue("system"); jest.spyOn(iccfUtils, "getClusterInfo").mockResolvedValue(validAnchoringChainInfo); jest.spyOn(blockchainClient, "createClient").mockResolvedValue(clientConfiguredToD1); const result = yield iccf.getAnchoringClient(clientConfiguredToD1, mockStringDirectoryChainRid); expect(result.config).toStrictEqual(clientConfiguredToD1.config); })); }); }); //# sourceMappingURL=iccfProofMaterialBuilder.test.js.map