address

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 27, 2023 License: BSD-3-Clause Imports: 11 Imported by: 2

Documentation

Overview

Implementation of various Bitcoin address types, including P2PKH, P2SH, and SegWit

Index

Constants

This section is empty.

Variables

View Source
var MainnetNetwork = networkInfo{
	// contains filtered or unexported fields
}

BitcoinNetwork represents the Bitcoin network information.

View Source
var TestnetNetwork = networkInfo{
	// contains filtered or unexported fields
}

TestnetNetwork represents the Bitcoin testnet network information.

Functions

func DefaultNetwork

func DefaultNetwork() *networkInfo

get default network of application

func IsValidAddress

func IsValidAddress(address string, addressType AddressType, network *networkInfo) bool

IsValidAddress checks the validity of a Bitcoin address. It verifies whether the input address is well-formed and complies with the specified address type and network. The function performs the following checks: 1. Ensures that the address length is within a valid range (between 26 and 35 characters). 2. Decodes the base58-encoded address and verifies the checksum. 3. Validates the address type and network based on the decoded address prefix.

Parameters: - address: A string representing the Bitcoin address to be validated. - addressType: An AddressType indicating the expected type of the address (e.g., P2PKH, P2PKHInP2SH, etc.). - network: A pointer to networkInfo representing the expected network configuration (can be nil for default networks).

Returns: - bool: True if the input address is valid according to the specified criteria; otherwise, false.

func IsValidHash160

func IsValidHash160(hash160 string) bool

IsValidHash160 checks the validity of a hash160 string. It verifies whether the input string represents a valid hash160 by ensuring that it has a length of 40 characters and can be successfully parsed as a hexadecimal number. If the input hash160 is valid, the function returns true; otherwise, it returns false.

Parameters: - hash160: A string representing the hash160 value to be validated.

Returns: - bool: True if the input hash160 is valid; otherwise, false.

func NetworkFromWIF

func NetworkFromWIF(wif string) (networkInfo, error)

NetworkFromWIF returns the Bitcoin network information based on a WIF (Wallet Import Format) string.

func ScriptToSegwitProgram

func ScriptToSegwitProgram(script *scripts.Script) string

ScriptToSegwitProgram converts a Bitcoin script to its hash equivalent as a witness program. The function takes a Bitcoin script and computes its hash (specifically, a single hash) to create a witness program. It then returns the resulting hash as a hexadecimal string.

Parameters: - script: A Script object representing the Bitcoin script.

Returns: - string: A hexadecimal string representing the hash-based witness program.

func SetDefaultNetwork

func SetDefaultNetwork(network networkInfo)

update the default network, the methods that require a network will use the default network if the desired parameter is not found.

Types

type AddressType

type AddressType int
const (

	/*
		P2PKH (Pay-to-Public-Key-Hash)
		This is the most common address type in Bitcoin.
		FAddresses start with the number "1."
		It represents a single public key hashed with SHA-256 and then with RIPEMD-160.
		Transactions sent to a P2PKH address can only be spent by providing a valid signature corresponding to the public key.
	*/
	P2PKH AddressType = iota
	/*
		P2SH (Pay-to-Script-Hash):
		Addresses start with the number "3."
		It allows for more complex scripts (such as multisig or custom scripts) to be used as the recipient's address.
		The actual script that unlocks the funds is included in the transaction input.
	*/
	P2WPKH
	/*
		P2PK (Pay-to-Public-Key):
		Addresses start with the number "1."
		It involves sending funds directly to a recipient's public key, without hashing.
		It's not commonly used because it exposes the recipient's public key on the blockchain, reducing privacy.
	*/
	P2PK
	/*
		P2TR (Pay-to-Taproot):
		An advanced address type introduced with the Taproot upgrade.
		It allows for greater flexibility and privacy by enabling complex scripts and conditions.
		Addresses start with "bc1p."
	*/
	P2TR
	/*
		P2WSH (Pay-to-Witness-Script-Hash):
		Part of the SegWit upgrade.
		Addresses start with "bc1."
		Similar to P2SH but for SegWit-compatible scripts.
		Allows for more efficient use of block space and enhanced security.
	*/
	P2WSH
	/*
		P2WSHInP2SH (Pay-to-Witness-Script-Hash inside Pay-to-Script-Hash):
		Addresses start with the number "3."
		Combines the benefits of both P2SH and SegWit.
		It involves sending funds to a P2SH address, where the redeem script is a witness script (SegWit-compatible).
	*/
	P2WSHInP2SH
	/*
		P2WPKHInP2SH (Pay-to-Witness-Script-Hash inside Pay-to-Script-Hash):
		Addresses start with the number "3."
		Combines the benefits of both P2SH and SegWit.
		It involves sending funds to a P2SH address, where the redeem script is a witness script (SegWit-compatible).
	*/
	P2WPKHInP2SH
	P2PKInP2SH
	P2PKHInP2SH
)

