matrix-org / olm

An implementation of the Double Ratchet cryptographic ratchet in C++/C
https://gitlab.matrix.org/matrix-org/olm
63 stars 9 forks source link

NPM example in documentation does not work #91

Open raxod502 opened 1 month ago

raxod502 commented 1 month ago

The following example code is given at https://www.npmjs.com/package/@matrix-org/olm:

var outbound_session = new Olm.OutboundGroupSession();
outbound_session.create();

// exchange these over a secure channel
var session_id = group_session.session_id();
var session_key = group_session.session_key();
var message_index = group_session.message_index();

var inbound_session = new Olm.InboundGroupSession();
inbound_session.create(message_index, session_key);

var ciphertext = outbound_session.encrypt("Hello");
var plaintext = inbound_session.decrypt(ciphertext);

This refers to a nonexistent group_session variable, but I assume that is a typo and outbound_session is meant instead. However, executing those commands produces an error:

> Olm = require('@matrix-org/olm'); await Olm.init()
undefined
> var outbound_session = new Olm.OutboundGroupSession();
undefined
> outbound_session.create();
undefined
> var session_id = outbound_session.session_id();
undefined
> var session_key = outbound_session.session_key();
undefined
> var message_index = outbound_session.message_index();
undefined
> var inbound_session = new Olm.InboundGroupSession();
undefined
> inbound_session.create(message_index, session_key);
/home/raxod502/files/temp/matrix/node_modules/@matrix-org/olm/olm.js:19
if(ia)m=l?require("path").dirname(m)+"/":__dirname+"/",na=()=>{ma||(fs=require("fs"),ma=require("path"))},ja=function(b,c){na();b=ma.normalize(b);return fs.readFileSync(b,c?void 0:"utf8")},la=b=>{b=ja(b,!0);b.buffer||(b=new Uint8Array(b));return b},ka=(b,c,d)=>{na();b=ma.normalize(b);fs.readFile(b,function(e,f){e?d(e):c(f.buffer)})},1<process.argv.length&&process.argv[1].replace(/\\/g,"/"),process.argv.slice(2),process.on("uncaughtException",function(b){throw b;}),process.on("unhandledRejection",
                                                                                                                                                                                                                                                                                                                                                                                                                                                                          ^

Error: OLM.BAD_SESSION_KEY
    at /home/raxod502/files/temp/matrix/node_modules/@matrix-org/olm/olm.js:78:251
    at O.<anonymous> (/home/raxod502/files/temp/matrix/node_modules/@matrix-org/olm/olm.js:80:92)
    at O.create (/home/raxod502/files/temp/matrix/node_modules/@matrix-org/olm/olm.js:96:290)

Node.js v20.15.1

What am I missing? This is matrix-olm installed from npm as linked above, release version 3.2.15.

raxod502 commented 1 month ago

Apparently the message_index argument doesn't exist...? Code here: https://gitlab.matrix.org/matrix-org/olm/-/blob/66294cf7f66ae380683dbb7f43a16a8922249fc8/javascript/olm_inbound_group_session.js#L66

If I omit that argument then it works... can the documentation be updated to be correct?