ethereum-tx 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: bd0ee3551841f532a5ecc0dfc7524205f09e269c
4
- data.tar.gz: 9f088bb9d3999be31aaf706bd8164555cd9d11f8
3
+ metadata.gz: fc27033601f87178ca950deac60cf55e4423993a
4
+ data.tar.gz: cce917c5ee1aba95dc0e0ac70293193c5acda069
5
5
  SHA512:
6
- metadata.gz: 1ac7d61b2b9d4e85c7da5c741e69a387dd8ef90d886ddbaa473bbf389400e124b160e0c45af90adebb2840f3275645e82b71920ecd5614d2791d53837a0eef95
7
- data.tar.gz: 776246ca9d84893139a8f1567fdd3f217e4532ff992e0ad84da2266203181a5f3adb607804b57a6c8714b99ff734aa9df40e6b2fab75353d19150577f03d36aa
6
+ metadata.gz: 0489c124c62a0fd46dbd6094d0428773e419f38b91402aafe4e40be7b4a8c89e7bf42e14f0da0bdd817e8aa34ae768bc4317b59142148b9ff6a8cfbd7decb122
7
+ data.tar.gz: bd089946955e17cadd852f8561a0a8bf84cc66c448d67589197b90c0dbe0d563a08be22034edb205e0945a07c64e79a88f0118b26fcd67a17c9093a0fdaf42e6
data/README.md CHANGED
@@ -1,8 +1,6 @@
1
1
  # Ethereum::Tx
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/ethereum/tx`. To experiment with that code, run `bin/console` for an interactive prompt.
4
-
5
- TODO: Delete this and the text above, and describe your gem
3
+ A simple, light weight, library to build and sign Ethereum transactions.
6
4
 
7
5
  ## Installation
8
6
 
@@ -22,20 +20,47 @@ Or install it yourself as:
22
20
 
23
21
  ## Usage
24
22
 
25
- TODO: Write usage instructions here
23
+ ### Keys
24
+ Create a new key:
25
+ ```
26
+ key = Ethereum::Key.new
27
+ ```
28
+ Or import and existing one:
29
+ ```
30
+ old_key = Ethereum::Key.new private_key
31
+ ```
32
+
33
+ ### Transactions
26
34
 
27
- ## Development
35
+ Build a transaction from scratch:
36
+ ```
37
+ tx = Ethereum::Tx.new to: key.address, gas_price: 25_000, gas_limit: 25_000
38
+ tx.data = 'abcdef' #optional
39
+ tx.value = 1_000_000_000_000 #optional
40
+ ```
41
+ Or decode an encoded raw transaction:
42
+ ```
43
+ tx = Ethereum::Tx.decode hex
44
+ ```
28
45
 
29
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
46
+ Then sign the transaction:
47
+ ```
48
+ tx.sign key
49
+ ```
50
+ Finally you can broadcast the raw transaction, `tx.encoded`, to your Ethereum node.
30
51
 
31
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
52
 
33
53
  ## Contributing
34
54
 
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/ethereum-tx.
55
+ Bug reports and pull requests are welcome on GitHub at https://github.com/se3000/ethereum-tx.
36
56
 
37
57
 
38
58
  ## License
39
59
 
40
60
  The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
41
61
 
62
+ ## TODO
63
+ - Better test suite.
64
+ - Expose API for HD keys.
65
+ - Separate out code pulled from [bitcoin-ruby](https://github.com/lian/bitcoin-ruby) and [ruby-ethereum](github.com/janx/ruby-ethereum) into their own gems to eliminate duplication.
66
+ - Support signing with [libsecp256k1](https://github.com/bitcoin-core/secp256k1).
@@ -1,11 +1,11 @@
1
1
  # coding: utf-8
2
2
  lib = File.expand_path('../lib', __FILE__)
3
3
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require 'ethereum/tx/version'
4
+ require 'ethereum'
5
5
 
6
6
  Gem::Specification.new do |spec|
7
7
  spec.name = "ethereum-tx"
8
- spec.version = Ethereum::Tx::VERSION
8
+ spec.version = '0.2.0'
9
9
  spec.authors = ["Steve Ellis"]
10
10
  spec.email = ["email@steveell.is"]
11
11
 
@@ -19,7 +19,7 @@ Gem::Specification.new do |spec|
19
19
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
20
  spec.require_paths = ["lib"]
21
21
 
22
- spec.add_dependency('digest-sha3', '~> 1.1')
22
+ spec.add_dependency "digest-sha3", "~> 1.1"
23
23
  spec.add_dependency "ffi", "~> 1.0"
24
24
  spec.add_dependency "money-tree", "~> 0.9"
25
25
  spec.add_dependency "rlp", "~> 0.7"
@@ -17,7 +17,6 @@ module Ethereum
17
17
  autoload :Sedes, 'ethereum/sedes'
18
18
  autoload :Tx, 'ethereum/tx'
19
19
  autoload :Utils, 'ethereum/utils'
20
- autoload :VERSION, 'ethereum/version'
21
20
 
22
21
  class ValidationError < StandardError; end
23
22
  class InvalidTransaction < ValidationError; end
@@ -19,7 +19,12 @@ module Ethereum
19
19
  s: big_endian_int
20
20
  })
21
21
 
22
- attr_reader :signature
22
+ attr_writer :signature
23
+
24
+ def self.decode(data)
25
+ data = Utils.hex_to_bin(data) if data.match(/\A\h+\Z/)
26
+ deserialize(RLP.decode data)
27
+ end
23
28
 
24
29
  def initialize(*args)
25
30
  fields = {v: 0, r: 0, s: 0}.merge parse_field_args(args)
@@ -45,10 +50,27 @@ module Ethereum
45
50
  self
46
51
  end
47
52
 
53
+ def to_h
54
+ self.class.serializable_fields.keys.inject({}) do |hash, field|
55
+ hash[field] = send field
56
+ hash
57
+ end
58
+ end
48
59
 
49
- private
60
+ def from
61
+ return @from if @from
62
+ @from ||= OpenSsl.recover_compact(signature_hash, signature) if signature
63
+ end
50
64
 
51
- attr_writer :signature
65
+ def signature
66
+ return @signature if @signature
67
+ self.signature = [v, r, s].map do |integer|
68
+ Utils.int_to_base256 integer
69
+ end.join if [v, r, s].all?
70
+ end
71
+
72
+
73
+ private
52
74
 
53
75
  def check_transaction_validity
54
76
  if [gas_price, gas_limit, value, nonce].max > UINT_MAX
@@ -72,5 +94,9 @@ module Ethereum
72
94
  GTXDATANONZERO * num_non_zero_bytes
73
95
  end
74
96
 
97
+ def signature_hash
98
+ Utils.keccak256 unsigned_encoded
99
+ end
100
+
75
101
  end
76
102
  end
@@ -34,6 +34,15 @@ module Ethereum
34
34
  end
35
35
  end
36
36
 
37
+ def int_to_base256(int)
38
+ bytes = []
39
+ while int > 0 do
40
+ bytes.unshift(int % 256)
41
+ int /= 256
42
+ end
43
+ bytes.pack('C*')
44
+ end
45
+
37
46
  def v_r_s_for(signature)
38
47
  [
39
48
  signature[0].bytes[0],
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ethereum-tx
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Steve Ellis
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-05-19 00:00:00.000000000 Z
11
+ date: 2016-05-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: digest-sha3
@@ -144,7 +144,6 @@ files:
144
144
  - lib/ethereum/open_ssl.rb
145
145
  - lib/ethereum/sedes.rb
146
146
  - lib/ethereum/tx.rb
147
- - lib/ethereum/tx/version.rb
148
147
  - lib/ethereum/utils.rb
149
148
  homepage: https://github.com/se3000/ethereum-tx
150
149
  licenses:
@@ -1,5 +0,0 @@
1
- module Ethereum
2
- class Tx
3
- VERSION = "0.1.0"
4
- end
5
- end