func NetworkFromXPrivePrefix

func NetworkFromXPrivePrefix(prefix []byte) AddressType

NetworkFromXPrivePrefix returns the Bitcoin address type based on an extended private key prefix.

func NetworkFromXPublicPrefix

func NetworkFromXPublicPrefix(prefix []byte) AddressType

NetworkFromXPublicPrefix returns the Bitcoin address type based on an extended public key prefix.

type BitcoinAddress

type BitcoinAddress interface {
	ToScriptPubKey() *scripts.Script
	Show(network ...interface{}) string
	GetType() AddressType
}

address access interface in multiple structs

type LegacyAddress

type LegacyAddress struct {
	/*
	   the hash160 string representation of the address; hash160 represents
	   two consequtive hashes of the public key or the redeam script, first
	   a SHA-256 and then an RIPEMD-160
	*/
	Hash160 string
	Type    AddressType
}

func (LegacyAddress) ToScriptPubKey

func (b LegacyAddress) ToScriptPubKey() *scripts.Script

a "scriptPubKey" (short for "script public key")

refers to a script that defines the conditions
that must be satisfied in order to spend funds
from a particular Bitcoin address. The script is
associated with the output of a Bitcoin transaction
and is used to lock the funds until the specified conditions are met.

type Network

type Network int
const (
	Mainnet Network = iota
	Testnet
)

type NetworkInfo

type NetworkInfo interface {
	Bech32() string
	P2PKHPrefix() int
	P2SHPrefix() int
	WIF() byte
	ExtendPrivate(AddressType) string
	ExtendPublic(AddressType) string
	IsMainNet() bool
	Network() Network
}

type NetworkParams

type NetworkParams struct {
	Network *networkInfo
}

struct of network parameters in function arguments

type P2PKAddress

type P2PKAddress struct {
	AddressProgram LegacyAddress
}

func P2PKAddressFromPublicKey

func P2PKAddressFromPublicKey(public string) (*P2PKAddress, error)

P2PKAddressFromPublicKey creates a P2PK (Pay-to-Public-Key) address object from a public key string. The function first converts the provided public key string to a byte slice and checks if it represents a valid elliptic curve point.

Parameters: - public: A string representing the public key to create the address from.

Returns: - *P2PKAddress: A P2PKAddress object representing the generated P2PK address. - error: An error if the public key is invalid or if there are any issues during address creation.

func (P2PKAddress) GetType

func (s P2PKAddress) GetType() AddressType

returns the type of address

func (P2PKAddress) Program

func (s P2PKAddress) Program() LegacyAddress

Access to the address program LegacyAddress

func (P2PKAddress) Show

func (s P2PKAddress) Show(network ...interface{}) string

The method calculates the address checksum and returns the Base58-encoded Bitcoin legacy address.

func (P2PKAddress) ToScriptPubKey

func (s P2PKAddress) ToScriptPubKey() *scripts.Script

a "scriptPubKey" (short for "script public key")

refers to a script that defines the conditions
that must be satisfied in order to spend funds
from a particular Bitcoin address. The script is
associated with the output of a Bitcoin transactionf
and is used to lock the funds until the specified conditions are met.

type P2PKHAddress

