-
Notifications
You must be signed in to change notification settings - Fork 152
/
Authorization.ts
145 lines (129 loc) · 4.28 KB
/
Authorization.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import { JSONSerializable } from '../../../util/json';
import { GenericAuthorization } from './GenericAuthorization';
import { SendAuthorization } from './SendAuthorization';
import { StakeAuthorization } from './StakeAuthorization';
import { Any } from '@terra-money/terra.proto/google/protobuf/any';
import { Grant as Grant_pb } from '@terra-money/terra.proto/cosmos/authz/v1beta1/authz';
export class AuthorizationGrant extends JSONSerializable<
AuthorizationGrant.Amino,
AuthorizationGrant.Data,
AuthorizationGrant.Proto
> {
constructor(public authorization: Authorization, public expiration: Date) {
super();
}
public static fromAmino(
amino: AuthorizationGrant.Amino,
isClassic?: boolean
): AuthorizationGrant {
const { authorization, expiration } = amino;
return new AuthorizationGrant(
Authorization.fromAmino(authorization, isClassic),
new Date(expiration)
);
}
public toAmino(isClassic?: boolean): AuthorizationGrant.Amino {
const { authorization, expiration } = this;
return {
authorization: authorization.toAmino(isClassic),
expiration: expiration.toISOString().replace(/\.000Z$/, 'Z'),
};
}
public static fromData(
data: AuthorizationGrant.Data,
isClassic?: boolean
): AuthorizationGrant {
const { authorization, expiration } = data;
return new AuthorizationGrant(
Authorization.fromData(authorization, isClassic),
new Date(expiration)
);
}
public toData(isClassic?: boolean): AuthorizationGrant.Data {
const { authorization, expiration } = this;
return {
authorization: authorization.toData(isClassic),
expiration: expiration.toISOString().replace(/\.000Z$/, 'Z'),
};
}
public static fromProto(
proto: AuthorizationGrant.Proto,
isClassic?: boolean
): AuthorizationGrant {
return new AuthorizationGrant(
Authorization.fromProto(proto.authorization as Any, isClassic),
proto.expiration as Date
);
}
public toProto(isClassic?: boolean): AuthorizationGrant.Proto {
const { authorization, expiration } = this;
return Grant_pb.fromPartial({
authorization: authorization.packAny(isClassic),
expiration,
});
}
}
export namespace AuthorizationGrant {
export interface Amino {
authorization: Authorization.Amino;
expiration: string;
}
export interface Data {
authorization: Authorization.Data;
expiration: string;
}
export type Proto = Grant_pb;
}
export type Authorization =
| SendAuthorization
| GenericAuthorization
| StakeAuthorization;
export namespace Authorization {
export type Amino = SendAuthorization.Amino | GenericAuthorization.Amino;
export type Data =
| SendAuthorization.Data
| GenericAuthorization.Data
| StakeAuthorization.Data;
export type Proto = Any;
export function fromAmino(
data: Authorization.Amino,
isClassic?: boolean
): Authorization {
switch (data.type) {
case 'msgauth/SendAuthorization':
case 'cosmos-sdk/SendAuthorization':
return SendAuthorization.fromAmino(data, isClassic);
case 'msgauth/GenericAuthorization':
case 'cosmos-sdk/GenericAuthorization':
return GenericAuthorization.fromAmino(data, isClassic);
}
}
export function fromData(
data: Authorization.Data,
isClassic?: boolean
): Authorization {
switch (data['@type']) {
case '/cosmos.authz.v1beta1.GenericAuthorization':
return GenericAuthorization.fromData(data, isClassic);
case '/cosmos.bank.v1beta1.SendAuthorization':
return SendAuthorization.fromData(data, isClassic);
case '/cosmos.staking.v1beta1.StakeAuthorization':
return StakeAuthorization.fromData(data, isClassic);
}
}
export function fromProto(
proto: Authorization.Proto,
isClassic?: boolean
): Authorization {
const typeUrl = proto.typeUrl;
switch (typeUrl) {
case '/cosmos.authz.v1beta1.GenericAuthorization':
return GenericAuthorization.unpackAny(proto, isClassic);
case '/cosmos.bank.v1beta1.SendAuthorization':
return SendAuthorization.unpackAny(proto, isClassic);
case '/cosmos.staking.v1beta1.StakeAuthorization':
return StakeAuthorization.unpackAny(proto, isClassic);
}
throw new Error(`Authorization type ${typeUrl} not recognized`);
}
}