neb 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +62 -0
- data/.travis.yml +5 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +15 -0
- data/Guardfile +9 -0
- data/LICENSE.txt +21 -0
- data/README.md +123 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/neb/account.rb +55 -0
- data/lib/neb/address.rb +90 -0
- data/lib/neb/base_convert.rb +57 -0
- data/lib/neb/client/admin.rb +95 -0
- data/lib/neb/client/api.rb +104 -0
- data/lib/neb/client/request.rb +43 -0
- data/lib/neb/client/response.rb +36 -0
- data/lib/neb/client.rb +19 -0
- data/lib/neb/configuration.rb +38 -0
- data/lib/neb/constant.rb +35 -0
- data/lib/neb/core_ext.rb +45 -0
- data/lib/neb/exceptions.rb +29 -0
- data/lib/neb/key.rb +133 -0
- data/lib/neb/private_key.rb +91 -0
- data/lib/neb/proto/transaction.proto +40 -0
- data/lib/neb/proto/transaction_pb.rb +30 -0
- data/lib/neb/public_key.rb +87 -0
- data/lib/neb/secp256k1.rb +66 -0
- data/lib/neb/transaction.rb +120 -0
- data/lib/neb/utils.rb +128 -0
- data/lib/neb/version.rb +6 -0
- data/lib/neb.rb +71 -0
- data/neb.gemspec +40 -0
- data/tmp/.keep +0 -0
- metadata +288 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: ab59927760b6f667af8bb29812e60751cd73eafbaac68c715126f097c4d6def8
|
4
|
+
data.tar.gz: 021746fb0d659d33f032c97ab0296c3c280b011569e59b408a46cefd3d8a4d08
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 35cb95f930fb38bf471a359da1ce6adb4cf8c1505a7a3347df7752ef288a19bea3cedbafc6dbf6dcc4145d05cad92d0205c5127a152152f678a00b3982a4ab52
|
7
|
+
data.tar.gz: 56e40fd005edc6fd156f1444b13d7724e1e6a831d4e0ec657da9910a2954e69a02155afdbae9a856f1931eb1750401455df6aa532e46b0ce5cf027aee5fa7657
|
data/.gitignore
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
|
2
|
+
# Created by https://www.gitignore.io/api/ruby
|
3
|
+
|
4
|
+
### Ruby ###
|
5
|
+
*.gem
|
6
|
+
*.rbc
|
7
|
+
/.config
|
8
|
+
/coverage/
|
9
|
+
/InstalledFiles
|
10
|
+
/pkg/
|
11
|
+
/spec/reports/
|
12
|
+
/spec/examples.txt
|
13
|
+
/test/tmp/
|
14
|
+
/test/version_tmp/
|
15
|
+
# /tmp/
|
16
|
+
/log
|
17
|
+
|
18
|
+
# Used by dotenv library to load environment variables.
|
19
|
+
# .env
|
20
|
+
|
21
|
+
## Specific to RubyMotion:
|
22
|
+
.dat*
|
23
|
+
.repl_history
|
24
|
+
build/
|
25
|
+
*.bridgesupport
|
26
|
+
build-iPhoneOS/
|
27
|
+
build-iPhoneSimulator/
|
28
|
+
|
29
|
+
## Specific to RubyMotion (use of CocoaPods):
|
30
|
+
#
|
31
|
+
# We recommend against adding the Pods directory to your .gitignore. However
|
32
|
+
# you should judge for yourself, the pros and cons are mentioned at:
|
33
|
+
# https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
|
34
|
+
#
|
35
|
+
# vendor/Pods/
|
36
|
+
|
37
|
+
## Documentation cache and generated files:
|
38
|
+
/.yardoc/
|
39
|
+
/_yardoc/
|
40
|
+
/doc/
|
41
|
+
/rdoc/
|
42
|
+
|
43
|
+
## Environment normalization:
|
44
|
+
/.bundle/
|
45
|
+
/vendor/bundle
|
46
|
+
/lib/bundler/man/
|
47
|
+
|
48
|
+
# for a library or gem, you might want to ignore these files since the code is
|
49
|
+
# intended to run in multiple environments; otherwise, check them in:
|
50
|
+
# Gemfile.lock
|
51
|
+
# .ruby-version
|
52
|
+
# .ruby-gemset
|
53
|
+
|
54
|
+
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
|
55
|
+
.rvmrc
|
56
|
+
.tags*
|
57
|
+
|
58
|
+
tmp/*.json
|
59
|
+
# End of https://www.gitignore.io/api/ruby
|
60
|
+
|
61
|
+
.ruby-version
|
62
|
+
Gemfile.lock
|
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at neverlandxy.naix@gmail.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
if ENV['USE_OFFICIAL_GEM_SOURCE']
|
4
|
+
source 'https://rubygems.org'
|
5
|
+
else
|
6
|
+
source 'https://gems.ruby-china.org'
|
7
|
+
end
|
8
|
+
|
9
|
+
git_source(:github) do |repo_name|
|
10
|
+
repo_name = "#{repo_name}/#{repo_name}" unless repo_name.include?("/")
|
11
|
+
"https://github.com/#{repo_name}.git"
|
12
|
+
end
|
13
|
+
|
14
|
+
# Specify your gem's dependencies in neb.gemspec
|
15
|
+
gemspec
|
data/Guardfile
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
guard :minitest do
|
5
|
+
# with Minitest::Unit
|
6
|
+
watch(%r{^test/(.*)\/?test_(.*)\.rb$})
|
7
|
+
watch(%r{^lib/(.*/)?([^/]+)\.rb$}) { |m| "test/#{m[1]}test_#{m[2]}.rb" }
|
8
|
+
watch(%r{^test/test_helper\.rb$}) { 'test' }
|
9
|
+
end
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 Spirit
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,123 @@
|
|
1
|
+
# Nebulas Ruby API
|
2
|
+
|
3
|
+
This is the Nebulas compatible Ruby API. Users can use it in Ruby and Rails. This ruby library also support API for our Repl console. Users can sign/send transactions and deploy/call smart contract with it. [neb.rb](https://github.com/NaixSpirit/neb.rb)
|
4
|
+
|
5
|
+
## Install Secp256k1
|
6
|
+
|
7
|
+
https://github.com/cryptape/ruby-bitcoin-secp256k1
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Add this line to your application's Gemfile:
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
gem 'neb'
|
15
|
+
```
|
16
|
+
|
17
|
+
And then execute:
|
18
|
+
|
19
|
+
$ bundle
|
20
|
+
|
21
|
+
Or install it yourself as:
|
22
|
+
|
23
|
+
$ gem install neb
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
|
28
|
+
### API Example
|
29
|
+
```ruby
|
30
|
+
Neb.configure(host: 'https://testnet.nebulas.io')
|
31
|
+
client = Neb::Client.new
|
32
|
+
|
33
|
+
resp = client.api.get_neb_state
|
34
|
+
resp.code # => 200
|
35
|
+
resp.success? # => true
|
36
|
+
resp.result # => {:chain_id=>100, :tail=>"xxxx", :lib=>"xxxx", :height=>"1085", :protocol_version=>"/neb/1.0.0", :synchronized=>false, :version=>"1.0.1"}
|
37
|
+
|
38
|
+
resp = client.admin.accounts
|
39
|
+
resp.code # => 200
|
40
|
+
resp.success? # => true
|
41
|
+
resp.result # => {:addresses=>["n1FF1nz6tarkDVwWQkMnnwFPuPKUaQTdptE", "n1FNj5aZhKFeFJ8cQ26Lvsr84NDvNSVRu67"]}
|
42
|
+
```
|
43
|
+
|
44
|
+
### Account Example
|
45
|
+
|
46
|
+
```ruby
|
47
|
+
# Create a new account with random private_key
|
48
|
+
account = Neb::Account.create
|
49
|
+
account = Neb::Account.create(password: YOUR_PASSOWRD)
|
50
|
+
|
51
|
+
account.private_key # => "b5e53a1582a48d243ebd478a7722d1bfea4805ff7c1da4cc7084043e8263c5a8"
|
52
|
+
account.public_key # => "35a80ac8a27e2bf072ae84b2cb019e3af0c06547ad939fab1c6d12f713d26ae178d1fd6677aef3e6e94bc7cc1a39f4ca80fc2409a5ef59f97ee55dbd6efc7714"
|
53
|
+
account.address # => "n1NfnKqgXBixjiDkJZDSVwqf7ps5roGwFyJ"
|
54
|
+
account.password = "123456" # or account.set_password("123456")
|
55
|
+
account.to_key # => {:version=>4, :id=>"becde267-902e-4f23-ac01-53a4ba6edac7", :address=>"n1VYLxkZoehWEWPHxi351HgZ2R8Hfn2DGpa" ....}
|
56
|
+
account.to_key_file(file_path: "/tmp/mykey.json")
|
57
|
+
|
58
|
+
# Create a new account from exist private_key
|
59
|
+
account = Neb::Account.new(private_key: YOUR_PRIVATE_KEY)
|
60
|
+
account = Neb::Account.new(private_key: YOUR_PRIVATE_KEY, password: YOUR_PASSOWRD)
|
61
|
+
|
62
|
+
# Restore account from key
|
63
|
+
account = Neb::Account.from_key(key: YOUR_KEY, password: YOUR_PASSOWRD)
|
64
|
+
|
65
|
+
# Restore account from a key file
|
66
|
+
account = Neb::Account.from_key_file(key_file: YOUR_KEY_FILE, password: YOUR_PASSOWRD)
|
67
|
+
```
|
68
|
+
|
69
|
+
### Transaction Example
|
70
|
+
|
71
|
+
```ruby
|
72
|
+
Neb.configure(host: 'https://testnet.nebulas.io')
|
73
|
+
client = Neb::Client.new
|
74
|
+
|
75
|
+
account = Neb::Account.new(private_key: YOUR_KEY)
|
76
|
+
tx = Neb::Transaction.new(
|
77
|
+
chain_id: 1,
|
78
|
+
from_account: account,
|
79
|
+
to_address: 'n1SAeQRVn33bamxN4ehWUT7JGdxipwn8b17',
|
80
|
+
value: 10,
|
81
|
+
nonce: 1,
|
82
|
+
gas_price: 1000000,
|
83
|
+
gas_limit: 2000000
|
84
|
+
)
|
85
|
+
|
86
|
+
tx.sign_hash
|
87
|
+
|
88
|
+
resp = client.api.send_raw_transaction(data: tx.to_proto_str)
|
89
|
+
resp.code # => 200
|
90
|
+
resp.success? # => true
|
91
|
+
resp.result # => {:txhash=>"8524384dce7e122bfd007e0ba465e597d821e22db6d563b87dfc55d703fb008c", :contract_address=>""}
|
92
|
+
|
93
|
+
resp = client.api.get_transaction_receipt(hash: "8524384dce7e122bfd007e0ba465e597d821e22db6d563b87dfc55d703fb008c")
|
94
|
+
resp.result[:status] # => 1
|
95
|
+
|
96
|
+
client.api.get_account_state(address: 'n1SAeQRVn33bamxN4ehWUT7JGdxipwn8b17').result # => {:balance=>"10", :nonce=>"0", :type=>87}
|
97
|
+
```
|
98
|
+
|
99
|
+
# Documentation
|
100
|
+
|
101
|
+
## API list
|
102
|
+
|
103
|
+
[Ruby Client API list](https://github.com/NaixSpirit/neb.rb/blob/master/lib/neb/client/api.rb)
|
104
|
+
|
105
|
+
[Ruby Admin API list](https://github.com/NaixSpirit/neb.rb/blob/master/lib/neb/client/admin.rb)
|
106
|
+
|
107
|
+
## Development
|
108
|
+
|
109
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
110
|
+
|
111
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
112
|
+
|
113
|
+
## Contributing
|
114
|
+
|
115
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/NaixSpirit/neb.rb. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
116
|
+
|
117
|
+
## License
|
118
|
+
|
119
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
120
|
+
|
121
|
+
## Code of Conduct
|
122
|
+
|
123
|
+
Everyone interacting in the Neb project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/NaixSpirit/neb.rb/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "neb"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
require "pry"
|
11
|
+
Pry.start
|
12
|
+
|
13
|
+
# require "irb"
|
14
|
+
# IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/lib/neb/account.rb
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
module Neb
|
5
|
+
class Account
|
6
|
+
attr_reader :private_key_obj, :public_key_obj, :address_obj
|
7
|
+
attr_accessor :password
|
8
|
+
|
9
|
+
alias_method :set_password, :password=
|
10
|
+
|
11
|
+
def initialize(private_key:, password: nil)
|
12
|
+
@private_key_obj = PrivateKey.new(private_key)
|
13
|
+
@public_key_obj = @private_key_obj.to_pubkey_obj
|
14
|
+
@address_obj = @public_key_obj.to_address_obj
|
15
|
+
@password = password
|
16
|
+
end
|
17
|
+
|
18
|
+
class << self
|
19
|
+
def create(password: nil)
|
20
|
+
new(private_key: PrivateKey.random.to_s, password: password)
|
21
|
+
end
|
22
|
+
|
23
|
+
def from_key(key:, password:)
|
24
|
+
new(private_key: Key.decrypt(key, password), password: password)
|
25
|
+
end
|
26
|
+
|
27
|
+
def from_key_file(key_file:, password:)
|
28
|
+
from_key(key: File.read(key_file), password: password)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def private_key
|
33
|
+
@private_key_obj.to_s
|
34
|
+
end
|
35
|
+
|
36
|
+
def public_key
|
37
|
+
@public_key_obj.to_s
|
38
|
+
end
|
39
|
+
|
40
|
+
def address
|
41
|
+
@address_obj.to_s
|
42
|
+
end
|
43
|
+
|
44
|
+
def to_key
|
45
|
+
raise ArgumentError.new("must set_password first") if password.blank?
|
46
|
+
Key.encrypt(address, private_key, password)
|
47
|
+
end
|
48
|
+
|
49
|
+
def to_key_file(file_path: nil)
|
50
|
+
file_path = Neb.root.join('tmp', "#{address}.json") if file_path.blank?
|
51
|
+
File.open(file_path, 'w+') { |f| f << Utils.to_json(to_key) }
|
52
|
+
file_path.to_s
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
data/lib/neb/address.rb
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
module Neb
|
5
|
+
class Address
|
6
|
+
attr_reader :raw
|
7
|
+
|
8
|
+
def initialize(raw)
|
9
|
+
@raw = raw
|
10
|
+
end
|
11
|
+
|
12
|
+
def value
|
13
|
+
@value ||= decode
|
14
|
+
end
|
15
|
+
|
16
|
+
def to_s
|
17
|
+
encode(:hex_extended)
|
18
|
+
end
|
19
|
+
|
20
|
+
def checksum
|
21
|
+
Utils.keccak256(encode(:bin))[0, 4]
|
22
|
+
end
|
23
|
+
|
24
|
+
def encode(fmt)
|
25
|
+
case fmt
|
26
|
+
when :decimal
|
27
|
+
raw
|
28
|
+
when :bin
|
29
|
+
BaseConvert.encode(value, 256, 22)
|
30
|
+
when :bin_extended
|
31
|
+
"#{BaseConvert.encode(value, 256, 22)}#{checksum}"
|
32
|
+
when :hex
|
33
|
+
Utils.binary_to_base58(BaseConvert.encode(value, 256, 22))
|
34
|
+
when :hex_extended
|
35
|
+
Utils.binary_to_base58("#{BaseConvert.encode(value, 256, 22)}#{checksum}")
|
36
|
+
else
|
37
|
+
raise ArgumentError, "invalid format: #{fmt}"
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def decode(fmt = nil)
|
42
|
+
fmt ||= format
|
43
|
+
|
44
|
+
case fmt
|
45
|
+
when :decimal
|
46
|
+
raw
|
47
|
+
when :bin
|
48
|
+
BaseConvert.decode(raw, 256)
|
49
|
+
when :bin_extended
|
50
|
+
BaseConvert.decode(raw[0, 22], 256)
|
51
|
+
when :hex
|
52
|
+
BaseConvert.decode(Utils.base58_to_binary(raw), 256)
|
53
|
+
when :hex_extended
|
54
|
+
BaseConvert.decode(Utils.base58_to_binary(raw)[0, 22], 256)
|
55
|
+
else
|
56
|
+
raise FormatError, "Invalid format!"
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
def format
|
61
|
+
return :decimal if raw.is_a?(Numeric)
|
62
|
+
return :bin if raw.size == 22
|
63
|
+
return :bin_extended if raw.size == 26
|
64
|
+
return :hex if raw.size == 30
|
65
|
+
return :hex_extended if raw.size == 35
|
66
|
+
end
|
67
|
+
|
68
|
+
class << self
|
69
|
+
|
70
|
+
def is_validate?(address)
|
71
|
+
addr = Utils.base58_to_binary(address)
|
72
|
+
return false if addr.length != Constant::ADDRESS_LENGTH
|
73
|
+
|
74
|
+
preifx = BaseConvert.decode(addr[0], 256)
|
75
|
+
type = BaseConvert.decode(addr[1], 256)
|
76
|
+
|
77
|
+
return false if preifx != Constant::ADDRESS_PREFIX
|
78
|
+
return false if type != Constant::NORMAL_TYPE && type != Constant::CONTRACT_TYPE
|
79
|
+
|
80
|
+
content = addr[0..21]
|
81
|
+
checksum = addr[-4..-1]
|
82
|
+
|
83
|
+
ActiveSupport::SecurityUtils.secure_compare(
|
84
|
+
Utils.keccak256(content)[0, 4], checksum
|
85
|
+
)
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
end
|
90
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
module Neb
|
5
|
+
#
|
6
|
+
# @see https://github.com/cryptape/ruby-ethereum/blob/master/lib/ethereum/base_convert.rb
|
7
|
+
#
|
8
|
+
module BaseConvert
|
9
|
+
|
10
|
+
BaseSymbols = {
|
11
|
+
2 => '01'.freeze,
|
12
|
+
10 => '0123456789'.freeze,
|
13
|
+
16 => '0123456789abcdef'.freeze,
|
14
|
+
32 => 'abcdefghijklmnopqrstuvwxyz234567'.freeze,
|
15
|
+
58 => '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'.freeze,
|
16
|
+
256 => 256.times.map {|i| i.chr }.join.freeze
|
17
|
+
}.freeze
|
18
|
+
|
19
|
+
extend self
|
20
|
+
|
21
|
+
def symbols(base)
|
22
|
+
BaseSymbols[base] or raise ArgumentError, "invalid base!"
|
23
|
+
end
|
24
|
+
|
25
|
+
def convert(s, from, to, minlen = 0)
|
26
|
+
return Utils.lpad(s, symbols(from)[0], minlen) if from == to
|
27
|
+
encode(decode(s, from), to, minlen)
|
28
|
+
end
|
29
|
+
|
30
|
+
def encode(v, base, minlen = 0)
|
31
|
+
syms = symbols(base)
|
32
|
+
|
33
|
+
result = ''
|
34
|
+
while v > 0
|
35
|
+
result = syms[v % base] + result
|
36
|
+
v /= base
|
37
|
+
end
|
38
|
+
|
39
|
+
Utils.lpad(result, syms[0], minlen)
|
40
|
+
end
|
41
|
+
|
42
|
+
def decode(s, base)
|
43
|
+
syms = symbols(base)
|
44
|
+
s = s.downcase if base == 16
|
45
|
+
|
46
|
+
result = 0
|
47
|
+
while s.size > 0
|
48
|
+
result *= base
|
49
|
+
result += syms.index(s[0])
|
50
|
+
s = s[1..-1]
|
51
|
+
end
|
52
|
+
|
53
|
+
result
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,95 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require_relative "./request"
|
5
|
+
require_relative "./response"
|
6
|
+
|
7
|
+
module Neb
|
8
|
+
class Client
|
9
|
+
class Admin
|
10
|
+
attr_reader :host, :api_version, :endpoint
|
11
|
+
|
12
|
+
def initialize
|
13
|
+
@host = CONFIG[:host]
|
14
|
+
@api_version = CONFIG[:api_version]
|
15
|
+
@endpoint = CONFIG[:admin_endpoint]
|
16
|
+
end
|
17
|
+
|
18
|
+
def node_info
|
19
|
+
send_request(:get, "/nodeinfo")
|
20
|
+
end
|
21
|
+
|
22
|
+
def accounts
|
23
|
+
send_request(:get, "/accounts")
|
24
|
+
end
|
25
|
+
|
26
|
+
def new_account(passphrase:)
|
27
|
+
send_request(:post, "/account/new", passphrase: passphrase)
|
28
|
+
end
|
29
|
+
|
30
|
+
def unlock_account(address:, passphrase:, duration: '30000000000')
|
31
|
+
params = {
|
32
|
+
address: address,
|
33
|
+
passphrase: passphrase,
|
34
|
+
duration: duration
|
35
|
+
}
|
36
|
+
send_request(:post, "/account/unlock", params)
|
37
|
+
end
|
38
|
+
|
39
|
+
def lock_account(address:)
|
40
|
+
send_request(:post, "/account/lock", address: address)
|
41
|
+
end
|
42
|
+
|
43
|
+
def send_transaction(from:, to:, value:, nonce:, gas_price: 1_000_000, gas_limit: 20_000)
|
44
|
+
params = {
|
45
|
+
from: from,
|
46
|
+
to: to,
|
47
|
+
value: value.to_i,
|
48
|
+
nonce: nonce.to_i,
|
49
|
+
gas_price: gas_price.to_i,
|
50
|
+
gas_limit: gas_limit.to_i
|
51
|
+
}
|
52
|
+
send_request(:post, "/transaction", params)
|
53
|
+
end
|
54
|
+
|
55
|
+
def sign_hash(address:, hash:, alg: 1)
|
56
|
+
send_request(:post, "/sign/hash", address: address, hash: hash, alg: alg)
|
57
|
+
end
|
58
|
+
|
59
|
+
def sign_transaction_with_passphrase(from:, to:, value:, nonce:, gas_price: 1_000_000, gas_limit: 20_000,
|
60
|
+
type:, contract:, binary:, passphrase:)
|
61
|
+
params = {
|
62
|
+
transaction: {
|
63
|
+
from: from,
|
64
|
+
to: to,
|
65
|
+
value: value.to_i,
|
66
|
+
nonce: nonce.to_i,
|
67
|
+
gas_price: gas_price.to_i,
|
68
|
+
gas_limit: gas_limit.to_i,
|
69
|
+
type: type,
|
70
|
+
contract: contract,
|
71
|
+
binary: binary
|
72
|
+
},
|
73
|
+
passphrase: passphrase
|
74
|
+
}
|
75
|
+
|
76
|
+
send_request(:post, "/transactionWithPassphrase", params)
|
77
|
+
end
|
78
|
+
|
79
|
+
def start_pprof(listen:)
|
80
|
+
send_request(:post, "/pprof", listen: listen)
|
81
|
+
end
|
82
|
+
|
83
|
+
def get_config
|
84
|
+
send_request(:get, "/getConfig")
|
85
|
+
end
|
86
|
+
|
87
|
+
private
|
88
|
+
|
89
|
+
def send_request(action, url, payload = {})
|
90
|
+
request_url = host + api_version + endpoint + url
|
91
|
+
Request.new(action, request_url, payload).execute
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|