txutils

package
v1.6.1 Latest Latest
Warning

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

Go to latest
Published: Mar 10, 2021 License: ISC Imports: 19 Imported by: 3

Documentation

Overview

Copyright (c) 2020 The JaxNetwork developers Use of this source code is governed by an ISC license that can be found in the LICENSE file.

Copyright (c) 2020 The JaxNetwork developers Use of this source code is governed by an ISC license that can be found in the LICENSE file.

Copyright (c) 2020 The JaxNetwork developers Use of this source code is governed by an ISC license that can be found in the LICENSE file.

Copyright (c) 2020 The JaxNetwork developers Use of this source code is governed by an ISC license that can be found in the LICENSE file.

Copyright (c) 2020 The JaxNetwork developers Use of this source code is governed by an ISC license that can be found in the LICENSE file.

Index

Constants

View Source
const (
	OneCoin int64 = 100_000_000
)

Variables

This section is empty.

Functions

func DecodeScript

func DecodeScript(script []byte, net *chaincfg.Params) (*btcjson.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 SendTx added in v0.1.6

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

func SetRedeemScript added in v0.1.6

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

func TxToJson

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

func WaitForTx added in v0.1.6

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

Types

type FeeProviderFunc added in v0.1.6

type FeeProviderFunc func(shardID uint32) (int64, error)

type GetTxOutFunc added in v0.1.6

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

type InMemoryKeystore added in v0.1.6

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

func (*InMemoryKeystore) Address added in v0.1.6

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

func (*InMemoryKeystore) AddressPubKey added in v0.1.6

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

func (*InMemoryKeystore) AddressPubKeyHash added in v0.1.6

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

func (*InMemoryKeystore) AddressString added in v0.1.6

func (kp *InMemoryKeystore) AddressString() string

func (*InMemoryKeystore) FromKeyData added in v0.1.6

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

func (*InMemoryKeystore) GetKey added in v0.1.6

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

func (*InMemoryKeystore) KeyData added in v0.1.6

func (kp *InMemoryKeystore) KeyData() *KeyData

DEPRECATED

type KeyData

type KeyData struct {
	PrivateKey    *btcec.PrivateKey
	Address       btcutil.Address
	AddressPubKey *btcutil.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 btcutil.Address) (*btcec.PrivateKey, bool, error)

type KeyStoreProvider added in v0.1.6

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

func NewInMemoryKeystore added in v0.1.6

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

type ManagerCfg

type ManagerCfg struct {
	Net        string  `json:"net" yaml:"net"`
	ShardID    uint32  `json:"shard_id" yaml:"shard_id"`
	RPC        NodeRPC `json:"rpc" yaml:"rpc"`
	PrivateKey string  `json:"private_key" yaml:"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 NewTxClient added in v0.1.6

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) (*btcjson.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 added in v0.1.6

type NewUTXOProvider interface {
	SelectForAmount(amount int64, shardID uint32, addresses ...string) (txmodels.UTXORows, error)
}

type NodeRPC

type NodeRPC struct {
	Host string `json:"host" yaml:"host"`
	User string `json:"user" yaml:"user"`
	Pass string `json:"pass" yaml:"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) SelectForAmount

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

type TxBuilder added in v0.1.6

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

	// utxo can be spent only after lock ended
	SetTimeLock(lockTime uint32) TxBuilder
	// utxo can be spent only before lock ended, after end money back
	SetTimeLockAllowance(lockTime uint32) TxBuilder

	AddRedeemScripts(redeemScripts ...string) TxBuilder

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

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

	SetUTXOProvider(provider NewUTXOProvider) TxBuilder

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

func NewTxBuilder added in v0.1.6

func NewTxBuilder(net chaincfg.NetName) TxBuilder

type TxClient added in v0.1.6

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) (*btcjson.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 added in v0.1.6

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) AddTimeLockAllowance added in v0.1.6

func (client *TxMan) AddTimeLockAllowance(lockTime uint32) *TxMan

AddTimeLockAllowance adds the lock time to the new tx, and utxo can only be spent until the lock completes, after the time lock ends - only refunds

func (*TxMan) CollectUTXO

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

func (*TxMan) CollectUTXOIndex added in v0.1.6

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

func (*TxMan) CollectUTXOs added in v0.1.6

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

func (*TxMan) DecodeScript

func (client *TxMan) DecodeScript(script []byte) (*btcjson.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() (int64, error)

func (*TxMan) NewEADRegistrationTx added in v0.1.6

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)

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 inputs and TWO 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) ([]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 added in v0.1.6

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) 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) SelectForAmount

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

type UTXOProvider

type UTXOProvider interface {
	SelectForAmount(amount int64, shardID uint32, addresses ...string) (txmodels.UTXORows, 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