tx

package
v0.0.0-...-e435de1 Latest Latest
Warning

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

Go to latest
Published: Jul 30, 2020 License: BSD-3-Clause Imports: 10 Imported by: 0

README

Build Status GoDoc GitHub license

tx

Overview

This library is for handling bitcoin transactions(tx), including P2PK(publish to public keey), P2SH(publish to script hash) and Micropayment Channel.

Requirements

This requires

  • git
  • go 1.3+

Installation

 $ go get github.com/bitgoin/tx

Example

(This example omits error handlings for simplicity.)

P2PK

import "git.gurkengewuerz.de/bitgoin/utils/tx"

func main(){
    fee:=0.001*Unit
	locktime:=0

	//prepare private key.
	txKey, err := address.FromWIF("some wif", address.BitcoinTest)

	//set UTXOs to be used in P2PK with script.
	//Assume script in UTXOs are default P2PK style.
	//(OP_DUP OP_HASH160 <pubKeyHash> OP_EQUALVERIFY OP_CHECKSIG)
	script, err := tx.DefaultP2PKScript(txKey.PublicKey.Address())

	//prepare unspent transaction outputs with its privatekey.
	coins := tx.UTXOs{
		&tx.UTXO{
			Key:     txKey,
			TxHash:  hash,
			TxIndex: 1,
			Script:  script,
			Value:   68000000 + fee,
		}}

	//prepare send addresses and its amount.
	//last address must be refund address and its amount must be 0.
	send := []*tx.Send{
		&tx.Send{
			Addr:   "n2eMqTT929pb1RDNuqEnxdaLau1rxy3efi",
			Amount: 68000000,
		},
		&Send{
			Addr:   "",
			Amount: 0,
		},
	}

	//get TX.
	tx, err := tx.NewP2PK(fee, coins, locktime, send...)

	//get binary form of tx.
	rawtx, err := tx.Pack()


    //if you want to add custom data to tx  with OP_RETURN.

	//get unsigned TX.
	ntx, used, err := tx.NewP2PKunsign(0.0001*Unit, coins, 0, send...)

	//add custom txout and add it to the tx.
	txout := tx.CustomTx([]byte("some public data"))
	ntx.TxOut = append(ntx.TxOut, txout)

	//sign tx.
	err := tx.FillP2PKsign(ntx, used);
}
P2SH

import "git.gurkengewuerz.de/bitgoin/utils/tx"

func main(){
    fee:=0.001*Unit
	locktime:=0

	//prepare private key.
	txKey1, err := address.FromWIF("some wif1", address.BitcoinTest)
	txKey2, err := address.FromWIF("some wif2", address.BitcoinTest)
	txKey3, err := address.FromWIF("some wif3", address.BitcoinTest)

	//set UTXOs to be used in P2SH with script.
	//Assume script in UTXOs are default P2PK style.
	//(OP_DUP OP_HASH160 <pubKeyHash> OP_EQUALVERIFY OP_CHECKSIG)
	script, err := tx.DefaultP2PKScript(txKey.PublicKey.Address())
	coins := tx.UTXOs{
		&tx.UTXO{
			Key:     txKey,
			TxHash:  hash,
			TxIndex: 1,
			Script:  script,
			Value:   68000000 + fee,
		}}

    //prepare M of N contract info.
	//set publickeys, amount, M, and fee. 
	pi := &PubInfo{
		Pubs:   []*address.PublicKey{pkey2.PublicKey, pkey3.PublicKey, pkey.PublicKey},
		Amount: 200 * Unit,
		M:      2,
		Fee:    fee,
	}

	//make bond transaction from coins.
	txout, err := pi.BondTx(utxos, pkey.PublicKey.Address(), locktime)

	//prepare send addresses and its amount.
	//last address must be refund address and its amount must be 0.
	send := []*tx.Send{
		&tx.Send{
			Addr:   "n2eMqTT929pb1RDNuqEnxdaLau1rxy3efi",
			Amount: 68000000,
		},
		&Send{
			Addr:   "",
			Amount: 0,
		},
	}

    //get 2(=M) signs 
	sig2, err := pi.SignMultisig(pkey2, locktime, send...)
	sig, err := pi.SignMultisig(pkey, locktime, send...)

    //make transaction which spends bond.
	//signs must be filled in same order as Pubinfo.Pubs.
	tx, err := pi.SpendBondTx(0, [][]byte{sig2, nil, sig}, send...)
}
Micropayment

