keys: change Signature() and Address() to not return errors

As they never can return any real one.
This commit is contained in:
Roman Khimov 2019-08-27 17:16:33 +03:00
parent 483b875f4a
commit 5836ae6873
3 changed files with 8 additions and 15 deletions

View file

@ -125,7 +125,7 @@ func (p *PrivateKey) Address() (string, error) {
if err != nil { if err != nil {
return "", err return "", err
} }
return pk.Address() return pk.Address(), nil
} }
// Signature creates the signature using the private key. // Signature creates the signature using the private key.
@ -134,7 +134,7 @@ func (p *PrivateKey) Signature() ([]byte, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
return pk.Signature() return pk.Signature(), nil
} }
// Sign signs arbitrary length data using the private key. // Sign signs arbitrary length data using the private key.

View file

@ -168,32 +168,25 @@ func (p *PublicKey) EncodeBinary(w io.Writer) error {
return binary.Write(w, binary.LittleEndian, p.Bytes()) return binary.Write(w, binary.LittleEndian, p.Bytes())
} }
func (p *PublicKey) Signature() ([]byte, error) { func (p *PublicKey) Signature() []byte {
b := p.Bytes() b := p.Bytes()
b = append([]byte{0x21}, b...) b = append([]byte{0x21}, b...)
b = append(b, 0xAC) b = append(b, 0xAC)
sig := hash.Hash160(b) sig := hash.Hash160(b)
return sig.Bytes(), nil return sig.Bytes()
} }
func (p *PublicKey) Address() (string, error) { func (p *PublicKey) Address() string {
var ( var b []byte = p.Signature()
err error
b []byte
)
if b, err = p.Signature(); err != nil {
return "", err
}
b = append([]byte{0x17}, b...) b = append([]byte{0x17}, b...)
csum := hash.Checksum(b) csum := hash.Checksum(b)
b = append(b, csum...) b = append(b, csum...)
address := crypto.Base58Encode(b) address := crypto.Base58Encode(b)
return address, nil return address
} }
// Verify returns true if the signature is valid and corresponds // Verify returns true if the signature is valid and corresponds

View file

@ -42,7 +42,7 @@ func TestDecodeFromString(t *testing.T) {
func TestPubkeyToAddress(t *testing.T) { func TestPubkeyToAddress(t *testing.T) {
pubKey, err := NewPublicKeyFromString("031ee4e73a17d8f76dc02532e2620bcb12425b33c0c9f9694cc2caa8226b68cad4") pubKey, err := NewPublicKeyFromString("031ee4e73a17d8f76dc02532e2620bcb12425b33c0c9f9694cc2caa8226b68cad4")
assert.Nil(t, err) assert.Nil(t, err)
actual, _ := pubKey.Address() actual := pubKey.Address()
expected := "AUpGsNCHzSimeMRVPQfhwrVdiUp8Q2N2Qx" expected := "AUpGsNCHzSimeMRVPQfhwrVdiUp8Q2N2Qx"
assert.Equal(t, expected, actual) assert.Equal(t, expected, actual)
} }