bitcoin

package
v1.2.15 Latest Latest
Warning

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

Go to latest
Published: Jul 13, 2022 License: AGPL-3.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

Inf is the point at "infinity"

Functions

func Base58Decode

func Base58Decode(s string) ([]byte, error)

Base58Decode converts a base58 representation into byte array

func Base58Encode

func Base58Encode(in []byte) string

Base58Encode converts byte array to base58 string representation

func ExportPrivateKey

func ExportPrivateKey(k *PrivateKey, testnet bool) string

ExportPrivateKey returns a private key in SIPA format

func GetUint

func GetUint(buf []byte, p, n int) (v uint64, err error)

GetUint converts 'n' bytes in a buffer 'buf' starting at position 'p' into an unsigned integer.

func GetVarUint

func GetVarUint(buf []byte, p int) (uint64, int, error)

GetVarUint gets a variable length unsigned integer from a buffer.

func Hash160

func Hash160(data []byte) []byte

Hash160 computes RIPEMD-160(SHA-256(data))

func Hash256

func Hash256(data []byte) []byte

Hash256 computes SHA-256(SHA-256(data))

func PutUint

func PutUint(n uint, j int) []byte

PutUint encodes an uint into a byte array of given length (1,2 or 4).

func PutVarUint

func PutVarUint(n uint) []byte

PutVarUint encodes a var_uint into a byte array.

func RipeMD160

func RipeMD160(data []byte) []byte

RipeMD160 computes RIPEMD160(data)

func Sha1

func Sha1(data []byte) []byte

Sha1 computes SHA1(data)

func Sha256

func Sha256(data []byte) []byte

Sha256 computes SHA-256(data)

func SignY

func SignY(p *Point) (int, error)

SignY of a point (y-coordinate) on the Bitcoin curve: +1 if above x-axis, -1 if below.

func Solve

func Solve(x *math.Int) (*math.Int, bool)

Solve the curve equation for given x-ccordinate (returns positive y)

func Verify

func Verify(key *PublicKey, hash []byte, sig *Signature) bool

