bip38

package
v0.0.0-...-fb34c67 Latest Latest
Warning

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

Go to latest
Published: Jan 24, 2019 License: MIT Imports: 17 Imported by: 0

README

gitlab.com/acid.sploit/gobip38/bip38

go get gitlab.com/acid.sploit/gobip38/bip38

This repository contains a Golang BIP38 package exporting all functions needed to encrypt WIF keys, decrypt and generate new BIP38 keys, complying with the BIP38 spec.

  • Encrypt WIF key (Compressed & Uncompressed)
  • Generate new WIF key and BIP38 encrypt with password
  • Generate new intermediary passphrase (EC Multiplied)
  • Generate new BIP38 key from intermediary passphrase (EC Multiplied)
  • Decrypt BIP38 secret (Non EC Multiplied & EC Multiplied) (Compressed & Uncompressed)
// Encrypt WIF with password
func Encrypt(wif *bchutil.WIF, password string) (string, error) 

// Generate intermediate passphrase to generate EC Multiplied keys
func GeneratePassphrase(password string, salt []byte) (string, error)

// Generate EC Multiplied BIP38 key (generate BIP38 key without knowing the private key)
func EncryptECMultiply(intermediatePassphrase string) (string, string, error)

// Catchall Decrypt function - automatically detect ECM or NonECM
func Decrypt(encryptedKey string, password string) (*bchutil.WIF, error)

// Decrypt BIP38 keys where EC Multiplication is not used
func DecryptNonECMultiplied(b []byte, password string) (*bchutil.WIF, error)

// Decrypt BIP38 keys where EC Multiplication is used
func DecryptECMuliplied(b []byte, passphrase string) (*bchutil.WIF, error)

// Return full SHA256(SHA256()) hash
func Hash256(buf []byte) []byte

// Return last 4 bytes of a SHA256(SHA256()) hash
func CheckSum(buf []byte) []byte

Example code

Decrypt()
func decryptBip38(bip38String *string, password *string) (*bchutil.WIF, error) {
	b := base58.Decode(*bip38String)
	wif, err := bip38.Decrypt(*bip38String, *password)
	if err != nil {
		return nil, err
	}

	return wif, nil
}

DecryptNonECMultiplied()
func decryptBip38NonECM(bip38String *string, password *string) (*bchutil.WIF, error) {
	byteKey := base58.Decode(*bip38String)
	wif, err := DecryptNonECMultiplied(byteKey, password)
	if err != nil {
		return nil, err
	}

	return wif, nil
}

Encrypt()
func encryptWif(wifString *string, password *string) (string, error) {
	wif, err := bchutil.DecodeWIF(*wifString)
	if err != nil {
		return "", err
	}

	encryptWif, err := bip38.Encrypt(wif, *password)
	if err != nil {
		return "", err
	}

	return encryptWif, nil
}
GeneratePassphrase()
salt := make([]byte, 4)
rand.Read(salt)

intermediatePassphraseString, _ := bip38.GeneratePassphrase(*passPtr, salt)
EncryptECMultiply()
bip38, addr, err := bip38.EncryptECMultiply(intermediatePassphraseString)
if err != nil {
	log.Fatal(err)
	return
}

Documentation

Index

Constants

View Source
const PubKeyBytesLenCompressed = 33

Variables

View Source
var (
	Base58DecodeError          = errors.New("Could not decode encryped BIP38 key.")
	BIP38UnsupportedKey        = errors.New("Unsupported BIP38 key. Encrypted key possibly corrupted.")
	BIP38ChecksumMismatch      = errors.New("BIP38 checksum mismatch. Encrypted key possibly corrupted.")
	FaultyPassword             = errors.New("Bitcoin address checksum mismatch. Faulty password!")
	PassfactorToPasspointError = errors.New("Could not derive passpoint from passfactor.")
	BigIntError                = errors.New("Failed to create Int for N")
)

Functions

func CheckSum

func CheckSum(buf []byte) []byte

Return first 4 bytes of a SHA256(SHA256()) hash

func CheckWIF

func CheckWIF(wif string) (valid bool, err error)

CheckWIF checks that string wif is a valid Wallet Import Format or Wallet Import Format Compressed string. If it is not, err is populated with the reason.

func Decrypt

func Decrypt(encryptedKey string, password string) (*bchutil.WIF, error)

Catchall Decrypt function

func DecryptECMuliplied

func DecryptECMuliplied(b []byte, passphrase string) (*bchutil.WIF, error)

Decrypt BIP38 byte slice which has the ECMultiply flag set

func DecryptNonECMultiplied

func DecryptNonECMultiplied(b []byte, password string) (*bchutil.WIF, error)

Decrypt BIP38 byte slice which does not have the ECMultiply flag set

func Encrypt

func Encrypt(wif *bchutil.WIF, password string) (string, error)

