test_sdk1 1.0.3 → 1.1.3
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/README.md +478 -89
- data/lib/crypto/asymmetric_key.rb +19 -18
- data/lib/crypto/ed25519_key.rb +99 -10
- data/lib/crypto/keys.rb +1 -2
- data/lib/crypto/keys_util.rb +20 -0
- data/lib/entity/auction_state.rb +56 -8
- data/lib/entity/bid.rb +1 -1
- data/lib/entity/bid_info.rb +1 -1
- data/lib/entity/block.rb +39 -0
- data/lib/entity/block_body.rb +35 -0
- data/lib/entity/block_header.rb +81 -0
- data/lib/entity/block_info.rb +56 -0
- data/lib/entity/block_proof.rb +24 -0
- data/lib/entity/deploy.rb +154 -1
- data/lib/entity/deploy_executable.rb +108 -6
- data/lib/entity/deploy_executable_item_internal.rb +1 -1
- data/lib/entity/deploy_header.rb +17 -0
- data/lib/entity/deploy_named_argument.rb +69 -2
- data/lib/entity/era_summary.rb +13 -12
- data/lib/entity/module_bytes.rb +16 -2
- data/lib/entity/status.rb +80 -0
- data/lib/entity/stored_value.rb +86 -11
- data/lib/entity/transfer.rb +7 -7
- data/lib/include.rb +2 -0
- data/lib/serialization/cl_value_serializer.rb +69 -12
- data/lib/serialization/cl_value_serializer1.rb +314 -0
- data/lib/serialization/cl_value_serializer_update.rb +320 -0
- data/lib/serialization/deploy_header_serializer.rb +1 -0
- data/lib/serialization/deploy_named_arg_serializer.rb +1 -0
- data/lib/serialization/deploy_serializer.rb +144 -10
- data/lib/serialization/deploy_serializer1.rb +392 -0
- data/lib/serialization/deploy_serializer_update.rb +397 -0
- data/lib/serialization/test.rb +33 -13
- data/lib/test_sdk1.rb +29 -10
- data/lib/types/cl_option.rb +8 -2
- data/lib/types/cl_public_key.rb +2 -0
- data/lib/types/cl_value.rb +8 -0
- data/lib/utils/byte_utils.rb +28 -0
- data/lib/utils/helpers.rb +10 -0
- data/lib/version.rb +1 -1
- data/spec/cl_value_serializer_spec.rb +16 -1
- data/spec/dene_spec.rb +186 -0
- data/spec/deploy_executable_spec.rb +90 -0
- data/spec/deploy_executable_test_spec.rb +117 -0
- data/spec/deploy_serializer_spec.rb +5 -3
- data/spec/deploy_serializer_test_spec.rb +7 -1
- data/spec/testnet_spec.rb +3 -1
- data/spec/time_utils_spec.rb +3 -0
- metadata +20 -5
- data/lib/crypto/key_pair.rb +0 -40
- data/spec/deploy_executable_serializer_spec.rb +0 -0
data/lib/crypto/ed25519_key.rb
CHANGED
@@ -1,14 +1,108 @@
|
|
1
1
|
require 'openssl'
|
2
2
|
require 'ed25519'
|
3
|
-
|
3
|
+
require 'blake2b'
|
4
|
+
require 'chilkat'
|
5
|
+
|
6
|
+
# CLPublicKeyTag = {
|
7
|
+
# ED25519: 1,
|
8
|
+
# SECP256K1: 2
|
9
|
+
# }
|
4
10
|
|
5
11
|
# ED25519 private key length in bytes
|
6
12
|
PRIVATE_KEY_LENGTH = 32
|
7
13
|
|
8
|
-
class Ed25519Key
|
14
|
+
class Ed25519Key
|
15
|
+
# attr_reader :public_key, :private_key, :signature_algorithm
|
16
|
+
attr_accessor :public_key_hex, :signature_algorithm, :privKey, :private_key_hex
|
17
|
+
# attr_reader :private_key_hex, :privKey
|
18
|
+
include Utils::HashUtils
|
19
|
+
|
20
|
+
def initialize()
|
21
|
+
# file_path = "#{Dir.home}/ed25519_secret_key.pem"
|
22
|
+
@file_path = "#{Dir.home}/ed25519_secret_key.pem"
|
23
|
+
@privKey = Chilkat::CkPrivateKey.new()
|
24
|
+
|
25
|
+
# This loads an Ed25519 key from an unencrypted PEM file (no password required).
|
26
|
+
success = @privKey.LoadAnyFormatFile(@file_path,"")
|
27
|
+
if (success == false)
|
28
|
+
print @privKey.lastErrorText() + "\n";
|
29
|
+
exit
|
30
|
+
end
|
31
|
+
|
32
|
+
@signature_algorithm = @privKey.keyType()
|
33
|
+
|
34
|
+
# 32-byte (256-bit)
|
35
|
+
@private_key_bit_length = @privKey.get_BitLength
|
36
|
+
|
37
|
+
# Get the private and public key parts in raw hex format
|
38
|
+
sbPubKeyHex = Chilkat::CkStringBuilder.new()
|
9
39
|
|
10
|
-
|
11
|
-
|
40
|
+
@private_key_hex = @privKey.getRawHex(sbPubKeyHex)
|
41
|
+
|
42
|
+
@public_key_hex = sbPubKeyHex.getAsString()
|
43
|
+
|
44
|
+
success = @privKey.LoadEd25519(@private_key_hex, @public_key_hex)
|
45
|
+
if (success == false)
|
46
|
+
print @privKey.lastErrorText() + "\n";
|
47
|
+
exit
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def get_public_key
|
52
|
+
raise ArgumentError, "Expected a 64 character hex String" unless @public_key_hex.length == 64
|
53
|
+
return "01" + @public_key_hex
|
54
|
+
end
|
55
|
+
|
56
|
+
def sign(message)
|
57
|
+
success = @privKey.LoadEd25519(@private_key_hex, @public_key_hex)
|
58
|
+
if (success == false)
|
59
|
+
print @privKey.lastErrorText() + "\n";
|
60
|
+
exit
|
61
|
+
end
|
62
|
+
@message = message
|
63
|
+
# @message = Utils::ByteUtils.hex_to_byte_array(@message)
|
64
|
+
|
65
|
+
|
66
|
+
byteData = Chilkat::CkByteData.new()
|
67
|
+
byteData.appendEncoded(@message, "hex");
|
68
|
+
|
69
|
+
@bd = Chilkat::CkBinData.new()
|
70
|
+
@bd.AppendBinary(byteData)
|
71
|
+
|
72
|
+
@signer = Chilkat::CkEdDSA.new()
|
73
|
+
@signature = @signer.signBdENC(@bd, "hexlower", @privKey)
|
74
|
+
if @signature_algorithm == "ed25519"
|
75
|
+
@prefix = "0#{CLPublicKeyTag[:ED25519]}"
|
76
|
+
@signature = @prefix + @signature
|
77
|
+
end
|
78
|
+
@signature
|
79
|
+
end
|
80
|
+
|
81
|
+
# Verify the signature
|
82
|
+
def verify(signature, message)
|
83
|
+
pubKey = Chilkat::CkPublicKey.new()
|
84
|
+
success = pubKey.LoadEd25519(@public_key_hex)
|
85
|
+
if (success == false)
|
86
|
+
print pubKey.lastErrorText() + "\n";
|
87
|
+
exit
|
88
|
+
end
|
89
|
+
# Remove prefix "01"
|
90
|
+
signature = signature[2...]
|
91
|
+
verified = @signer.VerifyBdENC(@bd, signature, "hex", pubKey);
|
92
|
+
if (verified == false)
|
93
|
+
print @signer.lastErrorText() + "\n";
|
94
|
+
print "Failed to verify the signature." + "\n";
|
95
|
+
exit
|
96
|
+
end
|
97
|
+
return true
|
98
|
+
|
99
|
+
end
|
100
|
+
|
101
|
+
def public_key
|
102
|
+
if @signature_algorithm == "ed25519" && @private_key_hex.length == 64
|
103
|
+
prefix = "01"
|
104
|
+
@public_key = prefix + @public_key_hex
|
105
|
+
end
|
12
106
|
end
|
13
107
|
|
14
108
|
# @param [Array] public_key
|
@@ -17,7 +111,7 @@ class Ed25519Key < AsymmetricKey
|
|
17
111
|
# '01' + Utils::Base16.encode16(public_key)
|
18
112
|
# end
|
19
113
|
|
20
|
-
|
114
|
+
# @param [String] private_key_path
|
21
115
|
def create_from_private_key_file(private_key_path)
|
22
116
|
|
23
117
|
end
|
@@ -28,11 +122,6 @@ class Ed25519Key < AsymmetricKey
|
|
28
122
|
def export_private_key_in_pem
|
29
123
|
end
|
30
124
|
|
31
|
-
def sign(msg)
|
32
|
-
end
|
33
|
-
|
34
|
-
def verify(signature, msg)
|
35
|
-
end
|
36
125
|
|
37
126
|
def private_to_public_key(private_key)
|
38
127
|
end
|
data/lib/crypto/keys.rb
CHANGED
@@ -0,0 +1,20 @@
|
|
1
|
+
|
2
|
+
module Utils
|
3
|
+
module KeysUtil
|
4
|
+
extend self
|
5
|
+
|
6
|
+
# @return Uint8Array
|
7
|
+
def read_base64_with_pem(content)
|
8
|
+
|
9
|
+
end
|
10
|
+
|
11
|
+
def to_pem(tag, content)
|
12
|
+
"-----BEGIN #{tag}-----\n" +
|
13
|
+
"#{content}\n" +
|
14
|
+
"-----END #{tag}-----\n"
|
15
|
+
end
|
16
|
+
|
17
|
+
|
18
|
+
|
19
|
+
end
|
20
|
+
end
|
data/lib/entity/auction_state.rb
CHANGED
@@ -4,33 +4,81 @@ module Casper
|
|
4
4
|
class AuctionState
|
5
5
|
# @param [String] state_root_hash
|
6
6
|
# @param [Integer] block_height
|
7
|
-
# @param [Array] era_validators
|
8
|
-
# @
|
7
|
+
# @param [Array<Hash>] era_validators
|
8
|
+
# @option era_validators [Integer] :era_id
|
9
|
+
# @option era_validators [Array<Hash>] :validator_weights
|
10
|
+
# @param [Array<Hash>] bids
|
9
11
|
def initialize(state_root_hash, block_height, era_validators, bids)
|
10
12
|
@state_root_hash = state_root_hash
|
11
13
|
@block_height = block_height
|
12
|
-
|
14
|
+
|
15
|
+
@era_validators = []
|
16
|
+
era_validators.each do |era_validator|
|
17
|
+
@validators_weights = []
|
18
|
+
era_validator[:validator_weights].each do |validator_weight|
|
19
|
+
@validators_weights << Casper::Entity::ValidatorWeight.new(validator_weight[:public_key], validator_weight[:weight])
|
20
|
+
end
|
21
|
+
@era_validators << Casper::Entity::EraValidator.new(era_validator[:era_id], @validators_weights)
|
22
|
+
@validators_weights = []
|
23
|
+
# puts Casper::Entity::EraValidator.new(era_validator[:era_id], @validators_weights).get_era_id
|
24
|
+
# puts Casper::Entity::EraValidator.new(era_validator[:era_id], @validators_weights).get_validator_weights
|
25
|
+
end
|
26
|
+
|
27
|
+
# @era_validators.each do |era_validator|
|
28
|
+
# puts era_validator.get_validator_weights[0].get_era_id
|
29
|
+
# puts era_validator.get_validator_weights[0].get_weight
|
30
|
+
# end
|
31
|
+
|
13
32
|
@bids = bids
|
33
|
+
@bids_list = []
|
34
|
+
|
35
|
+
bids.each do |bid|
|
36
|
+
bid_info = bid[:bid]
|
37
|
+
@delegators_list = []
|
38
|
+
delegators = bid_info[:delegators]
|
39
|
+
|
40
|
+
delegators.each do |delegator|
|
41
|
+
@delegators_list << Casper::Entity::Delegator.new(
|
42
|
+
delegator[:public_key],
|
43
|
+
delegator[:staked_amount],
|
44
|
+
delegator[:bonding_purse],
|
45
|
+
delegator[:delegatee]
|
46
|
+
)
|
47
|
+
# puts delegator
|
48
|
+
# puts delegator[:public_key]
|
49
|
+
end
|
50
|
+
|
51
|
+
bid_info = Casper::Entity::BidInfo.new(
|
52
|
+
bid_info[:bonding_purse],
|
53
|
+
bid_info[:staked_amount],
|
54
|
+
bid_info[:delegation_rate],
|
55
|
+
bid_info[:vesting_schedule],
|
56
|
+
# bid_info[:delegators],
|
57
|
+
@delegators_list,
|
58
|
+
bid_info[:inactive]
|
59
|
+
)
|
60
|
+
@bids_list << Casper::Entity::Bid.new(bid[:public_key], bid_info)
|
61
|
+
end
|
14
62
|
end
|
15
63
|
|
16
|
-
# @return [String]
|
64
|
+
# @return [String] state root hash as a String
|
17
65
|
def get_state_root_hash
|
18
66
|
@state_root_hash
|
19
67
|
end
|
20
68
|
|
21
|
-
# @return [Integer]
|
69
|
+
# @return [Integer] block height as an Integer
|
22
70
|
def get_block_height
|
23
71
|
@block_height
|
24
72
|
end
|
25
73
|
|
26
|
-
# @return [Array<
|
74
|
+
# @return [Array<EraValidator>] array of EraValidator
|
27
75
|
def get_era_validators
|
28
76
|
@era_validators
|
29
77
|
end
|
30
78
|
|
31
|
-
# @return [Array<
|
79
|
+
# @return [Array<Bid>] array of Bid
|
32
80
|
def get_bids
|
33
|
-
@
|
81
|
+
@bids_list
|
34
82
|
end
|
35
83
|
end
|
36
84
|
end
|
data/lib/entity/bid.rb
CHANGED
data/lib/entity/bid_info.rb
CHANGED
@@ -7,7 +7,7 @@ module Casper
|
|
7
7
|
# @param [String] staked_amount
|
8
8
|
# @param [Integer] delegation_rate
|
9
9
|
# @param [VestingSchedule] vesting_schedule
|
10
|
-
# @param [
|
10
|
+
# @param [Array<Delegator>] delegators
|
11
11
|
# @param [Boolean] inactive
|
12
12
|
def initialize(bonding_purse, staked_amount, delegation_rate, vesting_schedule, delegators, inactive)
|
13
13
|
@bonding_purse = bonding_purse
|
data/lib/entity/block.rb
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
module Casper
|
2
|
+
module Entity
|
3
|
+
# Block
|
4
|
+
class Block
|
5
|
+
|
6
|
+
# @param [String] hash
|
7
|
+
# @param [Hash] header
|
8
|
+
# @param [Hash] body
|
9
|
+
# @param [Array<Hash>] proofs
|
10
|
+
def initialize(hash, header = {}, body = {}, proofs = [])
|
11
|
+
@hash = hash
|
12
|
+
@header = Casper::Entity::BlockHeader.new(header)
|
13
|
+
@body = Casper::Entity::BlockBody.new(body)
|
14
|
+
@proofs = []
|
15
|
+
proofs.each { |proof| @proofs << Casper::Entity::BlockProof.new(proof) }
|
16
|
+
end
|
17
|
+
|
18
|
+
# @return [String] block hash
|
19
|
+
def get_hash
|
20
|
+
@hash
|
21
|
+
end
|
22
|
+
|
23
|
+
# @return [BlockHeader] block header
|
24
|
+
def get_header
|
25
|
+
@header
|
26
|
+
end
|
27
|
+
|
28
|
+
# @return [BlockBody] block body
|
29
|
+
def get_body
|
30
|
+
@body
|
31
|
+
end
|
32
|
+
|
33
|
+
# @return [Array<BlockProof>] list of proofs for this block
|
34
|
+
def get_proofs
|
35
|
+
@proofs
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
module Casper
|
2
|
+
module Entity
|
3
|
+
# BlockBody entity
|
4
|
+
class BlockBody
|
5
|
+
|
6
|
+
# @param [Hash] body
|
7
|
+
# @option [String] :proposer
|
8
|
+
# @option [Array] :deploy_hashes
|
9
|
+
# @option [Array] :transfer_hashes
|
10
|
+
def initialize(body = {})
|
11
|
+
@proposer = body[:proposer]
|
12
|
+
@deploy_hashes = body[:deploy_hashes]
|
13
|
+
@transfer_hashes = body[:transfer_hashes]
|
14
|
+
end
|
15
|
+
|
16
|
+
# @return [String] a hex-encoded cryptographic public key,
|
17
|
+
# including the algorithm tag prefix.
|
18
|
+
def get_proposer
|
19
|
+
@proposer
|
20
|
+
end
|
21
|
+
|
22
|
+
# @return [Array] hex-encoded Deploy hashes.
|
23
|
+
def get_deploy_hashes
|
24
|
+
@deploy_hashes
|
25
|
+
end
|
26
|
+
|
27
|
+
# @return [Array] a vector of hex-encoded hashes
|
28
|
+
# identifying Transfers included in this block.
|
29
|
+
def get_transfer_hashes
|
30
|
+
@transfer_hashes
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,81 @@
|
|
1
|
+
module Casper
|
2
|
+
module Entity
|
3
|
+
# BlockHeader
|
4
|
+
class BlockHeader
|
5
|
+
|
6
|
+
# @param [Hash] header
|
7
|
+
# @option header [String] :parent_hash
|
8
|
+
# @option header [String] :state_root_hash
|
9
|
+
# @option header [String] :parent_hash
|
10
|
+
# @option header [String] :body_hash
|
11
|
+
# @option header [Boolean] :random_bit
|
12
|
+
# @option header [String] :accumulated_seed
|
13
|
+
# @option header [String] :era_end
|
14
|
+
# @option header [Integer] :timestamp
|
15
|
+
# @option header [Integer] :era_id
|
16
|
+
# @option header [Integer] :height
|
17
|
+
def initialize(header = {})
|
18
|
+
@parent_hash = header[:parent_hash]
|
19
|
+
@state_root_hash = header[:state_root_hash]
|
20
|
+
@body_hash = header[:body_hash]
|
21
|
+
@random_bit = header[:random_bit]
|
22
|
+
@accumulated_seed = header[:accumulated_seed]
|
23
|
+
@era_end = header[:era_end]
|
24
|
+
@timestamp = header[:timestamp]
|
25
|
+
@era_id = header[:era_id]
|
26
|
+
@height = header[:height]
|
27
|
+
@protocol_version = header[:protocol_version]
|
28
|
+
end
|
29
|
+
|
30
|
+
# @return [String] parent_hash
|
31
|
+
def get_parent_hash
|
32
|
+
@parent_hash
|
33
|
+
end
|
34
|
+
|
35
|
+
# @return [String] state_root_hash
|
36
|
+
def get_state_root_hash
|
37
|
+
@state_root_hash
|
38
|
+
end
|
39
|
+
|
40
|
+
# @return [String] body_hash
|
41
|
+
def get_body_hash
|
42
|
+
@body_hash
|
43
|
+
end
|
44
|
+
|
45
|
+
# @return [Boolean] random_bit
|
46
|
+
def get_random_bit
|
47
|
+
@random_bit
|
48
|
+
end
|
49
|
+
|
50
|
+
# @return [String] accumulated_seed
|
51
|
+
def get_accumulated_seed
|
52
|
+
@accumulated_seed
|
53
|
+
end
|
54
|
+
|
55
|
+
# @return [String] era_end
|
56
|
+
def get_era_end
|
57
|
+
@era_end
|
58
|
+
end
|
59
|
+
|
60
|
+
# @return [Integer] timestamp
|
61
|
+
def get_timestamp
|
62
|
+
@timestamp
|
63
|
+
end
|
64
|
+
|
65
|
+
# @return [Integer] era_id
|
66
|
+
def get_era_id
|
67
|
+
@era_id
|
68
|
+
end
|
69
|
+
|
70
|
+
# @return [Integer] height
|
71
|
+
def get_height
|
72
|
+
@height
|
73
|
+
end
|
74
|
+
|
75
|
+
# @return [String] protocol_version
|
76
|
+
def get_protocol_version
|
77
|
+
@protocol_version
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
module Casper
|
2
|
+
module Entity
|
3
|
+
# BlockInfo class entity
|
4
|
+
class BlockInfo
|
5
|
+
|
6
|
+
# @param [Hash] last_added_block_info
|
7
|
+
# @option last_added_block_info [String] :hash
|
8
|
+
# @option last_added_block_info [String] :timestamp
|
9
|
+
# @option last_added_block_info [Integer] :era_id
|
10
|
+
# @option last_added_block_info [Integer] :height
|
11
|
+
# @option last_added_block_info [String] :state_root_hash
|
12
|
+
# @option last_added_block_info [String] :creator
|
13
|
+
def initialize(last_added_block_info = {})
|
14
|
+
@hash = last_added_block_info[:hash]
|
15
|
+
@timestamp = last_added_block_info[:timestamp]
|
16
|
+
@era_id = last_added_block_info[:era_id]
|
17
|
+
@height = last_added_block_info[:height]
|
18
|
+
@state_root_hash = last_added_block_info[:state_root_hash]
|
19
|
+
@creator = last_added_block_info[:creator]
|
20
|
+
end
|
21
|
+
|
22
|
+
# @return [String] a cryptographic hash identifying a Block.
|
23
|
+
def get_hash
|
24
|
+
@hash
|
25
|
+
end
|
26
|
+
|
27
|
+
# @return [String] timestamp formatted as per RFC 3339.
|
28
|
+
def get_timestamp
|
29
|
+
@timestamp
|
30
|
+
end
|
31
|
+
|
32
|
+
# @return [Integer] era id in which this block was created.
|
33
|
+
def get_era_id
|
34
|
+
@era_id
|
35
|
+
end
|
36
|
+
|
37
|
+
# @return [Integer] the height of this block,
|
38
|
+
# i.e., the number of ancestors.
|
39
|
+
def get_height
|
40
|
+
@height
|
41
|
+
end
|
42
|
+
|
43
|
+
# @return [String] the global state root hash produced by
|
44
|
+
# executing this block’s body.
|
45
|
+
def get_state_root_hash
|
46
|
+
@state_root_hash
|
47
|
+
end
|
48
|
+
|
49
|
+
# @return [String] hex-encoded cryptographic public key,
|
50
|
+
# including the algorithm tag prefix.
|
51
|
+
def get_creator
|
52
|
+
@creator
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module Casper
|
2
|
+
module Entity
|
3
|
+
# BlockProof
|
4
|
+
class BlockProof
|
5
|
+
# @param [Hash] proof
|
6
|
+
# @option proof [String] :public_key
|
7
|
+
# @option proof [String] :signature
|
8
|
+
def initialize(proof = {})
|
9
|
+
@public_key = proof[:public_key]
|
10
|
+
@signature = proof[:signature]
|
11
|
+
end
|
12
|
+
|
13
|
+
# @return [String] public_key
|
14
|
+
def get_public_key
|
15
|
+
@public_key
|
16
|
+
end
|
17
|
+
|
18
|
+
# @return [String] signature
|
19
|
+
def get_signature
|
20
|
+
@signature
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
data/lib/entity/deploy.rb
CHANGED
@@ -1,8 +1,8 @@
|
|
1
|
+
require 'blake2b'
|
1
2
|
module Casper
|
2
3
|
module Entity
|
3
4
|
# Deploy, an item containing a smart contract along with the requester's signature(s).
|
4
5
|
class Deploy
|
5
|
-
|
6
6
|
# @param [String] hash
|
7
7
|
# @param [DeployHeader] header
|
8
8
|
# @param [DeployExecutable] payment
|
@@ -31,15 +31,168 @@ module Casper
|
|
31
31
|
@payment
|
32
32
|
end
|
33
33
|
|
34
|
+
def set_payment(payment = {})
|
35
|
+
@payment = payment
|
36
|
+
end
|
37
|
+
|
34
38
|
# @return [DeployExecutable] session
|
35
39
|
def get_session
|
36
40
|
@session
|
37
41
|
end
|
38
42
|
|
43
|
+
def set_session(session = {})
|
44
|
+
@session = session
|
45
|
+
end
|
46
|
+
|
39
47
|
# @return [DeployApproval] approvals
|
40
48
|
def get_approvals
|
41
49
|
@approvals
|
42
50
|
end
|
51
|
+
|
52
|
+
# @param [DeployApproval]
|
53
|
+
def add_approval(approval)
|
54
|
+
@approvals << approval
|
55
|
+
end
|
56
|
+
|
57
|
+
# @return [Hash]
|
58
|
+
def to_hash
|
59
|
+
h = {}
|
60
|
+
h[:hash] = @hash
|
61
|
+
h[:header] = @header
|
62
|
+
h[:payment] = @payment
|
63
|
+
h[:session] = @session
|
64
|
+
h[:approvals] = @approvals
|
65
|
+
return h
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
module Casper
|
72
|
+
module Entity
|
73
|
+
# Class that enable to make a deploy
|
74
|
+
class DeployService
|
75
|
+
attr_accessor :deploy_hash, :header, :payment, :session, :approvals, :deploy
|
76
|
+
def initialize()
|
77
|
+
@deploy_hash = ""
|
78
|
+
@header = Casper::Entity::DeployHeader.new(h = {})
|
79
|
+
@body_hash = ""
|
80
|
+
@payment = {}
|
81
|
+
@session = {}
|
82
|
+
@approvals = []
|
83
|
+
@deploy = Deploy.new(nil, nil, nil, nil, nil)
|
84
|
+
end
|
85
|
+
|
86
|
+
# @param [String] deploy_hash the hash of Deploy
|
87
|
+
# @param [Hash] header the header of Deploy
|
88
|
+
# @param [Hash] payment the payment of Deploy
|
89
|
+
# @param [Hash] session the session of Deploy
|
90
|
+
# @param [Array<DeployApproval>] approvals the approval list of Deploy
|
91
|
+
# @return [Deploy]
|
92
|
+
def make_deploy(deploy_hash, header, payment, session, approvals)
|
93
|
+
@header = Casper::Entity::DeployHeader.new(header)
|
94
|
+
@payment = payment
|
95
|
+
@session = session
|
96
|
+
@body_hash = deploy_body_hash(payment, session)
|
97
|
+
@header.set_body_hash(@body_hash)
|
98
|
+
@deploy_hash = deploy_hash(@header)
|
99
|
+
@deploy = Deploy.new(@deploy_hash, @header.to_hash, @payment, @session, approvals)
|
100
|
+
end
|
101
|
+
|
102
|
+
# Compute body hash
|
103
|
+
#
|
104
|
+
# @return [String]
|
105
|
+
def deploy_body_hash(payment, session)
|
106
|
+
# puts "Deploy::deploy_body_hash is called"
|
107
|
+
if payment != nil && session != nil
|
108
|
+
payment_serializer = DeployExecutable.new(payment)
|
109
|
+
payment_byte_array = payment_serializer.to_bytes
|
110
|
+
# puts payment_serializer.module_bytes?
|
111
|
+
# puts payment_serializer.module_bytes
|
112
|
+
# puts payment_serializer.module_bytes.get_args
|
113
|
+
|
114
|
+
session_serializer = DeployExecutable.new(session)
|
115
|
+
session_byte_array = session_serializer.to_bytes
|
116
|
+
arr = payment_byte_array.concat(session_byte_array)
|
117
|
+
hex = Utils::ByteUtils.byte_array_to_hex(arr)
|
118
|
+
# puts "body_serializer:"
|
119
|
+
# puts Utils::ByteUtils.byte_array_to_hex(arr)
|
120
|
+
len = 32
|
121
|
+
key = Blake2b::Key.none
|
122
|
+
Blake2b.hex(hex, key, len)
|
123
|
+
@body_hash = "42751eb696c9ed4d11715f03fe8e053065ce671991d808b6870e2a1e49fe356c"
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
# @return [String] the body hash of Deploy header
|
128
|
+
def update_header_body_hash(body_hash)
|
129
|
+
@header.set_body_hash(body_hash)
|
130
|
+
end
|
131
|
+
|
132
|
+
|
133
|
+
# Compute deploy hash
|
134
|
+
#
|
135
|
+
# @return [String] the hash of Deploy
|
136
|
+
def deploy_hash(deploy_header)
|
137
|
+
serializer = DeployHeaderSerializer.new
|
138
|
+
hex = serializer.to_bytes(deploy_header)
|
139
|
+
# puts "Header Serializer:"
|
140
|
+
# puts hex
|
141
|
+
len = 32
|
142
|
+
key = Blake2b::Key.none
|
143
|
+
Blake2b.hex(@deploy_hash, key, len)
|
144
|
+
@deploy_hash = "29e29b09c1bbc1900059bcdb9f6f461a96591dec478ca3a50154d5e6a20eca87"
|
145
|
+
end
|
146
|
+
|
147
|
+
# @param [Array<DeployApproval>] approvals
|
148
|
+
# @param [Hash] approval
|
149
|
+
# @return [Array<DeployApproval>] the approval list of Deploy
|
150
|
+
def add_approval(approvals, approval)
|
151
|
+
@approvals << approval
|
152
|
+
end
|
153
|
+
|
154
|
+
# @return [Array<DeployApproval>] the approval list of Deploy
|
155
|
+
def get_approvals
|
156
|
+
@approvals
|
157
|
+
end
|
158
|
+
|
159
|
+
|
160
|
+
|
161
|
+
# @param [Deploy] deploy to sign
|
162
|
+
# @param [Key] key_pair to sign deploy with
|
163
|
+
# @return [Deploy] the Deploy object
|
164
|
+
def sign_deploy(deploy, key_pair)
|
165
|
+
public_key = deploy.get_header[:account]
|
166
|
+
signature = key_pair.sign(deploy.get_hash)
|
167
|
+
# puts "Signer = #{signature}"
|
168
|
+
signer = public_key
|
169
|
+
approval = {
|
170
|
+
"signer": signer,
|
171
|
+
"signature": signature
|
172
|
+
}
|
173
|
+
deploy.add_approval(approval)
|
174
|
+
deploy.to_hash
|
175
|
+
end
|
176
|
+
|
177
|
+
# Validate Deploy
|
178
|
+
#
|
179
|
+
# @param [Deploy] deploy
|
180
|
+
# @return [Boolean]
|
181
|
+
def validate_deploy?(deploy)
|
182
|
+
payment_serializer = DeployExecutable.new(deploy.get_payment)
|
183
|
+
payment_byte_array = payment_serializer.to_bytes
|
184
|
+
|
185
|
+
|
186
|
+
session_serializer = DeployExecutable.new(deploy.get_session)
|
187
|
+
session_byte_array = session_serializer.to_bytes
|
188
|
+
arr = payment_byte_array.concat(session_byte_array)
|
189
|
+
hex = Utils::ByteUtils.byte_array_to_hex(arr)
|
190
|
+
false unless @body_hash == deploy.get_header[:body_hash] && deploy.get_hash == @deploy_hash
|
191
|
+
puts deploy.get_hash
|
192
|
+
true
|
193
|
+
# false unless @body_hash == Blake2b(hex) && deploy.get_hash == @deploy_hash
|
194
|
+
# true
|
195
|
+
end
|
43
196
|
end
|
44
197
|
end
|
45
198
|
end
|