UNPKG

n8n-nodes-wax

Version:

n8n Community Node Package for the WAX Blockchain

189 lines 7.96 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ATOMIC_CONTRACT = void 0; exports.createAtomicRpc = createAtomicRpc; exports.createActionGenerator = createActionGenerator; exports.ensureAuthorized = ensureAuthorized; exports.fetchSchemaFormat = fetchSchemaFormat; exports.ensureTemplateExists = ensureTemplateExists; exports.buildAttributeMap = buildAttributeMap; exports.parseSchemaFormat = parseSchemaFormat; exports.parseTokensToBack = parseTokensToBack; exports.requireTemplateId = requireTemplateId; exports.requireMaxSupply = requireMaxSupply; const n8n_workflow_1 = require("n8n-workflow"); exports.ATOMIC_CONTRACT = 'atomicassets'; const ATOMICASSETS_MISSING_MESSAGE = 'AtomicAssets support is not installed. Reinstall n8n-nodes-wax from Settings → Community Nodes (and reload n8n if needed) to use Mint Asset / Create Template / Get Schema Format.'; const ASSET_STRING_RE = /^\d+(?:\.\d+)?\s+[A-Z]{1,7}$/; function loadRpcCtor(context) { var _a; try { const mod = require('atomicassets/build/API/Rpc'); return ((_a = mod === null || mod === void 0 ? void 0 : mod.default) !== null && _a !== void 0 ? _a : mod); } catch { throw new n8n_workflow_1.NodeOperationError(context.getNode(), ATOMICASSETS_MISSING_MESSAGE); } } function loadGeneratorMod(context) { try { return require('atomicassets/build/Actions/Generator'); } catch { throw new n8n_workflow_1.NodeOperationError(context.getNode(), ATOMICASSETS_MISSING_MESSAGE); } } function createAtomicRpc(context, endpoint, contract = exports.ATOMIC_CONTRACT) { const Ctor = loadRpcCtor(context); return new Ctor(endpoint, contract, { fetch: fetch }); } function createActionGenerator(context, contract) { const { ActionGenerator: Ctor } = loadGeneratorMod(context); return new Ctor(contract); } async function ensureAuthorized(context, rpc, collectionName, account) { let authorized; try { const collection = await rpc.getCollection(collectionName); authorized = await collection.authorizedAccounts(); } catch { throw new n8n_workflow_1.NodeOperationError(context.getNode(), `Collection "${collectionName}" not found`); } if (!authorized.includes(account)) { throw new n8n_workflow_1.NodeOperationError(context.getNode(), `Account "${account}" is not in authorized_accounts for collection "${collectionName}". The collection author must add it via atomicassets::addcolauth.`); } } async function fetchSchemaFormat(context, rpc, collectionName, schemaName) { try { const schema = await rpc.getSchema(collectionName, schemaName); const raw = await schema.rawFormat(); return raw.map((f) => ({ name: f.name, type: f.type })); } catch (error) { const reason = error instanceof Error ? error.message : 'unknown error'; throw new n8n_workflow_1.NodeOperationError(context.getNode(), `Schema "${schemaName}" not found in collection "${collectionName}" (${reason})`); } } async function ensureTemplateExists(context, rpc, collectionName, templateId) { try { const template = await rpc.getTemplate(collectionName, templateId); const data = await template.toObject(); const schemaName = typeof data.schema === 'string' ? data.schema : data.schema.schema_name; return { schema_name: schemaName }; } catch { throw new n8n_workflow_1.NodeOperationError(context.getNode(), `Template "${templateId}" not found in collection "${collectionName}"`); } } function parseJsonObject(context, raw, field) { if (raw === undefined || raw === null || raw === '') return {}; if (typeof raw === 'object' && !Array.isArray(raw)) { return raw; } if (typeof raw === 'string') { const trimmed = raw.trim(); if (trimmed === '') return {}; let parsed; try { parsed = JSON.parse(trimmed); } catch { throw new n8n_workflow_1.NodeOperationError(context.getNode(), `${field} must be valid JSON`); } if (parsed === null || typeof parsed !== 'object' || Array.isArray(parsed)) { throw new n8n_workflow_1.NodeOperationError(context.getNode(), `${field} must be a JSON object`); } return parsed; } throw new n8n_workflow_1.NodeOperationError(context.getNode(), `${field} must be a JSON object`); } function buildAttributeMap(context, raw, format, field) { const plain = parseJsonObject(context, raw, field); if (Object.keys(plain).length === 0) return []; const formatNames = new Set(format.map((f) => f.name)); for (const key of Object.keys(plain)) { if (!formatNames.has(key)) { throw new n8n_workflow_1.NodeOperationError(context.getNode(), `${field}: field "${key}" is not declared in the schema`); } } const { toAttributeMap } = loadGeneratorMod(context); try { return toAttributeMap(plain, format); } catch (error) { const reason = error instanceof Error ? error.message : 'unknown error'; throw new n8n_workflow_1.NodeOperationError(context.getNode(), `${field} could not be serialized against the schema: ${reason}`); } } function parseSchemaFormat(context, raw, field) { if (raw === undefined || raw === null || raw === '') { throw new n8n_workflow_1.NodeOperationError(context.getNode(), `${field} is required`); } let parsed; if (typeof raw === 'string') { try { parsed = JSON.parse(raw); } catch { throw new n8n_workflow_1.NodeOperationError(context.getNode(), `${field} must be valid JSON`); } } else { parsed = raw; } if (!Array.isArray(parsed)) { throw new n8n_workflow_1.NodeOperationError(context.getNode(), `${field} must be a JSON array`); } const result = []; for (const entry of parsed) { if (!entry || typeof entry !== 'object' || typeof entry.name !== 'string' || typeof entry.type !== 'string') { throw new n8n_workflow_1.NodeOperationError(context.getNode(), `${field} entries must be objects with string "name" and "type" fields`); } result.push({ name: entry.name, type: entry.type }); } return result; } function parseTokensToBack(context, raw, field) { if (raw === undefined || raw === null || raw === '') return []; if (typeof raw !== 'string') { throw new n8n_workflow_1.NodeOperationError(context.getNode(), `${field} must be a string`); } const items = raw.split(',').map((s) => s.trim()).filter((s) => s !== ''); for (const item of items) { if (!ASSET_STRING_RE.test(item)) { throw new n8n_workflow_1.NodeOperationError(context.getNode(), `${field} entries must be EOSIO asset strings like "1.00000000 WAX"`); } } return items; } function requireTemplateId(context, raw, field) { const node = context.getNode(); const n = typeof raw === 'number' ? raw : Number(raw); if (!Number.isInteger(n) || n <= 0) { throw new n8n_workflow_1.NodeOperationError(node, `${field} must be a positive integer`); } if (n > 0x7fffffff) { throw new n8n_workflow_1.NodeOperationError(node, `${field} exceeds int32 maximum`); } return String(n); } function requireMaxSupply(context, raw, field) { const node = context.getNode(); const n = typeof raw === 'number' ? raw : Number(raw); if (!Number.isInteger(n) || n < 0) { throw new n8n_workflow_1.NodeOperationError(node, `${field} must be a non-negative integer (0 = unlimited)`); } if (n > 0xffffffff) { throw new n8n_workflow_1.NodeOperationError(node, `${field} exceeds uint32 maximum`); } return String(n); } //# sourceMappingURL=atomic.js.map