2019-08-27 13:29:42 +00:00
|
|
|
package keys
|
2018-03-02 15:24:09 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
|
2019-12-25 12:05:54 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/encoding/base58"
|
2018-03-02 15:24:09 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2018-03-09 15:55:25 +00:00
|
|
|
// WIFVersion is the version used to decode and encode WIF keys.
|
2018-03-02 15:24:09 +00:00
|
|
|
WIFVersion = 0x80
|
|
|
|
)
|
|
|
|
|
|
|
|
// WIF represents a wallet import format.
|
|
|
|
type WIF struct {
|
|
|
|
// Version of the wallet import format. Default to 0x80.
|
|
|
|
Version byte
|
|
|
|
|
|
|
|
// Bool to determine if the WIF is compressed or not.
|
|
|
|
Compressed bool
|
|
|
|
|
|
|
|
// A reference to the PrivateKey which this WIF is created from.
|
|
|
|
PrivateKey *PrivateKey
|
|
|
|
|
|
|
|
// The string representation of the WIF.
|
|
|
|
S string
|
|
|
|
}
|
|
|
|
|
|
|
|
// WIFEncode encodes the given private key into a WIF string.
|
|
|
|
func WIFEncode(key []byte, version byte, compressed bool) (s string, err error) {
|
|
|
|
if version == 0x00 {
|
|
|
|
version = WIFVersion
|
|
|
|
}
|
|
|
|
if len(key) != 32 {
|
|
|
|
return s, fmt.Errorf("invalid private key length: %d", len(key))
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
buf.WriteByte(version)
|
|
|
|
buf.Write(key)
|
|
|
|
if compressed {
|
|
|
|
buf.WriteByte(0x01)
|
|
|
|
}
|
|
|
|
|
2019-12-25 12:05:54 +00:00
|
|
|
s = base58.CheckEncode(buf.Bytes())
|
2018-03-02 15:24:09 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-10-22 14:56:03 +00:00
|
|
|
// WIFDecode decodes the given WIF string into a WIF struct.
|
2018-03-02 15:24:09 +00:00
|
|
|
func WIFDecode(wif string, version byte) (*WIF, error) {
|
2019-12-25 12:05:54 +00:00
|
|
|
b, err := base58.CheckDecode(wif)
|
2018-03-02 15:24:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if version == 0x00 {
|
|
|
|
version = WIFVersion
|
|
|
|
}
|
|
|
|
if b[0] != version {
|
|
|
|
return nil, fmt.Errorf("invalid WIF version got %d, expected %d", b[0], version)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Derive the PrivateKey.
|
|
|
|
privKey, err := NewPrivateKeyFromBytes(b[1:33])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
w := &WIF{
|
|
|
|
Version: version,
|
|
|
|
PrivateKey: privKey,
|
|
|
|
S: wif,
|
|
|
|
}
|
|
|
|
|
2019-10-22 14:56:03 +00:00
|
|
|
// This is an uncompressed WIF.
|
2018-03-02 15:24:09 +00:00
|
|
|
if len(b) == 33 {
|
|
|
|
w.Compressed = false
|
|
|
|
return w, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(b) != 34 {
|
|
|
|
return nil, fmt.Errorf("invalid WIF length: %d expecting 34", len(b))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the compression flag.
|
|
|
|
if b[33] != 0x01 {
|
|
|
|
return nil, fmt.Errorf("invalid compression flag %d expecting %d", b[34], 0x01)
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Compressed = true
|
|
|
|
return w, nil
|
|
|
|
}
|