coloredcoins 0.0.0 → 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1b7110f96832703908f365f49cc54d66bc929092
4
- data.tar.gz: c16656da6604392b8c96fcae5c5de0d49fa419b0
3
+ metadata.gz: 6dc49542256b1f86ce32aa7e6a107e7abd8fa590
4
+ data.tar.gz: fca4a8dfe2efe63b375429dcdf28e8b8e2635020
5
5
  SHA512:
6
- metadata.gz: 90c41400aecb0bb3e4f485f902cf3c6c7162b907cf71c377799d7d699df4a0f327f74d83c7ec1374b01de374d5f9ead62e55e150a150acf0265ca1edfbea9a6b
7
- data.tar.gz: 31f70f9d03eb6b28506071ccea60c0ab4c598798863341bde2e0d56848501c2707b3cfdab6415141d9c03bc0425de2f72c68d6aca0d69927988faa074e72f2fc
6
+ metadata.gz: 6ab10c67df4296f21fc45b78751860bd191c5cea6504c5f443fd9e43016aa382ef25f683620986f206d65b9f00eae5910a08f2ddd3bd23a1415b84cf57dbfb49
7
+ data.tar.gz: 16887fe9d1b9eede983785c6fe111aa808ed05dbe9071da01b67bf62f095aea6f79bb8f5b70b2cde2e5688025f847b667b5e9aaed5d041fb31a67b5d89a65282
data/README.md CHANGED
@@ -1,5 +1,6 @@
1
1
  # Coloredcoins
2
2
 
3
+ [![Gem Version][rubygems-image]][rubygems-url]
3
4
  [![Build Status][travis-image]][travis-url]
4
5
  [![Coverage Status][coverage-image]][coverage-url]
5
6
 
@@ -122,6 +123,8 @@ Coloredcoins.asset_metadata(asset_id, utxo)
122
123
  4. Push to the branch (`git push origin my-new-feature`)
123
124
  5. Create a new Pull Request
124
125
 
126
+ [rubygems-image]: https://badge.fury.io/rb/coloredcoins.svg
127
+ [rubygems-url]: https://badge.fury.io/rb/coloredcoins
125
128
  [travis-image]: https://travis-ci.org/genaromadrid/coloredcoins-ruby.svg?branch=master
126
129
  [travis-url]: https://travis-ci.org/genaromadrid/coloredcoins-ruby
127
130
  [coverage-image]: https://coveralls.io/repos/github/genaromadrid/coloredcoins-ruby/badge.svg?branch=master
data/lib/coloredcoins.rb CHANGED
@@ -1,10 +1,12 @@
1
+ require 'bitcoin'
1
2
  require 'coloredcoins/version'
2
3
 
3
4
  module Coloredcoins
4
- autoload :API, 'coloredcoins/api'
5
- autoload :Connection, 'coloredcoins/connection'
5
+ autoload :API, 'coloredcoins/api'
6
+ autoload :Connection, 'coloredcoins/connection'
6
7
  autoload :Transaction, 'coloredcoins/transaction'
7
- autoload :MultisigTx, 'coloredcoins/multisig_tx'
8
+ autoload :Multisig, 'coloredcoins/multisig'
9
+ autoload :MultisigTx, 'coloredcoins/multisig_tx'
8
10
 
9
11
  attr_writer :api
10
12
 
