2017-05-24 18 views
2

こんにちは、私は、(テストのためだけに)シグナルプロトコルとの私のクライアント側のメッセージ交換をしようとします。しかし、私はproccessPreKeyに問題があります。ここで jsのシグナルプロトコルのための作業例

は、テストコードprocessPreKeyが欲しかったパラメータ私は本当に知らない

<script src="javascripts/libsignal-protocol.js"></script> 
<script src="javascripts/InMemorySignalProtocolStore.js"></script> 
<script> 
      var KeyHelperUser1 = libsignal.KeyHelper; 
      var KeyHelperUser2 = libsignal.KeyHelper; 

      var registrationId_User1 = KeyHelperUser1.generateRegistrationId(); 
      var registrationId_User2 = KeyHelperUser2.generateRegistrationId(); 
      // Store registrationId somewhere durable and safe. 

      var identityKeyPair_User1, identityKeyPair_User2; 
      var SignedPreKey_User1, SignedPreKey_User2; 

      // Test Store 
      var store_User1 = new SignalProtocolStore(); 
      var store_User2 = new SignalProtocolStore(); 

      var PreKey_User1, PreKey_User2; 

      // Build the session 
      var address_User1 = new libsignal.SignalProtocolAddress(1002, 0); 
      var address_User2 = new libsignal.SignalProtocolAddress(1001, 0); 

      var sessionBuilder_User1 = new libsignal.SessionBuilder(store_User1, address_User1); 
      var sessionBuilder_User2 = new libsignal.SessionBuilder(store_User2, address_User2); 

      KeyHelperUser1.generateIdentityKeyPair().then(function(identityKeyPair) { 
       // keyPair -> { pubKey: ArrayBuffer, privKey: ArrayBuffer } 
       // Store identityKeyPair somewhere durable and safe. 
       identityKeyPair_User1 = identityKeyPair; 

       KeyHelperUser1.generatePreKey(1001).then(function(preKey) { 
        //store.storePreKey(preKey.keyId, preKey.keyPair); 
        PreKey_User1 = preKey; 
        KeyHelperUser2.generatePreKey(1002).then(function(preKey) { 
         //store.storePreKey(preKey.keyId, preKey.keyPair); 
         PreKey_User2 = preKey; 
         KeyHelperUser1.generateSignedPreKey(identityKeyPair_User1, 1001).then(function(signedPreKey) { 
          store_User1.storeSignedPreKey(signedPreKey.keyId, signedPreKey.keyPair); 
          SignedPreKey_User1 = signedPreKey; 


          KeyHelperUser2.generateIdentityKeyPair().then(function(identityKeyPair) { 
           // keyPair -> { pubKey: ArrayBuffer, privKey: ArrayBuffer } 
           // Store identityKeyPair somewhere durable and safe. 
           identityKeyPair_User2 = identityKeyPair; 

           KeyHelperUser2.generateSignedPreKey(identityKeyPair_User2, 1002).then(function(signedPreKey) { 
            store_User2.storeSignedPreKey(signedPreKey.keyId, signedPreKey.keyPair); 
            SignedPreKey_User2 = signedPreKey; 

            var promise_User1 = sessionBuilder_User1.processPreKey({ 
             registrationId: registrationId_User2, 
             identityKey: identityKeyPair_User2.pubKey, 
             signedPreKey: { 
              keyId  : 1002, 
              publicKey : SignedPreKey_User2.pubKey, 
              signature : SignedPreKey_User2.signature 
             }, 
             preKey: { 
              keyId  : 1002, 
              publicKey : PreKey_User1.pubKey 
             } 
            }); 

            promise_User1.catch(function onerror(error) { 
             // handle identity key conflict 
             //console.log(error); 
            }); 

           }); 
          }); 
         }); 
        }); 
       }); 


      }); 
     </script> 

です。助けてもらえますか?

+0

ねえ、これを完了できましたか?私は信号の基本的なセットアップをしようとしています。これで私を助けることができますか? – tarun14110

答えて

0

こんにちは、これは私がまとめたものです。助けてください。

var KeyHelper = libsignal.KeyHelper; 

function generateIdentity(store) { 
    return Promise.all([ 
     KeyHelper.generateIdentityKeyPair(), 
     KeyHelper.generateRegistrationId(), 
    ]).then(function(result) { 
     store.put('identityKey', result[0]); 
     store.put('registrationId', result[1]); 
    }); 
} 

function generatePreKeyBundle(store, preKeyId, signedPreKeyId) { 
    return Promise.all([ 
     store.getIdentityKeyPair(), 
     store.getLocalRegistrationId() 
    ]).then(function(result) { 
     var identity = result[0]; 
     var registrationId = result[1]; 

     return Promise.all([ 
      KeyHelper.generatePreKey(preKeyId), 
      KeyHelper.generateSignedPreKey(identity, signedPreKeyId), 
     ]).then(function(keys) { 
      var preKey = keys[0] 
      var signedPreKey = keys[1]; 

      store.storePreKey(preKeyId, preKey.keyPair); 
      store.storeSignedPreKey(signedPreKeyId, signedPreKey.keyPair); 

      return { 
       identityKey: identity.pubKey, 
       registrationId : registrationId, 
       preKey: { 
        keyId  : preKeyId, 
        publicKey : preKey.keyPair.pubKey 
       }, 
       signedPreKey: { 
        keyId  : signedPreKeyId, 
        publicKey : signedPreKey.keyPair.pubKey, 
        signature : signedPreKey.signature 
       } 
      }; 
     }); 
    }); 
} 

var ALICE_ADDRESS = new libsignal.SignalProtocolAddress("xxxxxxxxx", 1); 
var BOB_ADDRESS = new libsignal.SignalProtocolAddress("yyyyyyyyyyyyy", 1); 

    var aliceStore = new libsignal.SignalProtocolStore(); 

    var bobStore = new libsignal.SignalProtocolStore(); 
    var bobPreKeyId = 1337; 
    var bobSignedKeyId = 1; 

    var Curve = libsignal.Curve; 

     Promise.all([ 
      generateIdentity(aliceStore), 
      generateIdentity(bobStore), 
     ]).then(function() { 
      return generatePreKeyBundle(bobStore, bobPreKeyId, bobSignedKeyId); 
     }).then(function(preKeyBundle) { 
      var builder = new libsignal.SessionBuilder(aliceStore, BOB_ADDRESS); 
      return builder.processPreKey(preKeyBundle).then(function() { 

       var originalMessage = util.toArrayBuffer("my message ......"); 
       var aliceSessionCipher = new libsignal.SessionCipher(aliceStore, BOB_ADDRESS); 
       var bobSessionCipher = new libsignal.SessionCipher(bobStore, ALICE_ADDRESS); 

       aliceSessionCipher.encrypt(originalMessage).then(function(ciphertext) { 

        // check for ciphertext.type to be 3 which includes the PREKEY_BUNDLE 
        return bobSessionCipher.decryptPreKeyWhisperMessage(ciphertext.body, 'binary'); 

       }).then(function(plaintext) { 

        alert(plaintext); 

       }); 

       bobSessionCipher.encrypt(originalMessage).then(function(ciphertext) { 

        return aliceSessionCipher.decryptWhisperMessage(ciphertext.body, 'binary'); 

       }).then(function(plaintext) { 

        assertEqualArrayBuffers(plaintext, originalMessage); 

       }); 

      }); 
     }); 
関連する問題