Verify a hash value with public key. [http://www.nsa.gov/ia/_files/ecdsa.pdf, page 15f]

Types

type Curve

type Curve struct {
	// P is the generator of the underlying field "F_p"
	// = 2^256 - 2^32 - 2^9 - 2^8 - 2^7 - 2^6 - 2^4 - 1
	P *math.Int
	// Gx is the x-coord of the base point
	Gx *math.Int
	// Gy is the y-coord of the base point
	Gy *math.Int
	// N is the order of G
	N *math.Int
	// curve parameter (=7)
	B *math.Int
}

Curve is the elliptic curve used for Bitcoin (Secp256k1)

func GetCurve

func GetCurve() *Curve

GetCurve returns the singleton curve instance

type DissectedTransaction

type DissectedTransaction struct {
	Version  uint64   // version number of the transaction
	LockTime uint64   // locktime of the transaction
	Content  [][]byte // list of transaction segments
	VinSeq   []uint64 // list of sequence numbers of vin slots
	VinSlot  int      // active vin slot index
	Signable []byte   // signable transaction
}

DissectedTransaction is dissected raw transaction for easier manipulation.

func NewDissectedTransaction

func NewDissectedTransaction(rawHex string) (dt *DissectedTransaction, err error)

NewDissectedTransaction dissects a raw transaction into its defining segments. A binary encoded raw transaction looks like this:

------+-------------+---------+---------------------------------
Level | Field       | length  | Value/Comment
------+-------------+---------+---------------------------------
    0 | VERSION     |    4    | version
    0 | N_VIN       |  var    | Number of Vin defs to follow
------+-------------+---------+---------------------------------
    1 | VIN:TXID    |   32    | Transaction ID
    1 | VIN:N       |    4    | VOUT number in transaction
    1 | VIN:SCRIPT  |         | scriptSig (hex-encoded)
------+-------------+---------+---------------------------------
    2 | SCRIPT:LEN  |   var   | Length of script
    2 | SCRIPT:DATA |   <n>   | Script data
------+-------------+---------+---------------------------------
    1 | VIN:SEQ     |         | sequence number
------+-------------+---------+---------------------------------
    0 | N_VOUT      |  var    | Number of Vout defs to follow
------+-------------+---------+---------------------------------
    1 | VOUT:AMOUNT |    8    | Number of Satoshis (1e-8 btc)
    1 | VOUT:SCRIPT |         | scriptPubkey
------+-------------+---------+---------------------------------
    2 | SCRIPT:LEN  |   var   | Length of script
    2 | SCRIPT:DATA |   <n>   | Script data
------+-------------+---------+---------------------------------
    0 | LOCKTIME    |    4    | locktime
------+-------------+---------+---------------------------------

func (*DissectedTransaction) Bytes

func (d *DissectedTransaction) Bytes() (buf []byte)

Bytes returns a flat binary array of as a raw transaction.

func (*DissectedTransaction) PrepareForSign

func (d *DissectedTransaction) PrepareForSign(vin int, scr []byte) error

PrepareForSign prepares a dissected raw transaction for a given vin script for signature.

func (*DissectedTransaction) Sign

func (d *DissectedTransaction) Sign(prv *PrivateKey, hashType byte) ([]byte, error)

Sign signs a prepared transaction with a private key

func (*DissectedTransaction) Verify

func (d *DissectedTransaction) Verify(pub *PublicKey, sig []byte) (bool, error)

Verify checks the signature on a prepared transaction with a public key.

type Point

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

Point (x,y) on the curve

func GetBasePoint

func GetBasePoint() *Point

GetBasePoint returns the base Point of the curve

func MultBase

func MultBase(k *math.Int) *Point

MultBase multiplies the base Point of the curve with a scalar value k

func NewPoint

func NewPoint(a, b *math.Int) *Point

NewPoint instaniates a new Point

func NewPointFromBytes

func NewPointFromBytes(b []byte) (p *Point, compr bool, err error)

NewPointFromBytes reconstructs a Point from binary representation.

func (*Point) Add

func (p *Point) Add(q *Point) *Point

Add two Points on the curve

func (*Point) Bytes

func (p *Point) Bytes(compressed bool) []byte

Bytes returns a byte representation of Point (compressed or uncompressed).

func (*Point) Double

func (p *Point) Double() *Point

Double a Point on the curve

func (*Point) Equals

func (p *Point) Equals(q *Point) bool

Equals checks if two Points are equal

func (*Point) IsInf

func (p *Point) IsInf() bool

IsInf checks if a Point is at infinity

func (*Point) IsOnCurve

func (p *Point) IsOnCurve() bool

IsOnCurve checks if a Point (x,y) is on the curve

func (*Point) Mult

func (p *Point) Mult(k *math.Int) *Point

Mult multiplies a Point on the curve with a scalar value k using a Montgomery multiplication approach

func (*Point) Neg

func (p *Point) Neg() *Point

Neg returns -P for the point P.

func (*Point) String

func (p *Point) String() string

String returns a human-readable representation of a point.

func (*Point) X

func (p *Point) X() *math.Int

X returns the x-coordinate of a point.

func (*Point) Y

func (p *Point) Y() *math.Int

Y returns the y-coordinate of a point.

type PrivateKey

type PrivateKey struct {
	PublicKey
	D *math.Int
}

PrivateKey is a random factor 'd' for the base Point that yields the associated PublicKey (Point on the curve (x,y) = d*G)

func GenerateKeys

func GenerateKeys(compr bool) *PrivateKey

GenerateKeys creates a new set of keys. [http://www.nsa.gov/ia/_files/ecdsa.pdf] page 19f but with a different range (value 1 and 2 for exponent are not allowed)

func ImportPrivateKey

func ImportPrivateKey(keydata string, testnet bool) (*PrivateKey, error)

ImportPrivateKey imports a private key in SIPA format

func PrivateKeyFromBytes

func PrivateKeyFromBytes(b []byte) (*PrivateKey, error)

PrivateKeyFromBytes returns a private key from a byte representation.

func (*PrivateKey) Bytes

func (k *PrivateKey) Bytes() []byte

Bytes returns a byte representation of private key.

type PublicKey

type PublicKey struct {
	Q            *Point
	IsCompressed bool
}

PublicKey is a Point on the elliptic curve: (x,y) = d*G, where 'G' is the base Point of the curve and 'd' is the secret private factor (private key)

func PublicKeyFromBytes

func PublicKeyFromBytes(b []byte) (*PublicKey, error)

PublicKeyFromBytes returns a public key from a byte representation.

func (*PublicKey) Bytes

func (k *PublicKey) Bytes() []byte

Bytes returns the byte representation of public key.

type Signature

type Signature struct {
	R, S *math.Int
}

Signature is a Bitcoin signature in scripts.

func NewSignatureFromASN1

func NewSignatureFromASN1(b []byte) (*Signature, error)

NewSignatureFromASN1 returns a signature from an ASN.1-encoded sequence.

func Sign

func Sign(key *PrivateKey, hash []byte) *Signature

Sign a hash value with private key. [http://www.nsa.gov/ia/_files/ecdsa.pdf, page 13f]

func (*Signature) Bytes

func (s *Signature) Bytes() ([]byte, error)

Bytes returns an ASN.1-encoded sequence of the signature.

Directories

Path Synopsis
tools

Jump to

Keyboard shortcuts

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