-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetch_by_key.ts
More file actions
60 lines (52 loc) · 2.05 KB
/
Copy pathfetch_by_key.ts
File metadata and controls
60 lines (52 loc) · 2.05 KB
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
import "./env"
import assert from "assert";
import fs from "fs";
import { MetalSeal, MetalServiceV2, SymbolService } from "metal-on-symbol";
import mime from "mime";
import { Account, Address, MetadataType, MosaicId, NamespaceId, NetworkType, UInt64 } 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 key = UInt64.fromHex(process.argv[2]);
// -----------------------
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 fetchMetal = async (
type: MetadataType,
sourceAddress: Address,
targetAddress: Address,
targetId: undefined | MosaicId | NamespaceId,
key: UInt64
) => {
const { payload, text } = await metalService.fetch(type, sourceAddress, targetAddress, targetId, key);
const metalId = MetalServiceV2.calculateMetalId(type, sourceAddress, targetAddress, targetId, key);
return { payload, text, metalId };
};
fetchMetal(
MetadataType.Account,
signerAccount.address,
signerAccount.address,
undefined,
key,
).then(({ payload, text, metalId }) => {
console.log(`Fetched! metalId=${metalId}`);
let fileName = `${metalId}.out`;
if (text) {
try {
const seal = MetalSeal.parse(text);
const contentType = seal.mimeType ?? "application/octet-stream";
fileName = seal.name || `${metalId}.${mime.getExtension(contentType) ?? "out"}`;
console.debug(`Decoded Metal Seal: schema=${seal.schema},length=${seal.length},mimeType=${seal.mimeType},` +
`name=${seal.name},comment=${seal.comment}`);
} catch (e) {}
}
fs.writeFileSync(fileName, payload);
console.log(`Saved Payload File: ${fileName}`);
console.log(`Text Section: ${text}`);
}).catch((e) => {
console.error(e);
process.exit(1);
});