Giter VIP home page Giter VIP logo

ckb-ruby-sdk's Introduction

ckb-ruby-sdk

Ruby SDK for CKB

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

Prerequisites

Require Ruby 2.4 and above.

Ubuntu

sudo apt install libsodium-dev

This SDK depends on the bitcoin-secp256k1 gem. You need to install libsecp256k1 with --enable-module-recovery (on which bitcoin-secp256k1 depends) manually. 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-ruby-sdk', github: 'quake/ckb-ruby-sdk'

And then execute:

$ bundle install

If you just want to use it in a console:

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

Usage

RPC interface returns parsed JSON object

rpc = CKB::Config.instance.rpc

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

# using RPC `get_header_by_number`, numeric args will be converted to hex string automatically
rpc.get_header_by_number 42

# batch requests
rpc.batch(["get_transaction", "0xb307facd11c63639a2ead93e6b77716124d3c2d351037474a010dedcaaf41613"], ["get_transaction", "0x97802da4f798d11240aa38a5bc2a629295bf41e32b232feff2c6d6732cb24610"])

Send capacity

# create wallet object
wallet = CKB::Wallets::Simple.new("ckt1qyqvsv5240xeh85wvnau2eky8pwrhh4jr8ts8vyj37")
# build transaction
tx_builder = wallet.build("ckt1qyqywrwdchjyqeysjegpzw38fvandtktdhrs0zaxl4", 100_0000_0000)
# sign
tx = wallet.sign(tx_builder, "0xd00c06bfd800d27397002dca6fb0993d5ba6399b4238b2f29ee9deb97593d2bc".from_hex)
# send transaction
CKB::Config.instance.rpc.send_transaction(tx.as_json)

Send capacity to multiple addresses

tx_builder = wallet.advance_build(
    "ckt1qyqr8ljpvy6y7t0cp2m0prv2whvm05whjzeqaydfze" => {capacity: 1234_0000_0000},
    "ckt1qyq0myesdwxwntsra2m75xtp8k7q8nphjmksxyzz0c" => {capacity: 4321_0000_0000},
)

Collect inputs with multiple addresses

wallet = CKB::Wallets::Simple.new(["ckt1qyqr8ljpvy6y7t0cp2m0prv2whvm05whjzeqaydfze", "ckt1qyq0myesdwxwntsra2m75xtp8k7q8nphjmksxyzz0c"])
tx_builder = wallet.build("ckt1qyqkqqppzt0svxzyedfe7jt0dhxhd9rvt2dskqrjem", 5000_0000_0000)
tx = wallet.sign(tx_builder, ["0x92116fee8735bd5d95f5f0e773a887f1a7d0b3d0c6007c8a66f844acffb9adc0".from_hex, "0x252948dddb55a54c93bf05c468acbaa6683c763c39132e71fd8ecb9fb6f88f5d".from_hex])

Deploy a contract binary or send capacity with data

data = File.read("/your-path-to/binary").unpack("C*")
tx_builder = wallet.build("ckt1qgqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqparrr6", 1800_0000_0000, {data: data})

Deploy a contract binary with type id

data = File.read("/your-path-to/binary").unpack("C*")
type_script = CKB::Types::Script.new(
    code_hash: CKB::Types::Script::TYPE_ID_HASH,
    hash_type: CKB::Types::Script::HASH_TYPE_TYPE
)
tx_builder = wallet.build("ckt1qgqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqparrr6", 1800_0000_0000, {data: data, type: type_script})

License

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

Changelog

See CHANGELOG for more information.

ckb-ruby-sdk's People

Contributors

dependabot[bot] avatar quake avatar shaojunda avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

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.