near_api 0.1.4 → 0.2.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.
- checksums.yaml +4 -4
- data/.github/workflows/ci.yml +31 -0
- data/Gemfile +2 -2
- data/README.md +107 -9
- data/Rakefile +2 -2
- data/bin/console +3 -3
- data/lib/near_api/access_key.rb +20 -0
- data/lib/near_api/actions/add_key.rb +26 -0
- data/lib/near_api/actions/create_account.rb +17 -0
- data/lib/near_api/actions/delete_account.rb +24 -0
- data/lib/near_api/actions/deploy_contract.rb +24 -0
- data/lib/near_api/actions/function_call.rb +30 -0
- data/lib/near_api/actions/transfer.rb +29 -0
- data/lib/near_api/api.rb +26 -5
- data/lib/near_api/base58.rb +13 -6
- data/lib/near_api/key.rb +2 -1
- data/lib/near_api/permissions/full_access_permission.rb +17 -0
- data/lib/near_api/permissions/function_call_permission.rb +28 -0
- data/lib/near_api/query.rb +3 -3
- data/lib/near_api/status.rb +25 -0
- data/lib/near_api/transaction.rb +72 -0
- data/lib/near_api/version.rb +1 -1
- data/lib/near_api/yocto.rb +11 -0
- data/lib/near_api.rb +18 -8
- data/near_api.gemspec +17 -17
- metadata +29 -18
- data/lib/near_api/action/function_call.rb +0 -26
- data/lib/near_api/action.rb +0 -66
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fa4a1b8997930d0b94e6a6a748a47cf5da7f1f80b5538e284b9d332f74429037
|
4
|
+
data.tar.gz: c00578f5f62af686602a4b7f87f9ecc713d419a4cc7d7cb8d663597d54245642
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 215dced20e4c338fa050e4f6d25e6b7f62511db23056bf9aaaa849d53eadebf7af4151f01b2921ff767cf54c79a9c29525470ebfabd64ed571b552c69eec1f00
|
7
|
+
data.tar.gz: ba78a6b6935a1d6e891ea3edb7872fd544a539301c53a61ddbc904f2cc5723f89e210aaebf9d734c022b8e4f66a78120d9e65b8b7809fbeccebda4ce01e18ece
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# This workflow uses actions that are not certified by GitHub.
|
2
|
+
# They are provided by a third-party and are governed by
|
3
|
+
# separate terms of service, privacy policy, and support
|
4
|
+
# documentation.
|
5
|
+
# This workflow will download a prebuilt Ruby version, install dependencies and run tests with Rake
|
6
|
+
# For more information see: https://github.com/marketplace/actions/setup-ruby-jruby-and-truffleruby
|
7
|
+
|
8
|
+
name: rspec
|
9
|
+
|
10
|
+
on:
|
11
|
+
push:
|
12
|
+
jobs:
|
13
|
+
test:
|
14
|
+
runs-on: ubuntu-latest
|
15
|
+
concurrency:
|
16
|
+
group: ${{ github.ref }}
|
17
|
+
cancel-in-progress: true
|
18
|
+
steps:
|
19
|
+
- uses: actions/checkout@v2
|
20
|
+
- name: Set up Ruby
|
21
|
+
uses: ruby/setup-ruby@v1
|
22
|
+
with:
|
23
|
+
ruby-version: 3.0.2
|
24
|
+
- name: Install dependencies
|
25
|
+
run: bundle install --path vendor/bundle --jobs 4 --retry 3
|
26
|
+
- name: Rspec
|
27
|
+
run: bundle exec rspec
|
28
|
+
env:
|
29
|
+
NEAR_SIGNER_ID: ${{ secrets.NEAR_SIGNER_ID }}
|
30
|
+
NEAR_KEYPAIR: ${{ secrets.NEAR_KEYPAIR }}
|
31
|
+
NEAR_NODE_URL: ${{ secrets.NEAR_NODE_URL }}
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -1,8 +1,7 @@
|
|
1
|
+

