selfsdk 0.0.138 → 0.0.143

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d9cbb2bad8f28cb417c2df600279bed7de2730b4391a735574a953e15da32eb0
4
- data.tar.gz: caee509d09748376b2185de51e73e4cdc78b1790a2de323f90babcf94b2df1f3
3
+ metadata.gz: d317a3c78c03af0c8882cf280a3811060d97b5fbfa1dd6c2261bfe8c1246d209
4
+ data.tar.gz: e346b04c24246db81a29f4d366d4af858f3936961396a5459ce6ebc446bfa468
5
5
  SHA512:
6
- metadata.gz: 63b33c646f77f9f07e365e2c33f840eef746b7e7f564833a4598755d36da7f698cb8a01c24158a82de8477e3861a82ce5dea588a4bf2d0ba7ff92addb101601f
7
- data.tar.gz: d858ae95e76994ac51a2622f141a8b8bd47eeaebca0ed55842ae6fa4bd025debaeb557b51201fd63755da9425341c5c85737b551718b21ad61b4860199d6a40c
6
+ metadata.gz: 6bd2d793deae3a91747ded4371d99a54f35979c494993be3cdb3514aef520f7b83354852d932c4ecf057cb6a08c3cf9502ab974d820d0b9ae87ba13eb6bd337b
7
+ data.tar.gz: be302ede9fb1498e2b9572bbb7ff6b4ff2a4fce9dfb326f223462869e3956319b8582a925f299beed23c0937ff85f23077edcaac7c4e0240644e695a3265976d
@@ -66,19 +66,31 @@ module SelfSDK
66
66
  end
67
67
 
68
68
  def post(endpoint, body)
69
- p HTTParty.post("#{@self_url}#{endpoint}",
70
- headers: {
71
- 'Content-Type' => 'application/json',
72
- 'Authorization' => "Bearer #{@jwt.auth_token}"
73
- },
74
- body: body)
69
+ res = nil
70
+ loop do
71
+ res = HTTParty.post("#{@self_url}#{endpoint}",
72
+ headers: {
73
+ 'Content-Type' => 'application/json',
74
+ 'Authorization' => "Bearer #{@jwt.auth_token}"
75
+ },
76
+ body: body)
77
+ break if res.code != 503
78
+ sleep 2
79
+ end
80
+ return res
75
81
  end
76
82
 
77
83
  def get(endpoint)
78
- HTTParty.get("#{@self_url}#{endpoint}", headers: {
84
+ res = nil
85
+ loop do
86
+ res = HTTParty.get("#{@self_url}#{endpoint}", headers: {
79
87
  'Content-Type' => 'application/json',
80
88
  'Authorization' => "Bearer #{@jwt.auth_token}"
81
- })
89
+ })
90
+ break if res.code != 503
91
+ sleep 2
92
+ end
93
+ return res
82
94
  end
83
95
 
84
96
  # Lists all public keys stored on self for the given ID
@@ -90,6 +102,15 @@ module SelfSDK
90
102
  sg.key_by_id(kid)
91
103
  end
92
104
 
105
+ # Get the active public key for a device
106
+ #
107
+ # @param id [string] identity id
108
+ def device_public_key(id, did)
109
+ i = entity(id)
110
+ sg = SelfSDK::SignatureGraph.new(i[:history])
111
+ sg.key_by_device(did)
112
+ end
113
+
93
114
  private
94
115
 
95
116
  def get_identity(endpoint)
@@ -8,9 +8,9 @@ module SelfSDK
8
8
  @storage_key = storage_key
9
9
  @storage_folder = storage_folder
10
10
 
11
- if File.exist?('account.pickle')
11
+ if File.exist?(account_path)
12
12
  # 1a) if alice's account file exists load the pickle from the file
13
- @account = SelfCrypto::Account.from_pickle(File.read('account.pickle'), @storage_key)
13
+ @account = SelfCrypto::Account.from_pickle(File.read(account_path), @storage_key)
14
14
  else
15
15
  # 1b-i) if create a new account for alice if one doesn't exist already
16
16
  @account = SelfCrypto::Account.from_seed(@client.jwt.key)
@@ -25,12 +25,12 @@ module SelfSDK
25
25
  @client.post("/v1/apps/#{@client.jwt.id}/devices/#{@device}/pre_keys", keys)
26
26
 
27
27
  # 1b-v) store the account to a file
28
- File.write('account.pickle', @account.to_pickle(storage_key))
28
+ File.write(account_path, @account.to_pickle(storage_key))
29
29
  end
30
30
  end
31
31
 
32
32
  def encrypt(message, recipient, recipient_device)
33
- session_file_name = "#{recipient}:#{recipient_device}-session.pickle"
33
+ session_file_name = session_path(recipient, recipient_device)
34
34
 
35
35
  if File.exist?(session_file_name)
36
36
  # 2a) if bob's session file exists load the pickle from the file
@@ -38,7 +38,7 @@ module SelfSDK
38
38
  else
