diff --git a/script-meta-v2.json b/script-meta-v2.json index b2d40c4..fee452a 100644 --- a/script-meta-v2.json +++ b/script-meta-v2.json @@ -20,8 +20,8 @@ }, "publickey.ts": { "script_name": "publickey.ts", - "script_length": 1148, - "script_sha256": "6be353c25d28e88e973c91f92d93728a7216dd08845fe76a7456121b5c3374a1", + "script_length": 1530, + "script_sha256": "ffd1181afb7bbc9cf0f45c2617dc956d4d363fd893456ad0ebc03fc63bab64ad", "script_full_url": "https://git.hatter.ink/hatter/ts-scripts/raw/branch/main/single-scripts/publickey.ts", "single_script_file": true }, diff --git a/single-scripts/publickey.ts b/single-scripts/publickey.ts index 2daa15e..9717821 100755 --- a/single-scripts/publickey.ts +++ b/single-scripts/publickey.ts @@ -8,7 +8,7 @@ const {X509Certificate, createPublicKey} = await import('node:crypto'); if (Deno.args.length === 0) { - console.log("Usage: certificate.ts "); + console.log("Usage: publickey.ts "); Deno.exit(1); } @@ -21,15 +21,31 @@ if (Deno.args.length === 0) { // console.log(x509.publicKey.export({format: "pem", type: "spki"})); // } -try { - const publicKey = createPublicKey(Deno.args[0]); - +function printPublicKey(publicKey) { console.log('JWK:'); const exportedJwk = publicKey.export({format: "jwk"}); console.log(JSON.stringify(exportedJwk, null, 4)); console.log(); console.log("PEM:"); console.log(publicKey.export({format: "pem", type: "spki"})); -} catch (e) { - console.error(`Parse certificate or public key failed: ${e}`); +} + +const firstArgument = Deno.args[0]; +try { + const publicKey = createPublicKey(firstArgument); + printPublicKey(publicKey); + Deno.exit(0); +} catch (e) { + // try jwk + try { + const key = JSON.parse(firstArgument); + const publicKey = createPublicKey({key, format: "jwk"}); + printPublicKey(publicKey); + Deno.exit(0); + } catch (e) { + // IGNORE + } + + console.error(`Parse certificate or public key failed: ${e}`); + Deno.exit(1); }