outscript

package module
v0.2.25 Latest Latest
Warning

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

Go to latest
Published: Dec 11, 2024 License: MIT Imports: 25 Imported by: 0

README

outscript

Generate potential outscripts for a given public key.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	Formats = map[string]Format{
		"p2pkh":  Format{Bytes{0x76, 0xa9}, IPushBytes{IHash160(IPubKeyComp)}, Bytes{0x88, 0xac}},
		"p2pukh": Format{Bytes{0x76, 0xa9}, IPushBytes{IHash160(IPubKey)}, Bytes{0x88, 0xac}},
		"p2pk":   Format{IPushBytes{IPubKeyComp}, Bytes{0xac}},
		"p2puk":  Format{IPushBytes{IPubKey}, Bytes{0xac}},
		"p2wpkh": Format{Bytes{0}, IPushBytes{IHash160(IPubKeyComp)}},

		"p2sh:p2pkh":  Format{Bytes{0xa9}, IPushBytes{IHash160(Lookup("p2pkh"))}, Bytes{0x87}},
		"p2sh:p2pukh": Format{Bytes{0xa9}, IPushBytes{IHash160(Lookup("p2pukh"))}, Bytes{0x87}},
		"p2sh:p2pk":   Format{Bytes{0xa9}, IPushBytes{IHash160(Lookup("p2pk"))}, Bytes{0x87}},
		"p2sh:p2puk":  Format{Bytes{0xa9}, IPushBytes{IHash160(Lookup("p2puk"))}, Bytes{0x87}},
		"p2sh:p2wpkh": Format{Bytes{0xa9}, IPushBytes{IHash160(Lookup("p2wpkh"))}, Bytes{0x87}},

		"p2wsh:p2pkh":  Format{Bytes{0}, IPushBytes{IHash(Lookup("p2pkh"), sha256.New)}},
		"p2wsh:p2pukh": Format{Bytes{0}, IPushBytes{IHash(Lookup("p2pukh"), sha256.New)}},
		"p2wsh:p2pk":   Format{Bytes{0}, IPushBytes{IHash(Lookup("p2pk"), sha256.New)}},
		"p2wsh:p2puk":  Format{Bytes{0}, IPushBytes{IHash(Lookup("p2puk"), sha256.New)}},
		"p2wsh:p2wpkh": Format{Bytes{0}, IPushBytes{IHash(Lookup("p2wpkh"), sha256.New)}},

		"eth": Format{IHash(IPubKey, newEtherHash)},
	}

	// FormatsPerNetwork is a table listing the typically available formats for each network
	FormatsPerNetwork = map[string][]string{
		"bitcoin":      []string{"p2wpkh", "p2sh:p2wpkh", "p2puk", "p2pk", "p2pukh", "p2pkh"},
		"bitcoin-cash": []string{"p2puk", "p2pk", "p2pukh", "p2pkh"},
		"litecoin":     []string{"p2wpkh", "p2sh:p2wpkh", "p2puk", "p2pk", "p2pukh", "p2pkh"},
		"dogecoin":     []string{"p2puk", "p2pk", "p2pukh", "p2pkh"},
		"evm":          []string{"eth"},
	}
)

Functions

func EvmCall added in v0.2.7

func EvmCall(method string, params ...any) ([]byte, error)

EvmCall generates calldata for a given EVM call, performing absolutely no check on the provided parameters as to whether these match the ABI or not.

Types

type AbiBuffer added in v0.2.7

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

func NewAbiBuffer added in v0.2.7

func NewAbiBuffer(buf []byte) *AbiBuffer

func (*AbiBuffer) AppendAddressAny added in v0.2.7

func (buf *AbiBuffer) AppendAddressAny(v any) error

func (*AbiBuffer) AppendBigInt added in v0.2.7

func (buf *AbiBuffer) AppendBigInt(v *big.Int) error

AppendBigInt appends a big.Int value to the buffer

func (*AbiBuffer) AppendBufferAny added in v0.2.7

func (buf *AbiBuffer) AppendBufferAny(v any) error

func (*AbiBuffer) AppendBytes added in v0.2.7

func (buf *AbiBuffer) AppendBytes(v []byte)

AppendBytes adds a byte buffer as parameter (which will be actually an offset to a later area)

func (*AbiBuffer) AppendUint256Any added in v0.2.7

func (buf *AbiBuffer) AppendUint256Any(v any) error

func (*AbiBuffer) Bytes added in v0.2.7

func (buf *AbiBuffer) Bytes() []byte

Bytes will return the encoded ABI buffer

func (*AbiBuffer) Call added in v0.2.7

