Class: Keoken::Token

Inherits:
Object
  • Object
show all
Defined in:
lib/keoken/token.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Token

Creates a new token object.

Parameters:

  • options (Hash) (defaults to: {})

    options parameters to create the token.

Options Hash (options):

  • :name (String)

    The name of token to create.

  • :id (Number)

    The id of token to obtain an amount to send to another address.


11
12
13
14
# File 'lib/keoken/token.rb', line 11

def initialize(options = {})
  @name = options[:name]
  @id = options[:id]
end

Instance Attribute Details

#hexObject

Returns the value of attribute hex


3
4
5
# File 'lib/keoken/token.rb', line 3

def hex
  @hex
end

#idObject

Returns the value of attribute id


3
4
5
# File 'lib/keoken/token.rb', line 3

def id
  @id
end

#nameObject

Returns the value of attribute name


3
4
5
# File 'lib/keoken/token.rb', line 3

def name
  @name
end

Instance Method Details

#create(amount) ⇒ Keoken::Token

Generate the script to create a token.

Parameters:

  • amount (Number)

    The token amount limit.

Returns:

  • (Keoken::Token)

    An object with the data needed to crate the token.

Raises:


22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/keoken/token.rb', line 22

def create(amount)
  raise Keoken::NameNotFound unless @name
  data_script =
    [Keoken::VERSION_NODE,
     Keoken::TYPE_CREATE_ASSET,
     name_to_hex(@name),
     Keoken::PREFIX_BYTE_AMOUNT[0..(Keoken::AMOUNT_SIZE - amount.to_s.length - 1)] + amount.to_s].flatten.join
  data_length = data_script.htb.bytesize.to_s(16)

  @hex = [Bitcoin::Script::OP_RETURN.to_s(16),
          Keoken::PREFIX_SIZE,
          Keoken::PREFIX,
          data_length].join + data_script
  self
end

#send_amount(amount) ⇒ Keoken::Token

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

Parameters:

  • amount (Number)

    The amount to send.

Returns:

  • (Keoken::Token)

    An object with the data needed to send the amount.

Raises:


44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/keoken/token.rb', line 44

def send_amount(amount)
  raise Keoken::IdNotFound unless @id
  data_script =
    [Keoken::VERSION_NODE,
     Keoken::TYPE_SEND_TOKEN,
     Keoken::PREFIX_BYTE_ASSET_ID[0..(Keoken::ASSET_ID_SIZE - @id.to_s.length - 1)] + @id.to_s,
     Keoken::PREFIX_BYTE_AMOUNT[0..(Keoken::AMOUNT_SIZE - amount.to_s.length - 1)] + amount.to_s].flatten.join

  data_length = data_script.htb.bytesize.to_s(16)

  @hex = [Bitcoin::Script::OP_RETURN.to_s(16),
          Keoken::PREFIX_SIZE,
          Keoken::PREFIX,
          data_length].join + data_script
  self
end