generated from PolymeshAssociation/typescript-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 11
/
AuthorizationRequest.ts
139 lines (117 loc) · 3.93 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
import BigNumber from 'bignumber.js';
import { Entity, Identity } from '~/api/entities';
import { consumeAuthorizationRequests, consumeJoinIdentityAuthorization } from '~/api/procedures';
import { Context, TransactionQueue } from '~/base';
import { Authorization, AuthorizationType, Signer } from '~/types';
export interface UniqueIdentifiers {
authId: BigNumber;
}
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 her asset ALICETOKEN to Bob, an authorization request gets emitted to Bob,
* who then has to accept it in order for the ownership transfer to be complete
*/
export class AuthorizationRequest extends Entity<UniqueIdentifiers> {
/**
* @hidden
* Check if a value is of type [[UniqueIdentifiers]]
*/
public static 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 |
* |-----------------------------|-----------|
* | Attest Primary Key Rotation | DID |
* | Rotate Primary Key | DID |
* | Transfer Ticker | Ticker |
* | Add MultiSig Signer | Account |
* | Transfer Token Ownership | Ticker |
* | Join Identity | DID |
* | Portfolio Custody | Portfolio |
* | Custom | Custom |
* | No Data | N/A |
*/
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;
}
/**
* Accept the authorization request. You must be the target of the request to be able to accept it
*/
public accept(): Promise<TransactionQueue> {
const {
context,
data: { type },
} = this;
if (type === AuthorizationType.JoinIdentity) {
return consumeJoinIdentityAuthorization.prepare({ authRequest: this, accept: true }, context);
}
return consumeAuthorizationRequests.prepare({ authRequests: [this], accept: true }, context);
}
/**
* 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(): Promise<TransactionQueue> {
const {
context,
data: { type },
} = this;
if (type === AuthorizationType.JoinIdentity) {
return consumeJoinIdentityAuthorization.prepare(
{ authRequest: this, accept: false },
context
);
}
return consumeAuthorizationRequests.prepare(
{ authRequests: [this], accept: false },
this.context
);
}
/**
* Returns whether the Authorization Request has expired
*/
public isExpired(): boolean {
const { expiry } = this;
return expiry !== null && expiry < new Date();
}
}