class Arweave::Transaction

Attributes

attributes[R]

Public Class Methods

accepted?() click to toggle source
# File lib/arweave/transaction.rb, line 114
def accepted?
  status == :accepted
end
anchor() click to toggle source
# File lib/arweave/transaction.rb, line 75
def anchor
  Api.instance.get_transaction_anchor.body
end
create_status_object(status, data) click to toggle source
# File lib/arweave/transaction.rb, line 110
def create_status_object(status, data)
  status_object = OpenStruct.new(status: status, data: data)

  status_object.instance_eval do
    def accepted?
      status == :accepted
    end

    def pending?
      status == :pending
    end

    def to_s
      status.to_s
    end

    def to_sym
      status
    end
  end

  status_object
end
data(id) click to toggle source
# File lib/arweave/transaction.rb, line 91
def data(id)
  res = Api.instance.get_transaction_data(id)
  raise TransactionNotFound if res.not_found?

  Base64.urlsafe_decode64(res.body)
end
find(id) click to toggle source
# File lib/arweave/transaction.rb, line 79
def find(id)
  res = Api.instance.get_transaction(id)
  raise TransactionNotFound if res.not_found?
  data =
    JSON.parse(res.body).transform_keys!(&:to_sym).yield_self do |hash|
      hash[:data] = Base64.urlsafe_decode64(hash[:data]) if hash[:data]
      hash
    end

  new(data)
end
new(attributes) click to toggle source
# File lib/arweave/transaction.rb, line 7
def initialize(attributes)
  @attributes =
    {
      id: '',
      last_tx: '',
      owner: '',
      tags: [],
      target: '',
      quantity: '0',
      data: '',
      reward: '0',
      signature: ''
    }.merge(attributes).transform_keys!(&:to_sym).yield_self do |hash|
      if hash[:data]
        hash[:data] = Base64.urlsafe_encode64(hash[:data], padding: false)
      end
      hash
    end
end
pending?() click to toggle source
# File lib/arweave/transaction.rb, line 118
def pending?
  status == :pending
end
status(id) click to toggle source
# File lib/arweave/transaction.rb, line 98
def status(id)
  res = Api.instance.get_transaction_status(id)
  raise TransactionNotFound if res.not_found?

  create_status_object(
    :accepted,
    JSON.parse(res.body).transform_keys!(&:to_sym)
  )
rescue JSON::ParserError
  create_status_object(:pending, {})
end
to_s() click to toggle source
# File lib/arweave/transaction.rb, line 122
def to_s
  status.to_s
end
to_sym() click to toggle source
# File lib/arweave/transaction.rb, line 126
def to_sym
  status
end

Public Instance Methods

add_tag(name:, value:) click to toggle source
# File lib/arweave/transaction.rb, line 53
def add_tag(name:, value:)
  attributes[:tags].push(
    {
      name: Base64.urlsafe_encode64(name, padding: false),
      value: Base64.urlsafe_encode64(value, padding: false)
    }
  )

  self
end
commit() click to toggle source
# File lib/arweave/transaction.rb, line 46
def commit
  raise Arweave::TransactionNotSigned if @attributes[:signature].empty?

  Api.instance.commit(self)
  self
end
sign(wallet) click to toggle source
# File lib/arweave/transaction.rb, line 27
def sign(wallet)
  @attributes[:last_tx] = self.class.anchor
  @attributes[:reward] =
    Api.instance.reward(
      @attributes.fetch(:data, '').length,
      @attributes.fetch(:target, nil)
    ).body
  @attributes[:owner] = wallet.owner

  signature = wallet.sign(get_signature_data)
  @attributes[:signature] =
    Base64.urlsafe_encode64 signature, padding: false
  @attributes[:id] =
    Base64.urlsafe_encode64 Digest::SHA256.digest(signature), padding: false

  # TODO: verify signature
  self
end
tags() click to toggle source
# File lib/arweave/transaction.rb, line 64
def tags
  attributes[:tags].map do |tag|
    tag.transform_keys!(&:to_sym)
    {
      name: Base64.urlsafe_decode64(tag[:name]),
      value: Base64.urlsafe_decode64(tag[:value])
    }
  end
end

Private Instance Methods

get_signature_data() click to toggle source
# File lib/arweave/transaction.rb, line 137
def get_signature_data
  Base64.urlsafe_decode64(attributes.fetch(:owner)) +
    Base64.urlsafe_decode64(attributes.fetch(:target)) +
    Base64.urlsafe_decode64(attributes.fetch(:data)) +
    attributes.fetch(:quantity) + attributes.fetch(:reward) +
    Base64.urlsafe_decode64(attributes.fetch(:last_tx)) + tags_string
end
tags_string() click to toggle source
# File lib/arweave/transaction.rb, line 149
def tags_string
  attributes.fetch(:tags).reduce('') do |acc, tag|
    acc + Base64.urlsafe_decode64(tag[:name]) +
      Base64.urlsafe_decode64(tag[:value])
  end
end
verify() click to toggle source
# File lib/arweave/transaction.rb, line 145
def verify
  true
end