Skip to content

Commit 7eac5ed

Browse files
Merge branch 'main' into okapi-349-eslint
2 parents e2a0167 + 233f14b commit 7eac5ed

25 files changed

+1554
-1094
lines changed

ruby/.rubocop.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
AllCops:
2+
Exclude:
3+
- '**/*.rbi' # Don't look at sorbet type hints
4+
- 'lib/okapi/**/*.rb' # don't check the generated code
5+
- 'lib/pbmse/**/*.rb' # don't check the generated code

ruby/lib/did_comm.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# frozen_string_literal: true
2+
3+
# DidComm module
4+
module DidComm
5+
def self.pack(request)
6+
Okapi.verify_type(request, Transport_V1::PackRequest)
7+
Okapi.ffi_call('didcomm_pack', request, Transport_V1::PackResponse)
8+
end
9+
10+
def self.unpack(request)
11+
Okapi.verify_type(request, Transport_V1::UnpackRequest)
12+
Okapi.ffi_call('didcomm_unpack', request, Transport_V1::UnpackResponse)
13+
end
14+
15+
def self.sign(request)
16+
Okapi.verify_type(request, Transport_V1::SignRequest)
17+
Okapi.ffi_call('didcomm_sign', request, Transport_V1::SignResponse)
18+
end
19+
20+
def self.verify(request)
21+
Okapi.verify_type(request, Transport_V1::VerifyRequest)
22+
Okapi.ffi_call('didcomm_verify', request, Transport_V1::VerifyResponse)
23+
end
24+
end

ruby/lib/did_key.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# frozen_string_literal: true
2+
3+
# DidKey module
4+
module DidKey
5+
def self.generate(request)
6+
Okapi.verify_type(request, Keys_V1::GenerateKeyRequest)
7+
Okapi.ffi_call('didkey_generate', request, Keys_V1::GenerateKeyResponse)
8+
end
9+
10+
def self.resolve(request)
11+
Okapi.verify_type(request, Keys_V1::ResolveRequest)
12+
Okapi.ffi_call('didkey_resolve', request, Keys_V1::ResolveResponse)
13+
end
14+
end

ruby/lib/hashing.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# frozen_string_literal: true
2+
3+
# Hashing module
4+
module Hashing
5+
def self.blake3_hash(request)
6+
Okapi.verify_type(request, Hashing_V1::Blake3HashRequest)
7+
Okapi.ffi_call('blake3_hash', request, Hashing_V1::Blake3HashResponse)
8+
end
9+
10+
def self.blake3_keyed_hash(request)
11+
Okapi.verify_type(request, Hashing_V1::Blake3KeyedHashRequest)
12+
Okapi.ffi_call('blake3_keyed_hash', request, Hashing_V1::Blake3KeyedHashResponse)
13+
end
14+
15+
def self.blake3_derive_key(request)
16+
Okapi.verify_type(request, Hashing_V1::Blake3DeriveKeyRequest)
17+
Okapi.ffi_call('blake3_derive_key', request, Hashing_V1::Blake3DeriveKeyResponse)
18+
end
19+
20+
def self.sha256_hash(request)
21+
Okapi.verify_type(request, Hashing_V1::SHA256HashRequest)
22+
Okapi.ffi_call('sha256_hash', request, Hashing_V1::SHA256HashResponse)
23+
end
24+
end

ruby/lib/ld_proofs.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# frozen_string_literal: true
2+
3+
# LdProofs module
4+
module LdProofs
5+
def self.create(request)
6+
Okapi.verify_type(request, Proofs_V1::CreateProofRequest)
7+
Okapi.ffi_call('ldproofs_create_proof', request, Proofs_V1::CreateProofResponse)
8+
end
9+
10+
def self.verify(request)
11+
Okapi.verify_type(request, Proofs_V1::VerifyProofRequest)
12+
Okapi.ffi_call('ldproofs_verify_proof', request, Proofs_V1::VerifyProofResponse)
13+
end
14+
end

ruby/lib/oberon.rb

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# frozen_string_literal: true
2+
3+
# Oberon module
4+
module Oberon
5+
def self.create_key(request)
6+
Okapi.verify_type(request, Security_V1::CreateOberonKeyRequest)
7+
Okapi.ffi_call('oberon_create_key', request, Security_V1::CreateOberonKeyResponse)
8+
end
9+
10+
def self.create_token(request)
11+
Okapi.verify_type(request, Security_V1::CreateOberonTokenRequest)
12+
Okapi.ffi_call('oberon_create_token', request, Security_V1::CreateOberonTokenResponse)
13+
end
14+
15+
def self.blind_token(request)
16+
Okapi.verify_type(request, Security_V1::BlindOberonTokenRequest)
17+
Okapi.ffi_call('oberon_blind_token', request, Security_V1::BlindOberonTokenResponse)
18+
end
19+
20+
def self.unblind_token(request)
21+
Okapi.verify_type(request, Security_V1::UnBlindOberonTokenRequest)
22+
Okapi.ffi_call('oberon_unblind_token', request, Security_V1::UnBlindOberonTokenResponse)
23+
end
24+
25+
def self.create_proof(request)
26+
Okapi.verify_type(request, Security_V1::CreateOberonProofRequest)
27+
Okapi.ffi_call('oberon_create_proof', request, Security_V1::CreateOberonProofResponse)
28+
end
29+
30+
def self.verify_proof(request)
31+
Okapi.verify_type(request, Security_V1::VerifyOberonProofRequest)
32+
Okapi.ffi_call('oberon_verify_proof', request, Security_V1::VerifyOberonProofResponse)
33+
end
34+
end

