mastercoin-ruby 0.0.5 → 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.
data/Gemfile CHANGED
@@ -8,7 +8,7 @@ source "http://rubygems.org"
8
8
 
9
9
  gem 'bitcoin-ruby', '~> 0.0.1', git: "https://github.com/maran/bitcoin-ruby.git"
10
10
  gem 'sequel', '~> 4.1.1'
11
- gem 'thor'
11
+ gem 'thor', '>= 0.14.6'
12
12
  gem 'activesupport', '>= 3.0.0'
13
13
 
14
14
  group :development do
data/Gemfile.lock CHANGED
@@ -84,4 +84,4 @@ DEPENDENCIES
84
84
  rdoc (~> 3.12)
85
85
  rspec
86
86
  sequel (~> 4.1.1)
87
- thor
87
+ thor (>= 0.14.6)
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.5
1
+ 0.1.0
@@ -12,7 +12,8 @@ module Mastercoin
12
12
  def lookup(hash)
13
13
  puts "Looking up tx with hash #{hash}"
14
14
  Mastercoin.set_storage(options[:storage])
15
- puts Mastercoin::Transaction.new(hash)
15
+ a = Mastercoin::Transaction.new(hash)
16
+ puts a.to_s
16
17
  end
17
18
  end
18
19
  end
@@ -9,11 +9,13 @@ module Mastercoin
9
9
  autoload :ExodusPayment, 'mastercoin-ruby/exodus_payment'
10
10
  autoload :Transaction, 'mastercoin-ruby/transaction'
11
11
  autoload :Message, 'mastercoin-ruby/message'
12
+ autoload :PurchaseOffer, 'mastercoin-ruby/purchase_offer'
12
13
  autoload :Util, 'mastercoin-ruby/util'
13
14
  autoload :BitcoinWrapper, 'mastercoin-ruby/bitcoin_wrapper'
14
15
 
15
16
  TRANSACTION_SIMPLE_SEND = "0"
16
17
  TRANSACTION_SELL_FOR_BITCOIN = 20
18
+ TRANSACTION_PURCHASE_BTC_TRADE = 22
17
19
 
18
20
  TRANSACTION_TYPES = {
19
21
  TRANSACTION_SIMPLE_SEND => "Simple transfer",
@@ -21,7 +23,7 @@ module Mastercoin
21
23
  "11" => "Mark compromised",
22
24
  TRANSACTION_SELL_FOR_BITCOIN => "Currency trade offer bitcoins",
23
25
  "21" => "Currency trade offer master-coin derived",
24
- "22" => "Currency trade offer accept",
26
+ TRANSACTION_PURCHASE_BTC_TRADE => "Currency trade offer accept",
25
27
  "30" => "Register data-stream",
26
28
  "40" => "Bet offer",
27
29
  "100" => "Create child currency"
@@ -13,12 +13,19 @@ module Mastercoin
13
13
 
14
14
  def self.probe_and_read(keys, xor_target)
15
15
  result = Message.probe(keys, xor_target)
16
- transaction_type = (result[0][1] + result[1][1])[2..9].to_i(16)
16
+
17
+ if result.length > 1
18
+ transaction_type = (result[0][1] + result[1][1])[2..9].to_i(16)
19
+ else
20
+ transaction_type = result[0][1][2..9].to_i(16)
21
+ end
17
22
 
18
23
  if transaction_type == Mastercoin::TRANSACTION_SELL_FOR_BITCOIN
19
24
  Mastercoin::SellingOffer.decode_from_compressed_public_key([result[0][0], result[1][0]], xor_target)
25
+ elsif transaction_type.to_s == Mastercoin::TRANSACTION_PURCHASE_BTC_TRADE.to_i.to_s
26
+ Mastercoin::PurchaseOffer.decode_from_compressed_public_key(result[0][0], xor_target)
20
27
  elsif transaction_type.to_s == Mastercoin::TRANSACTION_SIMPLE_SEND.to_s
21
- Mastercoin::SimpleSend.decode_from_compressed_public_key(keys, xor_target)
28
+ Mastercoin::SimpleSend.decode_from_compressed_public_key(result[0][0], xor_target)
22
29
  end
23
30
  end
24
31
 
@@ -0,0 +1,61 @@
1
+ module Mastercoin
2
+ class PurchaseOffer < Mastercoin::Message
3
+ attr_accessor :transaction_type, :currency_id, :amount
4
+
5
+ # Supply the amount in 'dacoinminster's
6
+ #77702fd6333eb5a67fa03b6fdb0fda04981bd671fd2a9175e2bee43340770940
7
+ def initialize(options= {})
8
+ self.transaction_type = Mastercoin::TRANSACTION_PURCHASE_BTC_TRADE
9
+ self.currency_id = options[:currency_id]
10
+ self.amount = options[:amount]
11
+ end
12
+
13
+ # hardcode the sequence for a public key simple send since it's always fits inside a public key
14
+ # Please note that we start at 01 - 00 will generate unvalid ECDSA points somehow
15
+ def public_key_sequence
16
+ 01
17
+ end
18
+
19
+ def self.decode_key_to_data(public_key)
20
+ simple_send = PurchaseOffer.new
21
+ simple_send.transaction_type = public_key[2..9].to_i(16)
22
+ simple_send.currency_id = public_key[10..17].to_i(16)
23
+ simple_send.amount = public_key[18..33].to_i(16)
24
+ return simple_send
25
+ end
26
+
27
+ def encode_data_to_key
28
+ raw = (self.public_key_sequence.to_i.to_s(16).rjust(2, "0") + self.transaction_type.to_i.to_s(16).rjust(8,"0") + self.currency_id.to_i.to_s(16).rjust(8, "0") + self.amount.to_i.to_s(16).rjust(16, "0"))
29
+ raw = raw.ljust(62,"0")
30
+ return raw
31
+ end
32
+
33
+ def encode_to_address
34
+ raw = (self.get_sequence.to_i.to_s(16).rjust(2, "0") + self.transaction_type.to_i.to_s(16).rjust(8,"0") + self.currency_id.to_i.to_s(16).rjust(8, "0") + self.amount.to_i.to_s(16).rjust(16, "0") + "000000")
35
+ Bitcoin.hash160_to_address(raw)
36
+ end
37
+
38
+ def self.decode_from_address(raw_address)
39
+ simple_send = Mastercoin::PurchaseOffer.new
40
+ decoded = Bitcoin.decode_base58(raw_address)
41
+ simple_send.sequence = decoded[2..3].to_i(16)
42
+ simple_send.transaction_type = decoded[4..11].to_i(16)
43
+ simple_send.currency_id = decoded[12..19].to_i(16)
44
+ simple_send.amount = decoded[20..35].to_i(16)
45
+ return simple_send
46
+ end
47
+
48
+ def get_sequence(bitcoin_address = nil)
49
+ bitcoin_address ||= self.receiving_address
50
+ Mastercoin::Util.get_sequence(bitcoin_address)
51
+ end
52
+
53
+ def explain
54
+ "Purchase Offer transaction for %.8f #{self.currency_id_text}." % (self.amount / 1e8)
55
+ end
56
+
57
+ def currency_id_text
58
+ Mastercoin::CURRENCY_IDS[self.currency_id.to_s]
59
+ end
60
+ end
61
+ end
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "mastercoin-ruby"
8
- s.version = "0.0.5"
8
+ s.version = "0.1.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Maran"]
12
- s.date = "2013-10-28"
12
+ s.date = "2013-11-04"
13
13
  s.description = "Basic implementation of the Mastercoin protocol."
