-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathaccount.go
160 lines (128 loc) · 4.15 KB
/
account.go
1
2
3
4
5
6
7
8
9
10
11
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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
126
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
154
155
156
157
158
159
160
package libolm-go
/*
#cgo LDFLAGS: -lolm
#include <olm/olm.h>
#include <stdlib.h>
*/
import "C"
import (
//"encoding/json"
"fmt"
"crypto/rand"
"unsafe"
)
func GetVersion() (major byte, minor byte, patch byte){
var ma C.uint8_t
var mi C.uint8_t
var pa C.uint8_t
C.olm_get_library_version(&ma, &mi, &pa)
return byte(ma), byte(mi), byte(pa)
}
type Account struct {
buf []byte
ptr *C.struct_OlmAccount
}
func AccountFromPickle(key string, pickle string) (Account){
account := newAccount()
key_buf := []byte(key)
pickle_buffer := []byte(pickle)
// this returns a result we should probably inspect
C.olm_unpickle_account(
account.ptr,
unsafe.Pointer(&key_buf[0]), C.size_t(len(key_buf)),
unsafe.Pointer(&pickle_buffer[0]), C.size_t(len(pickle_buffer)),
)
fmt.Println(account.lastError())
return account
}
func newAccount() (Account){
account_buf := make([]byte, C.olm_account_size())
olm_account := C.olm_account(unsafe.Pointer(&account_buf[0]))
return Account{buf: account_buf, ptr: olm_account}
}
func CreateNewAccount() (Account){
account := newAccount()
rand_len := C.olm_create_account_random_length(account.ptr)
rand_buf := make([]byte, rand_len)
_, err := rand.Read(rand_buf)
if err != nil {
// currently we panic when we don't have enough randomness but it might
// be better to return an error instead. however I feel like other
// programmers might not recognize what a huge issue not having
// randomness is so I chose the crash and burn approach
panic(err)
}
fmt.Println(account.lastError())
C.olm_create_account(account.ptr, unsafe.Pointer(&rand_buf[0]), rand_len)
return account
}
func (a Account) lastError() (string){
return C.GoString(C.olm_account_last_error(a.ptr))
}
func (a Account) Pickle(key string) (string){
key_buf := []byte(key)
pickle_buffer := make([]byte, C.olm_pickle_account_length(a.ptr))
// this returns a result we should probably inspect
C.olm_pickle_account(
a.ptr,
unsafe.Pointer(&key_buf[0]), C.size_t(len(key_buf)),
unsafe.Pointer(&pickle_buffer[0]), C.size_t(len(pickle_buffer)),
)
return string(pickle_buffer)
}
func (a Account) GetIdentityKeys() (string){
out_length := C.olm_account_identity_keys_length(a.ptr)
out_buffer := make([]byte, out_length)
C.olm_account_identity_keys(
a.ptr,
unsafe.Pointer(&out_buffer[0]), out_length,
)
// JSON, could parse
return string(out_buffer)
}
func (a Account) Sign(message string) (string){
message_buf := []byte(message)
out_length := C.olm_account_signature_length(a.ptr)
out_buffer := make([]byte, out_length)
C.olm_account_sign(
a.ptr,
unsafe.Pointer(&message_buf[0]), C.size_t(len(message_buf)),
unsafe.Pointer(&out_buffer[0]), out_length,
)
return string(out_buffer)
}
func (a Account) GetOneTimeKeys() (string){
out_length := C.olm_account_one_time_keys_length(a.ptr)
out_buffer := make([]byte, out_length)
C.olm_account_one_time_keys(
a.ptr,
unsafe.Pointer(&out_buffer[0]), out_length,
)
// JSON, could parse
return string(out_buffer)
}
func (a Account) MarkKeysAsPublished() {
C.olm_account_mark_keys_as_published(a.ptr)
}
func (a Account) GetMaxNumberOfOneTimeKeys() (int){
return int(C.olm_account_mark_keys_as_published(a.ptr))
}
func (a Account) GenerateOneTimeKeys(count int){
rand_len := C.olm_account_generate_one_time_keys_random_length(
a.ptr, C.size_t(count),
)
rand_buf := make([]byte, rand_len)
_, err := rand.Read(rand_buf)
if err != nil {
// currently we panic when we don't have enough randomness but it might
// be better to return an error instead. however I feel like other
// programmers might not recognize what a huge issue not having
// randomness is so I chose the crash and burn approach
panic(err)
}
C.olm_account_generate_one_time_keys(
a.ptr, C.size_t(count),
unsafe.Pointer(&rand_buf[0]), rand_len,
)
fmt.Println(a.lastError())
}