@@ -0,0 +1,47 @@
1
+ module Coloredcoins
2
+ class Multisig
3
+ attr_reader :m, :pub_keys
4
+ attr_accessor :redeem_script
5
+
6
+ def self.valid_sig?(i, script)
7
+ tx.verify_input_signature(i, script)
8
+ end
9
+
10
+ def initialize(m = nil, pub_keys = nil)
11
+ @m = m
12
+ @pub_keys = pub_keys
13
+ end
14
+
15
+ def sign(tx, key)
16
+ key = Coloredcoins::Transaction.build_key(key)
17
+ tx.inputs.each_with_index do |input, i|
18
+ sig_hash = tx.signature_hash_for_input(i, redeem_script)
19
+ sigs = Coloredcoins::Transaction.build_sigs(key, sig_hash)
20
+ initial = input.script_sig.or(initial_script)
21
+
22
+ input.script_sig = build_script_sig(sigs, sig_hash, initial)
23
+ end
24
+ end
25
+
26
+ def address
27
+ @address ||= Bitcoin.hash160_to_p2sh_address(Bitcoin.hash160(redeem_script.hth))
28
+ end
29
+
30
+ def redeem_script
31
+ @redeem_script ||= Bitcoin::Script.to_p2sh_multisig_script(m, *pub_keys).last
32
+ end
33
+
34
+ private
35
+
36
+ def initial_script
37
+ @initial_script ||= Bitcoin::Script.to_p2sh_multisig_script_sig(redeem_script)
38
+ end
39
+
40
+ def build_script_sig(sigs, sig_hash, initial)
41
+ sigs.each do |sig|
42
+ Bitcoin::Script.add_sig_to_multisig_script_sig(sig, initial)
43
+ end
44
+ Bitcoin::Script.sort_p2sh_multisig_signatures(initial, sig_hash)
45
+ end
46
+ end
47
+ end
@@ -1,6 +1,9 @@
1
1
  module Coloredcoins
2
2
  class MultisigTx < Transaction
3
- attr_accessor :m, :pub_keys, :redeem_script
3
+ attr_accessor :m,
4
+ :pub_keys,
5
+ :redeem_script,
6
+ :multisig
4
7
 
5
8
  def self.build(tx_hex)
6
9
  transaction = MultisigTx.new(tx_hex)
@@ -10,45 +13,23 @@ module Coloredcoins
10
13
 
11
14
  def sign(key)
12
15
  check
13
- key = build_key(key)
14
- tx.inputs.each_with_index do |input, i|
15
- sig_hash = tx.signature_hash_for_input(i, redeem_script)
16
- sigs = build_sigs(key, sig_hash)
17
- initial = input.script_sig.or(initial_script)
18
-
19
- input.script_sig = build_script_sig(sigs, sig_hash, initial)
20
- end
16
+ multisig.sign(tx, key)
21
17
  true
22
18
  end
23
19
 
24
- def redeem_script
25
- @redeem_script ||= Bitcoin::Script.to_p2sh_multisig_script(m, *pub_keys).last
20
+ def multisig
21
+ @multisig ||= Coloredcoins::Multisig.new(m, pub_keys)
26
22
  end
27
23
 
28
- def address
29
- @address ||= Bitcoin.hash160_to_p2sh_address(Bitcoin.hash160(redeem_script.hth))
24
+ def redeem_script=(script)
25
+ multisig.redeem_script = script
30
26
  end
31
27
 
32
28
  private
33
29
 
34
- def initial_script
35
- @initial_script ||= Bitcoin::Script.to_p2sh_multisig_script_sig(redeem_script)
36
- end
37
-
38
- def build_script_sig(sigs, sig_hash, initial)
39
- sigs.each do |sig|
40
- Bitcoin::Script.add_sig_to_multisig_script_sig(sig, initial)
41
- end
42
- Bitcoin::Script.sort_p2sh_multisig_signatures(initial, sig_hash)
43
- end
44
-
45
- def valid_sig?(i, script)
46
- tx.verify_input_signature(i, script)
47
- end
48
-
49
30
  def check
50
- raise ArgumentError, 'Set "m" before signing' unless m
51
- if !pub_keys && !redeem_script
31
+ raise ArgumentError, 'Set "m" before signing' unless multisig.m
32
+ if !multisig.pub_keys && !multisig.redeem_script
52
33
  raise ArgumentError, 'Set "pub_keys" or "redeem_script" before signing'
53
34
  end
54
35
  end
