@kadena/kadena-cli
Version:
Kadena CLI tool to interact with the Kadena blockchain (manage keys, transactions, etc.)
138 lines • 5.01 kB
JavaScript
import { asyncPipe } from '@kadena/client-utils';
import yaml from 'js-yaml';
import { join } from 'path';
import { services } from '../../services/index.js';
// Responsinble for splitting a string into parts and holes
export const getPartsAndHoles = (text) => {
return text.split(/{{(.*?)}}}?/g).reduce((acc, curr, i) => {
// if i is even, then it's a part
// if i is odd, then it's a hole
if (i % 2 === 0) {
acc[0].push(curr);
}
else {
if (curr.startsWith('{')) {
acc[1].push({ literal: curr.slice(1) });
return acc;
}
acc[1].push({ literal: curr });
}
return acc;
}, [[], []]);
};
export const getPartsAndHolesInCtx = (tplPath, cwd = process.cwd()) => {
const file = services.filesystem.readFileSync(join(cwd, tplPath));
if (file === null) {
throw new Error(`Failed to read file at path: ${join(cwd, tplPath)}`);
}
const tplString = getPartsAndHoles(file.toString());
return {
tplPath,
cwd,
tplString,
};
};
// Responsible for replacing holes with values
export const replaceHoles = (args) => (partsAndHoles) => {
const [parts, holes] = partsAndHoles;
const allParts = zip(parts, holes);
return allParts
.map((partOrHole, index) => {
if (typeof partOrHole === 'string') {
// it's a part
return partOrHole;
}
else {
// Currently we are unaware of the difference between {{}} and {{{}}} so we treat them the same: as a literal hole
if ('literal' in partOrHole) {
// it's a literal hole
if (!(partOrHole.literal in args)) {
throw new Error(`argument to fill hole for ${partOrHole.literal} is missing in ${allParts[index - 1]}{{${partOrHole.literal}}}${allParts[index + 1]}}`);
}
return args[partOrHole.literal];
}
}
})
.join('');
};
const loadYaml = (filledYamlString) => {
return yaml.load(filledYamlString);
};
// Responsible for replacing holes in a context with values
export const replaceHolesInCtx = (args) => {
return (ctx) => {
const { tplString } = ctx;
return { ...ctx, filledYamlString: replaceHoles(args)(tplString) };
};
};
export const parseYamlToKdaTx = (args) => (ctx) => {
const { filledYamlString } = ctx;
const kdaToolTx = loadYaml(filledYamlString);
if (!('codeFile' in kdaToolTx && kdaToolTx.codeFile !== undefined)) {
return kdaToolTx;
}
const { codeFile, ...kdaToolTxWithoutCodeFile } = kdaToolTx;
const codeWithHoles = services.filesystem.readFileSync(join(ctx.cwd, codeFile));
if (codeWithHoles === null) {
throw new Error(`Failed to read code file at path: ${join(ctx.cwd, codeFile)}`);
}
const code = replaceHoles(args)(getPartsAndHoles(codeWithHoles));
return {
...kdaToolTxWithoutCodeFile,
code,
};
};
// Responsible for converting a kda tool transaction into a kadena client transaction
export const convertTemplateTxToPactCommand = (tplTx) => {
var _a;
const { data, ...kdaToolTx } = tplTx;
const execPayload = {
exec: {
data: data !== null && data !== void 0 ? data : {},
code: kdaToolTx.code,
},
};
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { publicMeta, meta, code, ...kdaToolTxWithoutMeta } = kdaToolTx;
const metadata = meta || publicMeta || {};
return {
...kdaToolTxWithoutMeta,
payload: execPayload,
meta: {
...metadata,
chainId: metadata.chainId,
creationTime: Math.floor(Date.now() / 1000),
},
nonce: kdaToolTx.nonce ? kdaToolTx.nonce : '',
signers: ((_a = kdaToolTx.signers) !== null && _a !== void 0 ? _a : []).map(publicToPubkey),
networkId: kdaToolTx.networkId,
};
};
export const createPactCommandFromTemplate = (path, args, cwd) => {
return asyncPipe(getPartsAndHolesInCtx, replaceHolesInCtx(args), parseYamlToKdaTx(args), convertTemplateTxToPactCommand)(path, cwd);
};
export const createPactCommandFromStringTemplate = (template, args) => {
return asyncPipe(getPartsAndHoles, replaceHoles(args), loadYaml, convertTemplateTxToPactCommand)(template);
};
// Responsible for zipping together parts and holes
function zip(parts, holes) {
const result = [];
for (let i = 0; i < parts.length; i++) {
result.push(parts[i]);
if (i < holes.length) {
result.push(holes[i]);
}
}
return result;
}
// Responsible for converting a public key to a pubkey
function publicToPubkey(value) {
const pubKey = value.public;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
delete value.public;
return {
...value,
pubKey,
};
}
//# sourceMappingURL=yaml-converter.js.map