stellar-base 0.23.1 → 0.27.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (102) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +134 -29
  3. data/README.md +7 -7
  4. data/generated/stellar-base-generated.rb +44 -1
  5. data/generated/stellar/account_entry.rb +3 -13
  6. data/generated/stellar/account_entry/ext.rb +2 -16
  7. data/generated/stellar/account_entry_extension_v1.rb +32 -0
  8. data/generated/stellar/account_entry_extension_v1/ext.rb +28 -0
  9. data/generated/stellar/account_entry_extension_v2.rb +34 -0
  10. data/generated/stellar/{account_entry/ext/v1 → account_entry_extension_v2}/ext.rb +8 -12
  11. data/generated/stellar/account_flags.rb +9 -4
  12. data/generated/stellar/account_merge_result.rb +1 -1
  13. data/generated/stellar/account_merge_result_code.rb +3 -1
  14. data/generated/stellar/allow_trust_op.rb +3 -18
  15. data/generated/stellar/asset_code.rb +30 -0
  16. data/generated/stellar/begin_sponsoring_future_reserves_op.rb +18 -0
  17. data/generated/stellar/begin_sponsoring_future_reserves_result.rb +26 -0
  18. data/generated/stellar/begin_sponsoring_future_reserves_result_code.rb +29 -0
  19. data/generated/stellar/claim_claimable_balance_op.rb +18 -0
  20. data/generated/stellar/claim_claimable_balance_result.rb +25 -0
  21. data/generated/stellar/claim_claimable_balance_result_code.rb +31 -0
  22. data/generated/stellar/claim_predicate.rb +43 -0
  23. data/generated/stellar/claim_predicate_type.rb +30 -0
  24. data/generated/stellar/claimable_balance_entry.rb +46 -0
  25. data/generated/stellar/claimable_balance_entry/ext.rb +28 -0
  26. data/generated/stellar/claimable_balance_entry_extension_v1.rb +30 -0
  27. data/generated/stellar/claimable_balance_entry_extension_v1/ext.rb +24 -0
  28. data/generated/stellar/claimable_balance_flags.rb +22 -0
  29. data/generated/stellar/claimable_balance_id.rb +23 -0
  30. data/generated/stellar/claimable_balance_id_type.rb +20 -0
  31. data/generated/stellar/claimant.rb +31 -0
  32. data/generated/stellar/claimant/v0.rb +22 -0
  33. data/generated/stellar/claimant_type.rb +20 -0
  34. data/generated/stellar/clawback_claimable_balance_op.rb +18 -0
  35. data/generated/stellar/clawback_claimable_balance_result.rb +26 -0
  36. data/generated/stellar/clawback_claimable_balance_result_code.rb +29 -0
  37. data/generated/stellar/clawback_op.rb +22 -0
  38. data/generated/stellar/clawback_result.rb +25 -0
  39. data/generated/stellar/clawback_result_code.rb +31 -0
  40. data/generated/stellar/create_claimable_balance_op.rb +22 -0
  41. data/generated/stellar/create_claimable_balance_result.rb +27 -0
  42. data/generated/stellar/create_claimable_balance_result_code.rb +30 -0
  43. data/generated/stellar/create_passive_sell_offer_op.rb +1 -1
  44. data/generated/stellar/end_sponsoring_future_reserves_result.rb +26 -0
  45. data/generated/stellar/end_sponsoring_future_reserves_result_code.rb +25 -0
  46. data/generated/stellar/envelope_type.rb +3 -1
  47. data/generated/stellar/inner_transaction_result.rb +2 -1
  48. data/generated/stellar/inner_transaction_result/result.rb +3 -1
  49. data/generated/stellar/ledger_entry.rb +4 -0
  50. data/generated/stellar/ledger_entry/data.rb +12 -8
  51. data/generated/stellar/ledger_entry/ext.rb +4 -0
  52. data/generated/stellar/ledger_entry_extension_v1.rb +30 -0
  53. data/generated/stellar/ledger_entry_extension_v1/ext.rb +24 -0
  54. data/generated/stellar/ledger_entry_type.rb +7 -5
  55. data/generated/stellar/ledger_key.rb +17 -8
  56. data/generated/stellar/ledger_key/claimable_balance.rb +20 -0
  57. data/generated/stellar/operation.rb +16 -0
  58. data/generated/stellar/operation/body.rb +57 -26
  59. data/generated/stellar/operation_id.rb +32 -0
  60. data/generated/stellar/operation_id/id.rb +24 -0
  61. data/generated/stellar/operation_result.rb +16 -0
  62. data/generated/stellar/operation_result/tr.rb +60 -28
  63. data/generated/stellar/operation_result_code.rb +3 -1
  64. data/generated/stellar/operation_type.rb +31 -15
  65. data/generated/stellar/path_payment_strict_receive_result.rb +2 -1
  66. data/generated/stellar/payment_result_code.rb +1 -1
  67. data/generated/stellar/revoke_sponsorship_op.rb +35 -0
  68. data/generated/stellar/revoke_sponsorship_op/signer.rb +22 -0
  69. data/generated/stellar/revoke_sponsorship_result.rb +25 -0
  70. data/generated/stellar/revoke_sponsorship_result_code.rb +31 -0
  71. data/generated/stellar/revoke_sponsorship_type.rb +22 -0
  72. data/generated/stellar/set_options_result_code.rb +14 -11
  73. data/generated/stellar/set_trust_line_flags_op.rb +25 -0
  74. data/generated/stellar/set_trust_line_flags_result.rb +25 -0
  75. data/generated/stellar/set_trust_line_flags_result_code.rb +31 -0
  76. data/generated/stellar/transaction_result_code.rb +5 -3
  77. data/generated/stellar/trust_line_flags.rb +5 -1
  78. data/lib/stellar-base.rb +18 -5
  79. data/lib/stellar/account_flags.rb +1 -1
  80. data/lib/stellar/asset.rb +10 -0
  81. data/lib/stellar/claim_predicate.rb +198 -0
  82. data/lib/stellar/compat.rb +4 -16
  83. data/lib/stellar/concerns/transaction.rb +5 -4
  84. data/lib/stellar/dsl.rb +93 -0
  85. data/lib/stellar/ext/xdr.rb +50 -0
  86. data/lib/stellar/key_pair.rb +60 -53
  87. data/lib/stellar/ledger_key.rb +32 -0
  88. data/lib/stellar/muxed_account.rb +16 -0
  89. data/lib/stellar/networks.rb +12 -12
  90. data/lib/stellar/operation.rb +144 -17
  91. data/lib/stellar/price.rb +11 -2
  92. data/lib/stellar/transaction.rb +1 -169
  93. data/lib/stellar/transaction_builder.rb +28 -2
  94. data/lib/stellar/transaction_envelope.rb +7 -43
  95. data/lib/stellar/transaction_v0.rb +2 -10
  96. data/lib/stellar/trust_line_flags.rb +53 -0
  97. data/lib/stellar/util/strkey.rb +6 -6
  98. data/lib/stellar/version.rb +1 -3
  99. metadata +177 -30
  100. data/generated/stellar/account_entry/ext/v1.rb +0 -34
  101. data/generated/stellar/allow_trust_op/asset.rb +0 -33
  102. data/lib/stellar/util/continued_fraction.rb +0 -96