@@ -1,9 +1,20 @@
1
- require 'bitcoin'
2
-
3
1
  module Coloredcoins
4
2
  class Transaction
5
3
  attr_reader :tx
6
4
 
5
+ def self.build_key(key)
6
+ return key if key.is_a?(Array)
7
+ key = Bitcoin::Key.from_base58(key) unless key.is_a?(Bitcoin::Key)
8
+ key
9
+ rescue RuntimeError => e
10
+ raise InvalidKeyError, 'Invalid key' if e.message == 'Invalid version'
11
+ end
12
+
13
+ def self.build_sigs(key, sig_hash)
14
+ key = [key] unless key.is_a?(Array)
15
+ key.map { |k| k.sign(sig_hash) }
16
+ end
17
+
7
18
  def initialize(hex)
8
19
  @tx = Bitcoin::P::Tx.new([hex].pack('H*'))
9
20
  end
@@ -27,20 +38,5 @@ module Coloredcoins
27
38
  return response[:txId] if response[:txId]
28
39
  response
29
40
  end
30
-
31
- protected
32
-
33
- def build_key(key)
34
- return key if key.is_a?(Array)
35
- key = Bitcoin::Key.from_base58(key) unless key.is_a?(Bitcoin::Key)
36
- key
37
- rescue RuntimeError => e
38
- raise InvalidKeyError, 'Invalid key' if e.message == 'Invalid version'
39
- end
40
-
41
- def build_sigs(key, sig_hash)
42
- key = [key] unless key.is_a?(Array)
43
- key.map { |k| k.sign(sig_hash) }
44
- end
45
41
  end
46
42
  end
@@ -1,3 +1,3 @@
1
1
  module Coloredcoins
2
- VERSION = '0.0.0'.freeze
2
+ VERSION = '0.0.1'.freeze
3
3
  end
@@ -27,7 +27,7 @@ describe Coloredcoins::API do
27
27
  end
28
28
  end
29
29
 
30
- describe 'conncetion methods' do
30
+ describe 'connection methods' do
31
31
  before do
32
32
  allow(subject.connection).to receive(:post)
33
33
  allow(subject.connection).to receive(:get)
@@ -74,5 +74,27 @@ describe Coloredcoins::API do
74
74
  expect(subject.connection).to have_received(:get).with(/assetmetadata/)
75
75
  end
76
76
  end
77
+
78
+ describe 'called from class' do
79
+ before do
80
+ @api = Coloredcoins::API.new
81
+ allow(Coloredcoins).to receive(:api).and_return @api
82
+ allow(@api).to receive(:send).and_return true
83
+ end
84
+
85
+ %w(
86
+ issue_asset
87
+ send_asset
88
+ broadcast
89
+ address_info
90
+ asset_holders
91
+ asset_metadata
92
+ ).each do |method|
93
+ it do
94
+ Coloredcoins.send(method)
95
+ expect(@api).to have_received(:send)
96
+ end
97
+ end
98
+ end
77
99
  end
78
100
  end
@@ -54,7 +54,7 @@ describe Coloredcoins::MultisigTx do
54
54
  end
55
55
 
56
56
  before do
57
- Bitcoin.network = :testnet
57
+ Bitcoin.network = :testnet3
58
58
  Coloredcoins.network = Coloredcoins::TESTNET
59
59
  end
60
60
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: coloredcoins
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.0
4
+ version: 0.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Genaro Madrid
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-02-26 00:00:00.000000000 Z
11
+ date: 2016-03-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rest-client
@@ -178,6 +178,7 @@ files:
178
178
  - lib/coloredcoins.rb
179
179
  - lib/coloredcoins/api.rb
180
180
  - lib/coloredcoins/connection.rb
181
+ - lib/coloredcoins/multisig.rb
181
182
  - lib/coloredcoins/multisig_tx.rb
182
183
  - lib/coloredcoins/transaction.rb
183
184
  - lib/coloredcoins/version.rb