// String returns the extended key as a human-readable base58-encoded string. func (k *ExtendedKey) String() (string, error) { if len(k.key) == 0 { return "", fmt.Errorf("zeroed extended key") } var childNumBytes [4]byte depthByte := byte(k.depth % 256) binary.BigEndian.PutUint32(childNumBytes[:], k.childNum) // The serialized format is: // version (4) || depth (1) || parent fingerprint (4)) || // child num (4) || chain code (32) || key data (33) || checksum (4) serializedBytes := make([]byte, 0, serializedKeyLen+4) serializedBytes = append(serializedBytes, k.version...) serializedBytes = append(serializedBytes, depthByte) serializedBytes = append(serializedBytes, k.parentFP...) serializedBytes = append(serializedBytes, childNumBytes[:]...) serializedBytes = append(serializedBytes, k.chainCode...) if k.isPrivate { serializedBytes = append(serializedBytes, 0x00) serializedBytes = append(serializedBytes, k.key...) } else { serializedBytes = append(serializedBytes, k.pubKeyBytes()...) } checkSum := chainhash.HashFuncB(chainhash.HashFuncB(serializedBytes))[:4] serializedBytes = append(serializedBytes, checkSum...) return base58.Encode(serializedBytes), nil }
// NewKeyFromString returns a new extended key instance from a base58-encoded // extended key. func NewKeyFromString(key string) (*ExtendedKey, error) { // The base58-decoded extended key must consist of a serialized payload // plus an additional 4 bytes for the checksum. decoded := base58.Decode(key) if len(decoded) != serializedKeyLen+4 { return nil, ErrInvalidKeyLen } // The serialized format is: // version (4) || depth (1) || parent fingerprint (4)) || // child num (4) || chain code (32) || key data (33) || checksum (4) // Split the payload and checksum up and ensure the checksum matches. payload := decoded[:len(decoded)-4] checkSum := decoded[len(decoded)-4:] expectedCheckSum := chainhash.HashFuncB(chainhash.HashFuncB(payload))[:4] if !bytes.Equal(checkSum, expectedCheckSum) { return nil, ErrBadChecksum } // Deserialize each of the payload fields. version := payload[:4] depth := uint16(payload[4:5][0]) parentFP := payload[5:9] childNum := binary.BigEndian.Uint32(payload[9:13]) chainCode := payload[13:45] keyData := payload[45:78] // The key data is a private key if it starts with 0x00. Serialized // compressed pubkeys either start with 0x02 or 0x03. isPrivate := keyData[0] == 0x00 if isPrivate { // Ensure the private key is valid. It must be within the range // of the order of the secp256k1 curve and not be 0. keyData = keyData[1:] keyNum := new(big.Int).SetBytes(keyData) if keyNum.Cmp(btcec.S256().N) >= 0 || keyNum.Sign() == 0 { return nil, ErrUnusableSeed } } else { // Ensure the public key parses correctly and is actually on the // secp256k1 curve. _, err := btcec.ParsePubKey(keyData, btcec.S256()) if err != nil { return nil, err } } return newExtendedKey(version, keyData, chainCode, parentFP, depth, childNum, isPrivate), nil }
// String creates the Wallet Import Format string encoding of a WIF structure. // See DecodeWIF for a detailed breakdown of the format and requirements of // a valid WIF string. func (w *WIF) String() string { // Precalculate size. Maximum number of bytes before base58 encoding // is two bytes for the network, one byte for the ECDSA type, 32 bytes // of private key and finally four bytes of checksum. encodeLen := 2 + 1 + 32 + 4 a := make([]byte, 0, encodeLen) a = append(a, w.netID[:]...) a = append(a, byte(w.ecType)) a = append(a, w.PrivKey.Serialize()...) cksum := chainhash.HashFuncB(a) a = append(a, cksum[:4]...) return base58.Encode(a) }
// DecodeWIF creates a new WIF structure by decoding the string encoding of // the import format. // // The WIF string must be a base58-encoded string of the following byte // sequence: // // * 2 bytes to identify the network, must be 0x80 for mainnet or 0xef for // either testnet or the regression test network // * 1 extra byte // * 32 bytes of a binary-encoded, big-endian, zero-padded private key // * 4 bytes of checksum, must equal the first four bytes of the double SHA256 // of every byte before the checksum in this sequence // // If the base58-decoded byte sequence does not match this, DecodeWIF will // return a non-nil error. ErrMalformedPrivateKey is returned when the WIF // is of an impossible length. ErrChecksumMismatch is returned if the // expected WIF checksum does not match the calculated checksum. func DecodeWIF(wif string) (*WIF, error) { decoded := base58.Decode(wif) decodedLen := len(decoded) if decodedLen != 39 { return nil, ErrMalformedPrivateKey } // Checksum is first four bytes of hash of the identifier byte // and privKey. Verify this matches the final 4 bytes of the decoded // private key. cksum := chainhash.HashFuncB(decoded[:decodedLen-4]) if !bytes.Equal(cksum[:4], decoded[decodedLen-4:]) { return nil, address.ErrChecksumMismatch } netID := [2]byte{decoded[0], decoded[1]} privKeyBytes := decoded[3 : 3+btcec.PrivKeyBytesLen] privKey, _ := btcec.PrivKeyFromBytes(curve, privKeyBytes) return &WIF{0, *privKey, netID}, nil }
// checksum: first four bytes of hash^2 func checksum(input []byte) (cksum [4]byte) { h := chainhash.HashFuncB(input) h2 := chainhash.HashFuncB(h[:]) copy(cksum[:], h2[:4]) return }
// Hash160 calculates the hash ripemd160(hash256(b)). func Hash160(buf []byte) []byte { return calcHash(chainhash.HashFuncB(buf), ripemd160.New()) }