UNPKG

@liskhq/lisk-framework-faucet-plugin

Version:

A plugin for distributing testnet tokens from a newly developed blockchain application.

87 lines 3.97 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Endpoint = void 0; const axios_1 = require("axios"); const lisk_sdk_1 = require("lisk-sdk"); const schemas_1 = require("./schemas"); const validator = lisk_sdk_1.validator.validator; class Endpoint extends lisk_sdk_1.BasePluginEndpoint { constructor() { super(...arguments); this._state = { publicKey: undefined, privateKey: undefined, address: undefined }; } init(state, apiClient, config) { this._state = state; this._client = apiClient; this._config = config; } async authorize(context) { validator.validate(schemas_1.authorizeParamsSchema, context.params); const { enable, password } = context.params; try { const parsedEncryptedKey = lisk_sdk_1.cryptography.encrypt.parseEncryptedMessage(this._config.encryptedPrivateKey); const privateKeyStr = await lisk_sdk_1.cryptography.encrypt.decryptMessageWithPassword(parsedEncryptedKey, password, 'utf-8'); const privateKey = Buffer.from(privateKeyStr, 'hex'); const publicKey = lisk_sdk_1.cryptography.ed.getPublicKeyFromPrivateKey(privateKey); this._state.privateKey = enable ? privateKey : undefined; this._state.publicKey = enable ? publicKey : undefined; this._state.address = enable ? lisk_sdk_1.cryptography.address.getLisk32AddressFromPublicKey(publicKey, this._config.tokenPrefix) : undefined; const changedState = enable ? 'enabled' : 'disabled'; return { result: `Successfully ${changedState} the faucet.`, }; } catch (error) { throw new Error('Password given is not valid.'); } } async fundTokens(context) { var _a; validator.validate(schemas_1.fundParamsSchema, context.params); const { address, token } = context.params; if (!this._state.publicKey || !this._state.privateKey) { throw new Error('Faucet is not enabled.'); } const captchaResult = await (0, axios_1.default)({ method: 'post', url: 'https://www.google.com/recaptcha/api/siteverify', params: { secret: this._config.captchaSecretkey, response: token, }, headers: { Accept: 'application/json', 'Accept-Encoding': 'identity', 'Content-Type': 'application/x-www-form-urlencoded', }, }); if (!((_a = captchaResult === null || captchaResult === void 0 ? void 0 : captchaResult.data) === null || _a === void 0 ? void 0 : _a.success)) { throw new Error('Captcha response was invalid.'); } await this._transferFunds(address); return { result: `Successfully funded account at address: ${address}.`, }; } async _transferFunds(address) { var _a, _b; const transferTransactionParams = { tokenID: this._config.tokenID, amount: lisk_sdk_1.transactions.convertLSKToBeddows(this._config.amount), recipientAddress: lisk_sdk_1.cryptography.address.getLisk32AddressFromAddress(Buffer.from(address, 'hex')), data: '', }; const transaction = await this._client.transaction.create({ module: 'token', command: 'transfer', senderPublicKey: (_a = this._state.publicKey) === null || _a === void 0 ? void 0 : _a.toString('hex'), fee: lisk_sdk_1.transactions.convertLSKToBeddows(this._config.fee), params: transferTransactionParams, }, (_b = this._state.privateKey) === null || _b === void 0 ? void 0 : _b.toString('hex')); await this._client.transaction.send(transaction); } } exports.Endpoint = Endpoint; //# sourceMappingURL=endpoint.js.map