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 <roman@nspcc.ru>
This commit is contained in:
Roman Khimov 2024-08-26 21:11:40 +03:00
parent 9bc67f9da6
commit 97506fb48d
2 changed files with 11 additions and 16 deletions

View file

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

View file

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