xcore

package
v0.0.0-...-d2b8996 Latest Latest
Warning

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

Go to latest
Published: Sep 8, 2019 License: BSD-3-Clause Imports: 14 Imported by: 3

Documentation

Index

Constants

View Source
const (
	ER_KEY_SIGNATURE_VERIFY_FAILED                 int = 2101
	ER_ADDRESS_CHECKSUM_MISMATCH                   int = 3101
	ER_ADDRESS_TYPE_UNKNOWN                        int = 3102
	ER_ADDRESS_FORMAT_MALFORMED                    int = 3103
	ER_ADDRESS_SIZE_MALFORMED                      int = 3104
	ER_ADDRESS_WITNESS_VERSION_UNSUPPORTED         int = 3105
	ER_SCRIPT_TYPE_UNKNOWN                         int = 4100
	ER_SCRIPT_STANDARD_ADDRESS_TYPE_UNSUPPORTED    int = 4105
	ER_SCRIPT_STANDARD_PUBKEYS_LE_NREQUIRED        int = 4106
	ER_SCRIPT_SIGNATURE_TYPE_UNKNOW                int = 4107
	ER_TRANSACTION_SIGN_OUT_INDEX                  int = 5000
	ER_TRANSACTION_SIGN_REDEEM_EMPTY               int = 5001
	ER_TRANSACTION_VERIFY_FAILED                   int = 5002
	ER_TRANSACTION_BUILDER_AMOUNT_NOT_ENOUGH_ERROR int = 5101
	ER_TRANSACTION_BUILDER_FROM_EMPTY              int = 5102
	ER_TRANSACTION_BUILDER_CHANGETO_EMPTY          int = 5103
	ER_TRANSACTION_BUILDER_SENDTO_EMPTY            int = 5104
	ER_TRANSACTION_BUILDER_SIGN_KEY_EMPTY          int = 5105
	ER_TRANSACTION_BUILDER_MIN_FEE_NOT_ENOUGH      int = 5106
	ER_TRANSACTION_BUILDER_FEE_TOO_HIGH            int = 5107
	ER_TRANSACTION_PARTIALLY_MAGIC_MISMATCH        int = 5201
	ER_MICROPAYMENT_LOCKTIME_MISMATCH              int = 5301
	ER_MICROPAYMENT_REFUND_BOND_MISMATCH           int = 5302
)

Error type.

View Source
const (
	// Unit -- satoshi unit
	Unit = 1e8
)

Variables

