open-core 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +15 -0
- data/.travis.yml +15 -0
- data/.yardopts +8 -0
- data/CONTRIBUTING.md +48 -0
- data/Gemfile +15 -0
- data/Guardfile +5 -0
- data/LICENSE.txt +202 -0
- data/README.md +83 -0
- data/Rakefile +4 -0
- data/examples/create_account.rb +26 -0
- data/examples/low_level_transaction_post.rb +46 -0
- data/examples/mid_level_transaction_post.rb +33 -0
- data/examples/non_native_payment.rb +60 -0
- data/examples/offer.rb +75 -0
- data/examples/transaction_merge.rb +23 -0
- data/generated/stellar-base-generated.rb +134 -0
- data/generated/stellar/account_entry.rb +53 -0
- data/generated/stellar/account_entry/ext.rb +24 -0
- data/generated/stellar/account_flags.rb +27 -0
- data/generated/stellar/account_merge_result.rb +26 -0
- data/generated/stellar/account_merge_result_code.rb +30 -0
- data/generated/stellar/allow_trust_op.rb +38 -0
- data/generated/stellar/allow_trust_op/asset.rb +33 -0
- data/generated/stellar/allow_trust_result.rb +25 -0
- data/generated/stellar/allow_trust_result_code.rb +31 -0
- data/generated/stellar/asset.rb +47 -0
- data/generated/stellar/asset/alpha_num12.rb +22 -0
- data/generated/stellar/asset/alpha_num4.rb +22 -0
- data/generated/stellar/asset_type.rb +24 -0
- data/generated/stellar/bucket_entry.rb +28 -0
- data/generated/stellar/bucket_entry_type.rb +22 -0
- data/generated/stellar/change_trust_op.rb +22 -0
- data/generated/stellar/change_trust_result.rb +25 -0
- data/generated/stellar/change_trust_result_code.rb +30 -0
- data/generated/stellar/claim_offer_atom.rb +33 -0
- data/generated/stellar/create_account_op.rb +20 -0
- data/generated/stellar/create_account_result.rb +25 -0
- data/generated/stellar/create_account_result_code.rb +32 -0
- data/generated/stellar/create_passive_offer_op.rb +24 -0
- data/generated/stellar/crypto_key_type.rb +20 -0
- data/generated/stellar/decorated_signature.rb +20 -0
- data/generated/stellar/dont_have.rb +20 -0
- data/generated/stellar/envelope_type.rb +22 -0
- data/generated/stellar/error.rb +20 -0
- data/generated/stellar/hello.rb +26 -0
- data/generated/stellar/inflation_payout.rb +20 -0
- data/generated/stellar/inflation_result.rb +26 -0
- data/generated/stellar/inflation_result_code.rb +24 -0
- data/generated/stellar/ledger_entry.rb +44 -0
- data/generated/stellar/ledger_entry/data.rb +33 -0
- data/generated/stellar/ledger_entry/ext.rb +24 -0
- data/generated/stellar/ledger_entry_change.rb +31 -0
- data/generated/stellar/ledger_entry_change_type.rb +24 -0
- data/generated/stellar/ledger_entry_type.rb +24 -0
- data/generated/stellar/ledger_header.rb +65 -0
- data/generated/stellar/ledger_header/ext.rb +24 -0
- data/generated/stellar/ledger_header_history_entry.rb +33 -0
- data/generated/stellar/ledger_header_history_entry/ext.rb +24 -0
- data/generated/stellar/ledger_key.rb +50 -0
- data/generated/stellar/ledger_key/account.rb +20 -0
- data/generated/stellar/ledger_key/offer.rb +22 -0
- data/generated/stellar/ledger_key/trust_line.rb +22 -0
- data/generated/stellar/ledger_upgrade.rb +27 -0
- data/generated/stellar/ledger_upgrade_type.rb +22 -0
- data/generated/stellar/manage_offer_effect.rb +24 -0
- data/generated/stellar/manage_offer_op.rb +28 -0
- data/generated/stellar/manage_offer_result.rb +26 -0
- data/generated/stellar/manage_offer_result_code.rb +48 -0
- data/generated/stellar/manage_offer_success_result.rb +34 -0
- data/generated/stellar/manage_offer_success_result/offer.rb +30 -0
- data/generated/stellar/memo.rb +38 -0
- data/generated/stellar/memo_type.rb +28 -0
- data/generated/stellar/message_type.rb +45 -0
- data/generated/stellar/offer_entry.rb +49 -0
- data/generated/stellar/offer_entry/ext.rb +24 -0
- data/generated/stellar/offer_entry_flags.rb +21 -0
- data/generated/stellar/operation.rb +51 -0
- data/generated/stellar/operation/body.rb +60 -0
- data/generated/stellar/operation_meta.rb +18 -0
- data/generated/stellar/operation_result.rb +53 -0
- data/generated/stellar/operation_result/tr.rb +61 -0
- data/generated/stellar/operation_result_code.rb +25 -0
- data/generated/stellar/operation_type.rb +38 -0
- data/generated/stellar/path_payment_op.rb +32 -0
- data/generated/stellar/path_payment_result.rb +34 -0
- data/generated/stellar/path_payment_result/success.rb +22 -0
- data/generated/stellar/path_payment_result_code.rb +45 -0
- data/generated/stellar/payment_op.rb +22 -0
- data/generated/stellar/payment_result.rb +25 -0
- data/generated/stellar/payment_result_code.rb +39 -0
- data/generated/stellar/peer_address.rb +22 -0
- data/generated/stellar/price.rb +20 -0
- data/generated/stellar/public_key.rb +23 -0
- data/generated/stellar/scp_ballot.rb +20 -0
- data/generated/stellar/scp_envelope.rb +20 -0
- data/generated/stellar/scp_nomination.rb +22 -0
- data/generated/stellar/scp_quorum_set.rb +22 -0
- data/generated/stellar/scp_statement.rb +59 -0
- data/generated/stellar/scp_statement/pledges.rb +64 -0
- data/generated/stellar/scp_statement/pledges/confirm.rb +28 -0
- data/generated/stellar/scp_statement/pledges/externalize.rb +28 -0
- data/generated/stellar/scp_statement/pledges/prepare.rb +32 -0
- data/generated/stellar/scp_statement_type.rb +26 -0
- data/generated/stellar/set_options_op.rb +41 -0
- data/generated/stellar/set_options_result.rb +25 -0
- data/generated/stellar/set_options_result_code.rb +38 -0
- data/generated/stellar/signer.rb +20 -0
- data/generated/stellar/simple_payment_result.rb +22 -0
- data/generated/stellar/stellar_message.rb +66 -0
- data/generated/stellar/stellar_value.rb +40 -0
- data/generated/stellar/stellar_value/ext.rb +24 -0
- data/generated/stellar/threshold_indexes.rb +26 -0
- data/generated/stellar/time_bounds.rb +20 -0
- data/generated/stellar/transaction.rb +50 -0
- data/generated/stellar/transaction/ext.rb +24 -0
- data/generated/stellar/transaction_envelope.rb +20 -0
- data/generated/stellar/transaction_history_entry.rb +33 -0
- data/generated/stellar/transaction_history_entry/ext.rb +24 -0
- data/generated/stellar/transaction_history_result_entry.rb +33 -0
- data/generated/stellar/transaction_history_result_entry/ext.rb +24 -0
- data/generated/stellar/transaction_meta.rb +31 -0
- data/generated/stellar/transaction_meta/v0.rb +22 -0
- data/generated/stellar/transaction_result.rb +43 -0
- data/generated/stellar/transaction_result/ext.rb +24 -0
- data/generated/stellar/transaction_result/result.rb +30 -0
- data/generated/stellar/transaction_result_code.rb +45 -0
- data/generated/stellar/transaction_result_pair.rb +20 -0
- data/generated/stellar/transaction_result_set.rb +18 -0
- data/generated/stellar/transaction_set.rb +20 -0
- data/generated/stellar/trust_line_entry.rb +41 -0
- data/generated/stellar/trust_line_entry/ext.rb +24 -0
- data/generated/stellar/trust_line_flags.rb +21 -0
- data/lib/stellar-base.rb +32 -0
- data/lib/stellar/account_flags.rb +28 -0
- data/lib/stellar/asset.rb +69 -0
- data/lib/stellar/base.rb +1 -0
- data/lib/stellar/base/version.rb +5 -0
- data/lib/stellar/convert.rb +32 -0
- data/lib/stellar/key_pair.rb +112 -0
- data/lib/stellar/networks.rb +45 -0
- data/lib/stellar/operation.rb +304 -0
- data/lib/stellar/path_payment_result.rb +17 -0
- data/lib/stellar/price.rb +32 -0
- data/lib/stellar/thresholds.rb +39 -0
- data/lib/stellar/transaction.rb +170 -0
- data/lib/stellar/transaction_envelope.rb +32 -0
- data/lib/stellar/util/continued_fraction.rb +96 -0
- data/lib/stellar/util/strkey.rb +43 -0
- data/open-core.gemspec +37 -0
- data/spec/lib/stellar/account_flags_spec.rb +19 -0
- data/spec/lib/stellar/asset_spec.rb +45 -0
- data/spec/lib/stellar/convert_spec.rb +61 -0
- data/spec/lib/stellar/key_pair_spec.rb +238 -0
- data/spec/lib/stellar/networks_spec.rb +77 -0
- data/spec/lib/stellar/path_payment_result_spec.rb +95 -0
- data/spec/lib/stellar/price_spec.rb +34 -0
- data/spec/lib/stellar/thresholds_spec.rb +62 -0
- data/spec/lib/stellar/transaction_envelope_spec.rb +93 -0
- data/spec/lib/stellar/transaction_spec.rb +54 -0
- data/spec/lib/stellar/util/strkey_spec.rb +46 -0
- data/spec/spec_helper.rb +16 -0
- data/spec/support/matchers/be_strkey.rb +9 -0
- data/spec/support/matchers/eq_bytes.rb +5 -0
- data/spec/support/matchers/have_length.rb +5 -0
- data/tasks/rspec.rake +6 -0
- data/tasks/travis.rake +1 -0
- data/tasks/xdr.rake +50 -0
- data/xdr/Stellar-SCP.x +87 -0
- data/xdr/Stellar-ledger-entries.x +222 -0
- data/xdr/Stellar-ledger.x +231 -0
- data/xdr/Stellar-overlay.x +87 -0
- data/xdr/Stellar-transaction.x +670 -0
- data/xdr/Stellar-types.x +34 -0
- metadata +444 -0
@@ -0,0 +1,17 @@
|
|
1
|
+
module Stellar
|
2
|
+
class PathPaymentResult
|
3
|
+
# send_amount returns the actual amount paid for the corresponding
|
4
|
+
# PathPaymentOp to this result.
|
5
|
+
#
|
6
|
+
def send_amount
|
7
|
+
s = success!
|
8
|
+
return s.last.amount if s.offers.blank?
|
9
|
+
|
10
|
+
source_asset = s.offers.first.asset_bought
|
11
|
+
source_offers = s.offers.take_while{|o| o.asset_bought == source_asset}
|
12
|
+
|
13
|
+
source_offers.map(&:amount_bought).sum
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
module Stellar
|
2
|
+
|
3
|
+
# reopen class
|
4
|
+
class Price
|
5
|
+
|
6
|
+
MAX_PRECISION = (2**31) - 1
|
7
|
+
|
8
|
+
def self.from_f(number)
|
9
|
+
best_r = Util::ContinuedFraction.best_r(number, MAX_PRECISION)
|
10
|
+
new({
|
11
|
+
n: best_r.numerator,
|
12
|
+
d: best_r.denominator
|
13
|
+
})
|
14
|
+
end
|
15
|
+
|
16
|
+
def invert
|
17
|
+
self.class.new(n:d,d:n)
|
18
|
+
end
|
19
|
+
|
20
|
+
def to_f
|
21
|
+
n / d.to_f
|
22
|
+
end
|
23
|
+
|
24
|
+
def to_s
|
25
|
+
"#{n} / #{d}"
|
26
|
+
end
|
27
|
+
|
28
|
+
def inspect
|
29
|
+
"#<Stellar::Price #{self}>"
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module Stellar
|
2
|
+
class << Thresholds
|
3
|
+
COMPONENTS = [:master_weight, :low, :medium, :high]
|
4
|
+
VALID_RANGE = 0..255
|
5
|
+
|
6
|
+
def make(thresholds={})
|
7
|
+
|
8
|
+
# error if any of the needed components are not provided
|
9
|
+
if COMPONENTS.any?{|c| thresholds[c].blank? }
|
10
|
+
raise ArgumentError, "invalid thresholds hash, must have #{COMPONENTS.inspect} keys, had: #{thresholds.keys.inspect}"
|
11
|
+
end
|
12
|
+
|
13
|
+
# error if any of the needed components are not numbers 0 <= N <= 255
|
14
|
+
COMPONENTS.each do |c|
|
15
|
+
good = true
|
16
|
+
|
17
|
+
good &&= thresholds[c].is_a?(Fixnum)
|
18
|
+
good &&= VALID_RANGE.include? thresholds[c]
|
19
|
+
|
20
|
+
unless good
|
21
|
+
raise ArgumentError, "invalid #{c.inspect}, must be number in (0..255), got #{thresholds[c].inspect}"
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
|
26
|
+
thresholds.values_at(*COMPONENTS).pack("C*")
|
27
|
+
end
|
28
|
+
|
29
|
+
def parse(combined)
|
30
|
+
master_weight, low, medium, high = combined.unpack("C*")
|
31
|
+
{
|
32
|
+
master_weight: master_weight,
|
33
|
+
low: low,
|
34
|
+
medium: medium,
|
35
|
+
high: high,
|
36
|
+
}
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,170 @@
|
|
1
|
+
module Stellar
|
2
|
+
class Transaction
|
3
|
+
|
4
|
+
#
|
5
|
+
# @see Stellar::Operation.payment
|
6
|
+
def self.payment(attributes={})
|
7
|
+
make :payment, attributes
|
8
|
+
end
|
9
|
+
|
10
|
+
#
|
11
|
+
# @see Stellar::Operation.path_payment
|
12
|
+
def self.path_payment(attributes={})
|
13
|
+
make :path_payment, attributes
|
14
|
+
end
|
15
|
+
|
16
|
+
#
|
17
|
+
# @see Stellar::Operation.create_account
|
18
|
+
def self.create_account(attributes={})
|
19
|
+
make :create_account, attributes
|
20
|
+
end
|
21
|
+
|
22
|
+
#
|
23
|
+
# @see Stellar::Operation.change_trust
|
24
|
+
def self.change_trust(attributes={})
|
25
|
+
make :change_trust, attributes
|
26
|
+
end
|
27
|
+
|
28
|
+
#
|
29
|
+
# @see Stellar::Operation.create_offer
|
30
|
+
def self.manage_offer(attributes={})
|
31
|
+
make :manage_offer, attributes
|
32
|
+
end
|
33
|
+
|
34
|
+
#
|
35
|
+
# @see Stellar::Operation.create_passive_offer
|
36
|
+
def self.create_passive_offer(attributes={})
|
37
|
+
make :create_passive_offer, attributes
|
38
|
+
end
|
39
|
+
|
40
|
+
#
|
41
|
+
# @see Stellar::Operation.set_options
|
42
|
+
def self.set_options(attributes={})
|
43
|
+
make :set_options, attributes
|
44
|
+
end
|
45
|
+
|
46
|
+
#
|
47
|
+
# @see Stellar::Operation.allow_trust
|
48
|
+
def self.allow_trust(attributes={})
|
49
|
+
make :allow_trust, attributes
|
50
|
+
end
|
51
|
+
|
52
|
+
#
|
53
|
+
# @see Stellar::Operation.account_merge
|
54
|
+
def self.account_merge(attributes={})
|
55
|
+
make :account_merge, attributes
|
56
|
+
end
|
57
|
+
|
58
|
+
#
|
59
|
+
# @see Stellar::Operation.inflation
|
60
|
+
def self.inflation(attributes={})
|
61
|
+
make :inflation, attributes
|
62
|
+
end
|
63
|
+
|
64
|
+
#
|
65
|
+
# Helper method to create a transaction with a single
|
66
|
+
# operation of the provided type. See class methods
|
67
|
+
# on Stellar::Operation for available values for
|
68
|
+
# operation_type.
|
69
|
+
#
|
70
|
+
# @see Stellar::Operation
|
71
|
+
#
|
72
|
+
# @param operation_type [Symbol] the operation to use
|
73
|
+
# @param attributes={} [Hash] attributes to use for both the transaction and the operation
|
74
|
+
#
|
75
|
+
# @return [Stellar::Transaction] the resulting transaction
|
76
|
+
def self.make(operation_type, attributes={})
|
77
|
+
for_account(attributes).tap do |result|
|
78
|
+
result.operations << Operation.send(operation_type, attributes)
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
|
83
|
+
#
|
84
|
+
# Helper method to create the skeleton of a transaction.
|
85
|
+
# The resulting transaction will have its source account,
|
86
|
+
# sequence, fee, min ledger and max ledger set.
|
87
|
+
#
|
88
|
+
#
|
89
|
+
# @param attributes={} [type] [description]
|
90
|
+
#
|
91
|
+
# @return [Stellar::Transaction] the resulting skeleton
|
92
|
+
def self.for_account(attributes={})
|
93
|
+
account = attributes[:account]
|
94
|
+
sequence = attributes[:sequence]
|
95
|
+
fee = attributes[:fee]
|
96
|
+
|
97
|
+
raise ArgumentError, "Bad :account" unless account.is_a?(KeyPair) && account.sign?
|
98
|
+
raise ArgumentError, "Bad :sequence #{sequence}" unless sequence.is_a?(Integer)
|
99
|
+
raise ArgumentError, "Bad :fee #{sequence}" if fee.present? && !fee.is_a?(Integer)
|
100
|
+
|
101
|
+
new.tap do |result|
|
102
|
+
result.seq_num = sequence
|
103
|
+
result.fee = fee
|
104
|
+
result.source_account = account.account_id
|
105
|
+
result.apply_defaults
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
def sign(key_pair)
|
110
|
+
key_pair.sign(hash)
|
111
|
+
end
|
112
|
+
|
113
|
+
def sign_decorated(key_pair)
|
114
|
+
key_pair.sign_decorated(hash)
|
115
|
+
end
|
116
|
+
|
117
|
+
def hash
|
118
|
+
Digest::SHA256.digest(signature_base)
|
119
|
+
end
|
120
|
+
|
121
|
+
# Returns the string of bytes that, when hashed, provide the value which
|
122
|
+
# should be signed to create a valid stellar transaciton signature
|
123
|
+
def signature_base
|
124
|
+
signature_base_prefix + to_xdr
|
125
|
+
end
|
126
|
+
|
127
|
+
def signature_base_prefix
|
128
|
+
val = Stellar::EnvelopeType.envelope_type_tx
|
129
|
+
|
130
|
+
Stellar::EnvelopeType.to_xdr(val)
|
131
|
+
end
|
132
|
+
|
133
|
+
def to_envelope(*key_pairs)
|
134
|
+
signatures = key_pairs.map(&method(:sign_decorated))
|
135
|
+
|
136
|
+
TransactionEnvelope.new({
|
137
|
+
:signatures => signatures,
|
138
|
+
:tx => self
|
139
|
+
})
|
140
|
+
end
|
141
|
+
|
142
|
+
def merge(other)
|
143
|
+
cloned = Marshal.load Marshal.dump(self)
|
144
|
+
cloned.operations += other.to_operations
|
145
|
+
cloned
|
146
|
+
end
|
147
|
+
|
148
|
+
|
149
|
+
#
|
150
|
+
# Extracts the operations from this single transaction,
|
151
|
+
# setting the source account on the extracted operations.
|
152
|
+
#
|
153
|
+
# Useful for merging transactions.
|
154
|
+
#
|
155
|
+
# @return [Array<Operation>] the operations
|
156
|
+
def to_operations
|
157
|
+
cloned = Marshal.load Marshal.dump(operations)
|
158
|
+
operations.each do |op|
|
159
|
+
op.source_account = self.source_account
|
160
|
+
end
|
161
|
+
end
|
162
|
+
|
163
|
+
def apply_defaults
|
164
|
+
self.operations ||= []
|
165
|
+
self.fee ||= 10
|
166
|
+
self.memo ||= Memo.new(:memo_none)
|
167
|
+
self.ext ||= Stellar::Transaction::Ext.new 0
|
168
|
+
end
|
169
|
+
end
|
170
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
module Stellar
|
2
|
+
class TransactionEnvelope
|
3
|
+
|
4
|
+
#
|
5
|
+
# Checks to ensure that every signature for the envelope is
|
6
|
+
# a valid signature of one of the provided `key_pairs`
|
7
|
+
#
|
8
|
+
# NOTE: this does not do any authorization checks, which requires access to
|
9
|
+
# the current ledger state.
|
10
|
+
#
|
11
|
+
# @param *key_pairs [Array<Stellar::KeyPair>] The key pairs to check the envelopes signatures against
|
12
|
+
#
|
13
|
+
# @return [Boolean] true if all signatures are from the provided key_pairs and validly sign the tx's hash
|
14
|
+
def signed_correctly?(*key_pairs)
|
15
|
+
hash = tx.hash
|
16
|
+
return false if signatures.empty?
|
17
|
+
|
18
|
+
key_index = key_pairs.index_by(&:signature_hint)
|
19
|
+
|
20
|
+
signatures.all? do |sig|
|
21
|
+
key_pair = key_index[sig.hint]
|
22
|
+
break false if key_pair.nil?
|
23
|
+
|
24
|
+
key_pair.verify(sig.signature, hash)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def hash
|
29
|
+
Digest::SHA256.digest(to_xdr)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,96 @@
|
|
1
|
+
module Stellar
|
2
|
+
module Util
|
3
|
+
class ContinuedFraction
|
4
|
+
MAX_PRECISION = (2**32) - 1
|
5
|
+
attr_reader :i
|
6
|
+
attr_reader :f
|
7
|
+
|
8
|
+
def self.best_r(number, max_precision=MAX_PRECISION)
|
9
|
+
cur_cf = new(number)
|
10
|
+
|
11
|
+
loop do
|
12
|
+
next_cf = cur_cf.extend()
|
13
|
+
cur_r = cur_cf.to_r(max_precision)
|
14
|
+
next_r = next_cf.to_r(max_precision)
|
15
|
+
|
16
|
+
break cur_r if cur_cf.done? || cur_r == next_r
|
17
|
+
|
18
|
+
cur_cf = next_cf
|
19
|
+
end
|
20
|
+
|
21
|
+
cur_cf.to_r(max_precision)
|
22
|
+
end
|
23
|
+
|
24
|
+
def initialize(val, parents=[])
|
25
|
+
@i = val.floor
|
26
|
+
@f = val - @i
|
27
|
+
@parents = parents
|
28
|
+
end
|
29
|
+
|
30
|
+
def to_a
|
31
|
+
@parents + [i]
|
32
|
+
end
|
33
|
+
|
34
|
+
def error(actual)
|
35
|
+
(actual - to_f).abs
|
36
|
+
end
|
37
|
+
|
38
|
+
def to_f
|
39
|
+
convergent = convergents.last
|
40
|
+
convergent.n / convergent.d.to_f
|
41
|
+
end
|
42
|
+
|
43
|
+
def convergents
|
44
|
+
return @convergents if defined? @convergents
|
45
|
+
|
46
|
+
c = [Fraction.new(0,1), Fraction.new(1,0)]
|
47
|
+
to_a.each_with_index do |a, i|
|
48
|
+
i = i + 2
|
49
|
+
|
50
|
+
h = a * c[i-1].n + c[i-2].n
|
51
|
+
k = a * c[i-1].d + c[i-2].d
|
52
|
+
c << Fraction.new(h,k)
|
53
|
+
end
|
54
|
+
|
55
|
+
@converegents = c[2..-1]
|
56
|
+
end
|
57
|
+
|
58
|
+
def to_r(max_precision=MAX_PRECISION)
|
59
|
+
fraction = convergents.take_while do |c|
|
60
|
+
c.n <= max_precision && c.d <= max_precision
|
61
|
+
end.last
|
62
|
+
|
63
|
+
Rational(fraction.n, fraction.d)
|
64
|
+
end
|
65
|
+
|
66
|
+
def done?
|
67
|
+
@f == 0
|
68
|
+
end
|
69
|
+
|
70
|
+
def extend(count=1)
|
71
|
+
result = self
|
72
|
+
|
73
|
+
count.times do
|
74
|
+
break if result.done?
|
75
|
+
result = ContinuedFraction.new(1 / result.f, result.to_a)
|
76
|
+
end
|
77
|
+
|
78
|
+
result
|
79
|
+
end
|
80
|
+
|
81
|
+
class Fraction
|
82
|
+
attr_reader :n
|
83
|
+
attr_reader :d
|
84
|
+
|
85
|
+
def initialize(n,d)
|
86
|
+
@n = n
|
87
|
+
@d = d
|
88
|
+
end
|
89
|
+
|
90
|
+
def to_r
|
91
|
+
Rational(@n, @d)
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
module Stellar
|
2
|
+
module Util
|
3
|
+
|
4
|
+
require 'base32'
|
5
|
+
require 'digest/crc16_xmodem'
|
6
|
+
|
7
|
+
class StrKey
|
8
|
+
|
9
|
+
VERSION_BYTES = {
|
10
|
+
account_id: [ 6 << 3].pack("C"), # Base32-encodes to 'G...'
|
11
|
+
seed: [18 << 3].pack("C"), # Base32-encodes to 'S...'
|
12
|
+
}
|
13
|
+
|
14
|
+
def self.check_encode(version, byte_str)
|
15
|
+
version_byte = VERSION_BYTES[version]
|
16
|
+
raise ArgumentError, "Invalid version: #{version}" if version_byte.blank?
|
17
|
+
payload = version_byte + byte_str.dup.force_encoding("BINARY")
|
18
|
+
check = checksum(payload)
|
19
|
+
Base32.encode(payload + check)
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.check_decode(expected_version, str)
|
23
|
+
decoded = Base32.decode(str) rescue (raise ArgumentError, "Invalid base32 string")
|
24
|
+
version_byte = decoded[0]
|
25
|
+
payload = decoded[1...-2]
|
26
|
+
check = decoded[-2..-1]
|
27
|
+
version = VERSION_BYTES.key(version_byte)
|
28
|
+
|
29
|
+
raise ArgumentError, "Unexpected version: #{version.inspect}" if version != expected_version
|
30
|
+
raise ArgumentError, "Invalid checksum" if check != checksum(decoded[0...-2])
|
31
|
+
payload
|
32
|
+
end
|
33
|
+
|
34
|
+
# return the "XModem CRC16" (CCITT-like, but with 0-init and MSB first)
|
35
|
+
# packed into a string in little-endian order
|
36
|
+
def self.checksum(bytes)
|
37
|
+
crc = Digest::CRC16XModem.checksum(bytes)
|
38
|
+
[crc].pack("S<")
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
data/open-core.gemspec
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'stellar/base/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "open-core"
|
8
|
+
spec.version = Stellar::Base::VERSION
|
9
|
+
spec.authors = ["Stefan Buhrmester"]
|
10
|
+
spec.email = ["buhrmi@gmail.com"]
|
11
|
+
spec.summary = %q{Open core client library: XDR}
|
12
|
+
spec.homepage = "https://github.com/open-core/open-core-ruby"
|
13
|
+
spec.license = "Apache 2.0"
|
14
|
+
|
15
|
+
spec.files = `git ls-files -z`.split("\x0")
|
16
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
17
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
|
+
spec.require_paths = ["generated", "lib"]
|
19
|
+
|
20
|
+
spec.add_dependency "xdr", ">= 0.0.4"
|
21
|
+
spec.add_dependency "digest-crc"
|
22
|
+
spec.add_dependency "base32"
|
23
|
+
spec.add_dependency "rbnacl"
|
24
|
+
spec.add_dependency "rbnacl-libsodium", "~> 1.0.3"
|
25
|
+
spec.add_dependency "activesupport", "~> 4"
|
26
|
+
|
27
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
28
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
29
|
+
spec.add_development_dependency "xdrgen"
|
30
|
+
spec.add_development_dependency "rspec", "~> 3.1"
|
31
|
+
spec.add_development_dependency "guard-rspec"
|
32
|
+
spec.add_development_dependency "simplecov"
|
33
|
+
spec.add_development_dependency "octokit"
|
34
|
+
spec.add_development_dependency "netrc"
|
35
|
+
spec.add_development_dependency "yard"
|
36
|
+
|
37
|
+
end
|