39
39
  # 2b-i) if you have not previously sent or recevied a message to/from bob,
40
40
  # you must get his identity key from GET /v1/identities/bob/
41
- ed25519_identity_key = @client.public_keys(recipient).first[:key]
41
+ ed25519_identity_key = @client.device_public_key(recipient, recipient_device)
42
42
 
43
43
  # 2b-ii) get a one time key for bob
44
44
  res = @client.get("/v1/identities/#{recipient}/devices/#{recipient_device}/pre_keys")
@@ -51,13 +51,10 @@ module SelfSDK
51
51
  one_time_key = JSON.parse(res.body)["key"]
52
52
 
53
53
  # 2b-iii) convert bobs ed25519 identity key to a curve25519 key
54
- curve25519_identity_key = SelfCrypto::Util.ed25519_pk_to_curve25519(ed25519_identity_key)
54
+ curve25519_identity_key = SelfCrypto::Util.ed25519_pk_to_curve25519(ed25519_identity_key.raw_public_key)
55
55
 
56
56
  # 2b-iv) create the session with bob
57
57
  session_with_bob = @account.outbound_session(curve25519_identity_key, one_time_key)
58
-
59
- # 2b-v) store the session to a file
60
- File.write(session_file_name, session_with_bob.to_pickle(@storage_key))
61
58
  end
62
59
 
63
60
  # 3) create a group session and set the identity of the account youre using
@@ -67,11 +64,16 @@ module SelfSDK
67
64
  gs.add_participant("#{recipient}:#{recipient_device}", session_with_bob)
68
65
 
69
66
  # 5) encrypt a message
70
- gs.encrypt(message).to_s
67
+ ct = gs.encrypt(message).to_s
68
+
69
+ # 6) store the session to a file
70
+ File.write(session_file_name, session_with_bob.to_pickle(@storage_key))
71
+
72
+ ct
71
73
  end
72
74
 
73
75
  def decrypt(message, sender, sender_device)
74
- session_file_name = "#{sender}:#{sender_device}-session.pickle"
76
+ session_file_name = session_path(sender, sender_device)
75
77
 
76
78
  if File.exist?(session_file_name)
77
79
  # 7a) if carol's session file exists load the pickle from the file
@@ -84,9 +86,6 @@ module SelfSDK
84
86
 
85
87
  # 7b-ii) use the initial message to create a session for bob or carol
86
88
  session_with_bob = @account.inbound_session(m)
87
-
88
- # 7b-iii) store the session to a file
89
- File.write(session_file_name, session_with_bob.to_pickle(@storage_key))
90
89
  end
91
90
 
92
91
  # 8) create a group session and set the identity of the account you're using
@@ -96,7 +95,22 @@ module SelfSDK
96
95
  gs.add_participant("#{sender}:#{sender_device}", session_with_bob)
97
96
 
98
97
  # 10) decrypt the message ciphertext
99
- gs.decrypt("#{sender}:#{sender_device}", message).to_s
98
+ pt = gs.decrypt("#{sender}:#{sender_device}", message).to_s
99
+
100
+ # 11) store the session to a file
101
+ File.write(session_file_name, session_with_bob.to_pickle(@storage_key))
102
+
103
+ pt
104
+ end
105
+
106
+ private
107
+
108
+ def account_path
109
+ "#{@storage_folder}/account.pickle"
110
+ end
111
+
112
+ def session_path(selfid, device)
113
+ "#{@storage_folder}/#{selfid}:#{device}-session.pickle"
100
114
  end
101
115
  end
102
116
  end
@@ -11,7 +11,7 @@ module SelfSDK
11
11
  body = if input.is_a? String
12
12
  input
13
13
  else
14
- issuer = input.recipient.split(":")
14
+ issuer = input.sender.split(":")
15
15
  messaging.encryption_client.decrypt(input.ciphertext, issuer.first, issuer.last)
16
16
  end
17
17
 
@@ -30,7 +30,7 @@ module SelfSDK
30
30
  # @params storage_folder [String] folder to perist messaging encryption
31
31
  # @option opts [Bool] :auto_reconnect Automatically reconnects to websocket if connection is lost (defaults to true).
32
32
  # @option opts [String] :device_id The device id to be used by the app defaults to "1".
33
- def initialize(url, client, storage_key, storage_folder, options = {})
33
+ def initialize(url, client, storage_key, options = {})
34
34
  @mon = Monitor.new
35
35
  @url = url
36
36
  @messages = {}
@@ -49,7 +49,7 @@ module SelfSDK
49
49
 
50
50
  FileUtils.mkdir_p @storage_dir unless File.exist? @storage_dir
51
51
  unless options.include? :no_crypto
52
- @encryption_client = Crypto.new(@client, @device_id, storage_folder, storage_key)
52
+ @encryption_client = Crypto.new(@client, @device_id, @storage_dir, storage_key)
53
53
  end
54
54
 
55
55
  if options.include? :ws
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: selfsdk
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.138
4
+ version: 0.0.143
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aldgate Ventures