View Source
var Errors = map[int]*xerror.Error{
	ER_KEY_SIGNATURE_VERIFY_FAILED:                 {Num: ER_KEY_SIGNATURE_VERIFY_FAILED, State: "TKS00", Message: "key.signature.verify.failed"},
	ER_ADDRESS_CHECKSUM_MISMATCH:                   {Num: ER_ADDRESS_CHECKSUM_MISMATCH, State: "THK00", Message: "address.checksum.mismatch"},
	ER_ADDRESS_TYPE_UNKNOWN:                        {Num: ER_ADDRESS_TYPE_UNKNOWN, State: "TADDR0", Message: "address.unknown.type[%v]"},
	ER_ADDRESS_FORMAT_MALFORMED:                    {Num: ER_ADDRESS_FORMAT_MALFORMED, State: "TADDR0", Message: "address.unknown.format[%v]"},
	ER_ADDRESS_SIZE_MALFORMED:                      {Num: ER_ADDRESS_SIZE_MALFORMED, State: "TADDR0", Message: "address.size[%v].invalid"},
	ER_ADDRESS_WITNESS_VERSION_UNSUPPORTED:         {Num: ER_ADDRESS_WITNESS_VERSION_UNSUPPORTED, State: "TADDR0", Message: "address.witness.address.version[%v].unsupported"},
	ER_SCRIPT_TYPE_UNKNOWN:                         {Num: ER_SCRIPT_TYPE_UNKNOWN, State: "TS000", Message: "script.unknow.type[%v]"},
	ER_SCRIPT_STANDARD_ADDRESS_TYPE_UNSUPPORTED:    {Num: ER_SCRIPT_STANDARD_ADDRESS_TYPE_UNSUPPORTED, State: "TS000", Message: "script.standard.unsupported.address.type[%v]"},
	ER_SCRIPT_STANDARD_PUBKEYS_LE_NREQUIRED:        {Num: ER_SCRIPT_STANDARD_PUBKEYS_LE_NREQUIRED, State: "TS000", Message: "script.standard.pubkeys[%v].less.than.nrequired[%v]"},
	ER_SCRIPT_SIGNATURE_TYPE_UNKNOW:                {Num: ER_SCRIPT_SIGNATURE_TYPE_UNKNOW, State: "TS000", Message: "script.signature.type.unknow[%v]"},
	ER_TRANSACTION_SIGN_OUT_INDEX:                  {Num: ER_TRANSACTION_SIGN_OUT_INDEX, State: "TTX00", Message: "transaction.sign.idx[%v].out.index[%v]"},
	ER_TRANSACTION_SIGN_REDEEM_EMPTY:               {Num: ER_TRANSACTION_SIGN_REDEEM_EMPTY, State: "TTX00", Message: "transaction.sign.idx[%v].redeem.can.not.be.nil.since.keys[%v]>1"},
	ER_TRANSACTION_VERIFY_FAILED:                   {Num: ER_TRANSACTION_VERIFY_FAILED, State: "TTX00", Message: "transaction.verify.for.input[%v].referencing[%v].at[%v].failed"},
	ER_TRANSACTION_BUILDER_AMOUNT_NOT_ENOUGH_ERROR: {Num: ER_TRANSACTION_BUILDER_AMOUNT_NOT_ENOUGH_ERROR, State: "TTB00", Message: "transaction.builder.amount.totalout[%v].more.than.totalin[%v]"},
	ER_TRANSACTION_BUILDER_FROM_EMPTY:              {Num: ER_TRANSACTION_BUILDER_FROM_EMPTY, State: "TTB00", Message: "transaction.builder.from.is.empty"},
	ER_TRANSACTION_BUILDER_CHANGETO_EMPTY:          {Num: ER_TRANSACTION_BUILDER_CHANGETO_EMPTY, State: "TTB00", Message: "transaction.builder.changeto.is.empty"},
	ER_TRANSACTION_BUILDER_SENDTO_EMPTY:            {Num: ER_TRANSACTION_BUILDER_SENDTO_EMPTY, State: "TTB00", Message: "transaction.builder.sendto.is.empty"},
	ER_TRANSACTION_BUILDER_SIGN_KEY_EMPTY:          {Num: ER_TRANSACTION_BUILDER_SIGN_KEY_EMPTY, State: "TTB00", Message: "transaction.builder.sign.but.key.is.empty.at.input.idx[%v]"},
	ER_TRANSACTION_BUILDER_MIN_FEE_NOT_ENOUGH:      {Num: ER_TRANSACTION_BUILDER_MIN_FEE_NOT_ENOUGH, State: "TTB00", Message: "transaction.builder.min.fee[%v].not.enough.from.change.value[%v]"},
	ER_TRANSACTION_BUILDER_FEE_TOO_HIGH:            {Num: ER_TRANSACTION_BUILDER_FEE_TOO_HIGH, State: "TTB00", Message: "transaction.builder.fee[%v].too.high.than.max.fee[%v]"},
	ER_TRANSACTION_PARTIALLY_MAGIC_MISMATCH:        {Num: ER_TRANSACTION_PARTIALLY_MAGIC_MISMATCH, State: "TTP00", Message: "transaction.partially.request.magic.mismatch.want[%x].got[%x]"},
	ER_MICROPAYMENT_LOCKTIME_MISMATCH:              {Num: ER_MICROPAYMENT_LOCKTIME_MISMATCH, State: "TM000", Message: "micropayment.locktime.mismatch.want[%v].got[%v]"},
	ER_MICROPAYMENT_REFUND_BOND_MISMATCH:           {Num: ER_MICROPAYMENT_REFUND_BOND_MISMATCH, State: "TM000", Message: "micropayment.refund.bond.mismatch"},
}

Errors -- the jump table of error.

Functions

func EstimateFees

func EstimateFees(estimateSize int64, relayFeePerKb int64) int64

EstimateFees -- estimate the fee.

func EstimateNormalSize

func EstimateNormalSize(ins int, outs int) int64

EstimateNormalSize -- estimate the normal size by input and output count.

func EstimateSize

func EstimateSize(txins []*TxIn, txouts []*TxOut) int64

EstimateSize -- returns a worst case serialize size estimate for a signed transaction that spends inputCount number of compressed P2PKH outputs and contains each transaction output from txOuts. The estimated size is incremented for an additional P2PKH change output if addChangeOutput is true.

func GenMultiSigScript

func GenMultiSigScript(nrequired int, pkDatas ...[]byte) ([]byte, error)

GenMultiSigScript -- generates the multisig script for N of M.

OP_2 <A pubkey> <B pubkey> <C pubkey> OP_3 OP_CHECKMULTISIG Format: Required-N pubkey datas pubkey-N OP_CHECKMULTISIG

func MockPublicKeys

func MockPublicKeys(num int) [][]byte

MockPublicKeys -- mock publickey(serialize compressed) for tests.

Types

type Address

type Address interface {
	// ToString returns the string of the address with base58 encoding.
	ToString(net *network.Network) string

	// Hash160 -- hash160 of this address.
	Hash160() []byte

	// LockingScript -- the locking script of this address.
	LockingScript() ([]byte, error)
}

