aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/envshare/pkg/encoding.ts
diff options
context:
space:
mode:
author简律纯 <hsiangnianian@outlook.com>2023-04-18 03:02:17 +0800
committer简律纯 <hsiangnianian@outlook.com>2023-04-18 03:02:17 +0800
commit4919f028c884a041da7ff098abb02389b4eac598 (patch)
treeb0f482568c4b8c8a680ce6e2e70a7b7ca87dc190 /envshare/pkg/encoding.ts
parentb135aac8531c1e1488147ad8c6f98eddbdbe0c99 (diff)
downloadHydroRoll-4919f028c884a041da7ff098abb02389b4eac598.tar.gz
HydroRoll-4919f028c884a041da7ff098abb02389b4eac598.zip
✨add envshare docs
Diffstat (limited to 'envshare/pkg/encoding.ts')
-rw-r--r--envshare/pkg/encoding.ts31
1 files changed, 31 insertions, 0 deletions
diff --git a/envshare/pkg/encoding.ts b/envshare/pkg/encoding.ts
new file mode 100644
index 0000000..2025133
--- /dev/null
+++ b/envshare/pkg/encoding.ts
@@ -0,0 +1,31 @@
+import { fromBase58, toBase58 } from "../util/base58";
+import { ID_LENGTH, ENCRYPTION_KEY_LENGTH } from "./constants";
+/**
+ * To share links easily, we encode the id, where the data is stored in redis, together with the secret encryption key.
+ */
+export function encodeCompositeKey(version: number, id: string, encryptionKey: Uint8Array): string {
+ if (version < 0 || version > 255) {
+ throw new Error("Version must fit in a byte");
+ }
+ const compositeKey = new Uint8Array([version, ...fromBase58(id), ...encryptionKey]);
+
+ return toBase58(compositeKey);
+}
+
+/**
+ * To share links easily, we encode the id, where the data is stored in redis, together with the secret encryption key.
+ */
+export function decodeCompositeKey(compositeKey: string): { id: string; encryptionKey: Uint8Array; version: number } {
+ const decoded = fromBase58(compositeKey);
+ const version = decoded.at(0);
+
+ if (version === 1 || version === 2) {
+ return {
+ id: toBase58(decoded.slice(1, 1 + ID_LENGTH)),
+ encryptionKey: decoded.slice(1 + ID_LENGTH, 1 + ID_LENGTH + ENCRYPTION_KEY_LENGTH),
+ version,
+ };
+ }
+
+ throw new Error(`Unsupported composite key version: ${version}`);
+}