lib/ckb/wallet.rb
# Create a user defined token with fixed upper amount, subsequent invocations
# on this method will create different tokens.
Create a user defined token with fixed upper amount, subsequent invocations
on this method will create different tokens.
Create a user defined token with fixed upper amount subsequent invocations on this method will create different tokens .
def create_fixed_amount_token(capacity, tokens, rate, lock_hash: nil)
      lock_hash ||= verify_type_hash
      i = gather_inputs(capacity, MIN_CELL_CAPACITY)
      input_capacities = i.capacities
      ms = SHA3::Digest::SHA256.new
      i.inputs.each do |input|
        ms.update(Ckb::Utils.hex_to_bin(input[:previous_output][:hash]))
        ms.update(input[:previous_output][:index].to_s)
        ms.update(Ckb::Utils.hex_to_bin(Ckb::Utils.json_script_to_type_hash(input[:unlock])))
      end
      info = FixedAmountTokenInfo.new(
        api,
        Ckb::Utils.bin_to_hex(ms.digest),
        lock_hash,
        Ckb::Utils.bin_to_hex(pubkey_bin),
        rate)
      data = [tokens].pack("Q<")
      outputs = [
        {
          capacity: capacity,
          data: data,
          lock: info.genesis_unlock_type_hash,
          type: info.genesis_contract_script_json_object
        }
      ]
      if input_capacities > capacity
        outputs << {
          capacity: input_capacities - capacity,
          data: "",
          lock: self.address
        }
      end
      s = SHA3::Digest::SHA256.new
      contract_type_hash_bin = Ckb::Utils.hex_to_bin(Ckb::Utils.json_script_to_type_hash(outputs[0][:type]))
      s.update(contract_type_hash_bin)
      i.inputs.each do |input|
        s.update(Ckb::Utils.hex_to_bin(input[:previous_output][:hash]))
        s.update(input[:previous_output][:index].to_s)
        s.update(Ckb::Utils.hex_to_bin(Ckb::Utils.json_script_to_type_hash(input[:unlock])))
      end
      s.update(outputs[0][:capacity].to_s)
      s.update(Ckb::Utils.hex_to_bin(outputs[0][:lock]))
      s.update(contract_type_hash_bin)
      s.update(data)
      if outputs[1]
        s.update(outputs[1][:capacity].to_s)
        s.update(Ckb::Utils.hex_to_bin(outputs[1][:lock]))
      end
      key = Secp256k1::PrivateKey.new(privkey: privkey)
      signature = key.ecdsa_serialize(key.ecdsa_sign(s.digest, raw: true))
      signature_hex = Ckb::Utils.bin_to_hex(signature)
      outputs[0][:type][:args] = [signature_hex]
      tx = {
        version: 0,
        deps: [api.mruby_out_point],
        inputs: Ckb::Utils.sign_sighash_all_inputs(i.inputs, outputs, privkey),
        outputs: outputs
      }
      hash = api.send_transaction(tx)
      OpenStruct.new(tx_hash: hash, token_info: info)
    end
def create_fixed_amount_token ( capacity , tokens , rate , lock_hash : nil ) lock_hash ||= verify_type_hash i = gather_inputs ( capacity , MIN_CELL_CAPACITY ) input_capacities = i . capacities ms = SHA3 :: Digest :: SHA256 . new i . inputs . each do | input | ms . update ( Ckb :: Utils . hex_to_bin ( input [ :previous_output ] [ :hash ] ) ) ms . update ( input [ :previous_output ] [ :index ] . to_s ) ms . update ( Ckb :: Utils . hex_to_bin ( Ckb :: Utils . json_script_to_type_hash ( input [ :unlock ] ) ) ) end info = FixedAmountTokenInfo . new ( api , Ckb :: Utils . bin_to_hex ( ms . digest ) , lock_hash , Ckb :: Utils . bin_to_hex ( pubkey_bin ) , rate ) data = [ tokens ] . pack ( "Q<" ) outputs = [ { capacity : capacity , data : data , lock : info . genesis_unlock_type_hash , type : info . genesis_contract_script_json_object } ] if input_capacities > capacity outputs << { capacity : input_capacities - capacity , data : "" , lock : self . address } end s = SHA3 :: Digest :: SHA256 . new contract_type_hash_bin = Ckb :: Utils . hex_to_bin ( Ckb :: Utils . json_script_to_type_hash ( outputs [ 0 ] [ :type ] ) ) s . update ( contract_type_hash_bin ) i . inputs . each do | input | s . update ( Ckb :: Utils . hex_to_bin ( input [ :previous_output ] [ :hash ] ) ) s . update ( input [ :previous_output ] [ :index ] . to_s ) s . update ( Ckb :: Utils . hex_to_bin ( Ckb :: Utils . json_script_to_type_hash ( input [ :unlock ] ) ) ) end s . update ( outputs [ 0 ] [ :capacity ] . to_s ) s . update ( Ckb :: Utils . hex_to_bin ( outputs [ 0 ] [ :lock ] ) ) s . update ( contract_type_hash_bin ) s . update ( data ) if outputs [ 1 ] s . update ( outputs [ 1 ] [ :capacity ] . to_s ) s . update ( Ckb :: Utils . hex_to_bin ( outputs [ 1 ] [ :lock ] ) ) end key = Secp256k1 :: PrivateKey . new ( privkey : privkey ) signature = key . ecdsa_serialize ( key . ecdsa_sign ( s . digest , raw : true ) ) signature_hex = Ckb :: Utils . bin_to_hex ( signature ) outputs [ 0 ] [ :type ] [ :args ] = [ signature_hex ] tx = { version : 0 , deps : [ api . mruby_out_point ] , inputs : Ckb :: Utils . sign_sighash_all_inputs ( i . inputs , outputs , privkey ) , outputs : outputs } hash = api . send_transaction ( tx ) OpenStruct . new ( tx_hash : hash , token_info : info ) end
Create a user defined token with fixed upper amount, subsequent invocations
on this method will create different tokens.
Create a user defined token with fixed upper amount subsequent invocations on this method will create different tokens .
capacity
tokens
rate
lock_hash