arkecosystem-crypto 0.1.0 → 0.2.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 (75) hide show
  1. checksums.yaml +4 -4
  2. data/lib/arkecosystem/crypto.rb +35 -42
  3. data/lib/arkecosystem/crypto/{identity → identities}/address.rb +10 -4
  4. data/lib/arkecosystem/crypto/identities/private_key.rb +18 -0
  5. data/lib/arkecosystem/crypto/identities/public_key.rb +18 -0
  6. data/lib/arkecosystem/crypto/{identity → identities}/wif.rb +4 -4
  7. data/lib/arkecosystem/crypto/networks/devnet.rb +1 -5
  8. data/lib/arkecosystem/crypto/networks/mainnet.rb +1 -5
  9. data/lib/arkecosystem/crypto/networks/testnet.rb +1 -5
  10. data/lib/arkecosystem/crypto/transactions/builder/base.rb +52 -0
  11. data/lib/arkecosystem/crypto/transactions/builder/delegate_registration.rb +34 -0
  12. data/lib/arkecosystem/crypto/transactions/builder/multi_signature_registration.rb +43 -0
  13. data/lib/arkecosystem/crypto/transactions/builder/second_signature_registration.rb +28 -0
  14. data/lib/arkecosystem/crypto/transactions/builder/transfer.rb +33 -0
  15. data/lib/arkecosystem/crypto/transactions/builder/vote.rb +29 -0
  16. data/lib/arkecosystem/crypto/transactions/deserializer.rb +98 -0
  17. data/lib/arkecosystem/crypto/transactions/deserializers/base.rb +17 -0
  18. data/lib/arkecosystem/crypto/transactions/deserializers/delegate_registration.rb +24 -0
  19. data/lib/arkecosystem/crypto/transactions/deserializers/delegate_resignation.rb +14 -0
  20. data/lib/arkecosystem/crypto/transactions/deserializers/ipfs.rb +20 -0
  21. data/lib/arkecosystem/crypto/transactions/deserializers/multi_payment.rb +40 -0
  22. data/lib/arkecosystem/crypto/transactions/deserializers/multi_signature_registration.rb +34 -0
  23. data/lib/arkecosystem/crypto/transactions/deserializers/second_signature_registration.rb +20 -0
  24. data/lib/arkecosystem/crypto/transactions/deserializers/timelock_transfer.rb +21 -0
  25. data/lib/arkecosystem/crypto/transactions/deserializers/transfer.rb +21 -0
  26. data/lib/arkecosystem/crypto/transactions/deserializers/vote.rb +33 -0
  27. data/lib/arkecosystem/crypto/transactions/serializer.rb +86 -0
  28. data/lib/arkecosystem/crypto/transactions/serializers/base.rb +15 -0
  29. data/lib/arkecosystem/crypto/transactions/serializers/delegate_registration.rb +19 -0
  30. data/lib/arkecosystem/crypto/transactions/serializers/delegate_resignation.rb +14 -0
  31. data/lib/arkecosystem/crypto/transactions/serializers/ipfs.rb +19 -0
  32. data/lib/arkecosystem/crypto/transactions/serializers/multi_payment.rb +25 -0
  33. data/lib/arkecosystem/crypto/transactions/serializers/multi_signature_registration.rb +33 -0
  34. data/lib/arkecosystem/crypto/transactions/serializers/second_signature_registration.rb +16 -0
  35. data/lib/arkecosystem/crypto/transactions/serializers/timelock_transfer.rb +23 -0
  36. data/lib/arkecosystem/crypto/transactions/serializers/transfer.rb +22 -0
  37. data/lib/arkecosystem/crypto/transactions/serializers/vote.rb +25 -0
  38. data/lib/arkecosystem/crypto/transactions/transaction.rb +198 -0
  39. data/lib/arkecosystem/crypto/utils/message.rb +42 -0
  40. data/lib/arkecosystem/crypto/utils/slot.rb +23 -0
  41. data/lib/arkecosystem/crypto/version.rb +1 -1
  42. metadata +67 -39
  43. data/lib/arkecosystem/crypto/builder/delegate_registration.rb +0 -33
  44. data/lib/arkecosystem/crypto/builder/multi_signature_registration.rb +0 -45
  45. data/lib/arkecosystem/crypto/builder/second_signature_registration.rb +0 -27
  46. data/lib/arkecosystem/crypto/builder/transaction.rb +0 -109
  47. data/lib/arkecosystem/crypto/builder/transfer.rb +0 -32
  48. data/lib/arkecosystem/crypto/builder/vote.rb +0 -28
  49. data/lib/arkecosystem/crypto/crypto.rb +0 -144
  50. data/lib/arkecosystem/crypto/deserialiser.rb +0 -94
  51. data/lib/arkecosystem/crypto/deserialisers/base.rb +0 -15
  52. data/lib/arkecosystem/crypto/deserialisers/delegate_registration.rb +0 -22
  53. data/lib/arkecosystem/crypto/deserialisers/delegate_resignation.rb +0 -12
  54. data/lib/arkecosystem/crypto/deserialisers/ipfs.rb +0 -18
  55. data/lib/arkecosystem/crypto/deserialisers/multi_payment.rb +0 -38
  56. data/lib/arkecosystem/crypto/deserialisers/multi_signature_registration.rb +0 -32
  57. data/lib/arkecosystem/crypto/deserialisers/second_signature_registration.rb +0 -18
  58. data/lib/arkecosystem/crypto/deserialisers/timelock_transfer.rb +0 -19
  59. data/lib/arkecosystem/crypto/deserialisers/transfer.rb +0 -18
  60. data/lib/arkecosystem/crypto/deserialisers/vote.rb +0 -31
  61. data/lib/arkecosystem/crypto/identity/private_key.rb +0 -14
  62. data/lib/arkecosystem/crypto/identity/public_key.rb +0 -18
  63. data/lib/arkecosystem/crypto/message.rb +0 -42
  64. data/lib/arkecosystem/crypto/models/transaction.rb +0 -16
  65. data/lib/arkecosystem/crypto/serialiser.rb +0 -84
  66. data/lib/arkecosystem/crypto/serialisers/base.rb +0 -13
  67. data/lib/arkecosystem/crypto/serialisers/delegate_registration.rb +0 -17
  68. data/lib/arkecosystem/crypto/serialisers/delegate_resignation.rb +0 -12
  69. data/lib/arkecosystem/crypto/serialisers/ipfs.rb +0 -17
  70. data/lib/arkecosystem/crypto/serialisers/multi_payment.rb +0 -23
  71. data/lib/arkecosystem/crypto/serialisers/multi_signature_registration.rb +0 -31
  72. data/lib/arkecosystem/crypto/serialisers/second_signature_registration.rb +0 -14
  73. data/lib/arkecosystem/crypto/serialisers/timelock_transfer.rb +0 -21
  74. data/lib/arkecosystem/crypto/serialisers/transfer.rb +0 -20
  75. data/lib/arkecosystem/crypto/serialisers/vote.rb +0 -23
