[#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
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)
}

View file

@ -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
}

View file

@ -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))
}