forked from Agoric/agoric-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathissuerStorage.js
201 lines (184 loc) · 6.7 KB
/
issuerStorage.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
import { Fail } from '@endo/errors';
import { E } from '@endo/eventual-send';
import { deeplyFulfilledObject, objectMap } from '@agoric/internal';
import { provideDurableWeakMapStore } from '@agoric/vat-data';
import { cleanKeywords } from './cleanProposal.js';
import { makeIssuerRecord } from './issuerRecord.js';
const STORAGE_INSTANTIATED_KEY = 'IssuerStorageInstantiated';
/**
* Make the Issuer Storage.
*
* @param {import('@agoric/vat-data').Baggage} zcfBaggage
*/
export const provideIssuerStorage = zcfBaggage => {
/** @type {WeakMapStore<Brand,IssuerRecord>} */
const brandToIssuerRecord = provideDurableWeakMapStore(
zcfBaggage,
'brandToIssuerRecord',
);
/** @type {WeakMapStore<Issuer,IssuerRecord>} */
const issuerToIssuerRecord = provideDurableWeakMapStore(
zcfBaggage,
'issuerToIssuerRecord',
);
let instantiated = zcfBaggage.has(STORAGE_INSTANTIATED_KEY);
const assertInstantiated = () => {
instantiated || Fail`issuerStorage has not been instantiated`;
};
/**
* If we already know the entire issuer record, such as for a
* ZCFMint issuer, or for an issuer that Zoe has told us about,
* store the issuerRecord directly.
*
* A note on checking that the issuer and brand match: Because of
* how we use `storeIssuerRecord`, the issuer and brand in the
* `issuerRecord` parameter are guaranteed to match, and we do not
* need to do another asynchronous call.
*
* We use `storeIssuerRecord` in 4 cases: 1) ZoeMint records, 2)
* ZCFMint records, 3) within `storeIssuer`, and 4) storing an
* issuer record in ZCF that we obtained from Zoe that originally
* went through `storeIssuer`. In ZoeMints (repackaged as ZCFMints),
* we create the issuers and brands ourselves using ERTP directly,
* so we know they are not malicious and that the issuers and brands
* match. In the last two cases, we go through the
* brand-issuer-match check in `storeIssuer`.
*
* The reason why we need the `has` check below is for the 4th case
* above, in which we store an issuer record in ZCF that we obtained
* from Zoe. `WeakMapStore.init` errors if the key is already present,
* and because an issuer can be used more than once in the same
* contract, we need to make sure we aren't trying to `init` twice.
* If the issuer and its record are already present, we do not need
* to add the issuer again in ZCF.
*
*
* @param {IssuerRecord} issuerRecord
* @returns {IssuerRecord}
*/
const storeIssuerRecord = issuerRecord => {
assertInstantiated();
const { brand, issuer } = issuerRecord;
if (issuerToIssuerRecord.has(issuer)) {
return issuerToIssuerRecord.get(issuer);
}
brandToIssuerRecord.init(brand, issuerRecord);
issuerToIssuerRecord.init(issuer, issuerRecord);
return issuerToIssuerRecord.get(issuer);
};
const getByBrand = brand => {
assertInstantiated();
return brandToIssuerRecord.get(brand);
};
/**
* If the issuer is already stored, return the issuerRecord.
* Otherwise, make and save the issuerRecord.
*
* @param {ERef<Issuer>} issuerP
* @returns {Promise<IssuerRecord>}
*/
const storeIssuer = async issuerP => {
assertInstantiated();
const brandP = E(issuerP).getBrand();
const brandIssuerMatchP = E(brandP).isMyIssuer(issuerP);
const displayInfoP = E(brandP).getDisplayInfo();
/** @type {[Issuer,Brand,boolean,DisplayInfo]} */
const [issuer, brand, brandIssuerMatch, displayInfo] = await Promise.all([
issuerP,
brandP,
brandIssuerMatchP,
displayInfoP,
]);
// AWAIT /////
if (issuerToIssuerRecord.has(issuer)) {
return issuerToIssuerRecord.get(issuer);
}
// A malicious issuer may use another issuer's valid brand.
// Therefore, we must check that the brand recognizes the issuer
// as its own and vice versa. However, we only need to check this
// once per issuer per ZoeService, and we do not need to check
// this for issuers that the ZoeService creates, such as issuers
// made with ZCFMints.
// In the case of a malicious issuer using another issuer's brand,
// the line below will throw in all cases. However, in the case of
// a malicious issuer using a *malicious* brand, the malicious
// issuer and brand may coordinate to change their answers. At
// this point, Zoe has done all it could. Zoe does not prevent or
// intend to prevent issuer misbehavior in general, so the user
// *must* rely on the good behavior of the issuers used in the
// smart contracts they use.
brandIssuerMatch || Fail`issuer was using a brand which was not its own`;
const issuerRecord = makeIssuerRecord(brand, issuer, displayInfo);
storeIssuerRecord(issuerRecord);
return getByBrand(brand);
};
/** @type {GetAssetKindByBrand} */
const getAssetKindByBrand = brand => {
assertInstantiated();
return getByBrand(brand).assetKind;
};
/**
*
* @param {IssuerKeywordRecord} uncleanIssuerKeywordRecord
* @returns {Promise<{ issuers: IssuerKeywordRecord,
* brands: BrandKeywordRecord }>}
*/
const storeIssuerKeywordRecord = async uncleanIssuerKeywordRecord => {
assertInstantiated();
cleanKeywords(uncleanIssuerKeywordRecord);
const issuerRecordPs = objectMap(uncleanIssuerKeywordRecord, issuerP =>
storeIssuer(issuerP),
);
const issuerRecords = await deeplyFulfilledObject(issuerRecordPs);
const issuers = objectMap(issuerRecords, ({ issuer }) => issuer);
const brands = objectMap(issuerRecords, ({ brand }) => brand);
return harden({ issuers, brands });
};
/**
* @template {AssetKind} K
* @param {Issuer<K>} issuer
* @returns {Brand<K>}
*/
const getBrandForIssuer = issuer => {
assertInstantiated();
// @ts-expect-error cast
return issuerToIssuerRecord.get(issuer).brand;
};
/**
* @template {AssetKind} K
* @param {Brand<K>} brand
* @returns {Issuer<K>}
*/
const getIssuerForBrand = brand => {
assertInstantiated();
// @ts-expect-error cast
return brandToIssuerRecord.get(brand).issuer;
};
/**
* @param {Issuer[]} issuers
* @returns {IssuerRecords}
*/
const getIssuerRecords = issuers => {
assertInstantiated();
return issuers.map(issuerToIssuerRecord.get);
};
const instantiate = (issuerRecords = []) => {
if (!zcfBaggage.has(STORAGE_INSTANTIATED_KEY)) {
zcfBaggage.init(STORAGE_INSTANTIATED_KEY, true);
instantiated = true;
for (const record of issuerRecords) {
storeIssuerRecord(record);
}
}
};
return {
storeIssuerKeywordRecord,
storeIssuer,
storeIssuerRecord,
getAssetKindByBrand,
getBrandForIssuer,
getIssuerForBrand,
getIssuerRecords,
instantiate,
};
};