type P2PKHAddress struct {
	AddressProgram LegacyAddress
}

P2PKH (Pay-to-Public-Key-Hash) This is the most common address type in Bitcoin. FAddresses start with the number "1." It represents a single public key hashed with SHA-256 and then with RIPEMD-160. Transactions sent to a P2PKH address can only be spent by providing a valid signature corresponding to the public key.

func P2PKHAddressFromAddress

func P2PKHAddressFromAddress(address string, network ...interface{}) (*P2PKHAddress, error)

P2PKHAddressFromAddress creates a P2PKH (Pay-to-Public-Key-Hash) address object from an address string. The function first uses the `fromAddress` function to extract the relevant information from the provided address string. The `network` argument is optional and can be used to validate that the address belongs to a specific network configuration. If provided, the function checks if the address matches the expected network; if not, an error is returned. Finally, the function constructs and returns a P2PKHAddress object with the extracted data, setting its address type to P2PKH.

Parameters: - address: A string representing the address from which to create the P2PKH address. - network: An optional network configuration for address validation (e.g., Mainnet, Testnet).

Returns:

  • *P2PKHAddress: A P2PKHAddress object representing the generated P2PKH address.
  • error: An error if there are any issues during address creation, if the address is invalid, or if the address does not match the expected network (if provided).

func P2PKHAddressFromHash160

func P2PKHAddressFromHash160(hash160 string) (*P2PKHAddress, error)

P2PKHAddressFromHash160 creates a P2PKH (Pay-to-Public-Key-Hash) address object from a hash160 string. The function uses the `fromHash160` function to convert the provided hash160 string to its canonical form and validate its correctness. If the hash160 is valid, the function constructs and returns a P2PKHAddress object with the converted hash160, setting its address type to P2PKH.

Parameters: - hash160: A string representing the hash160 value from which to create the P2PKH address.

Returns: - *P2PKHAddress: A P2PKHAddress object representing the generated P2PKH address. - error: An error if there are any issues during address creation, or if the input hash160 is invalid.

func P2PKHAddressFromScript

func P2PKHAddressFromScript(script *scripts.Script) (*P2PKHAddress, error)

P2PKHAddressFromScript creates a P2PKH (Pay-to-Public-Key-Hash) address object from a Script object. The function extracts the relevant information from the provided Script object using the `fromScript` function and constructs a P2PKHAddress object with the derived data, setting its address type to P2PKH.

func (P2PKHAddress) GetType

func (s P2PKHAddress) GetType() AddressType

returns the type of address

func (P2PKHAddress) Program

func (s P2PKHAddress) Program() LegacyAddress

Access to the address program LegacyAddress

func (P2PKHAddress) Show

func (s P2PKHAddress) Show(network ...interface{}) string

The method calculates the address checksum and returns the Base58-encoded Bitcoin legacy address.

func (P2PKHAddress) ToScriptPubKey

func (s P2PKHAddress) ToScriptPubKey() *scripts.Script

a "scriptPubKey" (short for "script public key")

refers to a script that defines the conditions
that must be satisfied in order to spend funds
from a particular Bitcoin address. The script is
associated with the output of a Bitcoin transaction
and is used to lock the funds until the specified conditions are met.

type P2SHAddressTypeParam

type P2SHAddressTypeParam struct {
	AddressType *AddressType
}

struct of network parameters in function arguments

type P2SHAdress

type P2SHAdress struct {
	AddressProgram LegacyAddress
}

P2SH (Pay-to-Script-Hash): Addresses start with the number "3." It allows for more complex scripts (such as multisig or custom scripts) to be used as the recipient's address. The actual script that unlocks the funds is included in the transaction input.

func P2SHAddressFromAddress

func P2SHAddressFromAddress(address string, args ...interface{}) (*P2SHAdress, error)

P2SHAddressFromAddress creates a P2SH (Pay-to-Script-Hash) address object from an address string. The function uses the `fromAddress` function to extract the relevant information from the provided address string. It also accepts optional `args` parameters, including an `AddressType` indicating the expected type of the address and network configurations for address validation. If provided, the function checks if the address matches the expected network and address type; if not, an error is returned. Finally, the function constructs and returns a P2SHAddress object with the extracted data, setting its address type based on the provided parameters.