@@ -0,0 +1,98 @@
1
+ require 'arkecosystem/crypto/identities/address'
2
+
3
+ module ArkEcosystem
4
+ module Crypto
5
+ module Transactions
6
+ # The base deserializer for transactions.
7
+ class Deserializer
8
+ def initialize(serialized)
9
+ @serialized = serialized
10
+ @binary = BTC::Data.data_from_hex(@serialized)
11
+
12
+ @handlers = %w[
13
+ Transfer
14
+ SecondSignatureRegistration
15
+ DelegateRegistration
16
+ Vote
17
+ MultiSignatureRegistration
18
+ Ipfs
19
+ TimelockTransfer
20
+ MultiPayment
21
+ DelegateResignation
22
+ ]
23
+ end
24
+
25
+ def deserialize
26
+ transaction = ArkEcosystem::Crypto::Transactions::Transaction.new()
27
+ transaction.version = @binary.unpack('C2').last
28
+ transaction.network = @binary.unpack('C3').last
29
+ transaction.type = @binary.unpack('C4').last
30
+ transaction.timestamp = @binary.unpack('V2').last
31
+ transaction.sender_public_key = @binary.unpack('H16H66').last
32
+ transaction.fee = @binary.unpack('C41Q<').last
33
+
34
+ vendor_field_length = @binary.unpack('C49C1').last
35
+
36
+ if vendor_field_length > 0
37
+ vendor_field_offset = (41 + 8 + 1) * 2
38
+ vendor_field_take = vendor_field_length * 2
39
+
40
+ transaction.vendor_field_hex = @binary.unpack("H#{vendor_field_offset}H#{vendor_field_take}").last
41
+ end
42
+
43
+ asset_offset = (41 + 8 + 1) * 2 + vendor_field_length * 2
44
+
45
+ transaction = handle_type(asset_offset, transaction)
46
+
47
+ transaction.amount = 0 unless transaction.amount
48
+
49
+ if transaction.version == 1 || transaction.version.empty?
50
+ transaction = handle_version_one(transaction)
51
+ end
52
+
53
+ transaction
54
+ end
55
+
56
+ private
57
+
58
+ def handle_type(asset_offset, transaction)
59
+ deserializer = @handlers[transaction.type]
60
+ deserializer = Object.const_get("ArkEcosystem::Crypto::Transactions::Deserializers::#{deserializer}")
61
+ deserializer.new(@serialized, @binary, asset_offset, transaction).deserialize
62
+ end
63
+
64
+ def handle_version_one(transaction)
65
+ if transaction.second_signature
66
+ transaction.sign_signature = transaction.second_signature
67
+ end
68
+
69
+ if transaction.type == ArkEcosystem::Crypto::Enums::Types::VOTE
70
+ transaction.recipient_id = ArkEcosystem::Crypto::Identities::Address.from_public_key(transaction.sender_public_key, transaction.network)
71
+ end
72
+
73
+ if transaction.type == ArkEcosystem::Crypto::Enums::Types::MULTI_SIGNATURE_REGISTRATION
74
+ transaction.asset[:multisignature][:keysgroup] = transaction.asset[:multisignature][:keysgroup].map! { |key| '+' + key }
75
+ end
76
+
77
+ if transaction.vendor_field_hex
78
+ transaction.vendor_field = BTC::Data.data_from_hex(transaction.vendor_field_hex)
79
+ end
80
+
81
+ unless transaction.id
82
+ transaction.id = transaction.get_id
83
+ end
84
+
85
+ if transaction.type == ArkEcosystem::Crypto::Enums::Types::SECOND_SIGNATURE_REGISTRATION
86
+ transaction.recipient_id = ArkEcosystem::Crypto::Identities::Address.from_public_key(transaction.sender_public_key, transaction.network)
87
+ end
88
+
89
+ if transaction.type == ArkEcosystem::Crypto::Enums::Types::MULTI_SIGNATURE_REGISTRATION
90
+ transaction.recipient_id = ArkEcosystem::Crypto::Identities::Address::from_public_key(transaction.sender_public_key, transaction.network);
91
+ end
92
+
93
+ transaction
94
+ end
95
+ end
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,17 @@
1
+ module ArkEcosystem
2
+ module Crypto
3
+ module Transactions
4
+ module Deserializers
5
+ # The base deserializer for transactions.
6
+ class Base
7
+ def initialize(serialized, binary, asset_offset, transaction)
8
+ @serialized = serialized
9
+ @binary = binary
10
+ @asset_offset = asset_offset
11
+ @transaction = transaction
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,24 @@
1
+ module ArkEcosystem
2
+ module Crypto
3
+ module Transactions
4
+ module Deserializers
5
+ # The deserializer for delegate registration transactions.
6
+ class DelegateRegistration < Base
7
+ def deserialize
8
+ @transaction.asset = {
9
+ delegate: {}
10
+ }
11
+
12
+ username_length = @binary.unpack("C#{@asset_offset / 2}Q<").last & 0xff
13
+
14
+ username = @serialized[@asset_offset + 2, username_length * 2]
15
+
16
+ @transaction.asset[:delegate][:username] = BTC::Data.data_from_hex(username)
17
+
18
+ @transaction.parse_signatures(@serialized, @asset_offset + (username_length + 1) * 2)
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,14 @@
1
+ module ArkEcosystem
2
+ module Crypto
3
+ module Transactions
4
+ module Deserializers
5
+ # The deserializer for delegate resignation transactions.
6
+ class DelegateResignation < Base
7
+ def deserialize
8
+ @transaction.parse_signatures(@serialized, @asset_offset)
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,20 @@
1
+ module ArkEcosystem
2
+ module Crypto
3
+ module Transactions
4
+ module Deserializers
5
+ # The deserializer for IPFS transactions.
6
+ class IPFS < Base
7
+ def deserialize
8
+ @transaction.asset = {}
9
+
10
+ dag_length = @binary.unpack("C#{@asset_offset / 2}Q<").last & 0xff
11
+
12
+ @transaction.asset[:dag] = @serialized[@asset_offset + 2, dag_length * 2]
13
+
14
+ @transaction.parse_signatures(@serialized, @asset_offset + 2 + length * 2)
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,40 @@
1
+ module ArkEcosystem
2
+ module Crypto
3
+ module Transactions
4
+ module Deserializers
5
+ # The deserializer for multi payment transactions.
6
+ class MultiPayment < Base
7
+ def deserialize
8
+ @transaction.asset = {
9
+ amount: 0,
10
+ payments: []
11
+ }
12
+
13
+ total = @binary.unpack("C#{@asset_offset / 2}Q<").last & 0xff
14
+ offset = @asset_offset / 2 + 1
15
+
16
+ i = 0
17
+ while i < total
18
+ recipient_id = @binary.unpack("H#{(offset + 1) * 2}H42").last
19
+
20
+ payment = {
21
+ amount: @binary.unpack("C#{@asset_offset / 2}Q<").last,
22
+ recipient_id: BTC::Base58.base58check_from_data([recipient_id].pack('H*'))
23
+ }
24
+
25
+ @transaction.asset[:payments].push(payment)
26
+
27
+ offset += 22
28
+
29
+ i += 1
30
+ end
31
+
32
+ products.each { |item| @transaction.amount += item[:amount] }
33
+
34
+ @transaction.parse_signatures(@serialized, offset * 2)
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,34 @@
1
+ module ArkEcosystem
2
+ module Crypto
3
+ module Transactions
4
+ module Deserializers
5
+ # The deserializer for multi signature registrations transactions.
6
+ class MultiSignatureRegistration < Base
7
+ def deserialize
8
+ @transaction.asset = {
9
+ multisignature: {
10
+ keysgroup: []
11
+ }
12
+ }
13
+
14
+ @transaction.asset[:multisignature][:min] = @binary.unpack("C#{@asset_offset / 2}Q<").last & 0xff
15
+ @transaction.asset[:multisignature][:lifetime] = @binary.unpack("C#{@asset_offset / 2 + 2}Q<").last & 0xff
16
+
17
+ count = @binary.unpack("C#{@asset_offset / 2 + 1}Q<").last & 0xff
18
+
19
+ i = 0
20
+ while i < count
21
+ index_start = @asset_offset + 6 + i * 66
22
+
23
+ @transaction.asset[:multisignature][:keysgroup].push(@serialized[index_start, 66])
24
+
25
+ i += 1
26
+ end
27
+
28
+ @transaction.parse_signatures(@serialized, @asset_offset + 6 + count * 66)
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,20 @@
1
+ module ArkEcosystem
2
+ module Crypto
3
+ module Transactions
4
+ module Deserializers
5
+ # The deserializer for second signature registrations transactions.
6
+ class SecondSignatureRegistration < Base
7
+ def deserialize
8
+ @transaction.asset = {
9
+ signature: {}
10
+ }
11
+
12
+ @transaction.asset[:signature][:public_key] = @serialized[@asset_offset, 66]
13
+
14
+ @transaction.parse_signatures(@serialized, @asset_offset + 66)
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,21 @@
1
+ module ArkEcosystem
2
+ module Crypto
3
+ module Transactions
4
+ module Deserializers
5
+ # The deserializer for timelock transfer transactions.
6
+ class TimelockTransfer < Base
7
+ def deserialize
8
+ @transaction.amount = @binary.unpack("C#{@asset_offset / 2}Q<").last
9
+ @transaction.timelocktype = @binary.unpack("C#{@asset_offset / 2 + 8}").last & 0xff
10
+ @transaction.timelock = @binary.unpack("C#{@asset_offset / 2 + 9}V").last
11
+
12
+ recipient_id = @binary.unpack("H#{(@asset_offset / 2 + 13) * 2}H42").last
13
+ @transaction.recipient_id = BTC::Base58.base58check_from_data([recipient_id].pack('H*'))
14
+
15
+ @transaction.parse_signatures(@serialized, @asset_offset + (21 + 13) * 2)
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,21 @@
1
+ module ArkEcosystem
2
+ module Crypto
3
+ module Transactions
4
+ module Deserializers
5
+ # The deserializer for transfer transactions.
6
+ class Transfer < Base
7
+ def deserialize
8
+ @transaction.amount = @binary.unpack("C#{@asset_offset / 2}Q<").last
9
+ @transaction.expiration = @binary.unpack("C#{@asset_offset / 2 + 8}").last
10
+
11
+ recipient_id = @binary.unpack("H#{(@asset_offset / 2 + 12) * 2}H42").last
12
+ @transaction.recipient_id = BTC::Base58.base58check_from_data([recipient_id].pack('H*'))
13
+
14
+ puts @transaction
15
+ @transaction.parse_signatures(@serialized, @asset_offset + (21 + 12) * 2)
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,33 @@
1
+ module ArkEcosystem
2
+ module Crypto
3
+ module Transactions
4
+ module Deserializers
5
+ # The deserializer for vote transactions.
6
+ class Vote < Base
7
+ def deserialize
8
+ @transaction.asset = {
9
+ votes: []
10
+ }
11
+
12
+ vote_length = @binary.unpack("C#{@asset_offset / 2}Q<").last & 0xff
13
+
14
+ i = 0
15
+ while i < vote_length
16
+ index_start = @asset_offset + 2 + i * 2 * 34
17
+ index_end = 2 * 34
18
+
19
+ vote = @serialized[index_start, index_end]
20
+ vote = (vote[1] == '1' ? '+' : '-') + vote[2..-1]
21
+
22
+ @transaction.asset[:votes].push(vote)
23
+
24
+ i += 1
25
+ end
26
+
27
+ @transaction.parse_signatures(@serialized, @asset_offset + 2 + vote_length * 34 * 2)
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,86 @@
1
+ module ArkEcosystem
2
+ module Crypto
3
+ module Transactions
4
+ # The base serializer for transactions.
5
+ class Serializer
6
+ def initialize(transaction)
7
+ @transaction = transaction
8
+
9
+ @handlers = %w[
10
+ Transfer
11
+ SecondSignatureRegistration
12
+ DelegateRegistration
13
+ Vote
14
+ MultiSignatureRegistration
15
+ Ipfs
16
+ TimelockTransfer
17
+ MultiPayment
18
+ DelegateResignation
19
+ ]
20
+ end
21
+
22
+ def serialize
23
+ bytes = ''
24
+ bytes << [0xff].pack('C')
25
+ bytes << [@transaction[:version] || 0x01].pack('C')
26
+ bytes << [@transaction[:network] || ArkEcosystem::Crypto::Configuration::Network.get.version].pack('C')
27
+ bytes << [@transaction[:type]].pack('C')
28
+ bytes << [@transaction[:timestamp]].pack('V')
29
+ bytes << [@transaction[:senderPublicKey]].pack('H*')
30
+ bytes << [@transaction[:fee]].pack('Q<')
31
+
32
+ bytes = handle_vendor_field(bytes)
33
+ bytes = handle_type(bytes)
34
+ bytes = handle_signatures(bytes)
35
+
36
+ BTC::Data.hex_from_data(bytes)
37
+ end
38
+
39
+ private
40
+
41
+ def handle_type(bytes)
42
+ serializer = @handlers[@transaction[:type]]
43
+ serializer = Object.const_get("ArkEcosystem::Crypto::Transactions::Serializers::#{serializer}")
44
+ serializer.new(@transaction, bytes).serialize
45
+ end
46
+
47
+ def handle_vendor_field(bytes)
48
+ if @transaction[:vendorField]
49
+ vendor_field_length = @transaction[:vendorField].length
50
+
51
+ bytes << [vendor_field_length].pack('C')
52
+ bytes << @transaction[:vendorField]
53
+ elsif @transaction[:vendorFieldHex]
54
+ vendor_field_hex_length = @transaction[:vendorFieldHex].length
55
+
56
+ bytes << [vendor_field_hex_length / 2].pack('C')
57
+ bytes << @transaction[:vendorFieldHex]
58
+ else
59
+ bytes << [0x00].pack('C')
60
+ end
61
+
62
+ bytes
63
+ end
64
+
65
+ def handle_signatures(bytes)
66
+ if @transaction[:signature]
67
+ bytes << BTC::Data.data_from_hex(@transaction[:signature])
68
+ end
69
+
70
+ if @transaction[:secondSignature]
71
+ bytes << BTC::Data.data_from_hex(@transaction[:secondSignature])
72
+ elsif @transaction[:signSignature]
73
+ bytes << BTC::Data.data_from_hex(@transaction[:signSignature])
74
+ end
75
+
76
+ if @transaction[:signatures]
77
+ bytes << [0xff].pack('C')
78
+ bytes << BTC::Data.data_from_hex(@transaction[:signatures].join(''))
79
+ end
80
+
81
+ bytes
82
+ end
83
+ end
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,15 @@
1
+ module ArkEcosystem
2
+ module Crypto
3
+ module Transactions
4
+ module Serializers
5
+ # The base serializer for transactions.
6
+ class Base
7
+ def initialize(transaction, bytes)
8
+ @transaction = transaction
9
+ @bytes = bytes
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end