// class for COSE_KEY structure import { cborBackend } from "cbor-rpc"; export enum AlgorithmId { EDSA = -8, } export enum KeyType { OKP = 1, } export class CoseKey { private keyMap: Map; constructor(keyType: KeyType, algorithmId: AlgorithmId) { this.keyMap = new Map(); this.keyMap.set(1, keyType); this.keyMap.set(3, algorithmId); } /** * Generic_Headers = ( * ? 1 => int / tstr, ; algorithm identifier * ? 2 => [+label], ; criticality * ? 3 => tstr / int, ; content type * ? 4 => bstr, ; key identifier * ? 5 => bstr, ; IV * ? 6 => bstr, ; Partial IV * ? 7 => COSE_Signature / [+COSE_Signature] ; Counter signature * ) */ // Getter for algorithmId that accesses keyMap[3] get algorithmId(): AlgorithmId { return this.keyMap.get(3); } // Setter for algorithmId that updates keyMap[3] set algorithmId(value: AlgorithmId) { this.keyMap.set(3, value); } get keyType(): KeyType { return this.keyMap.get(1); } set keyType(keyType: KeyType) { this.keyMap.set(1, keyType); } public get pubKeyBytes(): Buffer { return this.keyMap.get(-2); } public get addressBytes(): Buffer|undefined{ return this.keyMap.get(2) } public addHeader(key: any, value: any): CoseKey { this.keyMap.set(key, value); return this; } toBytes(): Uint8Array { return cborBackend.encode(this.keyMap); } public static fromBytes(bytes: Buffer): CoseKey { const result = new CoseKey(KeyType.OKP,AlgorithmId.EDSA); result.keyMap = cborBackend.decode(bytes); if(result.algorithmId == null || result.algorithmId == undefined){ throw new Error("Algorithm Id(3) is missing in CoseKey bytes") } if(result.keyType == null || result.keyType == undefined){ throw new Error("Key Type(1) is missing in CoseKey bytes") } return result; } }