14
14
  s.email = "maran.hidskes@gmail.com"
15
15
  s.executables = ["console", "exodus_payment", "mastercoin_transaction", "simple_send", "wallet.rb"]
@@ -36,11 +36,13 @@ Gem::Specification.new do |s|
36
36
  "lib/mastercoin-ruby/bitcoin_wrapper.rb",
37
37
  "lib/mastercoin-ruby/exodus_payment.rb",
38
38
  "lib/mastercoin-ruby/message.rb",
39
+ "lib/mastercoin-ruby/purchase_offer.rb",
39
40
  "lib/mastercoin-ruby/selling_offer.rb",
40
41
  "lib/mastercoin-ruby/simple_send.rb",
41
42
  "lib/mastercoin-ruby/transaction.rb",
42
43
  "lib/mastercoin-ruby/util.rb",
43
44
  "mastercoin-ruby.gemspec",
45
+ "spec/purchase_offer_spec.rb",
44
46
  "spec/selling_offer_spec.rb",
45
47
  "spec/simple_send_spec.rb"
46
48
  ]
@@ -56,7 +58,7 @@ Gem::Specification.new do |s|
56
58
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
57
59
  s.add_runtime_dependency(%q<bitcoin-ruby>, ["~> 0.0.1"])
58
60
  s.add_runtime_dependency(%q<sequel>, ["~> 4.1.1"])
59
- s.add_runtime_dependency(%q<thor>, [">= 0"])
61
+ s.add_runtime_dependency(%q<thor>, [">= 0.14.6"])
60
62
  s.add_runtime_dependency(%q<activesupport>, [">= 3.0.0"])
61
63
  s.add_development_dependency(%q<rdoc>, ["~> 3.12"])
62
64
  s.add_development_dependency(%q<bundler>, ["~> 1.0"])
@@ -65,7 +67,7 @@ Gem::Specification.new do |s|
65
67
  else
66
68
  s.add_dependency(%q<bitcoin-ruby>, ["~> 0.0.1"])
67
69
  s.add_dependency(%q<sequel>, ["~> 4.1.1"])
68
- s.add_dependency(%q<thor>, [">= 0"])
70
+ s.add_dependency(%q<thor>, [">= 0.14.6"])
69
71
  s.add_dependency(%q<activesupport>, [">= 3.0.0"])
