stellar_base-rails 0.1.2 → 0.2.0

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: 3148ee962db068aa1a50ba957044611996c6e0462e68662d77881e3a2af71d26
4
- data.tar.gz: 6e4dd4193b3d808549cc45240f944c74753243c93890287120a2aa51c9fd5416
3
+ metadata.gz: 86c1a220ccc8dec7052bd575a4fbdc8b477ef714a8e99c0ba0f1d08ccd8e92ff
4
+ data.tar.gz: eb4b64fe63c1d8602ff823496d24da6793acbdb2e540665c0d4097e0e0267947
5
5
  SHA512:
6
- metadata.gz: ee9563139a43b42ffd370d49a1d5d77e93fc8d82451b3669ec0eea4fd92196ed567bad0bf5877345b9b1fe3255051c19793f4efead14cb7a999db7164859aef7
7
- data.tar.gz: 8b8580584eec84ac4d87f12f5712880fa5b4e74c4f98f88712750004a2f7bc5a78f26483e873f85a62ed9b8bddebdd126ba31005dd8fa670e816ce6b6f60487d
6
+ metadata.gz: be387cda44d420b25fc46f98f0e786cb1a74f38ae15020d658f86979575cd78ba3be480a785813f625cc930d176a72b21c29a1aa33cc3ab44c2f32f1e422ea48
7
+ data.tar.gz: 84ea52aea9cb4e759e1236f18909af28972e9d163c5c6dc03f0790308ce75e7b0cb8369690a5e12d7e2b96a27f06254fe38c24aafd46d287a7a1327f9fb56d4d
data/README.md CHANGED
@@ -19,18 +19,39 @@ Create an initializer in your rails application:
19
19
 
20
20
  StellarBase.configure do |c|
21
21
  c.modules = %i(bridge_callbacks)
22
+ c.horizon_url = "https://horizon.stellar.org"
23
+
24
+ c.check_bridge_callbacks_authenticity = true
22
25
  c.on_bridge_callback = "StellarBridgeReceive::SaveTxn"
23
26
  end
