near_api 0.1.1 → 0.1.5

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: f3329e91efbd5b305744e92c0edd739a7fbdada5a40f412bc75594ea04d463d1
4
- data.tar.gz: 5e3543eda45e4d65ae1014beb90152708ed697df3c5896aa4d2aa49b22004d83
3
+ metadata.gz: cc403c86a44e7bd759b4c71d8bf7499fbf5145c06d2a680811e2dea09fd1dada
4
+ data.tar.gz: b72df18eee4fec70ccc3f500255fa933a391b29606f564fd4264983369c81311
5
5
  SHA512:
6
- metadata.gz: 2eb64ba5ecb1cfaacb14ded64a31d2605dc8fa14cd7d8fd7388840e5227a99732a6bb8edf6e83f759c3ee3eb1e39b722565d6f34bbd0353c54b5ce2eb9fa7d95
7
- data.tar.gz: dcca8c7e39e47f4b111f22371fc384fd194625bfae3c333a69039ad9bb56ae9f3cb9d7119e3ff4771af61e1db387b76476ed7c427b6d88600067ccf6fc350d12
6
+ metadata.gz: 3ba8d6c54f88f826e475105ff0bb64d59d57a35a0cdc3b196b69d43acddfcd3f9bbcd7af7eb6753704ca4b964f61fa13a0d21c280522de8f34fa55caad6cf2e4
7
+ data.tar.gz: 6d00755fa74f570b0acfbe51e64ca5e95c36b643a8c29509df0e6bcb0040de983ea638b319f927e1b9933581a551395257f56d11a92f3591b070ca330c6bbed6
@@ -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).to_bytes
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
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ class NearApi::Status
4
+ def initialize(config = NearApi.config)
5
+ @api = NearApi::Api.new(config)
6
+ end
7
+
8
+ def transaction_status(transaction_hash, key: NearApi.key)
9
+ params = [transaction_hash, key.signer_id]
10
+ call_api('tx', params)
11
+ end
12
+
13
+ def final_transaction_status(transaction_hash, key: NearApi.key)
14
+ params = [transaction_hash, key.signer_id]
15
+ call_api('EXPERIMENTAL_tx_status', params)
16
+ end
17
+
18
+ private
19
+
20
+ attr_reader :api
21
+
22
+ def call_api(method, params)
23
+ api.json_rpc(method, params)
24
+ end
25
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module NearApi
4
- VERSION = "0.1.1"
4
+ VERSION = "0.1.5"
5
5
  end
data/lib/near_api.rb CHANGED
@@ -3,17 +3,34 @@
3
3
  require 'uri'
4
4
  require 'net/https'
5
5
  require 'json'
6
- require "base64"
6
+ require 'base64'
7
7
  require 'borsh'
8
- require_relative "near_api/version"
9
- require_relative "near_api/key_pair"
10
- require_relative "near_api/action"
11
- require_relative "near_api/action/function_call"
12
- require_relative "near_api/config"
13
- require_relative "near_api/api"
8
+ require_relative 'near_api/version'
9
+ require_relative 'near_api/key'
10
+ require_relative 'near_api/status'
11
+ require_relative 'near_api/action'
12
+ require_relative 'near_api/query'
13
+ require_relative 'near_api/action/function_call'
14
+ require_relative 'near_api/config'
15
+ require_relative 'near_api/api'
14
16
  require_relative "near_api/base58"
15
17
 
16
18
  module NearApi
17
19
  class Error < StandardError; end
18
- # Your code goes here...
20
+
21
+ def self.config
22
+ @config ||= Config.new
23
+ end
24
+
25
+ def config=(config)
26
+ @config = config
27
+ end
28
+
29
+ def self.key
30
+ @key ||= Key.new
31
+ end
32
+
33
+ def key=(key)
34
+ @key = key
35
+ end
19
36
  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.1
4
+ version: 0.1.5
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-16 00:00:00.000000000 Z
11
+ date: 2021-12-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: borsh-rb
@@ -87,7 +87,9 @@ 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
92
+ - lib/near_api/status.rb
91
93
  - lib/near_api/version.rb
92
94
  - near_api.gemspec
93
95
  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