diff options
author | Jeffrey Wilcke <jeffrey@ethereum.org> | 2015-10-16 01:32:05 +0800 |
---|---|---|
committer | Jeffrey Wilcke <jeffrey@ethereum.org> | 2015-10-16 01:32:05 +0800 |
commit | 2f1f2e4811a6f3094f99b55f6553fe27d83f9aad (patch) | |
tree | c346188eb09c640d03e493b25f6d6870c4e59a4a /crypto | |
parent | 0de9b16b118cc1c12689b96fdded22f8a42f71f4 (diff) | |
parent | 2db97986460c57ba74a563d97a704a45a270df7d (diff) | |
download | go-tangerine-2f1f2e4811a6f3094f99b55f6553fe27d83f9aad.tar go-tangerine-2f1f2e4811a6f3094f99b55f6553fe27d83f9aad.tar.gz go-tangerine-2f1f2e4811a6f3094f99b55f6553fe27d83f9aad.tar.bz2 go-tangerine-2f1f2e4811a6f3094f99b55f6553fe27d83f9aad.tar.lz go-tangerine-2f1f2e4811a6f3094f99b55f6553fe27d83f9aad.tar.xz go-tangerine-2f1f2e4811a6f3094f99b55f6553fe27d83f9aad.tar.zst go-tangerine-2f1f2e4811a6f3094f99b55f6553fe27d83f9aad.zip |
Merge pull request #1887 from Gustav-Simonsson/icap
common, crypto: add ICAP functions
Diffstat (limited to 'crypto')
-rw-r--r-- | crypto/key.go | 22 | ||||
-rw-r--r-- | crypto/key_store_test.go | 8 |
2 files changed, 30 insertions, 0 deletions
diff --git a/crypto/key.go b/crypto/key.go index 35139b67f..4ec43dfd7 100644 --- a/crypto/key.go +++ b/crypto/key.go @@ -22,6 +22,7 @@ import ( "encoding/hex" "encoding/json" "io" + "strings" "github.com/ethereum/go-ethereum/common" "github.com/pborman/uuid" @@ -143,3 +144,24 @@ func NewKey(rand io.Reader) *Key { return NewKeyFromECDSA(privateKeyECDSA) } + +// generate key whose address fits into < 155 bits so it can fit into +// the Direct ICAP spec. for simplicity and easier compatibility with +// other libs, we retry until the first byte is 0. +func NewKeyForDirectICAP(rand io.Reader) *Key { + randBytes := make([]byte, 64) + _, err := rand.Read(randBytes) + if err != nil { + panic("key generation: could not read from random source: " + err.Error()) + } + reader := bytes.NewReader(randBytes) + privateKeyECDSA, err := ecdsa.GenerateKey(S256(), reader) + if err != nil { + panic("key generation: ecdsa.GenerateKey failed: " + err.Error()) + } + key := NewKeyFromECDSA(privateKeyECDSA) + if !strings.HasPrefix(key.Address.Hex(), "0x00") { + return NewKeyForDirectICAP(rand) + } + return key +} diff --git a/crypto/key_store_test.go b/crypto/key_store_test.go index fda87ddc8..de4a21dcf 100644 --- a/crypto/key_store_test.go +++ b/crypto/key_store_test.go @@ -20,6 +20,7 @@ import ( "encoding/hex" "fmt" "reflect" + "strings" "testing" "github.com/ethereum/go-ethereum/common" @@ -223,3 +224,10 @@ func loadKeyStoreTestV1(file string, t *testing.T) map[string]KeyStoreTestV1 { } return tests } + +func TestKeyForDirectICAP(t *testing.T) { + key := NewKeyForDirectICAP(randentropy.Reader) + if !strings.HasPrefix(key.Address.Hex(), "0x00") { + t.Errorf("Expected first address byte to be zero, have: %s", key.Address.Hex()) + } +} |