UNPKG

@ndn/packet

Version:

NDNts: Network Layer Packets

111 lines (110 loc) 3.89 kB
import { Encoder, EvDecoder, Extensible, ExtensionRegistry, NNI } from "@ndn/tlv"; import { assert } from "@ndn/util"; import { SigType, TT } from "./an_browser.js"; import { KeyLocator } from "./key-locator_browser.js"; import { ValidityPeriod } from "./validity-period_browser.js"; const EXTENSIONS = new ExtensionRegistry(); const EVD = new EvDecoder("SigInfo", [TT.ISigInfo, TT.DSigInfo]) .add(TT.SigType, (t, { nni }) => t.type = nni, { required: true }) .add(TT.KeyLocator, (t, { decoder }) => t.keyLocator = decoder.decode(KeyLocator)) .add(TT.SigNonce, (t, { value }) => t.nonce = value) .add(TT.SigTime, (t, { nni }) => t.time = nni) .add(TT.SigSeqNum, (t, { nniBig }) => t.seqNum = nniBig) .add(TT.ValidityPeriod, (t, { decoder }) => t.validity = decoder.decode(ValidityPeriod)) .setUnknown(EXTENSIONS.decodeUnknown); /** SignatureInfo on Interest or Data. */ export class SigInfo { static decodeFrom(decoder) { return EVD.decode(new SigInfo(), decoder); } /** * Construct from flexible arguments. * * Arguments can include, in any order: * - {@link SigInfo} to copy from * - number as SigType * - {@link KeyLocator}, or Name/URI/KeyDigest to construct KeyLocator * - {@link SigInfo.Nonce}`(v)` * - {@link SigInfo.Time}`(v)` * - {@link SigInfo.SeqNum}`(v)` * - {@link ValidityPeriod} */ constructor(...args) { const klArgs = []; for (const arg of args) { if (typeof arg === "number") { this.type = arg; } else if (KeyLocator.isCtorArg(arg)) { klArgs.push(arg); } else if (arg instanceof SigInfo) { Object.assign(this, arg); Extensible.cloneRecord(this, arg); } else if (arg instanceof ValidityPeriod) { this.validity = arg; } else if (arg[ctorAssign]) { arg[ctorAssign](this); } else { throw new Error("unknown SigInfo constructor argument"); } } if (klArgs.length > 0) { this.keyLocator = new KeyLocator(...klArgs); } } type = SigType.Null; keyLocator; nonce; time; seqNum; validity; [Extensible.TAG] = EXTENSIONS; /** * Create an Encodable. * @param tt - Either `TT.ISigInfo` or `TT.DSigInfo`. */ encodeAs(tt) { return { encodeTo: (encoder) => this.encodeTo(encoder, tt), }; } encodeTo(encoder, tt) { encoder.prependTlv(tt, [TT.SigType, NNI(this.type)], this.keyLocator, [TT.SigNonce, Encoder.OmitEmpty, this.nonce], this.time !== undefined && [TT.SigTime, NNI(this.time)], this.seqNum !== undefined && [TT.SigSeqNum, NNI(this.seqNum)], this.validity, ...EXTENSIONS.encode(this)); } } const ctorAssign = Symbol("@ndn/packet#SigInfo.ctorAssign"); (function (SigInfo) { /** Constructor argument to set SigNonce field. */ function Nonce(v) { return { [ctorAssign](si) { si.nonce = v instanceof Uint8Array ? v : generateNonce(v); }, }; } SigInfo.Nonce = Nonce; /** Generate a random nonce. */ function generateNonce(size = 8) { assert(size >= 1); return crypto.getRandomValues(new Uint8Array(size)); } SigInfo.generateNonce = generateNonce; /** Constructor argument to set SigTime field. */ function Time(v = Date.now()) { return { [ctorAssign](si) { si.time = v; }, }; } SigInfo.Time = Time; /** Constructor argument to set SigSeqNum field. */ function SeqNum(v) { return { [ctorAssign](si) { si.seqNum = v; }, }; } SigInfo.SeqNum = SeqNum; })(SigInfo || (SigInfo = {}));