Class: Keoken::Token

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

Instance Attribute Summary collapse

Attributes inherited from Parser

#amount, #data, #name, #transaction_type

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.
  • :script (String)
    An hexadecimal script intended to be parsed.


12
13
14
15
16
17
18
# 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

Instance Attribute Details

#data_scriptObject

Returns the value of attribute data_script


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

def data_script
  @data_script
end

#idObject

Returns the value of attribute id


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

def id
  @id
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:



26
27
28
29
30
31
32
33
34
35
36
# 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),
      Keoken::PREFIX_BYTE_AMOUNT[0..prefix_length(amount)] + amount.to_s(16)
    ].flatten.join
  self
end

#hexString

Hexadecimal value of script.

Returns:

  • (String)
    Hexadecimal value of script token.


60
61
62
63
64
65
66
67
# File 'lib/keoken/token.rb', line 60

def hex
  [
    Bitcoin::Script::OP_RETURN.to_s(16),
    Keoken::PREFIX_SIZE,
    Keoken::PREFIX,
    data_length
  ].join + @data_script
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
# File 'lib/keoken/token.rb', line 44

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

#to_hashObject

Deserialization of object return [Hash] Deserialization of token object.


84
85
86
87
88
89
90
91
# File 'lib/keoken/token.rb', line 84

def to_hash
  {
    id: @id,
    name: @name,
    amount: @amount,
    transaction_type: @transaction_type
  }
end

#to_jsonObject

JSON serialization of object return [String] JSON serialization of token object.


72
73
74
75
76
77
78
79
# File 'lib/keoken/token.rb', line 72

def to_json
  {
    id: @id,
    name: @name,
    amount: @amount,
    transaction_type: @transaction_type
  }.to_json
end