|
1
2
|
# NearApi
|
2
3
|
|
3
|
-
|
4
|
-
|
5
|
-
TODO: Delete this and the text above, and describe your gem
|
4
|
+
[Near Protocol API](https://docs.near.org/docs/api/overview) ruby library
|
6
5
|
|
7
6
|
## Installation
|
8
7
|
|
@@ -20,19 +19,118 @@ Or install it yourself as:
|
|
20
19
|
|
21
20
|
$ gem install near_api
|
22
21
|
|
22
|
+
## Configuration
|
23
|
+
|
24
|
+
Via ENV variables:
|
25
|
+
- `NEAR_NODE_URL` RPC node url (for ex. `https://rpc.mainnet.near.org`)
|
26
|
+
- `NEAR_SIGNER_ID` account name for transactions and `view_access_key` requests (`account_id` in `.near-credentials`)
|
27
|
+
- `NEAR_KEYPAIR` base58 encoded private + public keys (`ed25519:4556s...` in `.near-credentials` named as `private_key` )
|
28
|
+
if no private key is available public key can be specified as `NEAR_PUBLIC_KEY` for view requests
|
29
|
+
|
30
|
+
Via explicit params:
|
31
|
+
example:
|
32
|
+
```ruby
|
33
|
+
key = NearApi::Key.new('account_id.testnet', key_pair: 'ed25519:4556s...')
|
34
|
+
config = NearApi::Config.new(node_url: 'https://rpc.testnet.near.org')
|
35
|
+
NearApi::Transaction.new(..., key: key, config: config)
|
36
|
+
```
|
37
|
+
|
23
38
|
## Usage
|
24
39
|
|
25
|
-
|
40
|
+
Operations:
|
41
|
+
- [View](#view)
|
42
|
+
- [Transaction](#transaction)
|
26
43
|
|
27
|
-
|
44
|
+
#### `View`
|
45
|
+
NearApi::Api instance view methods:
|
46
|
+
- [view_access_key](https://docs.near.org/docs/api/rpc/access-keys#view-access-key)
|
47
|
+
- [view_access_key_list](https://docs.near.org/docs/api/rpc/access-keys#view-access-key-list)
|
48
|
+
- [view_account](https://docs.near.org/docs/api/rpc/contracts#view-account)
|
28
49
|
|
29
|
-
|
50
|
+
example:
|
51
|
+
```ruby
|
52
|
+
NearApi::Api.new.view_access_key('account_id.testnet')
|
53
|
+
```
|
54
|
+
Response: hash as described in NEAR Protocol JS API
|
30
55
|
|
31
|
-
|
56
|
+
Other [methods](https://docs.near.org/docs/api/rpc/access-keys#view-access-key-changes-single) can be called as:
|
57
|
+
```ruby
|
58
|
+
NearApi::Api.new.json_rpc('EXPERIMENTAL_changes', {
|
59
|
+
"changes_type": "single_access_key_changes",
|
60
|
+
"keys": [
|
61
|
+
{
|
62
|
+
"account_id": "example-acct.testnet",
|
63
|
+
"public_key": "ed25519:25KEc7t7MQohAJ4EDThd2vkksKkwangnuJFzcoiXj9oM"
|
64
|
+
}
|
65
|
+
],
|
66
|
+
"finality": "final"
|
67
|
+
}
|
68
|
+
)
|
69
|
+
```
|
32
70
|
|
33
|
-
|
71
|
+
#### Smart contract view function
|
72
|
+
[query smart contract](https://docs.near.org/docs/api/rpc/contracts#call-a-contract-function)
|
73
|
+
|
74
|
+
```ruby
|
75
|
+
NearApi::Query.new.call(
|
76
|
+
'account_id.testnet',
|
77
|
+
'get_num',
|
78
|
+
{}
|
79
|
+
)
|
80
|
+
```
|
81
|
+
|
82
|
+
### `Transaction`
|
83
|
+
```ruby
|
84
|
+
NearApi::Transaction.new(
|
85
|
+
'account_id.testnet', # Transaction call receiver
|
86
|
+
[NearApi::Actions::FunctionCall.new('increment', {})], # Array of Actions
|
87
|
+
key: key, # Optional key
|
88
|
+
config: config # Optional config
|
89
|
+
).call
|
90
|
+
```
|
91
|
+
|
92
|
+
The example above is waiting for [transaction complete](https://docs.near.org/docs/api/rpc/transactions#send-transaction-await)
|
93
|
+
|
94
|
+
To send transaction [async](https://docs.near.org/docs/api/rpc/transactions#send-transaction-async):
|
95
|
+
```ruby
|
96
|
+
NearApi::Transaction.new(...).async
|
97
|
+
```
|
34
98
|
|
35
|
-
|
99
|
+
Transaction has one or many actions
|
100
|
+
Actions:
|
101
|
+
- Create Account
|
102
|
+
- Deploy Contract
|
103
|
+
- Function Call
|
104
|
+
- Transfer
|
105
|
+
- Stake
|
106
|
+
- Add Key
|
107
|
+
- Delete Key
|
108
|
+
- Delete Account
|
109
|
+
|
110
|
+
[Specs](https://github.com/near-rails-guide/near_api/tree/master/spec/near_api/actions)
|
111
|
+
|
112
|
+
#### Transaction status
|
113
|
+
[Transaction_status](https://docs.near.org/docs/api/rpc/transactions#transaction-status)
|
114
|
+
```ruby
|
115
|
+
NearApi::Status.new.transaction_status(transaction_hash)
|
116
|
+
```
|
117
|
+
[Transaction Status with Receipts](https://docs.near.org/docs/api/rpc/transactions#transaction-status-with-receipts)
|
118
|
+
```ruby
|
119
|
+
NearApi::Status.new.final_transaction_status(transaction_hash)
|
120
|
+
```
|
121
|
+
|
122
|
+
#### External Signing
|
123
|
+
To sign transaction with external signing service (dedicated secure service or hardware ledger)
|
124
|
+
```ruby
|
125
|
+
transaction = NearApi::Transaction.new(...)
|
126
|
+
digest = transaction.message.digest
|
127
|
+
# sign with external service
|
128
|
+
signature = Ed25519::SigningKey.from_keypair(bytestring).sign(digest)
|
129
|
+
transaction.call_api('broadcast_tx_commit', transaction.message.message, signature)
|
130
|
+
```
|
131
|
+
|
132
|
+
## Contributing
|
133
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/near-rails-guide/near_api
|
36
134
|
|
37
135
|
## License
|
38
136
|
|
data/Rakefile
CHANGED
data/bin/console
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
# frozen_string_literal: true
|
3
3
|
|
4
|
-
require
|
5
|
-
require
|
4
|
+
require 'bundler/setup'
|
5
|
+
require 'near_api'
|
6
6
|
|
7
7
|
# You can add fixtures and/or initialization code here to make experimenting
|
8
8
|
# with your gem easier. You can also use a different console, if you like.
|
@@ -11,5 +11,5 @@ require "near_api"
|
|
11
11
|
# require "pry"
|
12
12
|
# Pry.start
|
13
13
|
|
14
|
-
require
|
14
|
+
require 'irb'
|
15
15
|
IRB.start(__FILE__)
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class NearApi::AccessKey
|
4
|
+
include Borsh
|
5
|
+
|
6
|
+
borsh nonce: :u64,
|
7
|
+
permission: :borsh
|
8
|
+
|
9
|
+
def initialize(permission)
|
10
|
+
@permission = permission
|
11
|
+
end
|
12
|
+
|
13
|
+
private
|
14
|
+
|
15
|
+
attr_reader :permission
|
16
|
+
|
17
|
+
def nonce
|
18
|
+
0
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module NearApi
|
4
|
+
module Actions
|
5
|
+
class AddKey
|
6
|
+
include Borsh
|
7
|
+
|
8
|
+
borsh action_code: :u8,
|
9
|
+
public_key: { key_type: :u8, public_key: 32 },
|
10
|
+
access_key: :borsh
|
11
|
+
|
12
|
+
def initialize(public_key, permission)
|
13
|
+
@public_key = public_key
|
14
|
+
@access_key = NearApi::AccessKey.new(permission)
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
attr_reader :public_key, :access_key
|
20
|
+
|
21
|
+
def action_code
|
22
|
+
5
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module NearApi
|
4
|
+
module Actions
|
5
|
+
class DeleteAccount
|
6
|
+
include Borsh
|
7
|
+
|
8
|
+
borsh action_code: :u8,
|
9
|
+
beneficiary_id: :string
|
10
|
+
|
11
|
+
def initialize(beneficiary_id)
|
12
|
+
@beneficiary_id = beneficiary_id
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
attr_reader :beneficiary_id
|
18
|
+
|
19
|
+
def action_code
|
20
|
+
7
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module NearApi
|
4
|
+
module Actions
|
5
|
+
class DeployContract
|
6
|
+
include Borsh
|
7
|
+
|
8
|
+
borsh action_code: :u8,
|
9
|
+
contract: :string
|
10
|
+
|
11
|
+
def initialize(contract)
|
12
|
+
@contract = contract
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
attr_reader :contract
|
18
|
+
|
19
|
+
def action_code
|
20
|
+
1
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module NearApi
|
4
|
+
module Actions
|
5
|
+
class FunctionCall
|
6
|
+
include Borsh
|
7
|
+
|
8
|
+
borsh action_code: :u8,
|
9
|
+
method_name: :string,
|
10
|
+
args: :string,
|
11
|
+
gas: :u64,
|
12
|
+
amount: :u128
|
13
|
+
|
14
|
+
def initialize(method_name, args, gas: 100_000_000_000_000, amount: 0)
|
15
|
+
@method_name = method_name
|
16
|
+
@args = args.is_a?(String) ? args : args.to_json
|
17
|
+
@gas = gas
|
18
|
+
@amount = amount
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
attr_reader :method_name, :args, :gas, :amount
|
24
|
+
|
25
|
+
def action_code
|
26
|
+
2
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module NearApi
|
4
|
+
module Actions
|
5
|
+
class Transfer
|
6
|
+
include Borsh
|
7
|
+
|
8
|
+
borsh action_code: :u8,
|
9
|
+
amount: :u128
|
10
|
+
|
11
|
+
def initialize(near_amount: nil, amount: nil)
|
12
|
+
if (near_amount.nil? && amount.nil?) || (!near_amount.nil? && !amount.nil?)
|
13
|
+
raise ArgumentError, 'please specify one of: near_amount or amount'
|
14
|
+
end
|
15
|
+
|
16
|
+
@amount = amount unless amount.nil?
|
17
|
+
@amount = Yocto.from_near(near_amount) unless near_amount.nil?
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
attr_reader :amount
|
23
|
+
|
24
|
+
def action_code
|
25
|
+
3
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
data/lib/near_api/api.rb
CHANGED
@@ -14,14 +14,36 @@ class NearApi::Api
|
|
14
14
|
JSON.parse(Net::HTTP.get(uri))
|
15
15
|
end
|
16
16
|
|
17
|
-
def view_access_key(key)
|
17
|
+
def view_access_key(key, finality: 'optimistic')
|
18
18
|
call(
|
19
19
|
'query',
|
20
20
|
{
|
21
|
-
"request_type":
|
21
|
+
"request_type": 'view_access_key',
|
22
22
|
"account_id": key.signer_id,
|
23
23
|
"public_key": NearApi::Base58.encode(key.public_key),
|
24
|
-
"finality":
|
24
|
+
"finality": finality
|
25
|
+
}
|
26
|
+
)
|
27
|
+
end
|
28
|
+
|
29
|
+
def view_access_key_list(account_id, finality: 'optimistic')
|
30
|
+
call(
|
31
|
+
'query',
|
32
|
+
{
|
33
|
+
"request_type": 'view_access_key_list',
|
34
|
+
"account_id": account_id,
|
35
|
+
"finality": finality
|
36
|
+
}
|
37
|
+
)
|
38
|
+
end
|
39
|
+
|
40
|
+
def view_account(account_id, finality: 'optimistic')
|
41
|
+
call(
|
42
|
+
'query',
|
43
|
+
{
|
44
|
+
"request_type": 'view_account',
|
45
|
+
"account_id": account_id,
|
46
|
+
"finality": finality
|
25
47
|
}
|
26
48
|
)
|
27
49
|
end
|
@@ -37,7 +59,7 @@ class NearApi::Api
|
|
37
59
|
def json_rpc(method, payload)
|
38
60
|
json_rpc_payload = {
|
39
61
|
id: 'dontcare',
|
40
|
-
jsonrpc:
|
62
|
+
jsonrpc: '2.0',
|
41
63
|
method: method,
|
42
64
|
params: payload
|
43
65
|
}
|
@@ -55,4 +77,3 @@ class NearApi::Api
|
|
55
77
|
|
56
78
|
attr_reader :config
|
57
79
|
end
|
58
|
-
|
data/lib/near_api/base58.rb
CHANGED
@@ -4,18 +4,22 @@ class NearApi::Base58
|
|
4
4
|
BASE = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
|
5
5
|
|
6
6
|
def self.encode(bytestring)
|
7
|
-
number = bytestring.unpack(
|
8
|
-
sum + byte * (256
|
7
|
+
number = bytestring.unpack('C*').reverse.each_with_index.inject(0) do |sum, (byte, index)|
|
8
|
+
sum + byte * (256**index)
|
9
9
|
end
|
10
|
-
|
10
|
+
nzeroes = bytestring.bytes.find_index { |b| b != 0 } || bytestring.length - 1
|
11
|
+
prefix = BASE[0] * nzeroes
|
12
|
+
prefix + tos(number)
|
11
13
|
end
|
12
14
|
|
13
15
|
def self.decode(encoded_string)
|
14
16
|
integer = toi encoded_string
|
15
|
-
|
17
|
+
nzeroes = encoded_string.chars.find_index { |c| c != BASE[0] } || encoded_string.length - 1
|
18
|
+
prefix = nzeroes < 0 ? '' : 0.chr * nzeroes
|
19
|
+
prefix + to_bytestring(integer)
|
16
20
|
end
|
17
21
|
|
18
|
-
def self.toi(string=to_s, base=58, digits=BASE)
|
22
|
+
def self.toi(string = to_s, base = 58, digits = BASE)
|
19
23
|
return nil if string.empty?
|
20
24
|
|
21
25
|
integer = 0
|
@@ -26,9 +30,10 @@ class NearApi::Base58
|
|
26
30
|
integer
|
27
31
|
end
|
28
32
|
|
29
|
-
def self.tos(integer=to_i, base=58, digits=BASE)
|
33
|
+
def self.tos(integer = to_i, base = 58, digits = BASE)
|
30
34
|
return '' if integer.nil?
|
31
35
|
return digits[0] if integer == 0
|
36
|
+
|
32
37
|
string = ''
|
33
38
|
while integer > 0
|
34
39
|
integer, index = integer.divmod(base)
|
@@ -38,6 +43,8 @@ class NearApi::Base58
|
|
38
43
|
end
|
39
44
|
|
40
45
|
def self.to_bytestring(number)
|
46
|
+
return 0.chr if number == 0
|
47
|
+
|
41
48
|
integer = number
|
42
49
|
result = ''
|
43
50
|
while integer > 0
|
data/lib/near_api/key.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
+
|
2
3
|
require 'ed25519'
|
3
4
|
|
4
5
|
class NearApi::Key
|
@@ -9,7 +10,7 @@ class NearApi::Key
|
|
9
10
|
public_key: ENV.fetch('NEAR_PUBLIC_KEY', nil))
|
10
11
|
@signer_id = signer_id
|
11
12
|
if (!key_pair.nil? && !public_key.nil?) || (key_pair.nil? && public_key.nil?)
|
12
|
-
raise ArgumentError,
|
13
|
+
raise ArgumentError, 'please specify one of: key_pair or public_key'
|
13
14
|
end
|
14
15
|
|
15
16
|
unless key_pair.nil?
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module NearApi
|
4
|
+
module Permissions
|
5
|
+
class FunctionCallPermission
|
6
|
+
include Borsh
|
7
|
+
|
8
|
+
borsh permission_code: :u8,
|
9
|
+
allowance: :u128,
|
10
|
+
receiver_id: :string,
|
11
|
+
method_names: [:string]
|
12
|
+
|
13
|
+
def initialize(allowance, receiver_id, method_names)
|
14
|
+
@allowance = allowance
|
15
|
+
@receiver_id = receiver_id
|
16
|
+
@method_names = method_names
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
attr_reader :allowance, :receiver_id, :method_names
|
22
|
+
|
23
|
+
def permission_code
|
24
|
+
0
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
data/lib/near_api/query.rb
CHANGED
@@ -5,12 +5,12 @@ class NearApi::Query
|
|
5
5
|
@api = NearApi::Api.new(config)
|
6
6
|
end
|
7
7
|
|
8
|
-
def
|
8
|
+
def call(account_id, method_name, args, finality: 'optimistic')
|
9
9
|
params = {
|
10
|
-
request_type: 'call_function',
|
11
|
-
finality: finality,
|
12
10
|
account_id: account_id,
|
13
11
|
method_name: method_name,
|
12
|
+
finality: finality,
|
13
|
+
request_type: 'call_function',
|
14
14
|
args_base64: Base64.strict_encode64(args.to_json)
|
15
15
|
}
|
16
16
|
call_api(params)
|
@@ -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
|
@@ -0,0 +1,72 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module NearApi
|
4
|
+
class Transaction
|
5
|
+
include Borsh
|
6
|
+
|
7
|
+
borsh signer_id: :string,
|
8
|
+
key: { key_type: :u8, public_key: 32 },
|
9
|
+
nonce: :u64,
|
10
|
+
receiver_id: :string,
|
11
|
+
block_hash: 32,
|
12
|
+
actions: [:borsh]
|
13
|
+
|
14
|
+
def self.call(*args)
|
15
|
+
new(*args).call
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.async(*args)
|
19
|
+
new(*args).async
|
20
|
+
end
|
21
|
+
|
22
|
+
def initialize(receiver_id, actions, config: NearApi.config, key: NearApi.key)
|
23
|
+
@key = key
|
24
|
+
@receiver_id = receiver_id
|
25
|
+
@actions = Array(actions)
|
26
|
+
@config = config
|
27
|
+
@api = NearApi::Api.new(config)
|
28
|
+
end
|
29
|
+
|
30
|
+
def call
|
31
|
+
signature = key.sign(message.digest)
|
32
|
+
call_api('broadcast_tx_commit', message.message, signature)
|
33
|
+
end
|
34
|
+
|
35
|
+
def async
|
36
|
+
signature = key.sign(message.digest)
|
37
|
+
call_api('broadcast_tx_async', message.message, signature)
|
38
|
+
end
|
39
|
+
|
40
|
+
def message
|
41
|
+
@message ||= begin
|
42
|
+
message = to_borsh
|
43
|
+
Struct.new(:message, :digest).new(message, Digest::SHA256.digest(message))
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def transaction_hash
|
48
|
+
NearApi::Base58.encode(message.digest)
|
49
|
+
end
|
50
|
+
|
51
|
+
def call_api(method, message, signature)
|
52
|
+
signed_transaction = message + Borsh::Integer.new(key.key_type, :u8).to_borsh + signature
|
53
|
+
api.json_rpc(method, [Base64.strict_encode64(signed_transaction)])
|
54
|
+
end
|
55
|
+
|
56
|
+
private
|
57
|
+
|
58
|
+
def signer_id
|
59
|
+
key.signer_id
|
60
|
+
end
|
61
|
+
|
62
|
+
def nonce
|
63
|
+
api.nonce(key) + 1
|
64
|
+
end
|
65
|
+
|
66
|
+
def block_hash
|
67
|
+
api.block_hash
|
68
|
+
end
|
69
|
+
|
70
|
+
attr_reader :receiver_id, :actions, :key, :config, :api
|
71
|
+
end
|
72
|
+
end
|
data/lib/near_api/version.rb
CHANGED
data/lib/near_api.rb
CHANGED
@@ -3,16 +3,26 @@
|
|
3
3
|
require 'uri'
|
4
4
|
require 'net/https'
|
5
5
|
require 'json'
|
6
|
-
require
|
6
|
+
require 'base64'
|
7
7
|
require 'borsh'
|
8
|
-
require_relative
|
9
|
-
require_relative
|
10
|
-
require_relative
|
11
|
-
require_relative 'near_api/
|
12
|
-
require_relative
|
13
|
-
require_relative "near_api/config"
|
8
|
+
require_relative 'near_api/version'
|
9
|
+
require_relative 'near_api/key'
|
10
|
+
require_relative 'near_api/status'
|
11
|
+
require_relative 'near_api/transaction'
|
12
|
+
require_relative 'near_api/access_key'
|
14
13
|
require_relative 'near_api/api'
|
15
|
-
require_relative
|
14
|
+
require_relative 'near_api/query'
|
15
|
+
require_relative 'near_api/actions/add_key'
|
16
|
+
require_relative 'near_api/actions/create_account'
|
17
|
+
require_relative 'near_api/actions/function_call'
|
18
|
+
require_relative 'near_api/actions/transfer'
|
19
|
+
require_relative 'near_api/actions/delete_account'
|
20
|
+
require_relative 'near_api/actions/deploy_contract'
|
21
|
+
require_relative 'near_api/config'
|
22
|
+
require_relative 'near_api/permissions/full_access_permission'
|
23
|
+
require_relative 'near_api/permissions/function_call_permission'
|
24
|
+
require_relative 'near_api/base58'
|
25
|
+
require_relative 'near_api/yocto'
|
16
26
|
|
17
27
|
module NearApi
|
18
28
|
class Error < StandardError; end
|
data/near_api.gemspec
CHANGED
@@ -1,36 +1,36 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require_relative
|
3
|
+
require_relative 'lib/near_api/version'
|
4
4
|
|
5
5
|
Gem::Specification.new do |spec|
|
6
|
-
spec.name =
|
6
|
+
spec.name = 'near_api'
|
7
7
|
spec.version = NearApi::VERSION
|
8
|
-
spec.authors = [
|
9
|
-
spec.email = [
|
8
|
+
spec.authors = ['Serg Tyatin']
|
9
|
+
spec.email = ['700@2rba.com']
|
10
10
|
|
11
|
-
spec.summary =
|
12
|
-
spec.homepage =
|
13
|
-
spec.license =
|
14
|
-
spec.required_ruby_version =
|
11
|
+
spec.summary = 'NEAR blockchain ruby API'
|
12
|
+
spec.homepage = 'https://github.com/near-rails-guide/near_api'
|
13
|
+
spec.license = 'MIT'
|
14
|
+
spec.required_ruby_version = '>= 2.4.0'
|
15
15
|
|
16
|
-
spec.metadata[
|
17
|
-
spec.metadata[
|
18
|
-
spec.metadata[
|
16
|
+
spec.metadata['homepage_uri'] = spec.homepage
|
17
|
+
spec.metadata['source_code_uri'] = 'https://github.com/near-rails-guide/near_api'
|
18
|
+
spec.metadata['changelog_uri'] = 'https://github.com/near-rails-guide/near_api'
|
19
19
|
|
20
20
|
# Specify which files should be added to the gem when it is released.
|
21
21
|
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
22
22
|
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
23
23
|
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
|
24
24
|
end
|
25
|
-
spec.bindir =
|
25
|
+
spec.bindir = 'exe'
|
26
26
|
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
|
27
|
-
spec.require_paths = [
|
27
|
+
spec.require_paths = ['lib']
|
28
28
|
|
29
|
-
spec.add_dependency
|
30
|
-
spec.add_dependency
|
29
|
+
spec.add_dependency 'borsh-rb'
|
30
|
+
spec.add_dependency 'ed25519'
|
31
31
|
|
32
|
-
spec.add_development_dependency
|
33
|
-
spec.add_development_dependency
|
32
|
+
spec.add_development_dependency 'pry'
|
33
|
+
spec.add_development_dependency 'rspec', '~> 3.10'
|
34
34
|
|
35
35
|
# Uncomment to register a new dependency of your gem
|
36
36
|
# spec.add_dependency "example-gem", "~> 1.0"
|
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.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Serg Tyatin
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2022-04-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: borsh-rb
|
@@ -39,33 +39,33 @@ dependencies:
|
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
|
-
name:
|
42
|
+
name: pry
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- - "
|
45
|
+
- - ">="
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version: '
|
47
|
+
version: '0'
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
|
-
- - "
|
52
|
+
- - ">="
|
53
53
|
- !ruby/object:Gem::Version
|
54
|
-
version: '
|
54
|
+
version: '0'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
|
-
name:
|
56
|
+
name: rspec
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
|
-
- - "
|
59
|
+
- - "~>"
|
60
60
|
- !ruby/object:Gem::Version
|
61
|
-
version: '
|
61
|
+
version: '3.10'
|
62
62
|
type: :development
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
|
-
- - "
|
66
|
+
- - "~>"
|
67
67
|
- !ruby/object:Gem::Version
|
68
|
-
version: '
|
68
|
+
version: '3.10'
|
69
69
|
description:
|
70
70
|
email:
|
71
71
|
- 700@2rba.com
|
@@ -73,6 +73,7 @@ executables: []
|
|
73
73
|
extensions: []
|
74
74
|
extra_rdoc_files: []
|
75
75
|
files:
|
76
|
+
- ".github/workflows/ci.yml"
|
76
77
|
- ".gitignore"
|
77
78
|
- ".rspec"
|
78
79
|
- Gemfile
|
@@ -82,22 +83,32 @@ files:
|
|
82
83
|
- bin/console
|
83
84
|
- bin/setup
|
84
85
|
- lib/near_api.rb
|
85
|
-
- lib/near_api/
|
86
|
-
- lib/near_api/
|
86
|
+
- lib/near_api/access_key.rb
|
87
|
+
- lib/near_api/actions/add_key.rb
|
88
|
+
- lib/near_api/actions/create_account.rb
|
89
|
+
- lib/near_api/actions/delete_account.rb
|
90
|
+
- lib/near_api/actions/deploy_contract.rb
|
91
|
+
- lib/near_api/actions/function_call.rb
|
92
|
+
- lib/near_api/actions/transfer.rb
|
87
93
|
- lib/near_api/api.rb
|
88
94
|
- lib/near_api/base58.rb
|
89
95
|
- lib/near_api/config.rb
|
90
96
|
- lib/near_api/key.rb
|
97
|
+
- lib/near_api/permissions/full_access_permission.rb
|
98
|
+
- lib/near_api/permissions/function_call_permission.rb
|
91
99
|
- lib/near_api/query.rb
|
100
|
+
- lib/near_api/status.rb
|
101
|
+
- lib/near_api/transaction.rb
|
92
102
|
- lib/near_api/version.rb
|
103
|
+
- lib/near_api/yocto.rb
|
93
104
|
- near_api.gemspec
|
94
|
-
homepage: https://github.com/
|
105
|
+
homepage: https://github.com/near-rails-guide/near_api
|
95
106
|
licenses:
|
96
107
|
- MIT
|
97
108
|
metadata:
|
98
|
-
homepage_uri: https://github.com/
|
99
|
-
source_code_uri: https://github.com/
|
100
|
-
changelog_uri: https://github.com/
|
109
|
+
homepage_uri: https://github.com/near-rails-guide/near_api
|
110
|
+
source_code_uri: https://github.com/near-rails-guide/near_api
|
111
|
+
changelog_uri: https://github.com/near-rails-guide/near_api
|
101
112
|
post_install_message:
|
102
113
|
rdoc_options: []
|
103
114
|
require_paths:
|
@@ -1,26 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
class NearApi::Action::FunctionCall
|
4
|
-
include Borsh
|
5
|
-
|
6
|
-
borsh action_code: :u8,
|
7
|
-
method_name: :string,
|
8
|
-
args: :string,
|
9
|
-
gas: :u64,
|
10
|
-
amount: :u128
|
11
|
-
|
12
|
-
def initialize(method_name, args, gas: 100000000000000, amount: 0)
|
13
|
-
@method_name = method_name
|
14
|
-
@args = args
|
15
|
-
@gas = gas
|
16
|
-
@amount = amount
|
17
|
-
end
|
18
|
-
|
19
|
-
private
|
20
|
-
|
21
|
-
attr_reader :method_name, :args, :gas, :amount
|
22
|
-
|
23
|
-
def action_code
|
24
|
-
2
|
25
|
-
end
|
26
|
-
end
|
data/lib/near_api/action.rb
DELETED
@@ -1,66 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
class NearApi::Action
|
4
|
-
include Borsh
|
5
|
-
|
6
|
-
borsh signer_id: :string,
|
7
|
-
key: { key_type: :u8, public_key: 32 },
|
8
|
-
nonce: :u64,
|
9
|
-
receiver_id: :string,
|
10
|
-
block_hash: 32,
|
11
|
-
actions: [:borsh]
|
12
|
-
|
13
|
-
def self.call(*args)
|
14
|
-
new(*args).call
|
15
|
-
end
|
16
|
-
|
17
|
-
def self.async(*args)
|
18
|
-
new(*args).async
|
19
|
-
end
|
20
|
-
|
21
|
-
def initialize(receiver_id, actions, config: NearApi.config, key: NearApi.key)
|
22
|
-
@key = key
|
23
|
-
@receiver_id = receiver_id
|
24
|
-
@actions = Array(actions)
|
25
|
-
@config = config
|
26
|
-
@api = NearApi::Api.new(config)
|
27
|
-
end
|
28
|
-
|
29
|
-
def call
|
30
|
-
signature = key.sign(message.digest)
|
31
|
-
call_api('broadcast_tx_commit', message.message, signature)
|
32
|
-
end
|
33
|
-
|
34
|
-
def async
|
35
|
-
signature = key.sign(message.digest)
|
36
|
-
call_api('broadcast_tx_async', message.message, signature)
|
37
|
-
end
|
38
|
-
|
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
|
45
|
-
|
46
|
-
def call_api(method, message, signature)
|
47
|
-
signed_transaction = message + Borsh::Integer.new(key.key_type, :u8).to_borsh + signature
|
48
|
-
api.json_rpc(method, [Base64.strict_encode64(signed_transaction)])
|
49
|
-
end
|
50
|
-
|
51
|
-
private
|
52
|
-
|
53
|
-
def signer_id
|
54
|
-
key.signer_id
|
55
|
-
end
|
56
|
-
|
57
|
-
def nonce
|
58
|
-
api.nonce(key) + 1
|
59
|
-
end
|
60
|
-
|
61
|
-
def block_hash
|
62
|
-
api.block_hash
|
63
|
-
end
|
64
|
-
|
65
|
-
attr_reader :receiver_id, :actions, :key, :config, :api
|
66
|
-
end
|