import "git.gurkengewuerz.de/bitgoin/utils/tx"

func main(){
    fee:=0.001*Unit

	//prepare private key.
	txKey1, err := address.FromWIF("some wif1", address.BitcoinTest)
	txKey2, err := address.FromWIF("some wif2", address.BitcoinTest)

	//set UTXOs with script.
	//Assume script in UTXOs are default P2PK style.
	//(OP_DUP OP_HASH160 <pubKeyHash> OP_EQUALVERIFY OP_CHECKSIG)
	script, err := tx.DefaultP2PKScript(txKey.PublicKey.Address())
	coins := tx.UTXOs{
		&tx.UTXO{
			Key:     txKey,
			TxHash:  hash,
			TxIndex: 1,
			Script:  script,
			Value:   68000000 + fee,
		}}

    //prepare micropayer or micropayee.
	bondAmount:=200*Unit
	payer := NewMicroPayer(txKey, txKey2.PublicKey, bondAmount, fee)
	payee := NewMicroPayee(txKey.PublicKey, txKey2, bondAmount, fee)

    //payer creates bond and refund tx without sign.
	//refund tx will be validated after one hour.
	locktime:=uint32(time.Now().Add(time.Hour).Unix())
	bond, refund, err := payer.CreateBond(locktime, utxos, txKey.PublicKey.Address())

    //payee gets payer's refund tx by some way and signs it.
	sign, err := payee.SignRefund(refund, locktime)

    //payer gets and checks payee's sign and sings the refund.
    err := payer.SignRefund(refund, sign)

    //payee gets payer's bond and checks it.
	err := payee.CheckBond(refund, bond)

    for {
        //payee starts to work and after a while requests to increment his amount.
		time.Sleep(time.Hour)
	    //payer calculates sign for incremented tx.
	    signIP, err := payer.SignIncremented(0.001 * Unit)

    	//payee get payer's sign ,checks it, and get incremented tx.
	    tx, err := payee.IncrementedTx(0.001*Unit, signIP)
    }

}
  • Note

Payer must send refund tx after locktime.

http://chimera.labs.oreilly.com/books/1234000001802/ch05.html#tx_propagation

Transactions with locktime specifying a future block or time must be held by the originating system and transmitted to the bitcoin network only after they become valid.

Contribution

Improvements to the codebase and pull requests are encouraged.

Documentation

Index

Constants

View Source
const (

	//Unit is unit to convert BTC to satoshi
	Unit = 100000000
)

Variables

This section is empty.

Functions

func DefaultP2PKScript

func DefaultP2PKScript(btcadr string) ([]byte, error)

DefaultP2PKScript returns default p2pk script.

func FillP2PKsign

func FillP2PKsign(result *Tx, used []*UTXO) error

FillP2PKsign embeds sign script to result Tx.

func NewP2PKunsign

func NewP2PKunsign(fee uint64, coins UTXOs, locktime uint32, sends ...*Send) (*Tx, []*UTXO, error)

NewP2PKunsign creates msg.Tx from send infos without signing tx.. last index of sends must be refund address, and its amount must be 0..

func Reverse

func Reverse(bs []byte) []byte

Reverse reverse bits.

Types

type MicroPayee

type MicroPayee mpay

MicroPayee is struct for payee of micropayment.

func NewMicroPayee

func NewMicroPayee(payer *address.PublicKey, payee *address.PrivateKey, amount uint64, fee uint64) *MicroPayee

NewMicroPayee returns struct for payee.

func (*MicroPayee) CheckBond

func (m *MicroPayee) CheckBond(refund, bond *Tx) error

CheckBond checks and sets bond tx.

func (*MicroPayee) Filter

func (m *MicroPayee) Filter() ([]byte, []byte)

