stellar-base 0.0.20 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (62) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +8 -14
  3. data/examples/low_level_transaction_post.rb +1 -1
  4. data/examples/non_native_payment.rb +2 -2
  5. data/examples/offer.rb +5 -5
  6. data/generated/stellar-base-generated.rb +4 -5
  7. data/generated/stellar/account_flags.rb +1 -1
  8. data/generated/stellar/allow_trust_op.rb +11 -8
  9. data/generated/stellar/allow_trust_op/asset.rb +33 -0
  10. data/generated/stellar/allow_trust_result_code.rb +1 -1
  11. data/generated/stellar/asset.rb +47 -0
  12. data/generated/stellar/{currency/alpha_num.rb → asset/alpha_num12.rb} +5 -5
  13. data/generated/stellar/asset/alpha_num4.rb +22 -0
  14. data/generated/stellar/asset_type.rb +24 -0
  15. data/generated/stellar/change_trust_op.rb +2 -2
  16. data/generated/stellar/claim_offer_atom.rb +10 -10
  17. data/generated/stellar/create_passive_offer_op.rb +8 -8
  18. data/generated/stellar/crypto_key_type.rb +2 -2
  19. data/generated/stellar/ledger_key.rb +2 -2
  20. data/generated/stellar/ledger_key/offer.rb +3 -3
  21. data/generated/stellar/ledger_key/trust_line.rb +2 -2
  22. data/generated/stellar/manage_offer_op.rb +9 -9
  23. data/generated/stellar/offer_entry.rb +11 -11
  24. data/generated/stellar/{currency_type.rb → operation_meta.rb} +4 -8
  25. data/generated/stellar/path_payment_op.rb +10 -10
  26. data/generated/stellar/path_payment_result_code.rb +2 -2
  27. data/generated/stellar/payment_op.rb +2 -2
  28. data/generated/stellar/payment_result_code.rb +2 -2
  29. data/generated/stellar/public_key.rb +2 -2
  30. data/generated/stellar/simple_payment_result.rb +2 -2
  31. data/generated/stellar/transaction_meta.rb +17 -4
  32. data/generated/stellar/transaction_meta/v0.rb +22 -0
  33. data/generated/stellar/trust_line_entry.rb +4 -4
  34. data/lib/stellar-base.rb +2 -2
  35. data/lib/stellar/asset.rb +38 -0
  36. data/lib/stellar/base/version.rb +1 -1
  37. data/lib/stellar/convert.rb +1 -5
  38. data/lib/stellar/key_pair.rb +6 -6
  39. data/lib/stellar/operation.rb +24 -24
  40. data/lib/stellar/path_payment_result.rb +2 -2
  41. data/lib/stellar/util/strkey.rb +43 -0
  42. data/ruby-stellar-base.gemspec +2 -0
  43. data/spec/lib/stellar/key_pair_spec.rb +12 -12
  44. data/spec/lib/stellar/path_payment_result_spec.rb +7 -7
  45. data/spec/lib/stellar/transaction_spec.rb +1 -1
  46. data/spec/lib/stellar/util/strkey_spec.rb +46 -0
  47. data/spec/support/matchers/be_strkey.rb +9 -0
  48. data/tasks/xdr.rake +7 -8
  49. data/xdr/Stellar-SCP.x +1 -1
  50. data/xdr/Stellar-ledger-entries.x +26 -18
  51. data/xdr/Stellar-ledger.x +17 -5
  52. data/xdr/Stellar-overlay.x +1 -1
  53. data/xdr/Stellar-transaction.x +39 -36
  54. data/xdr/Stellar-types.x +2 -3
  55. metadata +43 -13
  56. data/generated/stellar/allow_trust_op/currency.rb +0 -28
  57. data/generated/stellar/currency.rb +0 -37
  58. data/lib/stellar/currency.rb +0 -39
  59. data/lib/stellar/util/base58.rb +0 -127
  60. data/spec/lib/stellar/util/base58_spec.rb +0 -74
  61. data/spec/support/matchers/be_base58_check.rb +0 -9
  62. data/xdr/StellarXDR.x +0 -11