ruby/lib/okapi.rb

Lines changed: 30 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# frozen_string_literal: true
22

3+
require_relative 'ld_proofs'
34
require 'ffi'
45
require 'os'
56
require 'okapi/keys/v1/keys_pb'
@@ -15,16 +16,17 @@
1516
Security_V1 = Okapi::Security::V1
1617
Transport_V1 = Okapi::Transport::V1
1718

19+
# Okapi wrapper module
1820
module Okapi
1921
extend FFI::Library
20-
@@library_path = nil
21-
@@library_linked = false
22+
@library_path = nil
23+
@library_linked = false
2224
def self.library_path
23-
@@library_path
25+
@library_path
2426
end
2527

2628
def self.library_path=(path)
27-
@@library_path = path
29+
@library_path = path
2830
end
2931

3032
def self.library_directory
@@ -53,17 +55,20 @@ class ExternError < FFI::Struct
5355
:message, :string
5456
end
5557

58+
# rubocop:disable Metrics/MethodLength
59+
# rubocop:disable Metrics/AbcSize
5660
def self.load_native_library
57-
return if @@library_linked
61+
return if @library_linked
5862

59-
@@library_linked = true
63+
@library_linked = true
6064

6165
full_path = library_name
6266
full_path = File.expand_path(File.join(library_path, library_name)) unless library_path.nil?
6367
begin
6468
ffi_lib full_path
6569
rescue LoadError
66-
# Get the environment variable RUBY_DLL_PATH on all platforms as a failsafe, MacOS system integrity protection, I'm looking at you.
70+
# Get the environment variable RUBY_DLL_PATH on all platforms as a failsafe,
71+
# MacOS system integrity protection, I'm looking at you.
6772
full_path = File.expand_path(File.join(ENV['RUBY_DLL_PATH'], library_name))
6873
ffi_lib full_path
6974
end
@@ -95,22 +100,6 @@ def self.load_native_library
95100
attach_function :okapi_bytebuffer_free, [ByteBuffer.by_value], :int
96101
attach_function :okapi_string_free, [:pointer], :int
97102
end
98-
end
99-
100-
module Okapi
101-
def self.verify_type(arg, klass)
102-
raise "Argument type error is: #{arg.class}, should be #{klass}" unless arg.is_a?(klass)
103-
end
104-
105-
def self.byte_buffer_free(buffer)
106-
verify_type(buffer, Okapi::ByteBuffer)
107-
okapi_bytebuffer_free(buffer)
108-
end
109-
110-
def self.string_free(ptr)
111-
verify_type(ptr, Fiddle::Pointer)
112-
okapi_string_free(ptr)
113-
end
114103

115104
def self.ffi_call(function, request, response_klass)
116105
load_native_library
@@ -123,124 +112,38 @@ def self.ffi_call(function, request, response_klass)
123112
error = Okapi::ExternError.new
124113
extern_function = Okapi.method(function)
125114
response_value = extern_function.call(request_buffer, response_buffer, error)
126-
if response_value != 0
127-
raise Okapi::DidError.new(code: response_value,
115+
raise Okapi::DidError.new(code: response_value, msg: error[:message]) if response_value != 0
128116

129-
msg: error[:message])
130-
end
131117
response = response_klass.decode(response_buffer[:data].read_string(response_buffer[:len]))
132118
byte_buffer_free(response_buffer)
133119
response
134120
end
121+
# rubocop:enable Metrics/MethodLength
122+
# rubocop:enable Metrics/AbcSize
135123

