@@ -444,22 +444,19 @@ EmbarkJS.Messages.setProvider = function(provider, options) {
444444 var self = this ;
445445 var ipfs ;
446446 if ( provider === 'whisper' ) {
447+ this . providerName = 'whisper'
447448 this . currentMessages = EmbarkJS . Messages . Whisper ;
448- if ( typeof variable === 'undefined' && typeof ( web3 ) === 'undefined' ) {
449- let provider ;
450- if ( options === undefined ) {
451- provider = "localhost:8546" ;
452- } else {
453- provider = options . server + ':' + options . port ;
454- }
455- if ( this . isNewWeb3 ( ) ) {
456- // TODO: add current Provider
457- self . currentMessages . web3 = new Web3 ( new Web3 . providers . WebsocketProvider ( "ws://" + provider ) ) ;
458- } else {
459- self . currentMessages . web3 = new Web3 ( new Web3 . providers . HttpProvider ( "http://" + provider ) ) ;
460- }
449+ let provider ;
450+ if ( options === undefined ) {
451+ provider = "localhost:8546" ;
452+ } else {
453+ provider = options . server + ':' + options . port ;
454+ }
455+ if ( this . isNewWeb3 ( ) ) {
456+ self . currentMessages . web3 = new Web3 ( new Web3 . providers . WebsocketProvider ( "ws://" + provider ) ) ;
457+ } else {
458+ self . currentMessages . web3 = new Web3 ( new Web3 . providers . HttpProvider ( "http://" + provider ) ) ;
461459 }
462- console . log ( "getting whisper version" ) ;
463460 self . getWhisperVersion ( function ( err , version ) {
464461 if ( err ) {
465462 console . log ( "whisper not available" ) ;
@@ -468,14 +465,15 @@ EmbarkJS.Messages.setProvider = function(provider, options) {
468465 self . currentMessages . web3 . shh . newSymKey ( ) . then ( ( id ) => { self . currentMessages . symKeyID = id ; } ) ;
469466 self . currentMessages . web3 . shh . newKeyPair ( ) . then ( ( id ) => { self . currentMessages . sig = id ; } ) ;
470467 } else {
471- console . log ( "this version of whisper is not supported yet; try a version of geth bellow 1.6.1 " ) ;
468+ console . log ( "this version of whisper in this node " ) ;
472469 }
473470 } else {
474471 self . currentMessages . identity = self . currentMessages . web3 . shh . newIdentity ( ) ;
475472 }
476473 self . currentMessages . whisperVersion = self . currentMessages . web3 . version . whisper ;
477474 } ) ;
478475 } else if ( provider === 'orbit' ) {
476+ this . providerName = 'orbit'
479477 this . currentMessages = EmbarkJS . Messages . Orbit ;
480478 if ( options === undefined ) {
481479 ipfs = HaadIpfsApi ( 'localhost' , '5001' ) ;
@@ -504,104 +502,108 @@ EmbarkJS.Messages.listenTo = function(options) {
504502EmbarkJS . Messages . Whisper = { } ;
505503
506504EmbarkJS . Messages . Whisper . sendMessage = function ( options ) {
505+ if ( EmbarkJS . Messages . isNewWeb3 ( ) ) {
507506 var topics = options . topic || options . topics ;
508507 var data = options . data || options . payload ;
509- var identity ;
510- if ( ! EmbarkJS . Messages . isNewWeb3 ( ) ) {
511- identity = options . identity || this . identity || web3 . shh . newIdentity ( ) ;
508+ var ttl = options . ttl || 100 ;
509+ var priority = options . priority || 1000 ;
510+ var powTime = options . powTime || 3 ;
511+ var powTarget = options . powTarget || 0.5 ;
512+
513+ if ( topics === undefined ) {
514+ throw new Error ( "missing option: topic" ) ;
515+ }
516+
517+ if ( data === undefined ) {
518+ throw new Error ( "missing option: data" ) ;
512519 }
520+
521+ topics = this . web3 . utils . toHex ( topics ) . slice ( 0 , 10 ) ;
522+
523+ var payload = JSON . stringify ( data ) ;
524+
525+ let message = {
526+ symKeyID : this . symKeyID , // encrypts using the sym key ID
527+ sig : this . sig , // signs the message using the keyPair ID
528+ ttl : ttl ,
529+ topic : topics ,
530+ payload : EmbarkJS . Utils . fromAscii ( payload ) ,
531+ powTime : powTime ,
532+ powTarget : powTarget
533+ } ;
534+
535+ this . web3 . shh . post ( message , function ( ) { } ) ;
536+ } else {
537+ var topics = options . topic || options . topics ;
538+ var data = options . data || options . payload ;
539+ var identity = options . identity || this . identity || web3 . shh . newIdentity ( ) ;
513540 var ttl = options . ttl || 100 ;
514541 var priority = options . priority || 1000 ;
515542 var _topics ;
516543
517544 if ( topics === undefined ) {
518- throw new Error ( "missing option: topic" ) ;
545+ throw new Error ( "missing option: topic" ) ;
519546 }
520547
521548 if ( data === undefined ) {
522- throw new Error ( "missing option: data" ) ;
549+ throw new Error ( "missing option: data" ) ;
523550 }
524551
525- if ( EmbarkJS . Messages . isNewWeb3 ( ) ) {
526- topics = this . web3 . utils . toHex ( topics ) . slice ( 0 , 10 ) ;
552+ if ( typeof topics === 'string' ) {
553+ _topics = [ EmbarkJS . Utils . fromAscii ( topics ) ] ;
527554 } else {
528- if ( typeof topics === 'string' ) {
529- _topics = [ EmbarkJS . Utils . fromAscii ( topics ) ] ;
530- } else {
531- // TODO: replace with es6 + babel;
532- for ( var i = 0 ; i < topics . length ; i ++ ) {
533- _topics . push ( EmbarkJS . Utils . fromAscii ( topics [ i ] ) ) ;
534- }
535- }
536- topics = _topics ;
555+ _topics = topics . map ( ( t ) => EmbarkJS . Utils . fromAscii ( t ) ) ;
537556 }
557+ topics = _topics ;
538558
539559 var payload = JSON . stringify ( data ) ;
540560
541561 var message ;
542- if ( EmbarkJS . Messages . isNewWeb3 ( ) ) {
543- message = {
544- symKeyID : this . symKeyID , // encrypts using the sym key ID
545- sig : this . sig , // signs the message using the keyPair ID
546- ttl : 10 ,
547- topic : topics ,
548- payload : EmbarkJS . Utils . fromAscii ( 'hello' ) ,
549- powTime : 3 ,
550- powTarget : 0.5
551- } ;
552- } else {
553- message = {
554- from : identity ,
555- topics : topics ,
556- payload : EmbarkJS . Utils . fromAscii ( payload ) ,
557- ttl : ttl ,
558- priority : priority
559- } ;
560- }
562+ message = {
563+ from : identity ,
564+ topics : topics ,
565+ payload : EmbarkJS . Utils . fromAscii ( payload ) ,
566+ ttl : ttl ,
567+ priority : priority
568+ } ;
561569
562- return this . web3 . shh . post ( message , function ( ) { } ) ;
570+ return EmbarkJS . Messages . currentMessages . web3 . shh . post ( message , function ( ) { console . log ( "message sent" ) } ) ;
571+ }
563572} ;
564573
565574EmbarkJS . Messages . Whisper . listenTo = function ( options ) {
566- var topics = options . topic || options . topics ;
567- var _topics = [ ] ;
575+ if ( EmbarkJS . Messages . isNewWeb3 ( ) ) {
576+ var messageEvents = function ( ) {
577+ this . cb = function ( ) { } ;
578+ } ;
568579
569- var messageEvents = function ( ) {
570- this . cb = function ( ) { } ;
571- } ;
580+ messageEvents . prototype . then = function ( cb ) {
581+ this . cb = cb ;
582+ } ;
572583
573- messageEvents . prototype . then = function ( cb ) {
574- this . cb = cb ;
575- } ;
584+ messageEvents . prototype . error = function ( err ) {
585+ return err ;
586+ } ;
587+
588+ messageEvents . prototype . stop = function ( ) {
589+ this . filter . stopWatching ( ) ;
590+ } ;
576591
577- messageEvents . prototype . error = function ( err ) {
578- return err ;
579- } ;
592+ var topics = options . topic || options . topics ;
593+ var _topics = [ ] ;
580594
581- messageEvents . prototype . stop = function ( ) {
582- this . filter . stopWatching ( ) ;
583- } ;
595+ let promise = new messageEvents ( ) ;
584596
585- if ( EmbarkJS . Messages . isNewWeb3 ( ) ) {
586- topics = [ this . web3 . utils . toHex ( topics ) . slice ( 0 , 10 ) ] ;
587- } else {
597+ // listenTo
588598 if ( typeof topics === 'string' ) {
589- _topics = [ topics ] ;
599+ topics = [ this . web3 . utils . toHex ( topics ) . slice ( 0 , 10 ) ] ;
590600 } else {
591- // TODO: replace with es6 + babel;
592- for ( var i = 0 ; i < topics . length ; i ++ ) {
593- _topics . push ( topics [ i ] ) ;
594- }
601+ topics = topics . map ( ( t ) => this . web3 . utils . toHex ( t ) . slice ( 0 , 10 ) ) ;
595602 }
596- topics = _topics ;
597- }
598-
599- if ( EmbarkJS . Messages . isNewWeb3 ( ) ) {
600- let promise = new messageEvents ( ) ;
601603
602604 let filter = this . web3 . shh . subscribe ( "messages" , {
603- symKeyID : this . symKeyID ,
604- topics : topics
605+ symKeyID : this . symKeyID ,
606+ topics : topics
605607 } ) . on ( 'data' , function ( result ) {
606608 var payload = JSON . parse ( EmbarkJS . Utils . toAscii ( result . payload ) ) ;
607609 var data ;
@@ -611,14 +613,40 @@ EmbarkJS.Messages.Whisper.listenTo = function(options) {
611613 //from: result.from,
612614 time : result . timestamp
613615 } ;
616+
614617 promise . cb ( payload , data , result ) ;
615618 } ) ;
616619
617620 promise . filter = filter ;
618621
619622 return promise ;
620-
621623 } else {
624+ var topics = options . topic || options . topics ;
625+ var _topics = [ ] ;
626+
627+ var messageEvents = function ( ) {
628+ this . cb = function ( ) { } ;
629+ } ;
630+
631+ messageEvents . prototype . then = function ( cb ) {
632+ this . cb = cb ;
633+ } ;
634+
635+ messageEvents . prototype . error = function ( err ) {
636+ return err ;
637+ } ;
638+
639+ messageEvents . prototype . stop = function ( ) {
640+ this . filter . stopWatching ( ) ;
641+ } ;
642+
643+ if ( typeof topics === 'string' ) {
644+ _topics = [ topics ] ;
645+ } else {
646+ _topics = topics . map ( ( t ) => EmbarkJS . Utils . fromAscii ( t ) ) ;
647+ }
648+ topics = _topics ;
649+
622650 var filterOptions = {
623651 topics : topics
624652 } ;
@@ -645,7 +673,7 @@ EmbarkJS.Messages.Whisper.listenTo = function(options) {
645673
646674 return promise ;
647675 }
648- } ;
676+ }
649677
650678EmbarkJS . Messages . Orbit = { } ;
651679
@@ -722,6 +750,10 @@ EmbarkJS.Utils = {
722750 fromAscii : function ( str ) {
723751 var _web3 = new Web3 ( ) ;
724752 return _web3 . utils ? _web3 . utils . fromAscii ( str ) : _web3 . fromAscii ( str ) ;
753+ } ,
754+ toAscii : function ( str ) {
755+ var _web3 = new Web3 ( ) ;
756+ return _web3 . utils . toAscii ( str ) ;
725757 }
726758} ;
727759
0 commit comments