keoken 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 357cbf1ad803c7daee79a96d0b5abe0da874186305112c34cd3e1bd49420960e
4
+ data.tar.gz: 0dd555b839dcdc945dc8c8e601cf51918978fca90205242a1c8e7de46f1dace8
5
+ SHA512:
6
+ metadata.gz: 733de94303f8ef40533231075bd0e8482b9851ad7fdce208012f36b778d6745d8f96aea1255ad7e34540261bcfa0965c2600eb77b6fe0372e0991562e0e414ec
7
+ data.tar.gz: b374b16d171f9cbc15a2fac64d9c8407a492485b003ae9f5069486c9fda0305293534b871da402975e0c887e238b869c9ec784fdb803fc5deae0d49c613a6d4e
data/.gitignore ADDED
@@ -0,0 +1 @@
1
+ .byebug_history
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --require spec_helper
2
+ --format documentation
3
+ --color
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,53 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ keoken (0.0.1)
5
+ bitcoin-ruby (~> 0.0.18)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ addressable (2.5.2)
11
+ public_suffix (>= 2.0.2, < 4.0)
12
+ bitcoin-ruby (0.0.18)
13
+ byebug (10.0.2)
14
+ crack (0.4.3)
15
+ safe_yaml (~> 1.0.0)
16
+ diff-lcs (1.3)
17
+ ffi (1.9.25)
18
+ hashdiff (0.3.2)
19
+ public_suffix (3.0.3)
20
+ rake (12.3.2)
21
+ rspec (3.8.0)
22
+ rspec-core (~> 3.8.0)
23
+ rspec-expectations (~> 3.8.0)
24
+ rspec-mocks (~> 3.8.0)
25
+ rspec-core (3.8.0)
26
+ rspec-support (~> 3.8.0)
27
+ rspec-expectations (3.8.2)
28
+ diff-lcs (>= 1.2.0, < 2.0)
29
+ rspec-support (~> 3.8.0)
30
+ rspec-mocks (3.8.0)
31
+ diff-lcs (>= 1.2.0, < 2.0)
32
+ rspec-support (~> 3.8.0)
33
+ rspec-support (3.8.0)
34
+ safe_yaml (1.0.4)
35
+ webmock (2.3.2)
36
+ addressable (>= 2.3.6)
37
+ crack (>= 0.3.2)
38
+ hashdiff
39
+
40
+ PLATFORMS
41
+ ruby
42
+
43
+ DEPENDENCIES
44
+ bundler (~> 1.11)
45
+ byebug (~> 10)
46
+ ffi (~> 1.9)
47
+ keoken!
48
+ rake (~> 12)
49
+ rspec (~> 3)
50
+ webmock (~> 2.3)
51
+
52
+ BUNDLED WITH
53
+ 1.16.4
data/keoken.gemspec ADDED
@@ -0,0 +1,26 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'keoken/version'
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = 'keoken'
7
+ s.version = Keoken::VERSION
8
+ s.date = '2019-01-24'
9
+ s.summary = 'Keoken protocol implemented in Ruby'
10
+ s.description = 'Provides an interface to Keoken protocol'
11
+ s.authors = ['Bitex']
12
+ s.email = 'developers@bitex.la'
13
+ s.files = `git ls-files`.split("\n")
14
+ s.require_paths = ['lib']
15
+ s.homepage = 'http://rubygems.org/gems/keoken'
16
+ s.license = 'MIT'
17
+
18
+ s.add_dependency 'bitcoin-ruby', '~> 0.0.18'
19
+
20
+ s.add_development_dependency "bundler", "~> 1.11"
21
+ s.add_development_dependency "rake", "~> 12"
22
+ s.add_development_dependency "rspec", "~> 3"
23
+ s.add_development_dependency "webmock", "~> 2.3"
24
+ s.add_development_dependency "byebug", "~> 10"
25
+ s.add_development_dependency "ffi", "~> 1.9"
26
+ end
@@ -0,0 +1,7 @@
1
+ Bitprim:
2
+ node:
3
+ testnet:
4
+ url: https://tbch.blockdozer.com/insight-api/
5
+ mainnet:
6
+ url: https://bch.blockdozer.com/insight-api/
7
+
@@ -0,0 +1,37 @@
1
+ require 'yaml'
2
+ require 'net/http'
3
+
4
+ module Keoken
5
+ module Bitprim
6
+ class Transaction
7
+ attr_reader :raw
8
+
9
+ def initialize(raw)
10
+ @raw = raw
11
+ end
12
+
13
+ def send_tx
14
+ uri = URI("#{root_url}tx/send")
15
+ req = Net::HTTP::Post.new(uri)
16
+ req.set_form_data(rawtx: @raw)
17
+
18
+ res = Net::HTTP.start(uri.hostname, uri.port) do |http|
19
+ http.request(req)
20
+ end
21
+
22
+ return res.value if res != Net::HTTPSuccess
23
+ end
24
+
25
+ private
26
+
27
+ def root_url
28
+ file = YAML.load_file('lib/keoken/bitprim/config.yaml')
29
+ if ENV['KEOKEN_NODE'] == 'PRODUCTION'
30
+ file['Bitprim']['node']['mainnet']['url']
31
+ else
32
+ file['Bitprim']['node']['testnet']['url']
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,7 @@
1
+ module Keoken
2
+ class IdNotFound < StandardError
3
+ def initialize(msg='Missing id')
4
+ super
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,8 @@
1
+ module Keoken
2
+ class NameNotFound < StandardError
3
+ def initialize(msg='Missing name')
4
+ super
5
+ end
6
+ end
7
+ end
8
+
@@ -0,0 +1,5 @@
1
+ class Bitcoin::Script
2
+ def self.to_custom_script(data=nil)
3
+ [data].pack("H*")
4
+ end
5
+ end
@@ -0,0 +1,50 @@
1
+ module Keoken
2
+ class Token
3
+ attr_accessor :hex, :name, :id
4
+
5
+ def initialize(options = {})
6
+ @name = options[:name]
7
+ @id = options[:id]
8
+ end
9
+
10
+ def create(amount)
11
+ raise Keoken::NameNotFound unless @name
12
+ data_script =
13
+ [Keoken::VERSION_NODE,
14
+ Keoken::TYPE_CREATE_ASSET,
15
+ name_to_hex(@name),
16
+ Keoken::PREFIX_BYTE_AMOUNT[0..(Keoken::AMOUNT_SIZE - amount.to_s.length - 1)] + amount.to_s].flatten.join
17
+
18
+ data_length = data_script.htb.bytesize.to_s(16)
19
+
20
+ @hex = [Bitcoin::Script::OP_RETURN.to_s(16),
21
+ Keoken::PREFIX_SIZE,
22
+ Keoken::PREFIX,
23
+ data_length].join + data_script
24
+ self
25
+ end
26
+
27
+ def send_amount(amount)
28
+ raise Keoken::IdNotFound unless @id
29
+ data_script =
30
+ [Keoken::VERSION_NODE,
31
+ Keoken::TYPE_SEND_TOKEN,
32
+ Keoken::PREFIX_BYTE_ASSET_ID[0..(Keoken::ASSET_ID_SIZE - @id.to_s.length - 1)] + @id.to_s,
33
+ Keoken::PREFIX_BYTE_AMOUNT[0..(Keoken::AMOUNT_SIZE - amount.to_s.length - 1)] + amount.to_s].flatten.join
34
+
35
+ data_length = data_script.htb.bytesize.to_s(16)
36
+
37
+ @hex = [Bitcoin::Script::OP_RETURN.to_s(16),
38
+ Keoken::PREFIX_SIZE,
39
+ Keoken::PREFIX,
40
+ data_length].join + data_script
41
+ self
42
+ end
43
+
44
+ private
45
+ def name_to_hex(name)
46
+ asset_bytes = name.bytes.map{|n| n.to_s(16)}
47
+ asset_bytes + ['00']
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,60 @@
1
+ module Keoken
2
+ module Transaction
3
+ class Token
4
+ attr_accessor :to_json, :raw
5
+ extend Bitcoin::Builder
6
+
7
+ def self.create(tx_id, position, input_script, input_amount, output_amount, key, script)
8
+ token = self.new
9
+ tx = build_tx do |t|
10
+ t.input do |i|
11
+ i.prev_out(tx_id, position, input_script.htb, input_amount, 0)
12
+ i.signature_key(key)
13
+ end
14
+ t.output do |o|
15
+ o.value output_amount
16
+ o.script do |s|
17
+ s.recipient(key.addr)
18
+ end
19
+ end
20
+ t.output do |o|
21
+ o.value 0
22
+ o.to(script, :custom)
23
+ end
24
+ end
25
+ token.to_json = tx.to_json
26
+ token.raw = tx.to_payload.bth
27
+ token
28
+ end
29
+
30
+ def self.send_amount(tx_id, position, input_script, input_amount, output_amount, output_amount_to_addr2, key, script)
31
+ token = self.new
32
+ tx = build_tx do |t|
33
+ t.input do |i|
34
+ i.prev_out(tx_id, position, script.htb, input_amount, 0)
35
+ i.signature_key(key)
36
+ end
37
+ t.output do |o|
38
+ o.value output_amount_to_addr2
39
+ o.script do |s|
40
+ s.recipient(key2.addr)
41
+ end
42
+ end
43
+ t.output do |o|
44
+ o.value output_amount
45
+ o.script do |s|
46
+ s.recipient(key.addr)
47
+ end
48
+ end
49
+ t.output do |o|
50
+ o.value 0
51
+ o.to(script, :custom)
52
+ end
53
+ end
54
+ token.to_json = tx.to_json
55
+ token.raw = tx.to_payload.bth
56
+ token
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,3 @@
1
+ module Keoken
2
+ VERSION = "0.0.1"
3
+ end
data/lib/keoken.rb ADDED
@@ -0,0 +1,20 @@
1
+ require 'bitcoin'
2
+
3
+ require 'keoken/extensions/bitcoin/script'
4
+ require 'keoken/token'
5
+ require 'keoken/transaction/token'
6
+ require 'keoken/bitprim/transaction'
7
+ require 'keoken/errors/id_not_found'
8
+ require 'keoken/errors/name_not_found'
9
+
10
+ module Keoken
11
+ PREFIX_SIZE = '04'
12
+ PREFIX = '00004b50'
13
+ VERSION_NODE = '0000'
14
+ TYPE_CREATE_ASSET = '0000'
15
+ TYPE_SEND_TOKEN = '0001'
16
+ PREFIX_BYTE_AMOUNT = '0000000000000000'
17
+ AMOUNT_SIZE = 16
18
+ PREFIX_BYTE_ASSET_ID = '00000000'
19
+ ASSET_ID_SIZE = 8
20
+ end
@@ -0,0 +1,100 @@
1
+ require 'spec_helper'
2
+
3
+ describe Keoken do
4
+ it 'has a version number' do
5
+ expect(Keoken::VERSION).not_to be nil
6
+ end
7
+
8
+ it 'defines keoken constants' do
9
+ expect(Keoken::PREFIX_SIZE).not_to be nil
10
+ expect(Keoken::PREFIX).not_to be nil
11
+ expect(Keoken::VERSION_NODE).not_to be nil
12
+ expect(Keoken::TYPE_CREATE_ASSET).not_to be nil
13
+ expect(Keoken::TYPE_SEND_TOKEN).not_to be nil
14
+ expect(Keoken::PREFIX_BYTE_AMOUNT).not_to be nil
15
+ expect(Keoken::AMOUNT_SIZE).not_to be nil
16
+ expect(Keoken::PREFIX_BYTE_ASSET_ID).not_to be nil
17
+ expect(Keoken::ASSET_ID_SIZE).not_to be nil
18
+ end
19
+
20
+ it 'creates the test-keoken token' do
21
+ token = Keoken::Token.new(name: 'test-keoken')
22
+ token.create(1_000_000)
23
+ expect(token.hex).to(
24
+ eq('6a0400004b501800000000746573742d6b656f6b656e000000000001000000')
25
+ )
26
+ end
27
+
28
+ it 'raise an error when name is not provided' do
29
+ token = Keoken::Token.new
30
+ expect { token.create(1_000_000) }.to raise_error(Keoken::NameNotFound)
31
+ end
32
+
33
+ it 'raise an error when id is not provided' do
34
+ token = Keoken::Token.new
35
+ expect { token.send_amount(1_000_000) }.to raise_error(Keoken::IdNotFound)
36
+ end
37
+
38
+ it 'send 1_000_000 to token with 34 id' do
39
+ token = Keoken::Token.new(id: 34)
40
+ token.send_amount(1_000_000)
41
+ expect(token.hex).to(
42
+ eq('6a0400004b501000000001000000340000000001000000')
43
+ )
44
+ end
45
+
46
+ describe 'creates bitcoin transaction' do
47
+ before(:each) do
48
+ Bitcoin.network = :testnet3
49
+ token = Keoken::Token.new(name: 'test-keoken')
50
+ token.create(1_000_000)
51
+ tx_id = 'aa699dc5ddf598a50dc2cb2cb2729629cb9d2d865df38e4367d13f81ef55f96e'
52
+ input_script = '76a9147bb97684cc43e2f8ea0ed1d50dddce3ebf80063888ac'
53
+ position = 0
54
+ script = token.hex
55
+ input_amount = 5_0000_0000
56
+ output_amount = 4_9991_0000
57
+ key = Bitcoin::Key.from_base58("cShKfHoHVf6iKKZym18ip1MJFQFxJwbcLxW53MQikxdDsGd2oxBU")
58
+ @transaction_token = Keoken::Transaction::Token.create(tx_id, position, input_script, input_amount, output_amount, key, script)
59
+ end
60
+
61
+ it 'format to_json' do
62
+ json = JSON.parse(@transaction_token.to_json)
63
+ expect(json['out']).to(
64
+ eq(
65
+ [
66
+ {
67
+ "value" => "4.99910000",
68
+ "scriptPubKey" => "OP_DUP OP_HASH160 7bb97684cc43e2f8ea0ed1d50dddce3ebf800638 OP_EQUALVERIFY OP_CHECKSIG"
69
+ },
70
+ {
71
+ "value" => "0.00000000",
72
+ "scriptPubKey" => "OP_RETURN 00004b50 00000000746573742d6b656f6b656e000000000001000000"
73
+ }
74
+ ]
75
+ )
76
+ )
77
+ end
78
+
79
+ it 'raw transaction' do
80
+ raw = @transaction_token.raw
81
+ expect(raw).to start_with('01000000016ef955ef813fd167438ef35d862d9dcb299672b22ccbc20da598f5ddc59d69aa00000000')
82
+ expect(raw).to end_with('6a0400004b501800000000746573742d6b656f6b656e00000000000100000000000000')
83
+ end
84
+
85
+ it 'broadcast transaction' do
86
+ stub_request(:post, 'http://tbch.blockdozer.com:443/insight-api/tx/send')
87
+ .with(:body => {"rawtx"=> /01000000016ef955ef813fd167438ef35d862d9dcb/},
88
+ headers: {
89
+ 'Accept' => '*/*',
90
+ 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
91
+ 'Content-Type' => 'application/x-www-form-urlencoded',
92
+ 'Host' => 'tbch.blockdozer.com',
93
+ 'User-Agent' => 'Ruby'
94
+ })
95
+ .to_return(status: 200, body: '', headers: {})
96
+ transaction = Keoken::Bitprim::Transaction.new(@transaction_token.raw)
97
+ expect(transaction.send_tx).to be_nil
98
+ end
99
+ end
100
+ end
@@ -0,0 +1,18 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+
3
+ require 'keoken'
4
+ require 'webmock/rspec'
5
+ require 'byebug'
6
+
7
+ RSpec.configure do |config|
8
+ config.expect_with :rspec do |expectations|
9
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
10
+ end
11
+
12
+ config.mock_with :rspec do |mocks|
13
+ mocks.verify_partial_doubles = true
14
+ end
15
+
16
+ config.shared_context_metadata_behavior = :apply_to_host_groups
17
+
18
+ end
metadata ADDED
@@ -0,0 +1,157 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: keoken
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Bitex
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-01-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bitcoin-ruby
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.0.18
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.0.18
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.11'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.11'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '12'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '12'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3'
69
+ - !ruby/object:Gem::Dependency
70
+ name: webmock
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '2.3'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '2.3'
83
+ - !ruby/object:Gem::Dependency
84
+ name: byebug
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '10'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '10'
97
+ - !ruby/object:Gem::Dependency
98
+ name: ffi
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '1.9'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '1.9'
111
+ description: Provides an interface to Keoken protocol
112
+ email: developers@bitex.la
113
+ executables: []
114
+ extensions: []
115
+ extra_rdoc_files: []
116
+ files:
117
+ - ".gitignore"
118
+ - ".rspec"
119
+ - Gemfile
120
+ - Gemfile.lock
121
+ - keoken.gemspec
122
+ - lib/keoken.rb
123
+ - lib/keoken/bitprim/config.yaml
124
+ - lib/keoken/bitprim/transaction.rb
125
+ - lib/keoken/errors/id_not_found.rb
126
+ - lib/keoken/errors/name_not_found.rb
127
+ - lib/keoken/extensions/bitcoin/script.rb
128
+ - lib/keoken/token.rb
129
+ - lib/keoken/transaction/token.rb
130
+ - lib/keoken/version.rb
131
+ - spec/keoken_spec.rb
132
+ - spec/spec_helper.rb
133
+ homepage: http://rubygems.org/gems/keoken
134
+ licenses:
135
+ - MIT
136
+ metadata: {}
137
+ post_install_message:
138
+ rdoc_options: []
139
+ require_paths:
140
+ - lib
141
+ required_ruby_version: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ required_rubygems_version: !ruby/object:Gem::Requirement
147
+ requirements:
148
+ - - ">="
149
+ - !ruby/object:Gem::Version
150
+ version: '0'
151
+ requirements: []
152
+ rubyforge_project:
153
+ rubygems_version: 2.7.7
154
+ signing_key:
155
+ specification_version: 4
156
+ summary: Keoken protocol implemented in Ruby
157
+ test_files: []