UNPKG

tickethead-sdk

Version:

SDK for the Tickethead API

126 lines 5.41 kB
"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.BlockchainService = void 0; const signer_1 = require("./signer"); const converter_1 = require("../common/converter"); /** * Service class for blockchain transactions. */ class BlockchainService { constructor(client, version) { this.client = client; this.version = version; } /** * Returns true if the service is reachable * * @returns Services' online status */ health() { return __awaiter(this, void 0, void 0, function* () { try { const res = yield this.client.get(`event/health`); if (res.data.status === 'ok') { return { online: true }; } } catch (e) { // Do nothing } return { online: false }; }); } /** * This is the first step in committing data to the blockchain. * Creates a new proposal which need to be signed with the private key. * * @param proposal New proposal data * @returns Digest hash of the proposal */ createProposal(proposal) { return __awaiter(this, void 0, void 0, function* () { // The payload is snake cased because chaincode consumes it const res = yield this.client.post(`blockchain/${this.version}/transaction/proposal`, Object.assign(Object.assign({}, proposal), { args: [JSON.stringify((0, converter_1.toSnakeCase)(proposal.args))] })); return res.data.data; }); } /** * This is the second step in committing data to the blockchain. * Submits the proposal signature so that it can be endorsed. * It is signed with the user's private key. * * @param proposal Proposal's digest and signature * @returns Transaction (tx) digest, used in the commit transaction step */ createTransaction(proposal) { return __awaiter(this, void 0, void 0, function* () { const res = yield this.client.post(`blockchain/${this.version}/transaction/proposal/${proposal.digest}`, { signature: proposal.signature }); return res.data.data; }); } /** * Third and final step in creating a transaction. * Commits a transaction. Once a proposal is submitted, * signed and endorsed it can be committed as a transaction to the blockchain. * * @param tx Signed transaction * @returns Status of the transaction */ commitTransaction(tx) { return __awaiter(this, void 0, void 0, function* () { const res = yield this.client.post(`blockchain/${this.version}/transaction/${tx.digest}`, { signature: tx.signature }); return res.data.data; }); } /** * Creates a proposal, signs it and commits it to the blockchain. * * @param proposal Transaction proposal containing the bc function and arguments * @param keys Private key and certificate to sign the intermediate steps * @returns Transaction commit status */ sendTransaction(proposal, keys) { return __awaiter(this, void 0, void 0, function* () { const proposalDigest = yield this.createProposal({ fcn: proposal.fcn, certificate: keys.certificate, args: proposal.args, }); const proposalSignature = (0, signer_1.signDigest)(keys.privateKey, proposalDigest.digest); const txDigest = yield this.createTransaction({ digest: proposalDigest.digest, signature: proposalSignature, }); const txSignature = (0, signer_1.signDigest)(keys.privateKey, txDigest.digest); return this.commitTransaction({ digest: txDigest.digest, signature: txSignature, }); }); } /** * Sends online transaction to the blockchain service. * * @param proposal Transaction proposal containing the bc function and arguments * @param keys Private key and certificate for which the transaction should be invoked. * @returns Response from the chaincode. */ sendOnlineTransaction(proposal, keys) { return __awaiter(this, void 0, void 0, function* () { const body = Object.assign(Object.assign(Object.assign({}, proposal), keys), { args: [JSON.stringify((0, converter_1.toSnakeCase)(proposal.args))] }); const res = yield this.client.post(`blockchain/${this.version}/transaction`, body); return res.data.data; }); } } exports.BlockchainService = BlockchainService; //# sourceMappingURL=service.js.map