func (buf *AbiBuffer) Call(method string) []byte

Call returns a EVM abi-encoded method call

func (*AbiBuffer) EncodeAbi added in v0.2.7

func (buf *AbiBuffer) EncodeAbi(abi string, params ...any) error

EncodeAbi takes as first parameter an abi such as "transfer(address,uint256)" and a matching number of parameters.

func (*AbiBuffer) EncodeAuto added in v0.2.7

func (buf *AbiBuffer) EncodeAuto(params ...any) error

EncodeAuto will encode a bunch of any values into whatever makes sense for the format they are. *big.Int will become uint256, *Script will become addresses, strings and []byte becomes bytes.

Non-compact format is fairly simple since all numeric values are uint256 (including addresses), and only strings/byte arrays are offsets to the end of the buffer where these are stored as length+data+padding

func (*AbiBuffer) EncodeTypes added in v0.2.7

func (buf *AbiBuffer) EncodeTypes(types []string, params ...any) error

type BtcTx added in v0.2.1

type BtcTx struct {
	Version  uint32         `json:"version"`
	In       []*BtcTxInput  `json:"vin"`
	Out      []*BtcTxOutput `json:"vout"`
	Locktime uint32         `json:"locktime"`
}

func (*BtcTx) AddNetOutput added in v0.2.24

func (tx *BtcTx) AddNetOutput(network, address string, amount uint64) error

AddNetOutput adds the specified address as an output to the transaction. The address will be parsed for the given network and an error will be returned if the address is not valid. Passing "auto" as network name disables all checks and it is up to the caller to confirm the address and resulting output is valid first.

func (*BtcTx) AddOutput added in v0.2.7

func (tx *BtcTx) AddOutput(address string, amount uint64) error

AddOutput adds the specified address as an output to the transaction. Note that this will not check if the output is valid for a given network, this is up to the caller to confirm things first.

func (*BtcTx) Bytes added in v0.2.1

func (tx *BtcTx) Bytes() []byte

func (*BtcTx) ClearInputs added in v0.2.1

func (tx *BtcTx) ClearInputs()

ClearInputs removes all the input scripts and witnesses from the transaction. Used during signing.

func (*BtcTx) ComputeSize added in v0.2.16

func (tx *BtcTx) ComputeSize() int

EstimateSize computes the transaction size, taking into account specific rules for segwit.

func (*BtcTx) Dup added in v0.2.1

func (tx *BtcTx) Dup() *BtcTx

Dup duplicates a transaction and its inputs/outputs

func (*BtcTx) HasWitness added in v0.2.1

func (tx *BtcTx) HasWitness() bool

func (*BtcTx) Hash added in v0.2.5

func (tx *BtcTx) Hash() ([]byte, error)

func (*BtcTx) MarshalBinary added in v0.2.4

func (tx *BtcTx) MarshalBinary() ([]byte, error)

func (*BtcTx) ReadFrom added in v0.2.1

func (tx *BtcTx) ReadFrom(r io.Reader) (int64, error)

func (*BtcTx) Sign added in v0.2.1

func (tx *BtcTx) Sign(keys ...*BtcTxSign) error

Sign will perform signature on the transaction

func (*BtcTx) UnmarshalBinary added in v0.2.4

func (tx *BtcTx) UnmarshalBinary(buf []byte) error

type BtcTxInput added in v0.2.1

type BtcTxInput struct {
	TXID      [32]byte
	Vout      uint32
	Script    []byte
	Sequence  uint32
	Witnesses [][]byte
}

func (*BtcTxInput) Bytes added in v0.2.1

func (in *BtcTxInput) Bytes() []byte

func (*BtcTxInput) Dup added in v0.2.1

func (in *BtcTxInput) Dup() *BtcTxInput

func (*BtcTxInput) MarshalJSON added in v0.2.10

func (in *BtcTxInput) MarshalJSON() ([]byte, error)

func (*BtcTxInput) Prefill added in v0.2.16

func (in *BtcTxInput) Prefill(scheme string) error

Prefill will fill the transaction input with empty data matching the expected signature length for the given scheme, if supported

func (*BtcTxInput) ReadFrom added in v0.2.1

func (in *BtcTxInput) ReadFrom(r io.Reader) (int64, error)

type BtcTxOutput added in v0.2.1

type BtcTxOutput struct {
	Amount uint64
	Script []byte
}

func (*BtcTxOutput) Bytes added in v0.2.1

func (out *BtcTxOutput) Bytes() []byte

func (*BtcTxOutput) Dup added in v0.2.1

func (out *BtcTxOutput) Dup() *BtcTxOutput

Dup returns a copy of the BtcTxOutput object; used for transaction signing.

func (*BtcTxOutput) MarshalJSON added in v0.2.10

func (out *BtcTxOutput) MarshalJSON() ([]byte, error)

func (*BtcTxOutput) ReadFrom added in v0.2.1

func (out *BtcTxOutput) ReadFrom(r io.Reader) (int64, error)

ReadFrom parses a transaction output from the provided reader.

type BtcTxSign added in v0.2.1

type BtcTxSign struct {
	Key     crypto.Signer
	Options crypto.SignerOpts
	Scheme  string // "p2pk", etc
	Amount  uint64 // value of input, required for segwit transaction signing
	SigHash uint32
}

type BtcVarInt added in v0.2.1

type BtcVarInt uint64

func (BtcVarInt) Bytes added in v0.2.1

func (v BtcVarInt) Bytes() []byte

func (BtcVarInt) Len added in v0.2.16

func (v BtcVarInt) Len() int

func (*BtcVarInt) ReadFrom added in v0.2.1

func (v *BtcVarInt) ReadFrom(r io.Reader) (int64, error)

func (BtcVarInt) WriteTo added in v0.2.1

func (v BtcVarInt) WriteTo(w io.Writer) (int64, error)

type Bytes added in v0.2.0

type Bytes []byte

func (Bytes) Bytes added in v0.2.0

func (b Bytes) Bytes(*Script) []byte

func (Bytes) String added in v0.2.0

func (b Bytes) String() string

type EvmTx added in v0.2.0

type EvmTx struct {
	Nonce      uint64
	GasTipCap  *big.Int // a.k.a. maxPriorityFeePerGas
	GasFeeCap  *big.Int // a.k.a. maxFeePerGas, correspond to GasFee if tx type is legacy or eip2930
	Gas        uint64   // gas of tx, can be obtained with eth_estimateGas, 21000 if Data is empty
	To         string
	Value      *big.Int
	Data       []byte
	ChainId    uint64    // in legacy tx, chainId is encoded in v before signature
	Type       EvmTxType // type of transaction: legacy, eip2930 or eip1559
	AccessList []any     // TODO
	Signed     bool
	Y, R, S    *big.Int
}

func (*EvmTx) Call added in v0.2.7

func (tx *EvmTx) Call(method string, params ...any) error

func (*EvmTx) Hash added in v0.2.5

func (tx *EvmTx) Hash() ([]byte, error)

func (*EvmTx) MarshalBinary added in v0.2.4

func (tx *EvmTx) MarshalBinary() ([]byte, error)

MarshalBinary transforms the transaction into its binary representation

func (*EvmTx) MarshalJSON added in v0.2.4

func (tx *EvmTx) MarshalJSON() ([]byte, error)

func (*EvmTx) ParseTransaction added in v0.2.0

func (tx *EvmTx) ParseTransaction(buf []byte) error

ParseTransaction will parse an incoming transaction and return an error in case of failure. In case of error, the state of tx is undefined.

func (*EvmTx) RlpFields added in v0.2.0

func (tx *EvmTx) RlpFields() []any

RlpFields returns the Rlp fields for the given transaction, less the signature fields

func (*EvmTx) SenderAddress added in v0.2.0

func (tx *EvmTx) SenderAddress() (string, error)

func (*EvmTx) SenderPubkey added in v0.2.0

func (tx *EvmTx) SenderPubkey() (*secp256k1.PublicKey, error)

func (*EvmTx) Sign added in v0.2.2

func (tx *EvmTx) Sign(key crypto.Signer) error

func (*EvmTx) SignBytes added in v0.2.0

func (tx *EvmTx) SignBytes() ([]byte, error)

SignBytes returns the bytes used to sign the transaction

func (*EvmTx) SignWithOptions added in v0.2.19

func (tx *EvmTx) SignWithOptions(key crypto.Signer, opts crypto.SignerOpts) error

func (*EvmTx) Signature added in v0.2.0

func (tx *EvmTx) Signature() (*secp256k1.Signature, error)

func (*EvmTx) UnmarshalBinary added in v0.2.4

func (tx *EvmTx) UnmarshalBinary(buf []byte) error

UnmarshalBinary implements encoding.BinaryUnmarshaler

func (*EvmTx) UnmarshalJSON added in v0.2.8

func (tx *EvmTx) UnmarshalJSON(b []byte) error

type EvmTxType added in v0.2.0

type EvmTxType int
const (
	EvmTxLegacy EvmTxType = iota
	EvmTxEIP2930
	EvmTxEIP1559
	EvmTxEIP4844 //
)