Encrypt WIF (prefix 0x42)

func EncryptECMultiply

func EncryptECMultiply(intermediatePassphrase string) (string, string, error)

Generate BIP38 secret from intermediate passphrase with EC multiply (prefix 0x43). BIP38 Printer code. https://github.com/bitcoin/bips/blob/master/bip-0038.mediawiki#encryption-when-ec-multiply-mode-is-used

func GeneratePassphrase

func GeneratePassphrase(password string, salt []byte) (string, error)

Generate intermediate passphrase from password + salt

func Hash256

func Hash256(buf []byte) []byte

Return full SHA256(SHA256()) hash

Types

type EllipticCurve

type EllipticCurve struct {
	A *big.Int
	B *big.Int
	P *big.Int
	G Point
	N *big.Int
	H *big.Int
}
y**2 = x**3 + a*x + b  % p

EllipticCurve represents the parameters of a short Weierstrass equation elliptic curve.

func (*EllipticCurve) Add

func (ec *EllipticCurve) Add(P, Q Point) (R Point)

Add computes R = P + Q on EllipticCurve ec.

func (*EllipticCurve) Decompress

func (ec *EllipticCurve) Decompress(x *big.Int, ylsb uint) (P Point, err error)

Decompress decompresses coordinate x and ylsb (y's least significant bit) into a Point P on EllipticCurve ec.

func (*EllipticCurve) IsInfinity

func (ec *EllipticCurve) IsInfinity(P Point) bool

IsInfinity checks if point P is infinity on EllipticCurve ec.

func (*EllipticCurve) IsOnCurve

func (ec *EllipticCurve) IsOnCurve(P Point) bool

IsOnCurve checks if point P is on EllipticCurve ec.

func (*EllipticCurve) ScalarBaseMult

func (ec *EllipticCurve) ScalarBaseMult(k *big.Int) (Q Point)

ScalarBaseMult computes Q = k * G on EllipticCurve ec.

func (*EllipticCurve) ScalarMult

func (ec *EllipticCurve) ScalarMult(k *big.Int, P Point) (Q Point)

ScalarMult computes Q = k * P on EllipticCurve ec.

type Point

type Point struct {
	X *big.Int
	Y *big.Int
}

Point represents a point on an EllipticCurve.

type PrivateKey

type PrivateKey struct {
	PublicKey
	D *big.Int
}

PrivateKey represents a Bitcoin private key.

func GenerateKey

func GenerateKey(rand io.Reader) (priv PrivateKey, err error)

GenerateKey generates a public and private key pair using random source rand.

func NewPrivateKey

func NewPrivateKey(d *big.Int) *PrivateKey

func (*PrivateKey) FromBytes

func (priv *PrivateKey) FromBytes(b []byte) (err error)

FromBytes converts a 32-byte byte slice to a Bitcoin private key and derives the corresponding Bitcoin public key.

func (*PrivateKey) FromWIF

func (priv *PrivateKey) FromWIF(wif string) (err error)

FromWIF converts a Wallet Import Format string to a Bitcoin private key and derives the corresponding Bitcoin public key.

func (*PrivateKey) ToBytes

func (priv *PrivateKey) ToBytes() (b []byte)

ToBytes converts a Bitcoin private key to a 32-byte byte slice.

func (*PrivateKey) ToWIF

func (priv *PrivateKey) ToWIF() (wif string)

ToWIF converts a Bitcoin private key to a Wallet Import Format string.

func (*PrivateKey) ToWIFC

func (priv *PrivateKey) ToWIFC() (wifc string)

ToWIFC converts a Bitcoin private key to a Wallet Import Format string with the public key compressed flag.

type PublicKey

type PublicKey struct {
	Point
}

PublicKey represents a Bitcoin public key.

func (*PublicKey) FromBytes

func (pub *PublicKey) FromBytes(b []byte) (err error)

FromBytes converts a byte slice (either with or without point compression) to a Bitcoin public key.

func (*PublicKey) ToAddress

func (pub *PublicKey) ToAddress() (address string)

ToAddress converts a Bitcoin public key to a compressed Bitcoin address string.

func (*PublicKey) ToAddressUncompressed

func (pub *PublicKey) ToAddressUncompressed() (address string)

ToAddressUncompressed converts a Bitcoin public key to an uncompressed Bitcoin address string.

func (*PublicKey) ToBytes

func (pub *PublicKey) ToBytes() (b []byte)

ToBytes converts a Bitcoin public key to a 33-byte byte slice with point compression.

func (*PublicKey) ToBytesUncompressed

func (pub *PublicKey) ToBytesUncompressed() (b []byte)

ToBytesUncompressed converts a Bitcoin public key to a 65-byte byte slice without point compression.

Jump to

Keyboard shortcuts

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