@ldclabs/cose-ts
Version:
Implemented Keys, Algorithms (RFC9053), COSE (RFC9052) and CWT (RFC8392) in TypeScript.
98 lines • 3.18 kB
JavaScript
// (c) 2023-present, LDC Labs. All rights reserved.
// See the file LICENSE for licensing terms.
import { Header } from './header';
import { assertEqual, decodeCBOR, encodeCBOR } from './utils';
// PartyInfo represents a PartyInfo object.
export class PartyInfo {
identity;
nonce;
other;
static fromCBORValue(value) {
const v = value;
assertEqual(Array.isArray(v), true, 'not an array');
assertEqual(v.length, 3, 'invalid length');
return new PartyInfo(v[0], v[1], v[2]);
}
constructor(identity = null, nonce = null, other = null) {
this.identity = identity;
this.nonce = nonce;
this.other = other;
}
toCBORValue() {
return [this.identity, this.nonce, this.other];
}
}
// SuppPubInfo represents a SuppPubInfo object.
export class SuppPubInfo {
keyDataLength; // bits
protected;
other;
static fromCBORValue(value) {
const v = value;
assertEqual(Array.isArray(v), true, 'not an array');
if (v.length === 2) {
return new SuppPubInfo(v[0], Header.fromBytes(v[1]));
}
else if (v.length === 3) {
return new SuppPubInfo(v[0], Header.fromBytes(v[1]), v[2]);
}
else {
throw new Error('invalid SuppPubInfo data length');
}
}
constructor(keyDataLength, protectedV, other = null) {
this.keyDataLength = keyDataLength;
this.protected = protectedV;
this.other = other;
}
toCBORValue() {
const val = [this.keyDataLength, this.protected.toBytes()];
if (this.other != null) {
val.push(this.other);
}
return val;
}
}
export class KDFContext {
algorithmID; // bits
partyUInfo;
partyVInfo;
suppPubInfo;
suppPrivInfo;
static fromBytes(data) {
if (data.length === 0) {
throw new Error('invalid KDFContext data length');
}
const v = decodeCBOR(data);
assertEqual(Array.isArray(v), true, 'not an array');
if (v.length === 4) {
return new KDFContext(v[0], PartyInfo.fromCBORValue(v[1]), PartyInfo.fromCBORValue(v[2]), SuppPubInfo.fromCBORValue(v[3]));
}
else if (v.length === 5) {
return new KDFContext(v[0], PartyInfo.fromCBORValue(v[1]), PartyInfo.fromCBORValue(v[2]), SuppPubInfo.fromCBORValue(v[3]), v[4]);
}
else {
throw new Error('invalid SuppPubInfo data length');
}
}
constructor(algorithmID, partyUInfo, partyVInfo, suppPubInfo, suppPrivInfo = null) {
this.algorithmID = algorithmID;
this.partyUInfo = partyUInfo;
this.partyVInfo = partyVInfo;
this.suppPubInfo = suppPubInfo;
this.suppPrivInfo = suppPrivInfo;
}
toBytes() {
const val = [
this.algorithmID,
this.partyUInfo.toCBORValue(),
this.partyVInfo.toCBORValue(),
this.suppPubInfo.toCBORValue()
];
if (this.suppPrivInfo != null) {
val.push(this.suppPrivInfo);
}
return encodeCBOR(val);
}
}
//# sourceMappingURL=kdfcontext.js.map