BSV.PrivKey (BSV v2.0.0) View Source

A PrivKey is a data structure representing a Bitcoin private key.

Internally, a private key is a secret 256-bit integer within the range of the ECDSA secp256k1 parmaeters. Each private key corresponds to a public key which is a coordinate on the secp256k1 curve.

Link to this section Summary

Types

Private key 256-bit binary

Wallet Import Format private key

t()

Private key struct

Link to this section Types

Specs

privkey_bin() :: <<_::256>>

Private key 256-bit binary

Specs

privkey_wif() :: String.t()

Wallet Import Format private key

WIF encoded keys is a common way to represent private Keys in Bitcoin. WIF encoded keys are shorter and include a built-in error checking and a type byte.

Specs

t() :: %BSV.PrivKey{compressed: boolean(), d: privkey_bin()}

Private key struct

Link to this section Functions

Link to this function

from_binary(privkey, opts \\ [])

View Source

Specs

from_binary(binary(), keyword()) :: {:ok, t()} | {:error, term()}

Parses the given binary into a BSV.PrivKey.t/0.

Returns the result in an :ok / :error tuple pair.

Options

The accepted options are:

  • :compressed - Denotes whether the correspding BSV.PubKey.t/0 is compressed on not. Defaults true.
  • :encoding - Optionally decode the binary with either the :base64 or :hex encoding scheme.

Examples

iex> PrivKey.from_binary("3cff04633088622e4599dc2ebf843f82cef3463b910d34a752a13622abae379b", encoding: :hex)
{:ok, %PrivKey{
  d: <<60, 255, 4, 99, 48, 136, 98, 46, 69, 153, 220, 46, 191, 132, 63, 130, 206, 243, 70, 59, 145, 13, 52, 167, 82, 161, 54, 34, 171, 174, 55, 155>>
}}
Link to this function

from_binary!(privkey, opts \\ [])

View Source

Specs

from_binary!(binary(), keyword()) :: t()

Parses the given binary into a BSV.PrivKey.t/0.

As from_binary/2 but returns the result or raises an exception.

Specs

from_wif(privkey_wif()) :: {:ok, t()} | {:error, term()}

Decodes the given BSV.PrivKey.privkey_wif/0 into a BSV.PrivKey.t/0.

Returns the result in an :ok / :error tuple pair.

Examples

iex> PrivKey.from_wif("KyGHAK8MNohVPdeGPYXveiAbTfLARVrQuJVtd3qMqN41UEnTWDkF")
{:ok, %PrivKey{
  d: <<60, 255, 4, 99, 48, 136, 98, 46, 69, 153, 220, 46, 191, 132, 63, 130, 206, 243, 70, 59, 145, 13, 52, 167, 82, 161, 54, 34, 171, 174, 55, 155>>
}}

Specs

from_wif!(privkey_wif()) :: t()

Decodes the given BSV.PrivKey.privkey_wif/0 into a BSV.PrivKey.t/0.

As from_wif/1 but returns the result or raises an exception.

Specs

new(keyword()) :: t()

Generates and returns a new BSV.PrivKey.t/0.

Options

The accepted options are:

  • :compressed - Denotes whether the correspding BSV.PubKey.t/0 is compressed on not. Defaults true.
Link to this function

to_binary(priv_key, opts \\ [])

View Source

Serialises the given BSV.PrivKey.t/0 into a binary.

Options

The accepted options are:

  • :encoding - Optionally encode the binary with either the :base64 or :hex encoding scheme.

Examples

iex> PrivKey.to_binary(@privkey, encoding: :hex)
"3cff04633088622e4599dc2ebf843f82cef3463b910d34a752a13622abae379b"

Specs

to_wif(t()) :: privkey_wif()

Encodes the given BSV.PrivKey.t/0 as a BSV.PrivKey.privkey_wif/0.

Examples

iex> PrivKey.to_wif(@privkey)
"KyGHAK8MNohVPdeGPYXveiAbTfLARVrQuJVtd3qMqN41UEnTWDkF"