Address -- an interface type for any type of destination a transaction. output may spent to, includes: 1. pay-to-pubkey-hash (P2PKH) 2. pay-to-script-hash (P2SH) 3. pay-to-witness-pubkey-hash (P2WPKH)

func DecodeAddress

func DecodeAddress(addr string, net *network.Network) (Address, error)

DecodeAddress -- decode the string address and returns the Address with a known address type.

func NewPayToPubKeyHashAddress

func NewPayToPubKeyHashAddress(hash160 []byte) Address

NewPayToPubKeyHashAddress -- creates a new PayToPubKeyHashAddress.

func NewPayToScriptHashAddress

func NewPayToScriptHashAddress(hash160 []byte) Address

NewPayToScriptHashAddress -- creates a new PayToScriptHashAddress.

func NewPayToWitnessV0PubKeyHashAddress

func NewPayToWitnessV0PubKeyHashAddress(witnessProgram []byte) Address

NewPayToWitnessV0PubKeyHashAddress -- create new PayToWitnessV0PubKeyHashAddress.

func NewPayToWitnessV0ScriptHashAddress

func NewPayToWitnessV0ScriptHashAddress(witnessProgram []byte) Address

NewPayToWitnessV0ScriptHashAddress -- create new PayToWitnessV0ScriptHashAddress.

type Coin

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

Coin -- the coin output from the pre tx.

func MockP2PKHCoin

func MockP2PKHCoin(hdKey *bip32.HDKey) *Coin

MockP2PKHCoin -- mock p2pkh coin for tests.

func MockP2SHCoin

func MockP2SHCoin(alice *bip32.HDKey, bob *bip32.HDKey, redeem []byte) *Coin

MockP2SHCoin -- mocks p2sh coin for tests.

type CoinBuilder

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

CoinBuilder --

func NewCoinBuilder

func NewCoinBuilder() *CoinBuilder

NewCoinBuilder -- creates new CoinBuilder

func (*CoinBuilder) AddOutput

func (b *CoinBuilder) AddOutput(txID string, n uint32, value uint64, script string) *CoinBuilder

AddOutput -- add output to coin builder.

func (*CoinBuilder) ToCoins

func (b *CoinBuilder) ToCoins() []*Coin

ToCoins -- returns the coin slice.

type Group

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

Group -- the group includes from/sendto/changeto.

type PayToMultiSigScript

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

PayToMultiSigScript -- returns a valid script for a multi-signature redemption where n-required of keys in pubkeys are required to have signed the transaction for success. pubkey is the public-key with compressed/uncompress format.

func NewPayToMultiSigScript

func NewPayToMultiSigScript(nrequired int, pkDatas ...[]byte) *PayToMultiSigScript

NewPayToMultiSigScript -- creates new PayToMultiSigScript.

func (*PayToMultiSigScript) GetLockingScriptBytes

func (s *PayToMultiSigScript) GetLockingScriptBytes() ([]byte, error)

GetLockingScriptBytes -- returns the locking script bytes.

func (*PayToMultiSigScript) Hash160

func (s *PayToMultiSigScript) Hash160() ([]byte, error)

Hash160 -- returns the hash160(locking-script) bytes.

func (*PayToMultiSigScript) Sha256

func (s *PayToMultiSigScript) Sha256() ([]byte, error)

Sha256 -- returns the sha256(locking-script) bytes.

type PayToPubKeyHashAddress

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

PayToPubKeyHashAddress --

func (*PayToPubKeyHashAddress) Hash160

func (a *PayToPubKeyHashAddress) Hash160() []byte

Hash160 -- the address hash160 bytes.

func (*PayToPubKeyHashAddress) LockingScript

func (a *PayToPubKeyHashAddress) LockingScript() ([]byte, error)

LockingScript -- the address locking script.

func (*PayToPubKeyHashAddress) ToString

func (a *PayToPubKeyHashAddress) ToString(net *network.Network) string

ToString -- the implementation method for xcore.Address interface.

type PayToPubKeyHashScript

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

PayToPubKeyHashScript -- P2PKH.

func (*PayToPubKeyHashScript) GetAddress

func (s *PayToPubKeyHashScript) GetAddress() Address

GetAddress -- returns the Address interface.

func (*PayToPubKeyHashScript) GetFinalLockingScriptBytes

func (s *PayToPubKeyHashScript) GetFinalLockingScriptBytes(redeem []byte) ([]byte, error)

GetFinalLockingScriptBytes -- used to get the re-written locking for witness. Same as raw.

func (*PayToPubKeyHashScript) GetRawLockingScriptBytes

func (s *PayToPubKeyHashScript) GetRawLockingScriptBytes() ([]byte, error)

GetRawLockingScriptBytes -- used to get locking script bytes.

OP_DUP OP_HASH160 <PubKeyHash> OP_EQUALVERIFY OP_CHECKSIG Format: - OP_DUP - OP_HASH160 - OP_DATA_20 - 20 bytes pubkey hash - OP_EQUALVERIFY - OP_CHECKSIG

func (*PayToPubKeyHashScript) GetRawUnlockingScriptBytes

func (s *PayToPubKeyHashScript) GetRawUnlockingScriptBytes(signs []PubKeySign, redeem []byte) ([]byte, error)

GetRawUnlockingScriptBytes -- returns the unlocking script bytes. unlocking: <sig> <pubkey> witness: (empty)

func (*PayToPubKeyHashScript) GetScriptVersion

func (s *PayToPubKeyHashScript) GetScriptVersion() ScriptVersion

GetScriptVersion -- used to get the version of this script.

func (*PayToPubKeyHashScript) GetWitnessScriptCode

func (s *PayToPubKeyHashScript) GetWitnessScriptCode(redeem []byte) ([]byte, error)

GetWitnessScriptCode -- used to get the unlocking script bytes of witness program.

func (*PayToPubKeyHashScript) GetWitnessUnlockingScriptBytes

func (s *PayToPubKeyHashScript) GetWitnessUnlockingScriptBytes(signs []PubKeySign, redeem []byte) ([][]byte, error)

GetWitnessUnlockingScriptBytes -- used to get witness script bytes.

func (*PayToPubKeyHashScript) WitnessToUnlockingScriptBytes

func (s *PayToPubKeyHashScript) WitnessToUnlockingScriptBytes(witness [][]byte) ([]byte, error)

WitnessToUnlockingScriptBytes -- converts witness slice to unlocking script. For txn deserialize from hex.

type PayToScriptHashAddress

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

PayToScriptHashAddress --

func (*PayToScriptHashAddress) Hash160

func (a *PayToScriptHashAddress) Hash160() []byte

Hash160 -- returns the address hash160 bytes>

func (*PayToScriptHashAddress) LockingScript

func (a *PayToScriptHashAddress) LockingScript() ([]byte, error)

LockingScript -- the address locking script.

func (*PayToScriptHashAddress) ToString

func (a *PayToScriptHashAddress) ToString(net *network.Network) string

ToString -- the implementation method for xcore.Address interface.

type PayToScriptHashScript

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

PayToScriptHashScript -- P2SH.

func (*PayToScriptHashScript) GetAddress

func (s *PayToScriptHashScript) GetAddress() Address

GetAddress -- returns the Address interface.

func (*PayToScriptHashScript) GetFinalLockingScriptBytes

func (s *PayToScriptHashScript) GetFinalLockingScriptBytes(redeem []byte) ([]byte, error)

GetFinalLockingScriptBytes -- used to get the re-written locking for witness. Same as raw.

func (*PayToScriptHashScript) GetRawLockingScriptBytes

func (s *PayToScriptHashScript) GetRawLockingScriptBytes() ([]byte, error)

GetRawLockingScriptBytes -- used to get locking script bytes.

OP_HASH160 <Hash160(redeemScript)> OP_EQUAL Format: - OP_HASH160 - OP_DATA_20 - 20 bytes script hash - OP_EQUAL

func (*PayToScriptHashScript) GetRawUnlockingScriptBytes

func (s *PayToScriptHashScript) GetRawUnlockingScriptBytes(signs []PubKeySign, redeem []byte) ([]byte, error)

GetRawUnlockingScriptBytes -- used to get raw unlocking script bytes. unlocking: OP_0 <A sig> <C sig> <redeemScript> witness: (empty)

func (*PayToScriptHashScript) GetScriptVersion

func (s *PayToScriptHashScript) GetScriptVersion() ScriptVersion

GetScriptVersion -- used to get the version of this script.

func (*PayToScriptHashScript) GetWitnessScriptCode

func (s *PayToScriptHashScript) GetWitnessScriptCode(redeem []byte) ([]byte, error)

GetWitnessScriptCode -- used to get the witness script for sighash of this txin.

func (*PayToScriptHashScript) GetWitnessUnlockingScriptBytes

func (s *PayToScriptHashScript) GetWitnessUnlockingScriptBytes(signs []PubKeySign, redeem []byte) ([][]byte, error)

GetWitnessUnlockingScriptBytes -- used to get witness script bytes.

func (*PayToScriptHashScript) WitnessToUnlockingScriptBytes

func (s *PayToScriptHashScript) WitnessToUnlockingScriptBytes(witness [][]byte) ([]byte, error)

WitnessToUnlockingScriptBytes -- converts witness slice to unlocking script. For txn deserialize from hex.

type PayToWitnessV0PubKeyHashAddress

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

PayToWitnessV0PubKeyHashAddress -- is an Address for a pay-to-witness-pubkey-hash (P2WPKH) output. Public key -> P2WPKH address witness program = ripemd160(sha256(public key)). Encode into bech32 by providing the witness program, bc as the human readable part and 0 as witness version. https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki

func (*PayToWitnessV0PubKeyHashAddress) Hash160

func (a *PayToWitnessV0PubKeyHashAddress) Hash160() []byte

Hash160 -- the address hash160 bytes.

func (*PayToWitnessV0PubKeyHashAddress) LockingScript

func (a *PayToWitnessV0PubKeyHashAddress) LockingScript() ([]byte, error)

LockingScript -- the address locking script.

func (*PayToWitnessV0PubKeyHashAddress) ToString

ToString -- the implementation method for xcore.Address interface.

type PayToWitnessV0PubKeyHashScript

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

PayToWitnessV0PubKeyHashScript -- P2WPKH (version 0 pay-to-witness-public-key-hash).

func (*PayToWitnessV0PubKeyHashScript) GetAddress

func (s *PayToWitnessV0PubKeyHashScript) GetAddress() Address

GetAddress -- returns the Address interface.

func (*PayToWitnessV0PubKeyHashScript) GetFinalLockingScriptBytes

func (s *PayToWitnessV0PubKeyHashScript) GetFinalLockingScriptBytes(redeem []byte) ([]byte, error)

GetFinalLockingScriptBytes -- used to get the re-written locking for witness. Same as P2PKH.

func (*PayToWitnessV0PubKeyHashScript) GetRawLockingScriptBytes

func (s *PayToWitnessV0PubKeyHashScript) GetRawLockingScriptBytes() ([]byte, error)

GetRawLockingScriptBytes -- used to get locking script bytes.

0 <20-byte-key-hash> Format: - OP_0 - OP_DATA_20 - 20 bytes pubkey hash

func (*PayToWitnessV0PubKeyHashScript) GetRawUnlockingScriptBytes

func (s *PayToWitnessV0PubKeyHashScript) GetRawUnlockingScriptBytes(signs []PubKeySign, redeem []byte) ([]byte, error)

GetRawUnlockingScriptBytes -- used to get raw unlocking script bytes.

func (*PayToWitnessV0PubKeyHashScript) GetScriptVersion

func (s *PayToWitnessV0PubKeyHashScript) GetScriptVersion() ScriptVersion

GetScriptVersion -- used to get the version of this script.

func (*PayToWitnessV0PubKeyHashScript) GetWitnessScriptCode

func (s *PayToWitnessV0PubKeyHashScript) GetWitnessScriptCode(redeem []byte) ([]byte, error)

GetWitnessScriptCode -- used to get the normal locking script bytes of witness program. For P2WPKH witness, we convert it to P2PKH. OP_0 OP_DATA_20 <20-bytes-pubkey-hash> to OP_DUP OP_HASH160 <20-bytes-pubkey-hash> OP_EQUALVERIFY OP_CHECKSIG

func (*PayToWitnessV0PubKeyHashScript) GetWitnessUnlockingScriptBytes

func (s *PayToWitnessV0PubKeyHashScript) GetWitnessUnlockingScriptBytes(signs []PubKeySign, redeem []byte) ([][]byte, error)

GetWitnessUnlockingScriptBytes -- used to get witness script bytes.

func (*PayToWitnessV0PubKeyHashScript) WitnessToUnlockingScriptBytes

func (s *PayToWitnessV0PubKeyHashScript) WitnessToUnlockingScriptBytes(witness [][]byte) ([]byte, error)

WitnessToUnlockingScriptBytes -- converts witness slice to unlocking script. For txn deserialize from hex.

type PayToWitnessV0ScriptHashAddress

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

PayToWitnessV0ScriptHashAddress -- is an Address for a pay-to-witness-script-hash (P2WSH) output. witness program = sha256(script). Encode into bech32 by providing the witness program, bc as the human readable part and 0 as witness version. https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki

func (*PayToWitnessV0ScriptHashAddress) Hash160

func (a *PayToWitnessV0ScriptHashAddress) Hash160() []byte

Hash160 -- the address hash160 bytes. Here is sha256, not hash160.

func (*PayToWitnessV0ScriptHashAddress) LockingScript

func (a *PayToWitnessV0ScriptHashAddress) LockingScript() ([]byte, error)

LockingScript -- the address locking script.

func (*PayToWitnessV0ScriptHashAddress) ToString

ToString -- the implementation method for xcore.Address interface.

type PayToWitnessV0ScriptHashScript

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

PayToWitnessV0ScriptHashScript -- P2WSH (version 0 pay-to-witness-script-hash).

func (*PayToWitnessV0ScriptHashScript) GetAddress

func (s *PayToWitnessV0ScriptHashScript) GetAddress() Address

GetAddress -- returns the Address interface.

func (*PayToWitnessV0ScriptHashScript) GetFinalLockingScriptBytes

func (s *PayToWitnessV0ScriptHashScript) GetFinalLockingScriptBytes(redeem []byte) ([]byte, error)

GetFinalLockingScriptBytes -- used to get the re-written locking for witness.

func (*PayToWitnessV0ScriptHashScript) GetRawLockingScriptBytes

func (s *PayToWitnessV0ScriptHashScript) GetRawLockingScriptBytes() ([]byte, error)

GetRawLockingScriptBytes -- used to get locking script bytes.

0 <32-byte-script-hash> Format: - OP_0 - OP_DATA_32 - 32 bytes sha256<script-hash>

func (*PayToWitnessV0ScriptHashScript) GetRawUnlockingScriptBytes

func (s *PayToWitnessV0ScriptHashScript) GetRawUnlockingScriptBytes(signs []PubKeySign, redeem []byte) ([]byte, error)

GetRawUnlockingScriptBytes -- used to get raw unlocking script bytes. unlocking: (empty)

func (*PayToWitnessV0ScriptHashScript) GetScriptVersion

func (s *PayToWitnessV0ScriptHashScript) GetScriptVersion() ScriptVersion

GetScriptVersion -- used to get the version of this script.

func (*PayToWitnessV0ScriptHashScript) GetWitnessScriptCode

func (s *PayToWitnessV0ScriptHashScript) GetWitnessScriptCode(redeem []byte) ([]byte, error)

GetWitnessScriptCode -- used to get the witness script for sighash of this txin. For P2WSH witness, we convert it to P2SH. OP_0 OP_DATA_20 <20-bytes-script-hash> to OP_HASH160 <Hash160(redeemScript)> OP_EQUAL

func (*PayToWitnessV0ScriptHashScript) GetWitnessUnlockingScriptBytes

func (s *PayToWitnessV0ScriptHashScript) GetWitnessUnlockingScriptBytes(signs []PubKeySign, redeem []byte) ([][]byte, error)

GetWitnessUnlockingScriptBytes -- used to get witness script bytes.

func (*PayToWitnessV0ScriptHashScript) WitnessToUnlockingScriptBytes

func (s *PayToWitnessV0ScriptHashScript) WitnessToUnlockingScriptBytes(witness [][]byte) ([]byte, error)

WitnessToUnlockingScriptBytes -- converts witness slice to unlocking script. For txn deserialize from hex.

type PubKeySign

type PubKeySign struct {
	PubKey    []byte
	Signature []byte
}

PubKeySign -- Public key and signature pair.

type Script

type Script interface {
	// GetAddress used to get the Address interface.
	GetAddress() Address

	// GetRawLockingScriptBytes -- used to get locking script bytes.
	GetRawLockingScriptBytes() ([]byte, error)

	// GetFinalLockingScriptBytes -- used to get the re-written locking for witness.
	// If txin is witness, returns re-written locking script code.
	// If txin is non-witness, returns same as GetLockingScriptBytes.
	GetFinalLockingScriptBytes(redeem []byte) ([]byte, error)

	// GetRawUnlockingScriptBytes -- used to get raw unlocking script bytes.
	GetRawUnlockingScriptBytes(signs []PubKeySign, redeem []byte) ([]byte, error)

	// GetWitnessUnlockingScriptBytes -- used to get witness script bytes.
	GetWitnessUnlockingScriptBytes(signs []PubKeySign, redeem []byte) ([][]byte, error)

	// GetWitnessScriptCode -- used to get the witness script for sighash of this txin.
	// If txin is non-witness, returns nil.
	GetWitnessScriptCode([]byte) ([]byte, error)

	// GetScriptVersion -- used to get the version of the script.
	GetScriptVersion() ScriptVersion

	// WitnessToUnlockingScriptBytes -- converts witness slice to unlocking script.
	// For txn deserialize from hex.
	WitnessToUnlockingScriptBytes(witness [][]byte) ([]byte, error)
}

Script --

func NewPayToPubKeyHashScript

func NewPayToPubKeyHashScript(pubkeyHash []byte) Script

NewPayToPubKeyHashScript -- creates new P2PKH.

func NewPayToScriptHashScript

func NewPayToScriptHashScript(scriptHash []byte) Script

NewPayToScriptHashScript -- creates P2SH.

func NewPayToWitnessV0PubKeyHashScript

func NewPayToWitnessV0PubKeyHashScript(pubkeyHash []byte) Script

NewPayToWitnessV0PubKeyHashScript -- creates new P2WPKH script. pubkeyHash = hash160(pubkey)

func NewPayToWitnessV0ScriptHashScript

func NewPayToWitnessV0ScriptHashScript(scriptHash []byte) Script

NewPayToWitnessV0ScriptHashScript -- creates new P2WSH script. scriptHash = sha256(script).

func ParseLockingScript

func ParseLockingScript(script []byte) (Script, error)

ParseLockingScript -- parse the locking script to script instance.

type ScriptVersion

type ScriptVersion int

ScriptVersion --

