-
Notifications
You must be signed in to change notification settings - Fork 5
/
trust.js
318 lines (275 loc) · 9.38 KB
/
trust.js
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
import checkAccount from '~/common/checkAccount';
import checkOptions from '~/common/checkOptions';
const DEFAULT_USER_LIMIT_PERCENTAGE = 50;
const DEFAULT_ORG_LIMIT_PERCENTAGE = 100;
const DEFAULT_TRUST_LIMIT = 3;
const NO_LIMIT_PERCENTAGE = 0;
/**
* Trust submodule to add and remove trust connections.
*
* @access private
*
* @param {Web3} web3 - Web3 instance
* @param {Object} contracts - common contract instances
* @param {Object} utils - utils module instance
*
* @return {Object} - trust module instance
*/
export default function createTrustModule(web3, contracts, utils) {
const { hub } = contracts;
return {
/**
* Find out if safe address has enough incoming trust connections.
*
* @namespace core.trust.isTrusted
*
* @param {Object} account - web3 account instance
* @param {Object} userOptions - options
* @param {string} userOptions.safeAddress - Safe address of user
* @param {string} userOptions.limit - Incoming trust limit
*
* @return {Object} Trust state and number of connections
*/
isTrusted: async (account, userOptions) => {
checkAccount(web3, account);
const options = checkOptions(userOptions, {
safeAddress: {
type: web3.utils.checkAddressChecksum,
},
limit: {
type: 'number',
default: DEFAULT_TRUST_LIMIT,
},
});
const safeAddress = options.safeAddress.toLowerCase();
const response = await utils.requestIndexedDB(
'trust_network',
safeAddress,
);
if (!response) {
return {
trustConnections: 0,
isTrusted: false,
};
}
const trustConnections = response.trusts.filter((connection) => {
return parseInt(connection.limitPercentage, 10) !== NO_LIMIT_PERCENTAGE;
});
return {
trustConnections: trustConnections.length,
isTrusted: trustConnections.length >= options.limit,
};
},
/**
* Get the current state of a users trust network, containing
* data to find transfer path between users.
*
* @namespace core.trust.getNetwork
*
* @param {Object} account - web3 account instance
* @param {Object} userOptions - options
* @param {string} userOptions.safeAddress - Safe address of user
*
* @return {Object} Trust network state
*/
getNetwork: async (account, userOptions) => {
checkAccount(web3, account);
const options = checkOptions(userOptions, {
safeAddress: {
type: web3.utils.checkAddressChecksum,
},
});
const safeAddress = options.safeAddress.toLowerCase();
const response = await utils.requestIndexedDB(
'trust_limits',
safeAddress,
);
if (!response || response.safe === null) {
// Fail silently with empty response / no trust connections when Safe
// does not exist yet
return [];
}
// Find mutual trust connections by comparing incoming addresses with
// outgoing ones of other users
const incomingAddresses = response.safe.incoming.map(
({ userAddress }) => {
return web3.utils.toChecksumAddress(userAddress);
},
);
const mutualFriendsMap = response.safe.incoming.reduce((acc, item) => {
const userAddress = web3.utils.toChecksumAddress(item.userAddress);
if (!(userAddress in acc)) {
acc[userAddress] = [];
}
if (!item.user) {
return acc;
}
item.user.outgoing.forEach((outgoingItem) => {
const canSendToAddress = web3.utils.toChecksumAddress(
outgoingItem.canSendToAddress,
);
const limitPercentage = parseInt(outgoingItem.limitPercentage, 10);
if (
incomingAddresses.includes(canSendToAddress) && // Is trusted by us
limitPercentage !== NO_LIMIT_PERCENTAGE && // Has trust
canSendToAddress !== userAddress && // Filter user trusting itself
canSendToAddress !== options.safeAddress // Filter user trusted by us
) {
acc[userAddress].push(canSendToAddress);
}
});
return acc;
}, {});
return []
.concat(response.safe.incoming)
.concat(response.safe.outgoing)
.reduce((acc, connection) => {
const limitPercentage = parseInt(connection.limitPercentage, 10);
if (limitPercentage === NO_LIMIT_PERCENTAGE) {
return acc;
}
const userAddress = web3.utils.toChecksumAddress(
connection.userAddress,
);
const canSendToAddress = web3.utils.toChecksumAddress(
connection.canSendToAddress,
);
// Filter connections to ourselves
if (userAddress === canSendToAddress) {
return acc;
}
// Merge incoming and outgoing connections
if (userAddress === options.safeAddress) {
acc.push({
isIncoming: false,
isOutgoing: true,
limitPercentageIn: NO_LIMIT_PERCENTAGE,
limitPercentageOut: limitPercentage,
mutualConnections: mutualFriendsMap[canSendToAddress] || [],
safeAddress: canSendToAddress,
});
} else if (canSendToAddress === options.safeAddress) {
acc.push({
isIncoming: true,
isOutgoing: false,
limitPercentageIn: limitPercentage,
limitPercentageOut: NO_LIMIT_PERCENTAGE,
mutualConnections: mutualFriendsMap[userAddress] || [],
safeAddress: userAddress,
});
}
return acc;
}, [])
.reduce((acc, connection) => {
// Find duplicates ...
const index = acc.findIndex((item) => {
return item.safeAddress === connection.safeAddress;
});
// ... and merge them
if (index > -1) {
const {
isIncoming,
isOutgoing,
limitPercentageIn,
limitPercentageOut,
mutualConnections,
safeAddress,
} = acc[index];
acc[index] = {
isIncoming: connection.isIncoming || isIncoming,
isOutgoing: connection.isOutgoing || isOutgoing,
limitPercentageIn:
connection.limitPercentageIn + limitPercentageIn,
limitPercentageOut:
connection.limitPercentageOut + limitPercentageOut,
mutualConnections: mutualConnections.concat(
connection.mutualConnections.filter((item) => {
return !mutualConnections.includes(item);
}),
),
safeAddress,
};
} else {
acc.push(connection);
}
return acc;
}, []);
},
/**
* Give other users possibility to send their Circles to you by
* giving them your trust.
*
* @namespace core.trust.addConnection
*
* @param {Object} account - web3 account instance
* @param {Object} userOptions - options
* @param {string} userOptions.user - trust receiver / sender
* @param {string} userOptions.canSendTo - trust giver / receiver
* @param {number} userOptions.limitPercentage - trust limit in % for transitive transactions
*
* @return {string} - transaction hash
*/
addConnection: async (account, userOptions) => {
checkAccount(web3, account);
const options = checkOptions(userOptions, {
user: {
type: web3.utils.checkAddressChecksum,
},
canSendTo: {
type: web3.utils.checkAddressChecksum,
},
limitPercentage: {
type: 'number',
default: DEFAULT_USER_LIMIT_PERCENTAGE,
},
});
const isOrgSignedup = await hub.methods
.organizations(options.canSendTo)
.call();
if (isOrgSignedup) {
options.limitPercentage = DEFAULT_ORG_LIMIT_PERCENTAGE;
}
const txData = await hub.methods
.trust(options.user, options.limitPercentage)
.encodeABI();
// Call method and return result
return await utils.executeTokenSafeTx(account, {
safeAddress: options.canSendTo,
to: hub.options.address,
txData,
});
},
/**
* Revoke a trust connection with a user. You don't allow this
* user to transfer their Token to or through you.
*
* @namespace core.trust.removeConnection
*
* @param {Object} account - web3 account instance
* @param {Object} userOptions - options
* @param {string} userOptions.user - trust receiver / sender
* @param {string} userOptions.canSendTo - trust giver / receiver
*
* @return {string} - transaction hash
*/
removeConnection: async (account, userOptions) => {
checkAccount(web3, account);
const options = checkOptions(userOptions, {
user: {
type: web3.utils.checkAddressChecksum,
},
canSendTo: {
type: web3.utils.checkAddressChecksum,
},
});
const txData = await hub.methods
.trust(options.user, NO_LIMIT_PERCENTAGE)
.encodeABI();
return await utils.executeTokenSafeTx(account, {
safeAddress: options.canSendTo,
to: hub.options.address,
txData,
});
},
};
}