Skip to content

Commit

Permalink
fix: fix package info for ECDH; add Key.getSecret method
Browse files Browse the repository at this point in the history
  • Loading branch information
zensh committed Aug 31, 2024
1 parent c7763a8 commit ead6f23
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 6 deletions.
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@ldclabs/cose-ts",
"type": "module",
"version": "1.3.0",
"version": "1.3.1",
"author": "0xZensh <[email protected]>",
"description": "Implemented Keys, Algorithms (RFC9053), COSE (RFC9052) and CWT (RFC8392) in TypeScript.",
"license": "MIT",
Expand Down Expand Up @@ -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",
Expand Down
22 changes: 17 additions & 5 deletions src/key.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
13 changes: 13 additions & 0 deletions src/key.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
}
}
}

0 comments on commit ead6f23

Please sign in to comment.