Parameters:

  • address: A string representing the address from which to create the P2SH address.
  • args: Optional arguments, including an `AddressType` (if specified) and network configurations for address validation.

Returns:

  • *P2SHAddress: A P2SHAddress object representing the generated P2SH address.
  • error: An error if there are any issues during address creation, if the address is invalid, or if it does not match the expected network or address type (if provided).

func P2SHAddressFromHash160

func P2SHAddressFromHash160(hash160 string) (*P2SHAdress, error)

P2SHAddressFromHash160 creates a P2SH (Pay-to-Script-Hash) address object from a hash160 string. The function uses the `fromHash160` function to convert the provided hash160 string to its canonical form and validate its correctness. If the hash160 is valid, the function constructs and returns a P2SHAddress object with the converted hash160, setting its address type to P2PKHInP2SH.

Parameters: - hash160: A string representing the hash160 value from which to create the P2SH address.

Returns: - *P2SHAddress: A P2SHAddress object representing the generated P2SH address. - error: An error if there are any issues during address creation, or if the input hash160 is invalid.

func P2SHAddressFromScript

func P2SHAddressFromScript(script *scripts.Script, addressType AddressType) (*P2SHAdress, error)

P2SHAddressFromScript creates a P2SH (Pay-to-Script-Hash) address object from a Script object. The function first uses the `fromScript` function to extract the relevant information from the provided Script object. It also accepts an `addressType` parameter, indicating the expected type of the address (e.g., P2SHInP2PK, P2SHInP2PKH). Finally, the function constructs and returns a P2SHAddress object with the extracted data, setting its address type based on the provided `addressType`.

Parameters: - script: A Script object representing the script from which to create the P2SH address. - addressType: An AddressType indicating the expected type of the address (e.g., P2SHInP2PK, P2SHInP2PKH).

Returns: - *P2SHAddress: A P2SHAddress object representing the generated P2SH address. - error: An error if there are any issues during address creation or if the script is invalid.

func (P2SHAdress) GetType

func (s P2SHAdress) GetType() AddressType

returns the type of address

func (P2SHAdress) Program

func (s P2SHAdress) Program() LegacyAddress

Access to the address program LegacyAddress

func (P2SHAdress) Show

func (s P2SHAdress) Show(network ...interface{}) string

The method calculates the address checksum and returns the Base58-encoded Bitcoin legacy address.

func (P2SHAdress) ToScriptPubKey

func (s P2SHAdress) ToScriptPubKey() *scripts.Script

a "scriptPubKey" (short for "script public key")

refers to a script that defines the conditions
that must be satisfied in order to spend funds
from a particular Bitcoin address. The script is
associated with the output of a Bitcoin transaction
and is used to lock the funds until the specified conditions are met.

type P2TRAddress

type P2TRAddress struct {
	AddressProgram SegwitAddress
}

P2TR (Pay-to-Taproot): An advanced address type introduced with the Taproot upgrade. It allows for greater flexibility and privacy by enabling complex scripts and conditions. Addresses start with "bc1p."

func P2TRAddressFromAddress

func P2TRAddressFromAddress(address string, network ...interface{}) (*P2TRAddress, error)

P2TRAddressFromAddress instantiates a P2TR (Pay-to-Taproot) address object from a P2TR address string encoding. The function takes the P2TR address string and an optional network parameter as input and sets the appropriate version and type for a P2TR address. It then returns the resulting P2TR address object.

Parameters: - address: A P2TR address string encoding. - network: An optional network parameter for address validation (e.g., Mainnet or Testnet).

Returns: - *P2TRAddress: A P2TR address object created from the provided P2TR address string. - error: An error if there are issues during object instantiation or if the address is invalid.

func P2TRAddressFromProgram

func P2TRAddressFromProgram(program string) (*P2TRAddress, error)