const (
	BASE ScriptVersion = iota
	WITNESS_V0
	TAPROOT
	TAPSCRIPT
)

Script version.

type SigHashType

type SigHashType uint32

SigHashType -- hash type bits at the end of a signature.

const (
	SigHashOld          SigHashType = 0x0
	SigHashAll          SigHashType = 0x1
	SigHashNone         SigHashType = 0x2
	SigHashSingle       SigHashType = 0x3
	SigTypeMask         SigHashType = 0x1f
	SigHashAnyOneCanPay SigHashType = 0x80
)

Hash type bits from the end of a signature.

type Transaction

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

Transaction -- the bitcoin transaction.

func NewTransaction

func NewTransaction() *Transaction

NewTransaction -- creates a new Transaction.

func (*Transaction) AddInput

func (tx *Transaction) AddInput(in *TxIn)

AddInput -- add a TxIn.

func (*Transaction) AddOutput

func (tx *Transaction) AddOutput(out *TxOut)

AddOutput -- add a TxOut.

func (*Transaction) BaseSize

func (tx *Transaction) BaseSize() int

BaseSize -- the size of the transaction serialised with the witness data stripped. https://github.com/bitcoin/bips/blob/master/bip-0141.mediawiki

func (*Transaction) Deserialize

func (tx *Transaction) Deserialize(data []byte) error

Deserialize -- decode bytes to witness transaction struct.

func (*Transaction) DeserializeNoWitness

func (tx *Transaction) DeserializeNoWitness(data []byte) error

DeserializeNoWitness -- decode bytes to raw transaction struct.

func (*Transaction) EmbedIdxEcdsaSignature

func (tx *Transaction) EmbedIdxEcdsaSignature(idx int, pubkey *xcrypto.PubKey, ecdsaSig []byte, hashType SigHashType) error

EmbedIdxEcdsaSignature -- used to embed the raw ecdsa signature.

func (*Transaction) EmbedIdxSignature

func (tx *Transaction) EmbedIdxSignature(idx int, signs []PubKeySign) error

EmbedIdxSignature -- build the unlocking with signs for the idx.

func (*Transaction) Fees

func (tx *Transaction) Fees() int

Fees -- returns the transaction fees.

func (*Transaction) HasWitness

func (tx *Transaction) HasWitness() bool

HasWitness -- returns whether the inputs contain witness datas.

func (*Transaction) Hash

func (tx *Transaction) Hash() []byte

Hash -- returns the tx hash.

func (*Transaction) ID

func (tx *Transaction) ID() string

ID -- returns transaction hex with reversed format.

func (*Transaction) RawSignature

func (tx *Transaction) RawSignature(idx int, hashType SigHashType, prv *xcrypto.PrvKey) ([]byte, error)

RawSignature -- sign the idx input and return the signature.

func (*Transaction) RawSignatureHash

func (tx *Transaction) RawSignatureHash(idx int, hashType SigHashType) []byte

RawSignatureHash -- returns transaction hash used to get signed/verified.

func (*Transaction) Serialize

func (tx *Transaction) Serialize() []byte

Serialize -- the new witness serialization defined in BIP0141 and BIP0144.

func (*Transaction) SerializeNoWitness

func (tx *Transaction) SerializeNoWitness() []byte

SerializeNoWitness -- normal serialization. https://en.bitcoin.it/wiki/Protocol_documentation#tx

func (*Transaction) SetLockTime

func (tx *Transaction) SetLockTime(time uint32)

SetLockTime -- set the tx locktime.

func (*Transaction) SetTxIn

func (tx *Transaction) SetTxIn(idx int, amount uint64, script []byte, redeemScript []byte) error

SetTxIn -- set the txin tuples. Used for deserialize the transaction and verify.

func (*Transaction) SetVersion

func (tx *Transaction) SetVersion(ver uint32)

SetVersion -- set the tx version(default 1).

func (*Transaction) SignIndex

func (tx *Transaction) SignIndex(idx int, compressed bool, hashType SigHashType, keys ...*xcrypto.PrvKey) error

SignIndex -- sign specified transaction input with pubkey format.

func (*Transaction) Size

func (tx *Transaction) Size() int

Size -- size in bytes serialized as described in BIP144, including base data and witness data.

func (*Transaction) ToString

func (tx *Transaction) ToString() string

ToString -- returns a human-readable representation of a transaction.

func (*Transaction) Verify

func (tx *Transaction) Verify() error

Verify -- verify the transaction with signature and pubkey.

func (*Transaction) Vsize

func (tx *Transaction) Vsize() int

Vsize -- defined as Transaction weight / 4 (rounded up to the next integer).

func (*Transaction) Weight

func (tx *Transaction) Weight() int

Weight -- defined as Base transaction size * 3 + Total transaction size.

func (*Transaction) WitnessHash

func (tx *Transaction) WitnessHash() []byte

