mirror of
https://github.com/0x5eal/wg-lua.git
synced 2024-12-12 12:50:36 +00:00
feat: basic error handling
Currently, this implements a simple runtime error which is raised when either the private or public key generation fail. In the future, it would make sense to include some runtime argument validation, and a more parseable error.
This commit is contained in:
parent
2a49e303f9
commit
6dff9bbdb8
1 changed files with 9 additions and 6 deletions
15
src/index.ts
15
src/index.ts
|
@ -16,16 +16,19 @@ export interface Wireguard {
|
|||
|
||||
export const wireguard: Wireguard = {
|
||||
generateKeypair: function () {
|
||||
const privateKey = generatePrivateKey();
|
||||
const publicKey = generatePublicKey(privateKey);
|
||||
|
||||
const [privateKeyOk, privateKey] = pcall<[], number[]>(() => generatePrivateKey());
|
||||
const [publicKeyOk, publicKey] = privateKeyOk
|
||||
? pcall<[], number[]>(() => generatePublicKey(privateKey))
|
||||
: error("failed to generate private key");
|
||||
return {
|
||||
publicKey: atob(publicKey),
|
||||
privateKey: atob(privateKey),
|
||||
publicKey: atob(publicKeyOk ? publicKey : error("failed to generate public key")),
|
||||
privateKey: atob(privateKey as number[]),
|
||||
};
|
||||
},
|
||||
|
||||
generatePublicKey: function (privateKey) {
|
||||
return atob(generatePublicKey(privateKey));
|
||||
const [publicKeyOk, publicKey] = pcall<[], number[]>(() => generatePublicKey(privateKey));
|
||||
|
||||
return atob(publicKeyOk ? publicKey : error("failed to generate public key"));
|
||||
},
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue