This repository has been archived by the owner on May 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into signature-publication-delays
- Loading branch information
Showing
3 changed files
with
195 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/keep-network/keep-common/pkg/chain/ethereum/ethutil" | ||
) | ||
|
||
// Signatures should match a signature format on mycrypto.com. A signing/verification | ||
// tool available on https://mycrypto.com/sign-and-verify-message can be used to | ||
// cross-check correctness of signatures provided by our implementation. | ||
|
||
var validSignature = EthereumSignature{ | ||
Address: common.HexToAddress("0x4BCFC3099F12C53D01Da46695CC8776be584b946"), | ||
Message: "verySecretMessage", | ||
Signature: "0xc8be189ab0ee691de7019eaa3de58558b84775085d9a0840908343ac690e02ca3f6e3d2dc70025b9b214d96c30e38c41f818cccd6f06b7a81c4afd26cbe6d6d600", | ||
Version: "2", | ||
} | ||
|
||
func TestSign(t *testing.T) { | ||
message := "verySecretMessage" | ||
keyFilePath := "../internal/testdata/eth_key.json" | ||
keyFilePassword := "password" | ||
|
||
expectedResult := &EthereumSignature{ | ||
Address: common.HexToAddress("0x4BCFC3099F12C53D01Da46695CC8776be584b946"), | ||
Message: message, | ||
Signature: "0xc8be189ab0ee691de7019eaa3de58558b84775085d9a0840908343ac690e02ca3f6e3d2dc70025b9b214d96c30e38c41f818cccd6f06b7a81c4afd26cbe6d6d600", | ||
Version: "2", | ||
} | ||
|
||
ethereumKey, err := ethutil.DecryptKeyFile(keyFilePath, keyFilePassword) | ||
if err != nil { | ||
t.Fatalf( | ||
"failed to read key file [%s]: [%v]", | ||
keyFilePath, | ||
err, | ||
) | ||
} | ||
|
||
ethereumSignature, err := sign(ethereumKey, message) | ||
if err != nil { | ||
t.Errorf("signing failed: [%v]", err) | ||
} | ||
|
||
if !reflect.DeepEqual(ethereumSignature, expectedResult) { | ||
t.Errorf( | ||
"unexpected signature\nexpected: %v\nactual: %v", | ||
expectedResult, | ||
ethereumSignature, | ||
) | ||
} | ||
} | ||
|
||
func TestVerify_V0(t *testing.T) { | ||
err := verify(&validSignature) | ||
if err != nil { | ||
t.Errorf("unexpected error: [%v]", err) | ||
} | ||
} | ||
|
||
func TestVerify_V27(t *testing.T) { | ||
// go-ethereum library produces a signature with V value of 0 or 1. In some | ||
// chains the V value is expected to be 27 or 28. Even ethereum is sometimes | ||
// inconsistent about that across their libraries. In our implementation we | ||
// expect V to be 0 or 1, we're not currently supporting 27 or 28. | ||
ethereumSignature := validSignature | ||
ethereumSignature.Signature = "0xc8be189ab0ee691de7019eaa3de58558b84775085d9a0840908343ac690e02ca3f6e3d2dc70025b9b214d96c30e38c41f818cccd6f06b7a81c4afd26cbe6d6d61b" | ||
|
||
expectedError := fmt.Errorf("could not recover public key from signature [invalid signature recovery id]") | ||
|
||
err := verify(ðereumSignature) | ||
if !reflect.DeepEqual(expectedError, err) { | ||
t.Errorf("unexpected error\nexpected: [%v]\nactual: [%v]", expectedError, err) | ||
} | ||
} | ||
|
||
func TestVerify_WrongAddress(t *testing.T) { | ||
ethereumSignature := validSignature | ||
ethereumSignature.Address = common.HexToAddress("0x93df7c54c41A9D7FB17C1E8039d387a2A924708c") | ||
|
||
expectedError := fmt.Errorf("invalid signer\n\texpected: 0x93df7c54c41A9D7FB17C1E8039d387a2A924708c\n\tactual: 0x4BCFC3099F12C53D01Da46695CC8776be584b946") | ||
|
||
err := verify(ðereumSignature) | ||
if !reflect.DeepEqual(expectedError, err) { | ||
t.Errorf("unexpected error\nexpected: [%v]\nactual: [%v]", expectedError, err) | ||
} | ||
} | ||
|
||
func TestVerify_WrongMessage(t *testing.T) { | ||
ethereumSignature := validSignature | ||
ethereumSignature.Message = "notTheSignedMessage" | ||
|
||
expectedError := fmt.Errorf("invalid signer\n\texpected: 0x4BCFC3099F12C53D01Da46695CC8776be584b946\n\tactual: 0x19882d7da145A10d5AEEFEe217Fd87dE679b4bb1") | ||
|
||
err := verify(ðereumSignature) | ||
if !reflect.DeepEqual(expectedError, err) { | ||
t.Errorf("unexpected error\nexpected: [%v]\nactual: [%v]", expectedError, err) | ||
} | ||
} | ||
|
||
func TestVerify_WrongSignature(t *testing.T) { | ||
ethereumSignature := validSignature | ||
ethereumSignature.Signature = "0xc8be189ab0ee691de7019eaa3de58558b84775085d9a0840908343ac690e02ca3f6e3d2dc70025b9b214d96c30e38c41f818cccd6f06b7a81c4afd26cbe6d6d601" | ||
|
||
expectedError := fmt.Errorf("invalid signer\n\texpected: 0x4BCFC3099F12C53D01Da46695CC8776be584b946\n\tactual: 0xb560e6c746138528509de08B782E3144E031a6B1") | ||
|
||
err := verify(ðereumSignature) | ||
if !reflect.DeepEqual(expectedError, err) { | ||
t.Errorf("unexpected error\nexpected: [%v]\nactual: [%v]", expectedError, err) | ||
} | ||
} | ||
|
||
func TestVerify_WrongVersion(t *testing.T) { | ||
ethereumSignature := validSignature | ||
ethereumSignature.Version = "1" | ||
|
||
expectedError := fmt.Errorf("unsupported ethereum signature version\n\texpected: 2\n\tactual: 1") | ||
|
||
err := verify(ðereumSignature) | ||
if !reflect.DeepEqual(expectedError, err) { | ||
t.Errorf("unexpected error\nexpected: [%v]\nactual: [%v]", expectedError, err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"address": "4bcfc3099f12c53d01da46695cc8776be584b946", | ||
"crypto": { | ||
"cipher": "aes-128-ctr", | ||
"ciphertext": "904cb53d4e6c3e7dc6fb2443ad5db42b502e221b4144276ac150d51cea3cd638", | ||
"cipherparams": { | ||
"iv": "be8d7c7197c45d371cb6d97f9878636e" | ||
}, | ||
"kdf": "scrypt", | ||
"kdfparams": { | ||
"dklen": 32, | ||
"n": 262144, | ||
"p": 1, | ||
"r": 8, | ||
"salt": "b93d46f5ff9a9cf15a3546b05a0941d17e1cb9467e2484f5e1495a030616f258" | ||
}, | ||
"mac": "87bb79dbb0e0f056d8d2e91d7c7a7c18367a52fae5324b5c75423272d849860f" | ||
}, | ||
"id": "bffd9951-e141-41a3-9d63-82c8dd92c1d5", | ||
"version": 3 | ||
} |