Skip to content

Commit 51bb2b0

Browse files
committed
Merge branch 'develop'
2 parents d96183d + af29916 commit 51bb2b0

7 files changed

+39
-8
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ Then sign the transaction:
5252
```ruby
5353
tx.sign key
5454
```
55-
Get the raw transaction with `tx.hex`, and broadcast it through any Ethereum node.
55+
Get the raw transaction with `tx.hex`, and broadcast it through any Ethereum node. Or, just get the TXID with `tx.hash`.
5656

5757

5858
## Contributing

lib/eth/tx.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def encoded
4141
end
4242

4343
def hex
44-
bin_to_hex encoded
44+
Utils.bin_to_hex encoded
4545
end
4646

4747
def sign(key)
@@ -70,6 +70,10 @@ def signature
7070
end.join if [v, r, s].all?
7171
end
7272

73+
def hash
74+
Utils.bin_to_hex Utils.keccak256_rlp(self)
75+
end
76+
7377

7478
private
7579

lib/eth/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module Eth
2-
VERSION = "0.3.1"
2+
VERSION = "0.3.2"
33
end

spec/eth/tx_spec.rb

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
end
6060

6161
it "also accepts hex" do
62-
tx2 = Eth::Tx.decode(Eth::Utils.bin_to_hex tx1.encoded)
62+
tx2 = Eth::Tx.decode(tx1.hex)
6363
expect(tx2).to eq(tx1)
6464
end
6565
end
@@ -131,4 +131,17 @@
131131
it { is_expected.to be_nil }
132132
end
133133
end
134+
135+
describe "#hash" do
136+
let(:txid1) { '66734e70ea28eaa28eb1bace4ca87573c48f52cca7590459ad20dc58bae1a819' }
137+
let(:txid2) { '7151f5b0d229c62a5076de4133ba06fffc033e25bf99691c3e0a0a99c5a64538' }
138+
let(:txids) { [txid1, txid2] }
139+
140+
it "hashes the serialized full transaction" do
141+
txids.each do |txid|
142+
tx = Eth::Tx.decode read_hex_fixture(txid)
143+
expect(tx.hash).to eq(txid)
144+
end
145+
end
146+
end
134147
end
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
f871831007e28505d21dba0082c350949757b0f208aebeb22b9f7a73c152654b2de1acc0808a8b1472453633352e36351ca066bbc9b2b4d5621de654df7391a29b884e89eda63c57bd903fffee0f8d2bbbc0a068b91d3ac9e74772f49cb53052df9d6038838959bb33a30248589dbe12895403
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
f871831007e08505d21dba0082c3509491d21117ce2056b62e42a422186e098ceab56f86808a8b1472453633372e35301ca0b0fa8b20828178ce382a1029f264065bcc8fcff36f28bff5430e7d2d6e5a78baa01156a157cb4bad439cbbd1e0fe74e7d028b280b124dd572f8035a9ec69736bc7

spec/spec_helper.rb

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,22 @@
22
require 'eth'
33
require 'securerandom'
44

5-
def bin_to_hex(string)
6-
string.unpack("H*")[0]
5+
module Helpers
6+
7+
def bin_to_hex(string)
8+
string.unpack("H*")[0]
9+
end
10+
11+
def hex_to_bin(string)
12+
[string].pack("H*")
13+
end
14+
15+
def read_hex_fixture(name)
16+
File.read("./spec/fixtures/#{name}.hex").strip
17+
end
18+
719
end
820

9-
def hex_to_bin(string)
10-
[string].pack("H*")
21+
RSpec.configure do |c|
22+
c.include Helpers
1123
end

0 commit comments

Comments
 (0)