bmt

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jul 10, 2024 License: MIT Imports: 23 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	OutOfRangeError = &PrivateKeyError{Message: "scalar is out of range"}
)

Functions

func CreateWallets

func CreateWallets(n int, path string)

func DoubleSHA256

func DoubleSHA256(b []byte) []byte

DoubleSHA256 calculates the SHA256 hash of the input byte slice twice.

Parameter:

  • b: input byte slice to be hashed.

Returns: The double SHA256 hashed byte slice.

func IsOdd

func IsOdd(n *big.Int) bool

IsOdd checks if the given big.Int is odd.

It takes a pointer to a big.Int as a parameter. It returns a boolean indicating whether the number is odd or not.

func ModInverse

func ModInverse(n, mod *big.Int) *big.Int

ModInverse calculates the modular inverse of a number.

It takes two parameters: n and mod, both of type *big.Int. The function returns a pointer to a new *big.Int representing the modular inverse of n modulo mod.

func Ripemd160SHA256

func Ripemd160SHA256(b []byte) []byte

Ripemd160SHA256 computes the RIPEMD160 hash of the SHA-256 hash of the input byte slice.

Parameter:

  • b: input byte slice to be hashed.

Returns: The RIPEMD160 hashed byte slice.

func Root

func Root(args []string) error

func ValidKey

func ValidKey(scalar *big.Int) bool

ValidKey checks if the given big.Int scalar is a valid key.

Parameters:

  • scalar: a pointer to a big.Int representing the scalar value.

Returns:

  • bool: true if the scalar is valid, false otherwise.

func VerifyMessage

func VerifyMessage(message *BitcoinMessage, electrum bool) (verified bool, pubkey string, result string, err error)

VerifyMessage verifies a signed message using the provided address, message, signature, and electrum flag.

Parameters:

  • address: the address used to sign the message.
  • message: the message to be verified.
  • signature: the signature to verify the message.
  • electrum: a flag indicating whether to use the electrum signature format.

Returns:

  • bool: true if the message is verified, false otherwise.
  • string: the hex-encoded public key.
  • string: a message indicating whether the message was verified or not.
  • error: an error if any occurred during the verification process.

Types

type BitcoinMessage

type BitcoinMessage struct {
	Address   string
	Data      string
	Signature []byte
}

func SignMessage

func SignMessage(pk *PrivateKey, addrType, message string, deterministic, electrum bool) (*BitcoinMessage, error)

SignMessage generates a Bitcoin message signature using the provided private key, address type, message, deterministic flag, and electrum flag.

Parameters:

  • pk: A pointer to a PrivateKey struct representing the private key. Compressed private key will produce compressed public key and address. Uncompressed private key will only produce one address type - uncompressed legacy address
  • addrType: A string representing the address type. It can be either p2pkh (compressed and uncompressed), p2wpkh-p2sh or p2wpkh (only compressed).
  • message: A string representing the message.
  • deterministic: A boolean indicating whether the signature should be deterministic. If set to true, each unique combination of private key and message will yield only one signature
  • electrum: A boolean indicating whether the signature should be in Electrum format.

Returns:

  • A pointer to a BitcoinMessage struct representing the signed message.
  • An error if there was a problem signing the message.

type CreateWalletCommand

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

func NewCreateWalletCommand

func NewCreateWalletCommand() *CreateWalletCommand

func (*CreateWalletCommand) Init

func (cwc *CreateWalletCommand) Init(args []string) error

func (*CreateWalletCommand) Name

func (cwc *CreateWalletCommand) Name() string

func (*CreateWalletCommand) Run

func (cwc *CreateWalletCommand) Run() error

type JacobianPoint

type JacobianPoint struct {
	X *big.Int
	Y *big.Int
	Z *big.Int
}

func NewJacobianPoint

func NewJacobianPoint(x, y, z *big.Int) *JacobianPoint

NewJacobianPoint creates a new JacobianPoint with the given coordinates.

