mirror of
https://github.com/streetwriters/notesnook.git
synced 2025-12-21 22:19:41 +01:00
global: add enable inbox setting to generate/discard inbox key pair (#8527)
Signed-off-by: 01zulfi <85733202+01zulfi@users.noreply.github.com>
This commit is contained in:
@@ -118,6 +118,9 @@ export class Sodium implements ISodium {
|
||||
get crypto_secretstream_xchacha20poly1305_TAG_MESSAGE() {
|
||||
return sodium.crypto_secretstream_xchacha20poly1305_TAG_MESSAGE;
|
||||
}
|
||||
get crypto_box_keypair() {
|
||||
return sodium.crypto_box_keypair;
|
||||
}
|
||||
}
|
||||
|
||||
function convertVariant(variant: base64_variants): sodium.base64_variants {
|
||||
|
||||
@@ -46,14 +46,29 @@ import {
|
||||
crypto_aead_xchacha20poly1305_ietf_KEYBYTES,
|
||||
crypto_aead_xchacha20poly1305_ietf_NPUBBYTES,
|
||||
crypto_secretstream_xchacha20poly1305_TAG_FINAL,
|
||||
crypto_secretstream_xchacha20poly1305_TAG_MESSAGE
|
||||
crypto_secretstream_xchacha20poly1305_TAG_MESSAGE,
|
||||
crypto_box_keypair as sodium_native_crypto_box_keypair,
|
||||
crypto_box_PUBLICKEYBYTES,
|
||||
crypto_box_SECRETKEYBYTES
|
||||
} from "sodium-native";
|
||||
import { Buffer } from "node:buffer";
|
||||
import { base64_variants, ISodium } from "./types";
|
||||
|
||||
export type Uint8ArrayOutputFormat = "uint8array";
|
||||
export type StringOutputFormat = "text" | "hex" | "base64";
|
||||
export type KeyType = "curve25519" | "ed25519" | "x25519";
|
||||
export type StateAddress = { name: string };
|
||||
export interface KeyPair {
|
||||
keyType: KeyType;
|
||||
privateKey: Uint8Array;
|
||||
publicKey: Uint8Array;
|
||||
}
|
||||
|
||||
export interface StringKeyPair {
|
||||
keyType: KeyType;
|
||||
privateKey: string;
|
||||
publicKey: string;
|
||||
}
|
||||
export interface MessageTag {
|
||||
message: Uint8Array;
|
||||
tag: number;
|
||||
@@ -329,6 +344,39 @@ function crypto_secretstream_xchacha20poly1305_pull(
|
||||
return { message, tag: tag.readUInt8() } as MessageTag | StringMessageTag;
|
||||
}
|
||||
|
||||
function crypto_box_keypair(
|
||||
outputFormat?: Uint8ArrayOutputFormat | null
|
||||
): KeyPair;
|
||||
function crypto_box_keypair(outputFormat: StringOutputFormat): StringKeyPair;
|
||||
function crypto_box_keypair(
|
||||
outputFormat?: Uint8ArrayOutputFormat | null | StringOutputFormat
|
||||
): KeyPair | StringKeyPair {
|
||||
const publicBuffer = Buffer.alloc(crypto_box_PUBLICKEYBYTES);
|
||||
const privateBuffer = Buffer.alloc(crypto_box_SECRETKEYBYTES);
|
||||
|
||||
sodium_native_crypto_box_keypair(publicBuffer, privateBuffer);
|
||||
|
||||
if (typeof outputFormat === "string") {
|
||||
const transformer =
|
||||
outputFormat === "base64"
|
||||
? to_base64
|
||||
: outputFormat === "hex"
|
||||
? to_hex
|
||||
: to_string;
|
||||
return {
|
||||
keyType: "x25519" as KeyType,
|
||||
publicKey: transformer(new Uint8Array(publicBuffer)),
|
||||
privateKey: transformer(new Uint8Array(privateBuffer))
|
||||
};
|
||||
} else {
|
||||
return {
|
||||
keyType: "x25519" as KeyType,
|
||||
publicKey: new Uint8Array(publicBuffer),
|
||||
privateKey: new Uint8Array(privateBuffer)
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
function randombytes_buf(
|
||||
length: number,
|
||||
outputFormat?: Uint8ArrayOutputFormat | null
|
||||
@@ -391,6 +439,10 @@ function to_string(input: Uint8Array): string {
|
||||
);
|
||||
}
|
||||
|
||||
function to_hex(input: Uint8Array): string {
|
||||
return Buffer.from(input, input.byteOffset, input.byteLength).toString("hex");
|
||||
}
|
||||
|
||||
type ToBufferInput = string | Uint8Array | null | undefined;
|
||||
type ToBufferResult<TInput extends ToBufferInput> = TInput extends
|
||||
| undefined
|
||||
@@ -539,6 +591,9 @@ export class Sodium implements ISodium {
|
||||
get crypto_secretstream_xchacha20poly1305_TAG_MESSAGE() {
|
||||
return crypto_secretstream_xchacha20poly1305_TAG_MESSAGE;
|
||||
}
|
||||
get crypto_box_keypair() {
|
||||
return crypto_box_keypair;
|
||||
}
|
||||
}
|
||||
|
||||
export { base64_variants, type ISodium };
|
||||
|
||||
@@ -17,7 +17,7 @@ You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
import type sodium from "libsodium-wrappers-sumo";
|
||||
import sodium from "libsodium-wrappers-sumo";
|
||||
|
||||
export enum base64_variants {
|
||||
ORIGINAL = 1,
|
||||
@@ -61,4 +61,5 @@ export interface ISodium {
|
||||
get crypto_aead_xchacha20poly1305_ietf_NPUBBYTES(): typeof sodium.crypto_aead_xchacha20poly1305_ietf_NPUBBYTES;
|
||||
get crypto_secretstream_xchacha20poly1305_TAG_FINAL(): typeof sodium.crypto_secretstream_xchacha20poly1305_TAG_FINAL;
|
||||
get crypto_secretstream_xchacha20poly1305_TAG_MESSAGE(): typeof sodium.crypto_secretstream_xchacha20poly1305_TAG_MESSAGE;
|
||||
get crypto_box_keypair(): typeof sodium.crypto_box_keypair;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user