btc

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Aug 28, 2017 License: MIT Imports: 11 Imported by: 0

README

btc

GoDoc wercker status

a package for mapping bitcoin messages and golang struct

$ go get github.com/m0t0k1ch1/btc

Example

package main

import (
	"encoding/json"
	"fmt"
	"log"

	"github.com/m0t0k1ch1/btc"
)

func main() {
	btc.UseTestnet()

	validTxHex := "0100000001ce3cf2e2b334e7e9fa84619469d9edc49368c2f752ea30fb48b080fc794f6d56010000006a473044022065fe1ea4e94a9b44fb62c2b874b63a947504273a60b99b8f7bbf77b4db9331b002205559d8ee93cf341d75866f9eb912af05904fb6eed7372a837308c4e37f3ab58f012103bae5f04799c40862358560e42e441c3080b997a3dec161dd40395e992362bfc9feffffff0200f2052a010000001976a914cbc222711a230ecdd9a5aa65b61ed39c24db2b3488acc08d931a1d0000001976a914426c1ad9fa94f9ea3e6f9248b8bff6768e3ac8c488ac951a1000"

	// hex -> struct
	tx, err := btc.NewTxFromHex(validTxHex)
	if err != nil {
		log.Fatal(err)
	}

	// struct -> hex
	txHex, err := tx.Hex()
	if err != nil {
		log.Fatal(err)
	}

	if txHex == validTxHex {
		b, err := json.Marshal(tx)
		if err != nil {
			log.Fatal(err)
		}
		fmt.Println(string(b))
	}
}
{
  "version": 1,
  "txIns": [
    {
      "txid": "566d4f79fc80b048fb30ea52f7c26893c4edd969946184fae9e734b3e2f23cce",
      "index": 1,
      "script": {
        "hex": "473044022065fe1ea4e94a9b44fb62c2b874b63a947504273a60b99b8f7bbf77b4db9331b002205559d8ee93cf341d75866f9eb912af05904fb6eed7372a837308c4e37f3ab58f012103bae5f04799c40862358560e42e441c3080b997a3dec161dd40395e992362bfc9",
        "asm": "3044022065fe1ea4e94a9b44fb62c2b874b63a947504273a60b99b8f7bbf77b4db9331b002205559d8ee93cf341d75866f9eb912af05904fb6eed7372a837308c4e37f3ab58f01 03bae5f04799c40862358560e42e441c3080b997a3dec161dd40395e992362bfc9"
      },
      "sequence": 4294967294
    }
  ],
  "txOuts": [
    {
      "amount": 5000000000,
      "script": {
        "hex": "76a914cbc222711a230ecdd9a5aa65b61ed39c24db2b3488ac",
        "asm": "OP_DUP OP_HASH160 cbc222711a230ecdd9a5aa65b61ed39c24db2b34 OP_EQUALVERIFY OP_CHECKSIG"
      }
    },
    {
      "amount": 124999929280,
      "script": {
        "hex": "76a914426c1ad9fa94f9ea3e6f9248b8bff6768e3ac8c488ac",
        "asm": "OP_DUP OP_HASH160 426c1ad9fa94f9ea3e6f9248b8bff6768e3ac8c4 OP_EQUALVERIFY OP_CHECKSIG"
      }
    }
  ],
  "lockTime": 1055381
}

Documentation

Index

Constants

View Source
const (
	NetworkTypeEnvKey = "BTC_NETWORK_TYPE"
	NetworkTypeMain   = "mainnet"
	NetworkTypeTest   = "testnet"

	SatoshiPerBtc = 100000000

	TxVersion    int32  = 1
	TxLockTime   uint32 = 0
	TxInSequence uint32 = 4294967295

	CoinBaseTxid = "0000000000000000000000000000000000000000000000000000000000000000"

	AddressVersionMain byte = 0x00
	AddressVersionTest byte = 0x6f

	PkhLength    = 20 // 0x14
	PubKeyLength = 65 // 0x41
)

Variables

View Source
var (
	ErrInvalidPkhLength = errors.New("invalid pkh length")
)

Functions

func Hash160 added in v0.2.0

func Hash160(b []byte) ([]byte, error)

func Sha256Double added in v0.2.0

func Sha256Double(b []byte) ([]byte, error)

func UseTestnet

func UseTestnet() error

Types

type Address added in v0.2.0

type Address string

func (Address) IsValid added in v0.3.0

func (address Address) IsValid() (bool, error)

ref. https://bitcointalk.org/index.php?topic=1026.0

func (Address) Pkh added in v0.2.0

