11import { Injectable } from '@angular/core' ;
2- import { BehaviorSubject , filter , from , fromEvent , map , Observable , of , switchMap , tap } from 'rxjs' ;
2+ import { BehaviorSubject , filter , from , fromEvent , map , merge , Observable , of , switchMap , tap } from 'rxjs' ;
33import base from 'base-x' ;
44import { any , log } from '@openmina/shared' ;
5- import { CONFIG } from '@shared/constants/config ' ;
5+ import { HttpClient } from '@angular/common/http ' ;
66
77@Injectable ( {
88 providedIn : 'root' ,
@@ -11,9 +11,10 @@ export class WebNodeService {
1111
1212 private readonly backendSubject$ : BehaviorSubject < any > = new BehaviorSubject < any > ( null ) ;
1313 private backend : any ;
14+ private webNodeKeyPair : { publicKey : string , privateKey : string } ;
1415 webNodeState : string = 'notLoaded' ;
1516
16- constructor ( ) {
17+ constructor ( private http : HttpClient ) {
1718 const basex = base ( '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz' ) ;
1819 any ( window ) [ 'bs58btc' ] = {
1920 encode : ( buffer : Uint8Array | number [ ] ) => 'z' + basex . encode ( buffer ) ,
@@ -22,19 +23,25 @@ export class WebNodeService {
2223 }
2324
2425 loadWasm$ ( ) : Observable < void > {
25- if ( ( window as any ) . webnode ) {
26- return of ( void 0 ) ;
27- }
28- return fromEvent ( window , 'webNodeLoaded' ) . pipe ( map ( ( ) => void 0 ) ) ;
26+ console . log ( '---LOADING WASM' ) ;
27+ return merge (
28+ of ( any ( window ) . webnode ) . pipe ( filter ( Boolean ) ) ,
29+ fromEvent ( window , 'webNodeLoaded' ) ,
30+ ) . pipe (
31+ switchMap ( ( ) => this . http . get < { publicKey : string , privateKey : string } > ( 'assets/webnode/web-node-secrets.json' ) ) ,
32+ tap ( data => this . webNodeKeyPair = data ) ,
33+ map ( ( ) => void 0 ) ,
34+ ) ;
2935 }
3036
3137 startWasm$ ( ) : Observable < any > {
32- return of ( ( window as any ) . webnode )
38+ console . log ( '---STARTING WASM' ) ;
39+ return of ( any ( window ) . webnode )
3340 . pipe (
3441 switchMap ( ( wasm : any ) => from ( wasm . default ( 'assets/webnode/pkg/openmina_node_web_bg.wasm' ) ) . pipe ( map ( ( ) => wasm ) ) ) ,
3542 switchMap ( ( wasm ) => {
3643 console . log ( wasm ) ;
37- return from ( wasm . run ( CONFIG . webNodeKey ) ) ;
44+ return from ( wasm . run ( this . webNodeKeyPair . privateKey ) ) ;
3845 } ) ,
3946 tap ( ( jsHandle : any ) => {
4047 this . backend = jsHandle ;
@@ -47,10 +54,15 @@ export class WebNodeService {
4754 ) ;
4855 }
4956
57+ get webNodeKeys ( ) : { publicKey : string , privateKey : string } {
58+ return this . webNodeKeyPair ;
59+ }
60+
5061 get status$ ( ) : Observable < any > {
5162 return this . backendSubject$ . asObservable ( ) . pipe (
5263 filter ( Boolean ) ,
5364 switchMap ( handle => from ( ( handle as any ) . status ( ) ) ) ,
65+ log ( ) ,
5466 ) ;
5567 }
5668
@@ -81,4 +93,40 @@ export class WebNodeService {
8193 switchMap ( handle => from ( ( handle as any ) . stats ( ) . sync ( ) ) ) ,
8294 ) ;
8395 }
96+
97+ get accounts$ ( ) : Observable < any > {
98+ return this . backendSubject$ . asObservable ( ) . pipe (
99+ filter ( Boolean ) ,
100+ switchMap ( handle => from ( ( handle as any ) . ledger ( ) . latest ( ) . accounts ( ) . all ( ) ) ) ,
101+ ) ;
102+ }
103+
104+ get bestChainUserCommands$ ( ) : Observable < any > {
105+ console . log ( '---GETTING BEST CHAIN USER COMMANDS' ) ;
106+ return this . backendSubject$ . asObservable ( ) . pipe (
107+ filter ( Boolean ) ,
108+ switchMap ( handle => from ( ( handle as any ) . transition_frontier ( ) . best_chain ( ) . user_commands ( ) ) ) ,
109+ tap ( ( r ) => {
110+ console . log ( 'response from GETTING BEST CHAIN USER COMMANDS' , r ) ;
111+ } ) ,
112+ ) ;
113+ }
114+
115+ sendPayment$ ( payment : any ) : Observable < any > {
116+ return this . backendSubject$ . asObservable ( ) . pipe (
117+ filter ( Boolean ) ,
118+ switchMap ( handle => from ( ( handle as any ) . transaction_pool ( ) . inject ( ) . payment ( payment ) ) ) ,
119+ ) ;
120+ }
121+
122+ get transactionPool$ ( ) : Observable < any > {
123+ console . log ( '---GETTING TRANSACTION POOL' ) ;
124+ return this . backendSubject$ . asObservable ( ) . pipe (
125+ filter ( Boolean ) ,
126+ switchMap ( handle => from ( ( handle as any ) . transaction_pool ( ) . get ( ) ) ) ,
127+ tap ( ( r ) => {
128+ console . log ( 'response from GETTING TRANSACTION POOL' , r ) ;
129+ } ) ,
130+ ) ;
131+ }
84132}
0 commit comments