keoken 0.2.0 → 0.2.1
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.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/lib/keoken/parser.rb +25 -17
- data/lib/keoken/token.rb +20 -16
- data/lib/keoken/version.rb +1 -1
- data/lib/keoken.rb +1 -1
- data/spec/keoken_spec.rb +23 -14
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 15695594d36a454d57720a31ba196856d28df82d10d2e9209ba436487458da64
|
4
|
+
data.tar.gz: e9f4b18afbad07bac82b0c2b5c001f186a2c5e2c13088c51f7a7369ff7d8f478
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 209f756479dce58ecbcc2754fdea37a0cff43018ffbcab3273e5660a466c8d058d03b7a364cba53c8b56a19dd944cb10adc01972a3953e7690acb057c8266fdf
|
7
|
+
data.tar.gz: e886697b6477af041787fa9152a998efaa08e3bd1c95087f2b5ea5665df37c68849599c259376f6938e58ae583082f94a541cbefd05e4d4cd60cc6e1f84496cf
|
data/Gemfile.lock
CHANGED
data/lib/keoken/parser.rb
CHANGED
@@ -6,26 +6,28 @@ module Keoken
|
|
6
6
|
@data = binary.bytes
|
7
7
|
end
|
8
8
|
|
9
|
+
protected
|
10
|
+
|
11
|
+
# rubocop:disable Metrics/AbcSize
|
9
12
|
def prefix
|
10
|
-
|
11
|
-
raise Keoken::DataNotParsed, '
|
12
|
-
result = @data.slice!(0)
|
13
|
-
raise Keoken::DataNotParsed, 'Prefix size missing' unless result == Keoken::PREFIX_SIZE.to_i
|
13
|
+
raise Keoken::DataNotParsed, 'OP_RETURN missing' unless @data.slice!(0) == Bitcoin::Script::OP_RETURN.to_i
|
14
|
+
raise Keoken::DataNotParsed, 'Prefix size missing' unless @data.slice!(0) == Keoken::PREFIX_SIZE.to_i
|
14
15
|
result = @data.slice!(0..Keoken::PREFIX.htb.bytes.length - 1)
|
15
16
|
raise Keoken::DataNotParsed, 'Prefix not provided' unless result == Keoken::PREFIX.htb.bytes
|
16
|
-
|
17
|
-
raise Keoken::DataNotParsed, 'Bytesize not provided' unless bytesize == @data.length
|
17
|
+
raise Keoken::DataNotParsed, 'Bytesize not provided' unless @data.slice!(0) == @data.length
|
18
18
|
end
|
19
|
+
# rubocop:enable Metrics/AbcSize
|
19
20
|
|
20
21
|
def set_transaction_type
|
21
22
|
result = @data.slice!(0..3).join
|
22
|
-
@transaction_type =
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
23
|
+
@transaction_type =
|
24
|
+
if result == Keoken::TYPE_CREATE_ASSET
|
25
|
+
:create
|
26
|
+
elsif result == Keoken::TYPE_SEND_TOKEN
|
27
|
+
:send
|
28
|
+
else
|
29
|
+
raise Keoken::DataNotParsed, 'Transaction type not valid'
|
30
|
+
end
|
29
31
|
@transaction_type
|
30
32
|
end
|
31
33
|
|
@@ -39,15 +41,21 @@ module Keoken
|
|
39
41
|
break if tmp.zero? && end_of_name
|
40
42
|
name.push tmp
|
41
43
|
end
|
44
|
+
name_or_id_to_hex(name)
|
45
|
+
end
|
46
|
+
|
47
|
+
def set_amount
|
48
|
+
@data.map { |datum| datum.to_s(16) }.join.to_i(16)
|
49
|
+
end
|
50
|
+
|
51
|
+
private
|
52
|
+
|
53
|
+
def name_or_id_to_hex(name)
|
42
54
|
if @transaction_type == :create
|
43
55
|
name.map { |n| n.to_s(16).htb }.join
|
44
56
|
elsif @transaction_type == :send
|
45
57
|
name.map { |n| n.to_s(16) }.join
|
46
58
|
end
|
47
59
|
end
|
48
|
-
|
49
|
-
def amount
|
50
|
-
@data.map { |byte| byte.zero? ? "#{byte}0" : byte.to_s }.join.to_i
|
51
|
-
end
|
52
60
|
end
|
53
61
|
end
|
data/lib/keoken/token.rb
CHANGED
@@ -1,16 +1,20 @@
|
|
1
1
|
module Keoken
|
2
|
-
class Token
|
3
|
-
attr_accessor :data_script, :
|
2
|
+
class Token < Parser
|
3
|
+
attr_accessor :data_script, :id
|
4
4
|
|
5
5
|
# Creates a new token object.
|
6
6
|
#
|
7
7
|
# @param options [Hash] options parameters to create the token.
|
8
8
|
# @option options [String] :name The name of token to create.
|
9
9
|
# @option options [Number] :id The id of token to obtain an amount to send to another address.
|
10
|
+
# @option options [String] :script An hexadecimal script intended to be parsed.
|
10
11
|
#
|
11
12
|
def initialize(options = {})
|
12
13
|
@name = options[:name]
|
13
14
|
@id = options[:id]
|
15
|
+
return unless options[:script]
|
16
|
+
super(options[:script])
|
17
|
+
parse_script
|
14
18
|
end
|
15
19
|
|
16
20
|
# Generate the script to create a token.
|
@@ -26,7 +30,7 @@ module Keoken
|
|
26
30
|
Keoken::VERSION_NODE,
|
27
31
|
Keoken::TYPE_CREATE_ASSET,
|
28
32
|
name_to_hex(@name),
|
29
|
-
Keoken::PREFIX_BYTE_AMOUNT[0..prefix_length(amount)] + amount.to_s
|
33
|
+
Keoken::PREFIX_BYTE_AMOUNT[0..prefix_length(amount)] + amount.to_s(16)
|
30
34
|
].flatten.join
|
31
35
|
self
|
32
36
|
end
|
@@ -45,7 +49,7 @@ module Keoken
|
|
45
49
|
Keoken::VERSION_NODE,
|
46
50
|
Keoken::TYPE_SEND_TOKEN,
|
47
51
|
Keoken::PREFIX_BYTE_ASSET_ID[0..asset_length] + @id.to_s,
|
48
|
-
Keoken::PREFIX_BYTE_AMOUNT[0..prefix_length(amount)] + amount.to_s
|
52
|
+
Keoken::PREFIX_BYTE_AMOUNT[0..prefix_length(amount)] + amount.to_s(16)
|
49
53
|
].flatten.join
|
50
54
|
self
|
51
55
|
end
|
@@ -86,18 +90,6 @@ module Keoken
|
|
86
90
|
}
|
87
91
|
end
|
88
92
|
|
89
|
-
def parse_script(script)
|
90
|
-
parser = Keoken::Parser.new(script)
|
91
|
-
parser.prefix
|
92
|
-
@transaction_type = parser.set_transaction_type
|
93
|
-
if @transaction_type == :create
|
94
|
-
@name = parser.name_or_id
|
95
|
-
else
|
96
|
-
@id = parser.name_or_id
|
97
|
-
end
|
98
|
-
@amount = parser.amount
|
99
|
-
end
|
100
|
-
|
101
93
|
private
|
102
94
|
|
103
95
|
def data_length
|
@@ -112,5 +104,17 @@ module Keoken
|
|
112
104
|
asset_bytes = name.bytes.map { |n| n.to_s(16) }
|
113
105
|
asset_bytes + ['00']
|
114
106
|
end
|
107
|
+
|
108
|
+
def parse_script
|
109
|
+
prefix
|
110
|
+
@transaction_type = set_transaction_type
|
111
|
+
if @transaction_type == :create
|
112
|
+
@name = name_or_id
|
113
|
+
else
|
114
|
+
@id = name_or_id
|
115
|
+
end
|
116
|
+
@amount = set_amount
|
117
|
+
self
|
118
|
+
end
|
115
119
|
end
|
116
120
|
end
|
data/lib/keoken/version.rb
CHANGED
data/lib/keoken.rb
CHANGED
data/spec/keoken_spec.rb
CHANGED
@@ -21,21 +21,19 @@ describe Keoken do
|
|
21
21
|
token = Keoken::Token.new(name: 'test-keoken')
|
22
22
|
token.create(1_000_000)
|
23
23
|
expect(token.hex).to(
|
24
|
-
eq('
|
24
|
+
eq('6a0400004b501700000000746573742d6b656f6b656e00000000000f4240')
|
25
25
|
)
|
26
26
|
end
|
27
27
|
|
28
28
|
it 'serialize the test-keoken token' do
|
29
|
-
token = Keoken::Token.new
|
30
|
-
token.parse_script('6a0400004b501800000000746573742d6b656f6b656e000000000001000000')
|
29
|
+
token = Keoken::Token.new(script: '6a0400004b501800000000746573742d6b656f6b656e0000000000000f4240')
|
31
30
|
expect(token.to_json).to(
|
32
31
|
eq('{"id":null,"name":"test-keoken","amount":1000000,"transaction_type":"create"}')
|
33
32
|
)
|
34
33
|
end
|
35
34
|
|
36
35
|
it 'deserialize the test-keoken token' do
|
37
|
-
token = Keoken::Token.new
|
38
|
-
token.parse_script('6a0400004b501800000000746573742d6b656f6b656e000000000001000000')
|
36
|
+
token = Keoken::Token.new(script: '6a0400004b501800000000746573742d6b656f6b656e0000000000000f4240')
|
39
37
|
expect(token.to_hash).to(
|
40
38
|
eq(
|
41
39
|
amount: 1_000_000,
|
@@ -47,8 +45,7 @@ describe Keoken do
|
|
47
45
|
end
|
48
46
|
|
49
47
|
it 'deserialize the token with id' do
|
50
|
-
token = Keoken::Token.new
|
51
|
-
token.parse_script('6a0400004b501000000001000000340000000001000000')
|
48
|
+
token = Keoken::Token.new(script: '6a0400004b5010000000010000003400000000000f4240')
|
52
49
|
expect(token.to_hash).to(
|
53
50
|
eq(
|
54
51
|
amount: 1_000_000,
|
@@ -59,6 +56,18 @@ describe Keoken do
|
|
59
56
|
)
|
60
57
|
end
|
61
58
|
|
59
|
+
it 'deserialize the Bitprim token' do
|
60
|
+
token = Keoken::Token.new(script: '6a0400004b5014000000004269747072696d0000000000000f4240')
|
61
|
+
expect(token.to_hash).to(
|
62
|
+
eq(
|
63
|
+
amount: 1_000_000,
|
64
|
+
name: 'Bitprim',
|
65
|
+
id: nil,
|
66
|
+
transaction_type: :create
|
67
|
+
)
|
68
|
+
)
|
69
|
+
end
|
70
|
+
|
62
71
|
it 'raise an error when name is not provided' do
|
63
72
|
token = Keoken::Token.new
|
64
73
|
expect { token.create(1_000_000) }.to raise_error(Keoken::NameNotFound)
|
@@ -73,7 +82,7 @@ describe Keoken do
|
|
73
82
|
token = Keoken::Token.new(id: 34)
|
74
83
|
token.send_amount(1_000_000)
|
75
84
|
expect(token.hex).to(
|
76
|
-
eq('
|
85
|
+
eq('6a0400004b50f0000000100000034000000000f4240')
|
77
86
|
)
|
78
87
|
end
|
79
88
|
|
@@ -100,7 +109,7 @@ describe Keoken do
|
|
100
109
|
},
|
101
110
|
{
|
102
111
|
"value" => "0.00000000",
|
103
|
-
"scriptPubKey" => "OP_RETURN 00004b50
|
112
|
+
"scriptPubKey" => "OP_RETURN 00004b50 00000000746573742d6b656f6b656e00000000000f4240",
|
104
113
|
},
|
105
114
|
]
|
106
115
|
)
|
@@ -110,7 +119,7 @@ describe Keoken do
|
|
110
119
|
it 'raw transaction' do
|
111
120
|
raw = @transaction_token.raw
|
112
121
|
expect(raw).to start_with('0100000001dae8143d5422d5e1018c43732baa74ac3114d4399a1f58a9ea7e31f656938a44010000006')
|
113
|
-
expect(raw).to end_with('
|
122
|
+
expect(raw).to end_with('6a0400004b501700000000746573742d6b656f6b656e00000000000f424000000000')
|
114
123
|
end
|
115
124
|
|
116
125
|
it 'broadcast transaction' do
|
@@ -162,17 +171,17 @@ describe Keoken do
|
|
162
171
|
},
|
163
172
|
{
|
164
173
|
"value" => "0.00000000",
|
165
|
-
"scriptPubKey" => "OP_RETURN 00004b50
|
174
|
+
"scriptPubKey" => "OP_RETURN 00004b50 000000010000012300000000007a1200",
|
166
175
|
},
|
167
176
|
]
|
168
177
|
)
|
169
178
|
)
|
170
179
|
end
|
171
180
|
|
172
|
-
it
|
181
|
+
it 'raw transaction' do
|
173
182
|
raw = @transaction_token.raw
|
174
|
-
expect(raw).to start_with(
|
175
|
-
expect(raw).to end_with(
|
183
|
+
expect(raw).to start_with('0100000001dae8143d5422d5e1018c43732baa74ac3114d4399a1f58a9ea7e31f656938a4401000000')
|
184
|
+
expect(raw).to end_with('6a0400004b5010000000010000012300000000007a120000000000')
|
176
185
|
end
|
177
186
|
end
|
178
187
|
end
|