-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConsensusCondition.js
286 lines (267 loc) · 9.05 KB
/
ConsensusCondition.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
const { BatchBuilder, TxBuilder } = require('iroha-helpers/lib/chain');
const { queries } = require('iroha-helpers');
const { isPropertyObject, updateSubject, array, includesValue } = require('@m-ld/m-ld');
const crypto = require('crypto');
/**
* Must conform to [A-Za-z0-9_]{1,64}
* @type {string}
* @see https://iroha.readthedocs.io/en/main/develop/api/commands.html#set-account-detail
*/
const PROOF_KEY_PREFIX = 'pk_';
/**
* The expected configuration with which every clone is initialised, when using
* an {@link ConsensusCondition}.
*
* @typedef {import('@m-ld/m-ld').MeldConfig} MeldIrohaConfig
*/
/**
* @typedef {import('@m-ld/m-ld').AppPrincipal} IrohaPrincipal
* @property {string} ed25519PrivateKey clone principal private key for signing
* Iroha transactions
* @see https://iroha.readthedocs.io/en/main/develop/keys.html
*/
/**
* The expected app object with which every clone is initialised, when using a
* {@link ConsensusCondition}.
*
* @typedef {import('@m-ld/m-ld').MeldApp} MeldIrohaApp
* @property {object} [iroha.commandService] Iroha command service
* @property {object} [iroha.queryService] Iroha query service
* @property {IrohaPrincipal} principal clone app principal (override)
* @see https://github.com/hyperledger/iroha-javascript#example
*/
/**
* Utility class to initialise Iroha with a m-ld domain, and register users.
* Requires an admin account.
*/
class IrohaMeldDomain {
/**
* @param {object} opts required parameters for initialisation
* @param {string} opts.domainId m-ld domain name
* @param {string} opts.adminId e.g. `'admin@test'`
* @param {string} opts.adminEd25519PrivateKey
* @param {object} opts.commandService Iroha command service
*/
constructor({
domainId,
adminId,
adminEd25519PrivateKey,
commandService
}) {
this.domainId = domainId;
this.adminId = adminId;
this.adminEd25519PrivateKey = adminEd25519PrivateKey;
this.commandService = commandService;
}
get accountId() {
return `clone@${this.domainId}`;
}
/**
* Initialises Iroha with a m-ld domain based on the given configuration.
* Requires an 'admin' account with permission to create domains and accounts.
*
* Alternatively, the requisite domain and account could be created in the
* genesis block.
*
* @param {object} iroha required parameters for initialisation
* @param {string} iroha.defaultRole e.g. `'user'`
* @param {string} iroha.firstPrincipalEd25519PublicKey first signatory user key
*/
initialise({
defaultRole,
firstPrincipalEd25519PublicKey
}) {
return new BatchBuilder([
new TxBuilder()
.createDomain({
domainId: this.domainId,
defaultRole
})
.addMeta(this.adminId, 1)
.tx,
new TxBuilder()
// Account should get default "user" role (see above)
.createAccount({
// One account for all clones
accountName: 'clone',
domainId: this.domainId,
publicKey: firstPrincipalEd25519PublicKey
})
.addMeta(this.adminId, 1)
.tx
]).setBatchMeta(0)
.sign([this.adminEd25519PrivateKey], 0)
.sign([this.adminEd25519PrivateKey], 1)
.send(this.commandService);
}
/**
* Registers a m-ld principal (user) to Iroha so they can add blocks.
*
* @param {string} newEd25519PublicKey the public key of the new signatory
* @param {string} existingEd25519PrivateKey the private key of an existing signatory
*/
registerPrincipal(newEd25519PublicKey, existingEd25519PrivateKey) {
return new TxBuilder()
.addSignatory({
accountId: this.accountId,
publicKey: newEd25519PublicKey
})
.addMeta(this.accountId, 1)
.sign([existingEd25519PrivateKey])
.send(this.commandService);
}
}
/**
* An agreement condition that proves agreement by adding a block to an Iroha
* blockchain; and tests agreement by inspecting the blockchain for the
* identified block.
*
* @implements {import('@m-ld/m-ld/ext/constraints/Statutory').ShapeAgreementCondition}
*/
class ConsensusCondition {
/**
* Creates the necessary metadata write to use a {@link ConsensusCondition}.
*
* @param {string} [id] the IRI identifier of the condition, for linking this
* condition to a Statute
* @param [requireOverride]
* @returns {import('@m-ld/m-ld').Subject} to be written to the m-ld domain
*/
static declare = (id, requireOverride) => ({
'@id': id,
'@type': 'http://js.m-ld.org/CommonJSModule',
'http://js.m-ld.org/#require': requireOverride || '@m-ld/m-ld-iroha',
'http://js.m-ld.org/#class': 'ConsensusCondition'
});
/**
* @param {import('@m-ld/m-ld/ext/orm').ExtensionEnvironment} env
*/
constructor({ env }) {
const config = /**@type MeldIrohaConfig*/env.config;
const app = /**@type MeldIrohaApp*/env.app;
this.accountId = `clone@${config['@domain']}`;
this.commandService = app.iroha.commandService;
this.queryService = app.iroha.queryService;
/**@type IrohaPrincipal*/this.appPrincipal = app.principal;
}
/**
* The value entered into Iroha as proof of agreement
* @typedef {object} ProofValue
* @property {import('@m-ld/m-ld').GraphSubject[]} state Agreed final state of statutes
* @property {string} pid Principal ID
*/
/**
* @param {import('@m-ld/m-ld').MeldReadState} state
* @param {import('@m-ld/m-ld').GraphUpdate} affected
* @param {import('@m-ld/m-ld').Reference} principalRef
* @returns {Promise<any>}
*/
async prove(
state,
affected,
principalRef
) {
if (principalRef == null)
return false;
const provedState = await new AffectedLoader(affected).load(state);
const proofKey = PROOF_KEY_PREFIX + crypto.randomBytes(16).toString('hex');
await this.setAccountDetail(proofKey, JSON.stringify(/**@type ProofValue*/{
pid: principalRef['@id'],
state: provedState
}));
return proofKey;
}
/**
* @param {import('@m-ld/m-ld').MeldReadState} state
* @param {import('@m-ld/m-ld').GraphUpdate} affected
* @param {any} proof
* @param {import('@m-ld/m-ld').Reference} [principalRef]
* @returns {Promise<true | string>}
*/
async test(
state,
affected,
proof,
principalRef
) {
if (principalRef == null)
return 'No principal in update';
const proofKey = array(proof).find(v => typeof v == 'string' && v.startsWith(PROOF_KEY_PREFIX));
if (proofKey == null)
return 'No proof key in update';
const proofValue = await this.getAccountDetail(proofKey);
if (proofValue == null)
return 'No proof in ledger';
const proved = /**@type ProofValue*/JSON.parse(proofValue);
if (proved.pid !== principalRef['@id'])
return 'Proof principal does not match update principal';
const actualState = await new AffectedLoader(affected).load(state);
// TODO: O(provedState.length x actualState.length)
for (let subject of actualState)
for (let [property, value] of Object.entries(subject))
if (isPropertyObject(property, value))
if (proved.state.find(provedSubject =>
provedSubject['@id'] === subject['@id'] &&
includesValue(provedSubject, property, value)) == null)
return 'Proof does not match update';
return true;
}
setAccountDetail(key, value) {
return new TxBuilder()
.setAccountDetail({
accountId: this.accountId,
key,
// Iroha does not properly escape the value
value: JSON.stringify(value).slice(1, -1)
})
.addMeta(this.accountId, 1)
.sign([this.appPrincipal.ed25519PrivateKey])
.send(this.commandService);
}
async getAccountDetail(key) {
return (await queries.getAccountDetail({
privateKey: this.appPrincipal.ed25519PrivateKey,
creatorAccountId: this.accountId,
queryService: this.queryService,
timeoutLimit: 5000
}, {
accountId: this.accountId,
key,
pageSize: 1,
paginationKey: key,
paginationWriter: this.accountId
}))[this.accountId]?.[key];
}
}
class AffectedLoader {
/** @param {import('@m-ld/m-ld').GraphUpdate} affected */
constructor(affected) {
this.affected = affected;
this.subjectPropertiesToLoad = /**@type {{ [key: string]: Set<string> }}*/{};
this.addProperties(affected['@delete']);
this.addProperties(affected['@insert']);
}
/**
* @param {import('@m-ld/m-ld').MeldReadState} state
* @returns {Promise<import('@m-ld/m-ld').GraphSubject[]>}
*/
load(state) {
return Promise.all(Object.entries(this.subjectPropertiesToLoad)
.map(async ([id, props]) =>
updateSubject(await state.get(id, ...props) ?? { '@id': id }, this.affected)));
}
/** @param {import('@m-ld/m-ld').GraphSubjects} subjects */
addProperties(subjects) {
for (let subject of subjects) {
this.subjectPropertiesToLoad[subject['@id']] ??= new Set;
Object.keys(subject).forEach(property => {
if (isPropertyObject(property, subject[property]))
this.subjectPropertiesToLoad[subject['@id']].add(property);
});
}
}
}
module.exports = {
IrohaMeldDomain,
ConsensusCondition
};