near_api 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2a1e7300ba5960960eae405d29da902ce972b51e10aec298a8a438cc93fdf185
4
- data.tar.gz: 84deaf2b6f1325b9d5a15241cd56e311abc3119958d809d055c9bca10813173a
3
+ metadata.gz: a2d52679da22f2e21379007bab1922d1977708033a6181e6b5987d59a0a5579b
4
+ data.tar.gz: 82aa0595f95e509277d901430f9140b9978a6369f45a107d9c6feaad4415f3eb
5
5
  SHA512:
6
- metadata.gz: cc0f0cf9213c9c0b2480cbd043aea1c7c54dd61b1e6150c7db21570072f499d20c23ff2973b8738afe56f40084d6cccb0f173cd548bb3b73427a18a01dbf8030
7
- data.tar.gz: 7650ee6aa625a77593f178ba0e091a30c98d1dfd6bd8622f42c29d26181c2e1699ef47b3e1f0c182b44d04e1ca26f64a299d733158a8fc5460a7dbaec427c901
6
+ metadata.gz: b8489f9a7889bcb819ce8155da38eabd460a7de4173d04f7b979784841d4b0a926f5fb0f4e89e8542a167eed26b5bcfa02f27ab56bcff995710efdab99471b90
7
+ data.tar.gz: ce6dfea367c7eef407f99e5d7d624b398469ae3065ca098434599b0177579dae2700a7c33f90b9eb46b662dff4b3179cc00120f2a349fcbb869624ffc5a50459
@@ -4,7 +4,7 @@ class NearApi::Action
4
4
  include Borsh
5
5
 
6
6
  borsh signer_id: :string,
7
- key_pair: { key_type: :u8, public_key: 32 },
7
+ key: { key_type: :u8, public_key: 32 },
8
8
  nonce: :u64,
9
9
  receiver_id: :string,
10
10
  block_hash: 32,
@@ -18,48 +18,49 @@ class NearApi::Action
18
18
  new(*args).async
19
19
  end
20
20
 
21
- def initialize(receiver_id, actions, config: NearApi.config)
21
+ def initialize(receiver_id, actions, config: NearApi.config, key: NearApi.key)
22
+ @key = key
22
23
  @receiver_id = receiver_id
23
- @actions = Array[actions]
24
+ @actions = Array(actions)
24
25
  @config = config
25
26
  @api = NearApi::Api.new(config)
26
27
  end
27
28
 
28
29
  def call
29
- call_api('broadcast_tx_commit')
30
+ signature = key.sign(message.digest)
31
+ call_api('broadcast_tx_commit', message.message, signature)
30
32
  end
31
33
 
32
34
  def async
33
- call_api('broadcast_tx_commit')
35
+ signature = key.sign(message.digest)
36
+ call_api('broadcast_tx_async', message.message, signature)
34
37
  end
35
38
 
36
- private
37
-
38
-
39
- def call_api(method)
40
- msg = to_borsh
41
- digest = Digest::SHA256.digest(msg)
42
- signature = key_pair.sign(digest)
39
+ def message
40
+ @message ||= begin
41
+ message = to_borsh
42
+ Struct.new(:message, :digest).new(message, Digest::SHA256.digest(message))
43
+ end
44
+ end
43
45
 
44
- signed_transaction = msg + Borsh::Integer.new(key_pair.key_type, :u8).to_borsh + signature
46
+ def call_api(method, message, signature)
47
+ signed_transaction = message + Borsh::Integer.new(key.key_type, :u8).to_borsh + signature
45
48
  api.json_rpc(method, [Base64.strict_encode64(signed_transaction)])
46
49
  end
47
50
 
48
- def signer_id
49
- config.signer_id
50
- end
51
+ private
51
52
 
52
- def key_pair
53
- config.key_pair
53
+ def signer_id
54
+ key.signer_id
54
55
  end
55
56
 
56
57
  def nonce
57
- api.nonce + 1
58
+ api.nonce(key) + 1
58
59
  end
59
60
 
60
61
  def block_hash
61
62
  api.block_hash
62
63
  end
63
64
 
64
- attr_reader :receiver_id, :actions, :config, :api
65
+ attr_reader :receiver_id, :actions, :key, :config, :api
65
66
  end
data/lib/near_api/api.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class NearApi::Api
4
- def initialize(config = Config.new)
4
+ def initialize(config = NearApi.config)
5
5
  @config = config
6
6
  end
7
7
 
@@ -14,20 +14,20 @@ class NearApi::Api
14
14
  JSON.parse(Net::HTTP.get(uri))
15
15
  end
16
16
 
17
- def view_access_key
17
+ def view_access_key(key)
18
18
  call(
19
19
  'query',
20
20
  {
21
21
  "request_type": "view_access_key",
22
- "account_id": config.signer_id,
23
- "public_key": NearApi::Base58.encode(config.key_pair.public_key),
22
+ "account_id": key.signer_id,
23
+ "public_key": NearApi::Base58.encode(key.public_key),
24
24
  "finality": 'optimistic'
25
25
  }
26
26
  )
27
27
  end
28
28
 
29
- def nonce
30
- view_access_key['result']['nonce']
29
+ def nonce(key)
30
+ view_access_key(key)['result']['nonce']
31
31
  end
32
32
 
33
33
  def block_hash
