arweave 0.9.0 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9d04de6a25b28b4b42809d2d14fbcd7e71befe7b49ed3977a81ef7eba5d3ac35
4
- data.tar.gz: 74e087df6a11aa74699391335df2c8c01ec6bfe640a874320ce6e2dca300426e
3
+ metadata.gz: 3735532e9b91e9cbe5e84b671ae9f7685fdd8328f2fd8ae644e0f069bcb75d3e
4
+ data.tar.gz: 4df8ea22c8f6a20ae9299deb6043c798a23cf071c0cfc9e7222ce843aa5b5da2
5
5
  SHA512:
6
- metadata.gz: 79526f3f277e5425d6c52b0dbfcd63e1852d8a6923195d38e28378dacf753992817ddbea79fcba7d1558260cc4985d2120decaa4af10b9d53bfacfc13acee831
7
- data.tar.gz: 52b5b82f06a20d5cee8f4c0e13621d27a50f5eb6068d7d1790c1d870c3420671ceadaab2ead190de3ea805529b92221ce1bf0ab66659f2d7b521d411af1a52ae
6
+ metadata.gz: 804ebaacd0bfe841f3a38b586dba8ae7e271248b80e35b6755c5affc0aa90fb292dfd0125fb3c029f2fab43afbe585b1a578a6759cda5c6ddaf4df63b14cec53
7
+ data.tar.gz: 25bc0d61ab800180e4066b9c171d1a383cd1a7f5d13ed7a803dea078b70c6ec01f8d207c35c8a6c9a5dea479ed9573f31e3ffb89f7c92cd7238c21361331bd43
data/README.md CHANGED
@@ -12,7 +12,7 @@ gem 'arweave'
12
12
  ```
13
13
  ## Usage
14
14
 
15
- ### Configuration
15
+ ## Configuration
16
16
  The default node that this package uses is `https://arweave.net`. But you can simply configure it:
17
17
 
18
18
  ```ruby
@@ -23,11 +23,96 @@ Arweave::Client.configure do |config|
23
23
  end
24
24
  ```
25
25
 
26
+ ## Transaction methods
27
+
28
+ ### Getting transaction anchor
29
+ ```ruby
30
+ anchor = Arweave::Transaction.anchor
31
+ ```
32
+
26
33
  ### Creating transactions
27
- For a complete list of argument you can pass to the `create_transaction` method, checkout the [documentation](https://docs.arweave.org/developers/server/http-api#submit-a-transaction).
34
+ For a complete list of arguments you can pass to the `new` method,
35
+ checkout the [documentation](https://docs.arweave.org/developers/server/http-api#submit-a-transaction).
28
36
 
29
37
  ```ruby
30
- client = Arweave::Client.new
31
38
  jwk = JSON.parse(File.read(File.expand_path(File.join('path-to-keyfile'))))
32
- client.create_transaction(wallet: wallet, data: '<b>test</b>').commit
39
+ wallet = Arweave::Wallet.new jwk
40
+
41
+ transaction = Arweave::Transaction.new(data: '<b>test</b>')
42
+ transaction.sign(wallet)
43
+ transaction.commit
44
+ # => #<Arweavev::Transaction:0x00007f9b61299330 @attributes={...}>
45
+ ```
46
+
47
+ You can also chain the methods
48
+ ```ruby
49
+ Arweave::Transaction.new(data: '<b>test</b>').sign(wallet).commit
50
+ # => #<Arweavev::Transaction:0x00007f9b61299330 @attributes={...}>
51
+ ```
52
+
53
+ You can get the transaction attributes from the attribuets hash:
54
+ ```ruby
55
+ transaction.attributes[:id]
56
+ # => "tSF6pxiknBk0hBUTkdzq02E0zvsrT0xe4UtCzZit-bz"
57
+ ```
58
+
59
+ ### Find transaction by id
60
+ ```ruby
61
+ Arweave::Transaction.find('tSF6pxiknBk0hBUTkdzq02E0zvsrT0xe4UtCzZit-bz')
62
+ # => #<Arweavev::Transaction:0x00007f9b61299330 @attributes={...}>
63
+ ```
64
+
65
+ ### Getting a transaction data
66
+ the transaction `data` class method returns base64 decoded data for data transactions
67
+ ```ruby
68
+ Arweave::Transaction.data('tSF6pxiknBk0hBUTkdzq02E0zvsrT0xe4UtCzZit-bz')
69
+ # => "<b>test</b>"
70
+ ```
71
+
72
+ ### Getting transaction status
73
+ the `status` class method returns a hash containing transaction status
74
+ (which is pending or accepted) and the and the data and a hash about the block.
75
+ ```ruby
76
+ Arweave::Transaction.status('tSF6pxiknBk0hBUTkdzq02E0zvsrT0xe4UtCzZit-bz')
77
+ # => {
78
+ # "status": "accepted",
79
+ # "data": {
80
+ # "block_height": 468306,
81
+ # "block_indep_hash": "hh0ceHGfEOuTQWYMXGNzb2AabezqZUhtSw5vtUPKTtGmkViPArX5WeLBKBYZIwlS",
82
+ # "number_of_confirmations": 388
83
+ # }
84
+ # }
85
+ ```
86
+
87
+ ## Wallet methods
88
+
89
+ ### Getting wallet owner
90
+ The `owner` method returns the full RSA modulus value of the wallet.
91
+ ```ruby
92
+ wallet.owner
93
+ # => "...-Tr3S_HBgX2ixr1ZwEMD7iJlUEJrvItE-lBepKHHLd0OFJ3PgkKsP15TEefVezhssSO_s_EQdJ4yA7Ij8Y_XsAGXrjM76MYa7QZNWTLqhc7cixBDBWk0KLPBuN-AdjN71BXYJRZ_5gMzUyu1GKSuaIcvzISTqPbVuJwFPTNLkmm3t-wRtioKAyQzieqskQuh4iYKmeBQ0SAuDd0Xf3wcGxWRIrK7lphP2A0cIA65dUY2klDbiZVwWh_82igD00cGmZLSzFTaVNqIBNyPN5nTLriCGnYbWnMj9-uPghK_NYGyKYOkwPGJB3bZ_fPvLzWkrTnKi1uqyKdp_4AEKAfKLO3agh7rfB3wNKe-..."
94
+ ```
95
+
96
+ ### Getting wallet address
97
+ The address of a wallet is a Base64 URL encoded SHA-256 hash of the `n` value from the JWK.
98
+ ```ruby
99
+ wallet.address
100
+ # => "bQ3zPuzKXOpnaZ_O_kcbByRBaso2e4nyIN8KmsQH80"
101
+ ```
102
+
103
+ ### Getting wallet balance
104
+ This method returns an open struct contains the balance both `ar` and `winston`
105
+ as `BigDecimal`.
106
+ ```ruby
107
+ balance = wallet.balance
108
+ # => #<OpenStruct ar=0.249891527825e0, winston=0.249891527825e12>
109
+ balance.ar # => 0.249891527825e0
110
+ balance.winston # => 0.249891527825e12
111
+
112
+ balance.winston.to_i # => 249891527825
113
+ ```
114
+
115
+ ### Get the wallet's last transaction id
116
+ ```ruby
117
+ wallet.last_transaction_id # => "BsHjWHBwSlmW_VgOcgLmsQacQjpohmvVDLMMVyuAkie"
33
118
  ```
@@ -3,3 +3,4 @@ require 'arweave/client'
3
3
  require 'arweave/api'
4
4
  require 'arweave/transaction'
5
5
  require 'arweave/wallet'
6
+ require 'arweave/errors'
@@ -19,6 +19,26 @@ module Arweave
19
19
  self.class.get('/tx_anchor')
20
20
  end
21
21
 
22
+ def get_transaction(transaction_id)
23
+ self.class.get("/tx/#{transaction_id}")
24
+ end
25
+
26
+ def get_transaction_data(transaction_id)
27
+ self.class.get("/tx/#{transaction_id}/data")
28
+ end
29
+
30
+ def get_transaction_status(transaction_id)
31
+ self.class.get("/tx/#{transaction_id}/status")
32
+ end
33
+
34
+ def get_last_transaction_id(wallet_address)
35
+ self.class.get("/wallet/#{wallet_address}/last_tx")
36
+ end
37
+
38
+ def get_wallet_balance(wallet_address)
39
+ self.class.get("/wallet/#{wallet_address}/balance")
40
+ end
41
+
22
42
  def reward(byte_size, address = '')
23
43
  self.class.get("/price/#{byte_size}/#{address}")
24
44
  end
@@ -2,40 +2,6 @@ require 'base64'
2
2
 
3
3
  module Arweave
4
4
  class Client
5
- def initialize
6
- @api = Api.instance
7
- end
8
-
9
- def create_transaction(wallet:, **attributes)
10
- attributes.transform_keys!(&:to_sym)
11
-
12
- transaction =
13
- Transaction.new(
14
- last_tx: api.get_transaction_anchor,
15
- owner: wallet.owner,
16
- tags: [],
17
- target: attributes.fetch(:target, ''),
18
- quantity: attributes.fetch(:quantity, '0'),
19
- data:
20
- Base64.urlsafe_encode64(
21
- attributes.fetch(:data, ''),
22
- padding: false
23
- ),
24
- reward:
25
- api.reward(
26
- attributes.fetch(:data, '').length,
27
- attributes.fetch(:target, nil)
28
- )
29
- )
30
-
31
- transaction.sign(wallet)
32
- transaction
33
- end
34
-
35
- def commit(transaction)
36
- api.commit(transaction)
37
- end
38
-
39
5
  class << self
40
6
  attr_accessor :configuration
41
7
  end
@@ -0,0 +1,4 @@
1
+ module Arweave
2
+ class TransactionNotFound < StandardError; end
3
+ class TransactionNotSigned < StandardError; end
4
+ end
@@ -16,29 +16,74 @@ module Arweave
16
16
  data: '',
17
17
  reward: '0',
18
18
  signature: ''
19
- }.merge(attributes).yield_self do |hash|
20
- hash.each do |key, value|
21
- value.respond_to?(:map) ? hash[key] = value : hash[key] = value.to_s
19
+ }.merge(attributes).transform_keys!(&:to_sym).yield_self do |hash|
20
+ if hash[:data]
21
+ hash[:data] = Base64.urlsafe_encode64(hash[:data], padding: false)
22
22
  end
23
-
24
23
  hash
25
- end.transform_keys!(&:to_sym)
24
+ end
26
25
  end
27
26
 
28
27
  def sign(wallet)
29
- signature = wallet.sign(get_signature_data)
28
+ @attributes[:last_tx] = self.class.anchor
29
+ @attributes[:reward] =
30
+ Api.instance.reward(
31
+ @attributes.fetch(:data, '').length,
32
+ @attributes.fetch(:target, nil)
33
+ ).body
34
+ @attributes[:owner] = wallet.owner
30
35
 
36
+ signature = wallet.sign(get_signature_data)
31
37
  @attributes[:signature] =
32
38
  Base64.urlsafe_encode64 signature, padding: false
33
39
  @attributes[:id] =
34
40
  Base64.urlsafe_encode64 Digest::SHA256.digest(signature), padding: false
35
41
 
36
42
  # TODO: verify signature
37
- signature
43
+ self
38
44
  end
39
45
 
40
46
  def commit
47
+ raise Arweave::TransactionNotSigned if @attributes[:signature].empty?
48
+
41
49
  Api.instance.commit(self)
50
+ self
51
+ end
52
+
53
+ class << self
54
+ def anchor
55
+ Api.instance.get_transaction_anchor.body
56
+ end
57
+
58
+ def find(id)
59
+ res = Api.instance.get_transaction(id)
60
+ raise TransactionNotFound if res.not_found?
61
+ data =
62
+ JSON.parse(res.body).transform_keys!(&:to_sym).yield_self do |hash|
63
+ hash[:data] = Base64.urlsafe_decode64(hash[:data]) if hash[:data]
64
+ hash
65
+ end
66
+
67
+ new(data)
68
+ end
69
+
70
+ def data(id)
71
+ res = Api.instance.get_transaction_data(id)
72
+ raise TransactionNotFound if res.not_found?
73
+
74
+ Base64.urlsafe_decode64(res.body)
75
+ end
76
+
77
+ def status(id)
78
+ res = Api.instance.get_transaction_status(id)
79
+ raise TransactionNotFound if res.not_found?
80
+
81
+ {
82
+ status: :accepted, data: JSON.parse(res.body).transform_keys(&:to_sym)
83
+ }
84
+ rescue JSON::ParserError
85
+ { status: :pending, data: {} }
86
+ end
42
87
  end
43
88
 
44
89
  private
@@ -1,3 +1,3 @@
1
1
  module Arweave
2
- VERSION = '0.9.0'
2
+ VERSION = '1.0.0'
3
3
  end
@@ -1,12 +1,14 @@
1
1
  require 'json/jwt'
2
2
  require 'base64'
3
3
  require 'digest'
4
+ require 'ostruct'
4
5
 
5
6
  module Arweave
6
7
  class Wallet
7
- attr_reader :address, :owner
8
+ attr_reader :address, :owner, :api
8
9
 
9
10
  def initialize(jwk)
11
+ @api = Api.instance
10
12
  @jwk = jwk.transform_keys!(&:to_sym)
11
13
  end
12
14
 
@@ -29,6 +31,19 @@ module Arweave
29
31
  )
30
32
  end
31
33
 
34
+ def balance
35
+ balance_in_winstons = BigDecimal(api.get_wallet_balance(address).body)
36
+
37
+ OpenStruct.new(
38
+ ar: balance_in_winstons / 1e12,
39
+ winston: balance_in_winstons
40
+ )
41
+ end
42
+
43
+ def last_transaction_id
44
+ api.get_last_transaction_id(address).body
45
+ end
46
+
32
47
  private
33
48
 
34
49
  attr_reader :jwk
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: arweave
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aref Aslani
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-06-16 00:00:00.000000000 Z
11
+ date: 2020-06-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json-jwt
@@ -74,6 +74,7 @@ files:
74
74
  - lib/arweave.rb
75
75
  - lib/arweave/api.rb
76
76
  - lib/arweave/client.rb
77
+ - lib/arweave/errors.rb
77
78
  - lib/arweave/transaction.rb
78
79
  - lib/arweave/version.rb
79
80
  - lib/arweave/wallet.rb