24
27
  ```
25
28
 
29
+
26
30
  #### c.modules
31
+ - Value(s): array of symbols
32
+ - bridge_callbacks - when supplied this will mount the `/bridge_callbacks` endpoint
33
+ - Default: `%i(bridge_callbacks)`
27
34
  - You can supply what endpoints you want to activate with the gem
28
35
  - `bridge_callbacks` - this will mount a HTTP/S POST endpoint that acts as callback receiver for bridge server payments on the path. It will call your `.on_bridge_callback` class.
29
36
 
30
37
  #### c.on_bridge_callback
38
+ - Value(s): Class
39
+ - Default: None
31
40
  - Once the bridge_receive endpoint receives a callback, the class will be called with .call
32
41
  - The class will be passed with the bridge server callback payload contained in a `StellarBase::BridgeCallback` object.
33
42
  - The class will be expected to return a boolean, return true if the callback was processed properly
43
+ - Warning: The bridge server will may post multiple callbacks with the same ID, make sure you handle these correctly. https://github.com/stellar/bridge-server/blob/master/readme_bridge.md#callbacksreceive
44
+
45
+
46
+ #### c.check_bridge_callbacks_authenticity
47
+ - Value(s): `true` or `false`
48
+ - Default: `false`
49
+ - This secures the `/bridge_callbacks` endpoint from fake transactions by checking the transaction ID and it's contents against the Stellar Blockchain. If it doesn't add up, `/bridge_callbacks` endpoint will respond with a 422
50
+
51
+ #### c.horizon_url
52
+ - Value(s): String, url to horizon
53
+ - Default: https://horizon.stellar.org
54
+ - This is where the engine will check bridge callbacks if `c.cross_reference_bridge_callback` is turned on
34
55
 
35
56
  ## Installation
36
57
  Add this line to your application's Gemfile:
@@ -14,6 +14,33 @@ module StellarBase
14
14
  property :data
15
15
  property :transaction_id
16
16
 
17
+ validate :check_callback_authenticity
18
+
19
+ def check_callback_authenticity
20
+ if !StellarBase.configuration.check_bridge_callbacks_authenticity
21
+ return
22
+ end
23
+
24
+ result = BridgeCallbacks::Check.({
25
+ operation_id: id,
26
+ transaction_id: transaction_id,
27
+ params: {
28
+ id: id,
29
+ from: from,
30
+ route: route,
31
+ amount: amount,
32
+ asset_code: asset_code,
33
+ asset_issuer: asset_issuer,
34
+ memo: memo,
35
+ memo_type: memo_type,
36
+ data: data,
37
+ transaction_id: transaction_id,
38
+ },
39
+ })
40
+
41
+ errors.add(:base, result.message) if result.failure?
42
+ end
43
+
17
44
  end
18
45
  end
19
46
  end
@@ -1,27 +1,17 @@
1
1
  module StellarBase
2
- class BridgeCallback < ApplicationDataModel
2
+ class BridgeCallback
3
+ include Virtus.model
3
4
  # Attributes and params from
4
5
  # https://github.com/stellar/bridge-server/blob/master/readme_bridge.md#callbacksreceive
5
- attribute :id, DryTypes::String.optional
6
- attribute :from, DryTypes::String.optional
7
- attribute :route, DryTypes::String.optional
8
- attribute :amount, DryTypes::Decimal.optional
9
- attribute :asset_code, DryTypes::String.optional
10
- attribute :asset_issuer, DryTypes::String.optional
11
- attribute :memo_type, DryTypes::String.optional
12
- attribute :memo, DryTypes::String.optional
13
- attribute :data, DryTypes::String.optional
14
- attribute :transaction_id, DryTypes::String.optional
15
-
16
- attr_writer :id
17
- attr_writer :from
18
- attr_writer :route
19
- attr_writer :amount
20
- attr_writer :asset_code
21
- attr_writer :asset_issuer
22
- attr_writer :memo_type
23
- attr_writer :memo
24
- attr_writer :data
25
- attr_writer :transaction_id
6
+ attribute :id, String
7
+ attribute :from, String
8
+ attribute :route, String
9
+ attribute :amount, Float
10
+ attribute :asset_code, String
11
+ attribute :asset_issuer, String
12
+ attribute :memo_type, String
13
+ attribute :memo, String
14
+ attribute :data, String
15
+ attribute :transaction_id, String
26
16
  end
27
17
  end
@@ -0,0 +1,16 @@
1
+ module StellarBase
2
+ module BridgeCallbacks
3
+ class Check
4
+ extend LightService::Organizer
5
+
6
+ def self.call(opts = {})
7
+ with(opts).reduce(
8
+ InitializeHorizonClient,
9
+ GetOperation,
10
+ GetTransaction,
11
+ )
12
+ end
13
+
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,18 @@
1
+ module StellarBase
2
+ module BridgeCallbacks
3
+ class GetOperation
4
+ extend LightService::Action
5
+ expects :operation_id, :client
6
+ promises :operation_response
7
+
8
+ executed do |c|
9
+ id = c.operation_id
10
+ response = c.client.get_operation(id)
11
+
12
+ c.fail_and_return! "Operation ##{id} doesn't exist" if !response.present?
13
+
14
+ c.operation_response = response
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,21 @@
1
+ module StellarBase
2
+ module BridgeCallbacks
3
+ class GetTransaction
4
+ extend LightService::Action
5
+ expects :client, :transaction_id
6
+ promises :transaction_response
7
+
8
+ executed do |c|
9
+ id = c.transaction_id
10
+ response = c.client.get_transaction(id)
11
+
12
+ if !response.present?
13
+ c.fail_and_return! "Transaction ##{id} doesn't exist"
14
+ end
15
+
16
+ c.transaction_response = response
17
+ end
18
+
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,13 @@
1
+ module StellarBase
2
+ module BridgeCallbacks
3
+ class InitializeHorizonClient
4
+ extend LightService::Action
5
+
6
+ promises :client
7
+
8
+ executed do |c|
9
+ c.client = HorizonClient.new
10
+ end
11
+ end
12
+ end
13
+ end
data/lib/stellar_base.rb CHANGED
@@ -1,17 +1,21 @@
1
1
  require "gem_config"
2
2
  require "light-service"
3
- require "dry-struct"
3
+ require "virtus"
4
+ require "httparty"
4
5
  require "trailblazer-rails"
5
6
  require "disposable"
6
7
  require "reform"
7
8
  require "reform/form/coercion"
9
+
8
10
  require "stellar_base/engine"
9
11
 
10
12
  module StellarBase
11
13
  include GemConfig::Base
12
14
 
13
15
  with_configuration do
16
+ has :horizon_url, default: "https://horizon.stellar.org"
14
17
  has :modules, default: [:bridge_callbacks]
18
+ has :check_bridge_callbacks_authenticity, default: false
15
19
  has :on_bridge_callback
16
20
  end
17
21
 
@@ -20,4 +24,4 @@ module StellarBase
20
24
  end
21
25
  end
22
26
 
23
-
27
+ require "stellar_base/horizon_client"
@@ -0,0 +1,29 @@
1
+ module StellarBase
2
+ class HorizonClient
3
+ def get_operation(operation_id)
4
+ response = HTTParty.get(horizon_operations_path(operation_id))
5
+
6
+ JSON.parse(response.body) if response.code == 200
7
+ end
8
+
9
+ def get_transaction(transaction_id)
10
+ response = HTTParty.get(horizon_transactions_path(transaction_id))
11
+
12
+ JSON.parse(response.body) if response.code == 200
13
+ end
14
+
15
+ private
16
+
17
+ def horizon_transactions_path(transaction_id)
18
+ "#{base_url}/transactions/#{transaction_id}"
19
+ end
20
+
21
+ def horizon_operations_path(operation_id)
22
+ "#{base_url}/operations/#{operation_id}"
23
+ end
24
+
25
+ def base_url
26
+ StellarBase.configuration.horizon_url
27
+ end
28
+ end
29
+ end
@@ -1,3 +1,3 @@
1
1
  module StellarBase
2
- VERSION = '0.1.2'
2
+ VERSION = '0.2.0'
3
3
  end
metadata CHANGED
@@ -1,17 +1,17 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: stellar_base-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ace Subido
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-05-03 00:00:00.000000000 Z
11
+ date: 2018-05-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: dry-struct
14
+ name: virtus
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - ">="
@@ -66,6 +66,20 @@ dependencies:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
68
  version: '5.1'
69
+ - !ruby/object:Gem::Dependency
70
+ name: dry-types
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
69
83
  - !ruby/object:Gem::Dependency
70
84
  name: trailblazer
71
85
  requirement: !ruby/object:Gem::Requirement
@@ -94,6 +108,20 @@ dependencies:
94
108
  - - ">="
95
109
  - !ruby/object:Gem::Version
96
110
  version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: httparty
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
97
125
  - !ruby/object:Gem::Dependency
98
126
  name: sqlite3
99
127
  requirement: !ruby/object:Gem::Requirement
@@ -122,6 +150,34 @@ dependencies:
122
150
  - - ">="
123
151
  - !ruby/object:Gem::Version
124
152
  version: '0'
153
+ - !ruby/object:Gem::Dependency
154
+ name: vcr
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - "~>"
158
+ - !ruby/object:Gem::Version
159
+ version: '4.0'
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - "~>"
165
+ - !ruby/object:Gem::Version
166
+ version: '4.0'
167
+ - !ruby/object:Gem::Dependency
168
+ name: webmock
169
+ requirement: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - ">="
172
+ - !ruby/object:Gem::Version
173
+ version: '0'
174
+ type: :development
175
+ prerelease: false
176
+ version_requirements: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - ">="
179
+ - !ruby/object:Gem::Version
180
+ version: '0'
125
181
  description: API Endpoints for the Stellar Protocol
126
182
  email:
127
183
  - ace.subido@gmail.com
@@ -144,15 +200,19 @@ files:
144
200
  - app/helpers/stellar_base/application_helper.rb
145
201
  - app/jobs/stellar_base/application_job.rb
146
202
  - app/mailers/stellar_base/application_mailer.rb
147
- - app/models/stellar_base/application_data_model.rb
148
203
  - app/models/stellar_base/application_record.rb
149
204
  - app/models/stellar_base/bridge_callback.rb
205
+ - app/services/stellar_base/bridge_callbacks/check.rb
206
+ - app/services/stellar_base/bridge_callbacks/get_operation.rb
207
+ - app/services/stellar_base/bridge_callbacks/get_transaction.rb
208
+ - app/services/stellar_base/bridge_callbacks/initialize_horizon_client.rb
150
209
  - app/services/stellar_base/bridge_callbacks/process.rb
151
210
  - app/views/layouts/stellar_base/application.html.erb
152
211
  - config/routes.rb
153
212
  - lib/stellar_base-rails.rb
154
213
  - lib/stellar_base.rb
155
214
  - lib/stellar_base/engine.rb
215
+ - lib/stellar_base/horizon_client.rb
156
216
  - lib/stellar_base/version.rb
157
217
  - lib/tasks/stellar_base_tasks.rake
158
218
  homepage: https://github.com/bloom-solutions/stellar_base-rails
@@ -1,9 +0,0 @@
1
- module StellarBase
2
- module DryTypes
3
- include Dry::Types.module
4
- end
5
-
6
- class ApplicationDataModel < Dry::Struct
7
- constructor_type :schema
8
- end
9
- end