@ndn/packet
Version:
NDNts: Network Layer Packets
53 lines (52 loc) • 1.7 kB
JavaScript
import { Encoder, EvDecoder } from "@ndn/tlv";
import { TT } from "./an_node.js";
import { Name } from "./name/mod_node.js";
const EVD = new EvDecoder("KeyLocator", TT.KeyLocator)
.add(TT.Name, (t, { value }) => t.name = new Name(value))
.add(TT.KeyDigest, (t, { value }) => t.digest = value);
/** KeyLocator in SigInfo. */
export class KeyLocator {
static decodeFrom(decoder) {
return EVD.decode(new KeyLocator(), decoder);
}
constructor(...args) {
for (const arg of args) {
if (Name.isNameLike(arg)) {
this.name = Name.from(arg);
}
else if (arg instanceof Uint8Array) {
this.digest = arg;
}
else if (arg instanceof KeyLocator) {
Object.assign(this, arg);
}
else {
throw new Error("unknown KeyLocator constructor argument");
}
}
}
name;
digest;
encodeTo(encoder) {
encoder.prependTlv(TT.KeyLocator, Encoder.OmitEmpty, this.name, [TT.KeyDigest, Encoder.OmitEmpty, this.digest]);
}
}
(function (KeyLocator) {
function isCtorArg(arg) {
return arg instanceof KeyLocator || Name.isNameLike(arg) || arg instanceof Uint8Array;
}
KeyLocator.isCtorArg = isCtorArg;
/**
* Extract KeyLocator name.
* @throws Error
* Thrown if KeyLocator is missing or does not have Name.
*/
function mustGetName(kl) {
const name = kl?.name;
if (!name) {
throw new Error("KeyLocator does not have name");
}
return name;
}
KeyLocator.mustGetName = mustGetName;
})(KeyLocator || (KeyLocator = {}));