txutils

package
v0.3.14 Latest Latest
Warning

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

Go to latest
Published: Sep 18, 2021 License: ISC Imports: 20 Imported by: 4

Documentation

Index

Constants

View Source
const (
	OneCoin int64 = 100_000_000
)

Variables

View Source
var (
	TxInEstWeight  = 180 // size of regular signed utxo
	TxOutEstWeight = 34  // simple output

)

Functions

func AddressPubKeyFromString

func AddressPubKeyFromString(pubKey string, net *chaincfg.Params) (*jaxutil.AddressPubKey, error)

func DecodeScript

func DecodeScript(script []byte, net *chaincfg.Params) (*jaxjson.DecodeScriptResult, error)

func DecodeTx

func DecodeTx(hexTx string) (*wire.MsgTx, error)

EncodeTx decodes hex-encoded wire.MsgTx.

func EncodeTx

func EncodeTx(tx *wire.MsgTx) string

EncodeTx serializes and encodes to hex wire.MsgTx.

func EstimateFee

func EstimateFee(inCount, outCount int, feeRate int64, addChange bool, shardID uint32) int64

func EstimateFeeForTx

func EstimateFeeForTx(tx *wire.MsgTx, feeRate int64, addChange bool, shardID uint32) int64

func SendTx

func SendTx(txMan *TxMan, senderKP *KeyData, shardID uint32, destination string, amount int64, timeLock uint32) (string, error)

func SetRedeemScript

func SetRedeemScript(utxo txmodels.UTXO, redeemScript string, net *chaincfg.Params) (txmodels.UTXO, error)

func TxToJson

func TxToJson(mtx *wire.MsgTx, chainParams *chaincfg.Params) jaxjson.TxRawDecodeResult

func WaitForTx

func WaitForTx(rpcClient *rpcclient.Client, shardID uint32, txHash string, _ uint32) error

Types

type FeeProviderFunc

type FeeProviderFunc func(shardID uint32) (fee, feeRate int64, err error)

FeeProviderFunc should return non-zero value either fee or feeRate rate. Static fee has main priority, otherwise, the fee will be calculated using feeRate.

type GetTxOutFunc

type GetTxOutFunc func(shardID uint32, txHash *chainhash.Hash, index uint32) (int64, error)

type InMemoryKeystore

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

func (*InMemoryKeystore) Address

func (kp *InMemoryKeystore) Address() jaxutil.Address

func (*InMemoryKeystore) AddressPubKey

func (kp *InMemoryKeystore) AddressPubKey() jaxutil.Address

func (*InMemoryKeystore) AddressPubKeyHash

func (kp *InMemoryKeystore) AddressPubKeyHash() jaxutil.Address

func (*InMemoryKeystore) AddressString

func (kp *InMemoryKeystore) AddressString() string

func (*InMemoryKeystore) FromKeyData

func (*InMemoryKeystore) FromKeyData(data *KeyData) KeyStoreProvider

func (*InMemoryKeystore) GetKey

func (kp *InMemoryKeystore) GetKey(address jaxutil.Address) (*btcec.PrivateKey, bool, error)

func (*InMemoryKeystore) KeyData

func (kp *InMemoryKeystore) KeyData() *KeyData

DEPRECATED

type KeyData

type KeyData struct {
	PrivateKey    *btcec.PrivateKey
	Address       jaxutil.Address
	AddressPubKey *jaxutil.AddressPubKey
}

func GenerateKey

func GenerateKey(networkCfg *chaincfg.Params) (*KeyData, error)

func NewKeyData

func NewKeyData(privateKeyString string, networkCfg *chaincfg.Params) (*KeyData, error)

func (*KeyData) GetKey

func (kd *KeyData) GetKey(address jaxutil.Address) (*btcec.PrivateKey, bool, error)

type KeyStoreProvider

type KeyStoreProvider interface {
	GetKey(jaxutil.Address) (*btcec.PrivateKey, bool, error)
	AddressPubKey() jaxutil.Address
	AddressPubKeyHash() jaxutil.Address
	Address() jaxutil.Address
	AddressString() string
}

func NewInMemoryKeystore

func NewInMemoryKeystore(secret string, net chaincfg.NetName) (KeyStoreProvider, error)

type ManagerCfg

type ManagerCfg struct {
	Net        string  `json:"net" yaml:"net" toml:"net"`
	ShardID    uint32  `json:"shard_id" yaml:"shard_id" toml:"shard_id"`
	RPC        NodeRPC `json:"rpc" yaml:"rpc" toml:"rpc"`
	PrivateKey string  `json:"private_key" yaml:"private_key" toml:"private_key"`
}

func (*ManagerCfg) NetParams

func (cfg *ManagerCfg) NetParams() *chaincfg.Params

type MultiSigAddress

type MultiSigAddress struct {
	Address            string `json:"address"`
	RedeemScript       string `json:"redeemScript"`
	SignaturesRequired int    `json:"signaturesRequired"`
	RawRedeemScript    []byte `json:"-"`
}

func MakeMultiSigScript

func MakeMultiSigScript(keys []string, nRequired int, net *chaincfg.Params) (*MultiSigAddress, error)

type MultiSigLockAddress

type MultiSigLockAddress struct {
	Address               string `json:"address"`
	RefundPublicKey       string `json:"refundPublicKey"`
	RefundDefferingPeriod int32  `json:"refundDefferingPeriod"`
	RedeemScript          string `json:"redeemScript"`
	SignaturesRequired    int    `json:"signaturesRequired"`
	RawRedeemScript       []byte `json:"-"`
}

func MakeMultiSigLockAddress

func MakeMultiSigLockAddress(keys []string, nRequired int, refundPublicKey string,
	refundDefferingPeriod int32, net *chaincfg.Params) (*MultiSigLockAddress, error)

type NewTxClient

type NewTxClient interface {
	SetKey(keystore KeyStoreProvider)
	WithKeys(keystore KeyStoreProvider) NewTxClient

	SetShard(shardID uint32)
	ForShard(shardID uint32) NewTxClient

	RPC() *rpcclient.Client
	NetworkFee() (int64, error)
	DecodeScript(script []byte) (*jaxjson.DecodeScriptResult, error)
	NewMultiSig2of2Address(firstPubKey, second string) (*MultiSigAddress, error)

	CollectUTXO(address string, offset int64) (txmodels.UTXORows, int64, error)
	CollectUTXOs(opts UTXOCollectorOpts) (map[uint32]txmodels.UTXORows, int64, error)
	CollectUTXOIndex(shardID uint32, offset int64,
		filter map[string]bool, index *txmodels.UTXOIndex) (*txmodels.UTXOIndex, int64, error)

	NewTx(tx TxBuilder) (*txmodels.Transaction, error)

	AddSignatureToSwapTx(msgTx *wire.MsgTx, shards []uint32,
		redeemScripts ...string) (*wire.MsgTx, error)
	AddSignatureToTx(msgTx *wire.MsgTx, redeemScripts ...string) (*wire.MsgTx, error)

	SignUTXOForTx(msgTx *wire.MsgTx, utxo txmodels.ShortUTXO, inIndex int, postVerify bool) ([]byte, error)
}

type NewUTXOProvider

type NewUTXOProvider interface {

	// SelectForAmount returns a list of txmodels.UTXO which satisfies the passed amount.
	SelectForAmount(amount int64, shardID uint32, addresses ...string) (txmodels.UTXORows, error)
	// GetForAmount returns a single txmodels.UTXO with Value GreaterOrEq passed amount.
	GetForAmount(amount int64, shardID uint32, addresses ...string) (*txmodels.UTXO, error)
}

type NodeRPC

type NodeRPC struct {
	Host string `json:"host" yaml:"host" toml:"host"`
	User string `json:"user" yaml:"user" toml:"user"`
	Pass string `json:"pass" yaml:"pass" toml:"pass"`
}

type Operator

type Operator struct {
	TxMan *TxMan
}

func NewOperator

func NewOperator(config ManagerCfg) (Operator, error)

func (*Operator) AddSignatureToTx

func (app *Operator) AddSignatureToTx(signer KeyData, txBody, redeemScript string) (*txmodels.Transaction, error)

func (*Operator) NewMultiSigSpendTx

func (app *Operator) NewMultiSigSpendTx(signer KeyData,
	txHash, redeemScript string, outIndex uint32, destination string, amount int64) (*txmodels.Transaction, error)

func (*Operator) SpendUTXO

func (app *Operator) SpendUTXO(signer KeyData,
	txHash string, outIndex uint32, destination string, amount int64) (*txmodels.Transaction, error)

SpendUTXO creates new transaction that spends UTXO with 'outIndex' from transaction with 'txHash'. Into new transactions will be added one Input and one or two Outputs, if 'amount' less than UTXO value.

func (*Operator) UTXOByHash

func (app *Operator) UTXOByHash(txHash string, outIndex uint32, redeemScript string) (*txmodels.UTXO, error)

type SingleUTXO

type SingleUTXO txmodels.UTXO

UTXOFromRows is a wrapper for txmodels.UTXO to implement the UTXOProvider.

func (SingleUTXO) GetForAmount

func (row SingleUTXO) GetForAmount(amount int64, shardID uint32, addresses ...string) (*txmodels.UTXO, error)

func (SingleUTXO) SelectForAmount

func (row SingleUTXO) SelectForAmount(amount int64, shardID uint32, addresses ...string) (txmodels.UTXORows, error)

type TxBuilder

type TxBuilder interface {
	SetType(txVersion int32) TxBuilder
	SetShardID(shardID uint32) TxBuilder
	SwapTx() TxBuilder

	AddRedeemScripts(redeemScripts ...string) TxBuilder

	SetDestination(destination string, amount int64) TxBuilder
	SetDestinationWithUTXO(destination string, amount int64, utxo txmodels.UTXOs) TxBuilder
	SetDestinationAtShard(destination string, amount int64, shardID uint32) TxBuilder

	SetSenders(addresses ...string) TxBuilder
	SetChangeDestination(changeAddresses ...string) TxBuilder

	// SetFee sets static fee with.
	// If it set FeeProviderFunc result will be ignored
	SetFee(shardID uint32, fee int64) TxBuilder
	SetUTXOProvider(provider NewUTXOProvider) TxBuilder

	IntoTx(getFee FeeProviderFunc, kdb txscript.KeyDB) (*wire.MsgTx, error)
}

func NewTxBuilder

func NewTxBuilder(net chaincfg.NetName) TxBuilder

type TxClient

type TxClient interface {
	SetKey(key *KeyData)
	WithKeys(key *KeyData) *TxMan
	ForShard(shardID uint32) *TxMan
	RPC() *rpcclient.Client
	CollectUTXO(address string, offset int64) (txmodels.UTXORows, int64, error)
	CollectUTXOs(opts UTXOCollectorOpts) (map[uint32]txmodels.UTXORows, int64, error)
	CollectUTXOIndex(shardID uint32, offset int64,
		filter map[string]bool, index *txmodels.UTXOIndex) (*txmodels.UTXOIndex, int64, error)
	NetworkFee() (int64, error)
	NewTx(destination string, amount int64, utxoPrv UTXOProvider,
		redeemScripts ...string) (*txmodels.Transaction, error)
	NewSwapTx(spendingMap map[string]txmodels.UTXO, postVerify bool,
		redeemScripts ...string) (*txmodels.SwapTransaction, error)
	DraftToSignedTx(data txmodels.DraftTx, postVerify bool) (*wire.MsgTx, error)
	AddSignatureToSwapTx(msgTx *wire.MsgTx, shards []uint32,
		redeemScripts ...string) (*wire.MsgTx, error)
	AddSignatureToTx(msgTx *wire.MsgTx, redeemScripts ...string) (*wire.MsgTx, error)
	SignUTXOForTx(msgTx *wire.MsgTx, utxo txmodels.ShortUTXO, inIndex int, postVerify bool) ([]byte, error)
	NewMultiSig2of2Address(firstPubKey, second string) (*MultiSigAddress, error)
	DecodeScript(script []byte) (*jaxjson.DecodeScriptResult, error)
}

type TxMan

type TxMan struct {
	NetParams *chaincfg.Params
	// contains filtered or unexported fields
}

func NewTxMan

func NewTxMan(cfg ManagerCfg) (*TxMan, error)

func (*TxMan) AddSignatureToSwapTx

func (client *TxMan) AddSignatureToSwapTx(msgTx *wire.MsgTx, shards []uint32,
	redeemScripts ...string) (*wire.MsgTx, error)

func (*TxMan) AddSignatureToTx

func (client *TxMan) AddSignatureToTx(msgTx *wire.MsgTx, redeemScripts ...string) (*wire.MsgTx, error)

AddSignatureToTx adds signature(s) to wire.TxIn in wire.MsgTx that spend coins with the txscript.MultiSigTy address. NOTE: this method don't work with SwapTx, use the AddSignatureToSwapTx.

func (*TxMan) CollectUTXO

func (client *TxMan) CollectUTXO(address string, offset int64) (txmodels.UTXORows, int64, error)

func (*TxMan) CollectUTXOIndex

func (client *TxMan) CollectUTXOIndex(shardID uint32, offset int64,
	filter map[string]bool, index *txmodels.UTXOIndex) (*txmodels.UTXOIndex, int64, error)

func (*TxMan) CollectUTXOs

func (client *TxMan) CollectUTXOs(opts UTXOCollectorOpts) (map[uint32]txmodels.UTXORows, int64, error)

func (*TxMan) DecodeScript

func (client *TxMan) DecodeScript(script []byte) (*jaxjson.DecodeScriptResult, error)

func (*TxMan) DraftToSignedTx

func (client *TxMan) DraftToSignedTx(data txmodels.DraftTx, postVerify bool) (*wire.MsgTx, error)

func (*TxMan) ForShard

func (client *TxMan) ForShard(shardID uint32) *TxMan

func (*TxMan) NetworkFee

func (client *TxMan) NetworkFee(shardID uint32) (int64, error)

NetworkFee returns Satoshi/bytes. To get size of the tx in bytes use chaindata.GetTransactionWeight(tx).

func (*TxMan) NetworkFeeProvider

func (client *TxMan) NetworkFeeProvider(shardID uint32) (int64, int64, error)

func (*TxMan) NewEADRegistrationTx

func (client *TxMan) NewEADRegistrationTx(amountToLock int64, utxoPrv UTXOProvider,
	destinationsScripts ...[]byte) (*txmodels.Transaction, error)

func (*TxMan) NewMultiSig2of2Address

func (client *TxMan) NewMultiSig2of2Address(firstPubKey, second string) (*MultiSigAddress, error)

func (*TxMan) NewSwapTx

func (client *TxMan) NewSwapTx(spendingMap map[string]txmodels.UTXO, postVerify bool,
	redeemScripts ...string) (*txmodels.SwapTransaction, error)

DEPRECATED NewSwapTx creates new transaction with wire.TxMarkShardSwap marker:

  • data is a map of <Destination Address> => <Source txmodels.UTXO>.
  • redeemScripts is optional, it allows to add proper signatures if source UTXO is a multisig address.

SwapTx is a special tx for atomic swap between chains. It can contain only TWO or FOUR inputs and TWO or FOUR outputs. wire.TxIn and wire.TxOut are strictly associated with each other by index. One pair corresponds to the one chain. The second is for another. | # | --- []TxIn ----- | --- | --- []TxOut ----- | # | | - | ---------------- | --- | ----------------- | - | | 0 | TxIn_0 ∈ Shard_X | --> | TxOut_0 ∈ Shard_X | 0 | | 1 | TxIn_1 ∈ Shard_Y | --> | TxOut_1 ∈ Shard_Y | 1 |

func (*TxMan) NewTx

func (client *TxMan) NewTx(destination string, amount int64, utxoPrv UTXOProvider,
	redeemScripts ...string) (*txmodels.Transaction, error)

func (*TxMan) RPC

func (client *TxMan) RPC() *rpcclient.Client

func (*TxMan) SetKey

func (client *TxMan) SetKey(key *KeyData)

func (*TxMan) SignUTXOForTx

func (client *TxMan) SignUTXOForTx(msgTx *wire.MsgTx, utxo txmodels.ShortUTXO,
	inIndex int, postVerify bool) (*wire.MsgTx, []byte, error)

SignUTXOForTx performs signing of UTXO, adds this signature to redeemTx. Method also supports signing of multiSig UTXOs, so just provide existing signature as prevScript

  • redeemTx is a transaction that will be sent
  • utxo is output that will be spent
  • inIndex is an index, where placed this UTXO
  • prevScript is a SignatureScript made by one or more previous key in case of multiSig UTXO, otherwise it nil
  • postVerify say to check tx after signing

func (*TxMan) WithKeys

func (client *TxMan) WithKeys(key *KeyData) *TxMan

type UTXOCollectorOpts

type UTXOCollectorOpts struct {
	Offset          int64
	OffsetByChain   map[uint32]int64
	AllChains       bool
	Shards          []uint32
	FilterAddresses []string
}

type UTXOFromCSV

type UTXOFromCSV string

UTXOFromCSV is an implementation of UTXOProvider that collects txmodels.UTXORows from CSV file, value of UTXOFromCSV is a path to file.

func (UTXOFromCSV) GetForAmount

func (path UTXOFromCSV) GetForAmount(amount int64, shardID uint32, addresses ...string) (*txmodels.UTXO, error)

func (UTXOFromCSV) SelectForAmount

func (path UTXOFromCSV) SelectForAmount(amount int64, shardID uint32, addresses ...string) (txmodels.UTXORows, error)

type UTXOFromRows

type UTXOFromRows txmodels.UTXORows

UTXOFromRows is a wrapper for txmodels.UTXORows to implement the UTXOProvider.

func (UTXOFromRows) GetForAmount

func (rows UTXOFromRows) GetForAmount(amount int64, shardID uint32, addresses ...string) (*txmodels.UTXO, error)

func (UTXOFromRows) SelectForAmount

func (rows UTXOFromRows) SelectForAmount(amount int64, shardID uint32, addresses ...string) (txmodels.UTXORows, error)

type UTXOProvider

type UTXOProvider interface {
	// SelectForAmount returns a list of txmodels.UTXO which satisfies the passed amount.
	SelectForAmount(amount int64, shardID uint32, addresses ...string) (txmodels.UTXORows, error)
	// GetForAmount returns a single txmodels.UTXO with Value GreaterOrEq passed amount.
	GetForAmount(amount int64, shardID uint32, addresses ...string) (*txmodels.UTXO, error)
}

UTXOProvider is provider that cat give list of txmodels.UTXO for provided amount.

Jump to

Keyboard shortcuts

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