P2TRAddressFromProgram instantiates a P2TR (Pay-to-Taproot) address object from a Taproot program. The function takes the Taproot program as input and sets the appropriate version and type for a P2TR address. It then returns the resulting P2TR address object.

Parameters: - program: A Taproot program in hexadecimal string format.

Returns: - *P2TRAddress: A P2TR address object created from the provided Taproot program. - error: An error if there are issues during object instantiation or if the Taproot program is invalid.

func (P2TRAddress) GetType

func (s P2TRAddress) GetType() AddressType

returns the type of address

func (P2TRAddress) Program

func (s P2TRAddress) Program() SegwitAddress

Access to the address program SegwitAddress

func (P2TRAddress) Show

func (s P2TRAddress) Show(network ...interface{}) string

address string encoded in the Bech32 format.

func (P2TRAddress) ToScriptPubKey

func (s P2TRAddress) ToScriptPubKey() *scripts.Script

a "scriptPubKey" (short for "script public key")

refers to a script that defines the conditions
that must be satisfied in order to spend funds
from a particular Bitcoin address. The script is
associated with the output of a Bitcoin transaction
and is used to lock the funds until the specified conditions are met.

type P2WPKHAddresss

type P2WPKHAddresss struct {
	AddressProgram SegwitAddress
}

P2WPKH (Pay-to-Witness-Public-Key-Hash): Also known as Bech32 addresses. Addresses start with "bc1." Part of the Segregated Witness (SegWit) upgrade. It represents a single public key hashed with SHA-256 and then with RIPEMD-160, but it's a SegWit-compatible format.

func P2WPKHAddresssFromAddress

func P2WPKHAddresssFromAddress(address string, network ...interface{}) (*P2WPKHAddresss, error)

P2WPKHAddresssFromAddress instantiates a P2WPKH (Pay-to-Witness-Public-Key-Hash) address object from a SegWit (Bech32) address string. The function takes the SegWit address as input and extracts the witness program from it. It then sets the appropriate version and type for a P2WPKH address and returns the resulting P2WPKH address object.

Parameters: - address: A SegWit (Bech32) address string. - network: Optional network configurations for address validation.

Returns:

  • *P2WPKHAddresss: A P2WPKH address object created from the provided SegWit address.
  • error: An error if there are issues during object instantiation, if the address is invalid, or if the extracted witness program is invalid.

func P2WPKHAddresssFromProgram

func P2WPKHAddresssFromProgram(program string) (*P2WPKHAddresss, error)

P2WPKHAddresssFromProgram instantiates a P2WPKH (Pay-to-Witness-Public-Key-Hash) address object from a witness program hexadecimal string. The function takes the witness program as input, sets the appropriate version and type for a P2WPKH address, and returns the resulting P2WPKH address object.

Parameters: - program: A hexadecimal string representing the witness program.

Returns: - *P2WPKHAddresss: A P2WPKH address object created from the provided witness program. - error: An error if there are issues during object instantiation or if the witness program is invalid.

func P2WPKHAddresssFromScript

func P2WPKHAddresssFromScript(script *scripts.Script) (*P2WPKHAddresss, error)

P2WPKHAddresssFromScript instantiates a P2WPKH (Pay-to-Witness-Public-Key-Hash) address object from a witness script. The function takes the witness script as input, derives the witness program from it, and sets the appropriate version and type for a P2WPKH address. It then returns the resulting P2WPKH address object.

Parameters: - script: A witness script.

Returns: - *P2WPKHAddresss: A P2WPKH address object created from the provided witness script. - error: An error if there are issues during object instantiation or if the witness script is invalid.

func (P2WPKHAddresss) GetType

func (s P2WPKHAddresss) GetType() AddressType

returns the type of address

func (P2WPKHAddresss) Program

func (s P2WPKHAddresss) Program() SegwitAddress

Access to the address program SegwitAddress

func (P2WPKHAddresss) Show

func (s P2WPKHAddresss) Show(network ...interface{}) string

address string encoded in the Bech32 format.

func (P2WPKHAddresss) ToScriptPubKey

func (s P2WPKHAddresss) ToScriptPubKey() *scripts.Script

a "scriptPubKey" (short for "script public key")

