-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathforge_recover.ts
93 lines (86 loc) · 2.58 KB
/
forge_recover.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
import "./env";
import assert from "assert";
import fs from "fs";
import { MetalSeal, MetalServiceV2, SymbolService } from "metal-on-symbol";
import mime from "mime";
import { Account, MetadataType, MosaicId, NamespaceId, NetworkType, PublicAccount } from "symbol-sdk";
// Edit here -------------
const nodeUrl = process.env.TEST_NODE_URL;
const privateKey = process.env.TEST_PRIVATE_KEY; // The account will be signer/source/target
const payloadFilePath = process.argv[2];
let text = process.argv[3];
// -----------------------
assert(payloadFilePath);
const payload = fs.readFileSync(payloadFilePath);
text = text ?? new MetalSeal(payload.length, mime.getType(payloadFilePath) ?? undefined).stringify();
assert(nodeUrl);
const symbolService = new SymbolService({ node_url: nodeUrl });
const metalService = new MetalServiceV2(symbolService);
assert(privateKey);
const signerAccount = Account.createFromPrivateKey(privateKey, NetworkType.TEST_NET);
const forgeMetal = async (
type: MetadataType,
sourcePubAccount: PublicAccount,
targetPubAccount: PublicAccount,
targetId: undefined | MosaicId | NamespaceId,
payload: Uint8Array,
signerAccount: Account,
cosignerAccounts: Account[],
additive?: number,
text?: string,
) => {
const metadataPool = await symbolService.searchBinMetadata(
type,
{
source: sourcePubAccount,
target: targetPubAccount,
targetId
});
const { key, txs, additive: newAdditive } = await metalService.createForgeTxs(
type,
sourcePubAccount,
targetPubAccount,
targetId,
payload,
additive,
text,
metadataPool,
);
const batches = await symbolService.buildSignedAggregateCompleteTxBatches(
txs,
signerAccount,
cosignerAccounts,
);
const errors = await symbolService.executeBatches(batches, signerAccount);
if (errors) {
throw Error("Transaction error.");
}
const metalId = MetalServiceV2.calculateMetalId(
type,
sourcePubAccount.address,
targetPubAccount.address,
targetId,
key,
);
return {
metalId,
key,
additive: newAdditive,
};
};
forgeMetal(
MetadataType.Account,
signerAccount.publicAccount,
signerAccount.publicAccount,
undefined,
payload,
signerAccount,
[],
undefined,
text,
).then(({ metalId, key, additive }) => {
console.log(`Forged! metalId=${metalId},key=${key.toHex()},additive=${additive}`);
}).catch((e) => {
console.error(e);
process.exit(1);
});