generated from PolymeshAssociation/typescript-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 11
/
AuthorizationRequest.ts
236 lines (210 loc) · 7.34 KB
/
AuthorizationRequest.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
import BigNumber from 'bignumber.js';
import {
consumeAddMultiSigSignerAuthorization,
ConsumeAddMultiSigSignerAuthorizationParams,
consumeAddRelayerPayingKeyAuthorization,
ConsumeAddRelayerPayingKeyAuthorizationParams,
consumeAuthorizationRequests,
ConsumeAuthorizationRequestsParams,
consumeJoinOrRotateAuthorization,
ConsumeJoinOrRotateAuthorizationParams,
Context,
Entity,
Identity,
} from '~/internal';
import {
Authorization,
AuthorizationType,
NoArgsProcedureMethod,
Signer,
SignerValue,
} from '~/types';
import { HumanReadableType } from '~/types/utils';
import { bigNumberToU64, signerToSignerValue, signerValueToSignatory } from '~/utils/conversion';
import { createProcedureMethod, toHumanReadable } from '~/utils/internal';
export interface UniqueIdentifiers {
authId: BigNumber;
}
export interface HumanReadable {
issuer: HumanReadableType<Identity>;
expiry: string | null;
target: SignerValue;
data: HumanReadableType<Authorization>;
id: string;
}
export interface Params {
target: Signer;
issuer: Identity;
expiry: Date | null;
data: Authorization;
}
/**
* Represents a request made by an Identity to another Identity (or Account) for some sort of authorization. This has multiple uses. For example, if Alice
* wants to transfer ownership of one of her Assets to Bob, this method emits an authorization request for Bob,
* who then has to accept it in order to complete the ownership transfer
*/
export class AuthorizationRequest extends Entity<UniqueIdentifiers, HumanReadable> {
/**
* @hidden
* Check if a value is of type {@link UniqueIdentifiers}
*/
public static override isUniqueIdentifiers(identifier: unknown): identifier is UniqueIdentifiers {
const { authId } = identifier as UniqueIdentifiers;
return authId instanceof BigNumber;
}
/**
* Identity or Account to which the request was emitted
*/
public target: Signer;
/**
* Identity that emitted the request
*/
public issuer: Identity;
/**
* Authorization Request data corresponding to type of Authorization
*
* | Type | Data |
* |---------------------------------|---------------------------------|
* | Add Relayer Paying Key | Beneficiary, Relayer, Allowance |
* | Become Agent | Permission Group |
* | Attest Primary Key Rotation | DID |
* | Rotate Primary Key | N/A |
* | Rotate Primary Key to Secondary | Permissions |
* | Transfer Ticker | Ticker |
* | Add MultiSig Signer | Account |
* | Transfer Asset Ownership | Ticker |
* | Join Identity | Permissions |
* | Portfolio Custody | Portfolio |
*/
public data: Authorization;
/**
* date at which the Authorization Request expires and can no longer be accepted.
* At this point, a new Authorization Request must be emitted. Null if the Request never expires
*/
public expiry: Date | null;
/**
* internal identifier for the Request (used to accept/reject/cancel)
*/
public authId: BigNumber;
/**
* @hidden
*/
public constructor(args: UniqueIdentifiers & Params, context: Context) {
const { target, issuer, expiry, data, ...identifiers } = args;
super(identifiers, context);
const { authId } = identifiers;
this.target = target;
this.issuer = issuer;
this.authId = authId;
this.expiry = expiry;
this.data = data;
this.accept = createProcedureMethod<
| ConsumeAuthorizationRequestsParams
| ConsumeJoinOrRotateAuthorizationParams
| ConsumeAddMultiSigSignerAuthorizationParams
| ConsumeAddRelayerPayingKeyAuthorizationParams,
void,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
any
>(
{
getProcedureAndArgs: () => {
switch (this.data.type) {
case AuthorizationType.AddRelayerPayingKey: {
return [consumeAddRelayerPayingKeyAuthorization, { authRequest: this, accept: true }];
}
case AuthorizationType.JoinIdentity:
case AuthorizationType.RotatePrimaryKey:
case AuthorizationType.RotatePrimaryKeyToSecondary: {
return [consumeJoinOrRotateAuthorization, { authRequest: this, accept: true }];
}
case AuthorizationType.AddMultiSigSigner: {
return [consumeAddMultiSigSignerAuthorization, { authRequest: this, accept: true }];
}
default: {
return [consumeAuthorizationRequests, { authRequests: [this], accept: true }];
}
}
},
voidArgs: true,
},
context
);
this.remove = createProcedureMethod<
| ConsumeAuthorizationRequestsParams
| ConsumeJoinOrRotateAuthorizationParams
| ConsumeAddMultiSigSignerAuthorizationParams
| ConsumeAddRelayerPayingKeyAuthorizationParams,
void,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
any
>(
{
getProcedureAndArgs: () => {
switch (this.data.type) {
case AuthorizationType.AddRelayerPayingKey: {
return [
consumeAddRelayerPayingKeyAuthorization,
{ authRequest: this, accept: false },
];
}
case AuthorizationType.JoinIdentity:
case AuthorizationType.RotatePrimaryKeyToSecondary: {
return [consumeJoinOrRotateAuthorization, { authRequest: this, accept: false }];
}
case AuthorizationType.AddMultiSigSigner: {
return [consumeAddMultiSigSignerAuthorization, { authRequest: this, accept: false }];
}
default: {
return [consumeAuthorizationRequests, { authRequests: [this], accept: false }];
}
}
},
voidArgs: true,
},
context
);
}
/**
* Accept the Authorization Request. You must be the target of the Request to be able to accept it
*/
public accept: NoArgsProcedureMethod<void>;
/**
* Remove the Authorization Request
*
* - If you are the Request issuer, this will cancel the Authorization
* - If you are the Request target, this will reject the Authorization
*/
public remove: NoArgsProcedureMethod<void>;
/**
* Returns whether the Authorization Request has expired
*/
public isExpired(): boolean {
const { expiry } = this;
return expiry !== null && expiry < new Date();
}
/**
* Determine whether this Authorization Request exists on chain
*/
public async exists(): Promise<boolean> {
const { authId, target, context } = this;
const auth = await context.polymeshApi.query.identity.authorizations(
signerValueToSignatory(signerToSignerValue(target), context),
bigNumberToU64(authId, context)
);
return auth.isSome;
}
/**
* Return the Authorization's static data
*/
public toHuman(): HumanReadable {
const { data, issuer, target, expiry, authId } = this;
return toHumanReadable({
id: authId,
expiry,
data,
issuer,
target: signerToSignerValue(target),
});
}
}