refers to a script that defines the conditions
that must be satisfied in order to spend funds
from a particular Bitcoin address. The script is
associated with the output of a Bitcoin transaction
and is used to lock the funds until the specified conditions are met.

type P2WSHAddresss

type P2WSHAddresss struct {
	AddressProgram SegwitAddress
}

P2WSH (Pay-to-Witness-Script-Hash): Part of the SegWit upgrade. Addresses start with "bc1." Similar to P2SH but for SegWit-compatible scripts. Allows for more efficient use of block space and enhanced security.

func P2WSHAddresssFromAddress

func P2WSHAddresssFromAddress(address string, network ...interface{}) (*P2WSHAddresss, error)

P2WSHAddresssFromAddress instantiates a P2WSH (Pay-to-Witness-Script-Hash) address object from a SegWit (Bech32) address string. The function takes the SegWit address as input and extracts the witness program from it. It then sets the appropriate version and type for a P2WSH address and returns the resulting P2WSH address object.

Parameters: - address: A SegWit (Bech32) address string. - network: Optional network configurations for address validation.

Returns:

  • *P2WSHAddresss: A P2WSH address object created from the provided SegWit address.
  • error: An error if there are issues during object instantiation, if the address is invalid, or if the extracted witness program is invalid.

func P2WSHAddresssFromProgram

func P2WSHAddresssFromProgram(program string) (*P2WSHAddresss, error)

P2WSHAddresssFromProgram instantiates a P2WSH (Pay-to-Witness-Script-Hash) address object from a witness program hexadecimal string. The function takes the witness program as input, sets the appropriate version and type for a P2WSH address, and returns the resulting P2WSH address object.

Parameters: - program: A hexadecimal string representing the witness program.

Returns: - *P2WSHAddresss: A P2WSH address object created from the provided witness program. - error: An error if there are issues during object instantiation or if the witness program is invalid.

func P2WSHAddresssFromScript

func P2WSHAddresssFromScript(script *scripts.Script) (*P2WSHAddresss, error)

P2WSHAddresssFromScript instantiates a P2WSH (Pay-to-Witness-Script-Hash) address object from a witness script. The function takes the witness script as input, computes the witness program by hashing the script, and sets the appropriate version and type for a P2WSH address. It then returns the resulting P2WSH address object.

Parameters: - script: A Script object representing the witness script.

Returns: - *P2WSHAddresss: A P2WSH address object created from the provided witness script. - error: An error if there are issues during object instantiation or if the witness script is invalid.

func (P2WSHAddresss) GetType

func (s P2WSHAddresss) GetType() AddressType

returns the type of address

func (P2WSHAddresss) Program

func (s P2WSHAddresss) Program() SegwitAddress

Access to the address program SegwitAddress

func (P2WSHAddresss) Show

func (s P2WSHAddresss) Show(network ...interface{}) string

address string encoded in the Bech32 format.

func (P2WSHAddresss) ToScriptPubKey

func (s P2WSHAddresss) ToScriptPubKey() *scripts.Script

a "scriptPubKey" (short for "script public key")

refers to a script that defines the conditions
that must be satisfied in order to spend funds
from a particular Bitcoin address. The script is
associated with the output of a Bitcoin transaction
and is used to lock the funds until the specified conditions are met.

type SegwitAddress

type SegwitAddress struct {
	/*
	   for segwit v0 this is the hash string representation of either the address;
	   it can be either a public key hash (P2WPKH) or the hash of the script (P2WSH)
	   for segwit v1 (aka taproot) this is the public key
	*/
	Program          string
	Version          string
	SegwitNumVersion int
	Type             AddressType
}

func (SegwitAddress) ToScriptPubKey

func (segwit SegwitAddress) ToScriptPubKey() *scripts.Script

a "scriptPubKey" (short for "script public key")

refers to a script that defines the conditions
that must be satisfied in order to spend funds
from a particular Bitcoin address. The script is
associated with the output of a Bitcoin transaction
and is used to lock the funds until the specified conditions are met.

Jump to

Keyboard shortcuts

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