|
6 | 6 | "crypto/rand"
|
7 | 7 | "crypto/sha512"
|
8 | 8 | "encoding/binary"
|
9 |
| - "encoding/hex" |
10 | 9 | "fmt"
|
11 | 10 | "io"
|
12 | 11 | "strconv"
|
@@ -133,7 +132,7 @@ func ExtendedKeyFromStr(s string) (ExtendedKey, error) {
|
133 | 132 | // Fall back to BIP-0032 format
|
134 | 133 | bip32Key, b32err := bip32.B58Deserialize(s)
|
135 | 134 | if b32err != nil {
|
136 |
| - return ExtendedKey{}, errors.Wrap(err, "decode xkey hex string") |
| 135 | + return ExtendedKey{}, errors.Wrap(err, "base58 deserialize") |
137 | 136 | }
|
138 | 137 |
|
139 | 138 | return fromBIP32(bip32Key)
|
@@ -218,7 +217,9 @@ func (k ExtendedKey) String() string {
|
218 | 217 |
|
219 | 218 | // String58 returns the key formatted as base 58 text.
|
220 | 219 | func (k ExtendedKey) String58() string {
|
221 |
| - return BIP0276Encode58(k.Network, ExtendedKeyURLPrefix, k.Bytes()) |
| 220 | + // return BIP0276Encode58(k.Network, ExtendedKeyURLPrefix, k.Bytes()) |
| 221 | + bip32 := k.ToBIP32() |
| 222 | + return bip32.String() |
222 | 223 | }
|
223 | 224 |
|
224 | 225 | // SetString decodes a key from hex text.
|
@@ -460,22 +461,14 @@ func (k *ExtendedKey) UnmarshalJSON(data []byte) error {
|
460 | 461 | // MarshalText returns the text encoding of the extended key.
|
461 | 462 | // Implements encoding.TextMarshaler interface.
|
462 | 463 | func (k ExtendedKey) MarshalText() ([]byte, error) {
|
463 |
| - b := k.Bytes() |
464 |
| - result := make([]byte, hex.EncodedLen(len(b))) |
465 |
| - hex.Encode(result, b) |
466 |
| - return result, nil |
| 464 | + s := k.String58() |
| 465 | + return []byte(s), nil |
467 | 466 | }
|
468 | 467 |
|
469 | 468 | // UnmarshalText parses a text encoded extended key and sets the value of this object.
|
470 | 469 | // Implements encoding.TextUnmarshaler interface.
|
471 | 470 | func (k *ExtendedKey) UnmarshalText(text []byte) error {
|
472 |
| - b := make([]byte, hex.DecodedLen(len(text))) |
473 |
| - _, err := hex.Decode(b, text) |
474 |
| - if err != nil { |
475 |
| - return err |
476 |
| - } |
477 |
| - |
478 |
| - return k.SetBytes(b) |
| 471 | + return k.SetString58(string(text)) |
479 | 472 | }
|
480 | 473 |
|
481 | 474 | // MarshalBinary returns the binary encoding of the extended key.
|
@@ -526,6 +519,30 @@ func (k *ExtendedKey) setFromBIP32(old *bip32.Key) error {
|
526 | 519 | return nil
|
527 | 520 | }
|
528 | 521 |
|
| 522 | +func (k ExtendedKey) ToBIP32() bip32.Key { |
| 523 | + var result bip32.Key |
| 524 | + |
| 525 | + result.FingerPrint = make([]byte, 4) |
| 526 | + copy(result.FingerPrint, k.FingerPrint[:]) |
| 527 | + result.ChildNumber = make([]byte, 4) |
| 528 | + binary.BigEndian.PutUint32(result.ChildNumber, k.Index) |
| 529 | + result.ChainCode = make([]byte, 32) |
| 530 | + copy(result.ChainCode, k.ChainCode[:]) |
| 531 | + result.Depth = k.Depth |
| 532 | + if k.KeyValue[0] == 0 { |
| 533 | + result.IsPrivate = true |
| 534 | + result.Version = bip32.PrivateWalletVersion |
| 535 | + result.Key = make([]byte, 32) |
| 536 | + copy(result.Key, k.KeyValue[1:]) |
| 537 | + } else { |
| 538 | + result.Version = bip32.PublicWalletVersion |
| 539 | + result.Key = make([]byte, 33) |
| 540 | + copy(result.Key, k.KeyValue[:]) |
| 541 | + } |
| 542 | + |
| 543 | + return result |
| 544 | +} |
| 545 | + |
529 | 546 | // read reads just the basic data of the extended key.
|
530 | 547 | func (k *ExtendedKey) read(r io.Reader) error {
|
531 | 548 | var b [1]byte
|
|
0 commit comments