bip32

package module
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 30, 2024 License: MIT Imports: 10 Imported by: 1

README

bip32-typesafe Go

bip32-typesafe is a type safe, cryptographically secure implementation of BIP 32 (hierarchical deterministic wallets).

Functions in this implementation let users avoid common mistakes/vulnerablities like:

  • mixing private keys and public keys: by type safety (for example, PrivateKey and PublicKey are different types)
  • side-channel attacks such as timing attacks: by making functions constant-time (taking the same amount of time regardless of the input)

Therefore, this is an easy-to-use and hard-to-misuse library that users can use with confidence.

Examples

package main

import (
	"crypto/rand"
	"fmt"
	"log"

	bip32 "github.com/koba-e964/bip32-typesafe"
)

func main() {
	// Generate random 32 bytes
	seed := make([]byte, 32)
	if _, err := rand.Read(seed); err != nil {
		panic(err)
	}

	master := bip32.NewMasterKey(seed)
	log.Println(master.PrivateKey())
	child0, err := master.NewChildKey(0) // master/0
	if err != nil {
		panic(err)
	}
	fmt.Println("master/0 =", child0.B58Serialize())
	childH0, err := master.NewChildKey(bip32.FirstHardenedChildIndex + 0) // master/0_H
	if err != nil {
		panic(err)
	}
	fmt.Println("master/0_H =", childH0.B58Serialize())
}

Documentation

Package info: https://pkg.go.dev/github.com/koba-e964/bip32-typesafe

Documentation

Overview

Package bip32 provides BIP 32 related functions.

Spec: https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki

Index

Constants

View Source
const FirstHardenedChildIndex uint32 = 0x80000000

FirstHardenedChildIndex is the first index of hardened child keys. Hardened child keys don't allow for public parent key -> public child key derivation, but provide more security than non-hardened child keys.

View Source
const KeyLengthInBytes = 82 // when serialized, public/private keys have this length

Variables

View Source
var (
	ErrorHardenedPublicChildKey               = errors.New("can't create a hardened child key from a public key")
	ErrorTooDeepKey                           = errors.New("depth can't be >= 256")
	ErrorInvalidKeyLength                     = errors.New("invalid key length")
	ErrorInvalidVersion                       = errors.New("version is invalid")
	ErrorInvalidPublicKey                     = errors.New("public key is invalid")
	ErrorInvalidPrivateKey                    = errors.New("private key is invalid")
	ErrorChecksumMismatch                     = errors.New("checksum mismatch")
	ErrorZeroDepthAndNonZeroParentFingerprint = errors.New("zero depth with non-zero parent fingerprint")
	ErrorZeroDepthAndNonZeroIndex             = errors.New("zero depth with non-zero index")
	ErrorPrivateKeyNotInRange                 = errors.New("private key not in range (1 <= p <= n-1)")
)

Functions

This section is empty.

Types

type PrivateKey

type PrivateKey struct {
	// contains filtered or unexported fields
}

PrivateKey is a private key.

func B58DeserializePrivateKey

func B58DeserializePrivateKey(encoded string) (*PrivateKey, error)

B58DeserializePrivateKey decodes a base58-encoded string and returns a PrivateKey.

func DeserializePrivateKey

func DeserializePrivateKey(data [KeyLengthInBytes]byte) (*PrivateKey, error)

DeserializePrivateKey reads a []byte and returns a PrivateKey.

func NewMasterKey

func NewMasterKey(seed []byte) *PrivateKey

NewMasterKey generates a new master private key with the given seed.

Example:

// the length of a seed should be between 128 and 512 bits;
// this length (32 bits) is too short and for illustration purpose only
seed, err := hex.DecodeString("01020304")
master := NewMasterKey(seed)

func (*PrivateKey) B58Serialize

func (p *PrivateKey) B58Serialize() string

B58Serialize returns the base58 representation of this PrivateKey.

func (*PrivateKey) ChainCode

func (p *PrivateKey) ChainCode() [32]byte

ChainCode returns the chain code of this PrivateKey. This value is used in derivation of child keys.

func (*PrivateKey) ChildNumber

func (p *PrivateKey) ChildNumber() uint32

