@@ -290,8 +290,10 @@ EmbarkJS.Storage.setProvider = function(provider, options) {
290290 try {
291291 if ( options === undefined ) {
292292 self . ipfsConnection = IpfsApi ( 'localhost' , '5001' ) ;
293+ self . getUrl = "http://localhost:8080/ipfs/" ;
293294 } else {
294295 self . ipfsConnection = IpfsApi ( options . server , options . port ) ;
296+ self . getUrl = options . getUrl || "http://localhost:8080/ipfs/" ;
295297 }
296298 resolve ( self ) ;
297299 } catch ( err ) {
@@ -383,7 +385,8 @@ EmbarkJS.Storage.IPFS.uploadFile = function(inputSelector) {
383385EmbarkJS . Storage . IPFS . getUrl = function ( hash ) {
384386 //var ipfsHash = web3.toAscii(hash);
385387
386- return 'http://localhost:8080/ipfs/' + hash ;
388+ //return 'http://localhost:8080/ipfs/' + hash;
389+ return ( self . getUrl || "http://localhost:8080/ipfs/" ) + hash ;
387390} ;
388391
389392//=========================================================
@@ -392,26 +395,70 @@ EmbarkJS.Storage.IPFS.getUrl = function(hash) {
392395
393396EmbarkJS . Messages = { } ;
394397
398+ EmbarkJS . Messages . web3CompatibleWithV5 = function ( ) {
399+ var _web3 = new Web3 ( ) ;
400+ if ( typeof ( _web3 . version ) === "string" ) {
401+ return true ;
402+ } else {
403+ return parseInt ( _web3 . version . api . split ( '.' ) [ 1 ] , 10 ) >= 20 ;
404+ }
405+ } ;
406+
407+ EmbarkJS . Messages . isNewWeb3 = function ( ) {
408+ var _web3 = new Web3 ( ) ;
409+ if ( typeof ( _web3 . version ) === "string" ) {
410+ return true ;
411+ } else {
412+ return parseInt ( _web3 . version . api . split ( '.' ) [ 0 ] , 10 ) >= 1 ;
413+ }
414+ } ;
415+
416+ EmbarkJS . Messages . getWhisperVersion = function ( cb ) {
417+ if ( this . isNewWeb3 ( ) ) {
418+ this . currentMessages . web3 . shh . getVersion ( function ( err , version ) {
419+ cb ( err , version ) ;
420+ } ) ;
421+ } else {
422+ this . currentMessages . web3 . version . getWhisper ( function ( err , res ) {
423+ cb ( err , web3 . version . whisper ) ;
424+ } ) ;
425+ }
426+ } ;
427+
395428EmbarkJS . Messages . setProvider = function ( provider , options ) {
396429 var self = this ;
397430 var ipfs ;
398431 if ( provider === 'whisper' ) {
399432 this . currentMessages = EmbarkJS . Messages . Whisper ;
400433 if ( typeof variable === 'undefined' && typeof ( web3 ) === 'undefined' ) {
434+ let provider ;
401435 if ( options === undefined ) {
402- web3 = new Web3 ( new Web3 . providers . HttpProvider ( "http://localhost:8545" ) ) ;
436+ provider = "localhost:8546" ;
437+ } else {
438+ provider = options . server + ':' + options . port ;
439+ }
440+ if ( this . isNewWeb3 ( ) ) {
441+ // TODO: add current Provider
442+ self . currentMessages . web3 = new Web3 ( new Web3 . providers . WebsocketProvider ( "ws://" + provider ) ) ;
403443 } else {
404- web3 = new Web3 ( new Web3 . providers . HttpProvider ( "http://" + options . server + ':' + options . port ) ) ;
444+ self . currentMessages . web3 = new Web3 ( new Web3 . providers . HttpProvider ( "http://" + provider ) ) ;
405445 }
406446 }
407- web3 . version . getWhisper ( function ( err , res ) {
447+ console . log ( "getting whisper version" ) ;
448+ self . getWhisperVersion ( function ( err , version ) {
408449 if ( err ) {
409450 console . log ( "whisper not available" ) ;
410- } else if ( web3 . version . whisper >= 5 ) {
411- console . log ( "this version of whisper is not supported yet; try a version of geth bellow 1.6.1" ) ;
451+ } else if ( version >= 5 ) {
452+ if ( self . web3CompatibleWithV5 ( ) ) {
453+ self . currentMessages . web3 . shh . newSymKey ( ) . then ( ( id ) => { self . currentMessages . symKeyID = id ; } ) ;
454+ self . currentMessages . web3 . shh . newKeyPair ( ) . then ( ( id ) => { self . currentMessages . sig = id ; } ) ;
455+ } else {
456+ console . log ( "this version of whisper is not supported yet; try a version of geth bellow 1.6.1" ) ;
457+ }
412458 } else {
413- self . currentMessages . identity = web3 . shh . newIdentity ( ) ;
459+ self . currentMessages . identity = self . currentMessages . web3 . shh . newIdentity ( ) ;
414460 }
461+ self . currentMessages . whisperVersion = self . currentMessages . web3 . version . whisper ;
415462 } ) ;
416463 } else if ( provider === 'orbit' ) {
417464 this . currentMessages = EmbarkJS . Messages . Orbit ;
@@ -444,7 +491,10 @@ EmbarkJS.Messages.Whisper = {};
444491EmbarkJS . Messages . Whisper . sendMessage = function ( options ) {
445492 var topics = options . topic || options . topics ;
446493 var data = options . data || options . payload ;
447- var identity = options . identity || this . identity || web3 . shh . newIdentity ( ) ;
494+ var identity ;
495+ if ( ! EmbarkJS . Messages . isNewWeb3 ( ) ) {
496+ identity = options . identity || this . identity || web3 . shh . newIdentity ( ) ;
497+ }
448498 var ttl = options . ttl || 100 ;
449499 var priority = options . priority || 1000 ;
450500 var _topics ;
@@ -457,85 +507,129 @@ EmbarkJS.Messages.Whisper.sendMessage = function(options) {
457507 throw new Error ( "missing option: data" ) ;
458508 }
459509
460- // do fromAscii to each topics unless it's already a string
461- if ( typeof topics === 'string' ) {
462- _topics = [ web3 . fromAscii ( topics ) ] ;
510+ if ( EmbarkJS . Messages . isNewWeb3 ( ) ) {
511+ topics = this . web3 . utils . toHex ( topics ) . slice ( 0 , 10 ) ;
463512 } else {
513+ if ( typeof topics === 'string' ) {
514+ _topics = [ EmbarkJS . Utils . fromAscii ( topics ) ] ;
515+ } else {
464516 // TODO: replace with es6 + babel;
465517 for ( var i = 0 ; i < topics . length ; i ++ ) {
466- _topics . push ( web3 . fromAscii ( topics [ i ] ) ) ;
518+ _topics . push ( EmbarkJS . Utils . fromAscii ( topics [ i ] ) ) ;
467519 }
520+ }
521+ topics = _topics ;
468522 }
469- topics = _topics ;
470523
471524 var payload = JSON . stringify ( data ) ;
472525
473- var message = {
526+ var message ;
527+ if ( EmbarkJS . Messages . isNewWeb3 ( ) ) {
528+ message = {
529+ symKeyID : this . symKeyID , // encrypts using the sym key ID
530+ sig : this . sig , // signs the message using the keyPair ID
531+ ttl : 10 ,
532+ topic : topics ,
533+ payload : EmbarkJS . Utils . fromAscii ( 'hello' ) ,
534+ powTime : 3 ,
535+ powTarget : 0.5
536+ } ;
537+ } else {
538+ message = {
474539 from : identity ,
475540 topics : topics ,
476- payload : web3 . fromAscii ( payload ) ,
541+ payload : EmbarkJS . Utils . fromAscii ( payload ) ,
477542 ttl : ttl ,
478543 priority : priority
479- } ;
544+ } ;
545+ }
480546
481- return web3 . shh . post ( message , function ( ) { } ) ;
547+ return this . web3 . shh . post ( message , function ( ) { } ) ;
482548} ;
483549
484550EmbarkJS . Messages . Whisper . listenTo = function ( options ) {
485- var topics = options . topic || options . topics ;
486- var _topics = [ ] ;
551+ var topics = options . topic || options . topics ;
552+ var _topics = [ ] ;
487553
554+ var messageEvents = function ( ) {
555+ this . cb = function ( ) { } ;
556+ } ;
557+
558+ messageEvents . prototype . then = function ( cb ) {
559+ this . cb = cb ;
560+ } ;
561+
562+ messageEvents . prototype . error = function ( err ) {
563+ return err ;
564+ } ;
565+
566+ messageEvents . prototype . stop = function ( ) {
567+ this . filter . stopWatching ( ) ;
568+ } ;
569+
570+ if ( EmbarkJS . Messages . isNewWeb3 ( ) ) {
571+ topics = [ this . web3 . utils . toHex ( topics ) . slice ( 0 , 10 ) ] ;
572+ } else {
488573 if ( typeof topics === 'string' ) {
489- _topics = [ topics ] ;
574+ _topics = [ topics ] ;
490575 } else {
491- // TODO: replace with es6 + babel;
492- for ( var i = 0 ; i < topics . length ; i ++ ) {
493- _topics . push ( topics [ i ] ) ;
494- }
576+ // TODO: replace with es6 + babel;
577+ for ( var i = 0 ; i < topics . length ; i ++ ) {
578+ _topics . push ( topics [ i ] ) ;
579+ }
495580 }
496581 topics = _topics ;
582+ }
497583
498- var filterOptions = {
499- topics : topics
500- } ;
584+ if ( EmbarkJS . Messages . isNewWeb3 ( ) ) {
585+ let promise = new messageEvents ( ) ;
501586
502- var messageEvents = function ( ) {
503- this . cb = function ( ) { } ;
504- } ;
587+ let filter = this . web3 . shh . subscribe ( "messages" , {
588+ symKeyID : this . symKeyID ,
589+ topics : topics
590+ } ) . on ( 'data' , function ( result ) {
591+ var payload = JSON . parse ( EmbarkJS . Utils . toAscii ( result . payload ) ) ;
592+ var data ;
593+ data = {
594+ topic : result . topic ,
595+ data : payload ,
596+ //from: result.from,
597+ time : result . timestamp
598+ } ;
599+ promise . cb ( payload , data , result ) ;
600+ } ) ;
505601
506- messageEvents . prototype . then = function ( cb ) {
507- this . cb = cb ;
508- } ;
602+ promise . filter = filter ;
509603
510- messageEvents . prototype . error = function ( err ) {
511- return err ;
512- } ;
604+ return promise ;
513605
514- messageEvents . prototype . stop = function ( ) {
515- this . filter . stopWatching ( ) ;
606+ } else {
607+ var filterOptions = {
608+ topics : topics
516609 } ;
517610
518- var promise = new messageEvents ( ) ;
519-
520- var filter = web3 . shh . filter ( filterOptions , function ( err , result ) {
521- var payload = JSON . parse ( web3 . toAscii ( result . payload ) ) ;
522- var data ;
523- if ( err ) {
524- promise . error ( err ) ;
525- } else {
526- data = {
527- topic : topics ,
528- data : payload ,
529- from : result . from ,
530- time : ( new Date ( result . sent * 1000 ) )
531- } ;
532- promise . cb ( payload , data , result ) ;
533- }
611+ let promise = new messageEvents ( ) ;
612+
613+ let filter = this . web3 . shh . filter ( filterOptions , function ( err , result ) {
614+ var payload = JSON . parse ( EmbarkJS . Utils . toAscii ( result . payload ) ) ;
615+ var data ;
616+ if ( err ) {
617+ promise . error ( err ) ;
618+ } else {
619+ data = {
620+ topic : topics ,
621+ data : payload ,
622+ from : result . from ,
623+ time : ( new Date ( result . sent * 1000 ) )
624+ } ;
625+ promise . cb ( payload , data , result ) ;
626+ }
534627 } ) ;
535628
536629 promise . filter = filter ;
537630
538631 return promise ;
632+ }
539633} ;
540634
541635EmbarkJS . Messages . Orbit = { } ;
@@ -609,6 +703,13 @@ EmbarkJS.Messages.Orbit.listenTo = function(options) {
609703 return promise ;
610704} ;
611705
706+ EmbarkJS . Utils = {
707+ fromAscii : function ( str ) {
708+ var _web3 = new Web3 ( ) ;
709+ return _web3 . utils ? _web3 . utils . fromAscii ( str ) : _web3 . fromAscii ( str ) ;
710+ }
711+ } ;
712+
612713module . exports = EmbarkJS ;
613714
614715
0 commit comments