func (address Address) Pkh() (Pkh, error)

func (Address) String added in v0.2.0

func (address Address) String() string

type Block

type Block struct {
	*BlockHeader
	Txes []*Tx
}

func NewBlockFromBytes

func NewBlockFromBytes(b []byte) (*Block, error)

func NewBlockFromHex

func NewBlockFromHex(s string) (*Block, error)

func (*Block) Blockhash

func (block *Block) Blockhash() (string, error)

func (*Block) Bytes

func (block *Block) Bytes() ([]byte, error)

func (*Block) Hex

func (block *Block) Hex() (string, error)

type BlockHeader

type BlockHeader struct {
	Version       int32
	PrevBlockhash string
	MerkleRoot    string
	Timestamp     uint32
	Bits          uint32
	Nonce         uint32
}

func NewBlockHeaderFromBytes

func NewBlockHeaderFromBytes(b []byte) (*BlockHeader, error)

func NewBlockHeaderFromHex

func NewBlockHeaderFromHex(s string) (*BlockHeader, error)

func (*BlockHeader) Blockhash

func (bh *BlockHeader) Blockhash() (string, error)

func (*BlockHeader) Bytes

func (bh *BlockHeader) Bytes() ([]byte, error)

func (*BlockHeader) Hex

func (bh *BlockHeader) Hex() (string, error)

type Btc

type Btc float64

func (Btc) Float64

func (btc Btc) Float64() float64

func (Btc) Satoshi

func (btc Btc) Satoshi() Satoshi

type OpCode

type OpCode byte
const (
	OpZero        OpCode = 0x00
	OpFalse       OpCode = 0x00
	OpDataLenMin  OpCode = 0x01
	OpDataLenMax  OpCode = 0x4b
	OpPushdata1   OpCode = 0x4c
	OpPushdata2   OpCode = 0x4d
	OpPushdata4   OpCode = 0x4e
	OpReturn      OpCode = 0x6a
	OpDrop        OpCode = 0x75
	OpDup         OpCode = 0x76
	OpEqualVerify OpCode = 0x88
	OpHash160     OpCode = 0xa9
	OpCheckSig    OpCode = 0xac
)

func (OpCode) Byte

func (op OpCode) Byte() byte

func (OpCode) Name

func (op OpCode) Name() string

type Pkh added in v0.2.0

type Pkh []byte

func (Pkh) Address added in v0.2.0

func (pkh Pkh) Address() (Address, error)

func (Pkh) Bytes added in v0.2.0

func (pkh Pkh) Bytes() []byte

type Satoshi

type Satoshi int64

func (Satoshi) Btc

func (satoshi Satoshi) Btc() Btc

func (Satoshi) Int64

func (satoshi Satoshi) Int64() int64

type Script

type Script struct {
	Hex string `json:"hex"`
	Asm string `json:"asm"`
}

func NewScriptFromBytes

func NewScriptFromBytes(b []byte) (*Script, error)

func NewScriptFromHex

func NewScriptFromHex(s string) (*Script, error)

func (*Script) Bytes

func (script *Script) Bytes() ([]byte, error)

type Tx

type Tx struct {
	Version  int32    `json:"version"`
	TxIns    []*TxIn  `json:"txIns"`
	TxOuts   []*TxOut `json:"txOuts"`
	LockTime uint32   `json:"lockTime"`
}

func NewTx

func NewTx() *Tx

func NewTxFromBytes

func NewTxFromBytes(b []byte) (*Tx, error)

func NewTxFromHex

func NewTxFromHex(s string) (*Tx, error)

func (*Tx) AddTxIn

func (tx *Tx) AddTxIn(txIn *TxIn)

func (*Tx) AddTxOut

func (tx *Tx) AddTxOut(txOut *TxOut)

func (*Tx) Bytes

func (tx *Tx) Bytes() ([]byte, error)

func (*Tx) Hex

func (tx *Tx) Hex() (string, error)

func (*Tx) Txid

func (tx *Tx) Txid() (string, error)

type TxIn

type TxIn struct {
	Txid     string  `json:"txid"`
	Index    uint32  `json:"index"`
	Script   *Script `json:"script"`
	Sequence uint32  `json:"sequence"`
}

func NewTxIn

func NewTxIn(txid string, index uint32, script *Script) *TxIn

type TxOut

type TxOut struct {
	Amount Satoshi `json:"amount"`
	Script *Script `json:"script"`
}

func NewTxOut

func NewTxOut(amount int64, script *Script) *TxOut

Jump to

Keyboard shortcuts

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