Project

neb

0.0
Repository is archived
No commit activity in last 3 years
No release in over 3 years
the Nebulas compatible Ruby API
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.16
~> 2.14
~> 5.0
~> 0.11
~> 10.0

Runtime

~> 0.2
~> 1.9
~> 0.7.3
~> 3.0
~> 1.0
 Project Readme

Nebulas Ruby API

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

Install Secp256k1

https://github.com/cryptape/ruby-bitcoin-secp256k1

Installation

Add this line to your application's Gemfile:

gem 'neb', '0.1.3'

And then execute:

$ bundle

Or install it yourself as:

$ gem install neb

Usage

API Example

Neb.configure(host: 'https://testnet.nebulas.io')
client = Neb::Client.new

resp = client.api.get_neb_state
resp.code     # => 200
resp.success? # => true
resp.result   # =>  {:chain_id=>100, :tail=>"xxxx", :lib=>"xxxx", :height=>"1085", :protocol_version=>"/neb/1.0.0", :synchronized=>false, :version=>"1.0.1"}

client.api.subscribe(
  topics: ["chain.pendingTransaction"],
  on_download_progress: ->(c) { puts c }
)

resp = client.admin.accounts
resp.code     # => 200
resp.success? # => true
resp.result   # => {:addresses=>["n1FF1nz6tarkDVwWQkMnnwFPuPKUaQTdptE", "n1FNj5aZhKFeFJ8cQ26Lvsr84NDvNSVRu67"]}

Account Example

# Create a new account with random private_key
account = Neb::Account.create
account = Neb::Account.create(password: YOUR_PASSOWRD)

account.private_key         # => "b5e53a1582a48d243ebd478a7722d1bfea4805ff7c1da4cc7084043e8263c5a8"
account.public_key          # => "35a80ac8a27e2bf072ae84b2cb019e3af0c06547ad939fab1c6d12f713d26ae178d1fd6677aef3e6e94bc7cc1a39f4ca80fc2409a5ef59f97ee55dbd6efc7714"
account.address             # => "n1NfnKqgXBixjiDkJZDSVwqf7ps5roGwFyJ"
account.password = "123456" # or account.set_password("123456")
account.to_key              # => {:version=>4, :id=>"becde267-902e-4f23-ac01-53a4ba6edac7", :address=>"n1VYLxkZoehWEWPHxi351HgZ2R8Hfn2DGpa" ....}
account.to_key_file(file_path: "/tmp/mykey.json")

# Create a new account from exist private_key
account = Neb::Account.new(private_key: YOUR_PRIVATE_KEY)
account = Neb::Account.new(private_key: YOUR_PRIVATE_KEY, password: YOUR_PASSOWRD)

# Restore account from key
account = Neb::Account.from_key(key: YOUR_KEY, password: YOUR_PASSOWRD)

# Restore account from a key file
account = Neb::Account.from_key_file(key_file: YOUR_KEY_FILE, password: YOUR_PASSOWRD)

Transaction Example

Neb.configure(host: 'https://testnet.nebulas.io')
client = Neb::Client.new

account = Neb::Account.new(private_key: YOUR_KEY)
tx = Neb::Transaction.new(
  chain_id: 1,
  from_account: account,
  to_address: 'n1SAeQRVn33bamxN4ehWUT7JGdxipwn8b17',
  value: 10,
  nonce: 1,
  gas_price: 1000000,
  gas_limit: 2000000
)

tx.sign_hash

resp = client.api.send_raw_transaction(data: tx.to_proto_str)
resp.code     # => 200
resp.success? # => true
resp.result   # => {:txhash=>"8524384dce7e122bfd007e0ba465e597d821e22db6d563b87dfc55d703fb008c", :contract_address=>""}

resp = client.api.get_transaction_receipt(hash: "8524384dce7e122bfd007e0ba465e597d821e22db6d563b87dfc55d703fb008c")
resp.result[:status] # => 1

client.api.get_account_state(address: 'n1SAeQRVn33bamxN4ehWUT7JGdxipwn8b17').result # => {:balance=>"10", :nonce=>"0", :type=>87}

Documentation

API list

Ruby Client API list

Ruby Admin API list

Development

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.

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.

Test

  1. Install ruby-bitcoin-secp256k1
  2. Start go-nebulas at localhost, ./neb -c conf/default/config.conf, ./neb -c conf/example/miner.conf
  3. Install dependency, cd ~/neb.rb and bundle install
  4. Run test, rake test or rake test test/account_test.rb
  5. Also you can use guard to autotest

Contributing

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 code of conduct.

License

The gem is available as open source under the terms of the MIT License.

Code of Conduct

Everyone interacting in the Neb project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.