@@ -1,39 +0,0 @@
1
- module Stellar
2
- class Currency
3
- def self.native
4
- new(:currency_type_native)
5
- end
6
-
7
- def self.alphanum(code, issuer)
8
- raise ArgumentError, "Bad :issuer" unless issuer.is_a?(KeyPair)
9
- code = normalize_code(code)
10
- an = AlphaNum.new({currency_code:code, issuer:issuer.account_id})
11
- new(:currency_type_alphanum, an)
12
- end
13
-
14
- def to_s
15
- if switch == CurrencyType.currency_type_native
16
- "native"
17
- else
18
- encoder = Stellar::Util::Base58.stellar
19
- issuer_address = encoder.check_encode(:account_id,alpha_num.issuer)
20
- "#{alpha_num.currency_code}/#{issuer_address}"
21
- end
22
- end
23
-
24
- def inspect
25
- label = switch.to_s
26
- "#<Stellar::Currency #{to_s}>"
27
- end
28
-
29
- def code
30
- self.alpha_num!.currency_code
31
- end
32
-
33
- def self.normalize_code(code)
34
- raise ArgumentError, "Invalid currency code: #{code}, must be <= 4 bytes" if code.length > 4
35
-
36
- code.ljust(4, "\x00")
37
- end
38
- end
39
- end
@@ -1,127 +0,0 @@
1
- module Stellar
2
- module Util
3
- class Base58
4
- STELLAR_ALPHABET = "gsphnaf39wBUDNEGHJKLM4PQRST7VWXYZ2bcdeCr65jkm8oFqi1tuvAxyz"
5
- BITCOIN_ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
6
- BASE = 58
7
-
8
- # TODO: improve the conversion to bitstring, perhaps `Fixnum#to_byte`?
9
- VERSION_BYTES = {
10
- none: [1].pack("C").encode("BINARY"),
11
- account_id: [0].pack("C").encode("BINARY"),
12
- seed: [33].pack("C").encode("BINARY"),
13
- }
14
-
15
- def self.stellar
16
- Thread.current[:stellar_base58] ||= new(STELLAR_ALPHABET)
17
- end
18
-
19
- def self.bitcoin
20
- Thread.current[:bitcoin_base58] ||= new(BITCOIN_ALPHABET)
21
- end
22
-
23
- def initialize(alphabet)
24
- raise ArgumentError, "Invalid alphabet length" if alphabet.length != BASE
25
- @alphabet = alphabet
26
- end
27
-
28
- def encode(byte_str)
29
- return "" if byte_str.nil? || byte_str.empty?
30
-
31
- leading_zeros = byte_str.each_byte.take_while{|b| b == 0}.length
32
- int = bytes_to_int(byte_str) # step 1
33
-
34
- encode_int(int, leading_zeros)
35
- end
36
-
37
- def check_encode(version, byte_str)
38
- version_byte = VERSION_BYTES[version]
39
- raise ArgumentError, "Invalid version: #{version}" if version_byte.blank?
40
-
41
- payload = version_byte + byte_str.dup.force_encoding("BINARY")
42
- check = checksum(payload)
43
- encode(payload + check)
44
- end
45
-
46
- def decode(str)
47
- leading_zeros = str.each_char.take_while{|c| c == @alphabet[0]}.length
48
-
49
- ("\x00" * leading_zeros) + decode_int(str)
50
- end
51
-
52
- def check_decode(expected_version, str)
53
- decoded = decode(str)
54
- version_byte = decoded[0]
55
- payload = decoded[1...-4]
56
- check = decoded[-4..-1]
57
- version = VERSION_BYTES.key(version_byte)
58
-
59
- raise ArgumentError, "Unexpected version: #{version.inspect}" if version != expected_version
60
- raise ArgumentError, "Invalid checksum" if check != checksum(decoded[0...-4])
61
- payload
62
- end
63
-
64
- def checksum(bytes)
65
- inner = Digest::SHA256.digest(bytes)
66
- Digest::SHA256.digest(inner)[0...4]
67
- end
68
-
69
- private
70
- def encode_int(int, leading_zeros=0)
71
- # algorithm:
72
- # 1. convert the bytes to a bignum
73
- # 2. turn the bignum into a array of digits, least significant first
74
- # 3. add any leading zero bytes as leading 0 digits
75
- # 4. alphabatize the digits based upon the chosen alphabet
76
- # 5. reverse the alphabetized digits (to get most significant digit first)
77
-
78
- digits = []
79
-
80
- # step 2
81
- while int > 0
82
- int, rem = int.divmod(BASE)
83
- digits.push rem
84
- end
85
-
86
- # step 3
87
- leading_zeros.times{ digits.push 0 }
88
-
89
- digits
90
- .map{|d| @alphabet[d]} # step 4
91
- .reverse # step 5
92
- .join
93
- end
94
-
95
- def decode_int(str)
96
- int = str.reverse.each_char.with_index.inject(0) do |result, (digit, index)|
97
- digit_val = @alphabet.index(digit)
98
- raise ArgumentError, "#{digit} is not a valid base58 digit" if digit_val.nil?
99
- result + (digit_val * (BASE**index))
100
- end
101
-
102
- int_to_bytes(int)
103
- end
104
-
105
- def bytes_to_int(bytes)
106
- bytes.unpack("C*").inject do |result, byte|
107
- result <<= 8
108
- result + byte
109
- end
110
- end
111
-
112
- def int_to_bytes(int)
113
- return "\x00" if int == 0
114
-
115
- bytes = []
116
- current = int
117
-
118
- while current > 0
119
- bytes.unshift(current & 0xFF)
120
- current >>= 8
121
- end
122
-
123
- bytes.pack("C*")
124
- end
125
- end
126
- end
127
- end
@@ -1,74 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Stellar::Util::Base58 do
4
- subject{ Stellar::Util::Base58.new(Stellar::Util::Base58::BITCOIN_ALPHABET) }
5
-
6
- describe "#decode" do
7
- it "properly encodes" do
8
- expect(decode "z").to eq_bytes("\x39")
9
- expect(decode "111z").to eq_bytes("\x00\x00\x00\x39")
10
- expect(decode "2UzHL").to eq_bytes("\xFF\xFF\xFF")
11
- end
12
-
13
- it "raises an ArgumentError when the string cantains an invalid character" do
14
- expect{ decode "OOO" }.to raise_error(ArgumentError)
15
- expect{ decode "\xFF" }.to raise_error(ArgumentError)
16
- expect{ decode "\x00" }.to raise_error(ArgumentError)
17
- end
18
-
19
- def decode(bytes)
20
- subject.decode(bytes)
21
- end
22
- end
23
-
24
- describe "#check_decode" do
25
- it "properly decodes" do
26
- expect(decode :none, "cKyAv51").to eq_bytes("\x39")
27
- expect(decode :seed, "RN3BaguaZ6Mi").to eq_bytes("\x00\x00\x00\x39")
28
- expect(decode :account_id, "1Ahg1iQXoss").to eq_bytes("\xFF\xFF\xFF")
29
- end
30
-
31
- it "raises an ArgumentError if the decoded version byte does not match the expected value" do
32
- expect{ decode :none, "1Ahg1iQXoss" }.to raise_error(ArgumentError)
33
- expect{ decode :seed, "cKyAv51" }.to raise_error(ArgumentError)
34
- expect{ decode :account_id, "RN3BaguaZ6Mi" }.to raise_error(ArgumentError)
35
- end
36
-
37
- it "raises an ArgumentError if the decoded value cannot be validated by the checksum" do
38
- expect{decode :seed, "RN3BaguaZ6MM"}.to raise_error(ArgumentError)
39
- end
40
-
41
- def decode(version, bytes)
42
- subject.check_decode(version, bytes)
43
- end
44
- end
45
-
46
- describe"#encode" do
47
- it "properly encodes" do
48
- expect(encode "\x39").to eq("z")
49
- expect(encode "\x00\x00\x00\x39").to eq("111z")
50
- expect(encode "\xFF\xFF\xFF").to eq("2UzHL")
51
- end
52
-
53
- def encode(bytes)
54
- subject.encode(bytes)
55
- end
56
- end
57
-
58
- describe"#check_encode" do
59
- it "properly encodes" do
60
- expect(encode :none, "\x39").to eq("cKyAv51")
61
- expect(encode :seed, "\x00\x00\x00\x39").to eq("RN3BaguaZ6Mi")
62
- expect(encode :account_id, "\xFF\xFF\xFF").to eq("1Ahg1iQXoss")
63
- end
64
-
65
- it "raises an ArgumentError when an invalid version is provided" do
66
- expect{ encode :floob, "\x39" }.to raise_error(ArgumentError)
67
- end
68
-
69
- def encode(version, bytes)
70
- subject.check_encode(version, bytes)
71
- end
72
- end
73
-
74
- end
@@ -1,9 +0,0 @@
1
- RSpec::Matchers.define :be_base58_check do |version_byte|
2
- match do |actual|
3
- begin
4
- Stellar::Util::Base58.stellar.check_decode(version_byte, actual)
5
- rescue ArgumentError
6
- false
7
- end
8
- end
9
- end
@@ -1,11 +0,0 @@
1
- %#include "generated/Stellar-SCP.h"
2
- %#include "generated/Stellar-types.h"
3
- %#include "generated/Stellar-ledger-entries.h"
4
- %#include "generated/Stellar-transaction.h"
5
- %#include "generated/Stellar-ledger.h"
6
- %#include "generated/Stellar-overlay.h"
7
-
8
- namespace stellar
9
- {
10
- }
11
-