Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
client.delegate = async ({ from, to, privileges }, extra) => {
let ops = Array.isArray(privileges) ? privileges : [privileges];
ops = ops.map(x => {
if (x.typeUrl && Array.isArray(x.rules)) {
return x;
}
return { typeUrl: x.typeUrl, rules: [] };
});
const txTypes = Object.values(typeUrls).filter(x => x.startsWith('fg:t:'));
if (ops.some(x => txTypes.includes(x.typeUrl) === false)) {
throw new Error('Invalid type url provided for delegation');
}
const address = toDelegateAddress(from.toAddress(), to.toAddress());
const hash = await client.sendDelegateTx(
{
tx: {
itx: {
address,
to: to.toAddress(),
ops,
},
},
wallet: from,
},
extra
);
return [hash, address];
};