arkecosystem-crypto 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
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
@@ -1,14 +0,0 @@
1
- require 'btcruby'
2
-
3
- module ArkEcosystem
4
- module Crypto
5
- module Identity
6
- # The identity utility for a private key.
7
- class PrivateKey
8
- def self.from_secret(secret)
9
- BTC::Key.new(private_key: Digest::SHA256.digest(secret), public_key_compressed: true)
10
- end
11
- end
12
- end
13
- end
14
- end
@@ -1,18 +0,0 @@
1
- require 'btcruby'
2
-
3
- module ArkEcosystem
4
- module Crypto
5
- module Identity
6
- # The identity utility for a public key.
7
- class PublicKey
8
- def self.from_secret(secret)
9
- PrivateKey.from_secret(secret).public_key
10
- end
11
-
12
- def self.from_secret_as_hex(secret)
13
- from_secret(secret).unpack('H*').first
14
- end
15
- end
16
- end
17
- end
18
- end
@@ -1,42 +0,0 @@
1
- require 'arkecosystem/crypto/crypto'
2
-
3
- module ArkEcosystem
4
- module Crypto
5
- # The builder to work with signed messages.
6
- class Message
7
- def initialize(message)
8
- @public_key = message[:publickey]
9
- @signature = message[:signature]
10
- @message = message[:message]
11
- end
12
-
13
- def self.sign(message, passphrase)
14
- key = ArkEcosystem::Crypto::Identity::PrivateKey.from_secret(passphrase)
15
-
16
- hash = Digest::SHA256.digest(message)
17
-
18
- Message.new(publickey: BTC.to_hex(key.public_key),
19
- signature: BTC.to_hex(key.ecdsa_signature(hash)),
20
- message: message)
21
- end
22
-
23
- def verify
24
- key = BTC::Key.new(public_key: BTC.from_hex(@public_key))
25
-
26
- hash = Digest::SHA256.digest(@message)
27
-
28
- key.verify_ecdsa_signature(BTC.from_hex(@signature), hash)
29
- end
30
-
31
- def to_params
32
- { publickey: @public_key,
33
- signature: @signature,
34
- message: @message }
35
- end
36
-
37
- def to_json
38
- to_params.to_json
39
- end
40
- end
41
- end
42
- end
@@ -1,16 +0,0 @@
1
- module ArkEcosystem
2
- module Crypto
3
- module Models
4
- # The model of a transaction.
5
- class Transaction
6
- def serialise(transaction)
7
- ArkEcosystem::Crypto::Serialiser.new(transaction).serialise
8
- end
9
-
10
- def deserialise(serialised)
11
- ArkEcosystem::Crypto::Deserialiser.new(serialised).deserialise
12
- end
13
- end
14
- end
15
- end
16
- end
@@ -1,84 +0,0 @@
1
- module ArkEcosystem
2
- module Crypto
3
- # The base serialiser for transactions.
4
- class Serialiser
5
- def initialize(transaction)
6
- @transaction = transaction
7
-
8
- @handlers = %w[
9
- Transfer
10
- SecondSignatureRegistration
11
- DelegateRegistration
12
- Vote
13
- MultiSignatureRegistration
14
- Ipfs
15
- TimelockTransfer
16
- MultiPayment
17
- DelegateResignation
18
- ]
19
- end
20
-
21
- def serialise
22
- bytes = ''
23
- bytes << [0xff].pack('C')
24
- bytes << [@transaction[:version] || 0x01].pack('C')
25
- bytes << [@transaction[:network]].pack('C')
26
- bytes << [@transaction[:type]].pack('C')
27
- bytes << [@transaction[:timestamp]].pack('V')
28
- bytes << [@transaction[:senderPublicKey]].pack('H*')
29
- bytes << [@transaction[:fee]].pack('Q<')
30
-
31
- bytes = handle_vendor_field(bytes)
32
- bytes = handle_type(bytes)
33
- bytes = handle_signatures(bytes)
34
-
35
- BTC::Data.hex_from_data(bytes)
36
- end
37
-
38
- private
39
-
40
- def handle_type(bytes)
41
- serialiser = @handlers[@transaction[:type]]
42
- serialiser = Object.const_get("ArkEcosystem::Crypto::Serialisers::#{serialiser}")
43
- serialiser.new(@transaction, bytes).serialise
44
- end
45
-
46
- def handle_vendor_field(bytes)
47
- if @transaction[:vendorField]
48
- vendor_field_length = @transaction[:vendorField].length
49
-
50
- bytes << [vendor_field_length].pack('C')
51
- bytes << @transaction[:vendorField]
52
- elsif @transaction[:vendorFieldHex]
53
- vendor_field_hex_length = @transaction[:vendorFieldHex].length
54
-
55
- bytes << [vendor_field_hex_length / 2].pack('C')
56
- bytes << @transaction[:vendorFieldHex]
57
- else
58
- bytes << [0x00].pack('C')
59
- end
60
-
61
- bytes
62
- end
63
-
64
- def handle_signatures(bytes)
65
- if @transaction[:signature]
66
- bytes << BTC::Data.data_from_hex(@transaction[:signature])
67
- end
68
-
69
- if @transaction[:secondSignature]
70
- bytes << BTC::Data.data_from_hex(@transaction[:secondSignature])
71
- elsif @transaction[:signSignature]
72
- bytes << BTC::Data.data_from_hex(@transaction[:signSignature])
73
- end
74
-
75
- if @transaction[:signatures]
76
- bytes << [0xff].pack('C')
77
- bytes << BTC::Data.data_from_hex(@transaction[:signatures].join(''))
78
- end
79
-
80
- bytes
81
- end
82
- end
83
- end
84
- end
@@ -1,13 +0,0 @@
1
- module ArkEcosystem
2
- module Crypto
3
- module Serialisers
4
- # The base serialiser for transactions.
5
- class Base
6
- def initialize(transaction, bytes)
7
- @transaction = transaction
8
- @bytes = bytes
9
- end
10
- end
11
- end
12
- end
13
- end
@@ -1,17 +0,0 @@
1
- module ArkEcosystem
2
- module Crypto
3
- module Serialisers
4
- # The serialiser for delegate registration transactions.
5
- class DelegateRegistration < Base
6
- def serialise
7
- delegate_bytes = BTC::Data.hex_from_data(@transaction[:asset][:delegate][:username])
8
-
9
- @bytes << [delegate_bytes.length / 2].pack('C')
10
- @bytes << BTC::Data.data_from_hex(delegate_bytes)
11
-
12
- @bytes
13
- end
14
- end
15
- end
16
- end
17
- end
@@ -1,12 +0,0 @@
1
- module ArkEcosystem
2
- module Crypto
3
- module Serialisers
4
- # The serialiser for delegate resignation transactions.
5
- class DelegateResignation < Base
6
- def serialise
7
- @bytes
8
- end
9
- end
10
- end
11
- end
12
- end
@@ -1,17 +0,0 @@
1
- module ArkEcosystem
2
- module Crypto
3
- module Serialisers
4
- # The serialiser for IPFS transactions.
5
- class IPFS < Base
6
- def serialise
7
- dag = @transaction[:asset][:ipfs][:dag]
8
-
9
- @bytes << [dag.length / 2].pack('C')
10
- @bytes << BTC::Data.data_from_hex(dag)
11
-
12
- @bytes
13
- end
14
- end
15
- end
16
- end
17
- end
@@ -1,23 +0,0 @@
1
- module ArkEcosystem
2
- module Crypto
3
- module Serialisers
4
- # The serialiser for multi payment transactions.
5
- class MultiPayment < Base
6
- def serialise
7
- @bytes << [@transaction[:asset][:payments].count].pack('Q<')
8
-
9
- @transaction[:asset][:payments].each do |_item|
10
- @bytes << [@item[:amount]].pack('Q<')
11
-
12
- recipient_id = BTC::Base58.data_from_base58check(@item[:recipientId])
13
- recipient_id = BTC::Data.hex_from_data(recipient_id)
14
-
15
- @bytes << [recipient_id].pack('H*')
16
- end
17
-
18
- @bytes
19
- end
20
- end
21
- end
22
- end
23
- end
@@ -1,31 +0,0 @@
1
- module ArkEcosystem
2
- module Crypto
3
- module Serialisers
4
- # The serialiser for multi signature registrations transactions.
5
- class MultiSignatureRegistration < Base
6
- def serialise
7
- keysgroup = []
8
-
9
- if @transaction[:version] == 1 || @transaction[:version].empty?
10
- @transaction[:asset][:multisignature][:keysgroup].each do |item|
11
- if item.start_with?('+')
12
- keysgroup.push(item[1..-1])
13
- else
14
- keysgroup.push(item)
15
- end
16
- end
17
- else
18
- keysgroup = @transaction[:asset][:multisignature][:keysgroup]
19
- end
20
-
21
- @bytes << [@transaction[:asset][:multisignature][:min]].pack('C')
22
- @bytes << [@transaction[:asset][:multisignature][:keysgroup].count].pack('C')
23
- @bytes << [@transaction[:asset][:multisignature][:lifetime]].pack('C')
24
- @bytes << BTC::Data.data_from_hex(keysgroup.join(''))
25
-
26
- @bytes
27
- end
28
- end
29
- end
30
- end
31
- end
@@ -1,14 +0,0 @@
1
- module ArkEcosystem
2
- module Crypto
3
- module Serialisers
4
- # The serialiser for second signature registrations transactions.
5
- class SecondSignatureRegistration < Base
6
- def serialise
7
- @bytes << BTC::Data.data_from_hex(@transaction[:asset][:signature][:publicKey])
8
-
9
- @bytes
10
- end
11
- end
12
- end
13
- end
14
- end
@@ -1,21 +0,0 @@
1
- module ArkEcosystem
2
- module Crypto
3
- module Serialisers
4
- # The serialiser for timelock transfer transactions.
5
- class TimelockTransfer < Base
6
- def serialise
7
- @bytes << [@transaction[:amount]].pack('Q<')
8
- @bytes << [@transaction[:timelocktype]].pack('h*')
9
- @bytes << [@transaction[:timelock]].pack('Q<')
10
-
11
- recipient_id = BTC::Base58.data_from_base58check(@transaction[:recipientId])
12
- recipient_id = BTC::Data.hex_from_data(recipient_id)
13
-
14
- @bytes << [recipient_id].pack('H*')
15
-
16
- @bytes
17
- end
18
- end
19
- end
20
- end
21
- end
@@ -1,20 +0,0 @@
1
- module ArkEcosystem
2
- module Crypto
3
- module Serialisers
4
- # The serialiser for transfer transactions.
5
- class Transfer < Base
6
- def serialise
7
- @bytes << [@transaction[:amount]].pack('Q<')
8
- @bytes << [@transaction[:expiration] || 0].pack('V')
9
-
10
- recipient_id = BTC::Base58.data_from_base58check(@transaction[:recipientId])
11
- recipient_id = BTC::Data.hex_from_data(recipient_id)
12
-
13
- @bytes << [recipient_id].pack('H*')
14
-
15
- @bytes
16
- end
17
- end
18
- end
19
- end
20
- end
@@ -1,23 +0,0 @@
1
- module ArkEcosystem
2
- module Crypto
3
- module Serialisers
4
- # The serialiser for vote transactions.
5
- class Vote < Base
6
- def serialise
7
- vote_bytes = []
8
-
9
- @transaction[:asset][:votes].each do |item|
10
- prefix = item.start_with?('+') ? '01' : '00'
11
-
12
- vote_bytes.push(prefix + item[1..-1])
13
- end
14
-
15
- @bytes << [@transaction[:asset][:votes].count].pack('C')
16
- @bytes << BTC::Data.data_from_hex(vote_bytes.join(''))
17
-
18
- @bytes
19
- end
20
- end
21
- end
22
- end
23
- end