@@ -18,17 +18,19 @@ import { Observable, of, OperatorFunction, Subject } from 'rxjs';
1818import { filter , flatMap , map , share } from 'rxjs/operators' ;
1919import * as WebSocket from 'ws' ;
2020import { Address } from '../model/account/Address' ;
21- import { BlockInfo } from '../model/blockchain/BlockInfo' ;
2221import { NamespaceName } from '../model/namespace/NamespaceName' ;
2322import { AggregateTransaction } from '../model/transaction/AggregateTransaction' ;
2423import { CosignatureSignedTransaction } from '../model/transaction/CosignatureSignedTransaction' ;
2524import { Deadline } from '../model/transaction/Deadline' ;
2625import { Transaction } from '../model/transaction/Transaction' ;
2726import { TransactionStatusError } from '../model/transaction/TransactionStatusError' ;
28- import { BlockHttp } from './BlockHttp' ;
2927import { IListener } from './IListener' ;
3028import { NamespaceRepository } from './NamespaceRepository' ;
3129import { CreateTransactionFromDTO } from './transaction/CreateTransactionFromDTO' ;
30+ import { BlockInfoDTO } from 'symbol-openapi-typescript-node-client/dist/model/blockInfoDTO' ;
31+ import { NewBlock } from '../model/blockchain/NewBlock' ;
32+ import { PublicAccount } from '../model/account/PublicAccount' ;
33+ import { UInt64 } from '../model/UInt64' ;
3234
3335export enum ListenerChannelName {
3436 block = 'block' ,
@@ -44,14 +46,13 @@ export enum ListenerChannelName {
4446
4547interface ListenerMessage {
4648 readonly channelName : ListenerChannelName ;
47- readonly message : Transaction | string | BlockInfo | TransactionStatusError | CosignatureSignedTransaction ;
49+ readonly message : Transaction | string | NewBlock | TransactionStatusError | CosignatureSignedTransaction ;
4850}
4951
5052/**
5153 * Listener service
5254 */
5355export class Listener implements IListener {
54- public readonly url : string ;
5556 /**
5657 * @internal
5758 * WebSocket connector
@@ -70,14 +71,15 @@ export class Listener implements IListener {
7071
7172 /**
7273 * Constructor
73- * @param config - Listener configuration
74- * @param websocketInjected - (Optional) WebSocket injected when using listeners in client
74+ * @param url - Listener websocket server url. default: rest-gateway's url with ''/ws'' suffix. (e.g. http://localhost:3000/ws).
75+ * @param namespaceRepository - NamespaceRepository interface for resolving alias.
76+ * @param websocketInjected - (Optional) WebSocket injected when using listeners in client.
7577 */
7678 constructor (
7779 /**
78- * Listener configuration.
80+ * Listener websocket server url. default: rest-gateway's url with ''/ws'' suffix. (e.g. http://localhost:3000/ws)
7981 */
80- private config : string ,
82+ public readonly url : string ,
8183 /**
8284 * Namespace repository for resolving account alias
8385 */
@@ -87,8 +89,7 @@ export class Listener implements IListener {
8789 */
8890 private websocketInjected ?: any ,
8991 ) {
90- this . config = config . replace ( / \/ $ / , '' ) ;
91- this . url = `${ this . config } /ws` ;
92+ this . url = url . replace ( / \/ $ / , '' ) ;
9293 this . messageSubject = new Subject ( ) ;
9394 }
9495
@@ -139,7 +140,7 @@ export class Listener implements IListener {
139140 } else if ( message . block ) {
140141 this . messageSubject . next ( {
141142 channelName : ListenerChannelName . block ,
142- message : BlockHttp . toBlockInfo ( message ) ,
143+ message : this . toNewBlock ( message ) ,
143144 } ) ;
144145 } else if ( message . code ) {
145146 this . messageSubject . next ( {
@@ -192,13 +193,13 @@ export class Listener implements IListener {
192193 *
193194 * @return an observable stream of BlockInfo
194195 */
195- public newBlock ( ) : Observable < BlockInfo > {
196+ public newBlock ( ) : Observable < NewBlock > {
196197 this . subscribeTo ( 'block' ) ;
197198 return this . messageSubject . asObservable ( ) . pipe (
198199 share ( ) ,
199200 filter ( ( _ ) => _ . channelName === ListenerChannelName . block ) ,
200- filter ( ( _ ) => _ . message instanceof BlockInfo ) ,
201- map ( ( _ ) => _ . message as BlockInfo ) ,
201+ filter ( ( _ ) => _ . message instanceof NewBlock ) ,
202+ map ( ( _ ) => _ . message as NewBlock ) ,
202203 ) ;
203204 }
204205
@@ -406,4 +407,36 @@ export class Listener implements IListener {
406407 } ;
407408 this . webSocket . send ( JSON . stringify ( subscriptionMessage ) ) ;
408409 }
410+
411+ /**
412+ * This method maps a BlockInfoDTO from rest to the SDK's BlockInfo model object.
413+ *
414+ * @internal
415+ * @param {BlockInfoDTO } dto the dto object from rest.
416+ * @returns {NewBlock } a BlockInfo model
417+ */
418+ private toNewBlock ( dto : BlockInfoDTO ) : NewBlock {
419+ const networkType = dto . block . network . valueOf ( ) ;
420+ return new NewBlock (
421+ dto . meta . hash ,
422+ dto . meta . generationHash ,
423+ dto . block . signature ,
424+ PublicAccount . createFromPublicKey ( dto . block . signerPublicKey , networkType ) ,
425+ networkType ,
426+ dto . block . version ,
427+ dto . block . type ,
428+ UInt64 . fromNumericString ( dto . block . height ) ,
429+ UInt64 . fromNumericString ( dto . block . timestamp ) ,
430+ UInt64 . fromNumericString ( dto . block . difficulty ) ,
431+ dto . block . feeMultiplier ,
432+ dto . block . previousBlockHash ,
433+ dto . block . transactionsHash ,
434+ dto . block . receiptsHash ,
435+ dto . block . stateHash ,
436+ dto . block . proofGamma ,
437+ dto . block . proofScalar ,
438+ dto . block . proofVerificationHash ,
439+ dto . block . beneficiaryPublicKey ? PublicAccount . createFromPublicKey ( dto . block . beneficiaryPublicKey , networkType ) : undefined ,
440+ ) ;
441+ }
409442}
0 commit comments