/bip32-typesafe

A Go implementation of BIP 32 which is type-safe and a best-effort attempt at timing-attack resistance

Primary LanguageGoMIT LicenseMIT

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