Skip to content

Commit

Permalink
expect and return hex sig instead binary sig
Browse files Browse the repository at this point in the history
  • Loading branch information
buhrmi committed Jul 30, 2018
1 parent b56c622 commit 97d70a0
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,16 @@ Eth::Utils.format_address "0x4bc787699093f11316e819b5692be04a712c4e69" # => "0x4

### Personal Signatures

You can generate and verify web3/metamask-compatible signatures:
You can recover public keys and generate web3/metamask-compatible signatures:

```ruby
# Generate signature
key.personal_sign('hello world')

# Recover signature
message = 'test'
signature = '0x3eb24bd327df8c2b614c3f652ec86efe13aa721daf203820241c44861a26d37f2bffc6e03e68fc4c3d8d967054c9cb230ed34339b12ef89d512b42ae5bf8c2ae1c'
Eth::Key.personal_recover(message, Eth::Utils.hex_to_bin(signature)) # => 043e5b33f0080491e21f9f5f7566de59a08faabf53edbc3c32aaacc438552b25fdde531f8d1053ced090e9879cbf2b0d1c054e4b25941dab9254d2070f39418afc
Eth::Key.personal_recover(message, signature) # => 043e5b33f0080491e21f9f5f7566de59a08faabf53edbc3c32aaacc438552b25fdde531f8d1053ced090e9879cbf2b0d1c054e4b25941dab9254d2070f39418afc
```

### Configure
Expand Down
5 changes: 3 additions & 2 deletions lib/eth/key.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ def self.decrypt(data, password)
end

def self.personal_recover(message, signature)
OpenSsl.recover_compact(Utils.keccak256(Utils.prefix_message(message)), signature.bytes.rotate(-1).pack('c*'))
bin_signature = Utils.hex_to_bin(signature).bytes.rotate(-1).pack('c*')
OpenSsl.recover_compact(Utils.keccak256(Utils.prefix_message(message)), bin_signature)
end

def initialize(priv: nil)
Expand Down Expand Up @@ -59,7 +60,7 @@ def verify_signature(message, signature)
end

def personal_sign(message)
sign(Utils.prefix_message(message)).bytes.rotate(1).pack('c*')
Utils.bin_to_hex(sign(Utils.prefix_message(message)).bytes.rotate(1).pack('c*'))
end


Expand Down
2 changes: 1 addition & 1 deletion spec/eth/key_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@

describe ".personal_recover" do
let(:message) { "test" }
let(:signature) { hex_to_bin "3eb24bd327df8c2b614c3f652ec86efe13aa721daf203820241c44861a26d37f2bffc6e03e68fc4c3d8d967054c9cb230ed34339b12ef89d512b42ae5bf8c2ae1c" }
let(:signature) { "3eb24bd327df8c2b614c3f652ec86efe13aa721daf203820241c44861a26d37f2bffc6e03e68fc4c3d8d967054c9cb230ed34339b12ef89d512b42ae5bf8c2ae1c" }
let(:public_hex) { "043e5b33f0080491e21f9f5f7566de59a08faabf53edbc3c32aaacc438552b25fdde531f8d1053ced090e9879cbf2b0d1c054e4b25941dab9254d2070f39418afc" }

it "it can recover a public key from a signature generated with web3/metamask" do
Expand Down

0 comments on commit 97d70a0

Please sign in to comment.