From 2aa3ee46e7ccc55868b9fb06de968cf753719471 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Wed, 3 May 2023 14:09:17 +0300 Subject: [PATCH] [#27] util/signature: Fix staticcheck warning Signed-off-by: Evgenii Stratonikov --- util/signature/buffer.go | 22 +++++++++++++--------- util/signature/data.go | 4 ++-- util/signature/options.go | 8 ++++---- 3 files changed, 19 insertions(+), 15 deletions(-) diff --git a/util/signature/buffer.go b/util/signature/buffer.go index 2a7f1e0..c2fa855 100644 --- a/util/signature/buffer.go +++ b/util/signature/buffer.go @@ -4,26 +4,30 @@ import "sync" const poolSliceMaxSize = 64 * 1024 +type buffer struct { + data []byte +} + var buffersPool = sync.Pool{ New: func() any { - return make([]byte, 0) + return new(buffer) }, } -func newBufferFromPool(size int) []byte { - result := buffersPool.Get().([]byte) - if cap(result) < size { - result = make([]byte, size) +func newBufferFromPool(size int) *buffer { + result := buffersPool.Get().(*buffer) + if cap(result.data) < size { + result.data = make([]byte, size) } else { - result = result[:size] + result.data = result.data[:size] } return result } -func returnBufferToPool(buf []byte) { - if cap(buf) > poolSliceMaxSize { +func returnBufferToPool(buf *buffer) { + if cap(buf.data) > poolSliceMaxSize { return } - buf = buf[:0] + buf.data = buf.data[:0] buffersPool.Put(buf) } diff --git a/util/signature/data.go b/util/signature/data.go index d2bf1dc..1db46dc 100644 --- a/util/signature/data.go +++ b/util/signature/data.go @@ -38,7 +38,7 @@ func SignDataWithHandler(key *ecdsa.PrivateKey, src DataSource, handler KeySigna buffer := newBufferFromPool(src.SignedDataSize()) defer returnBufferToPool(buffer) - data, err := src.ReadSignedData(buffer) + data, err := src.ReadSignedData(buffer.data) if err != nil { return err } @@ -67,7 +67,7 @@ func VerifyDataWithSource(dataSrc DataSource, sigSrc KeySignatureSource, opts .. buffer := newBufferFromPool(dataSrc.SignedDataSize()) defer returnBufferToPool(buffer) - data, err := dataSrc.ReadSignedData(buffer) + data, err := dataSrc.ReadSignedData(buffer.data) if err != nil { return err } diff --git a/util/signature/options.go b/util/signature/options.go index a8a3522..b9bf68d 100644 --- a/util/signature/options.go +++ b/util/signature/options.go @@ -37,8 +37,8 @@ func verify(cfg *cfg, data []byte, sig *refs.Signature) error { case refs.ECDSA_RFC6979_SHA256_WALLET_CONNECT: buffer := newBufferFromPool(base64.StdEncoding.EncodedLen(len(data))) defer returnBufferToPool(buffer) - base64.StdEncoding.Encode(buffer, data) - if !walletconnect.Verify(pub, buffer, sig.GetSign()) { + base64.StdEncoding.Encode(buffer.data, data) + if !walletconnect.Verify(pub, buffer.data, sig.GetSign()) { return crypto.ErrInvalidSignature } return nil @@ -56,8 +56,8 @@ func sign(cfg *cfg, key *ecdsa.PrivateKey, data []byte) ([]byte, error) { case refs.ECDSA_RFC6979_SHA256_WALLET_CONNECT: buffer := newBufferFromPool(base64.StdEncoding.EncodedLen(len(data))) defer returnBufferToPool(buffer) - base64.StdEncoding.Encode(buffer, data) - return walletconnect.Sign(key, buffer) + base64.StdEncoding.Encode(buffer.data, data) + return walletconnect.Sign(key, buffer.data) default: panic(fmt.Sprintf("unsupported scheme %s", cfg.scheme)) }