-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForgeRecover.tsx
348 lines (318 loc) · 12.8 KB
/
ForgeRecover.tsx
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
import assert from "assert";
import { MetalSeal, MetalServiceV2, SymbolService } from "metal-on-symbol";
import mime from "mime";
import React, { useCallback, useEffect, useState } from "react";
import { useForm } from "react-hook-form";
import { Link } from "react-router-dom";
import { Account, MetadataType, MosaicId } from "symbol-sdk";
assert(process.env.REACT_APP_NODE_URL);
const symbolService = new SymbolService({ node_url: process.env.REACT_APP_NODE_URL, repo_factory_config: {
websocketInjected: WebSocket,
websocketUrl: process.env.REACT_APP_NODE_URL.replace('http', 'ws') + '/ws',
}
});
const metalService = new MetalServiceV2(symbolService);
interface FormData {
type: MetadataType;
private_key: string;
target_id?: string;
additive?: number;
text?: string;
payload?: File;
}
const readFile = async (file: File) => await new Promise<ArrayBuffer>((resolve, reject) => {
const reader = new FileReader();
reader.onload = (event) => {
if (event.target?.result) {
resolve(event.target.result as ArrayBuffer);
} else {
reject(new TypeError("Result isn't an ArrayBuffer"));
}
};
reader.onerror = (error) => {
reject(error);
};
reader.readAsArrayBuffer(file);
});
const ForgeRecover = () => {
const [ partial, setPartial ] = useState<boolean>();
const [ metalId, setMetalId ] = useState<string>();
const [ key, setKey ] = useState<string>();
const [ additive, setAdditive ] = useState<number>();
const [ error, setError ] = useState<string>();
const {
handleSubmit,
register,
setValue,
watch,
trigger,
formState: { errors, isValid, isSubmitting }
} = useForm<FormData>({
mode: "onBlur",
defaultValues: {
type: MetadataType.Account,
additive: 0,
},
});
const watchPayload = watch("payload");
// Announce 1st TX only.
const partialForge = useCallback(async (data: FormData) => {
if (!data.payload) {
setError("Payload required.");
return;
}
const payload = data.payload;
try {
setPartial(false);
setMetalId(undefined);
setError(undefined);
const { networkType } = await symbolService.getNetwork();
const signerAccount = Account.createFromPrivateKey(data.private_key, networkType);
const targetId = data.target_id
? [ undefined, new MosaicId(data.target_id), SymbolService.createNamespaceId(data.target_id)][data.type]
: undefined;
const payloadBuffer = await readFile(payload);
const { txs } = await metalService.createForgeTxs(
data.type,
signerAccount.publicAccount,
signerAccount.publicAccount,
targetId,
new Uint8Array(payloadBuffer),
data.additive,
data.text,
);
const batches = await symbolService.buildSignedAggregateCompleteTxBatches(
txs.slice(0, 1),
signerAccount,
[],
);
const errors = await symbolService.executeBatches(batches, signerAccount);
if (errors) {
setError("Transaction error.");
return;
}
setPartial(true);
} catch (e) {
console.error(e);
setError(String(e));
}
}, []);
// Announce all remaining TXs.
const recoveryForge = useCallback(async (data: FormData) => {
if (!data.payload) {
setError("Payload required.");
return;
}
const payload = data.payload;
try {
setMetalId(undefined);
setError(undefined);
const { networkType } = await symbolService.getNetwork();
const signer = Account.createFromPrivateKey(data.private_key, networkType);
const targetId = data.target_id
? [ undefined, new MosaicId(data.target_id), SymbolService.createNamespaceId(data.target_id)][data.type]
: undefined;
const payloadBuffer = await readFile(payload);
const metadataPool = await symbolService.searchBinMetadata(
data.type,
{
source: signer.publicAccount,
target: signer.publicAccount,
targetId
});
const { key, txs, additive } = await metalService.createForgeTxs(
data.type,
signer.publicAccount,
signer.publicAccount,
targetId,
new Uint8Array(payloadBuffer),
data.additive,
data.text,
metadataPool,
);
const batches = await symbolService.buildSignedAggregateCompleteTxBatches(
txs,
signer,
[],
);
const errors = await symbolService.executeBatches(batches, signer);
if (errors) {
setError("Transaction error.");
return;
}
const metalId = MetalServiceV2.calculateMetalId(
data.type,
signer.address,
signer.address,
targetId,
key,
);
setMetalId(metalId);
setAdditive(additive);
setKey(key.toHex());
setPartial(false);
} catch (e) {
console.error(e);
setError(String(e));
}
}, []);
const handlePayloadChange: React.ChangeEventHandler<HTMLInputElement> = useCallback(async (event) => {
const file = event.target.files?.[0];
if (!file) {
return;
}
setValue("payload", file);
setValue("text", new MetalSeal(file.size, mime.getType(file.name) ?? undefined).stringify());
await trigger();
}, [ setValue, trigger ]);
useEffect(() => {
if (watchPayload) {
setError(undefined);
} else {
setError("Payload required.");
}
}, [watchPayload]);
return (<div className="content">
<h1 className="title is-3">Recovery Forge Metal sample</h1>
<form>
<div className="field">
<label className="label">Private Key (* The account will be signer/target/source)</label>
<div className="control">
<input className={ `input ${ errors.private_key ? "is-danger" : "" }` } type="password"
autoComplete="off" {
...register("private_key", { required: "Required field." }) }
/>
</div>
</div>
{ errors.private_key && <div className="field">
<p className="help is-danger">
{ errors.private_key.message }
</p>
</div> }
<div className="field">
<label className="label">Metadata Type (*)</label>
<div className="control">
<div className="select">
<select { ...register('type', { required: "Required field." }) }>
<option value={ MetadataType.Account }>Account</option>
<option value={ MetadataType.Mosaic }>Mosaic</option>
<option value={ MetadataType.Namespace }>Namespace</option>
</select>
</div>
</div>
</div>
<div className="field">
<label className="label">Target ID (Mosaic ID / Namespace ID, Leave blank when Account metadata)</label>
<div className="control">
<input className={ `input ${ errors.target_id ? "is-danger" : "" }` } type="text" {
...register("target_id") }
/>
</div>
</div>
{ errors.target_id && <div className="field">
<p className="help is-danger">
{ errors.target_id.message }
</p>
</div> }
<div className="field">
<label className="label">Additive (Default:0)</label>
<div className="control">
<input
className={ `input ${ errors.additive ? "is-danger" : "" }` }
type="number"
min={ 0 }
max={ 65535 }
{ ...register("additive", {
valueAsNumber: true,
}) }
/>
</div>
</div>
{ errors.additive && <div className="field">
<p className="help is-danger">
{ errors.additive.message }
</p>
</div> }
<div className="field">
<label className="label">Payload (*)</label>
<div className="control">
<div className="file has-name is-fullwidth">
<label className="file-label">
<input className="file-input" type="file" onChange={ handlePayloadChange }/>
<span className="file-cta">
<span className="file-icon">
<i className="fas fa-upload"></i>
</span>
<span className="file-label">
Choose a file…
</span>
</span>
<span className="file-name">
{ watchPayload?.name ?? "Empty" }
</span>
</label>
</div>
</div>
</div>
<div className="field">
<label className="label">Text Section</label>
<div className="control">
<input
className={ `input ${ errors.text ? "is-danger" : "" }` }
type="text"
{ ...register("text") }
/>
</div>
</div>
{ errors.text && <div className="field">
<p className="help is-danger">
{ errors.text.message }
</p>
</div> }
<div className="buttons is-centered">
<button className={ `button is-warning ${ isSubmitting ? "is-loading" : "" }` }
disabled={ !isValid || isSubmitting || !watchPayload || partial }
onClick={ handleSubmit(partialForge) }
>
(1) Partial Forge
</button>
<button className={ `button is-primary ${ isSubmitting ? "is-loading" : "" }` }
disabled={ !isValid || isSubmitting || !watchPayload || !partial }
onClick={ handleSubmit(recoveryForge) }
>
(2) Recovery Forge
</button>
</div>
{ partial ? <div className="notification is-warning is-lignt">
Now your Metal has been forged partially. Please hit "(2) Recovery Forge".
</div> : null }
{ error ? <div className="notification is-danger is-light">
{ error }
</div> : null }
{ metalId ? <div className="notification is-success is-light">
<div className="field">
<label className="label">Forged Metal ID</label>
<div className="control">
<input type="text" className="input" value={ metalId } readOnly={ true }/>
</div>
</div>
<div className="field">
<label className="label">Header chunk Metadata Key</label>
<div className="control">
<input type="text" className="input" value={ key } readOnly={ true }/>
</div>
</div>
<div className="field">
<label className="label">Additive</label>
<div className="control">
<input type="text" className="input" value={ additive } readOnly={ true }/>
</div>
</div>
</div> : null }
<div className="buttons is-centered">
<Link to="/" className="button is-text">Back to Index</Link>
</div>
</form>
</div>);
};
export default ForgeRecover;