Class: Arweave::Transaction

Inherits:
Object
  • Object
show all
Defined in:
lib/arweave/transaction.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ Transaction

Returns a new instance of Transaction.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# 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

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



5
6
7
# File 'lib/arweave/transaction.rb', line 5

def attributes
  @attributes
end

Class Method Details

.anchorObject



54
55
56
# File 'lib/arweave/transaction.rb', line 54

def anchor
  Api.instance.get_transaction_anchor.body
end

.create_status_object(status, data) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/arweave/transaction.rb', line 86

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) ⇒ Object



70
71
72
73
74
75
# File 'lib/arweave/transaction.rb', line 70

def data(id)
  res = Api.instance.get_transaction_data(id)
  raise TransactionNotFound if res.not_found?

  Base64.urlsafe_decode64(res.body)
end

.find(id) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/arweave/transaction.rb', line 58

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

.status(id) ⇒ Object



77
78
79
80
81
82
83
84
# File 'lib/arweave/transaction.rb', line 77

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

Instance Method Details

#commitObject



46
47
48
49
50
51
# File 'lib/arweave/transaction.rb', line 46

def commit
  raise Arweave::TransactionNotSigned if @attributes[:signature].empty?

  Api.instance.commit(self)
  self
end

#sign(wallet) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# 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