UNPKG

mazzaroth-js

Version:

Library that facilitates interaction with Mazzaroth nodes from both the browser and node-js

243 lines (204 loc) 9.14 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var _axios = require("axios"); var _axios2 = _interopRequireDefault(_axios); var _mazzarothXdr = require("mazzaroth-xdr"); var types = _interopRequireWildcard(_mazzarothXdr); function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); } function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; } function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } class Client { constructor(address) { this.version = 'v1'; // Get the address. this.address = address || 'http://localhost:6299'; } /** * Lookup the block height for the ledger on a node. * * @param channelID Hex string (32 chars) identifier for the channel. * * @return Promise that on success provides a JSON Result */ async BlockHeight(channelID) { return _axios2.default.get(`${this.address}/${this.version}/channels/${channelID}/blocks/height`).then(res => { const height = types.BlockHeight(); return height.fromJSON(res.data.data); }).catch(err => { if (err.response && err.response.data && err.response.data.error) { throw new Error(err.response.data.error); } throw err; }); } /** * Lookup a block that is on the node ledger by block ID. * * @param channelID Hex string (32 chars) identifier for the channel. * @param block Hex string (32 chars) identifier for the block on the Mazzaroth node. * * @return Promise that on success provides a JSON Result */ async BlockLookup(channelID, blockID) { return _axios2.default.get(`${this.address}/${this.version}/channels/${channelID}/blocks/${blockID}`).then(res => { const block = types.Block(); return block.fromJSON(res.data.data); }).catch(err => { if (err.response && err.response.data && err.response.data.error) { throw new Error(err.response.data.error); } throw err; }); } /** * Lookup a list of blocks that are on the node ledger by block height and number. * * @param channelID Hex string (32 chars) identifier for the channel. * @param blockHeight Number the starting height to get blocks for. * @param number Number the number of blocks to include. * * @return Promise that on success provides a JSON Result */ async BlockList(channelID, blockHeight, number) { return _axios2.default.get(`${this.address}/${this.version}/channels/${channelID}/blocks?height=${blockHeight}&number=${number}`).then(res => { const blockList = res.data.data; blockList.map(n => types.Block().fromJSON(n)); return blockList; }).catch(err => { if (err.response && err.response.data && err.response.data.error) { throw new Error(err.response.data.error); } throw err; }); } /** * Lookup a block header that is on the node ledger by block ID. * * @param channelID Hex string (32 chars) identifier for the channel. * @param blockID Hex string (32 chars) identifier for the block on the Mazzaroth node. * * @return Promise that on success provides a JSON Result */ async BlockHeaderLookup(channelID, blockID) { return _axios2.default.get(`${this.address}/${this.version}/channels/${channelID}/blockheaders/${blockID}`).then(res => { const blockHeader = types.BlockHeader(); return blockHeader.fromJSON(res.data.data); }).catch(err => { if (err.response && err.response.data && err.response.data.error) { throw new Error(err.response.data.error); } throw err; }); } /** * Lookup a list of block headers that are on the node ledger by block height and number. * * @param channelID Hex string (32 chars) identifier for the channel. * @param blockHeight Number the starting height to get block headers for. * @param number Number the number of block headers to include. * * @return Promise that on success provides a JSON Result */ async BlockHeaderList(channelID, blockHeight, number) { return _axios2.default.get(`${this.address}/${this.version}/channels/${channelID}/blockheaders?height=${blockHeight}&number=${number}`).then(res => { const blockHeaderList = res.data.data; blockHeaderList.map(n => types.BlockHeader().fromJSON(n)); return blockHeaderList; }).catch(err => { if (err.response && err.response.data && err.response.data.error) { throw new Error(err.response.data.error); } throw err; }); } /** * Lookup a contract ABI that is on the node for the given channel. * * @param channelID Hex string (32 chars) identifier for the channel. * * @return Promise that on success provides a JSON Result */ async ChannelAbi(channelID) { return _axios2.default.get(`${this.address}/${this.version}/channels/${channelID}/abi`).then(res => { const abi = types.Abi(); return abi.fromJSON(res.data.data); }).catch(err => { if (err.response && err.response.data && err.response.data.error) { throw new Error(err.response.data.error); } throw err; }); } /** * Lookup a receipt that is on the node ledger by transaction ID. * * @param channelID Hex string (32 chars) identifier for the channel. * @param transactionID Hex string (32 chars) identifier for the transaction submitted * to the Mazzaroth node. * * @return Promise that on success provides a JSON Result */ async ReceiptLookup(channelID, transactionID) { return _axios2.default.get(`${this.address}/${this.version}/channels/${channelID}/receipts/${transactionID}`).then(res => { const receipt = types.Receipt(); return receipt.fromJSON(res.data.data); }).catch(err => { if (err.response && err.response.data && err.response.data.error) { throw new Error(err.response.data.error); } throw err; }); } /** * Submits a transaction to a Mazzaroth node. These are transactions * that must be signed and will eventually be sent to the backing census * pool to be submitted to the blockchain. This includes transactions that * update channel state including contract updates and authorization * transactions. Write transactions are submitted asynchronously and the * results must be looked up by querying the Receipt for the transaction. * Readonly Transactions will return the receipt immediately. * * @param transaction Signed Transaction object. * * @return Promise that on success provides an XDR Transaction ID or Receipt */ async TransactionSubmit(transaction) { return _axios2.default.post(`${this.address}/${this.version}/channels/${transaction.data.channelID}/transactions`, transaction).then(res => { // Check return type which will be either a TransactionID or Receipt if (res.data.type === 1) { const txID = types.ID(); return txID.fromJSON(res.data.data); } else { const receipt = types.Receipt(); return receipt.fromJSON(res.data.data); } }).catch(err => { if (err.response && err.response.data && err.response.data.error) { throw new Error(err.response.data.error); } throw err; }); } /** * Lookup a write transaction that is on the node ledger by the given transaction ID. * * @param channelID Hex string (32 chars) identifier for the channel. * @param transactionID Hex string (32 chars) identifier for the transaction submitted * to the Mazzaroth node. * * @return Promise that on success provides a JSON Result */ async TransactionLookup(channelID, transactionID) { return _axios2.default.get(`${this.address}/${this.version}/channels/${channelID}/transactions/${transactionID}`).then(res => { const tx = types.Transaction(); return tx.fromJSON(res.data.data); }).catch(err => { if (err.response && err.response.data && err.response.data.error) { throw new Error(err.response.data.error); } throw err; }); } } exports.default = Client;