@tedcryptoorg/cosmos-signer
Version:
Cosmos Signer - A library for signing transactions for Cosmos SDK chains
136 lines (135 loc) • 5.56 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createAuthzAminoConverters = createAuthzAminoConverters;
exports.createAuthzExecAminoConverters = createAuthzExecAminoConverters;
const moment_1 = __importDefault(require("moment"));
const authz_js_1 = require("cosmjs-types/cosmos/authz/v1beta1/authz.js");
const authz_js_2 = require("cosmjs-types/cosmos/staking/v1beta1/authz.js");
function createAuthzAuthorizationAminoConverter() {
return {
"/cosmos.authz.v1beta1.GenericAuthorization": {
aminoType: "cosmos-sdk/GenericAuthorization",
toAmino: (value) => authz_js_1.GenericAuthorization.decode(value),
fromAmino: ({ msg }) => (authz_js_1.GenericAuthorization.encode(authz_js_1.GenericAuthorization.fromPartial({
msg
})).finish())
},
"/cosmos.staking.v1beta1.StakeAuthorization": {
aminoType: "cosmos-sdk/StakeAuthorization",
toAmino: (value) => {
const { allowList, maxTokens, authorizationType } = authz_js_2.StakeAuthorization.decode(value);
return {
Validators: {
type: "cosmos-sdk/StakeAuthorization/AllowList",
value: {
allow_list: allowList
}
},
max_tokens: maxTokens,
authorization_type: authorizationType
};
},
fromAmino: ({ allow_list, max_tokens, authorization_type }) => (authz_js_2.StakeAuthorization.encode(authz_js_2.StakeAuthorization.fromPartial({
allowList: allow_list,
maxTokens: max_tokens,
authorizationType: authorization_type
})).finish())
}
};
}
const dateConverter = {
toAmino(date) {
return (0, moment_1.default)(Number(date.seconds) * 1000).utc().format();
},
fromAmino(date) {
return {
seconds: (0, moment_1.default)(date).unix(),
nanos: 0
};
}
};
function createAuthzAminoConverters() {
const grantConverter = createAuthzAuthorizationAminoConverter();
return {
"/cosmos.authz.v1beta1.MsgGrant": {
aminoType: "cosmos-sdk/MsgGrant",
toAmino: ({ granter, grantee, grant }) => {
const converter = grantConverter[grant.authorization.typeUrl];
return {
granter,
grantee,
grant: {
authorization: {
type: converter.aminoType,
value: converter.toAmino(grant.authorization.value)
},
expiration: dateConverter.toAmino(grant.expiration)
}
};
},
fromAmino: ({ granter, grantee, grant }) => {
const protoType = Object.keys(grantConverter).find(type => grantConverter[type].aminoType === grant.authorization.type);
const converter = grantConverter[protoType];
return {
granter,
grantee,
grant: {
authorization: {
typeUrl: protoType,
value: converter.fromAmino(grant.authorization.value)
},
expiration: dateConverter.fromAmino(grant.expiration)
}
};
},
},
"/cosmos.authz.v1beta1.MsgRevoke": {
aminoType: "cosmos-sdk/MsgRevoke",
toAmino: ({ granter, grantee, msgTypeUrl }) => ({
granter,
grantee,
msg_type_url: msgTypeUrl
}),
fromAmino: ({ granter, grantee, msg_type_url }) => ({
granter,
grantee,
msgTypeUrl: msg_type_url
}),
},
};
}
function createAuthzExecAminoConverters(registry, aminoTypes) {
return {
"/cosmos.authz.v1beta1.MsgExec": {
aminoType: "cosmos-sdk/MsgExec",
toAmino: ({ grantee, msgs }) => ({
grantee,
msgs: msgs.map(({ typeUrl, value }) => {
const msgType = registry.lookupType(typeUrl);
if (msgType === undefined) {
throw new Error(`No message type registered for ${typeUrl}`);
}
return aminoTypes.toAmino({ typeUrl, value: msgType.decode(value) });
})
}),
fromAmino: ({ grantee, msgs }) => ({
grantee,
msgs: msgs.map(({ type, value }) => {
const proto = aminoTypes.fromAmino({ type, value });
const { typeUrl } = proto;
const msgType = registry.lookupType(typeUrl);
if (msgType === undefined) {
throw new Error(`No message type registered for ${typeUrl}`);
}
return {
typeUrl,
value: msgType.encode(msgType.fromPartial(proto.value)).finish()
};
})
}),
},
};
}