Skip to content

Commit 0814a2c

Browse files
committed
feat: add optional file support for network commands
1 parent 7b2c426 commit 0814a2c

File tree

1 file changed

+25
-8
lines changed

1 file changed

+25
-8
lines changed

src/index.ts

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,19 @@ const getPrivateKeyFromFile = async (path: string): Promise<PrivateKey> => {
6161
}
6262
};
6363

64+
const getBytesFromFile = async (path: string): Promise<Uint8Array> => {
65+
const f = path.replace(/^file:\/\//, "");
66+
return await fs.readFile(f);
67+
};
68+
69+
const putBytesToFile = async (
70+
path: string,
71+
data: Uint8Array,
72+
): Promise<void> => {
73+
const f = path.replace(/^file:\/\//, "");
74+
return await fs.writeFile(f, data);
75+
};
76+
6477
const program = new Command();
6578
program.name("cli").description("appchain cli");
6679

@@ -309,13 +322,16 @@ const executeCommand = async (
309322
.command("networks")
310323
.description("networks commands");
311324
commandNetworks
312-
.command("register <identifier>")
313-
.description("register a network <parameters := payload>")
314-
.action(async (identifier: string) => {
325+
.command("register <identifier> [file://]")
326+
.description("register a network <parameters := file:// OR payload>")
327+
.action(async (identifier: string, file?: string) => {
315328
if (!ipfsNode) return callback(responses.IPFS_NOT_STARTED);
316-
if (!payload) return callback(responses.PAYLOAD_UNDEFINED);
317329

318-
const parametersCID = await ipfsNode.putBytes(payload);
330+
// get network parameters from file or payload
331+
const _payload = file ? await getBytesFromFile(file) : payload;
332+
if (!_payload) return callback(responses.PAYLOAD_UNDEFINED);
333+
334+
const parametersCID = await ipfsNode.putBytes(_payload);
319335

320336
const r = await txer(async () => {
321337
await networks.register(
@@ -330,9 +346,9 @@ const executeCommand = async (
330346
callback({ id, ...r }, debug);
331347
});
332348
commandNetworks
333-
.command("getNetwork <identifier>")
334-
.description("get network by identifier")
335-
.action(async (identifier: string) => {
349+
.command("getNetwork <identifier> [file://]")
350+
.description("get network by identifier; optionally save params to file")
351+
.action(async (identifier: string, file?: string) => {
336352
if (!ipfsNode) return callback(responses.IPFS_NOT_STARTED);
337353
const networkID = Network.getID(CircuitString.fromString(identifier));
338354
const network = (await client.query.runtime.Networks.networks.get(
@@ -342,6 +358,7 @@ const executeCommand = async (
342358

343359
const cid = network.parametersCID.toString();
344360
const parameters = await ipfsNode.getBytes(cid);
361+
if (file) await putBytesToFile(file, parameters);
345362

346363
const { parametersCID, ...rest } = Network.toObject(network);
347364
const data = {

0 commit comments

Comments
 (0)