data/lib/stellar/price.rb CHANGED
@@ -4,7 +4,12 @@ module Stellar
4
4
  MAX_PRECISION = (2**31) - 1
5
5
 
6
6
  def self.from_f(number)
7
- best_r = Util::ContinuedFraction.best_r(number, MAX_PRECISION)
7
+ best_r = number.to_r.rationalize(1.0e-7)
8
+
9
+ if best_r.numerator > MAX_PRECISION || best_r.denominator > MAX_PRECISION
10
+ raise ArgumentError("Couldn't find price approximation")
11
+ end
12
+
8
13
  new({
9
14
  n: best_r.numerator,
10
15
  d: best_r.denominator
@@ -15,8 +20,12 @@ module Stellar
15
20
  self.class.new(n: d, d: n)
16
21
  end
17
22
 
23
+ def to_d
24
+ n.to_d / d
25
+ end
26
+
18
27
  def to_f
19
- n / d.to_f
28
+ n.to_f / d
20
29
  end
21
30
 
22
31
  def to_s
@@ -2,174 +2,6 @@ module Stellar
2
2
  class Transaction
3
3
  include Stellar::Concerns::Transaction
4
4
 
5
- class << self
6
- #
7
- # @see Stellar::Operation.payment
8
- def payment(attributes = {})
9
- make :payment, attributes
10
- end
11
-
12
- #
13
- # @see Stellar::Operation.path_payment
14
- def path_payment(attributes = {})
15
- make :path_payment, attributes
16
- end
17
-
18
- #
19
- # @see Stellar::Operation.path_payment_strict_receive
20
- def path_payment_strict_receive(attributes = {})
21
- make :path_payment_strict_receive, attributes
22
- end
23
-
24
- #
25
- # @see Stellar::Operation.path_payment_strict_send
26
- def path_payment_strict_send(attributes = {})
27
- make :path_payment_strict_send, attributes
28
- end
29
-
30
- #
31
- # @see Stellar::Operation.create_account
32
- def create_account(attributes = {})
33
- make :create_account, attributes
34
- end
35
-
36
- #
37
- # @see Stellar::Operation.change_trust
38
- def change_trust(attributes = {})
39
- make :change_trust, attributes
40
- end
41
-
42
- #
43
- # @see Stellar::Operation.manage_sell_offer
44
- def manage_sell_offer(attributes = {})
45
- make :manage_sell_offer, attributes
46
- end
47
-
48
- #
49
- # @see Stellar::Operation.manage_buy_offer
50
- def manage_buy_offer(attributes = {})
51
- make :manage_buy_offer, attributes
52
- end
53
-
54
- #
55
- # @see Stellar::Operation.create_passive_sell_offer
56
- def create_passive_sell_offer(attributes = {})
57
- make :create_passive_sell_offer, attributes
58
- end
59
-
60
- #
61
- # @see Stellar::Operation.set_options
62
- def set_options(attributes = {})
63
- make :set_options, attributes
64
- end
65
-
66
- #
67
- # @see Stellar::Operation.allow_trust
68
- def allow_trust(attributes = {})
69
- make :allow_trust, attributes
70
- end
71
-
72
- #
73
- # @see Stellar::Operation.account_merge
74
- def account_merge(attributes = {})
75
- make :account_merge, attributes
76
- end
77
-
78
- #
79
- # @see Stellar::Operation.inflation
80
- def inflation(attributes = {})
81
- make :inflation, attributes
82
- end
83
-
84
- #
85
- # @see Stellar::Operation.manage_data
86
- def manage_data(attributes = {})
87
- make :manage_data, attributes
88
- end
89
-
90
- #
91
- # @see Stellar::Operation.manage_data
92
- def bump_sequence(attributes = {})
93
- make :bump_sequence, attributes
94
- end
95
-
96
- #
97
- # DEPRECATED
98
- #
99
- # All methods calling make() have been deprecated in favor of Stellar::TransactionBuilder.
100
- # These functions only create single-operation transactions and essentially duplicate the
101
- # methods provided by Stellar::Operation. Stellar::TransactionBuilder enables the construction
102
- # of multi-operation transactions and mirrors the functionality provided by the Python and
103
- # JavaScript SDKs.
104
- #
105
- # Helper method to create a transaction with a single
106
- # operation of the provided type. See class methods
107
- # on Stellar::Operation for available values for
108
- # operation_type.
109
- #
110
- # @see Stellar::Operation
111
- #
112
- # @param operation_type [Symbol] the operation to use
113
- # @param attributes={} [Hash] attributes to use for both the transaction and the operation
114
- #
115
- # @return [Stellar::Transaction] the resulting transaction
116
- def make(operation_type, attributes = {})
117
- Stellar::Deprecation.warn(
118
- "Transaction.#{operation_type} is deprecated. Use Stellar::TransactionBuilder instead."
119
- )
120
- for_account(attributes).tap do |result|
121
- result.operations << Operation.send(operation_type, attributes)
122
- end
123
- end
124
-
125
- #
126
- # Helper method to create the skeleton of a transaction.
127
- # The resulting transaction will have its source account,
128
- # sequence, fee, min ledger and max ledger set.
129
- #
130
- #
131
- # @param attributes={} [type] [description]
132
- #
133
- # @return [Stellar::Transaction] the resulting skeleton
134
- def for_account(attributes = {})
135
- account = attributes[:account]
136
- sequence = attributes[:sequence]
137
- fee = attributes[:fee]
138
-
139
- raise ArgumentError, "Bad :account" unless account.is_a?(KeyPair)
140
- raise ArgumentError, "Bad :sequence #{sequence}" unless sequence.is_a?(Integer)
141
- raise ArgumentError, "Bad :fee #{sequence}" if fee.present? && !fee.is_a?(Integer)
142
-
143
- new.tap do |result|
144
- result.seq_num = sequence
145
- result.fee = fee
146
- result.memo = make_memo(attributes[:memo])
147
- result.source_account = account.muxed_account
148
- result.apply_defaults
149
- end
150
- end
151
-
152
- private
153
-
154
- def make_memo(memo)
155
- case memo
156
- when Stellar::Memo
157
- memo
158
- when nil
159
- nil
160
- when Integer
161
- Memo.new(:memo_id, memo)
162
- when String
163
- Memo.new(:memo_text, memo)
164
- when Array
165
- t, val = *memo
166
- Memo.new(:"memo_#{t}", val)
167
- else
168
- raise ArgumentError, "Bad :memo"
169
- end
170
- end
171
- end
172
-
173
5
  def to_v0
174
6
  ed25519 = if source_account.switch == Stellar::CryptoKeyType.key_type_ed25519
175
7
  source_account.ed25519!
@@ -197,7 +29,7 @@ module Stellar
197
29
  end
198
30
 
199
31
  def to_envelope(*key_pairs)
200
- signatures = (key_pairs || []).map(&method(:sign_decorated))
32
+ signatures = key_pairs.map(&method(:sign_decorated))
201
33
 
202
34
  TransactionEnvelope.v1(signatures: signatures, tx: self)
203
35
  end
@@ -2,12 +2,34 @@ module Stellar
2
2
  class TransactionBuilder
3
3
  attr_reader :source_account, :sequence_number, :base_fee, :time_bounds, :memo, :operations
4
4
 
5
+ class << self
6
+ # This enable user to call shortcut methods, like
7
+ # TransactionBuilder.payment(...),
8
+ # TransactionBuilder.manage_data(...) and etc.
9
+ # It reduces the boilerplate, when you just need to
10
+ # shoot a single operation in transaction
11
+ def method_missing(method_name, *args, **kwargs)
12
+ unless Stellar::Operation.respond_to?(method_name)
13
+ return super
14
+ end
15
+
16
+ op = Stellar::Operation.send(method_name, **kwargs)
17
+
18
+ new(**kwargs).add_operation(op).build
19
+ end
20
+
21
+ def respond_to_missing?(method_name, include_private = false)
22
+ Stellar::Operation.respond_to?(method_name) || super
23
+ end
24
+ end
25
+
5
26
  def initialize(
6
27
  source_account:,
7
28
  sequence_number:,
8
29
  base_fee: 100,
9
30
  time_bounds: nil,
10
- memo: nil
31
+ memo: nil,
32
+ **_ # ignore any additional parameters without errors
11
33
  )
12
34
  raise ArgumentError, "Bad :source_account" unless source_account.is_a?(Stellar::KeyPair)
13
35
  raise ArgumentError, "Bad :sequence_number" unless sequence_number.is_a?(Integer) && sequence_number >= 0
@@ -18,6 +40,11 @@ module Stellar
18
40
  @sequence_number = sequence_number
19
41
  @base_fee = base_fee
20
42
  @time_bounds = time_bounds
43
+
44
+ if time_bounds.nil?
45
+ set_timeout(0)
46
+ end
47
+
21
48
  @memo = make_memo(memo)
22
49
  @operations = []
23
50
  end
@@ -47,7 +74,6 @@ module Stellar
47
74
  end
48
75
 
49
76
  def build_fee_bump(inner_txe:)
50
- p inner_txe.switch
51
77
  if inner_txe.switch == Stellar::EnvelopeType.envelope_type_tx_v0
52
78
  inner_txe = Stellar::TransactionEnvelope.v1(tx: inner_txe.tx.to_v1, signatures: inner_txe.signatures)
53
79
  elsif inner_txe.switch != Stellar::EnvelopeType.envelope_type_tx
@@ -1,39 +1,7 @@
1
1
  module Stellar
2
2
  class TransactionEnvelope
3
- class << self
4
- def v0(tx:, signatures:)
5
- v0_envelope = TransactionV0Envelope.new(tx: tx, signatures: signatures)
6
- new(:envelope_type_tx_v0, v0_envelope)
7
- end
8
-
9
- def v1(tx:, signatures:)
10
- v1_envelope = TransactionV1Envelope.new(tx: tx, signatures: signatures)
11
- new(:envelope_type_tx, v1_envelope)
12
- end
13
-
14
- def fee_bump(tx:, signatures:)
15
- fee_bump_envelope = FeeBumpTransactionEnvelope.new(tx: tx, signatures: signatures)
16
- new(:envelope_type_tx_fee_bump, fee_bump_envelope)
17
- end
18
- end
19
-
20
- # Delegates any undefined method to the currently set arm
21
- def method_missing(method, *args, &block)
22
- case switch
23
- when EnvelopeType.envelope_type_tx_v0
24
- v0!.public_send(method, *args)
25
- when EnvelopeType.envelope_type_tx
26
- v1!.public_send(method, *args)
27
- when EnvelopeType.envelope_type_tx_fee_bump
28
- fee_bump!.public_send(method, *args)
29
- else
30
- super
31
- end
32
- end
33
-
34
- def respond_to_missing?(method, include_private = false)
35
- ["tx", "signatures"].include?(method) || super
36
- end
3
+ delegate :tx, :signatures, to: :value
4
+ delegate :hash, to: :tx
37
5
 
38
6
  # Checks to ensure that every signature for the envelope is
39
7
  # a valid signature of one of the provided `key_pairs`
@@ -41,27 +9,23 @@ module Stellar
41
9
  # NOTE: this does not do any authorization checks, which requires access to
42
10
  # the current ledger state.
43
11
  #
44
- # @param *key_pairs [Array<Stellar::KeyPair>] The key pairs to check the envelopes signatures against
12
+ # @param key_pairs [Array<Stellar::KeyPair>] The key pairs to check the envelopes signatures against
45
13
  #
46
14
  # @return [Boolean] true if all signatures are from the provided key_pairs and validly sign the tx's hash
47
15
  def signed_correctly?(*key_pairs)
48
- hash = tx.hash
49
16
  return false if signatures.empty?
50
17
 
51
- key_index = key_pairs.index_by(&:signature_hint)
18
+ tx_hash = tx.hash
19
+ keys_by_hint = key_pairs.index_by(&:signature_hint)
52
20
 
53
21
  signatures.all? do |sig|
54
- key_pair = key_index[sig.hint]
22
+ key_pair = keys_by_hint[sig.hint]
55
23
  break false if key_pair.nil?
56
24
 
57
- key_pair.verify(sig.signature, hash)
25
+ key_pair.verify(sig.signature, tx_hash)
58
26
  end
59
27
  end
60
28
 
61
- def hash
62
- Digest::SHA256.digest(to_xdr)
63
- end
64
-
65
29
  def merge(other)
66
30
  merged_tx = tx.merge(other.tx)
67
31
  merged_tx.signatures = [signatures, other.signatures]
@@ -3,15 +3,7 @@ module Stellar
3
3
  include Stellar::Concerns::Transaction
4
4
 
5
5
  def to_v1
6
- Transaction.new(
7
- source_account: Stellar::MuxedAccount.new(:key_type_ed25519, source_account),
8
- seq_num: seq_num,
9
- operations: operations,
10
- fee: fee,
11
- memo: memo,
12
- time_bounds: time_bounds,
13
- ext: ext
14
- )
6
+ Transaction.new(**attributes.except(:source_account_ed25519), source_account: source_account)
15
7
  end
16
8
 
17
9
  def to_envelope(*key_pairs)
@@ -45,7 +37,7 @@ module Stellar
45
37
  end
46
38
 
47
39
  def source_account
48
- source_account_ed25519
40
+ Stellar::MuxedAccount.ed25519(source_account_ed25519)
49
41
  end
50
42
  end
51
43
  end
@@ -0,0 +1,53 @@
1
+ module Stellar
2
+ class TrustLineFlags
3
+ # Converts an array of Stellar::TrustLineFlags members into
4
+ # an integers suitable for use in in a SetTrustLineFlagsOp.
5
+ #
6
+ # @param flags [Array<Stellar::TrustLineFlags>] the flags to combine
7
+ #
8
+ # @return [Fixnum] the combined result
9
+ def self.make_mask(flags)
10
+ normalize(flags).map(&:value).inject(&:|) || 0
11
+ end
12
+
13
+ # Converts an integer used in SetTrustLineFlagsOp on the set/clear flag options
14
+ # into an array of Stellar::TrustLineFlags members
15
+ #
16
+ # @param combined [Fixnum]
17
+ # @return [Array<Stellar::AccountFlags>]
18
+ def self.parse_mask(combined)
19
+ members.values.select { |m| (m.value & combined) != 0 }
20
+ end
21
+
22
+ # Converts each element of the input array to Stellar::TrustLineFlags instance.
23
+ #
24
+ # @param [Array<Stellar::TrustLineFlags,Symbol,#to_s>] input
25
+ # @return [Array<Stellar::TrustLineFlags>]
26
+ # @raise [TypeError] if any element of the input cannot be converted
27
+ def self.normalize(input)
28
+ input.map do |val|
29
+ case val
30
+ when Stellar::TrustLineFlags
31
+ val
32
+ when ->(_) { members.key?(val.to_s) }
33
+ from_name(val.to_s)
34
+ when ->(_) { members.key?("#{val}_flag") }
35
+ from_name("#{val}_flag")
36
+ else
37
+ raise TypeError, "unknown trustline flag: #{val}"
38
+ end
39
+ end
40
+ end
41
+
42
+ def self.set_clear_masks(flags)
43
+ set_flags = []
44
+ clear_flags = []
45
+
46
+ Hash(flags).each do |flag, value|
47
+ value.present? ? set_flags.push(flag) : clear_flags.push(flag)
48
+ end
49
+
50
+ {set_flags: make_mask(set_flags), clear_flags: make_mask(clear_flags)}
51
+ end
52
+ end
53
+ end
@@ -22,7 +22,7 @@ module Stellar
22
22
  end
23
23
 
24
24
  # Converts an Stellar::MuxedAccount to its string representation, forcing the ed25519 representation.
25
- # @param [Stellar::MuxedAccount] muxed account
25
+ # @param muxed_account [Stellar::MuxedAccount] account
26
26
  # @return [String] "G.."-like address
27
27
  def self.encode_muxed_account(muxed_account)
28
28
  ed25519 = if muxed_account.switch == Stellar::CryptoKeyType.key_type_ed25519
@@ -36,7 +36,7 @@ module Stellar
36
36
 
37
37
  # Returns a Stellar::MuxedAccount, forcing the ed25519 discriminant
38
38
  #
39
- # @param [String] address to decode to XDR
39
+ # @param strkey [String] address string to decode
40
40
  # @return [Stellar::MuxedAccount] MuxedAccount with ed25519 discriminant
41
41
  def self.decode_muxed_account(strkey)
42
42
  Stellar::MuxedAccount.new(:key_type_ed25519, check_decode(:account_id, strkey))
@@ -44,10 +44,10 @@ module Stellar
44
44
 
45
45
  def self.check_decode(expected_version, str)
46
46
  decoded = begin
47
- Base32.decode(str)
48
- rescue
49
- raise ArgumentError, "Invalid base32 string"
50
- end
47
+ Base32.decode(str)
48
+ rescue
49
+ raise ArgumentError, "Invalid base32 string"
50
+ end
51
51
  version_byte = decoded[0]
52
52
  payload = decoded[1...-2]
53
53
  check = decoded[-2..-1]