Giter Club home page Giter Club logo

ckb-sdk-ruby's Introduction

ckb-sdk-ruby

Build Status

Ruby SDK for CKB

The ckb-sdk-ruby is still under development and NOT production ready. You should get familiar with CKB transaction structure and RPC before using it.

WARNING

Module Indexer has been removed from ckb_v0.40.0: Please use ckb-indexer as an alternate solution.

The following RPCs hash been removed from ckb_v0.40.0:

  • get_live_cells_by_lock_hash
  • get_transactions_by_lock_hash
  • index_lock_hash
  • deindex_lock_hash
  • get_lock_hash_index_states
  • get_capacity_by_lock_hash

Since ckb_v0.36.0 SDK use ckb-indexer to collect cells, please see Usage for examples.

Prerequisites

Require Ruby 2.4 and above.

Ubuntu

sudo apt install libsodium-dev

This SDK depends on the rbsecp256k1 gem. You need to install libsecp256k1. Follow this to do so.

macOS

brew tap nervosnetwork/tap
brew install libsodium libsecp256k1

Installation

Add this line to your application's Gemfile:

gem 'ckb-sdk-ruby', github: 'nervosnetwork/ckb-sdk-ruby', require: 'ckb'

And then execute:

$ bundle install

If you just want to use it in a console:

git clone https://github.com/nervosnetwork/ckb-sdk-ruby.git
cd ckb-sdk-ruby
bundle install
bundle exec bin/console

Usage

RPC interface returns parsed JSON object

rpc = CKB::RPC.new

# using RPC `get_tip_header`, it will return a Hash
rpc.get_tip_header

API interface returns Types instead of Hash

api = CKB::API.new

# it will return a CKB::Types::BlockHeader
api.get_tip_header

Send capacity

# create api first
api = CKB::API.new

# create ckb-indexer api
indexer_api = CKB::Indexer::API.new

# create two wallet object
bob = CKB::Wallet.from_hex(api, "0xe79f3207ea4980b7fed79956d5934249ceac4751a4fae01a0f7c4a96884bc4e3", indexer_api: indexer_api)
alice = CKB::Wallet.from_hex(api, "0x76e853efa8245389e33f6fe49dcbd359eb56be2f6c3594e12521d2a806d32156", indexer_api: indexer_api)

# bob send 1000 bytes to alice
tx_hash = bob.send_capacity(alice.address, 1000 * 10**8, fee: 1000)

# loop up the transaction by tx_hash
api.get_transaction(tx_hash)

Provide wallet with a public key

api = CKB::API.new
indexer_api = CKB::Indexer::API.new

bob = CKB::Wallet.new(api, "0x024a501efd328e062c8675f2365970728c859c592beeefd6be8ead3d901330bc01", indexer_api: indexer_api)
alice = CKB::Wallet.new(api, "0x0257623ec521657a27204c5590384cd59d9267c06d75ab308070be692251b67c57", indexer_api: indexer_api)

bob_key = "0xe79f3207ea4980b7fed79956d5934249ceac4751a4fae01a0f7c4a96884bc4e3"

tx_hash = bob.send_capacity(alice.address, 1000 * 10**8, key: bob_key)

Run Tests

BOB_PRIVATE_KEY="0xe79f3207ea4980b7fed79956d5934249ceac4751a4fae01a0f7c4a96884bc4e3" rake spec

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake spec 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.

License

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

Changelog

See CHANGELOG for more information.

ckb-sdk-ruby's People

Contributors

ashchan avatar classicalliu avatar dependabot-preview[bot] avatar dependabot-support avatar dependabot[bot] avatar doitian avatar jjyr avatar quake avatar shaojunda avatar shiningray avatar u2 avatar xxuejie avatar zhangsoledad avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

ckb-sdk-ruby's Issues

Required Ruby Version?

Either leave in the documentation on required Ruby version or consider a ".ruby-version" file that works with most Ruby version managers. The current code seems to require at least 2.4 (?). I was on an older version and got hit with unpack1.

How to new a script with more than one arg?

The test code in spec seems like that the length of one arg must be 20B? How to send args have different lengths?

    it "should build correct hash when args more than one" do
      script = CKB::Types::Script.new(
        code_hash: code_hash,
        args: "0x3954acece65096bfa81258983ddb83915fc56bd83954acece65096bfa81258983ddb83915fc56bd8"
      )
      expect(
        script.compute_hash
      ).to eq api.compute_script_hash(script)
    end
  end

NoMethodError: undefined method `system_script_code_hash`

creat a wallet by RPC.new, but when execute puts walleat.block_assembler_config , a mistake has occurred:

[1] pry(main)> privkey = CKB::Key.random_private_key
=> "0x1c4ad8140a2d9cdbfb316bb33781329f6eff4ccdef8796732ea257beddaec198"
[2] pry(main)> rpc = CKB::RPC.new(host:  'http://172.17.0.4:8114')
=> #<RPC@http://172.17.0.4:8114>
[3] pry(main)> wallet = CKB::Wallet.from_hex(rpc, privkey)
=> #<CKB::Wallet:0x000055a17426c788
 @api=#<RPC@http://172.17.0.4:8114>,
 @key=
  #<CKB::Key:0x000055a17426df20
   @address=#<CKB::Address:0x000055a17426c8f0 @blake160="0xfb219548477cdcb97255057fa0cfcddcd585ae95", @prefix="ckt">,
   @privkey="0x1c4ad8140a2d9cdbfb316bb33781329f6eff4ccdef8796732ea257beddaec198",
   @pubkey="0x03e162604c61b1a574de691ebc675fcb3a3fec0cfee682da241ac054e39dcf1f9b">>
