Class: RubyOlm::Account

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_olm/account.rb,
ext/ruby_olm/ext_lib_olm/ext_account.c

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'ext/ruby_olm/ext_lib_olm/ext_account.c', line 12

static VALUE initialize(int argc, VALUE *argv, VALUE self)
{
    VALUE pickle, password, opts;
    size_t size;
    OlmAccount *this;
    Data_Get_Struct(self, OlmAccount, this);
    
    (void)rb_scan_args(argc, argv, "0:", &opts);

    opts = (opts == Qnil) ? rb_hash_new(): opts;
        
    pickle = rb_hash_aref(opts, ID2SYM(rb_intern("pickle")));
    password = rb_hash_aref(opts, ID2SYM(rb_intern("password")));

    if(pickle != Qnil){

        if(rb_obj_is_kind_of(pickle, rb_cString) != Qtrue){
            
            rb_raise(rb_eTypeError, "pickle must be kind of String");
        }
    }
    
    if(password != Qnil){
        
        if(rb_obj_is_kind_of(password, rb_cString) != Qtrue){
            
            rb_raise(rb_eTypeError, "password must be kind of String");
        }
    }
    else{
        
        password = rb_str_new2("");
    }
    
    if(pickle != Qnil){    
        
        if(olm_unpickle_account(this, RSTRING_PTR(password), RSTRING_LEN(password), RSTRING_PTR(dup_string(pickle)), RSTRING_LEN(pickle)) == olm_error()){
            
            raise_olm_error(olm_account_last_error(this));
        }
    }
    else{
        
        size = olm_create_account_random_length(this);
        
        if(olm_create_account(this, RSTRING_PTR(get_random(size)), size) == olm_error()){
            
            raise_olm_error(olm_account_last_error(this));
        }
    }
    
    return self;
}

Class Method Details

.from_pickle(pickle, password = "") ⇒ Account

Parameters:

  • pickle (String)

    pickled state

  • password (String) (defaults to: "")

    password used to encrypt pickled state

Returns:



8
9
10
# File 'lib/ruby_olm/account.rb', line 8

def self.from_pickle(pickle, password="")
  Account.new(pickle: pickle, password: password)
end

Instance Method Details

#gen_otk(number = 1) ⇒ Object



32
33
34
# File 'lib/ruby_olm/account.rb', line 32

def gen_otk(number=1)
  generate_one_time_keys(number)
end

#generate_one_time_keys(number) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'ext/ruby_olm/ext_lib_olm/ext_account.c', line 83

static VALUE generate_one_time_keys(VALUE self, VALUE number)
{
    size_t size;
    OlmAccount *this;
    Data_Get_Struct(self, OlmAccount, this);
    
    size = olm_account_generate_one_time_keys_random_length(this, NUM2SIZET(number));        
    
    if(olm_account_generate_one_time_keys(this, NUM2SIZET(number), RSTRING_PTR(get_random(size)), size) == olm_error()){
        
        raise_olm_error(olm_account_last_error(this));
    }
    
    return self;
}

#identity_keysObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'ext/ruby_olm/ext_lib_olm/ext_account.c', line 66

static VALUE identity_keys(VALUE self)
{
    OlmAccount *this;
    size_t size;
    Data_Get_Struct(self, OlmAccount, this);
    
    size = olm_account_identity_keys_length(this);
    uint8_t buf[size];
    
    if(olm_account_identity_keys(this, buf, size) != size){
        
        raise_olm_error(olm_account_last_error(this));
    }
    
    return rb_funcall(rb_eval_string("JSON"), rb_intern("parse"), 1, rb_str_new((char *)buf, size));    
}

#ik(type = 'curve25519') ⇒ String

Parameters:

  • defaults (type)

    to ‘curve25519’

Returns:

  • (String)

Raises:

  • (RangeError)


14
15
16
17
# File 'lib/ruby_olm/account.rb', line 14

def ik(type='curve25519')
  raise RangeError unless identity_keys[type]
  identity_keys[type]
end

#inbound_session(*args) ⇒ Object



194
195
196
197
198
199
200
201
# File 'ext/ruby_olm/ext_lib_olm/ext_account.c', line 194

static VALUE inbound_session(int argc, VALUE *argv, VALUE self)
{
    VALUE args[] = {self, Qnil, Qnil};
    
    (void)rb_scan_args(argc, argv, "11", &args[1], &args[2]);
    
    return rb_class_new_instance(sizeof(args)/sizeof(*args), args, rb_eval_string("RubyOlm::InboundSession"));
}

#last_errorObject



4
5
6
7
8
9
10
# File 'ext/ruby_olm/ext_lib_olm/ext_account.c', line 4

static VALUE last_error(VALUE self)
{
    OlmAccount *this;
    Data_Get_Struct(self, OlmAccount, this);
    
    return rb_funcall(rb_eval_string("RubyOlm::OlmError"), rb_intern("from_string"), 1, rb_str_new2(olm_account_last_error(this)));
}

