generated from PolymeshAssociation/typescript-boilerplate
/
modifyTokenTrustedClaimIssuers.ts
225 lines (195 loc) · 6.51 KB
/
modifyTokenTrustedClaimIssuers.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
import { difference, differenceWith, intersection, isEqual, sortBy } from 'lodash';
import { IdentityId, Ticker, TrustedIssuer, TxTags } from 'polymesh-types/types';
import { Context, Identity, PolymeshError, Procedure, SecurityToken } from '~/internal';
import { ClaimType, ErrorCode, TrustedClaimIssuer } from '~/types';
import { ProcedureAuthorization, TrustedClaimIssuerOperation } from '~/types/internal';
import { tuple } from '~/types/utils';
import {
signerToString,
stringToIdentityId,
stringToTicker,
trustedClaimIssuerToTrustedIssuer,
trustedIssuerToTrustedClaimIssuer,
} from '~/utils/conversion';
export interface ModifyTokenTrustedClaimIssuersAddSetParams {
/**
* array of Identity IDs
*/
claimIssuers: { identity: string | Identity; trustedFor?: ClaimType[] }[];
}
export interface ModifyTokenTrustedClaimIssuersRemoveParams {
/**
* array of Identities (or DIDs) of the default claim issuers
*/
claimIssuers: (string | Identity)[];
}
/**
* @hidden
*/
export type Params = { ticker: string } & (
| (ModifyTokenTrustedClaimIssuersAddSetParams & {
operation: TrustedClaimIssuerOperation.Add | TrustedClaimIssuerOperation.Set;
})
| (ModifyTokenTrustedClaimIssuersRemoveParams & {
operation: TrustedClaimIssuerOperation.Remove;
})
);
const convertArgsToRaw = (
claimIssuers: ModifyTokenTrustedClaimIssuersAddSetParams['claimIssuers'],
rawTicker: Ticker,
context: Context
): { claimIssuersToAdd: [Ticker, TrustedIssuer][]; inputDids: string[] } => {
const claimIssuersToAdd: [Ticker, TrustedIssuer][] = [];
const inputDids: string[] = [];
claimIssuers.forEach(({ identity, trustedFor }) => {
let issuerIdentity: Identity;
if (typeof identity === 'string') {
issuerIdentity = new Identity({ did: identity }, context);
} else {
issuerIdentity = identity;
}
claimIssuersToAdd.push(
tuple(
rawTicker,
trustedClaimIssuerToTrustedIssuer({ identity: issuerIdentity, trustedFor }, context)
)
);
inputDids.push(issuerIdentity.did);
});
return {
claimIssuersToAdd,
inputDids,
};
};
const areSameClaimIssuers = (
currentClaimIssuers: TrustedClaimIssuer[],
claimIssuers: ModifyTokenTrustedClaimIssuersAddSetParams['claimIssuers']
): boolean =>
!differenceWith(
currentClaimIssuers,
claimIssuers,
(
{ identity: { did: aDid }, trustedFor: aTrustedFor },
{ identity: bIdentity, trustedFor: bTrustedFor }
) => {
const sameClaimTypes =
(aTrustedFor === undefined && bTrustedFor === undefined) ||
(aTrustedFor && bTrustedFor && isEqual(sortBy(aTrustedFor), sortBy(bTrustedFor)));
return aDid === signerToString(bIdentity) && !!sameClaimTypes;
}
).length && currentClaimIssuers.length === claimIssuers.length;
/**
* @hidden
*/
export async function prepareModifyTokenTrustedClaimIssuers(
this: Procedure<Params, SecurityToken>,
args: Params
): Promise<SecurityToken> {
const {
context: {
polymeshApi: { query, tx },
},
context,
} = this;
const { ticker } = args;
const rawTicker = stringToTicker(ticker, context);
let claimIssuersToDelete: [Ticker, IdentityId][] = [];
let claimIssuersToAdd: [Ticker, TrustedIssuer][] = [];
let inputDids: string[];
const rawCurrentClaimIssuers = await query.complianceManager.trustedClaimIssuer(rawTicker);
const currentClaimIssuers = rawCurrentClaimIssuers.map(issuer =>
trustedIssuerToTrustedClaimIssuer(issuer, context)
);
const currentClaimIssuerDids = currentClaimIssuers.map(({ identity: { did } }) => did);
if (args.operation === TrustedClaimIssuerOperation.Remove) {
inputDids = args.claimIssuers.map(signerToString);
const notPresent = difference(inputDids, currentClaimIssuerDids);
if (notPresent.length) {
throw new PolymeshError({
code: ErrorCode.ValidationError,
message: 'One or more of the supplied Identities are not Trusted Claim Issuers',
data: {
notPresent,
},
});
}
claimIssuersToDelete = currentClaimIssuers
.filter(({ identity: { did } }) => inputDids.includes(did))
.map(({ identity: { did } }) => tuple(rawTicker, stringToIdentityId(did, context)));
} else {
({ claimIssuersToAdd, inputDids } = convertArgsToRaw(args.claimIssuers, rawTicker, context));
}
if (args.operation === TrustedClaimIssuerOperation.Set) {
claimIssuersToDelete = rawCurrentClaimIssuers.map(({ issuer }) => [rawTicker, issuer]);
if (areSameClaimIssuers(currentClaimIssuers, args.claimIssuers)) {
throw new PolymeshError({
code: ErrorCode.ValidationError,
message: 'The supplied claim issuer list is equal to the current one',
});
}
}
if (args.operation === TrustedClaimIssuerOperation.Add) {
const present = intersection(inputDids, currentClaimIssuerDids);
if (present.length) {
throw new PolymeshError({
code: ErrorCode.ValidationError,
message: 'One or more of the supplied Identities already are Trusted Claim Issuers',
data: {
present,
},
});
}
}
const nonExistentDids: string[] = await context.getInvalidDids(inputDids);
if (nonExistentDids.length) {
throw new PolymeshError({
code: ErrorCode.ValidationError,
message: 'Some of the supplied Identity IDs do not exist',
data: {
nonExistentDids,
},
});
}
if (claimIssuersToDelete.length) {
this.addBatchTransaction(
tx.complianceManager.removeDefaultTrustedClaimIssuer,
{},
claimIssuersToDelete
);
}
if (claimIssuersToAdd.length) {
this.addBatchTransaction(
tx.complianceManager.addDefaultTrustedClaimIssuer,
{},
claimIssuersToAdd
);
}
return new SecurityToken({ ticker }, context);
}
/**
* @hidden
*/
export function getAuthorization(
this: Procedure<Params, SecurityToken>,
{ ticker, operation }: Params
): ProcedureAuthorization {
const transactions = [];
if (operation !== TrustedClaimIssuerOperation.Add) {
transactions.push(TxTags.complianceManager.RemoveDefaultTrustedClaimIssuer);
}
if (operation !== TrustedClaimIssuerOperation.Remove) {
transactions.push(TxTags.complianceManager.AddDefaultTrustedClaimIssuer);
}
return {
permissions: {
transactions,
tokens: [new SecurityToken({ ticker }, this.context)],
portfolios: [],
},
};
}
/**
* @hidden
*/
export const modifyTokenTrustedClaimIssuers = (): Procedure<Params, SecurityToken> =>
new Procedure(prepareModifyTokenTrustedClaimIssuers, getAuthorization);