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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +134 -29
- data/README.md +7 -7
- data/generated/stellar-base-generated.rb +44 -1
- data/generated/stellar/account_entry.rb +3 -13
- data/generated/stellar/account_entry/ext.rb +2 -16
- data/generated/stellar/account_entry_extension_v1.rb +32 -0
- data/generated/stellar/account_entry_extension_v1/ext.rb +28 -0
- data/generated/stellar/account_entry_extension_v2.rb +34 -0
- data/generated/stellar/{account_entry/ext/v1 → account_entry_extension_v2}/ext.rb +8 -12
- data/generated/stellar/account_flags.rb +9 -4
- data/generated/stellar/account_merge_result.rb +1 -1
- data/generated/stellar/account_merge_result_code.rb +3 -1
- data/generated/stellar/allow_trust_op.rb +3 -18
- data/generated/stellar/asset_code.rb +30 -0
- data/generated/stellar/begin_sponsoring_future_reserves_op.rb +18 -0
- data/generated/stellar/begin_sponsoring_future_reserves_result.rb +26 -0
- data/generated/stellar/begin_sponsoring_future_reserves_result_code.rb +29 -0
- data/generated/stellar/claim_claimable_balance_op.rb +18 -0
- data/generated/stellar/claim_claimable_balance_result.rb +25 -0
- data/generated/stellar/claim_claimable_balance_result_code.rb +31 -0
- data/generated/stellar/claim_predicate.rb +43 -0
- data/generated/stellar/claim_predicate_type.rb +30 -0
- data/generated/stellar/claimable_balance_entry.rb +46 -0
- data/generated/stellar/claimable_balance_entry/ext.rb +28 -0
- data/generated/stellar/claimable_balance_entry_extension_v1.rb +30 -0
- data/generated/stellar/claimable_balance_entry_extension_v1/ext.rb +24 -0
- data/generated/stellar/claimable_balance_flags.rb +22 -0
- data/generated/stellar/claimable_balance_id.rb +23 -0
- data/generated/stellar/claimable_balance_id_type.rb +20 -0
- data/generated/stellar/claimant.rb +31 -0
- data/generated/stellar/claimant/v0.rb +22 -0
- data/generated/stellar/claimant_type.rb +20 -0
- data/generated/stellar/clawback_claimable_balance_op.rb +18 -0
- data/generated/stellar/clawback_claimable_balance_result.rb +26 -0
- data/generated/stellar/clawback_claimable_balance_result_code.rb +29 -0
- data/generated/stellar/clawback_op.rb +22 -0
- data/generated/stellar/clawback_result.rb +25 -0
- data/generated/stellar/clawback_result_code.rb +31 -0
- data/generated/stellar/create_claimable_balance_op.rb +22 -0
- data/generated/stellar/create_claimable_balance_result.rb +27 -0
- data/generated/stellar/create_claimable_balance_result_code.rb +30 -0
- data/generated/stellar/create_passive_sell_offer_op.rb +1 -1
- data/generated/stellar/end_sponsoring_future_reserves_result.rb +26 -0
- data/generated/stellar/end_sponsoring_future_reserves_result_code.rb +25 -0
- data/generated/stellar/envelope_type.rb +3 -1
- data/generated/stellar/inner_transaction_result.rb +2 -1
- data/generated/stellar/inner_transaction_result/result.rb +3 -1
- data/generated/stellar/ledger_entry.rb +4 -0
- data/generated/stellar/ledger_entry/data.rb +12 -8
- data/generated/stellar/ledger_entry/ext.rb +4 -0
- data/generated/stellar/ledger_entry_extension_v1.rb +30 -0
- data/generated/stellar/ledger_entry_extension_v1/ext.rb +24 -0
- data/generated/stellar/ledger_entry_type.rb +7 -5
- data/generated/stellar/ledger_key.rb +17 -8
- data/generated/stellar/ledger_key/claimable_balance.rb +20 -0
- data/generated/stellar/operation.rb +16 -0
- data/generated/stellar/operation/body.rb +57 -26
- data/generated/stellar/operation_id.rb +32 -0
- data/generated/stellar/operation_id/id.rb +24 -0
- data/generated/stellar/operation_result.rb +16 -0
- data/generated/stellar/operation_result/tr.rb +60 -28
- data/generated/stellar/operation_result_code.rb +3 -1
- data/generated/stellar/operation_type.rb +31 -15
- data/generated/stellar/path_payment_strict_receive_result.rb +2 -1
- data/generated/stellar/payment_result_code.rb +1 -1
- data/generated/stellar/revoke_sponsorship_op.rb +35 -0
- data/generated/stellar/revoke_sponsorship_op/signer.rb +22 -0
- data/generated/stellar/revoke_sponsorship_result.rb +25 -0
- data/generated/stellar/revoke_sponsorship_result_code.rb +31 -0
- data/generated/stellar/revoke_sponsorship_type.rb +22 -0
- data/generated/stellar/set_options_result_code.rb +14 -11
- data/generated/stellar/set_trust_line_flags_op.rb +25 -0
- data/generated/stellar/set_trust_line_flags_result.rb +25 -0
- data/generated/stellar/set_trust_line_flags_result_code.rb +31 -0
- data/generated/stellar/transaction_result_code.rb +5 -3
- data/generated/stellar/trust_line_flags.rb +5 -1
- data/lib/stellar-base.rb +18 -5
- data/lib/stellar/account_flags.rb +1 -1
- data/lib/stellar/asset.rb +10 -0
- data/lib/stellar/claim_predicate.rb +198 -0
- data/lib/stellar/compat.rb +4 -16
- data/lib/stellar/concerns/transaction.rb +5 -4
- data/lib/stellar/dsl.rb +93 -0
- data/lib/stellar/ext/xdr.rb +50 -0
- data/lib/stellar/key_pair.rb +60 -53
- data/lib/stellar/ledger_key.rb +32 -0
- data/lib/stellar/muxed_account.rb +16 -0
- data/lib/stellar/networks.rb +12 -12
- data/lib/stellar/operation.rb +144 -17
- data/lib/stellar/price.rb +11 -2
- data/lib/stellar/transaction.rb +1 -169
- data/lib/stellar/transaction_builder.rb +28 -2
- data/lib/stellar/transaction_envelope.rb +7 -43
- data/lib/stellar/transaction_v0.rb +2 -10
- data/lib/stellar/trust_line_flags.rb +53 -0
- data/lib/stellar/util/strkey.rb +6 -6
- data/lib/stellar/version.rb +1 -3
- metadata +177 -30
- data/generated/stellar/account_entry/ext/v1.rb +0 -34
- data/generated/stellar/allow_trust_op/asset.rb +0 -33
- data/lib/stellar/util/continued_fraction.rb +0 -96
data/lib/stellar/compat.rb
CHANGED
@@ -1,21 +1,9 @@
|
|
1
|
-
require "active_support/deprecation"
|
2
|
-
|
3
|
-
Stellar::Deprecation ||= ActiveSupport::Deprecation.new("next release", "stellar-base")
|
4
|
-
|
5
1
|
class << Stellar::Operation
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
deprecate deprecator: Stellar::Deprecation,
|
10
|
-
manage_offer: :manage_sell_offer,
|
11
|
-
create_passive_offer: :create_passive_sell_offer
|
12
|
-
end
|
13
|
-
|
14
|
-
class << Stellar::Transaction
|
15
|
-
alias manage_offer manage_sell_offer
|
16
|
-
alias create_passive_offer create_passive_sell_offer
|
2
|
+
alias_method :manage_offer, :manage_sell_offer
|
3
|
+
alias_method :create_passive_offer, :create_passive_sell_offer
|
17
4
|
|
18
5
|
deprecate deprecator: Stellar::Deprecation,
|
19
6
|
manage_offer: :manage_sell_offer,
|
20
|
-
create_passive_offer: :create_passive_sell_offer
|
7
|
+
create_passive_offer: :create_passive_sell_offer,
|
8
|
+
allow_trust: :set_trust_line_flags
|
21
9
|
end
|
@@ -19,7 +19,7 @@ module Stellar::Concerns
|
|
19
19
|
end
|
20
20
|
|
21
21
|
def merge(other)
|
22
|
-
cloned =
|
22
|
+
cloned = from_xdr(to_xdr)
|
23
23
|
cloned.operations += other.to_operations
|
24
24
|
cloned
|
25
25
|
end
|
@@ -32,9 +32,10 @@ module Stellar::Concerns
|
|
32
32
|
#
|
33
33
|
# @return [Array<Operation>] the operations
|
34
34
|
def to_operations
|
35
|
-
|
36
|
-
|
37
|
-
|
35
|
+
codec = XDR::VarArray[Stellar::Operation]
|
36
|
+
ops = respond_to?(:operations) ? operations : inner_tx.value.tx.operations
|
37
|
+
cloned = codec.from_xdr(codec.to_xdr(ops))
|
38
|
+
cloned.each do |op|
|
38
39
|
op.source_account ||= source_account
|
39
40
|
end
|
40
41
|
end
|
data/lib/stellar/dsl.rb
ADDED
@@ -0,0 +1,93 @@
|
|
1
|
+
module Stellar
|
2
|
+
module DSL
|
3
|
+
module_function
|
4
|
+
|
5
|
+
# Constructs a new ClaimPredicate using DSL
|
6
|
+
#
|
7
|
+
# @example fulfilled during [T+5min, T+60min] period, where T refers to claimable balance entry creation time
|
8
|
+
# Stellar::ClaimPredicate { before_relative_time(1.hour) & ~before_relative_time(5.minutes) }
|
9
|
+
#
|
10
|
+
# @example not fulfilled starting from today midnight until tomorrow midnight,
|
11
|
+
# Stellar::ClaimPredicate { before_absolute_time(Date.today.end_of_day) | ~before_absolute_time(Date.tomorrow.end_of_day) }
|
12
|
+
#
|
13
|
+
# @example always fulfilled
|
14
|
+
# Stellar::ClaimPredicate { }
|
15
|
+
def ClaimPredicate(&block)
|
16
|
+
return ClaimPredicate.unconditional unless block
|
17
|
+
ClaimPredicate.compose(&block)
|
18
|
+
end
|
19
|
+
|
20
|
+
def Claimant(destination, &block)
|
21
|
+
Claimant.new(
|
22
|
+
ClaimantType.claimant_type_v0,
|
23
|
+
Claimant::V0.new(
|
24
|
+
destination: KeyPair(destination).account_id,
|
25
|
+
predicate: ClaimPredicate(&block)
|
26
|
+
)
|
27
|
+
)
|
28
|
+
end
|
29
|
+
|
30
|
+
# @param [Asset, String, nil] subject
|
31
|
+
# @return [Stellar::Asset] instance of the Stellar::Asset
|
32
|
+
# @raise [TypeError] if subject cannot be converted to Stellar::Asset
|
33
|
+
def Asset(subject = nil)
|
34
|
+
case subject
|
35
|
+
when Asset
|
36
|
+
subject
|
37
|
+
when nil, /^(XLM[-:])?native$/
|
38
|
+
Asset.native
|
39
|
+
when /^([0-9A-Z]{1,4})[-:](G[A-Z0-9]{55})$/
|
40
|
+
Asset.alphanum4($1, KeyPair($2))
|
41
|
+
when /^([0-9A-Z]{5,12})[-:](G[A-Z0-9]{55})$/
|
42
|
+
Asset.alphanum12($1, KeyPair($2))
|
43
|
+
else
|
44
|
+
raise TypeError, "Cannot convert #{subject.inspect} to Stellar::Asset"
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
# Generates Stellar::Keypair from subject, use Stellar::Client.to_keypair as shortcut.
|
49
|
+
# @param subject [String|Stellar::Account|Stellar::PublicKey|Stellar::SignerKey|Stellar::Keypair] subject.
|
50
|
+
# @return [Stellar::Keypair] Stellar::Keypair instance.
|
51
|
+
# @raise [TypeError] if subject cannot be converted to Stellar::KeyPair
|
52
|
+
def KeyPair(subject = nil)
|
53
|
+
case subject
|
54
|
+
when ->(subj) { subj.respond_to?(:to_keypair) }
|
55
|
+
subject.to_keypair
|
56
|
+
when PublicKey
|
57
|
+
KeyPair.from_public_key(subject.value)
|
58
|
+
when SignerKey
|
59
|
+
KeyPair.from_raw_seed(subject.value)
|
60
|
+
when /^G[A-Z0-9]{55}$/
|
61
|
+
KeyPair.from_address(subject.to_str)
|
62
|
+
when /^S[A-Z0-9]{55}$/
|
63
|
+
KeyPair.from_seed(subject.to_str)
|
64
|
+
when /^.{32}$/
|
65
|
+
KeyPair.from_raw_seed(subject.to_str)
|
66
|
+
when nil
|
67
|
+
KeyPair.random
|
68
|
+
else
|
69
|
+
raise TypeError, "cannot convert #{subject.inspect} to Stellar::KeyPair"
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
# Provides conversion from different input types into the SignerKey to use in ManageData operation.
|
74
|
+
# @param input [String|zStellar::Account|Stellar::PublicKey|Stellar::SignerKey|Stellar::Keypair] subject.
|
75
|
+
# @return [Stellar::SignerKey] Stellar::Keypair instance.
|
76
|
+
def SignerKey(input = nil)
|
77
|
+
case input
|
78
|
+
when Transaction
|
79
|
+
SignerKey.pre_auth_tx(input.hash)
|
80
|
+
when /^[0-9A-Za-z+\/=]{44}$/
|
81
|
+
SignerKey.hash_x(Stellar::Convert.from_base64(input))
|
82
|
+
when /^[0-9a-f]{64}$/
|
83
|
+
SignerKey.hash_x(Stellar::Convert.from_hex(input))
|
84
|
+
when /^.{32}$/
|
85
|
+
SignerKey.hash_x(input)
|
86
|
+
else
|
87
|
+
SignerKey.ed25519(KeyPair(input))
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
include DSL
|
93
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
require "active_support/core_ext/string/inflections"
|
2
|
+
require "xdr"
|
3
|
+
|
4
|
+
# For every member of XDR::Enum define constant on the corresponding enum type class.
|
5
|
+
# Constant name is a member name stripped of the common prefix with enum type name.
|
6
|
+
XDR::DSL::Enum.redefine_method(:seal) do
|
7
|
+
names = [members.keys.first, name.demodulize.underscore + "_"].join(" ")
|
8
|
+
common_prefix = /\A(.*_).* \1.*\Z/.match(names)&.values_at(1)&.first
|
9
|
+
members.each do |name, value|
|
10
|
+
unless common_prefix.nil?
|
11
|
+
full_name, name = [name, name.delete_prefix(common_prefix)]
|
12
|
+
singleton_class.alias_method(name, full_name)
|
13
|
+
end
|
14
|
+
const_set(name.upcase, value)
|
15
|
+
end
|
16
|
+
self.sealed = true
|
17
|
+
end
|
18
|
+
|
19
|
+
XDR::DSL::Union.redefine_method(:switch) do |switch, arm = nil|
|
20
|
+
raise ArgumentError, "`switch_on` not defined yet" if switch_type.nil?
|
21
|
+
|
22
|
+
switch = normalize_switch_declaration(switch)
|
23
|
+
self.switches = switches.merge(switch => arm)
|
24
|
+
|
25
|
+
unless arm.nil?
|
26
|
+
define_singleton_method(arm) do |*args, **kwargs|
|
27
|
+
value_type = arms[arm]
|
28
|
+
value = if value_type.valid?(args.first)
|
29
|
+
args.first
|
30
|
+
elsif value_type.ancestors.include?(XDR::Struct)
|
31
|
+
value_type.new(kwargs)
|
32
|
+
elsif value_type.ancestors.include?(XDR::Union)
|
33
|
+
value_type.new(*args[0..1])
|
34
|
+
else
|
35
|
+
args.first
|
36
|
+
end
|
37
|
+
new(switch, value)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
# XDR::Union delegates missing methods to the underlying value
|
43
|
+
XDR::Union.define_method(:method_missing) do |name, *args|
|
44
|
+
return super(name, *args) unless value&.respond_to?(name)
|
45
|
+
value&.public_send(name, *args)
|
46
|
+
end
|
47
|
+
|
48
|
+
XDR::Union.define_method(:respond_to_missing?) do |*args|
|
49
|
+
value&.respond_to?(*args)
|
50
|
+
end
|
data/lib/stellar/key_pair.rb
CHANGED
@@ -1,44 +1,58 @@
|
|
1
1
|
module Stellar
|
2
2
|
class KeyPair
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
3
|
+
module FactoryMethods
|
4
|
+
def from_seed(seed)
|
5
|
+
seed_bytes = Util::StrKey.check_decode(:seed, seed)
|
6
|
+
from_raw_seed seed_bytes
|
7
|
+
end
|
8
|
+
|
9
|
+
def from_address(address)
|
10
|
+
pk_bytes = Util::StrKey.check_decode(:account_id, address)
|
11
|
+
from_public_key(pk_bytes)
|
12
|
+
end
|
13
|
+
|
14
|
+
def from_raw_seed(seed_bytes)
|
15
|
+
secret_key = RbNaCl::SigningKey.new(seed_bytes)
|
16
|
+
public_key = secret_key.verify_key
|
17
|
+
new(public_key, secret_key)
|
18
|
+
end
|
19
|
+
|
20
|
+
def from_public_key(pk_bytes)
|
21
|
+
public_key = RbNaCl::VerifyKey.new(pk_bytes)
|
22
|
+
new(public_key)
|
23
|
+
end
|
24
|
+
|
25
|
+
def random
|
26
|
+
secret_key = RbNaCl::SigningKey.generate
|
27
|
+
public_key = secret_key.verify_key
|
28
|
+
new(public_key, secret_key)
|
29
|
+
end
|
30
|
+
|
31
|
+
def from_network_passphrase(passphrase)
|
32
|
+
network_id = Digest::SHA256.digest(passphrase)
|
33
|
+
from_raw_seed network_id
|
34
|
+
end
|
35
|
+
|
36
|
+
def master
|
37
|
+
from_raw_seed(Stellar.current_network_id)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
extend FactoryMethods
|
42
|
+
|
43
|
+
# @param [RbNaCl::VerifyKey] public_key
|
44
|
+
# @param [RbNaCl::SigningKey, nil] secret_key
|
45
|
+
def initialize(public_key, secret_key = nil)
|
46
|
+
@public_key = public_key
|
47
|
+
@secret_key = secret_key
|
33
48
|
end
|
34
49
|
|
35
|
-
def
|
36
|
-
|
50
|
+
def address
|
51
|
+
Util::StrKey.check_encode(:account_id, raw_public_key)
|
37
52
|
end
|
38
53
|
|
39
|
-
def
|
40
|
-
|
41
|
-
@secret_key = secret_key
|
54
|
+
def seed
|
55
|
+
Util::StrKey.check_encode(:seed, raw_seed)
|
42
56
|
end
|
43
57
|
|
44
58
|
def account_id
|
@@ -57,16 +71,17 @@ module Stellar
|
|
57
71
|
Stellar::SignerKey.new :signer_key_type_ed25519, raw_public_key
|
58
72
|
end
|
59
73
|
|
60
|
-
def raw_public_key
|
61
|
-
@public_key.to_bytes
|
62
|
-
end
|
63
|
-
|
64
74
|
def signature_hint
|
65
75
|
# take last 4 bytes
|
66
76
|
account_id.to_xdr.slice(-4, 4)
|
67
77
|
end
|
68
78
|
|
79
|
+
def raw_public_key
|
80
|
+
@public_key.to_bytes
|
81
|
+
end
|
82
|
+
|
69
83
|
def raw_seed
|
84
|
+
raise "no private key" if @secret_key.nil?
|
70
85
|
@secret_key.to_bytes
|
71
86
|
end
|
72
87
|
|
@@ -78,25 +93,13 @@ module Stellar
|
|
78
93
|
@public_key
|
79
94
|
end
|
80
95
|
|
81
|
-
def address
|
82
|
-
pk_bytes = raw_public_key
|
83
|
-
Util::StrKey.check_encode(:account_id, pk_bytes)
|
84
|
-
end
|
85
|
-
|
86
|
-
def seed
|
87
|
-
raise "no private key" if @secret_key.nil?
|
88
|
-
# TODO: improve the error class above
|
89
|
-
seed_bytes = raw_seed
|
90
|
-
Util::StrKey.check_encode(:seed, seed_bytes)
|
91
|
-
end
|
92
|
-
|
93
96
|
def sign?
|
94
97
|
!@secret_key.nil?
|
95
98
|
end
|
96
99
|
|
97
100
|
def sign(message)
|
98
|
-
raise "no private key"
|
99
|
-
|
101
|
+
raise NotImplementedError, "no private key, signing is not available" unless sign?
|
102
|
+
|
100
103
|
@secret_key.sign(message)
|
101
104
|
end
|
102
105
|
|
@@ -115,5 +118,9 @@ module Stellar
|
|
115
118
|
rescue RbNaCl::BadSignatureError
|
116
119
|
false
|
117
120
|
end
|
121
|
+
|
122
|
+
def to_keypair
|
123
|
+
self
|
124
|
+
end
|
118
125
|
end
|
119
126
|
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require "stellar/convert"
|
2
|
+
require "stellar/dsl"
|
3
|
+
|
4
|
+
module Stellar
|
5
|
+
class LedgerKey
|
6
|
+
class << self
|
7
|
+
include Stellar::DSL
|
8
|
+
|
9
|
+
def switch_for_arm(name)
|
10
|
+
(@switch_by_arm ||= switches.invert).fetch(name)
|
11
|
+
end
|
12
|
+
|
13
|
+
def from(account_id:, **options)
|
14
|
+
field, value = options.first
|
15
|
+
case field
|
16
|
+
when nil
|
17
|
+
account(account_id: KeyPair(account_id).account_id)
|
18
|
+
when :balance_id
|
19
|
+
claimable_balance(balance_id: ClaimableBalanceID.v0(Stellar::Convert.from_hex(value.to_s)))
|
20
|
+
when :offer_id
|
21
|
+
offer(seller_id: account_id, offer_id: Integer(value))
|
22
|
+
when :data_name
|
23
|
+
data(account_id: account_id, data_name: value.to_s)
|
24
|
+
when :asset
|
25
|
+
trust_line(account_id: account_id, asset: Asset(value))
|
26
|
+
else
|
27
|
+
raise ArgumentError, "unknown option #{field} (not in :asset, :offer_id, :data_name, :balance_id)"
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module Stellar
|
2
|
+
class MuxedAccount
|
3
|
+
def to_keypair
|
4
|
+
case arm
|
5
|
+
when :ed25519 then KeyPair.from_public_key(value)
|
6
|
+
when :med25519 then KeyPair.from_public_key(value.ed25519)
|
7
|
+
else
|
8
|
+
raise "impossible"
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def address
|
13
|
+
to_keypair.address
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
data/lib/stellar/networks.rb
CHANGED
@@ -12,18 +12,18 @@ module Stellar
|
|
12
12
|
# methods of specifying a network if you need two threads in the same process to communicate with
|
13
13
|
# different networks
|
14
14
|
#
|
15
|
-
# @see Stellar.default_network
|
16
15
|
# @see Stellar.on_network
|
17
|
-
|
18
|
-
|
19
|
-
|
16
|
+
mattr_accessor :default_network, default: Networks::TESTNET
|
17
|
+
|
18
|
+
# Stellar network passphrase selected for current thread
|
19
|
+
#
|
20
|
+
# @see Stellar.current_network
|
21
|
+
# @see Stellar.on_network
|
22
|
+
thread_mattr_accessor :network
|
20
23
|
|
21
|
-
# Returns the passphrase for the currently-
|
22
|
-
# or Stellar.on_network
|
24
|
+
# Returns the passphrase for the network currently active per-thread with a fallback to `Stellar.default_network`
|
23
25
|
def self.current_network
|
24
|
-
|
25
|
-
@default_network ||
|
26
|
-
Stellar::Networks::TESTNET
|
26
|
+
network.presence || default_network
|
27
27
|
end
|
28
28
|
|
29
29
|
# Returns the id for the currently configured network, suitable for use in generating
|
@@ -34,10 +34,10 @@ module Stellar
|
|
34
34
|
|
35
35
|
# Executes the provided block in the context of the provided network.
|
36
36
|
def self.on_network(passphrase, &block)
|
37
|
-
old =
|
38
|
-
|
37
|
+
old = network
|
38
|
+
self.network = passphrase
|
39
39
|
block.call
|
40
40
|
ensure
|
41
|
-
|
41
|
+
self.network = old
|
42
42
|
end
|
43
43
|
end
|
data/lib/stellar/operation.rb
CHANGED
@@ -3,8 +3,14 @@ require "bigdecimal"
|
|
3
3
|
module Stellar
|
4
4
|
class Operation
|
5
5
|
MAX_INT64 = 2**63 - 1
|
6
|
+
TRUST_LINE_FLAGS_MAPPING = {
|
7
|
+
full: Stellar::TrustLineFlags.authorized_flag,
|
8
|
+
maintain_liabilities: Stellar::TrustLineFlags.authorized_to_maintain_liabilities_flag,
|
9
|
+
clawback_enabled: Stellar::TrustLineFlags.trustline_clawback_enabled_flag
|
10
|
+
}.freeze
|
6
11
|
|
7
12
|
class << self
|
13
|
+
include Stellar::DSL
|
8
14
|
#
|
9
15
|
# Construct a new Stellar::Operation from the provided
|
10
16
|
# source account and body
|
@@ -16,16 +22,17 @@ module Stellar
|
|
16
22
|
# @return [Stellar::Operation] the built operation
|
17
23
|
def make(attributes = {})
|
18
24
|
source_account = attributes[:source_account]
|
19
|
-
body = Stellar::Operation::Body.new(*attributes[:body])
|
20
|
-
|
21
|
-
op = Stellar::Operation.new(body: body)
|
22
25
|
|
23
|
-
if source_account
|
24
|
-
raise ArgumentError, "Bad :source_account"
|
25
|
-
op.source_account = source_account.muxed_account
|
26
|
+
if source_account && !source_account.is_a?(Stellar::KeyPair)
|
27
|
+
raise ArgumentError, "Bad :source_account"
|
26
28
|
end
|
27
29
|
|
28
|
-
|
30
|
+
body = Stellar::Operation::Body.new(*attributes[:body])
|
31
|
+
|
32
|
+
Stellar::Operation.new(
|
33
|
+
body: body,
|
34
|
+
source_account: source_account&.muxed_account
|
35
|
+
)
|
29
36
|
end
|
30
37
|
|
31
38
|
#
|
@@ -168,7 +175,6 @@ module Stellar
|
|
168
175
|
}))
|
169
176
|
end
|
170
177
|
|
171
|
-
#
|
172
178
|
# Helper method to create a valid ChangeTrustOp, wrapped
|
173
179
|
# in the necessary XDR structs to be included within a
|
174
180
|
# transactions `operations` array.
|
@@ -200,6 +206,64 @@ module Stellar
|
|
200
206
|
}))
|
201
207
|
end
|
202
208
|
|
209
|
+
# Helper method to create a valid CreateClaimableBalanceOp, ready to be used
|
210
|
+
# within a transactions `operations` array.
|
211
|
+
#
|
212
|
+
# @see Stellar::DSL::Claimant
|
213
|
+
# @see https://github.com/astroband/ruby-stellar-sdk/tree/master/base/examples/claimable_balances.rb
|
214
|
+
#
|
215
|
+
# @param asset [Asset] the asset to transfer to a claimable balance
|
216
|
+
# @param amount [Fixnum] the amount of `asset` to put into a claimable balance
|
217
|
+
# @param claimants [Array<Claimant>] accounts authorized to claim the balance in the future
|
218
|
+
#
|
219
|
+
# @return [Operation] the built operation
|
220
|
+
def create_claimable_balance(asset:, amount:, claimants:, **attributes)
|
221
|
+
op = CreateClaimableBalanceOp.new(asset: asset, amount: amount, claimants: claimants)
|
222
|
+
|
223
|
+
make(attributes.merge(body: [:create_claimable_balance, op]))
|
224
|
+
end
|
225
|
+
|
226
|
+
# Helper method to create a valid CreateClaimableBalanceOp, ready to be used
|
227
|
+
# within a transactions `operations` array.
|
228
|
+
#
|
229
|
+
# @see Stellar::DSL::Claimant
|
230
|
+
# @see https://github.com/astroband/ruby-stellar-sdk/tree/master/base/examples/claimable_balances.rb
|
231
|
+
#
|
232
|
+
# @param balance_id [ClaimableBalanceID] unique ID of claimable balance
|
233
|
+
#
|
234
|
+
# @return [Operation] the built operation, containing a Stellar::ChangeTrustOp body
|
235
|
+
def claim_claimable_balance(balance_id:, **attributes)
|
236
|
+
op = ClaimClaimableBalanceOp.new(balance_id: balance_id)
|
237
|
+
|
238
|
+
make(attributes.merge(body: [:claim_claimable_balance, op]))
|
239
|
+
end
|
240
|
+
|
241
|
+
def begin_sponsoring_future_reserves(sponsored:, **attributes)
|
242
|
+
op = BeginSponsoringFutureReservesOp.new(
|
243
|
+
sponsored_id: KeyPair(sponsored).account_id
|
244
|
+
)
|
245
|
+
|
246
|
+
make(attributes.merge(body: [:begin_sponsoring_future_reserves, op]))
|
247
|
+
end
|
248
|
+
|
249
|
+
def end_sponsoring_future_reserves(**attributes)
|
250
|
+
make(attributes.merge(body: [:end_sponsoring_future_reserves]))
|
251
|
+
end
|
252
|
+
|
253
|
+
# @param sponsored [#to_keypair] owner of sponsored entry
|
254
|
+
def revoke_sponsorship(sponsored:, **attributes)
|
255
|
+
key_fields = attributes.slice(:offer_id, :data_name, :balance_id, :asset, :signer)
|
256
|
+
raise ArgumentError, "conflicting attributes: #{key_fields.keys.join(", ")}" if key_fields.size > 1
|
257
|
+
account_id = KeyPair(sponsored).account_id
|
258
|
+
key, value = key_fields.first
|
259
|
+
op = if key == :signer
|
260
|
+
RevokeSponsorshipOp.signer(account_id: account_id, signer_key: SignerKey(value))
|
261
|
+
else
|
262
|
+
RevokeSponsorshipOp.ledger_key(LedgerKey.from(account_id: account_id, **key_fields))
|
263
|
+
end
|
264
|
+
make(attributes.merge(body: [:revoke_sponsorship, op]))
|
265
|
+
end
|
266
|
+
|
203
267
|
def manage_sell_offer(attributes = {})
|
204
268
|
buying = attributes[:buying]
|
205
269
|
if buying.is_a?(Array)
|
@@ -242,7 +306,7 @@ module Stellar
|
|
242
306
|
op = ManageBuyOfferOp.new({
|
243
307
|
buying: buying,
|
244
308
|
selling: selling,
|
245
|
-
|
309
|
+
buy_amount: amount,
|
246
310
|
price: price,
|
247
311
|
offer_id: offer_id
|
248
312
|
})
|
@@ -313,11 +377,31 @@ module Stellar
|
|
313
377
|
}))
|
314
378
|
end
|
315
379
|
|
380
|
+
# @param asset [Stellar::Asset]
|
381
|
+
# @param trustor [Stellar::KeyPair]
|
382
|
+
# @param flags [{String, Symbol, Stellar::TrustLineFlags => true, false}] flags to to set or clear
|
383
|
+
# @param source_account [Stellar::KeyPair] source account (default is `nil`, which will use the source account of transaction)
|
384
|
+
def set_trust_line_flags(asset:, trustor:, flags: {}, source_account: nil)
|
385
|
+
op = Stellar::SetTrustLineFlagsOp.new
|
386
|
+
op.trustor = KeyPair(trustor).account_id
|
387
|
+
op.asset = Asset(asset)
|
388
|
+
op.attributes = Stellar::TrustLineFlags.set_clear_masks(flags)
|
389
|
+
|
390
|
+
make(
|
391
|
+
source_account: source_account,
|
392
|
+
body: [:set_trust_line_flags, op]
|
393
|
+
)
|
394
|
+
end
|
395
|
+
|
396
|
+
# DEPRECATED in favor of `set_trustline_flags`
|
316
397
|
#
|
317
398
|
# Helper method to create a valid AllowTrustOp, wrapped
|
318
399
|
# in the necessary XDR structs to be included within a
|
319
400
|
# transactions `operations` array.
|
320
401
|
#
|
402
|
+
# @deprecated Use `set_trustline_flags` operation
|
403
|
+
# See {https://github.com/stellar/stellar-protocol/blob/master/core/cap-0035.md#allow-trust-operation-1 CAP-35 description}
|
404
|
+
# for more details
|
321
405
|
# @param [Hash] attributes the attributes to create the operation with
|
322
406
|
# @option attributes [Stellar::KeyPair] :trustor
|
323
407
|
# @option attributes [Stellar::Asset] :asset
|
@@ -329,7 +413,8 @@ module Stellar
|
|
329
413
|
op = AllowTrustOp.new
|
330
414
|
|
331
415
|
trustor = attributes[:trustor]
|
332
|
-
|
416
|
+
# we handle booleans here for the backward compatibility
|
417
|
+
authorize = attributes[:authorize].yield_self { |value| value == true ? :full : value }
|
333
418
|
asset = attributes[:asset]
|
334
419
|
if asset.is_a?(Array)
|
335
420
|
asset = Asset.send(*asset)
|
@@ -337,20 +422,21 @@ module Stellar
|
|
337
422
|
|
338
423
|
raise ArgumentError, "Bad :trustor" unless trustor.is_a?(Stellar::KeyPair)
|
339
424
|
|
340
|
-
|
341
|
-
|
342
|
-
|
343
|
-
|
425
|
+
allowed_flags = TRUST_LINE_FLAGS_MAPPING.slice(:full, :maintain_liabilities)
|
426
|
+
|
427
|
+
# we handle booleans here for the backward compatibility
|
428
|
+
op.authorize = if allowed_flags.key?(authorize)
|
429
|
+
allowed_flags[authorize].value
|
430
|
+
elsif [:none, false].include?(authorize)
|
431
|
+
0
|
344
432
|
else
|
345
433
|
raise ArgumentError, "Bad :authorize, supported values: :full, :maintain_liabilities, :none"
|
346
434
|
end
|
347
435
|
|
348
436
|
raise ArgumentError, "Bad :asset" unless asset.type == Stellar::AssetType.asset_type_credit_alphanum4
|
349
437
|
|
350
|
-
atc = AllowTrustOp::Asset.new(:asset_type_credit_alphanum4, asset.code)
|
351
|
-
|
352
438
|
op.trustor = trustor.account_id
|
353
|
-
op.asset =
|
439
|
+
op.asset = AssetCode.new(:asset_type_credit_alphanum4, asset.code)
|
354
440
|
|
355
441
|
make(attributes.merge({
|
356
442
|
body: [:allow_trust, op]
|
@@ -435,6 +521,47 @@ module Stellar
|
|
435
521
|
}))
|
436
522
|
end
|
437
523
|
|
524
|
+
def clawback(source_account:, from:, amount:)
|
525
|
+
asset, amount = get_asset_amount(amount)
|
526
|
+
|
527
|
+
if amount == 0
|
528
|
+
raise ArgumentError, "Amount can not be zero"
|
529
|
+
end
|
530
|
+
|
531
|
+
if amount < 0
|
532
|
+
raise ArgumentError, "Negative amount is not allowed"
|
533
|
+
end
|
534
|
+
|
535
|
+
op = ClawbackOp.new(
|
536
|
+
amount: amount,
|
537
|
+
from: from.muxed_account,
|
538
|
+
asset: asset
|
539
|
+
)
|
540
|
+
|
541
|
+
make({
|
542
|
+
source_account: source_account,
|
543
|
+
body: [:clawback, op]
|
544
|
+
})
|
545
|
+
end
|
546
|
+
|
547
|
+
# Helper method to create clawback claimable balance operation
|
548
|
+
#
|
549
|
+
# @param [Stellar::KeyPair] source_account the attributes to create the operation with
|
550
|
+
# @param [String] balance_id `ClaimableBalanceID`, serialized in hex
|
551
|
+
#
|
552
|
+
# @return [Stellar::Operation] the built operation
|
553
|
+
def clawback_claimable_balance(source_account:, balance_id:)
|
554
|
+
balance_id = Stellar::ClaimableBalanceID.from_xdr(balance_id, :hex)
|
555
|
+
op = ClawbackClaimableBalanceOp.new(balance_id: balance_id)
|
556
|
+
|
557
|
+
make(
|
558
|
+
source_account: source_account,
|
559
|
+
body: [:clawback_claimable_balance, op]
|
560
|
+
)
|
561
|
+
rescue XDR::ReadError
|
562
|
+
raise ArgumentError, "Claimable balance id '#{balance_id}' is invalid"
|
563
|
+
end
|
564
|
+
|
438
565
|
private
|
439
566
|
|
440
567
|
def get_asset_amount(values)
|