From 97506fb48d6d8b8e605864c23b9baec76bf4540d Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Mon, 26 Aug 2024 21:11:40 +0300 Subject: [PATCH] keys: rework nep-2/wif encoding without bytes.Buffer It doesn't make any sense here and the length check was just a dead code. Signed-off-by: Roman Khimov --- pkg/crypto/keys/nep2.go | 16 ++++++---------- pkg/crypto/keys/wif.go | 11 +++++------ 2 files changed, 11 insertions(+), 16 deletions(-) diff --git a/pkg/crypto/keys/nep2.go b/pkg/crypto/keys/nep2.go index 7c4bd088d..f0c92eaa5 100644 --- a/pkg/crypto/keys/nep2.go +++ b/pkg/crypto/keys/nep2.go @@ -67,17 +67,13 @@ func NEP2Encrypt(priv *PrivateKey, passphrase string, params ScryptParams) (s st return s, err } - buf := new(bytes.Buffer) - buf.Write(nepHeader) - buf.WriteByte(nepFlag) - buf.Write(addrHash) - buf.Write(encrypted) + var buf = make([]byte, 0, len(nepHeader)+1+len(addrHash)+len(encrypted)) + buf = append(buf, nepHeader...) + buf = append(buf, nepFlag) + buf = append(buf, addrHash...) + buf = append(buf, encrypted...) - if buf.Len() != 39 { - return s, fmt.Errorf("invalid buffer length: expecting 39 bytes got %d", buf.Len()) - } - - return base58.CheckEncode(buf.Bytes()), nil + return base58.CheckEncode(buf), nil } // NEP2Decrypt decrypts an encrypted key using the given passphrase diff --git a/pkg/crypto/keys/wif.go b/pkg/crypto/keys/wif.go index f1fe96a91..67689cced 100644 --- a/pkg/crypto/keys/wif.go +++ b/pkg/crypto/keys/wif.go @@ -1,7 +1,6 @@ package keys import ( - "bytes" "fmt" "github.com/nspcc-dev/neo-go/pkg/encoding/base58" @@ -36,14 +35,14 @@ func WIFEncode(key []byte, version byte, compressed bool) (s string, err error) return s, fmt.Errorf("invalid private key length: %d", len(key)) } - buf := new(bytes.Buffer) - buf.WriteByte(version) - buf.Write(key) + var buf = make([]byte, 0, 1+len(key)+1) + buf = append(buf, version) + buf = append(buf, key...) if compressed { - buf.WriteByte(0x01) + buf = append(buf, 0x01) } - s = base58.CheckEncode(buf.Bytes()) + s = base58.CheckEncode(buf) return }