MCPcopy Create free account
hub / github.com/WICG/webpackage / parseRemovalKey

Function parseRemovalKey

js/sign/src/cli-sign.ts:48–80  ·  view source on GitHub ↗
(keyInput: string)

Source from the content-addressed store, hash-verified

46
47// That key may be either provided by Bytes encoded in base64 or path to file with key. This function checks that and parses key.
48async function parseRemovalKey(keyInput: string): Promise<Uint8Array> {
49 if (keyInput.endsWith('.pem') || fs.existsSync(keyInput)) {
50 if (!fs.existsSync(keyInput)) {
51 throw new Error(`The key file "${keyInput}" does not exist.`);
52 }
53 const data = await fs.promises.readFile(keyInput);
54 try {
55 // Try parsing as private key (it handles encrypted ones too)
56 const privateKey = await parseMaybeEncryptedKey(data, keyInput);
57 return getRawPublicKey(privateKey);
58 } catch {
59 // Try parsing as public key
60 try {
61 const pubKeyObject = crypto.createPublicKey(data);
62 return getRawPublicKey(pubKeyObject);
63 } catch (err) {
64 throw new Error(
65 `Failed to parse key from file "${keyInput}". Ensure it is a valid PEM-encoded Ed25519 or ECDSA P-256 key.`,
66 { cause: err }
67 );
68 }
69 }
70 } else {
71 // Assume Base64 string
72 // Base64 regex check (simplified, but sufficient for raw keys)
73 if (!/^[A-Za-z0-9+/=]+$/.test(keyInput)) {
74 throw new Error(
75 `The provided key "${keyInput}" is neither a valid file path nor a proper Base64-encoded string.`
76 );
77 }
78 return new Uint8Array(Buffer.from(keyInput, 'base64'));
79 }
80}
81
82const program = new Command()
83 .name(name)

Callers 1

parseArgumentsFunction · 0.85

Calls 2

parseMaybeEncryptedKeyFunction · 0.90
getRawPublicKeyFunction · 0.90

Tested by

no test coverage detected