@ -30,10 +30,7 @@ const fetch = async (url, options = {}) => {
const method = options . method || 'GET' ;
const address = parse ( url ) . hostname ;
const doEncryptChannel =
address . endsWith ( '.snode' ) &&
options . headers &&
LOKI _EPHEMKEY _HEADER in options . headers ;
const doEncryptChannel = address . endsWith ( '.snode' ) ;
if ( doEncryptChannel ) {
try {
// eslint-disable-next-line no-param-reassign
@ -42,7 +39,11 @@ const fetch = async (url, options = {}) => {
options . body
) ;
// eslint-disable-next-line no-param-reassign
options . headers [ 'Content-Type' ] = 'text/plain' ;
options . headers = {
... options . headers ,
'Content-Type' : 'text/plain' ,
[ LOKI _EPHEMKEY _HEADER ] : libloki . crypto . snodeCipher . getChannelPublicKeyHex ( ) ,
} ;
} catch ( e ) {
log . warn ( ` Could not encrypt channel for ${ address } : ` , e ) ;
}
@ -90,6 +91,25 @@ const fetch = async (url, options = {}) => {
}
} ;
// Wrapper for a JSON RPC request
const rpc = ( address , port , method , params , options = { } ) => {
const headers = options . headers || { } ;
const url = ` ${ address } ${ port } ${ endpointBase } ` ;
const body = {
method ,
params ,
} ;
const fetchOptions = {
method : 'POST' ,
... options ,
body : JSON . stringify ( body ) ,
headers ,
} ;
return fetch ( url , fetchOptions ) ;
} ;
// Will be raised (to 3?) when we get more nodes
const MINIMUM _SUCCESSFUL _REQUESTS = 2 ;
@ -109,22 +129,13 @@ class LokiMessageAPI {
const data64 = dcodeIO . ByteBuffer . wrap ( data ) . toString ( 'base64' ) ;
const p2pDetails = lokiP2pAPI . getContactP2pDetails ( pubKey ) ;
const body = {
method : 'store' ,
params : {
data : data64 ,
} ,
} ;
if ( p2pDetails && ( isPing || p2pDetails . isOnline ) ) {
try {
const port = p2pDetails . port ? ` : ${ p2pDetails . port } ` : '' ;
const url = ` ${ p2pDetails . address } ${ port } ${ endpointBase } ` ;
const fetchOptions = {
method : 'POST' ,
body : JSON . stringify ( body ) ,
} ;
await fetch ( url , fetchOptions ) ;
await rpc ( p2pDetails . address , port , 'store' , {
data : data64 ,
} ) ;
lokiP2pAPI . setContactOnline ( pubKey ) ;
window . Whisper . events . trigger ( 'p2pMessageSent' , messageEventData ) ;
return ;
@ -155,16 +166,6 @@ class LokiMessageAPI {
// Something went horribly wrong
throw err ;
}
const storageParams = {
pubKey ,
ttl : ttl . toString ( ) ,
nonce ,
timestamp : timestamp . toString ( ) ,
} ;
body . params = {
... body . params ,
... storageParams ,
} ;
const completedNodes = [ ] ;
const failedNodes = [ ] ;
@ -179,17 +180,16 @@ class LokiMessageAPI {
} ;
const doRequest = async nodeUrl => {
const url = ` ${ nodeUrl } ${ this . messageServerPort } ${ endpointBase } ` ;
const fetchOptions = {
method : 'POST' ,
body : JSON . stringify ( body ) ,
headers : {
[ LOKI _EPHEMKEY _HEADER ] : libloki . crypto . snodeCipher . getChannelPublicKeyHex ( ) ,
} ,
const params = {
pubKey ,
ttl : ttl . toString ( ) ,
nonce ,
timestamp : timestamp . toString ( ) ,
data : data64 ,
} ;
try {
await fetch( url , fetchOption s) ;
await rpc( nodeUrl , this . messageServerPort , 'store' , param s) ;
nodeComplete ( nodeUrl ) ;
successfulRequests += 1 ;
@ -271,24 +271,18 @@ class LokiMessageAPI {
} ;
const doRequest = async ( nodeUrl , nodeData ) => {
const url = ` ${ nodeUrl } ${ this . messageServerPort } ${ endpointBase } ` ;
const body = {
method : 'retrieve' ,
params : {
pubKey : ourKey ,
lastHash : nodeData . lastHash ,
} ,
} ;
const headers = {
[ LOKI _EPHEMKEY _HEADER ] : libloki . crypto . snodeCipher . getChannelPublicKeyHex ( ) ,
} ;
const fetchOptions = {
method : 'POST' ,
body : JSON . stringify ( body ) ,
headers ,
const params = {
pubKey : ourKey ,
lastHash : nodeData . lastHash ,
} ;
try {
const result = await fetch ( url , fetchOptions ) ;
const result = await rpc (
nodeUrl ,
this . messageServerPort ,
'retrieve' ,
params
) ;
nodeComplete ( nodeUrl ) ;