Skip to content

Commit

Permalink
Add a configurable cloud storage key suffix
Browse files Browse the repository at this point in the history
  • Loading branch information
zkharit committed Oct 23, 2024
1 parent f7f666d commit 822cb69
Showing 1 changed file with 23 additions and 6 deletions.
29 changes: 23 additions & 6 deletions packages/telegram-stamper/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ declare global {
export type TTelegramStamperConfig = {
apiPublicKey: string;
apiPrivateKey: string;
cloudStorageKeySuffix?: string;
};

// Constants for default key names, note these should NOT be used directly, use the getPublicKeyCloudStorageKeyName() functions as they include the user passed key suffix
const PUBLIC_KEY_CLOUD_STORAGE_KEY = "TURNKEY_API_PUBLIC_KEY";
const PRIVATE_KEY_CLOUD_STORAGE_KEY = "TURNKEY_API_PRIVATE_KEY";

Expand All @@ -22,6 +24,7 @@ const PRIVATE_KEY_CLOUD_STORAGE_KEY = "TURNKEY_API_PRIVATE_KEY";
*/
export default class TelegramStamper {
stamper: ApiKeyStamper | undefined;
cloudStorageKeySuffix: string = "";

constructor(config?: TTelegramStamperConfig) {
// check to see if were in a telegram mini app context
Expand All @@ -34,6 +37,12 @@ export default class TelegramStamper {
apiPublicKey: config.apiPublicKey,
apiPrivateKey: config.apiPrivateKey,
});

// set the key cloud storage key suffix
if (config.cloudStorageKeySuffix) {
// add an underscore prior to the suffix so the key is readable
this.cloudStorageKeySuffix = "_" + config.cloudStorageKeySuffix;
}
}
}

Expand All @@ -46,7 +55,7 @@ export default class TelegramStamper {
// insert creds into telegram cloud storage
// insert public key
await window.Telegram.WebApp.CloudStorage.setItem(
PUBLIC_KEY_CLOUD_STORAGE_KEY,
this.getPublicKeyCloudStorageKeyName(),
this.stamper.apiPublicKey,
(err: any, stored: boolean) => {
if (err != null || !stored) {
Expand All @@ -61,16 +70,16 @@ export default class TelegramStamper {

// insert private key
await window.Telegram.WebApp.CloudStorage.setItem(
PRIVATE_KEY_CLOUD_STORAGE_KEY,
this.getPrivateKeyCloudStorageKeyName(),
this.stamper.apiPrivateKey,
(err: any, stored: boolean) => {
if (err != null || !stored) {
// remove public key from storage if private key could not be stored
try {
this.clearKey(PUBLIC_KEY_CLOUD_STORAGE_KEY);
this.clearKey(this.getPublicKeyCloudStorageKeyName());
} catch (err) {
console.log(
`Failed removing public key from Telegram Cloud Storage stored at: ${PUBLIC_KEY_CLOUD_STORAGE_KEY}`
`Failed removing public key from Telegram Cloud Storage stored at: ${this.getPublicKeyCloudStorageKeyName()}`
);
}
throw new TelegramStamperError(
Expand All @@ -88,7 +97,7 @@ export default class TelegramStamper {

// get public key
await window.Telegram.WebApp.CloudStorage.getItem(
PUBLIC_KEY_CLOUD_STORAGE_KEY,
this.getPublicKeyCloudStorageKeyName(),
(err: any, pubKey: string) => {
if (err != null || !pubKey) {
throw new TelegramStamperError(
Expand All @@ -104,7 +113,7 @@ export default class TelegramStamper {

// get private key
await window.Telegram.WebApp.CloudStorage.getItem(
PUBLIC_KEY_CLOUD_STORAGE_KEY,
this.getPrivateKeyCloudStorageKeyName(),
(err: any, privKey: string) => {
if (err != null || !privKey) {
throw new TelegramStamperError(
Expand Down Expand Up @@ -166,4 +175,12 @@ export default class TelegramStamper {
);
}
}

getPublicKeyCloudStorageKeyName() {
return PUBLIC_KEY_CLOUD_STORAGE_KEY + this.cloudStorageKeySuffix;
}

getPrivateKeyCloudStorageKeyName() {
return PRIVATE_KEY_CLOUD_STORAGE_KEY + this.cloudStorageKeySuffix;
}
}

0 comments on commit 822cb69

Please sign in to comment.