70
72
  s.add_dependency(%q<rdoc>, ["~> 3.12"])
71
73
  s.add_dependency(%q<bundler>, ["~> 1.0"])
@@ -75,7 +77,7 @@ Gem::Specification.new do |s|
75
77
  else
76
78
  s.add_dependency(%q<bitcoin-ruby>, ["~> 0.0.1"])
77
79
  s.add_dependency(%q<sequel>, ["~> 4.1.1"])
78
- s.add_dependency(%q<thor>, [">= 0"])
80
+ s.add_dependency(%q<thor>, [">= 0.14.6"])
79
81
  s.add_dependency(%q<activesupport>, [">= 3.0.0"])
80
82
  s.add_dependency(%q<rdoc>, ["~> 3.12"])
81
83
  s.add_dependency(%q<bundler>, ["~> 1.0"])
@@ -0,0 +1,30 @@
1
+ require 'mastercoin-ruby'
2
+
3
+ describe Mastercoin::PurchaseOffer do
4
+ before do
5
+ @purchase_offer = Mastercoin::PurchaseOffer.new(currency_id: 2, amount: 50)
6
+ end
7
+
8
+ it "Should generate valid non-obfusciated Mastercoin keys" do
9
+ @purchase_offer.encode_data_to_key.should eq("01000000160000000200000000000000320000000000000000000000000000")
10
+ end
11
+
12
+ it "Should parse valid non-obfusciated keys" do
13
+ @purchase_offer = Mastercoin::PurchaseOffer.decode_key_to_data("01000000160000000200000000000000320000000000000000000000000000")
14
+ @purchase_offer.currency_id.should be(2)
15
+ @purchase_offer.amount.should be(50)
16
+ @purchase_offer.transaction_type.to_i.should be(22)
17
+ end
18
+
19
+ it "Should generate valid obfusciated Mastercoin keys" do
20
+ @purchase_offer.encode_to_compressed_public_key("1J2svn2GxYx9LPrpCLFikmzn9kkrXBrk8B")[0..-3].should eq("02d52c390e44f1110410078a9db148e7a334924666fb10aaaa9bffcc2e2ecde3")
21
+ @purchase_offer.transaction_type.to_i.should be(22)
22
+ end
23
+
24
+ it "Should read valid obfusciated Mastercoin keys" do
25
+ @purchase_offer = Mastercoin::PurchaseOffer.decode_from_compressed_public_key("02d52c390e44f1110410078a9db148e7a334924666fb10aaaa9bffcc2e2ecde33e", "1J2svn2GxYx9LPrpCLFikmzn9kkrXBrk8B")
26
+ @purchase_offer.currency_id.should be(2)
27
+ @purchase_offer.amount.should be(50)
28
+ @purchase_offer.transaction_type.to_i.should be(22)
29
+ end
30
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mastercoin-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-10-28 00:00:00.000000000 Z
12
+ date: 2013-11-04 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bitcoin-ruby
@@ -50,7 +50,7 @@ dependencies:
50
50
  requirements:
51
51
  - - ! '>='
52
52
  - !ruby/object:Gem::Version
53
- version: '0'
53
+ version: 0.14.6
54
54
  type: :runtime
55
55
  prerelease: false
56
56
  version_requirements: !ruby/object:Gem::Requirement
@@ -58,7 +58,7 @@ dependencies:
58
58
  requirements:
59
59
  - - ! '>='
60
60
  - !ruby/object:Gem::Version
61
- version: '0'
61
+ version: 0.14.6
62
62
  - !ruby/object:Gem::Dependency
63
63
  name: activesupport
64
64
  requirement: !ruby/object:Gem::Requirement
@@ -170,11 +170,13 @@ files:
170
170
  - lib/mastercoin-ruby/bitcoin_wrapper.rb
171
171
  - lib/mastercoin-ruby/exodus_payment.rb
172
172
  - lib/mastercoin-ruby/message.rb
173
+ - lib/mastercoin-ruby/purchase_offer.rb
173
174
  - lib/mastercoin-ruby/selling_offer.rb
174
175
  - lib/mastercoin-ruby/simple_send.rb
175
176
  - lib/mastercoin-ruby/transaction.rb
176
177
  - lib/mastercoin-ruby/util.rb
177
178
  - mastercoin-ruby.gemspec
179
+ - spec/purchase_offer_spec.rb
178
180
  - spec/selling_offer_spec.rb
179
181
  - spec/simple_send_spec.rb
180
182
  homepage: http://github.com/maran/mastercoin-ruby
@@ -192,7 +194,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
192
194
  version: '0'
193
195
  segments:
194
196
  - 0
195
- hash: -1833207625332828551
197
+ hash: 3082060516544649458
196
198
  required_rubygems_version: !ruby/object:Gem::Requirement
197
199
  none: false
198
200
  requirements: