1
- import * as crypto from "node:crypto" ;
1
+ import { box , BoxKeyPair , randomBytes , } from "tweetnacl" ;
2
+ import { encodeBase64 , decodeBase64 } from "tweetnacl-util" ;
2
3
import * as path from "node:path" ;
3
4
import * as os from "node:os" ;
4
5
import { Buffer } from "node:buffer" ;
@@ -18,47 +19,37 @@ export async function getKeys(): Promise<{ publicKey: string; privateKey: string
18
19
}
19
20
20
21
function generateKeyPair ( ) {
21
- // Generate RSA key pair
22
- const { publicKey, privateKey } = crypto . generateKeyPairSync ( 'rsa' , {
23
- modulusLength : 4096 ,
24
- publicKeyEncoding : {
25
- type : 'spki' ,
26
- format : 'pem'
27
- } ,
28
- privateKeyEncoding : {
29
- type : 'pkcs8' ,
30
- format : 'pem'
31
- }
32
- } ) ;
22
+ // generate a key pair
23
+ const pair = box . keyPair ( ) ;
24
+ const publicKey = encodeBase64 ( pair . publicKey ) ;
25
+ const privateKey = encodeBase64 ( pair . secretKey ) ;
33
26
34
27
return {
35
28
publicKey,
36
29
privateKey,
37
30
} ;
38
31
}
39
32
40
- export function decryptSecret ( encrypted_secret : string , privateKey : string ) {
41
- try {
42
- const decoded = Buffer . from ( encrypted_secret , 'base64' ) ;
43
- const decrypted = crypto . privateDecrypt ( {
44
- key : privateKey ,
45
- padding : crypto . constants . RSA_PKCS1_PADDING ,
46
- } , decoded ) ;
47
-
48
- // Convert decrypted array to Buffer
49
- const decryptedBuffer = Buffer . isBuffer ( decrypted ) ? decrypted : Buffer . from ( decrypted ) ;
50
-
51
- return decryptedBuffer . toString ( 'utf8' ) ;
52
- } catch ( err ) {
53
- throw new Error ( `Failed to decrypt secret: ${ err } ` ) ;
33
+ export function decryptSecret ( props : { encrypted : string , secretKey : string , nonce : string , ephemeralKey : string } ) {
34
+ // Generate nonce and message from encrypted secret
35
+ const decrypted = box . open (
36
+ decodeBase64 ( props . encrypted ) ,
37
+ decodeBase64 ( props . nonce ) ,
38
+ decodeBase64 ( props . secretKey ) ,
39
+ decodeBase64 ( props . ephemeralKey )
40
+ ) ;
41
+
42
+ if ( ! decrypted ) {
43
+ throw new Error ( "Failed to decrypt secret" ) ;
54
44
}
45
+ return Buffer . from ( decrypted ) . toString ( 'utf8' ) ;
55
46
}
56
47
57
48
58
49
async function saveKeys ( keys : { publicKey : string ; privateKey : string } ) {
59
50
const { publicKey, privateKey } = keys ;
60
- const publicKeyPath = path . join ( os . homedir ( ) , ".sf" , "public.pem " ) ;
61
- const privateKeyPath = path . join ( os . homedir ( ) , ".sf" , "private.pem " ) ;
51
+ const publicKeyPath = path . join ( os . homedir ( ) , ".sf" , "public_key " ) ;
52
+ const privateKeyPath = path . join ( os . homedir ( ) , ".sf" , "private_key " ) ;
62
53
63
54
try {
64
55
// Create .sf directory if it doesn't exist
@@ -76,8 +67,8 @@ async function saveKeys(keys: { publicKey: string; privateKey: string }) {
76
67
}
77
68
78
69
async function loadKeys ( ) {
79
- const publicKeyPath = path . join ( os . homedir ( ) , ".sf" , "public.pem " ) ;
80
- const privateKeyPath = path . join ( os . homedir ( ) , ".sf" , "private.pem " ) ;
70
+ const publicKeyPath = path . join ( os . homedir ( ) , ".sf" , "public_key " ) ;
71
+ const privateKeyPath = path . join ( os . homedir ( ) , ".sf" , "private_key " ) ;
81
72
82
73
let publicKey = null ;
83
74
let privateKey = null ;
0 commit comments