UNPKG

@archwayhq/arch3-core

Version:

Core library to interact with Archway Network

122 lines 6.07 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.createAuthzAminoConverters = void 0; const authz_1 = require("cosmjs-types/cosmos/authz/v1beta1/authz"); const tx_1 = require("cosmjs-types/cosmos/authz/v1beta1/tx"); const authz_2 = require("cosmjs-types/cosmos/bank/v1beta1/authz"); const timestamp_1 = require("cosmjs-types/google/protobuf/timestamp"); const helpers_1 = require("cosmjs-types/helpers"); /** * Creates the Amino converters for the Authz tx messages * * @returns to be used in a client */ const createAuthzAminoConverters = () => { return { [tx_1.MsgGrant.typeUrl]: { aminoType: 'cosmos-sdk/MsgGrant', toAmino: ({ granter, grantee, grant }) => { var _a, _b, _c; if (!grant || !grant.authorization) { throw new Error(`Unsupported grant type: '${(_a = grant === null || grant === void 0 ? void 0 : grant.authorization) === null || _a === void 0 ? void 0 : _a.typeUrl}'`); } let authorizationValue; switch ((_b = grant === null || grant === void 0 ? void 0 : grant.authorization) === null || _b === void 0 ? void 0 : _b.typeUrl) { case authz_1.GenericAuthorization.typeUrl: { const generic = authz_1.GenericAuthorization.decode(grant.authorization.value); authorizationValue = { type: 'cosmos-sdk/GenericAuthorization', value: { msg: generic.msg, }, }; break; } case authz_2.SendAuthorization.typeUrl: { const spend = authz_2.SendAuthorization.decode(grant.authorization.value); authorizationValue = { type: 'cosmos-sdk/SendAuthorization', value: { // eslint-disable-next-line camelcase, @typescript-eslint/naming-convention spend_limit: spend.spendLimit, // eslint-disable-next-line camelcase, @typescript-eslint/naming-convention allow_list: ((_c = spend.allowList) === null || _c === void 0 ? void 0 : _c.length) ? spend.allowList : undefined, }, }; break; } default: throw new Error(`Unsupported grant type: '${grant.authorization.typeUrl}'`); } return { granter, grantee, grant: { authorization: authorizationValue, expiration: grant.expiration ? (0, helpers_1.fromTimestamp)(grant.expiration) .toISOString() .replace(/\.\d{3}Z$/, 'Z') : undefined, }, }; }, fromAmino: ({ granter, grantee, grant, }) => { var _a, _b; const authorizationType = (_a = grant === null || grant === void 0 ? void 0 : grant.authorization) === null || _a === void 0 ? void 0 : _a.type; let authorizationValue; switch (authorizationType) { case 'cosmos-sdk/GenericAuthorization': { authorizationValue = { typeUrl: authz_1.GenericAuthorization.typeUrl, value: authz_1.GenericAuthorization.encode({ msg: grant.authorization.value.msg, }).finish(), }; break; } case 'cosmos-sdk/SendAuthorization': { authorizationValue = { typeUrl: authz_2.SendAuthorization.typeUrl, value: authz_2.SendAuthorization.encode(authz_2.SendAuthorization.fromPartial({ spendLimit: grant.authorization.value.spend_limit, allowList: grant.authorization.value.allow_list, })).finish(), }; break; } default: throw new Error(`Unsupported grant type: '${(_b = grant === null || grant === void 0 ? void 0 : grant.authorization) === null || _b === void 0 ? void 0 : _b.type}'`); } return tx_1.MsgGrant.fromPartial({ granter, grantee, grant: { authorization: authorizationValue, expiration: grant.expiration ? timestamp_1.Timestamp.fromPartial((0, helpers_1.fromJsonTimestamp)(grant.expiration)) : undefined, }, }); }, }, [tx_1.MsgRevoke.typeUrl]: { aminoType: 'cosmos-sdk/MsgRevoke', toAmino: ({ granter, grantee, msgTypeUrl }) => { return { granter, grantee, // eslint-disable-next-line camelcase, @typescript-eslint/naming-convention msg_type_url: msgTypeUrl, }; }, // eslint-disable-next-line camelcase, @typescript-eslint/naming-convention fromAmino: ({ granter, grantee, msg_type_url }) => tx_1.MsgRevoke.fromPartial({ granter, grantee, // eslint-disable-next-line camelcase msgTypeUrl: msg_type_url, }), }, }; }; exports.createAuthzAminoConverters = createAuthzAminoConverters; //# sourceMappingURL=tx.js.map