[4] pry(main)> wallet.address
=> "ckt1q9gry5zglvse2jz80nwtjuj4q4l6pn7dmn2ctt54fl4sfs"
[5] pry(main)> puts wallet.block_assembler_config
NoMethodError: undefined method `system_script_code_hash' for #<RPC@http://172.17.0.4:8114>
from /ckb-sdk-ruby/lib/ckb/wallet.rb:229:in `lock'

the server in http://172.17.0.4:8114 is created by docker image nervos/ckb:latest

Please add more field that returned from raw RPC for

Hi ~

I am wondering if you can add a feature that could get more field that returned from raw RPC for get_transaction ?

e.g. for transaction 0xed2049c21ffccfcd26281d60f8f77ff117adb9df9d3f8cbe5fe86e893c66d359, the raw RPC result shown as below, which contains the reason field say that Resolv failed Deat(OutPoint... :

{
    "jsonrpc":"2.0",
    "result":{
        "transaction":null,
        "tx_status":{
            "block_hash":null,

            // I need this message. 
            "reason":"{\"type\":\"Resolve\",\"description\":\"Resolve failed Dead(OutPoint(0x8dbf552ea6278ee765421e288acbf0489c8d55172954a10d8fec2257a76e713f00000000))\"}",
            "status":"rejected"
        }
    },
    "id":42
}

However, in current Ruby SDK, it only returned a wrapped class CKB::Types::TxStatus, which don't have the reason field. shown as below:

> api = CKB::API.new(host: ENV["CKB_NODE_URL"], timeout_config: { open_timeout: 1, read_timeout: 3, write_timeout: 1 })
=> #<API@http://localhost:8114>

> api.get_transaction '0xed2049c21ffccfcd26281d60f8f77ff117adb9df9d3f8cbe5fe86e893c66d359'
=> #<CKB::Types::TransactionWithStatus:0x000055dc78e945d0 
    @transaction=nil, 
    @tx_status=#<CKB::Types::TxStatus:0x000055dc78e94990 @block_hash=nil, @status="rejected">>

so, please add this field (reason) in the returned result. e.g.

> api.get_transaction '0xed2049c21ffccfcd26281d60f8f77ff117adb9df9d3f8cbe5fe86e893c66d359'
=> #<CKB::Types::TransactionWithStatus:0x000055dc78e945d0 
    @transaction=nil, 
    @tx_status=#<CKB::Types::TxStatus:0x000055dc78e94990 @block_hash=nil, @status="rejected" @reason="Resolve failed Dead(OutPoint(0x8dbf552ea6278ee765421e288acbf0489c8d55172954a10d8fec2257a76e713f00000000))">>

thanks a lot!

Consecutive Transactions

  • The same tx_hash is generated when consecutive transactions occur
  • When sending the same price to the same address continuously, the same tx_hash is generated.
  • An error occurs if it is executed several times consecutively.
  • It is not clear how much the cost of the attack will be because the fee value has not yet been set. However, it is expected that it will be helpful to prevent the attack and to develop the application by giving priority to the recovery of the continuous transaction.
    This may be a daemon issue or a policy.

[60] pry(main)> send_txHash = sender_wallet.send_capacity(recv_wallet.address, 60 * 10 ** 8 )
=> "0x9f7fff7ce189c5693682eef48a7cf163824e7690c8917d4ff9ebfe0ec4ee0ef0"
[61] pry(main)> send_txHash = sender_wallet.send_capacity(recv_wallet.address, 60 * 10 ** 8 )
=> "0x9f7fff7ce189c5693682eef48a7cf163824e7690c8917d4ff9ebfe0ec4ee0ef0"
[62] pry(main)> send_txHash = sender_wallet.send_capacity(recv_wallet.address, 60 * 10 ** 8 )
=> "0x9f7fff7ce189c5693682eef48a7cf163824e7690c8917d4ff9ebfe0ec4ee0ef0"
[63] pry(main)> send_txHash = sender_wallet.send_capacity(recv_wallet.address, 60 * 10 ** 8 )
=> "0x9f7fff7ce189c5693682eef48a7cf163824e7690c8917d4ff9ebfe0ec4ee0ef0"
[64] pry(main)> send_txHash = sender_wallet.send_capacity(recv_wallet.address, 60 * 10 ** 8 )
CKB::RPCError: jsonrpc error: {:code=>-3, :message=>"UnresolvableTransaction(Dead(OutPoint { cell: Some(CellOutPoint { tx_hash: 0xa4bd28d6342a1e91bc2948d4bb34f40146e04efebda5a8107ec703e0a143e72c, index: 0 }), block_hash: Some(1c2a781b20c643dd0815446c67064647329425e337fdd35484a1c8e1904ad73f) }))"}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.