136-
class DidError < StandardError
137-
def initialize(code = 0, msg = nil)
138-
@code = code
139-
super(msg)
140-
end
141-
142-
def to_s
143-
"code=#{@code} message=#{message}"
144-
end
145-
end
146-
147-
module DidComm
148-
def self.pack(request)
149-
Okapi.verify_type(request, Transport_V1::PackRequest)
150-
Okapi.ffi_call('didcomm_pack', request, Transport_V1::PackResponse)
151-
end
152-
153-
def self.unpack(request)
154-
Okapi.verify_type(request, Transport_V1::UnpackRequest)
155-
Okapi.ffi_call('didcomm_unpack', request, Transport_V1::UnpackResponse)
156-
end
157-
158-
def self.sign(request)
159-
Okapi.verify_type(request, Transport_V1::SignRequest)
160-
Okapi.ffi_call('didcomm_sign', request, Transport_V1::SignResponse)
161-
end
162-
163-
def self.verify(request)
164-
Okapi.verify_type(request, Transport_V1::VerifyRequest)
165-
Okapi.ffi_call('didcomm_verify', request, Transport_V1::VerifyResponse)
166-
end
167-
end
168-
169-
module DidKey
170-
def self.generate(request)
171-
Okapi.verify_type(request, Keys_V1::GenerateKeyRequest)
172-
Okapi.ffi_call('didkey_generate', request, Keys_V1::GenerateKeyResponse)
173-
end
174-
175-
def self.resolve(request)
176-
Okapi.verify_type(request, Keys_V1::ResolveRequest)
177-
Okapi.ffi_call('didkey_resolve', request, Keys_V1::ResolveResponse)
178-
end
124+
def self.verify_type(arg, klass)
125+
raise "Argument type error is: #{arg.class}, should be #{klass}" unless arg.is_a?(klass)
179126
end
180127

181-
module LdProofs
182-
def self.create(request)
183-
Okapi.verify_type(request, Proofs_V1::CreateProofRequest)
184-
Okapi.ffi_call('ldproofs_create_proof', request, Proofs_V1::CreateProofResponse)
185-
end
186-
187-
def self.verify(request)
188-
Okapi.verify_type(request, Proofs_V1::VerifyProofRequest)
189-
Okapi.ffi_call('ldproofs_verify_proof', request, Proofs_V1::VerifyProofResponse)
190-
end
128+
def self.byte_buffer_free(buffer)
129+
verify_type(buffer, Okapi::ByteBuffer)
130+
okapi_bytebuffer_free(buffer)
191131
end
192132

193-
module Oberon
194-
def self.create_key(request)
195-
Okapi.verify_type(request, Security_V1::CreateOberonKeyRequest)
196-
Okapi.ffi_call('oberon_create_key', request, Security_V1::CreateOberonKeyResponse)
197-
end
198-
199-
def self.create_token(request)
200-
Okapi.verify_type(request, Security_V1::CreateOberonTokenRequest)
201-
Okapi.ffi_call('oberon_create_token', request, Security_V1::CreateOberonTokenResponse)
202-
end
203-
204-
def self.blind_token(request)
205-
Okapi.verify_type(request, Security_V1::BlindOberonTokenRequest)
206-
Okapi.ffi_call('oberon_blind_token', request, Security_V1::BlindOberonTokenResponse)
207-
end
208-
209-
def self.unblind_token(request)
210-
Okapi.verify_type(request, Security_V1::UnBlindOberonTokenRequest)
211-
Okapi.ffi_call('oberon_unblind_token', request, Security_V1::UnBlindOberonTokenResponse)
212-
end
213-
214-
def self.create_proof(request)
215-
Okapi.verify_type(request, Security_V1::CreateOberonProofRequest)
216-
Okapi.ffi_call('oberon_create_proof', request, Security_V1::CreateOberonProofResponse)
217-
end
218-
219-
def self.verify_proof(request)
220-
Okapi.verify_type(request, Security_V1::VerifyOberonProofRequest)
221-
Okapi.ffi_call('oberon_verify_proof', request, Security_V1::VerifyOberonProofResponse)
222-
end
133+
def self.string_free(ptr)
134+
verify_type(ptr, Fiddle::Pointer)
135+
okapi_string_free(ptr)
223136
end
224137

225-
module Hashing
226-
def self.blake3_hash(request)
227-
Okapi.verify_type(request, Hashing_V1::Blake3HashRequest)
228-
Okapi.ffi_call('blake3_hash', request, Hashing_V1::Blake3HashResponse)
229-
end
230-
231-
def self.blake3_keyed_hash(request)
232-
Okapi.verify_type(request, Hashing_V1::Blake3KeyedHashRequest)
233-
Okapi.ffi_call('blake3_keyed_hash', request, Hashing_V1::Blake3KeyedHashResponse)
234-
end
235-
236-
def self.blake3_derive_key(request)
237-
Okapi.verify_type(request, Hashing_V1::Blake3DeriveKeyRequest)
238-
Okapi.ffi_call('blake3_derive_key', request, Hashing_V1::Blake3DeriveKeyResponse)
138+
# Wrapping error for failures in the native code
139+
class DidError < StandardError
140+
def initialize(code = 0, msg = '')
141+
@code = code
142+
super(msg)
239143
end
240144

241-
def self.sha256_hash(request)
242-
Okapi.verify_type(request, Hashing_V1::SHA256HashRequest)
243-
Okapi.ffi_call('sha256_hash', request, Hashing_V1::SHA256HashResponse)
145+
def to_s
146+
"code=#{@code} message=#{message}"
244147
end
245148
end
246149
end

0 commit comments

Comments
 (0)