class Keoken::Token
Attributes
Public Class Methods
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.
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
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
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
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
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
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
# File lib/keoken/token.rb, line 94 def data_length @data_script.htb.bytesize.to_s(16) end
# 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
# 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
# File lib/keoken/token.rb, line 98 def prefix_length(amount) Keoken::AMOUNT_SIZE - (amount.to_s(16).length + 1) end