Parameters:

  • x: a pointer to a big.Int representing the x-coordinate.
  • y: a pointer to a big.Int representing the y-coordinate.
  • z: a pointer to a big.Int representing the z-coordinate.

Returns:

  • a pointer to a JacobianPoint struct representing the new point.

func (*JacobianPoint) Add

func (pt *JacobianPoint) Add(p, q *JacobianPoint) *JacobianPoint

Add performs elliptic curve point addition in Jacobian coordinates for the secp256k1 curve.

It takes two JacobianPoint points p and q as input parameters and returns a JacobianPoint point.

Parameters:

  • p: a pointer to a JacobianPoint representing the first point.
  • q: a pointer to a JacobianPoint representing the second point.

Returns:

  • a pointer to a JacobianPoint representing the sum of p and q.

Fast Prime Field Elliptic Curve Cryptography with 256 Bit Primes Shay Gueron, Vlad Krasnov https://eprint.iacr.org/2013/816.pdf page 4

func (*JacobianPoint) Dbl

Dbl performs a point doubling operation in the elliptic curve cryptography with 256 Bit Primes.

Parameter:

  • p: a pointer to a JacobianPoint struct representing the point to be doubled.

Returns: A pointer to a JacobianPoint struct representing the result of the point doubling operation.

Fast Prime Field Elliptic Curve Cryptography with 256 Bit Primes Shay Gueron, Vlad Krasnov https://eprint.iacr.org/2013/816.pdf page 4

func (*JacobianPoint) Eq

func (pt *JacobianPoint) Eq(q *JacobianPoint) bool

Eq compares the current JacobianPoint with another JacobianPoint.

Parameters:

  • q: the JacobianPoint to compare with.

Returns:

  • bool: true if the points are equal, false otherwise.

func (*JacobianPoint) Mul

func (pt *JacobianPoint) Mul(scalar *big.Int, p *JacobianPoint) *JacobianPoint

Mul performs elliptic curve multiplication.

It takes two parameters:

  • scalar: a pointer to a big.Int representing the scalar value.
  • p: a pointer to a JacobianPoint representing the point to be multiplied.

It returns a pointer to a JacobianPoint representing the result of the multiplication.

https://paulmillr.com/posts/noble-secp256k1-fast-ecc/#fighting-timing-attacks

func (*JacobianPoint) String

func (pt *JacobianPoint) String() string

String returns a string representation of the JacobianPoint struct.

It returns a string in the format "(x=<X>, y=<Y>, z=<Z>)", where <X>, <Y> and <Z> are the string representations of the X, Y and Z coordinates of the JacobianPoint.

func (*JacobianPoint) ToAffine

func (pt *JacobianPoint) ToAffine() *Point

ToAffine converts a point from Jacobian coordinates to affine coordinates.

Parameter:

  • p: the point in Jacobian coordinates.

Returns: A pointer to a Point representing the point in affine coordinates.

type Point

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

func (*Point) String

func (pt *Point) String() string

String returns a string representation of the Point struct.

It returns a string in the format "(x=<X>, y=<Y>)", where <X> and <Y> are the string representations of the X and Y coordinates of the Point.

func (*Point) ToJacobian

func (pt *Point) ToJacobian() *JacobianPoint

ToJacobian converts a point from affine coordinates to Jacobian coordinates.

Parameter:

  • p: a pointer to a Point representing the point in affine coordinates.

Returns: A pointer to a JacobianPoint representing the point in Jacobian coordinates.

func (*Point) Valid

func (pt *Point) Valid() bool

Valid checks if a given point is on the elliptic curve.

Parameters:

  • p: a pointer to a Point representing the point to be validated.

Returns:

  • bool: true if the point is valid, false otherwise.

type PointError

type PointError struct {
	Message string
	Err     error
}

func (*PointError) Error

func (e *PointError) Error() string

func (*PointError) Unwrap

func (e *PointError) Unwrap() error

type PrivateKey

type PrivateKey struct {
	Raw          *big.Int
	Wif          *string
	Uncompressed bool
}

func NewPrivateKey

func NewPrivateKey(raw *big.Int, wif *string) (*PrivateKey, error)

NewPrivateKey generates a new PrivateKey object.

It takes in two parameters:

  • raw: a pointer to a big.Int object representing the raw value of the private key.
  • wif: a pointer to a string representing the WIF (Wallet Import Format) of the private key.

The function returns a pointer to a PrivateKey object and an error.

  • If both raw and wif are provided, it returns an error.
  • If neither raw nor wif is provided, it generates a random private key and returns a new PrivateKey object.
  • If only wif is provided, it creates a new PrivateKey object with the provided WIF.
  • If only raw is provided, it creates a new PrivateKey object with the provided raw value.

The function checks if the generated or provided private key is valid. If the private key is invalid, it returns an error.

The function also encodes the generated or provided private key using the Wif() method. If the encoding fails, it returns an error.

The function returns a pointer to the newly created PrivateKey object.

func (*PrivateKey) SplitBytes

func (k *PrivateKey) SplitBytes() (version []byte, payload []byte, checkSum []byte, err error)

SplitBytes splits the private key bytes into three parts: the version byte, the private key bytes, and the checksum bytes.

It takes no parameters. It returns three byte slices: the version byte, the private key bytes, and the checksum bytes.

func (*PrivateKey) ToInt

func (k *PrivateKey) ToInt() (uncompressed bool, err error)

Int calculates the integer value of the private key.

It returns a boolean indicating if the key is uncompressed and an error if any.

func (*PrivateKey) ToWif

func (k *PrivateKey) ToWif(uncompressed bool) (*string, error)

Wif generates the Wallet Import Format (WIF) for the private key.

It takes a boolean uncompressed indicating if the key is uncompressed. It returns a pointer to a string and an error.

type PrivateKeyError

type PrivateKeyError struct {
	Message string
	Err     error
}

func (*PrivateKeyError) Error

func (e *PrivateKeyError) Error() string

func (*PrivateKeyError) Unwrap

func (e *PrivateKeyError) Unwrap() error

type Runner

type Runner interface {
	Init([]string) error
	Run() error
	Name() string
}

type Secp256k1

type Secp256k1 struct {
	PCurve   *big.Int
	NCurve   *big.Int
	ACurve   *big.Int
	BCurve   *big.Int
	GenPoint *JacobianPoint
}

type SignCommand

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

func NewSignCommand

func NewSignCommand() *SignCommand

func (*SignCommand) Init

func (sc *SignCommand) Init(args []string) error

func (*SignCommand) Name

func (sc *SignCommand) Name() string

func (*SignCommand) Run

func (sc *SignCommand) Run() error

type Signature

type Signature struct {
	R *big.Int
	S *big.Int
}

type SignatureError

type SignatureError struct {
	Message string
	Err     error
}

func (*SignatureError) Error

func (e *SignatureError) Error() string

func (*SignatureError) Unwrap

func (e *SignatureError) Unwrap() error

type VerifyCommand

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

func NewVerifyCommand

func NewVerifyCommand() *VerifyCommand

func (*VerifyCommand) Init

func (vc *VerifyCommand) Init(args []string) error

func (*VerifyCommand) Name

func (vc *VerifyCommand) Name() string

func (*VerifyCommand) Run

func (vc *VerifyCommand) Run() error

type Wallet

type Wallet struct {
	PrivKey   *PrivateKey
	RawPubKey *Point
	PubKey    string
	Legacy    string
	Nested    string
	Native    string
}

func CreateNewWallet

func CreateNewWallet() *Wallet

CreateNewWallet generates a new wallet with private key, public key, and various address types.

Returns:

  • A pointer to a Wallet struct representing the new wallet.

func (*Wallet) String

func (w *Wallet) String() string

String returns a formatted string representation of the Wallet.

It concatenates the private key (raw), private key (WIF), public key (raw), public key (hex compressed), legacy address, nested segwit address, and native segwit address into a single string.

Returns:

  • A string containing the formatted representation of the Wallet.

Jump to

Keyboard shortcuts

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