1
1
import { expect } from "chai" ;
2
- import { Address , Field , PrivateKey , ViewKey , Signature , RecordCiphertext , RecordPlaintext , PrivateKeyCiphertext , EncryptionToolkit , Transition } from "../src/node.js" ;
2
+ import { Address , AleoNetworkClient , Field , PrivateKey , ViewKey , Signature , RecordCiphertext , RecordPlaintext , PrivateKeyCiphertext , EncryptionToolkit , Transition } from "../src/node.js" ;
3
3
import {
4
4
seed ,
5
5
message ,
@@ -383,34 +383,65 @@ describe('WASM Objects', () => {
383
383
} ) ;
384
384
385
385
describe ( 'Transition' , ( ) => {
386
- 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"}` ;
387
- const transition = Transition . fromString ( transitionString ) ;
388
- 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"}`
389
- const transitionDecrypted = Transition . fromString ( transitionDecryptedString ) ;
386
+ const transitionStringTestnet = `{"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"}` ;
387
+ const transitionTestnet = Transition . fromString ( transitionStringTestnet ) ;
388
+ const transitionDecryptedStringTestnet = `{"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"}`
389
+ const transitionDecryptedTestnet = Transition . fromString ( transitionDecryptedStringTestnet ) ;
390
+ const transitionViewKeyStringTestnet = "3975242887442171718863200089461896014344887434842278474302914755871123010247field" ;
391
+
392
+ const transitionStringMainnet = `{"id":"au1mguuz0dh20f78802m4z0py7n08xhl0pz60llzck63mhl8pc8l5xqxpwgtn","program":"hello_hello.aleo","function":"main","inputs":[{"type":"public","id":"6393584049543470937057043098611271993206122889317039351966319038535020834557field","value": "1u32"},{"type":"private","id":"8207446256045172951742235001162005156507562935942883128759030124682934277495field","value":"ciphertext1qyqqgz9qnupeld9vr4vuwp6yrpmhgtkvmgag5m7mmrruw0r6je666qgqdswk3"}],"outputs":[{"type":"private","id":"127469473292952941321346770257126666363371158501875622169294663492714835110field","value":"ciphertext1qyqyapkjuxm9dcslgyjf7hkr2k3dek500z40gjspnwvll0uawj23vzgggc405"}],"tpk":"7647553513996966044119163122930125808381703910407273818947266861843062002251group","tcm":"4479413938380109857414238205380483440836495997450846894155088299187217672609field","scm":"6461007226176477784737642021400489186736987671609840640950580467598882134642field"}` ;
393
+ const transitionMainnet = Transition . fromString ( transitionStringMainnet ) ;
394
+ const transitionDecryptedStringMainnet = `{"id":"au1jl2ur42sj7hwe4r0alv6gnklqxj0fszrvu3q82gjcls5x6q9pyzqdgmu2k","program":"hello_hello.aleo","function":"main","inputs":[{"type":"public","id":"6393584049543470937057043098611271993206122889317039351966319038535020834557field","value":"1u32"},{"type":"public","id":"8207446256045172951742235001162005156507562935942883128759030124682934277495field","value":"2u32"}],"outputs":[{"type":"public","id":"127469473292952941321346770257126666363371158501875622169294663492714835110field","value":"3u32"}],"tpk":"7647553513996966044119163122930125808381703910407273818947266861843062002251group","tcm":"4479413938380109857414238205380483440836495997450846894155088299187217672609field","scm":"6461007226176477784737642021400489186736987671609840640950580467598882134642field"}` ;
395
+ const transitionDecryptedMainnet = Transition . fromString ( transitionDecryptedStringMainnet ) ;
396
+ const transitionViewKeyStringMainnet = "8161419549946991944867064830365679191883723972221767444308198038592561311302field" ;
397
+
390
398
const invalidTransitionViewKeyString = "5089075468761042335883809641276568724119791331127957254389204093712358605127field"
391
399
const invalidTransitionViewKey = Field . fromString ( invalidTransitionViewKeyString ) ;
392
400
const privateKey = PrivateKey . from_string ( "APrivateKey1zkp8CZNn3yeCseEtxuVPbDCwSyhGW6yZKUYKfgXmcpoGPWH" ) ;
393
401
const viewKey = privateKey . to_view_key ( ) ;
394
- const tvk = transition . tvk ( viewKey ) ;
395
402
403
+ let connection = new AleoNetworkClient ( "https://api.explorer.provable.com/v1" ) ;
404
+
405
+ if ( connection . network === "testnet" ) {
396
406
it ( 'can be decrypted with a valid transition view key' , ( ) => {
397
- const transitionDecryptedWithTVK = transition . decryptTransition ( tvk ) ;
407
+ const tvk = transitionTestnet . tvk ( viewKey ) ;
408
+ const transitionDecryptedWithTVK = transitionTestnet . decryptTransition ( tvk ) ;
398
409
// Ensure the transition is valid
399
- expect ( transitionDecryptedWithTVK . toString ( ) ) . equal ( transitionDecrypted . toString ( ) ) ;
410
+ expect ( transitionDecryptedWithTVK . toString ( ) ) . equal ( transitionDecryptedTestnet . toString ( ) ) ;
400
411
} ) ;
401
412
402
413
it ( 'cannot be decrypted with an invalid transition view key' , ( ) => {
403
- expect ( ( ) => transition . decryptTransition ( invalidTransitionViewKey ) . toThrow ( ) ) ;
414
+ expect ( ( ) => transitionTestnet . decryptTransition ( invalidTransitionViewKey ) . toThrow ( ) ) ;
404
415
} ) ;
405
416
406
417
it ( 'can generate a transition view key from a valid view key' , ( ) => {
407
- const generatedTransitionViewKey = transition . tvk ( viewKey ) ;
418
+ const generatedTransitionViewKey = transitionTestnet . tvk ( viewKey ) ;
408
419
409
- const generatedTransitionViewKeyFromEncryptionToolkit = EncryptionToolkit . generateTransitionViewKey ( viewKey , transition ) ;
410
420
// Ensure the generated transition view key is the same as the one used to decrypt
411
- expect ( generatedTransitionViewKey . toString ( ) ) . equal ( generatedTransitionViewKeyFromEncryptionToolkit . toString ( ) ) ;
421
+ expect ( generatedTransitionViewKey . toString ( ) ) . equal ( transitionViewKeyStringTestnet ) ;
412
422
} ) ;
413
- } ) ;
423
+ }
424
+ if ( connection . network === "mainnet" ) {
425
+ const tvk = transitionMainnet . tvk ( viewKey ) ;
426
+ it ( 'can be decrypted with a valid transition view key' , ( ) => {
427
+ const transitionDecryptedWithTVK = transitionMainnet . decryptTransition ( tvk ) ;
428
+ // Ensure the transition is valid
429
+ expect ( transitionDecryptedWithTVK . toString ( ) ) . equal ( transitionDecryptedMainnet . toString ( ) ) ;
430
+ } ) ;
431
+
432
+ it ( 'cannot be decrypted with an invalid transition view key' , ( ) => {
433
+ expect ( ( ) => transitionMainnet . decryptTransition ( invalidTransitionViewKey ) ) . to . throw ( ) ;
434
+ } ) ;
435
+
436
+ it ( 'can generate a transition view key from a valid view key' , ( ) => {
437
+ const generatedTransitionViewKey = transitionMainnet . tvk ( viewKey ) ;
438
+
439
+ // Ensure the generated transition view key is the same as the one used to decrypt
440
+ expect ( generatedTransitionViewKey . toString ( ) ) . equal ( transitionViewKeyStringMainnet ) ;
441
+ } ) ;
442
+ }
443
+ } ) ;
444
+
414
445
415
446
describe ( 'EncryptionToolkit' , ( ) => {
416
447
const recordCiphertextString = "record1qyqsqpe2szk2wwwq56akkwx586hkndl3r8vzdwve32lm7elvphh37rsyqyxx66trwfhkxun9v35hguerqqpqzqrtjzeu6vah9x2me2exkgege824sd8x2379scspmrmtvczs0d93qttl7y92ga0k0rsexu409hu3vlehe3yxjhmey3frh2z5pxm5cmxsv4un97q" ;
@@ -434,7 +465,7 @@ owner: aleo1j7qxyunfldj2lp8hsvy7mw5k8zaqgjfyr72x2gh3x4ewgae8v5gscf5jh3.private,
434
465
it ( 'can decrypt a record ciphertext with the record view key' , ( ) => {
435
466
const decryptedRecord = EncryptionToolkit . decryptRecordWithRVk ( recordViewKey , recordCiphertext ) ;
436
467
// Ensure the decrypted record is the same as the plaintext
437
- expect ( decryptedRecord ) . equal ( recordPlaintext ) ;
468
+ expect ( decryptedRecord . toString ( ) ) . equal ( recordPlaintext . toString ( ) ) ;
438
469
} ) ;
439
470
it ( 'cannot decrypt a record ciphertext with an invalid record view key' , ( ) => {
440
471
const invalidRecordViewKey = Field . fromString ( "4445718830394614891114647247073357114867447866913203502139893824059966201724field" ) ;
0 commit comments