33    get_emulator_log , 
44    kill_emulator , 
55    sapio , 
6+     SapioWorkspace , 
67    start_sapio_oracle , 
78}  from  './sapio' ; 
89import  {  readFile ,  writeFile  }  from  'fs/promises' ; 
@@ -11,7 +12,9 @@ import { preferences, Prefs } from './settings';
1112import  {  get_bitcoin_node  }  from  './bitcoin_rpc' ; 
1213import  RpcError  from  'bitcoin-core-ts/dist/src/errors/rpc-error' ; 
1314import  path  from  'path' ; 
15+ import  {  setup_chat  }  from  './chat' ; 
1416export  default  function  ( window : BrowserWindow )  { 
17+     setup_chat ( ) ; 
1518    ipcMain . handle ( 'bitcoin::command' ,  async  ( event ,  arg )  =>  { 
1619        const  node  =  await  get_bitcoin_node ( ) ; 
1720        try  { 
@@ -41,23 +44,37 @@ export default function (window: BrowserWindow) {
4144        const  contracts  =  await  sapio . list_contracts ( ) ; 
4245        return  contracts ; 
4346    } ) ; 
44-     ipcMain . handle ( 'sapio::create_contract' ,  async  ( event ,  [ which ,  args ] )  =>  { 
45-         const  result  =  await  sapio . create_contract ( which ,  args ) ; 
46-         return  result ; 
47-     } ) ; 
47+     ipcMain . handle ( 
48+         'sapio::create_contract' , 
49+         async  ( event ,  workspace ,  [ which ,  psbt ,  args ] )  =>  { 
50+             const  result  =  await  sapio . create_contract ( 
51+                 workspace , 
52+                 which , 
53+                 psbt , 
54+                 args 
55+             ) ; 
56+             return  result ; 
57+         } 
58+     ) ; 
4859
4960    ipcMain . handle ( 'sapio::show_config' ,  async  ( event )  =>  { 
5061        return  await  sapio . show_config ( ) ; 
5162    } ) ; 
5263
5364    ipcMain . handle ( 'sapio::load_wasm_plugin' ,  ( event )  =>  { 
54-         const  plugin  =  dialog . showOpenDialogSync ( { 
55-             properties : [ 'openFile' ] , 
65+         const  plugins  =  dialog . showOpenDialogSync ( { 
66+             properties : [ 'openFile' ,   'multiSelections' ] , 
5667            filters : [ {  extensions : [ 'wasm' ] ,  name : 'WASM'  } ] , 
5768        } ) ; 
58-         if  ( plugin  &&  plugin . length ) 
59-             return  sapio . load_contract_file_name ( plugin [ 0 ] ! ) ; 
60-         return  {  err : 'No Plugin Selected'  } ; 
69+         const  errs  =  [ ] ; 
70+         if  ( ! plugins  ||  ! plugins . length )  return  {  err : 'No Plugin Selected'  } ; 
71+         for  ( const  plugin  of  plugins )  { 
72+             const  loaded  =  sapio . load_contract_file_name ( plugin ) ; 
73+             if  ( 'err'  in  loaded )  { 
74+                 return  loaded ; 
75+             } 
76+         } 
77+         return  {  ok : null  } ; 
6178    } ) ; 
6279
6380    ipcMain . handle ( 'sapio::open_contract_from_file' ,  ( event )  =>  { 
@@ -77,62 +94,46 @@ export default function (window: BrowserWindow) {
7794            return  {  ok : data  } ; 
7895        } 
7996    } ) ; 
80-     ipcMain . handle ( 'sapio::compiled_contracts::list' ,  ( event )  =>  { 
81-         return  sapio . list_compiled_contracts ( ) ; 
82-     } ) ; 
83-     ipcMain . handle ( 'sapio::compiled_contracts::trash' ,  ( event ,  file_name )  =>  { 
84-         return  sapio . trash_compiled_contract ( file_name ) ; 
85-     } ) ; 
97+     ipcMain . handle ( 
98+         'sapio::compiled_contracts::list' , 
99+         async  ( event ,  workspace )  =>  { 
100+             return  ( 
101+                 await  SapioWorkspace . new ( workspace ) 
102+             ) . list_compiled_contracts ( ) ; 
103+         } 
104+     ) ; 
105+     ipcMain . handle ( 
106+         'sapio::compiled_contracts::trash' , 
107+         async  ( event ,  workspace ,  file_name )  =>  { 
108+             return  ( 
109+                 await  SapioWorkspace . new ( workspace ) 
110+             ) . trash_compiled_contract ( file_name ) ; 
111+         } 
112+     ) ; 
86113    ipcMain . handle ( 'sapio::psbt::finalize' ,  ( event ,  psbt )  =>  { 
87114        return  sapio . psbt_finalize ( psbt ) ; 
88115    } ) ; 
89- 
90116    ipcMain . handle ( 
91117        'sapio::compiled_contracts::open' , 
92-         async  ( event ,  file_name )  =>  { 
93-             const  data  =  JSON . parse ( 
94-                 await  readFile ( 
95-                     path . join ( 
96-                         app . getPath ( 'userData' ) , 
97-                         'compiled_contracts' , 
98-                         file_name , 
99-                         'bound.json' 
100-                     ) , 
101-                     { 
102-                         encoding : 'utf-8' , 
103-                     } 
104-                 ) 
105-             ) ; 
106-             const  args  =  JSON . parse ( 
107-                 await  readFile ( 
108-                     path . join ( 
109-                         app . getPath ( 'userData' ) , 
110-                         'compiled_contracts' , 
111-                         file_name , 
112-                         'args.json' 
113-                     ) , 
114-                     { 
115-                         encoding : 'utf-8' , 
116-                     } 
117-                 ) 
118-             ) ; 
119-             const  mod  =  await  readFile ( 
120-                 path . join ( 
121-                     app . getPath ( 'userData' ) , 
122-                     'compiled_contracts' , 
123-                     file_name , 
124-                     'module.json' 
125-                 ) , 
126-                 { 
127-                     encoding : 'utf-8' , 
128-                 } 
129-             ) ; 
130- 
131-             const  name  =  JSON . parse ( mod ) . module ; 
132- 
118+         async  ( event ,  workspace_name ,  file_name )  =>  { 
119+             const  workspace  =  await  SapioWorkspace . new ( workspace_name ) ; 
120+             const  data  =  await  workspace . read_bound_data_for ( file_name ) ; 
121+             const  name  =  await  workspace . read_module_for ( file_name ) ; 
122+             const  args  =  await  workspace . read_args_for ( file_name ) ; 
133123            return  {  ok : {  data,  name,  args }  } ; 
134124        } 
135125    ) ; 
126+ 
127+     ipcMain . handle ( 'sapio::workspaces::init' ,  async  ( event ,  workspace )  =>  { 
128+         await  SapioWorkspace . new ( workspace ) ; 
129+     } ) ; 
130+     ipcMain . handle ( 'sapio::workspaces::list' ,  async  ( event )  =>  { 
131+         return  await  SapioWorkspace . list_all ( ) ; 
132+     } ) ; 
133+     ipcMain . handle ( 'sapio::workspaces::trash' ,  async  ( event ,  workspace )  =>  { 
134+         return  ( await  SapioWorkspace . new ( workspace ) ) . trash_workspace ( workspace ) ; 
135+     } ) ; 
136+ 
136137    ipcMain . handle ( 'write_clipboard' ,  ( event ,  s : string )  =>  { 
137138        clipboard . writeText ( s ) ; 
138139    } ) ; 
0 commit comments