arkecosystem-crypto 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (46) hide show
  1. checksums.yaml +7 -0
  2. data/lib/arkecosystem/crypto.rb +49 -0
  3. data/lib/arkecosystem/crypto/builder/delegate_registration.rb +33 -0
  4. data/lib/arkecosystem/crypto/builder/multi_signature_registration.rb +45 -0
  5. data/lib/arkecosystem/crypto/builder/second_signature_registration.rb +27 -0
  6. data/lib/arkecosystem/crypto/builder/transaction.rb +109 -0
  7. data/lib/arkecosystem/crypto/builder/transfer.rb +32 -0
  8. data/lib/arkecosystem/crypto/builder/vote.rb +28 -0
  9. data/lib/arkecosystem/crypto/configuration/fee.rb +30 -0
  10. data/lib/arkecosystem/crypto/configuration/network.rb +18 -0
  11. data/lib/arkecosystem/crypto/crypto.rb +144 -0
  12. data/lib/arkecosystem/crypto/deserialiser.rb +94 -0
  13. data/lib/arkecosystem/crypto/deserialisers/base.rb +15 -0
  14. data/lib/arkecosystem/crypto/deserialisers/delegate_registration.rb +22 -0
  15. data/lib/arkecosystem/crypto/deserialisers/delegate_resignation.rb +12 -0
  16. data/lib/arkecosystem/crypto/deserialisers/ipfs.rb +18 -0
  17. data/lib/arkecosystem/crypto/deserialisers/multi_payment.rb +38 -0
  18. data/lib/arkecosystem/crypto/deserialisers/multi_signature_registration.rb +32 -0
  19. data/lib/arkecosystem/crypto/deserialisers/second_signature_registration.rb +18 -0
  20. data/lib/arkecosystem/crypto/deserialisers/timelock_transfer.rb +19 -0
  21. data/lib/arkecosystem/crypto/deserialisers/transfer.rb +18 -0
  22. data/lib/arkecosystem/crypto/deserialisers/vote.rb +31 -0
  23. data/lib/arkecosystem/crypto/enums/fees.rb +18 -0
  24. data/lib/arkecosystem/crypto/enums/types.rb +18 -0
  25. data/lib/arkecosystem/crypto/identity/address.rb +35 -0
  26. data/lib/arkecosystem/crypto/identity/private_key.rb +14 -0
  27. data/lib/arkecosystem/crypto/identity/public_key.rb +18 -0
  28. data/lib/arkecosystem/crypto/identity/wif.rb +22 -0
  29. data/lib/arkecosystem/crypto/message.rb +42 -0
  30. data/lib/arkecosystem/crypto/models/transaction.rb +16 -0
  31. data/lib/arkecosystem/crypto/networks/devnet.rb +24 -0
  32. data/lib/arkecosystem/crypto/networks/mainnet.rb +24 -0
  33. data/lib/arkecosystem/crypto/networks/testnet.rb +24 -0
  34. data/lib/arkecosystem/crypto/serialiser.rb +84 -0
  35. data/lib/arkecosystem/crypto/serialisers/base.rb +13 -0
  36. data/lib/arkecosystem/crypto/serialisers/delegate_registration.rb +17 -0
  37. data/lib/arkecosystem/crypto/serialisers/delegate_resignation.rb +12 -0
  38. data/lib/arkecosystem/crypto/serialisers/ipfs.rb +17 -0
  39. data/lib/arkecosystem/crypto/serialisers/multi_payment.rb +23 -0
  40. data/lib/arkecosystem/crypto/serialisers/multi_signature_registration.rb +31 -0
  41. data/lib/arkecosystem/crypto/serialisers/second_signature_registration.rb +14 -0
  42. data/lib/arkecosystem/crypto/serialisers/timelock_transfer.rb +21 -0
  43. data/lib/arkecosystem/crypto/serialisers/transfer.rb +20 -0
  44. data/lib/arkecosystem/crypto/serialisers/vote.rb +23 -0
  45. data/lib/arkecosystem/crypto/version.rb +15 -0
  46. metadata +159 -0
@@ -0,0 +1,42 @@
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
@@ -0,0 +1,16 @@
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
@@ -0,0 +1,24 @@
1
+ module ArkEcosystem
2
+ module Crypto
3
+ module Networks
4
+ # The configuration for the development network.
5
+ class Devnet
6
+ def self.epoch
7
+ '2017-03-21T13:00:00.000Z'
8
+ end
9
+
10
+ def self.version
11
+ '1e'
12
+ end
13
+
14
+ def self.nethash
15
+ '578e820911f24e039733b45e4882b73e301f813a0d2c31330dafda84534ffa23'
16
+ end
17
+
18
+ def self.wif
19
+ 170
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,24 @@
1
+ module ArkEcosystem
2
+ module Crypto
3
+ module Networks
4
+ # The configuration for the main network.
5
+ class Mainnet
6
+ def self.epoch
7
+ '2017-03-21T13:00:00.000Z'
8
+ end
9
+
10
+ def self.version
11
+ '17'
12
+ end
13
+
14
+ def self.nethash
15
+ '6e84d08bd299ed97c212c886c98a57e36545c8f5d645ca7eeae63a8bd62d8988'
16
+ end
17
+
18
+ def self.wif
19
+ 170
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,24 @@
1
+ module ArkEcosystem
2
+ module Crypto
3
+ module Networks
4
+ # The configuration for the test network.
5
+ class Testnet
6
+ def self.epoch
7
+ '2017-03-21T13:00:00.000Z'
8
+ end
9
+
10
+ def self.version
11
+ '17'
12
+ end
13
+
14
+ def self.nethash
15
+ 'd9acd04bde4234a81addb8482333b4ac906bed7be5a9970ce8ada428bd083192'
16
+ end
17
+
18
+ def self.wif
19
+ 186
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,84 @@
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
@@ -0,0 +1,13 @@
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
@@ -0,0 +1,17 @@
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
@@ -0,0 +1,12 @@
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
@@ -0,0 +1,17 @@
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
@@ -0,0 +1,23 @@
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
@@ -0,0 +1,31 @@
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
@@ -0,0 +1,14 @@
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
@@ -0,0 +1,21 @@
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
@@ -0,0 +1,20 @@
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
@@ -0,0 +1,23 @@
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
@@ -0,0 +1,15 @@
1
+ module ArkEcosystem
2
+ module Crypto
3
+ # Current major release.
4
+ MAJOR = 0
5
+
6
+ # Current minor release.
7
+ MINOR = 1
8
+
9
+ # Current patch level.
10
+ PATCH = 0
11
+
12
+ # Full release version.
13
+ VERSION = [MAJOR, MINOR, PATCH].join('.').freeze
14
+ end
15
+ end