@@ -1,15 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class NearApi::Config
4
- def initialize(
5
- signer_id: ENV.fetch('NEAR_SIGNER_ID'),
6
- key_pair: ENV.fetch('NEAR_KEYPAIR'),
7
- node_url: ENV.fetch('NEAR_NODE_URL')
8
- )
9
- @signer_id = signer_id
10
- @key_pair = NearApi::KeyPair.new(key_pair)
4
+ def initialize(node_url: ENV.fetch('NEAR_NODE_URL'))
11
5
  @node_url = node_url
12
6
  end
13
7
 
14
- attr_reader :signer_id, :key_pair, :node_url
8
+ attr_reader :node_url
15
9
  end
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+ require 'ed25519'
3
+
4
+ class NearApi::Key
5
+ attr_reader :signer_id, :public_key
6
+
7
+ def initialize(signer_id = ENV.fetch('NEAR_SIGNER_ID'),
8
+ key_pair: ENV.fetch('NEAR_KEYPAIR', nil),
9
+ public_key: ENV.fetch('NEAR_PUBLIC_KEY', nil))
10
+ @signer_id = signer_id
11
+ if (!key_pair.nil? && !public_key.nil?) || (key_pair.nil? && public_key.nil?)
12
+ raise ArgumentError, "please specify one of: key_pair or public_key"
13
+ end
14
+
15
+ unless key_pair.nil?
16
+ key_value = if key_pair.include?(':')
17
+ key_pair.split(':').last
18
+ else
19
+ key_pair
20
+ end
21
+ bytestring = NearApi::Base58.decode(key_value)
22
+ @key_pair = Ed25519::SigningKey.from_keypair(bytestring)
23
+ @public_key = @key_pair.verify_key.to_bytes
24
+ end
25
+
26
+ unless public_key.nil?
27
+ key_value = if public_key.include?(':')
28
+ public_key.split(':').last
29
+ else
30
+ public_key
31
+ end
32
+ bytestring = NearApi::Base58.decode(key_value)
33
+ @public_key = Ed25519::VerifyKey.new(bytestring)
34
+ end
35
+ end
36
+
37
+ def key_type
38
+ 0
39
+ end
40
+
41
+ def sign(msg)
42
+ key_pair.sign(msg)
43
+ end
44
+
45
+ private
46
+
47
+ attr_reader :key_pair
48
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ class NearApi::Query
4
+ def initialize(config = NearApi.config)
5
+ @api = NearApi::Api.new(config)
6
+ end
7
+
8
+ def function(account_id, method_name, args, finality: 'final')
9
+ params = {
10
+ request_type: 'call_function',
11
+ finality: finality,
12
+ account_id: account_id,
13
+ method_name: method_name,
14
+ args_base64: Base64.strict_encode64(args.to_json)
15
+ }
16
+ call_api(params)
17
+ end
18
+
19
+ private
20
+
21
+ attr_reader :api
22
+
23
+ def call_api(params)
24
+ api.json_rpc('query', params)
25
+ end
26
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module NearApi
4
- VERSION = "0.1.2"
4
+ VERSION = "0.1.3"
5
5
  end
data/lib/near_api.rb CHANGED
@@ -6,11 +6,12 @@ require 'json'
6
6
  require "base64"
7
7
  require 'borsh'
8
8
  require_relative "near_api/version"
9
- require_relative "near_api/key_pair"
9
+ require_relative "near_api/key"
10
10
  require_relative "near_api/action"
11
+ require_relative 'near_api/query'
11
12
  require_relative "near_api/action/function_call"
12
13
  require_relative "near_api/config"
13
- require_relative "near_api/api"
14
+ require_relative 'near_api/api'
14
15
  require_relative "near_api/base58"
15
16
 
16
17
  module NearApi
@@ -23,4 +24,12 @@ module NearApi
23
24
  def config=(config)
24
25
  @config = config
25
26
  end
27
+
28
+ def self.key
29
+ @key ||= Key.new
30
+ end
31
+
32
+ def key=(key)
33
+ @key = key
34
+ end
26
35
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: near_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Serg Tyatin
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-12-18 00:00:00.000000000 Z
11
+ date: 2021-12-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: borsh-rb
@@ -87,7 +87,8 @@ files:
87
87
  - lib/near_api/api.rb
88
88
  - lib/near_api/base58.rb
89
89
  - lib/near_api/config.rb
90
- - lib/near_api/key_pair.rb
90
+ - lib/near_api/key.rb
91
+ - lib/near_api/query.rb
91
92
  - lib/near_api/version.rb
92
93
  - near_api.gemspec
93
94
  homepage: https://github.com/2rba/near_api
@@ -1,30 +0,0 @@
1
- # frozen_string_literal: true
2
- require 'ed25519'
3
-
4
- class NearApi::KeyPair
5
- def initialize(value)
6
- key_value = if value.include?(':')
7
- value.split(':').last
8
- else
9
- value
10
- end
11
- bytestring = NearApi::Base58.decode(key_value)
12
- @key_pair = Ed25519::SigningKey.from_keypair(bytestring)
13
- end
14
-
15
- def public_key
16
- key_pair.verify_key.to_bytes
17
- end
18
-
19
- def key_type
20
- 0
21
- end
22
-
23
- def sign(msg)
24
- key_pair.sign(msg)
25
- end
26
-
27
- private
28
-
29
- attr_reader :key_pair
30
- end