Filter returns redeem script and its hash, which payee should wait for..

func (*MicroPayee) IncrementedTx

func (m *MicroPayee) IncrementedTx(amount uint64, sign []byte) (*Tx, error)

IncrementedTx returns an incremented tx..

func (*MicroPayee) SignRefund

func (m *MicroPayee) SignRefund(refund *Tx, locktime uint32) ([]byte, error)

SignRefund sings refund tx.

type MicroPayer

type MicroPayer mpay

MicroPayer is struct for payer of micropayment.

func NewMicroPayer

func NewMicroPayer(payer *address.PrivateKey, payee *address.PublicKey, amount uint64, fee uint64) *MicroPayer

NewMicroPayer returns struct for payer.

func (*MicroPayer) CreateBond

func (m *MicroPayer) CreateBond(locktime uint32, coins UTXOs, ref string) (*Tx, *Tx, error)

CreateBond returns bond and refund tx for sign.

func (*MicroPayer) SignIncremented

func (m *MicroPayer) SignIncremented(amount uint64) ([]byte, error)

SignIncremented signs incremented tx..

func (*MicroPayer) SignRefund

func (m *MicroPayer) SignRefund(refund *Tx, sign []byte) error

SignRefund signs refund..

type PubInfo

type PubInfo struct {
	Pubs   []*address.PublicKey
	Amount uint64

	Fee uint64
	M   byte
	// contains filtered or unexported fields
}

PubInfo is infor of public key in M of N multisig.

func (*PubInfo) BondTx

func (p *PubInfo) BondTx(coins UTXOs, refund string, locktime uint32) (*Tx, error)

BondTx creates a bond transaction.

func (*PubInfo) SignMultisig

func (p *PubInfo) SignMultisig(priv *address.PrivateKey,
	locktime uint32, sends ...*Send) ([]byte, error)

SignMultisig signs multisig transaction by priv.

func (*PubInfo) SpendBondTx

func (p *PubInfo) SpendBondTx(locktime uint32, sigs [][]byte, sends ...*Send) (*Tx, error)

SpendBondTx creates tx which spends bond. Bond field in PubInfo must be filled previously.

type Send

type Send struct {
	Addr   string
	Amount uint64
}

Send is information about addrress and amount to send.

type Tx

type Tx struct {
	Version  uint32
	TxIn     []*TxIn  `len:"prefix"`
	TxOut    []*TxOut `len:"prefix"`
	Locktime uint32
}

Tx describes a bitcoin transaction,

func NewP2PK

func NewP2PK(fee uint64, coins UTXOs, locktime uint32, sends ...*Send) (*Tx, error)

NewP2PK creates msg.Tx from send infos. last index of sends must be refund address, and its amount must be 0..

func ParseTX

func ParseTX(dat []byte) (*Tx, error)

ParseTX parses byte array and returns Tx struct.

func (*Tx) Hash

func (t *Tx) Hash() []byte

Hash returns hash of the tx.

func (*Tx) Pack

func (t *Tx) Pack() ([]byte, error)

Pack packs Tx struct to bin.

type TxIn

type TxIn struct {
	Hash   []byte `len:"32"`
	Index  uint32
	Script []byte `len:"prefix"`
	Seq    uint32
}

TxIn is the info of input transaction.

type TxOut

type TxOut struct {
	Value  uint64
	Script []byte `len:"prefix"`
}

TxOut is the info of output transaction.

func CustomTx

func CustomTx(data []byte) *TxOut

CustomTx returns OP_RETURN txout with the custome data.

type UTXO

type UTXO struct {
	Key     *address.PrivateKey
	TxHash  []byte
	Value   uint64
	Script  []byte
	TxIndex uint32
}

UTXO represents an available transaction.

type UTXOs

type UTXOs []*UTXO

UTXOs is array of coins.

func (UTXOs) Len

func (c UTXOs) Len() int

func (UTXOs) Less

func (c UTXOs) Less(i, j int) bool

func (UTXOs) Swap

func (c UTXOs) Swap(i, j int)

Jump to

Keyboard shortcuts

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