UNPKG

openchain-sdk-yxl-ts

Version:

OpenChain SDK for browser

88 lines (68 loc) 2.06 kB
import protobuf from 'protobufjs'; import long from 'long'; import bundleJson from '../../crypto/protobuf/bundle.js'; import errors from '../../exception/index.js'; /** * Account set privilege * @param args * @return {payload} */ export default function (args) { try { const { sourceAddress, masterWeight, txThreshold, metadata, signers, typeThresholds } = args; const root = protobuf.Root.fromJSON(bundleJson); let signersList = []; let typeThresholdsList = []; const signer = root.lookupType('protocol.Signer'); if (Array.isArray(signers)) { signers.forEach(item => { let signerMsg = signer.create({ address: item.address, weight: long.fromValue(item.weight), }); signersList.push(signerMsg); }); } const typeThreshold = root.lookupType('protocol.OperationTypeThreshold'); if (Array.isArray(typeThresholds)) { typeThresholds.forEach(item => { let typeThresholdMsg = typeThreshold.create({ type: parseInt(item.type), threshold: long.fromValue(item.threshold), }); typeThresholdsList.push(typeThresholdMsg); }); } const setPrivilege = root.lookupType('protocol.OperationSetPrivilege'); const opt = {}; if (masterWeight) { opt.masterWeight = masterWeight; } if (txThreshold) { opt.txThreshold = txThreshold; } if (signersList.length > 0) { opt.signers = signersList; } if (typeThresholdsList.length > 0) { opt.typeThresholds = typeThresholdsList; } const setPrivilegeMsg = setPrivilege.create(opt); const operation = root.lookupType('protocol.Operation'); const payload = { sourceAddress, type: operation.Type.SET_PRIVILEGE, setPrivilege: setPrivilegeMsg, }; if (metadata) { payload.metadata = metadata; } const err = operation.verify(payload); if (err) { throw Error(err); } return operation.create(payload); } catch (err) { throw err; } };