UNPKG

openchain-sdk-yxl

Version:

openchain sdk

206 lines (174 loc) 5.1 kB
'use strict'; const is = require('is-type-of'); const { keypair } = require('openchain-encryption-nodejs-yxl'); const errors = require('../exception'); const proto = exports; // 账户激活操作 proto.accountActivateOperation = function(args) { try { if (is.array(args) || !is.object(args)) { return this._responseError(errors.INVALID_ARGUMENTS); } const schema = { sourceAddress: { required: false, address: true, }, destAddress: { required: true, address: true, }, initBalance: { required: true, numeric: true, }, metadata: { required: false, string: true, } }; if (!this._validate(args, schema).tag) { const msg = this._validate(args, schema).msg; return this._responseError(errors[msg]); } if (args.sourceAddress && args.destAddress === args.sourceAddress) { return this._responseError(errors.SOURCEADDRESS_EQUAL_DESTADDRESS_ERROR); } return this._responseData({ operation: { type: 'activateAccount', data: args, }, }); } catch (err) { throw err; } }; /** * Creates an account set metadata operation. * @param {Object} args - Operation arguments * @param {string} args.key - The metadata key to set * @param {string} [args.sourceAddress] - Optional source account address * @param {boolean} [args.deleteFlag] - Optional flag to delete the metadata * @param {string} [args.metadata] - Optional metadata value * @param {string} [args.value] - The metadata value (must be non-empty and <= 256000 chars) * @param {string} [args.version] - Optional version number * @returns {Object} Response containing the operation data or error * @throws {Error} If an unexpected error occurs */ proto.accountSetMetadataOperation = function(args) { try { if (is.array(args) || !is.object(args)) { return this._responseError(errors.INVALID_ARGUMENTS); } const schema = { key: { required: true, string: true, }, sourceAddress: { required: false, address: true, }, deleteFlag: { required: false, boolean: true, }, metadata: { required: false, string: true, } }; if (!this._validate(args, schema).tag) { const msg = this._validate(args, schema).msg; return this._responseError(errors[msg]); } let value = args.value; if (!is.string(value) || value.trim().length === 0 || value.length > 256000) { return this._responseError(errors.INVALID_DATAVALUE_ERROR); } if (args.version && !this._isAvailableVersion(args.version)) { return this._responseError(errors.INVALID_DATAVERSION_ERROR) } return this._responseData({ operation: { type: 'accountSetMetadata', data: args, }, }); } catch (err) { throw err; } }; proto.accountSetPrivilegeOperation = function(args = {}) { try { if (is.array(args) || !is.object(args)) { return this._responseError(errors.INVALID_ARGUMENTS); } const schema = { sourceAddress: { required: false, address: true, }, metadata: { required: false, string: true, }, }; if (!this._validate(args, schema).tag) { const msg = this._validate(args, schema).msg; return this._responseError(errors[msg]); } let maxInt32 = Math.pow(2, 32) - 1; if (!is.undefined(args.masterWeight) && !this._isAvailableValue(args.masterWeight, -1, maxInt32)) { return this._responseError(errors.INVALID_MASTERWEIGHT_ERROR) } if (!is.undefined(args.txThreshold) && !this._isAvailableValue(args.txThreshold)) { return this._responseError(errors.INVALID_TX_THRESHOLD_ERROR) } const signers = args.signers; const typeThresholds = args.typeThresholds; if (is.array(signers) && signers.length > 0) { let msg = ''; signers.some(item => { if (!keypair.checkAddress(item.address)) { msg = 'INVALID_SIGNER_ADDRESS_ERROR'; return true; } if (!this._isAvailableValue(item.weight, -1, maxInt32)) { msg = 'INVALID_SIGNER_WEIGHT_ERROR'; return true; } }); if (msg !== '') { return this._responseError(errors[msg]); } } if (is.array(typeThresholds) && typeThresholds.length > 0) { let msg = ''; typeThresholds.some(item => { if (!this._isAvailableValue(item.type, -1, 100)) { msg = 'INVALID_OPERATION_TYPE_ERROR'; return true; } if (!this._isAvailableValue(item.threshold)) { msg = 'INVALID_TYPE_THRESHOLD_ERROR'; return true; } }); if (msg !== '') { return this._responseError(errors[msg]); } } return this._responseData({ operation: { type: 'accountSetPrivilege', data: args, }, }); } catch (err) { throw err; } };