class Keoken::Token

Attributes

amount[RW]
data_script[RW]
id[RW]

Public Class Methods

new(options = {}) click to toggle source

Creates a new token object.

@param options [Hash] options parameters to create the token. @option options [String] :name The name of token to create. @option options [Number] :id The id of token to obtain an amount to send to another address. @option options [String] :script An hexadecimal script intended to be parsed.

Calls superclass method Keoken::Parser::new
# File lib/keoken/token.rb, line 12
def initialize(options = {})
  @name   = options[:name]
  @id     = options[:id]
  return unless options[:script]
  super(options[:script])
  parse_script
end

Public Instance Methods

create(amount) click to toggle source

Generate the script to create a token.

@param amount [Number] The token amount limit.

@return [Keoken::Token] An object with the data needed to crate the token.

# File lib/keoken/token.rb, line 26
def create(amount)
  raise Keoken::Error::NameNotFound unless @name
  @data_script =
    [
      Keoken::VERSION_NODE,
      Keoken::TYPE_CREATE_ASSET,
      name_to_hex(@name),
      [amount.to_i].pack('q>').unpack('H*')
    ].flatten.join
  self
end
hex() click to toggle source

Hexadecimal value of script.

@return [String] Hexadecimal value of script token.

# File lib/keoken/token.rb, line 59
def hex
  [
    Bitcoin::Script::OP_RETURN.to_s(16),
    Keoken::PREFIX_SIZE,
    Keoken::PREFIX,
    data_length
  ].join + @data_script
end
send_amount(amount) click to toggle source

Generate the script to send an amount from one address to another.

@param amount [Number] The amount to send.

@return [Keoken::Token] An object with the data needed to send the amount.

# File lib/keoken/token.rb, line 44
def send_amount(amount)
  raise Keoken::Error::IdNotFound unless @id
  @data_script =
    [
      Keoken::VERSION_NODE,
      Keoken::TYPE_SEND_TOKEN,
      [@id.to_i].pack('L>').unpack('H*'),
      [amount.to_i].pack('q>').unpack('H*')
    ].flatten.join
  self
end
to_hash() click to toggle source

Deserialization of object

return [Hash] Deserialization of token object.

# File lib/keoken/token.rb, line 83
def to_hash
  {
    id: @id,
    name: @name,
    amount: @amount,
    transaction_type: @transaction_type
  }
end
to_json() click to toggle source

JSON serialization of object

return [String] JSON serialization of token object.

# File lib/keoken/token.rb, line 71
def to_json
  {
    id: @id,
    name: @name,
    amount: @amount,
    transaction_type: @transaction_type
  }.to_json
end

Private Instance Methods

data_length() click to toggle source
# File lib/keoken/token.rb, line 94
def data_length
  @data_script.htb.bytesize.to_s(16)
end
name_to_hex(name) click to toggle source
# File lib/keoken/token.rb, line 102
def name_to_hex(name)
  asset_bytes = name.bytes.map { |n| n.to_s(16) }
  asset_bytes + ['00']
end
parse_script() click to toggle source
# File lib/keoken/token.rb, line 107
def parse_script
  prefix
  @transaction_type = set_transaction_type
  if @transaction_type == :create
    @name = name_or_id
  else
    @id = name_or_id
  end
  @amount = set_amount
  self
end
prefix_length(amount) click to toggle source
# File lib/keoken/token.rb, line 98
def prefix_length(amount)
  Keoken::AMOUNT_SIZE - (amount.to_s(16).length + 1)
end