Class: Arweave::Transaction
- Inherits:
-
Object
- Object
- Arweave::Transaction
- Defined in:
- lib/arweave/transaction.rb
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
Class Method Summary collapse
- .anchor ⇒ Object
- .create_status_object(status, data) ⇒ Object
- .data(id) ⇒ Object
- .find(id) ⇒ Object
- .status(id) ⇒ Object
Instance Method Summary collapse
- #add_tag(name:, value:) ⇒ Object
- #commit ⇒ Object
-
#initialize(attributes) ⇒ Transaction
constructor
A new instance of Transaction.
- #sign(wallet) ⇒ Object
- #tags ⇒ Object
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
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
5 6 7 |
# File 'lib/arweave/transaction.rb', line 5 def attributes @attributes end |
Class Method Details
.anchor ⇒ Object
75 76 77 |
# File 'lib/arweave/transaction.rb', line 75 def anchor Api.instance.get_transaction_anchor.body end |
.create_status_object(status, data) ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# 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) ⇒ Object
91 92 93 94 95 96 |
# 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) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 |
# 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 |
.status(id) ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 |
# 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 |
Instance Method Details
#add_tag(name:, value:) ⇒ Object
53 54 55 56 57 58 59 60 61 62 |
# 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 ⇒ Object
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 |
#tags ⇒ Object
64 65 66 67 68 69 70 71 72 |
# File 'lib/arweave/transaction.rb', line 64 def attributes[:tags].map do |tag| tag.transform_keys!(&:to_sym) { name: Base64.urlsafe_decode64(tag[:name]), value: Base64.urlsafe_decode64(tag[:value]) } end end |