type Format added in v0.2.0

type Format []Insertable

type IHashInfo added in v0.2.0

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

func IHash added in v0.2.0

func IHash(v Insertable, hash ...func() hash.Hash) IHashInfo

func IHash160 added in v0.2.0

func IHash160(v Insertable) IHashInfo

func (IHashInfo) Bytes added in v0.2.0

func (i IHashInfo) Bytes(s *Script) []byte

func (IHashInfo) String added in v0.2.0

func (i IHashInfo) String() string

type IPushBytes added in v0.2.0

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

func (IPushBytes) Bytes added in v0.2.0

func (i IPushBytes) Bytes(s *Script) []byte

func (IPushBytes) String added in v0.2.0

func (i IPushBytes) String() string

type Insertable added in v0.2.0

type Insertable interface {
	Bytes(*Script) []byte
	String() string
}

type Lookup added in v0.2.0

type Lookup string

func (Lookup) Bytes added in v0.2.0

func (l Lookup) Bytes(s *Script) []byte

func (Lookup) String added in v0.2.0

func (l Lookup) String() string

type Out added in v0.1.2

type Out struct {
	Name   string   `json:"name"`            // p2sh, etc
	Script string   `json:"script"`          // out script
	Flags  []string `json:"flags,omitempty"` // flags
	// contains filtered or unexported fields
}

func GetOuts added in v0.1.2

func GetOuts(pubkey *secp256k1.PublicKey) []*Out

GetOuts returns the potential outputs that can be opened in theory with the given pubkey. p2w* values are "pay to segwit" and can only be used on segwit-enabled chains.

func GuessOut added in v0.2.11

func GuessOut(script []byte, pubkeyhint *secp256k1.PublicKey) *Out

GuessOut will return a out matching the provided script, and attempt to guess the correct type. pubkeyhint can be nil and this function will still be useful, but it won't be able to differenciate between compressed and uncompressed keys if the script contains a hashed key

func ParseBitcoinAddress deprecated added in v0.2.6

func ParseBitcoinAddress(address string) (*Out, error)

ParseBitcoinAddress parses an address in bitcoin format and returns the matching script, accepting also other networks addresses (in which cash a flag will be set)

Deprecated: use ParseBitcoinBasedAddress instead

func ParseBitcoinBasedAddress added in v0.2.23

func ParseBitcoinBasedAddress(network, address string) (*Out, error)

ParseBitcoinBasedAddress parses an address in bitcoin format and returns the matching script, for the specified network. The special value "auto" for network will attempt to detect the network.

func ParseEvmAddress added in v0.2.6

func ParseEvmAddress(address string) (*Out, error)

ParseEvmAddress parses an address to return an Out, supporting various networks.

func (*Out) Address added in v0.2.6

func (out *Out) Address(flags ...string) (string, error)

Address returns an address matching the provided out. Flags will be used for hints if multiple addresses are possible.

func (*Out) Bytes added in v0.1.4

func (o *Out) Bytes() []byte

func (*Out) Hash added in v0.2.25

func (o *Out) Hash() []byte

Hash will extract the hash part of the Out, or return nil if there is no known hash

func (*Out) String added in v0.2.0

func (o *Out) String() string

type PubKeyInsert added in v0.2.0

type PubKeyInsert int
const (
	IPubKeyComp PubKeyInsert = iota
	IPubKey
)

func (PubKeyInsert) Bytes added in v0.2.0

func (pk PubKeyInsert) Bytes(s *Script) []byte

func (PubKeyInsert) String added in v0.2.0

func (pk PubKeyInsert) String() string

type PublicKeyIntf added in v0.2.1

type PublicKeyIntf interface {
	SerializeCompressed() []byte
	SerializeUncompressed() []byte
}

PublicKeyIntf is the interface we need public keys to implement in order to work. Using an interface type allows us to work with many different implementations of secp256k1.

type Script added in v0.2.0

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

func New added in v0.2.0

func New(pubkey PublicKeyIntf) *Script

New returns a new Script object for the given public key, which can be used to generate output scripts

func (*Script) Generate added in v0.2.0

func (s *Script) Generate(name string) []byte

Generate will return the byte value for the specified script type for the current public key

func (*Script) Out added in v0.2.0

func (s *Script) Out(name string) *Out

Out returns a Out object matching the requested script

type Transaction added in v0.2.4

type Transaction interface {
	encoding.BinaryMarshaler
	encoding.BinaryUnmarshaler
	Hash() ([]byte, error)
}

Jump to

Keyboard shortcuts

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