@@ -17,6 +17,7 @@ import {
17
17
SessionKeyPair ,
18
18
SigningAccessControlConditionJWTPayload ,
19
19
SigShare ,
20
+ WalletEncryptedPayload ,
20
21
} from '@lit-protocol/types' ;
21
22
import {
22
23
uint8arrayFromString ,
@@ -372,6 +373,103 @@ async function getAmdCert(url: string): Promise<Uint8Array> {
372
373
}
373
374
}
374
375
376
+ export const walletEncrypt = async (
377
+ myWalletSecretKey : Uint8Array ,
378
+ theirWalletPublicKey : Uint8Array ,
379
+ sessionSig : Uint8Array ,
380
+ message : Uint8Array
381
+ ) : Promise < WalletEncryptedPayload > => {
382
+ const random = new Uint8Array ( 16 ) ;
383
+ window . crypto . getRandomValues ( random ) ;
384
+ const dateNow = Date . now ( ) ;
385
+ const createdAt = Math . floor ( dateNow / 1000 ) ;
386
+ const timestamp = Buffer . alloc ( 8 ) ;
387
+ timestamp . writeBigUInt64BE ( BigInt ( createdAt ) , 0 ) ;
388
+
389
+ const myWalletPublicKey = new Uint8Array ( 32 ) ;
390
+ nacl . crypto_scalarmult_base ( myWalletPublicKey , myWalletSecretKey ) ;
391
+
392
+ // Construct AAD
393
+ const sessionSignature = Buffer . from ( sessionSig ) ; // Replace with actual session signature
394
+ const theirPublicKey = Buffer . from ( theirWalletPublicKey ) ; // Replace with their public key
395
+ const myPublicKey = Buffer . from ( myWalletPublicKey ) ; // Replace with your wallet public key
396
+
397
+ const aad = Buffer . concat ( [
398
+ sessionSignature ,
399
+ random ,
400
+ timestamp ,
401
+ theirPublicKey ,
402
+ myPublicKey ,
403
+ ] ) ;
404
+
405
+ const hash = new Uint8Array ( 64 ) ;
406
+ nacl . crypto_hash ( hash , aad ) ;
407
+
408
+ const nonce = hash . slice ( 0 , 24 ) ;
409
+ const ciphertext = nacl . box ( message , nonce , theirPublicKey , myWalletSecretKey ) ;
410
+ return {
411
+ V1 : {
412
+ verification_key : uint8ArrayToHex ( myWalletPublicKey ) ,
413
+ ciphertext_and_tag : uint8ArrayToHex ( ciphertext ) ,
414
+ session_signature : uint8ArrayToHex ( sessionSignature ) ,
415
+ random : uint8ArrayToHex ( random ) ,
416
+ created_at : dateNow . toISOString ( ) ,
417
+ }
418
+ } ;
419
+ }
420
+
421
+ export const walletDecrypt = async (
422
+ myWalletSecretKey : Uint8Array ,
423
+ payload : WalletEncryptedPayload
424
+ ) : Promise < Uint8Array > => {
425
+ const dateSent = new Date ( payload . V1 . created_at )
426
+ const createdAt = Math . floor ( dateSent / 1000 ) ;
427
+ const timestamp = Buffer . alloc ( 8 ) ;
428
+ timestamp . writeBigUInt64BE ( BigInt ( createdAt ) , 0 ) ;
429
+
430
+ const myWalletPublicKey = new Uint8Array ( 32 ) ;
431
+ nacl . crypto_scalarmult_base ( myWalletPublicKey , myWalletSecretKey ) ;
432
+
433
+ // Construct AAD
434
+ const random = Buffer . from ( hexToUint8Array ( payload . V1 . random ) ) ;
435
+ const sessionSignature = Buffer . from ( hexToUint8Array ( payload . V1 . session_signature ) ) ; // Replace with actual session signature
436
+ const theirPublicKey = hexToUint8Array ( payload . V1 . verification_key ) ;
437
+ const theirPublicKeyBuffer = Buffer . from ( theirPublicKey ) ; // Replace with their public key
438
+ const myPublicKey = Buffer . from ( myWalletPublicKey ) ; // Replace with your wallet public key
439
+
440
+ const aad = Buffer . concat ( [
441
+ sessionSignature ,
442
+ random ,
443
+ timestamp ,
444
+ theirPublicKeyBuffer ,
445
+ myPublicKey ,
446
+ ] ) ;
447
+
448
+ const hash = new Uint8Array ( 64 ) ;
449
+ nacl . crypto_hash ( hash , aad ) ;
450
+
451
+ const nonce = hash . slice ( 0 , 24 ) ;
452
+ const message = nacl . box . open ( payload . V1 . ciphertext_and_tag , nonce , theirPublicKey , myWalletSecretKey ) ;
453
+ return message ;
454
+ }
455
+
456
+ function uint8ArrayToHex ( array : Uint8Array ) {
457
+ return Array . from ( array )
458
+ . map ( byte => byte . toString ( 16 ) . padStart ( 2 , '0' ) )
459
+ . join ( '' ) ;
460
+ }
461
+
462
+ function hexToUint8Array ( hexString : string ) : Uint8Array {
463
+ if ( hexString . length % 2 !== 0 ) {
464
+ throw new Error ( "Hex string must have an even length" ) ;
465
+ }
466
+ const bytes = new Uint8Array ( hexString . length / 2 ) ;
467
+ for ( let i = 0 ; i < bytes . length ; i ++ ) {
468
+ bytes [ i ] = parseInt ( hexString . slice ( i * 2 , i * 2 + 2 ) , 16 ) ;
469
+ }
470
+ return bytes ;
471
+ }
472
+
375
473
/**
376
474
*
377
475
* Check the attestation against AMD certs
0 commit comments