forked from ubiquity-os/plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add GH_PAT decryption mechanism
- Loading branch information
Showing
3 changed files
with
59 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import sodium from "libsodium-wrappers"; | ||
import { Logs } from "@ubiquity-dao/ubiquibot-logger"; | ||
|
||
const KEY_PREFIX = "HSK_"; | ||
|
||
export async function decryptKeys( | ||
cipherText: string, | ||
x25519PrivateKey: string, | ||
logger: Logs | ||
): Promise<{ privateKey: string; publicKey: string } | { privateKey: null; publicKey: null }> { | ||
await sodium.ready; | ||
|
||
let _public: null | string = null; | ||
let _private: null | string = null; | ||
|
||
_public = await getScalarKey(x25519PrivateKey); | ||
if (!_public) { | ||
logger.error("Public key is null"); | ||
return { privateKey: null, publicKey: null }; | ||
} | ||
if (!cipherText?.length) { | ||
logger.error("No cipherText was provided"); | ||
return { privateKey: null, publicKey: null }; | ||
} | ||
const binaryPublic = sodium.from_base64(_public, sodium.base64_variants.URLSAFE_NO_PADDING); | ||
const binaryPrivate = sodium.from_base64(x25519PrivateKey, sodium.base64_variants.URLSAFE_NO_PADDING); | ||
|
||
const binaryCipher = sodium.from_base64(cipherText, sodium.base64_variants.URLSAFE_NO_PADDING); | ||
|
||
const walletPrivateKey: string | null = sodium.crypto_box_seal_open(binaryCipher, binaryPublic, binaryPrivate, "text"); | ||
_private = walletPrivateKey?.replace(KEY_PREFIX, ""); | ||
|
||
return { privateKey: _private, publicKey: _public }; | ||
} | ||
|
||
async function getScalarKey(x25519PrivateKey: string) { | ||
await sodium.ready; | ||
const binPriv = sodium.from_base64(x25519PrivateKey, sodium.base64_variants.URLSAFE_NO_PADDING); | ||
return sodium.crypto_scalarmult_base(binPriv, "base64"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters