1
1
import { expect } from "chai" ;
2
- import { Address , PrivateKey , ViewKey , Signature , RecordCiphertext , RecordPlaintext , PrivateKeyCiphertext } from "../src/node.js" ;
2
+ import { Address , Field , PrivateKey , ViewKey , Signature , RecordCiphertext , RecordPlaintext , PrivateKeyCiphertext , EncryptionToolkit , Transition } from "../src/node.js" ;
3
3
import {
4
4
seed ,
5
5
message ,
@@ -351,6 +351,22 @@ describe('WASM Objects', () => {
351
351
// Ensure the record ciphertext cannot be decrypted with a foreign view key
352
352
expect ( ( ) => ciphertext . decrypt ( foreignViewKey ) ) . throw ( ) ;
353
353
} ) ;
354
+
355
+ it ( 'can be decrypted with a valid record view key' , ( ) => {
356
+ const recordViewKey = ciphertext . recordViewKey ( viewKey ) ;
357
+ const plaintext = ciphertext . decryptWithRecordViewKey ( recordViewKey ) ;
358
+ const isOwner = ciphertext . isOwner ( viewKey ) ;
359
+
360
+ // Ensure the record ciphertext is decrypted correctly
361
+ expect ( plaintext . toString ( ) ) . equal ( recordPlaintextString ) ;
362
+ expect ( isOwner ) . equal ( true ) ;
363
+ } )
364
+
365
+ it ( 'cannot be decrypted with an invalid record view key' , ( ) => {
366
+ const badRecordViewKey = ciphertext . recordViewKey ( ViewKey . from_string ( foreignViewKeyString ) ) ;
367
+ // Ensure the record ciphertext cannot be decrypted with an invalid record view key
368
+ expect ( ( ) => ciphertext . decryptWithRecordViewKey ( badRecordViewKey ) ) . throw ( ) ;
369
+ } )
354
370
} ) ;
355
371
356
372
describe ( 'RecordPlaintext' , ( ) => {
@@ -364,5 +380,63 @@ describe('WASM Objects', () => {
364
380
} ) ;
365
381
} ) ;
366
382
383
+ describe ( 'Transition' , ( ) => {
384
+ const transitionString = `{"id":"au1u62jasyx78x9hktak24awyj38fz73aseq8g9cx98u8egd9pj9uxq3u6s2z","program":"hello_hello.aleo","function":"hello","inputs":[{"type":"public","id":"3748790614260807060977840590007893602934308327222309419419577452790958781330field","value":"1u32"},{"type":"private","id":"5954208307642819953251922459490586292095132973876550778604572231610245257004field","value":"ciphertext1qyq0m5mp0d2gzh2pv9p25z70gz2avhqdt3dp8y8thzwf3aq6g35zcqcuyptz3"}],"outputs":[{"type":"private","id":"1557506318887190915592751299113729867877933642317637206076176689093854281418field","value":"ciphertext1qyqzmhw8ln9r6uuyh0n5jrsqlt25wdggqp3d9yqyttpr3g7g00k2sysdf9rmv"}],"tpk":"7532444547840484531569841377269810017844130178606467837628364672670182422388group","tcm":"7292056195970541935877520517416922164990366931599720071937561392936678536563field","scm":"8283770351301010771186520129040704279224805960417079922462917369178354050332field"}` ;
385
+ const transition = Transition . fromString ( transitionString ) ;
386
+ const transitionDecryptedString = `{"id":"au1mhdz6jqm973v5vfkz2pwgv63p340c9tpvydxha2zs8w03746qcpqvx3yye","program":"hello_hello.aleo","function":"hello","inputs":[{"type":"public","id":"3748790614260807060977840590007893602934308327222309419419577452790958781330field","value":"1u32"},{"type":"public","id":"5954208307642819953251922459490586292095132973876550778604572231610245257004field","value":"2u32"}],"outputs":[{"type":"public","id":"1557506318887190915592751299113729867877933642317637206076176689093854281418field","value":"3u32"}],"tpk":"7532444547840484531569841377269810017844130178606467837628364672670182422388group","tcm":"7292056195970541935877520517416922164990366931599720071937561392936678536563field","scm":"8283770351301010771186520129040704279224805960417079922462917369178354050332field"}`
387
+ const transitionDecrypted = Transition . fromString ( transitionDecryptedString ) ;
388
+ const invalidTransitionViewKeyString = "5089075468761042335883809641276568724119791331127957254389204093712358605127field"
389
+ const invalidTransitionViewKey = Field . fromString ( invalidTransitionViewKeyString ) ;
390
+ const privateKey = PrivateKey . from_string ( "APrivateKey1zkp8CZNn3yeCseEtxuVPbDCwSyhGW6yZKUYKfgXmcpoGPWH" ) ;
391
+ const viewKey = privateKey . to_view_key ( ) ;
392
+ const tvk = transition . tvk ( viewKey ) ;
393
+
394
+ it ( 'can be decrypted with a valid transition view key' , ( ) => {
395
+ const transitionDecryptedWithTVK = transition . decryptTransition ( tvk ) ;
396
+ // Ensure the transition is valid
397
+ expect ( transitionDecryptedWithTVK . toString ( ) ) . equal ( transitionDecrypted . toString ( ) ) ;
398
+ } ) ;
399
+
400
+ it ( 'cannot be decrypted with an invalid transition view key' , ( ) => {
401
+ expect ( ( ) => transition . decryptTransition ( invalidTransitionViewKey ) . toThrow ( ) ) ;
402
+ } ) ;
367
403
368
- } ) ;
404
+ it ( 'can generate a transition view key from a valid view key' , ( ) => {
405
+ const generatedTransitionViewKey = transition . tvk ( viewKey ) ;
406
+
407
+ const generatedTransitionViewKeyFromEncryptionToolkit = EncryptionToolkit . generateTvk ( viewKey , transition . tpk ( ) ) ;
408
+ // Ensure the generated transition view key is the same as the one used to decrypt
409
+ expect ( generatedTransitionViewKey . toString ( ) ) . equal ( generatedTransitionViewKeyFromEncryptionToolkit . toString ( ) ) ;
410
+ } ) ;
411
+ } ) ;
412
+
413
+ describe ( 'EncryptionToolkit' , ( ) => {
414
+ const recordCiphertextString = "record1qyqsqpe2szk2wwwq56akkwx586hkndl3r8vzdwve32lm7elvphh37rsyqyxx66trwfhkxun9v35hguerqqpqzqrtjzeu6vah9x2me2exkgege824sd8x2379scspmrmtvczs0d93qttl7y92ga0k0rsexu409hu3vlehe3yxjhmey3frh2z5pxm5cmxsv4un97q" ;
415
+ const recordCiphertext = RecordCiphertext . fromString ( recordCiphertextString ) ;
416
+ const recordPlaintextString = `{
417
+ owner: aleo1j7qxyunfldj2lp8hsvy7mw5k8zaqgjfyr72x2gh3x4ewgae8v5gscf5jh3.private,
418
+ microcredits: 1500000000000000u64.private,
419
+ _nonce: 3077450429259593211617823051143573281856129402760267155982965992208217472983group.public
420
+ }` ;
421
+ const recordPlaintext = RecordPlaintext . fromString ( recordPlaintextString ) ;
422
+ const viewKeyString = "AViewKey1ccEt8A2Ryva5rxnKcAbn7wgTaTsb79tzkKHFpeKsm9NX" ;
423
+ const viewKey = ViewKey . from_string ( viewKeyString ) ;
424
+ const recordViewKeyString = "4445718830394614891114647247073357094867447866913203502139893824059966201724field" ;
425
+ const recordViewKey = Field . fromString ( recordViewKeyString ) ;
426
+
427
+ it ( 'can generate a record view key from a view key and a record ciphertext' , ( ) => {
428
+ const generatedRecordViewKey = EncryptionToolkit . generateRecordViewKey ( viewKey , recordCiphertext ) ;
429
+ // Ensure the generated record view key is the same as the one used to decrypt
430
+ expect ( generatedRecordViewKey . toString ( ) ) . equal ( recordViewKey . toString ( ) ) ;
431
+ } ) ;
432
+ it ( 'can decrypt a record ciphertext with the record view key' , ( ) => {
433
+ const decryptedRecord = EncryptionToolkit . decryptRecordWithRVk ( recordViewKey , recordCiphertext ) ;
434
+ // Ensure the decrypted record is the same as the plaintext
435
+ expect ( decryptedRecord . toString ( ) ) . equal ( recordPlaintext . toString ( ) ) ;
436
+ } ) ;
437
+ it ( 'cannot decrypt a record ciphertext with an invalid record view key' , ( ) => {
438
+ const invalidRecordViewKey = Field . fromString ( "4445718830394614891114647247073357114867447866913203502139893824059966201724field" ) ;
439
+ expect ( ( ) => EncryptionToolkit . decryptRecordWithRVk ( invalidRecordViewKey , recordCiphertext ) ) . to . throw ( ) ;
440
+ } ) ;
441
+ } ) ;
442
+ } ) ;
0 commit comments