import sodium from "libsodium-wrappers" ; import { v4 as uuidv4 } from "uuid" ; import { UpdatePublicData } from "./types" ; import { createUpdate , verifyAndDecryptUpdate } from "./update" ; test ( "createUpdate & verifyAndDecryptUpdate successfully" , async ( ) => { await sodium . ready ; const key = sodium . from_hex ( "724b092810ec86d7e35c9d067702b31ef90bc43a7b598626749914d6a3e033ed" ) ; const signatureKeyPair = { privateKey : sodium . from_base64 ( "g3dtwb9XzhSzZGkxTfg11t1KEIb4D8rO7K54R6dnxArvgg_OzZ2GgREtG7F5LvNp3MS8p9vsio4r6Mq7SZDEgw" ) , publicKey : sodium . from_base64 ( "74IPzs2dhoERLRuxeS7zadzEvKfb7IqOK-jKu0mQxIM" ) , keyType : "ed25519" , } ; const publicData = { refSnapshotId : uuidv4 ( ) , docId : "6e46c006-5541-11ec-bf63-0242ac130002" , pubKey : sodium . to_base64 ( signatureKeyPair . publicKey ) , } ; const update = createUpdate ( "Hello World" , publicData , key , signatureKeyPair ) ; const result = verifyAndDecryptUpdate ( update , key , signatureKeyPair . publicKey ) ; expect ( new TextDecoder ( ) . decode ( result ) ) . toBe ( "Hello World" ) ; } ) ; test ( "createUpdate & verifyAndDecryptUpdate break due changed signature" , async ( ) => { await sodium . ready ; const key = sodium . from_hex ( "724b092810ec86d7e35c9d067702b31ef90bc43a7b598626749914d6a3e033ed" ) ; const signatureKeyPair = { privateKey : sodium . from_base64 ( "g3dtwb9XzhSzZGkxTfg11t1KEIb4D8rO7K54R6dnxArvgg_OzZ2GgREtG7F5LvNp3MS8p9vsio4r6Mq7SZDEgw" ) , publicKey : sodium . from_base64 ( "74IPzs2dhoERLRuxeS7zadzEvKfb7IqOK-jKu0mQxIM" ) , keyType : "ed25519" , } ; const publicData = { refSnapshotId : uuidv4 ( ) , docId : "6e46c006-5541-11ec-bf63-0242ac130002" , pubKey : sodium . to_base64 ( signatureKeyPair . publicKey ) , } ; const update = createUpdate ( "Hello World" , publicData , key , signatureKeyPair ) ; const result = verifyAndDecryptUpdate ( { ... update , signature : update . signature . replace ( /^./ , "a" ) , } , key , signatureKeyPair . publicKey ) ; expect ( result ) . toBeNull ( ) ; } ) ; test ( "createUpdate & verifyAndDecryptUpdate break due changed ciphertext" , async ( ) => { await sodium . ready ; const key = sodium . from_hex ( "724b092810ec86d7e35c9d067702b31ef90bc43a7b598626749914d6a3e033ed" ) ; const signatureKeyPair = { privateKey : sodium . from_base64 ( "g3dtwb9XzhSzZGkxTfg11t1KEIb4D8rO7K54R6dnxArvgg_OzZ2GgREtG7F5LvNp3MS8p9vsio4r6Mq7SZDEgw" ) , publicKey : sodium . from_base64 ( "74IPzs2dhoERLRuxeS7zadzEvKfb7IqOK-jKu0mQxIM" ) , keyType : "ed25519" , } ; const publicData = { refSnapshotId : uuidv4 ( ) , docId : "6e46c006-5541-11ec-bf63-0242ac130002" , pubKey : sodium . to_base64 ( signatureKeyPair . publicKey ) , } ; const update = createUpdate ( "Hello World" , publicData , key , signatureKeyPair ) ; const result = verifyAndDecryptUpdate ( { ... update , ciphertext : update . ciphertext . replace ( /^./ , "a" ) , } , key , signatureKeyPair . publicKey ) ; expect ( result ) . toBeNull ( ) ; } ) ;