ChildNumber returns the child index of this PrivateKey. If this PrivateKey is a master key, this function returns 0.

func (*PrivateKey) Depth

func (p *PrivateKey) Depth() byte

Depth returns the depth of this PrivateKey. If the depth is 0, this key is a master key.

func (*PrivateKey) GetPublicKey

func (p *PrivateKey) GetPublicKey() *PublicKey

GetPublicKey finds the corresponding PublicKey from this PrivateKey.

func (*PrivateKey) NewChildKey

func (p *PrivateKey) NewChildKey(childIdx uint32) (*PrivateKey, error)

NewChildKey derives a new child key from this PrivateKey. The following errors may be returned:

  • ErrorTooDeepKey: if this PrivateKey has depth 255
  • ErrorInvalidPrivateKey: if the derived private key satisfies parse_{256}(I_L) >= n or k_i = 0 (with probability < 2^{-127})

func (*PrivateKey) ParentFingerprint

func (p *PrivateKey) ParentFingerprint() [4]byte

ParentFingerprint returns the fingerprint of this PrivateKey's parent key. If this key is a master key, the fingerprint is filled with zero.

func (*PrivateKey) PrivateKey

func (p *PrivateKey) PrivateKey() secp256k1.Scalar

PrivateKey returns the private key of secp256k1 in this PrivateKey.

func (*PrivateKey) Serialize

func (p *PrivateKey) Serialize() [KeyLengthInBytes]byte

Serialize returns the []byte representation of this PrivateKey.

type PublicKey

type PublicKey struct {
	// contains filtered or unexported fields
}

PublicKey is a public key.

func B58DeserializePublicKey

func B58DeserializePublicKey(encoded string) (*PublicKey, error)

B58DeserializePublicKey decodes a base58-encoded string and returns a PublicKey.

func DeserializePublicKey

func DeserializePublicKey(data [KeyLengthInBytes]byte) (*PublicKey, error)

DeserializePublicKey reads a []byte and returns a PublicKey.

func MasterPublicKeyFromRaw

func MasterPublicKeyFromRaw(publicKey [33]byte, chainCode [32]byte) *PublicKey

MasterPublicKeyFromRaw returns a master public key for mainnet with the given public key and the chain code.

func (*PublicKey) B58Serialize

func (p *PublicKey) B58Serialize() string

B58Serialize returns the base58 representation of this PublicKey.

func (*PublicKey) ChainCode

func (p *PublicKey) ChainCode() [32]byte

ChainCode returns the chain code of this PublicKey. This value is used in derivation of child public keys.

func (*PublicKey) ChildNumber

func (p *PublicKey) ChildNumber() uint32

ChildNumber returns the child index of this PublicKey. If this PublicKey is a master key, this function returns 0.

func (*PublicKey) Depth

func (p *PublicKey) Depth() byte

Depth returns the depth of this PublicKey. If the depth is 0, this key is a master key.

func (*PublicKey) NewChildKey

func (p *PublicKey) NewChildKey(childIdx uint32) (*PublicKey, error)

NewChildKey derives a new child key from this PublicKey. The following errors may be returned:

  • ErrorHardenedPublicChildKey: if childIdx >= FirstHardenedChildIndex = 0x80000000
  • ErrorTooDeepKey: if this PublicKey has depth 255
  • ErrorInvalidPublicKey: if the derived public key satisfies parse_{256}(I_L) >= n (with probability < 2^{-127})

func (*PublicKey) ParentFingerprint

func (p *PublicKey) ParentFingerprint() [4]byte

ParentFingerprint returns the fingerprint of this PublicKey's parent key. If this key is a master key, the fingerprint is filled with zero.

func (*PublicKey) PublicKey

func (p *PublicKey) PublicKey() secp256k1.Compressed

PublicKey returns the public key of secp256k1 (a compressed point) in this PublicKey.

func (*PublicKey) Serialize

func (p *PublicKey) Serialize() [KeyLengthInBytes]byte

Serialize returns the []byte representation of this PublicKey.

Directories

Path Synopsis
Package base58 handles encoding/decoding of base58.
Package base58 handles encoding/decoding of base58.
Package secp256k1 implements secp256k1-related functions and types.
Package secp256k1 implements secp256k1-related functions and types.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL