wallet

package
v0.9.1 Latest Latest
Warning

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

Go to latest
Published: Mar 22, 2023 License: MIT Imports: 29 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// MaxBlindingAttempts is the max number of times the blinding of a pset
	// can be repeated in case it fails to generate valid proofs.
	MaxBlindingAttempts = 8
	// DefaultBlindingAttempts is the default number of times the blinding of a
	// pset is retried if it fails to generate valid proofs.
	DefaultBlindingAttempts = 4
)
View Source
const (
	P2PK = iota
	P2PKH
	P2MS
	P2SH_P2WPKH
	P2SH_P2WSH
	P2WPKH
	P2WSH
)
View Source
const (
	// MaxHardenedValue is the max value for hardened indexes of BIP32
	// derivation paths
	MaxHardenedValue = math.MaxUint32 - hdkeychain.HardenedKeyStart
)

Variables

View Source
var (
	// ErrNullNetwork ...
	ErrNullNetwork = errors.New("network must not be null")
	// ErrNullInputWitnessUtxo ...
	ErrNullInputWitnessUtxo = errors.New("input witness utxo must not be null")
	// ErrNullSigningMnemonic ...
	ErrNullSigningMnemonic = errors.New("signing mnemonic is null")
	// ErrNullBlindingMnemonic ...
	ErrNullBlindingMnemonic = errors.New("blinding mnemonic is null")
	// ErrNullSigningMasterKey ...
	ErrNullSigningMasterKey = errors.New("signing master key is null")
	// ErrNullBlindingMasterKey ...
	ErrNullBlindingMasterKey = errors.New("blinding master key is null")
	// ErrNullPassphrase ...
	ErrNullPassphrase = errors.New("passphrase must not be null")
	// ErrNullPlainText ...
	ErrNullPlainText = errors.New("text to encrypt must not be null")
	// ErrNullCypherText ...
	ErrNullCypherText = errors.New("cypher to decrypt must not be null")
	// ErrNullDerivationPath ...
	ErrNullDerivationPath = errors.New("derivation path must not be null")
	// ErrNullOutputDerivationPath ...
	ErrNullOutputDerivationPath = fmt.Errorf("output %v", ErrNullDerivationPath)
	// ErrNullChangeDerivationPath ...
	ErrNullChangeDerivationPath = fmt.Errorf("change %v", ErrNullDerivationPath)
	// ErrNullOutputScript ...
	ErrNullOutputScript = errors.New("output script must not be null")
	// ErrNullPset ...
	ErrNullPset = errors.New("pset base64 must not be null")
	// ErrNullChangePathsByAsset ...
	ErrNullChangePathsByAsset = errors.New(
		"derivation paths for eventual change(s) must not be null",
	)
	// ErrNullInputBlindingData ...
	ErrNullInputBlindingData = errors.New("input blinding data must not be null")

	// ErrInvalidSigningMnemonic ...
	ErrInvalidSigningMnemonic = errors.New("signing mnemonic is invalid")
	// ErrInvalidEntropySize ...
	ErrInvalidEntropySize = errors.New(
		"entropy size must be a multiple of 32 in the range [128,256]",
	)
	// ErrInvalidBlindingMnemonic ...
	ErrInvalidBlindingMnemonic = errors.New("blinding mnemonic is invalid")
	// ErrInvalidCypherText ...
	ErrInvalidCypherText = errors.New("cypher must be in base64 format")
	// ErrInvalidDerivationPath ...
	ErrInvalidDerivationPath = errors.New("invalid derivation path")
	// ErrInvalidDerivationPathLength ...
	ErrInvalidDerivationPathLength = errors.New(
		"derivation path must be a relative path in the form \"account'/branch/index\"",
	)
	// ErrInvalidDerivationPathAccount ...
	ErrInvalidDerivationPathAccount = errors.New(
		"derivation path's account (first elem) must be hardened (suffix \"'\")",
	)
	// ErrInvalidInputAsset ...
	ErrInvalidInputAsset = errors.New("input asset must be a 32 byte array in hex format")
	// ErrInvalidOutputAsset ...
	ErrInvalidOutputAsset = errors.New("output asset must be a 32 byte array in hex format")
	// ErrInvalidOutputAddress ...
	ErrInvalidOutputAddress = errors.New("output address must be a valid address")
	// ErrInvalidChangeAddress ...
	ErrInvalidChangeAddress = errors.New("change address must be a valid address")
	// ErrInvalidMilliSatsPerBytes ...
	ErrInvalidMilliSatsPerBytes = errors.New("unit of mSats/byte must be at least 100 (0.1 sats/byte)")
	// ErrInvalidOutputBlindingKeysLen ...
	ErrInvalidOutputBlindingKeysLen = errors.New(
		"number of output blinding keys must match number of outputs",
	)
	// ErrInvalidPassphrase ...
	ErrInvalidPassphrase = errors.New("passphrase provided is not correct")
	// ErrInvalidSignatures ...
	ErrInvalidSignatures = errors.New("transaction contains invalid signature(s)")
	// ErrInvalidAttempts ...
	ErrInvalidAttempts = fmt.Errorf(
		"attempts must be a number in range [0, %d]",
		MaxBlindingAttempts,
	)
	// ErrInvalidInputIndex ...
	ErrInvalidInputIndex = errors.New("index of input blinder is invalid")
	// ErrInvalidInputAssetBlinder ...
	ErrInvalidInputAssetBlinder = errors.New("asset blinder must be a 32-byte array")
	// ErrInvalidInputAmountBlinder ...
	ErrInvalidInputAmountBlinder = errors.New("amount blinder must be a 32-byte array")
	// ErrInvalidInBlindingKey ...
	ErrInvalidInBlindingKey = errors.New("unable to recover input blinding data with provided key")
	// ErrInvalidOutBlindingKey ...
	ErrInvalidOutBlindingKey = errors.New("unable to recover output blinding data with provided key")

	// ErrEmptyDerivationPaths ...
	ErrEmptyDerivationPaths = errors.New("derivation path list must not be empty")
	// ErrEmptyUnspents ...
	ErrEmptyUnspents = errors.New("unspents list must not be empty")

	// ErrMalformedDerivationPath ...
	ErrMalformedDerivationPath = errors.New(
		"path must not start or end with a '/' and " +
			"can optionally start with 'm/' for absolute paths",
	)
	// ErrOutOfRangeDerivationPathAccount ...
	ErrOutOfRangeDerivationPathAccount = fmt.Errorf(
		"account index must be in hardened range [0, %d]",
		MaxHardenedValue,
	)
	// ErrZeroInputAmount ...
	ErrZeroInputAmount = errors.New("input amount must not be zero")
	// ErrZeroOutputAmount ...
	ErrZeroOutputAmount = errors.New("output amount must not be zero")

	// ErrReachedMaxBlindingAttempts ...
	ErrReachedMaxBlindingAttempts = errors.New(
		"max number of attempts reached for blinding the transaction",
	)

	// ErrMissingInBlindingKey ...
	ErrMissingInBlindingKey = errors.New("missing blinding key for input")
	// ErrMissingOutBlindingKey ...
	ErrMissingOutBlindingKey = errors.New("missing blinding key for output")

	// ErrMissingLBTCInput ...
	ErrMissingLBTCInput = errors.New("missing LBTC input to cover network fees")
	// ErrMissingLBTCOutput ...
	ErrMissingLBTCOutput = errors.New("missing LBTC output to subtract network fees from")
)
View Source
var (
	// DefaultBaseDerivationPath m/84'/0'
	DefaultBaseDerivationPath = DerivationPath{
		hdkeychain.HardenedKeyStart + 84,
		hdkeychain.HardenedKeyStart + 0,
	}
)
View Source
var (
	// DummyFeeAmount is used as the fee amount to cover when coin-selecting the
	// inputs to use to cover the true fee amount, which, instead, is calculated
	// with more precision from the tx size.
	// The real fee amount strictly depends on the number of tx inputs and
	// outputs, and even input types.
	// This value is thought for transactions on TDEX network, whose are composed
	// by at least 3 inputs and 6 outputs.
	// If all inputs are wrapped or native segwit, is shouls be unlikely for the
	// tx virtual size to be higher than 700 vB/sat, taking into account that
	// this pkg supports ONLY native segwit scripts/addresses.
	// For any other case this value can be tweaked at will.
	DummyFeeAmount uint64 = 700
)

Functions

func Decrypt

func Decrypt(opts DecryptOpts) (string, error)

Decrypt decrypts (with AES-128) a cyphertext with the provided passphrase

func DeriveKey

func DeriveKey(passphrase, salt []byte) ([]byte, []byte, error)

DeriveKey derives a 32 byte array key from a custom passhprase

func Encrypt

func Encrypt(opts EncryptOpts) (string, error)

Encrypt encrypts (with AES-128) a plaintext with the provided passphrase

func EstimateTxSize added in v0.3.1

func EstimateTxSize(
	inScriptTypes, inAuxiliaryRedeemScriptSize, inAuxiliaryWitnessSize,
	outScriptTypes, outAuxiliaryRedeemScriptSize []int,
) int

EstimateTxSize makes an estimation of the virtual size of a transaction for which is required to specify the type of the inputs and outputs according to those of the Bitcoin standard (P2PK, P2PKH, P2MS, P2SH(P2WPKH), P2SH(P2WSH), P2WPKH, P2WSH). In case some inputs or outputs are of type P2MS, it is mandatory to pass their redeem script sizes as auxiliary slices in accordance.

func ExtractBlindingDataFromTx added in v0.3.2

func ExtractBlindingDataFromTx(
	psetBase64 string,
	inBlindingKeys, outBlindingKeys map[string][]byte,
) (inBlindingData, outBlindingData map[int]BlindingData, err error)

ExtractBlindingDataFromTx unblinds the confidential inputs of the given tx (in pset's base64 format) with the provided blinding keys. The revealed data are returned mapped by output script.

func FinalizeAndExtractTransaction

func FinalizeAndExtractTransaction(opts FinalizeAndExtractTransactionOpts) (string, string, error)

FinalizeAndExtractTransaction attempts to finalize the provided partial transaction and eventually extracts the final transaction and returns it in hex string format, along with its transaction id

func NewMnemonic

func NewMnemonic(opts NewMnemonicOpts) ([]string, error)

NewMnemonic returns a new mnemonic as a list of words

Types

type BlindSwapTransactionWithDataOpts added in v0.3.1

type BlindSwapTransactionWithDataOpts struct {
	PsetBase64         string
	InputBlindingData  map[int]BlindingData
	OutputBlindingKeys map[string][]byte
	Attempts           int
}

BlindSwapTransactionWithDataOpts is the struct given to BlindSwapTransactionWithKeys method

type BlindSwapTransactionWithKeysOpts added in v0.3.1

type BlindSwapTransactionWithKeysOpts struct {
	PsetBase64         string
	InputBlindingKeys  map[string][]byte
	OutputBlindingKeys map[string][]byte
	Attempts           int
}

BlindSwapTransactionWithKeysOpts is the struct given to BlindSwapTransactionWithKeys method

type BlindTransactionWithDataOpts added in v0.3.1

type BlindTransactionWithDataOpts struct {
	PsetBase64         string
	InputBlindingData  map[int]BlindingData
	OutputBlindingKeys [][]byte
	Attempts           int
}

BlindTransactionWithDataOpts is the struct given to BlindTransactionWithData method

type BlindTransactionWithKeysOpts added in v0.3.1

type BlindTransactionWithKeysOpts struct {
	PsetBase64         string
	OutputBlindingKeys [][]byte
	Attempts           int
}

BlindTransactionWithKeysOpts is the struct given to BlindTransactionWithKeys method

type BlindingData added in v0.3.1

type BlindingData struct {
	Asset         string
	Amount        uint64
	AssetBlinder  []byte
	AmountBlinder []byte
}

func (BlindingData) ToBlindingData added in v0.3.1

func (b BlindingData) ToBlindingData() pset.BlindingData

type DecryptOpts

type DecryptOpts struct {
	CypherText string
	Passphrase string
}

DecryptOpts is the struct given to Decrypt method

type DerivationPath

type DerivationPath []uint32

DerivationPath is the internal representation of a hierarchical deterministic wallet account

func ParseDerivationPath

func ParseDerivationPath(strPath string) (DerivationPath, error)

ParseDerivationPath converts a derivation path string to the internal binary representation

func (DerivationPath) String

func (path DerivationPath) String() string

String converts a binary derivation path to its canonical representation

type DeriveBlindingKeyPairOpts

type DeriveBlindingKeyPairOpts struct {
	Script []byte
}

DeriveBlindingKeyPairOpts is the struct given to DeriveBlindingKeyPair method

type DeriveConfidentialAddressOpts

type DeriveConfidentialAddressOpts struct {
	DerivationPath string
	Network        *network.Network
}

DeriveConfidentialAddressOpts is the struct given to DeriveConfidentialAddress method

type DeriveSigningKeyPairOpts

type DeriveSigningKeyPairOpts struct {
	DerivationPath string
}

DeriveSigningKeyPairOpts is the struct given to DeriveSigningKeyPair method

type EncryptOpts

type EncryptOpts struct {
	PlainText  string
	Passphrase string
}

EncryptOpts is the struct given to Encrypt method

type ExtendedKeyOpts

type ExtendedKeyOpts struct {
	Account uint32
}

ExtendedKeyOpts is the struct given to ExtendedPrivateKey and ExtendedPublicKey methods

type FinalizeAndExtractTransactionOpts

type FinalizeAndExtractTransactionOpts struct {
	PsetBase64 string
}

FinalizeAndExtractTransactionOpts is the struct given to FinalizeAndExtractTransaction method

type NewMnemonicOpts

type NewMnemonicOpts struct {
	EntropySize int
}

type NewWalletFromMnemonicOpts

type NewWalletFromMnemonicOpts struct {
	SigningMnemonic  []string
	BlindingMnemonic []string
}

NewWalletFromMnemonicOpts is the struct given to the NewWalletFromMnemonicOpts method

type NewWalletOpts

type NewWalletOpts struct {
	ExtraMnemonic bool
}

NewWalletOpts is the struct given to the NewWallet method

type SignInputOpts

type SignInputOpts struct {
	PsetBase64     string
	InIndex        uint32
	DerivationPath string
}

SignInputOpts is the struct given to SignInput method

type SignTransactionOpts

type SignTransactionOpts struct {
	PsetBase64        string
	DerivationPathMap map[string]string
}

SignTransactionOpts is the struct given to SignTransaction method

type UpdateSwapTxOpts

type UpdateSwapTxOpts struct {
	PsetBase64           string
	Unspents             []explorer.Utxo
	InputAmount          uint64
	InputAsset           string
	OutputAmount         uint64
	OutputAsset          string
	OutputDerivationPath string
	ChangeDerivationPath string
	Network              *network.Network
}

UpdateSwapTxOpts is the struct given to UpdateTx method

type UpdateTxOpts

type UpdateTxOpts struct {
	PsetBase64           string
	Unspents             []explorer.Utxo
	Outputs              []*transaction.TxOutput
	ChangePathsByAsset   map[string]string
	MilliSatsPerBytes    int
	Network              *network.Network
	WantPrivateBlindKeys bool
	WantChangeForFees    bool
	SubtractFees         bool
}

UpdateTxOpts is the struct given to UpdateTx method

type UpdateTxResult

type UpdateTxResult struct {
	PsetBase64                string
	SelectedUnspents          []explorer.Utxo
	ChangeOutputsBlindingKeys map[string][]byte
	FeeAmount                 uint64
}

UpdateTxResult is the struct returned by UpdateTx method. PsetBase64: the updated partial transaction with new inputs and outputs SelectedUnspents: the list of unspents added as inputs to the pset ChangeOutptusBlindingKeys: the list of blinding keys for the evnutal

change(s) added to the pset

FeeAmount: the amount in satoshi of the fee amount that can added in a

second moment giving the user the possibility to eventually blind
the pset first

type Wallet

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

Wallet data structure allows to create a new wallet from seed/mnemonic, derive signing and blinding key pairs, and manage those keys to blind and sign transactions

func NewWallet

func NewWallet(opts NewWalletOpts) (*Wallet, error)

NewWallet creates a new wallet holding signing/blinding mnemonic and seed

func NewWalletFromMnemonic

func NewWalletFromMnemonic(opts NewWalletFromMnemonicOpts) (*Wallet, error)

NewWalletFromMnemonic generates the sigining and (optionally) blinding seeds from the corresponding mnemonics provided

func (*Wallet) BlindSwapTransactionWithData added in v0.3.1

func (w *Wallet) BlindSwapTransactionWithData(opts BlindSwapTransactionWithDataOpts) (string, error)

BlindSwapTransactionWithData blinds the outputs of a swap transaction. Instead of unblinding the input proofs with keys, blinding data (asset, value and respective blinders) are provided as a map script -> blinding_data.

func (*Wallet) BlindSwapTransactionWithKeys added in v0.3.1

func (w *Wallet) BlindSwapTransactionWithKeys(opts BlindSwapTransactionWithKeysOpts) (string, error)

BlindSwapTransactionWithKeys blinds the outputs of a swap transaction. Since this type of transaciton is composed of inputs and outputs owned by 2 different parties, the blinding keys for inputs and outputs are provided through maps outputScript -> blinding key. Note that all the blinding keys provided must be private, thus for the outputs this function will use the provided blinding keys to get the list of all public keys. This of course also means that no blinding keys are derived internally, but these are all provided as function arguments.

func (*Wallet) BlindTransactionWithData added in v0.3.1

func (w *Wallet) BlindTransactionWithData(opts BlindTransactionWithDataOpts) (string, error)

BlindTransactionWithData blinds the outputs of the provided partial transaction by using the provided input blinding data.

func (*Wallet) BlindTransactionWithKeys added in v0.3.1

func (w *Wallet) BlindTransactionWithKeys(opts BlindTransactionWithKeysOpts) (string, error)

BlindTransactionWithKeys blinds the outputs of the provided partial transaction by deriving the blinding keys from the output scripts following SLIP-77 spec

func (*Wallet) BlindingMnemonic

func (w *Wallet) BlindingMnemonic() ([]string, error)

BlindingMnemonic is getter for blinding mnemonic

func (*Wallet) CreateTx

func (w *Wallet) CreateTx() (string, error)

CreateTx crafts a new empty partial transaction

func (*Wallet) DeriveBlindingKeyPair

func (w *Wallet) DeriveBlindingKeyPair(opts DeriveBlindingKeyPairOpts) (
	*btcec.PrivateKey,
	*btcec.PublicKey,
	error,
)

DeriveBlindingKeyPair derives the SLIP77 blinding key pair from the provided output script

func (*Wallet) DeriveConfidentialAddress

func (w *Wallet) DeriveConfidentialAddress(
	opts DeriveConfidentialAddressOpts,
) (string, []byte, error)

DeriveConfidentialAddress derives both the signing and blinding pubkeys to then generate the corresponding confidential address

func (*Wallet) DeriveSigningKeyPair

func (w *Wallet) DeriveSigningKeyPair(opts DeriveSigningKeyPairOpts) (
	*btcec.PrivateKey,
	*btcec.PublicKey,
	error,
)

DeriveSigningKeyPair derives the key pair of the provided derivation path

func (*Wallet) ExtendedPrivateKey

func (w *Wallet) ExtendedPrivateKey(opts ExtendedKeyOpts) (string, error)

ExtendedPrivateKey returns the signing extended private key in base58 format for the provided account index

func (*Wallet) ExtendedPublicKey

func (w *Wallet) ExtendedPublicKey(opts ExtendedKeyOpts) (string, error)

ExtendedPublicKey returns the signing extended public key in base58 format for the provided account index

func (*Wallet) MasterBlindingKey added in v0.6.0

func (w *Wallet) MasterBlindingKey() (string, error)

MasterBlindingKey returns the master blinding key of the wallet in hex format.

func (*Wallet) SignInput

func (w *Wallet) SignInput(opts SignInputOpts) (string, error)

SignInput takes care of producing (and verifying) a signature for a specific input of a partial transaction with the provided private key

func (*Wallet) SignTransaction

func (w *Wallet) SignTransaction(opts SignTransactionOpts) (string, error)

SignTransaction signs all inputs of a partial transaction using the keys derived with the help of the map script:derivation_path

func (*Wallet) SigningMnemonic

func (w *Wallet) SigningMnemonic() ([]string, error)

SigningMnemonic is getter for signing mnemonic

func (*Wallet) UpdateSwapTx

func (w *Wallet) UpdateSwapTx(opts UpdateSwapTxOpts) (string, []explorer.Utxo, error)

UpdateSwapTx takes care of adding inputs and output(s) to the provided partial transaction. Inputs are selected so that the minimum number of them is used to reach the target InputAmount. The subset of selected inputs is returned along with the updated partial transaction

func (*Wallet) UpdateTx

func (w *Wallet) UpdateTx(opts UpdateTxOpts) (*UpdateTxResult, error)

UpdateTx adds the provided outputs and eventual inputs to the provided partial transaction. The assets of the inputs to add is determined by the assets of the provided outputs. For each asset type a derivation path for an eventual change must be provided. Its also mandatory to provide a derivation path for the LBTC asset type since this method takes care of adding inputs (if necessary) for covering the fee amount. While the list of outputs is required, the list of unspents is optional. In case it's not empty, a coin selection is performed for each type of asset, adding the eventual change output to the list of outputs to add to the tx. In the other case, only the outputs are added to the provided partial transaction.

Jump to

Keyboard shortcuts

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