diff --git a/package.json b/package.json index b88a9c4..20da1b9 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "@ldclabs/cose-ts", "type": "module", - "version": "1.3.0", + "version": "1.3.1", "author": "0xZensh ", "description": "Implemented Keys, Algorithms (RFC9053), COSE (RFC9052) and CWT (RFC8392) in TypeScript.", "license": "MIT", @@ -40,6 +40,11 @@ "browser": "./dist/cwt.js", "default": "./dist/cwt.js" }, + "./ecdh": { + "types": "./dist/ecdh.d.ts", + "browser": "./dist/ecdh.js", + "default": "./dist/ecdh.js" + }, "./ecdsa": { "types": "./dist/ecdsa.d.ts", "browser": "./dist/ecdsa.js", diff --git a/src/key.test.ts b/src/key.test.ts index 6ab24eb..d62428a 100644 --- a/src/key.test.ts +++ b/src/key.test.ts @@ -2,15 +2,15 @@ // See the file LICENSE for licensing terms. import { assert, describe, it } from 'vitest' +import * as iana from './iana' +import { Key } from './key' import { bytesToHex, - hexToBytes, - utf8ToBytes, compareBytes, - encodeCBOR + encodeCBOR, + hexToBytes, + utf8ToBytes } from './utils' -import * as iana from './iana' -import { Key } from './key' describe('Key Examples', () => { it('Key', () => { @@ -42,6 +42,10 @@ describe('Key Examples', () => { const expected = 'a40104024c53796d6d6574726963313238030a2050231f4c4d4d3051fdc2ec0a3851d5b383' assert.equal(bytesToHex(key.toBytes()), expected) + assert.equal( + bytesToHex(key.getSecret()), + '231f4c4d4d3051fdc2ec0a3851d5b383' + ) const key2 = Key.fromBytes(hexToBytes(expected)) assert.equal(bytesToHex(key2.toBytes()), expected) @@ -61,6 +65,10 @@ describe('Key Examples', () => { const expected = 'a40104024c53796d6d65747269633235360304205820403697de87af64611c1d32a05dab0fe1fcb715a86ab435f1ec99192d79569388' assert.equal(bytesToHex(key.toBytes()), expected) + assert.equal( + bytesToHex(key.getSecret()), + '403697de87af64611c1d32a05dab0fe1fcb715a86ab435f1ec99192d79569388' + ) const key2 = Key.fromBytes(hexToBytes(expected)) assert.equal(bytesToHex(key2.toBytes()), expected) @@ -93,6 +101,10 @@ describe('Key Examples', () => { const expected = 'a7010202524173796d6d6574726963454344534132353603262001215820143329cce7868e416927599cf65a34f3ce2ffda55a7eca69ed8919a394d42f0f22582060f7f1a780d8a783bfb7a2dd6b2796e8128dbbcef9d3d168db9529971a36e7b92358206c1382765aec5358f117733d281c1c7bdc39884d04a45a1e6c67c858bc206c19' assert.equal(bytesToHex(key.toBytes()), expected) + assert.equal( + bytesToHex(key.getSecret()), + '6c1382765aec5358f117733d281c1c7bdc39884d04a45a1e6c67c858bc206c19' + ) const key2 = Key.fromBytes(hexToBytes(expected)) assert.equal(bytesToHex(key2.toBytes()), expected) diff --git a/src/key.ts b/src/key.ts index 729cdde..817acd3 100644 --- a/src/key.ts +++ b/src/key.ts @@ -101,4 +101,17 @@ export class Key extends KVMap { this.setParam(iana.KeyParameterKid, encodeCBOR(kid)) return this } + + getSecret(): Uint8Array { + switch (this.kty) { + case iana.KeyTypeOKP: + return this.getBytes(iana.OKPKeyParameterD, 'k') + case iana.KeyTypeEC2: + return this.getBytes(iana.EC2KeyParameterD, 'd') + case iana.KeyTypeSymmetric: + return this.getBytes(iana.SymmetricKeyParameterK, 'k') + default: + throw new Error('unsupported key type') + } + } }