#mark_keys_as_publishedObject Also known as: mark_otk



155
156
157
158
159
160
161
162
163
# File 'ext/ruby_olm/ext_lib_olm/ext_account.c', line 155

static VALUE mark_keys_as_published(VALUE self)
{
    OlmAccount *this;
    Data_Get_Struct(self, OlmAccount, this);
    
    (void)olm_account_mark_keys_as_published(this);
    
    return self; 
}

#max_number_of_one_time_keysObject Also known as: max_otk



165
166
167
168
169
170
171
# File 'ext/ruby_olm/ext_lib_olm/ext_account.c', line 165

static VALUE max_number_of_one_time_keys(VALUE self)
{
    OlmAccount *this;
    Data_Get_Struct(self, OlmAccount, this);
    
    return SIZET2NUM(olm_account_max_number_of_one_time_keys(this));
}

#one_time_keysObject



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'ext/ruby_olm/ext_lib_olm/ext_account.c', line 99

static VALUE one_time_keys(VALUE self)
{
    VALUE retval;
    size_t size;
    void *ptr;    
    OlmAccount *this;
    Data_Get_Struct(self, OlmAccount, this);
    
    size = olm_account_one_time_keys_length(this);
    
    if((ptr = malloc(size)) == NULL){
        
        rb_raise(rb_eNoMemError, "%s()", __FUNCTION__);
    }
    
    if(olm_account_one_time_keys(this, ptr, size) != size){
        
        free(ptr);
        raise_olm_error(olm_account_last_error(this));
    }
    
    retval = rb_funcall(rb_eval_string("JSON"), rb_intern("parse"), 1, rb_str_new(ptr, size));
    
    free(ptr);
    
    return retval;
}

#otk(values = true, type = 'curve25519') ⇒ Array<Hash>, Array<String>

Parameters:

  • defaults (type)

    to ‘curve25519’

  • return (value)

    only the key values and not the ids

Returns:

  • (Array<Hash>)

    if !values

  • (Array<String>)

    if values



24
25
26
27
28
29
30
# File 'lib/ruby_olm/account.rb', line 24

def otk(values=true, type='curve25519')
  if values
    one_time_keys[type].values
  else
    one_time_keys[type]
  end
end

#outbound_session(identity_key, pre_key) ⇒ Object



189
190
191
192
# File 'ext/ruby_olm/ext_lib_olm/ext_account.c', line 189

static VALUE outbound_session(VALUE self, VALUE identity_key, VALUE pre_key)
{
    return rb_funcall(rb_eval_string("RubyOlm::OutboundSession"), rb_intern("new"), 3, self, identity_key, pre_key);    
}

#remove_one_time_keys(session) ⇒ Object Also known as: update_otk



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'ext/ruby_olm/ext_lib_olm/ext_account.c', line 173

static VALUE remove_one_time_keys(VALUE self, VALUE session)
{
    OlmAccount *this;
    Data_Get_Struct(self, OlmAccount, this);
    
    OlmSession *s;
    Data_Get_Struct(session, OlmSession, s);
    
    if(olm_remove_one_time_keys(this, s) == olm_error()){
        
        raise_olm_error(olm_account_last_error(this));
    }
       
    return self;
}

#sign(message) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'ext/ruby_olm/ext_lib_olm/ext_account.c', line 127

static VALUE sign(VALUE self, VALUE message)
{
    VALUE retval;
    size_t size;
    void *ptr;    
    OlmAccount *this;
    Data_Get_Struct(self, OlmAccount, this);
    
    size = olm_account_signature_length(this);
    
    if((ptr = malloc(size)) == NULL){
        
        rb_raise(rb_eNoMemError, "%s()", __FUNCTION__);
    }
    
    if(olm_account_sign(this, RSTRING_PTR(message), RSTRING_LEN(message), ptr, size) == olm_error()){
        
        free(ptr);
        raise_olm_error(olm_account_last_error(this));
    }
    
    retval = rb_str_new(ptr, size);
    
    free(ptr);
    
    return retval;
}

#to_pickle(*args) ⇒ Object



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'ext/ruby_olm/ext_lib_olm/ext_account.c', line 203

static VALUE to_pickle(int argc, VALUE *argv, VALUE self)
{
    VALUE password, retval;
    OlmAccount *this;
    void *ptr;
    size_t size;
    Data_Get_Struct(self, OlmAccount, this);
    
    (void)rb_scan_args(argc, argv, "01", &password);
    
    password = (password == Qnil) ? rb_str_new2("") : password;
   
    size = olm_pickle_account_length(this);
   
    if((ptr = malloc(size)) == NULL){
        
        rb_raise(rb_eNoMemError, "%s()", __FUNCTION__);
    }
    
    if(olm_pickle_account(this, RSTRING_PTR(password), RSTRING_LEN(password), ptr, size) != size){
        
        free(ptr);
        raise_olm_error(olm_account_last_error(this));
    } 
    
    retval = rb_str_new(ptr, size);
    
    free(ptr);
    
    return retval;
}