panva / paseto

PASETO (Platform-Agnostic SEcurity TOkens) for Node.js with no dependencies
MIT License
428 stars 26 forks source link

TypeError: keyObject must be a KeyObject instance #23

Closed seia-soto closed 2 years ago

seia-soto commented 2 years ago

What happened?

Tried to import existing key but type error occurs.

yarn run v1.22.10
$ ts-node index.ts
/Users/gohojeong/seia/tmp/a/node_modules/paseto/lib/v2/key.js:105
    throw new TypeError('keyObject must be a KeyObject instance')
          ^
TypeError: keyObject must be a KeyObject instance
    at _keyObjectToBytes (/Users/gohojeong/seia/tmp/a/node_modules/paseto/lib/v2/key.js:105:11)
    at Object.keyObjectToBytes (/Users/gohojeong/seia/tmp/a/node_modules/paseto/lib/v4/key.js:14:10)
    at loadKeyStore (/Users/gohojeong/seia/tmp/a/index.ts:11:24)
    at async main (/Users/gohojeong/seia/tmp/a/index.ts:26:3)
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

Version

^3.0.1

Node.js Version

v16.5.0 (arm64)

Code to reproduce

import type { KeyObject } from 'crypto'
import { V4 as paseto } from 'paseto'
import { promises as fs, existsSync } from 'fs'

const keyLocation = './key.cert'
let keyStore: KeyObject

const loadKeyStore = async () => {
  if (!existsSync(keyLocation)) {
    const key = await paseto.generateKey('public')
    const raw = paseto.keyObjectToBytes(keyStore).toString('hex')

    await fs.writeFile(keyLocation, raw, 'utf-8')

    keyStore = paseto.bytesToKeyObject(Buffer.from(raw, 'hex'))
  } else {
    const raw = await fs.readFile(keyLocation, 'utf-8')

    console.log(raw)

    keyStore = paseto.bytesToKeyObject(Buffer.from(raw, 'hex'))
  }
}

const main = async () => {
  await loadKeyStore()
}

main()

panva commented 2 years ago
-   const raw = paseto.keyObjectToBytes(keyStore).toString('hex')
+   const raw = paseto.keyObjectToBytes(key).toString('hex')
seia-soto commented 2 years ago

Sorry, I misused the library without checking things again to avoid meaningless time-consuming even sample code didn't work at the time. I hope you not to feel uncomfortable with this issue.

Thank you and take care.