-
Notifications
You must be signed in to change notification settings - Fork 8
/
util.ts
28 lines (24 loc) · 941 Bytes
/
util.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
import { openKeyChain } from "@ndn/cli-common";
import { Certificate, KeyChain } from "@ndn/keychain";
import { Data } from "@ndn/packet";
import { Decodable, Decoder, Encoder } from "@ndn/tlv";
import fastChunkString from "fast-chunk-string";
import getStdin from "get-stdin";
import stdout from "stdout-stream";
export const keyChain: KeyChain = openKeyChain();
export async function inputBase64<R>(d: Decodable<R>): Promise<R> {
const wire = Buffer.from(await getStdin(), "base64");
return new Decoder(wire).decode(d);
}
export async function inputCertBase64(): Promise<Certificate> {
const data = await inputBase64(Data);
return Certificate.fromData(data);
}
export function printCertBase64(cert: Certificate) {
const wire = Encoder.encode(cert.data);
const b64 = Buffer.from(wire).toString("base64");
const lines = fastChunkString(b64, { size: 64 });
for (const line of lines) {
stdout.write(`${line}\n`);
}
}