[#27] util/signature: Fix staticcheck warning

Signed-off-by: Evgenii Stratonikov <e.stratonikov@yadro.com>
This commit is contained in:
Evgenii Stratonikov 2023-05-03 14:09:17 +03:00
parent a6e2ab3845
commit 2aa3ee46e7
3 changed files with 19 additions and 15 deletions

View file

@ -4,26 +4,30 @@ import "sync"
const poolSliceMaxSize = 64 * 1024 const poolSliceMaxSize = 64 * 1024
type buffer struct {
data []byte
}
var buffersPool = sync.Pool{ var buffersPool = sync.Pool{
New: func() any { New: func() any {
return make([]byte, 0) return new(buffer)
}, },
} }
func newBufferFromPool(size int) []byte { func newBufferFromPool(size int) *buffer {
result := buffersPool.Get().([]byte) result := buffersPool.Get().(*buffer)
if cap(result) < size { if cap(result.data) < size {
result = make([]byte, size) result.data = make([]byte, size)
} else { } else {
result = result[:size] result.data = result.data[:size]
} }
return result return result
} }
func returnBufferToPool(buf []byte) { func returnBufferToPool(buf *buffer) {
if cap(buf) > poolSliceMaxSize { if cap(buf.data) > poolSliceMaxSize {
return return
} }
buf = buf[:0] buf.data = buf.data[:0]
buffersPool.Put(buf) buffersPool.Put(buf)
} }

View file

@ -38,7 +38,7 @@ func SignDataWithHandler(key *ecdsa.PrivateKey, src DataSource, handler KeySigna
buffer := newBufferFromPool(src.SignedDataSize()) buffer := newBufferFromPool(src.SignedDataSize())
defer returnBufferToPool(buffer) defer returnBufferToPool(buffer)
data, err := src.ReadSignedData(buffer) data, err := src.ReadSignedData(buffer.data)
if err != nil { if err != nil {
return err return err
} }
@ -67,7 +67,7 @@ func VerifyDataWithSource(dataSrc DataSource, sigSrc KeySignatureSource, opts ..
buffer := newBufferFromPool(dataSrc.SignedDataSize()) buffer := newBufferFromPool(dataSrc.SignedDataSize())
defer returnBufferToPool(buffer) defer returnBufferToPool(buffer)
data, err := dataSrc.ReadSignedData(buffer) data, err := dataSrc.ReadSignedData(buffer.data)
if err != nil { if err != nil {
return err return err
} }

View file

@ -37,8 +37,8 @@ func verify(cfg *cfg, data []byte, sig *refs.Signature) error {
case refs.ECDSA_RFC6979_SHA256_WALLET_CONNECT: case refs.ECDSA_RFC6979_SHA256_WALLET_CONNECT:
buffer := newBufferFromPool(base64.StdEncoding.EncodedLen(len(data))) buffer := newBufferFromPool(base64.StdEncoding.EncodedLen(len(data)))
defer returnBufferToPool(buffer) defer returnBufferToPool(buffer)
base64.StdEncoding.Encode(buffer, data) base64.StdEncoding.Encode(buffer.data, data)
if !walletconnect.Verify(pub, buffer, sig.GetSign()) { if !walletconnect.Verify(pub, buffer.data, sig.GetSign()) {
return crypto.ErrInvalidSignature return crypto.ErrInvalidSignature
} }
return nil return nil
@ -56,8 +56,8 @@ func sign(cfg *cfg, key *ecdsa.PrivateKey, data []byte) ([]byte, error) {
case refs.ECDSA_RFC6979_SHA256_WALLET_CONNECT: case refs.ECDSA_RFC6979_SHA256_WALLET_CONNECT:
buffer := newBufferFromPool(base64.StdEncoding.EncodedLen(len(data))) buffer := newBufferFromPool(base64.StdEncoding.EncodedLen(len(data)))
defer returnBufferToPool(buffer) defer returnBufferToPool(buffer)
base64.StdEncoding.Encode(buffer, data) base64.StdEncoding.Encode(buffer.data, data)
return walletconnect.Sign(key, buffer) return walletconnect.Sign(key, buffer.data)
default: default:
panic(fmt.Sprintf("unsupported scheme %s", cfg.scheme)) panic(fmt.Sprintf("unsupported scheme %s", cfg.scheme))
} }