WitnessHash -- returns the tx withess format hash.

func (*Transaction) WitnessID

func (tx *Transaction) WitnessID() string

WitnessID -- returns transaction hex with reversed format.

func (*Transaction) WitnessSignature

func (tx *Transaction) WitnessSignature(idx int, hashType SigHashType, prv *xcrypto.PrvKey) ([]byte, error)

WitnessSignature -- sign the idx input and return the witness signature.

func (*Transaction) WitnessSize

func (tx *Transaction) WitnessSize() int

WitnessSize -- the witness datas serialised size.

func (*Transaction) WitnessV0SignatureHash

func (tx *Transaction) WitnessV0SignatureHash(idx int, hashType SigHashType) []byte

WitnessV0SignatureHash -- returns transaction witness V0 signature hash.

type TransactionBuilder

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

TransactionBuilder --

func NewTransactionBuilder

func NewTransactionBuilder() *TransactionBuilder

NewTransactionBuilder -- creates new TransactionBuilder.

func (*TransactionBuilder) AddCoin

func (b *TransactionBuilder) AddCoin(coin *Coin) *TransactionBuilder

AddCoin -- set the from coin.

func (*TransactionBuilder) AddKeys

func (b *TransactionBuilder) AddKeys(keys ...*xcrypto.PrvKey) *TransactionBuilder

AddKeys -- set the private keys for signing.

func (*TransactionBuilder) AddPushData

func (b *TransactionBuilder) AddPushData(data []byte) *TransactionBuilder

AddPushData -- add pushdata, such as OP_RETURN.

func (*TransactionBuilder) BuildTransaction

func (b *TransactionBuilder) BuildTransaction() (*Transaction, error)

BuildTransaction -- build the transaction.

func (*TransactionBuilder) SendFees

func (b *TransactionBuilder) SendFees(fees uint64) *TransactionBuilder

SendFees -- set the amount fee of this send.

func (*TransactionBuilder) SetChange

func (b *TransactionBuilder) SetChange(addr Address) *TransactionBuilder

SetChange -- set the change address.

func (*TransactionBuilder) SetLockTime

func (b *TransactionBuilder) SetLockTime(lockTime uint32) *TransactionBuilder

SetLockTime -- set the locktime.

func (*TransactionBuilder) SetMaxFees

func (b *TransactionBuilder) SetMaxFees(max int64) *TransactionBuilder

SetMaxFees -- set the max fee, the maxFees is non-zero after setting. If the tx fees larger than the max, it returns error after the building.

func (*TransactionBuilder) SetPubKeyUncompressed

func (b *TransactionBuilder) SetPubKeyUncompressed() *TransactionBuilder

SetPubKeyUncompressed -- set the pubkey to uncompressed format.

func (*TransactionBuilder) SetRedeemScript

func (b *TransactionBuilder) SetRedeemScript(redeem []byte) *TransactionBuilder

SetRedeemScript -- set the redeemscript to group.

func (*TransactionBuilder) SetRelayFeePerKb

func (b *TransactionBuilder) SetRelayFeePerKb(relayFeePerKb int64) *TransactionBuilder

SetRelayFeePerKb -- set the relay fee per Kb.

func (*TransactionBuilder) SetSigHashType

func (b *TransactionBuilder) SetSigHashType(typ SigHashType) *TransactionBuilder

SetSigHashType -- set the SigHashType of this input.

func (*TransactionBuilder) Sign

Sign -- sets the sign flag to tell the builder do sign or not.

func (*TransactionBuilder) Then

Then -- say that one group is end we will start a new one.

func (*TransactionBuilder) To

To -- set the to address and value.

type TxIn

type TxIn struct {
	Hash               []byte // Previous Tx ID(Hash).
	Index              uint32 // Previous tx output index.
	Value              uint64 // Previous tx output amount.
	Sequence           uint32
	SignatureHash      []byte   // Signature hash of the input.
	RedeemScript       []byte   // Previous redeem script.
	Witness            [][]byte // Witness script.
	WitnessScriptCode  []byte   // Witness  script for sighash.
	RawLockingScript   []byte   // Previous tx output script(locking script).
	FinalLockingScript []byte   // The scriptPubKey for verify.
	RawUnlockingScript []byte   // scriptSig.
}

TxIn -- the info of input transaction.

func NewTxIn

func NewTxIn(txHash []byte, n uint32, value uint64, script []byte, redeemScript []byte) (*TxIn, error)

NewTxIn -- build a TxIn.

func (*TxIn) HasWitness

func (txin *TxIn) HasWitness() bool

HasWitness -- check the TxIn is a witness program.

type TxOut

type TxOut struct {
	Value  uint64
	Script []byte
}

TxOut -- the info of output transaction.

func NewTxOut

func NewTxOut(value uint64, script []byte) *TxOut

NewTxOut -- create a new TxOut.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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