iost_sdk 0.1.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 (57) hide show
  1. checksums.yaml +7 -0
  2. data/lib/iost_sdk/crypto.rb +144 -0
  3. data/lib/iost_sdk/errors.rb +10 -0
  4. data/lib/iost_sdk/http/client.rb +58 -0
  5. data/lib/iost_sdk/http/get_account.rb +17 -0
  6. data/lib/iost_sdk/http/get_block_by_hash.rb +17 -0
  7. data/lib/iost_sdk/http/get_block_by_number.rb +17 -0
  8. data/lib/iost_sdk/http/get_chain_info.rb +17 -0
  9. data/lib/iost_sdk/http/get_contract.rb +17 -0
  10. data/lib/iost_sdk/http/get_contract_storage.rb +25 -0
  11. data/lib/iost_sdk/http/get_contract_storage_fields.rb +25 -0
  12. data/lib/iost_sdk/http/get_gas_ratio.rb +17 -0
  13. data/lib/iost_sdk/http/get_node_info.rb +17 -0
  14. data/lib/iost_sdk/http/get_ram_info.rb +17 -0
  15. data/lib/iost_sdk/http/get_token_balance.rb +17 -0
  16. data/lib/iost_sdk/http/get_tx_by_hash.rb +17 -0
  17. data/lib/iost_sdk/http/get_tx_receipt_by_tx_hash.rb +17 -0
  18. data/lib/iost_sdk/http/http_request_error.rb +11 -0
  19. data/lib/iost_sdk/http/send_tx.rb +26 -0
  20. data/lib/iost_sdk/models/abi.rb +20 -0
  21. data/lib/iost_sdk/models/account.rb +32 -0
  22. data/lib/iost_sdk/models/account_ram_info.rb +19 -0
  23. data/lib/iost_sdk/models/action.rb +24 -0
  24. data/lib/iost_sdk/models/amount_limit.rb +22 -0
  25. data/lib/iost_sdk/models/block.rb +30 -0
  26. data/lib/iost_sdk/models/block_info.rb +19 -0
  27. data/lib/iost_sdk/models/chain_info.rb +25 -0
  28. data/lib/iost_sdk/models/contract.rb +22 -0
  29. data/lib/iost_sdk/models/frozen_balance.rb +18 -0
  30. data/lib/iost_sdk/models/gas_info.rb +23 -0
  31. data/lib/iost_sdk/models/gas_ratio.rb +18 -0
  32. data/lib/iost_sdk/models/info.rb +19 -0
  33. data/lib/iost_sdk/models/network_info.rb +20 -0
  34. data/lib/iost_sdk/models/node_info.rb +21 -0
  35. data/lib/iost_sdk/models/peer_info.rb +18 -0
  36. data/lib/iost_sdk/models/permission.rb +20 -0
  37. data/lib/iost_sdk/models/permission_group.rb +19 -0
  38. data/lib/iost_sdk/models/permission_item.rb +20 -0
  39. data/lib/iost_sdk/models/pledge_info.rb +18 -0
  40. data/lib/iost_sdk/models/query/contract_storage.rb +24 -0
  41. data/lib/iost_sdk/models/query/contract_storage_fields.rb +23 -0
  42. data/lib/iost_sdk/models/query/signed_transaction.rb +114 -0
  43. data/lib/iost_sdk/models/query/transaction.rb +75 -0
  44. data/lib/iost_sdk/models/ram_info.rb +21 -0
  45. data/lib/iost_sdk/models/receipt.rb +18 -0
  46. data/lib/iost_sdk/models/signature.rb +42 -0
  47. data/lib/iost_sdk/models/token_balance.rb +19 -0
  48. data/lib/iost_sdk/models/transaction.rb +32 -0
  49. data/lib/iost_sdk/models/transaction_info.rb +19 -0
  50. data/lib/iost_sdk/models/tx_receipt.rb +24 -0
  51. data/lib/iost_sdk/models/util/serializer.rb +52 -0
  52. data/lib/iost_sdk/models/vote_info.rb +19 -0
  53. data/lib/iost_sdk/models.rb +276 -0
  54. data/lib/iost_sdk/string.rb +11 -0
  55. data/lib/iost_sdk/version.rb +3 -0
  56. data/lib/iost_sdk.rb +141 -0
  57. metadata +255 -0
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'iost_sdk/models'
4
+ require 'iost_sdk/models/block'
5
+
6
+ module IOSTSdk
7
+ module Models
8
+ class BlockInfo
9
+ include Models
10
+
11
+ def self.attr_names
12
+ [
13
+ 'status',
14
+ 'block'
15
+ ]
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'iost_sdk/models'
4
+
5
+ module IOSTSdk
6
+ module Models
7
+ class ChainInfo
8
+ include Models
9
+
10
+ def self.attr_names
11
+ [
12
+ 'net_name',
13
+ 'protocol_version',
14
+ 'chain_id',
15
+ 'head_block',
16
+ 'head_block_hash',
17
+ 'lib_block',
18
+ 'lib_block_hash',
19
+ 'witness_list',
20
+ 'lib_witness_list'
21
+ ]
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'iost_sdk/models'
4
+ require 'iost_sdk/models/abi'
5
+
6
+ module IOSTSdk
7
+ module Models
8
+ class Contract
9
+ include Models
10
+
11
+ def self.attr_names
12
+ [
13
+ 'id',
14
+ 'code',
15
+ 'language',
16
+ 'version',
17
+ 'abis'
18
+ ]
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'iost_sdk/models'
4
+
5
+ module IOSTSdk
6
+ module Models
7
+ class FrozenBalance
8
+ include Models
9
+
10
+ def self.attr_names
11
+ [
12
+ 'amount',
13
+ 'time'
14
+ ]
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'iost_sdk/models'
4
+ require 'iost_sdk/models/pledge_info'
5
+
6
+ module IOSTSdk
7
+ module Models
8
+ class GasInfo
9
+ include Models
10
+
11
+ def self.attr_names
12
+ [
13
+ 'current_total',
14
+ 'transferable_gas',
15
+ 'pledge_gas',
16
+ 'increase_speed',
17
+ 'limit',
18
+ 'pledged_info'
19
+ ]
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'iost_sdk/models'
4
+
5
+ module IOSTSdk
6
+ module Models
7
+ class GasRatio
8
+ include Models
9
+
10
+ def self.attr_names
11
+ [
12
+ 'lowest_gas_ratio',
13
+ 'median_gas_ratio'
14
+ ]
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'iost_sdk/models'
4
+
5
+ module IOSTSdk
6
+ module Models
7
+ class Info
8
+ include Models
9
+
10
+ def self.attr_names
11
+ [
12
+ 'mode',
13
+ 'thread',
14
+ 'batch_index'
15
+ ]
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'iost_sdk/models'
4
+ require 'iost_sdk/models/peer_info'
5
+
6
+ module IOSTSdk
7
+ module Models
8
+ class NetworkInfo
9
+ include Models
10
+
11
+ def self.attr_names
12
+ [
13
+ 'id',
14
+ 'peer_count',
15
+ 'peer_info'
16
+ ]
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'iost_sdk/models'
4
+ require 'iost_sdk/models/network_info'
5
+
6
+ module IOSTSdk
7
+ module Models
8
+ class NodeInfo
9
+ include Models
10
+
11
+ def self.attr_names
12
+ [
13
+ 'build_time',
14
+ 'git_hash',
15
+ 'mode',
16
+ 'network'
17
+ ]
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'iost_sdk/models'
4
+
5
+ module IOSTSdk
6
+ module Models
7
+ class PeerInfo
8
+ include Models
9
+
10
+ def self.attr_names
11
+ [
12
+ 'id',
13
+ 'addr'
14
+ ]
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'iost_sdk/models'
4
+
5
+ module IOSTSdk
6
+ module Models
7
+ class Permission
8
+ include Models
9
+
10
+ def self.attr_names
11
+ [
12
+ 'name',
13
+ 'group_names',
14
+ 'items',
15
+ 'threshold'
16
+ ]
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'iost_sdk/models'
4
+ require 'iost_sdk/models/permission_item'
5
+
6
+ module IOSTSdk
7
+ module Models
8
+ class PermissionGroup
9
+ include Models
10
+
11
+ def self.attr_names
12
+ [
13
+ 'name',
14
+ 'items'
15
+ ]
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'iost_sdk/models'
4
+
5
+ module IOSTSdk
6
+ module Models
7
+ class PermissionItem
8
+ include Models
9
+
10
+ def self.attr_names
11
+ [
12
+ 'id',
13
+ 'is_key_pair',
14
+ 'weight',
15
+ 'permission'
16
+ ]
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'iost_sdk/models'
4
+
5
+ module IOSTSdk
6
+ module Models
7
+ class PledgeInfo
8
+ include Models
9
+
10
+ def self.attr_names
11
+ [
12
+ 'pledger',
13
+ 'amount'
14
+ ]
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'iost_sdk/models'
4
+
5
+ module IOSTSdk
6
+ module Models
7
+ module Query
8
+ # This represents the JSON payload for https://developers.iost.io/docs/en/6-reference/API.html#getcontractstorage
9
+ # The API simply returns { "data": value }, where value is a JSON string defined by a DApp
10
+ class ContractStorage
11
+ include Models
12
+
13
+ def self.attr_names
14
+ [
15
+ 'id',
16
+ 'field',
17
+ 'key',
18
+ 'by_longest_chain'
19
+ ]
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'iost_sdk/models'
4
+
5
+ module IOSTSdk
6
+ module Models
7
+ module Query
8
+ # This represents the JSON payload for https://developers.iost.io/docs/en/6-reference/API.html#getcontractstoragefields
9
+ # The API simply returns { "fields": value }, where value is a JSON string defined by a DApp
10
+ class ContractStorageFields
11
+ include Models
12
+
13
+ def self.attr_names
14
+ [
15
+ 'id',
16
+ 'key',
17
+ 'by_longest_chain'
18
+ ]
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,114 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'sha3'
4
+ require 'base58'
5
+ require 'iost_sdk/models'
6
+ require 'iost_sdk/models/query/transaction'
7
+ require 'iost_sdk/models/util/serializer'
8
+
9
+ module IOSTSdk
10
+ module Models
11
+ module Query
12
+ # This represents the signed transaction payload for https://developers.iost.io/docs/en/6-reference/API.html#sendtx
13
+ class SignedTransaction
14
+ include Models
15
+
16
+ def self.from_transaction(transaction:)
17
+ raise ArgumentError.new('tx must be an instance of IOSTSdk::Models::Query::Transaction') unless
18
+ transaction.is_a?(IOSTSdk::Models::Query::Transaction)
19
+
20
+ # assign fields and values from tx
21
+ signed_tx = SignedTransaction.new
22
+ transaction.instance_variables.each do |var_name|
23
+ n = var_name.to_s[1..-1].to_sym
24
+ signed_tx.instance_variable_set(var_name, transaction.send(n))
25
+ signed_tx.class.send(:define_method, n) do
26
+ instance_variable_get(var_name)
27
+ end
28
+ end
29
+
30
+ signed_tx
31
+ end
32
+
33
+ def sign(account_name:, key_pair:)
34
+ add_publisher_sig(account_name: account_name, key_pair: key_pair)
35
+ self
36
+ end
37
+
38
+ private
39
+
40
+ def bytes_for_signature
41
+ serializer = IOSTSdk::Models::Util::Serializer
42
+ tx_bytes = serializer.int64_to_bytes(time) +
43
+ serializer.int64_to_bytes(expiration) +
44
+ serializer.int64_to_bytes(gas_ratio * 100) +
45
+ serializer.int64_to_bytes(gas_limit * 100) +
46
+ serializer.int64_to_bytes(delay) +
47
+ serializer.int32_to_bytes(chain_id)
48
+ tx_bytes += serializer.int32_to_bytes(0) unless reserved
49
+ tx_bytes += serializer.array_to_bytes(signers) +
50
+ serializer.array_to_bytes(actions) +
51
+ serializer.array_to_bytes(amount_limit)
52
+
53
+ tx_bytes
54
+ end
55
+
56
+ def hash_value(include_signatures:)
57
+ byte_string = bytes_for_signature
58
+ if include_signatures
59
+ serializer = IOSTSdk::Models::Util::Serializer
60
+ byte_string = byte_string +
61
+ serializer.int32_to_bytes(signatures.size) +
62
+ signatures.map { |sig| serializer.int32_to_bytes(sig.bytes.size) + sig.bytes }.flatten
63
+
64
+ byte_string
65
+ end
66
+
67
+ SHA3::Digest.new(:sha256)
68
+ .update(byte_string.pack('C*'))
69
+ .digest
70
+ end
71
+
72
+ def gen_sig(key_pair:)
73
+ key_pair.sign(message: hash_value(include_signatures: true))
74
+ end
75
+
76
+ def add_sig(key_pair:)
77
+ signature = gen_sig(key_pair: key_pair)
78
+ @signatures ||= []
79
+ @signatures << IOSTSdk::Models::Signature.new.populate(
80
+ model_data: {
81
+ 'algorithm' => key_pair.algo,
82
+ 'public_key' => [key_pair.public_key_raw].pack('m0'),
83
+ 'signature' => [signature].pack('m0')
84
+ }
85
+ )
86
+
87
+ self
88
+ end
89
+
90
+ def add_publisher_sig(account_name:, key_pair:)
91
+ signature = gen_sig(key_pair: key_pair)
92
+ # set "publisher"
93
+ instance_variable_set('@publisher', account_name)
94
+ self.class.send(:define_method, :publisher) { instance_variable_get('@publisher') }
95
+ # set "publisher_sigs"
96
+ instance_variable_set(
97
+ '@publisher_sigs',
98
+ [
99
+ IOSTSdk::Models::Signature.new.populate(
100
+ model_data: {
101
+ 'algorithm' => key_pair.algo,
102
+ 'public_key' => [key_pair.public_key_raw].pack('m0'),
103
+ 'signature' => [signature].pack('m0')
104
+ }
105
+ )
106
+ ]
107
+ )
108
+ self.class.send(:define_method, :publisher_sigs) { instance_variable_get('@publisher_sigs') }
109
+ self
110
+ end
111
+ end
112
+ end
113
+ end
114
+ end
@@ -0,0 +1,75 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'json'
4
+ require 'iost_sdk/models'
5
+ require 'iost_sdk/models/amount_limit'
6
+ require 'iost_sdk/models/action'
7
+ require 'iost_sdk/models/signature'
8
+
9
+ module IOSTSdk
10
+ module Models
11
+ module Query
12
+ class Transaction
13
+ include Models
14
+
15
+ attr_accessor :chain_id
16
+
17
+ def self.attr_names
18
+ [
19
+ 'time',
20
+ 'expiration',
21
+ 'gas_ratio',
22
+ 'gas_limit',
23
+ 'delay',
24
+ 'chain_id',
25
+ 'reserved',
26
+ 'signers',
27
+ 'actions',
28
+ 'amount_limit',
29
+ 'signatures'
30
+ ]
31
+ end
32
+
33
+ # set the +time+ implicitly, and set +expiration+ and +delay+ explicitly
34
+ #
35
+ # @param expiration [Integer] number of seconds, since creation, the transaction will expire in
36
+ # @param delay [Integer] the delay
37
+ def set_time_params(expiration:, delay:)
38
+ time_now = (Time.now.utc.to_f * 1000).to_i * 1_000_000
39
+
40
+ @time = time_now
41
+ @expiration = @time + expiration * 1_000_000_000
42
+ @delay = delay
43
+ end
44
+
45
+ # Add an action to the transaction
46
+ #
47
+ # @param contract_id [String] a Contract's ID
48
+ # @param action_name [String] the name of an action
49
+ # @param action_data [any] args to the action
50
+ def add_action(contract_id:, action_name:, action_data:)
51
+ @actions << IOSTSdk::Models::Action.new.populate(
52
+ model_data: {
53
+ 'contract' => contract_id,
54
+ 'action_name' => action_name.to_s,
55
+ 'data' => JSON.generate(action_data)
56
+ }
57
+ )
58
+ end
59
+
60
+ # Add an amount limit to the transaction
61
+ #
62
+ # @param token [String] name of the token
63
+ # @param amount [Integer|String] amount of the token or 'unlimited'
64
+ def add_approve(token:, amount:)
65
+ @amount_limit << IOSTSdk::Models::AmountLimit.new.populate(
66
+ model_data: {
67
+ 'token' => token.to_s,
68
+ 'value' => amount.to_s
69
+ }
70
+ )
71
+ end
72
+ end
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'iost_sdk/models'
4
+
5
+ module IOSTSdk
6
+ module Models
7
+ class RAMInfo
8
+ include Models
9
+
10
+ def self.attr_names
11
+ [
12
+ 'available_ram',
13
+ 'buy_price',
14
+ 'sell_price',
15
+ 'total_ram',
16
+ 'used_ram'
17
+ ]
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'iost_sdk/models'
4
+
5
+ module IOSTSdk
6
+ module Models
7
+ class Receipt
8
+ include Models
9
+
10
+ def self.attr_names
11
+ [
12
+ 'func_name',
13
+ 'content'
14
+ ]
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'iost_sdk/models'
4
+ require 'iost_sdk/models/util/serializer'
5
+
6
+ module IOSTSdk
7
+ module Models
8
+ class Signature
9
+ include Models
10
+
11
+ ALGORITHM = {
12
+ ED25519: 'ED25519',
13
+ SECP256K1: 'SECP256K1'
14
+ }
15
+
16
+ def self.algorithm
17
+ ALGORITHM
18
+ end
19
+
20
+ def self.attr_names
21
+ [
22
+ 'algorithm',
23
+ 'public_key',
24
+ 'signature'
25
+ ]
26
+ end
27
+
28
+ def bytes
29
+ serializer = IOSTSdk::Models::Util::Serializer
30
+ public_key_raw = public_key.unpack('m0').first
31
+ signature_raw = signature.unpack('m0').first
32
+
33
+ byte_value = (algorithm == ALGORITHM[:SECP256K1] ? [1] : [2]) +
34
+ serializer.int32_to_bytes(signature_raw.size) +
35
+ signature_raw.unpack('C*') +
36
+ serializer.int32_to_bytes(public_key_raw.size) +
37
+ public_key_raw.unpack('C*')
38
+ byte_value
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'iost_sdk/models'
4
+ require 'iost_sdk/models/frozen_balance'
5
+
6
+ module IOSTSdk
7
+ module Models
8
+ class TokenBalance
9
+ include Models
10
+
11
+ def self.attr_names
12
+ [
13
+ 'balance',
14
+ 'frozen_balances'
15
+ ]
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'iost_sdk/models'
4
+ require 'iost_sdk/models/action'
5
+ require 'iost_sdk/models/amount_limit'
6
+ require 'iost_sdk/models/tx_receipt'
7
+
8
+ module IOSTSdk
9
+ module Models
10
+ class Transaction
11
+ include Models
12
+
13
+ def self.attr_names
14
+ [
15
+ 'hash',
16
+ 'time',
17
+ 'expiration',
18
+ 'gas_ratio',
19
+ 'gas_limit',
20
+ 'delay',
21
+ 'actions',
22
+ 'chain_id',
23
+ 'signers',
24
+ 'publisher',
25
+ 'referred_tx',
26
+ 'amount_limit',
27
+ 'tx_receipt'
28
+ ]
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'iost_sdk/models'
4
+ require 'iost_sdk/models/transaction'
5
+
6
+ module IOSTSdk
7
+ module Models
8
+ class TransactionInfo
9
+ include Models
10
+
11
+ def self.attr_names
12
+ [
13
+ 'status',
14
+ 'transaction'
15
+ ]
16
+ end
17
+ end
18
+ end
19
+ end