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 return s, err
} }
buf := new(bytes.Buffer) var buf = make([]byte, 0, len(nepHeader)+1+len(addrHash)+len(encrypted))
buf.Write(nepHeader) buf = append(buf, nepHeader...)
buf.WriteByte(nepFlag) buf = append(buf, nepFlag)
buf.Write(addrHash) buf = append(buf, addrHash...)
buf.Write(encrypted) buf = append(buf, encrypted...)
if buf.Len() != 39 { return base58.CheckEncode(buf), nil
return s, fmt.Errorf("invalid buffer length: expecting 39 bytes got %d", buf.Len())
}
return base58.CheckEncode(buf.Bytes()), nil
} }
// NEP2Decrypt decrypts an encrypted key using the given passphrase // NEP2Decrypt decrypts an encrypted key using the given passphrase

View file

@ -1,7 +1,6 @@
package keys package keys
import ( import (
"bytes"
"fmt" "fmt"
"github.com/nspcc-dev/neo-go/pkg/encoding/base58" "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)) return s, fmt.Errorf("invalid private key length: %d", len(key))
} }
buf := new(bytes.Buffer) var buf = make([]byte, 0, 1+len(key)+1)
buf.WriteByte(version) buf = append(buf, version)
buf.Write(key) buf = append(buf, key...)
if compressed { if compressed {
buf.WriteByte(0x01) buf = append(buf, 0x01)
} }
s = base58.CheckEncode(buf.Bytes()) s = base58.CheckEncode(buf)
return return
} }