doge

package module
v0.0.12 Latest Latest
Warning

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

Go to latest
Published: Sep 20, 2024 License: MIT Imports: 14 Imported by: 0

README

Doge

Doge is a library of helpful dogecoin primitives written in Go.

Documentation

Index

Constants

View Source
const (
	SerializedBip32KeyLength = 4 + 1 + 4 + 4 + 32 + 33
	HardenedKey              = 0x80000000
)
View Source
const (
	VersionAuxPoW = 256
	CoinbaseVOut  = 0xffffffff
)
View Source
const (
	ECPrivKeyLen            = 32 // bytes.
	ECPubKeyCompressedLen   = 33 // bytes: [x02/x03][32-X] 2=even 3=odd
	ECPubKeyUncompressedLen = 65 // bytes: [x04][32-X][32-Y]
)
View Source
const (
	OP_0     = 0x00 // push empty array
	OP_FALSE = 0x00 // alias
	// 0x01..0x4b push data of `opcode` length (1-75)
	OP_PUSHDATA1           = 0x4c // 1-byte length, push data
	OP_PUSHDATA2           = 0x4d // 2-byte length, push data
	OP_PUSHDATA4           = 0x4e // 4-byte length, push data
	OP_1NEGATE             = 0x4f // push #-1
	OP_RESERVED            = 0x50
	OP_1                   = 0x51 // push #1
	OP_TRUE                = 0x51 // alias
	OP_2                   = 0x52
	OP_3                   = 0x53
	OP_4                   = 0x54
	OP_5                   = 0x55
	OP_6                   = 0x56
	OP_7                   = 0x57
	OP_8                   = 0x58
	OP_9                   = 0x59
	OP_10                  = 0x5a
	OP_11                  = 0x5b
	OP_12                  = 0x5c
	OP_13                  = 0x5d
	OP_14                  = 0x5e
	OP_15                  = 0x5f
	OP_16                  = 0x60
	OP_NOP                 = 0x61
	OP_VER                 = 0x62
	OP_IF                  = 0x63
	OF_NOTIF               = 0x64
	OP_VERIF               = 0x65
	OP_VERNOTIF            = 0x66
	OP_ELSE                = 0x67
	OP_ENDIF               = 0x68
	OP_VERIFY              = 0x69
	OP_RETURN              = 0x6a
	OP_TOALTSTACK          = 0x6b
	OP_FROMALTSTACK        = 0x6c
	OP_IFDUP               = 0x73
	OP_DEPTH               = 0x74
	OP_DROP                = 0x75
	OP_DUP                 = 0x76
	OP_NIP                 = 0x77
	OP_OVER                = 0x78
	OP_PICK                = 0x79
	OP_ROLL                = 0x7a
	OP_ROT                 = 0x7b
	OP_SWAP                = 0x7c
	OP_TUCK                = 0x7d
	OP_2DROP               = 0x6d
	OP_2DUP                = 0x6e
	OP_3DUP                = 0x6f
	OP_2OVER               = 0x70
	OP_2ROT                = 0x71
	OP_2SWAP               = 0x72
	OP_CAT                 = 0x7e
	OP_SUBSTR              = 0x7
	OP_LEFT                = 0x80
	OP_RIGHT               = 0x81
	OP_SIZE                = 0x82
	OP_INVERT              = 0x83
	OP_AND                 = 0x84
	OP_OR                  = 0x85
	OP_XOR                 = 0x86
	OP_EQUAL               = 0x87
	OP_EQUALVERIFY         = 0x88
	OP_RESERVED1           = 0x89
	OP_RESERVED2           = 0x8a
	OP_1ADD                = 0x8b
	OP_1SUB                = 0x8c
	OP_2MUL                = 0x8d
	OP_2DIV                = 0x8e
	OP_NEGATE              = 0x8f
	OP_ABS                 = 0x90
	OP_NOT                 = 0x91
	OP_0NOTEQUAL           = 0x92
	OP_ADD                 = 0x93
	OP_SUB                 = 0x94
	OP_MUL                 = 0x95
	OP_DIV                 = 0x96
	OP_MOD                 = 0x97
	OP_LSHIFT              = 0x98
	OP_RSHIFT              = 0x99
	OP_BOOLAND             = 0x9a
	OP_BOOLOR              = 0x9b
	OP_NUMEQUAL            = 0x9c
	OP_NUMEQUALVERIFY      = 0x9d
	OP_NUMNOTEQUAL         = 0x9e
	OP_LESSTHAN            = 0x9f
	OP_GREATERTHAN         = 0xa0
	OP_LESSTHANOREQUAL     = 0xa1
	OP_GREATERTHANOREQUAL  = 0xa2
	OP_MIN                 = 0xa3
	OP_MAX                 = 0xa4
	OP_WITHIN              = 0xa5
	OP_RIPEMD160           = 0xa6
	OP_SHA1                = 0xa7
	OP_SHA256              = 0xa8
	OP_HASH160             = 0xa9
	OP_HASH256             = 0xaa
	OP_CODESEPARATOR       = 0xab
	OP_CHECKSIG            = 0xac
	OP_CHECKSIGVERIFY      = 0xad
	OP_CHECKMULTISIG       = 0xae
	OP_CHECKMULTISIGVERIFY = 0xaf
	OP_NOP1                = 0xb0
	OP_CHECKLOCKTIMEVERIFY = 0xb1
	OP_CHECKSEQUENCEVERIFY = 0xb2
	OP_NOP4                = 0xb3
	OP_NOP5                = 0xb4
	OP_NOP6                = 0xb5
	OP_NOP7                = 0xb6
	OP_NOP8                = 0xb7
	OP_NOP9                = 0xb8
	OP_NOP10               = 0xb9
	OP_CHECKSIGADD         = 0xba
)

source: https://en.bitcoin.it/wiki/Script

View Source
const (
	OneDoge = int64(100_000_000) // in Koinu
)

Variables

View Source
var CoinbaseTxID = [32]byte{}
View Source
var ErrAnotherSeed = errors.New("cannot derive a valid master key from this seed (generate another seed)")
View Source
var ErrBadSeed = errors.New("bad seed: must be 16-64 bytes")
View Source
var ErrCannotDerive = errors.New("cannot derive private key from public key")
View Source
var ErrHardened = errors.New("cannot derive a public key from a hardened parent key")
View Source
var ErrInvalidPrivateKey = errors.New("invalid private key")
View Source
var ErrNextIndex = errors.New("cannot derive a valid child key at this key index (try the next index)")
View Source
var ErrTooDeep = errors.New("key derivation path is too long (more than 255)")

Functions

func Base58Decode

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

func Base58DecodeCheck

func Base58DecodeCheck(str string) ([]byte, error)

func Base58Encode

func Base58Encode(bytes []byte) string

func Base58EncodeCheck

func Base58EncodeCheck(bytes []byte) string

CAUTION: appends the Checksum to `bytes` if it has sufficient capacity (4 bytes)

func Base58VerifyChecksum

func Base58VerifyChecksum(bytes []byte, str string) error

func ClassifyScript

func ClassifyScript(script []byte, chain *ChainParams) (ScriptType, Address)

func DecodeECPrivKeyWIF

func DecodeECPrivKeyWIF(str string, chain *ChainParams) (ec_priv_key ECPrivKey, out_chain *ChainParams, err error)

chain is optional, will auto-detect if nil.

func ECKeyIsValid

func ECKeyIsValid(pk ECPrivKey) bool

func ECPrivKeyFromBip32WIF

func ECPrivKeyFromBip32WIF(ext_key_wif string, chain *ChainParams) (ec_privkey_wif string, err error)

Given a Bip32 Extended Private Key WIF, extract the WIF-encoded EC Private Key. chain is optional, will auto-detect if nil.

func EncodeBip32WIF

func EncodeBip32WIF(key *Bip32Key) string

EncodeBip32WIF encodes a Bip32Key in WIF format (dgpv,dgub)

func EncodeECPrivKeyUncompressedWIF

func EncodeECPrivKeyUncompressedWIF(key ECPrivKey, chain *ChainParams) string

func EncodeECPrivKeyWIF

func EncodeECPrivKeyWIF(key ECPrivKey, chain *ChainParams) string

func Hash160

func Hash160(bytes []byte) []byte

func HexDecode

func HexDecode(str string) ([]byte, error)

func HexEncode

func HexEncode(bytes []byte) string

func HexEncodeReversed

func HexEncodeReversed(data []byte) string

func IsValidHex

func IsValidHex(hex string) bool

func RIPEMD160

func RIPEMD160(bytes []byte) []byte

func TxHashHex

func TxHashHex(tx []byte) string

func ValidateP2PKH

func ValidateP2PKH(address Address, chain *ChainParams) bool

func ValidateP2SH

func ValidateP2SH(address Address, chain *ChainParams) bool

func VerifyMessage added in v0.0.10

func VerifyMessage(pubKey ECPubKeySchnorr, msg []byte, sig SchnorrSignature) bool

VerifyMessage verifies an arbitrary message with a public key and signature; the signature and public key are also validated, i.e. all parameters can be untrusted data.

Types

type Address

type Address string // Dogecoin address (base-58 Public Key Hash aka PKH)

func Hash160toAddress

func Hash160toAddress(hash []byte, prefix byte) Address

func P2PKHFromECPrivKeyWIF

func P2PKHFromECPrivKeyWIF(ec_priv_key_wif string) (p2pkh Address, err error)

func PubKeyToP2PKH

func PubKeyToP2PKH(key []byte, chain *ChainParams) (Address, error)

PubKeyToP2PKH converst an ECPubKeyCompressed or ECPubKeyUncompressed to a Dogecoin P2PKH Address.

func ScriptToP2SH

func ScriptToP2SH(redeemScript []byte, chain *ChainParams) Address

type Bip32Key

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

https://en.bitcoin.it/wiki/BIP_0032

func Bip32MasterFromSeed added in v0.0.5

func Bip32MasterFromSeed(seed []byte, chain *ChainParams) (*Bip32Key, error)

Bip32MasterFromSeed derives the Bip32 master key from an entropy seed. Note: bip39.SeedFromMnemonic generates an appropriate seed (also GenerateRandomMnemonic)

func DecodeBip32WIF

func DecodeBip32WIF(extendedKey string, chain *ChainParams) (*Bip32Key, error)

DecodeBip32WIF decodes a WIF-encoded Bip32Key (dgpv,dgub) chain is optional, will auto-detect if nil.

func (*Bip32Key) ChainParams added in v0.0.5

func (key *Bip32Key) ChainParams() *ChainParams

ChainParams returns the chain this key belongs to.

func (*Bip32Key) Clear

func (key *Bip32Key) Clear()

func (*Bip32Key) DeriveChild added in v0.0.5

func (key *Bip32Key) DeriveChild(path []uint32, useSLIP10 bool) (*Bip32Key, error)

DeriveChild derives a child key according to BIP-32. Path is a list of child key indexes. If an index is >= HardenedKey, the derived key will be hardened. For a Private Bip32Key we use PrivateCKD on each path element; for a Public key we use PublicCKD.

func (*Bip32Key) EncodeWIF added in v0.0.8

func (key *Bip32Key) EncodeWIF() string

EncodeWIF encodes this key in Bip32 WIF format (dgpv,dgub)

func (*Bip32Key) GetECPrivKey

func (key *Bip32Key) GetECPrivKey() (ECPrivKey, error)

GetECPrivKey gets a copy of the underlying private key.

func (*Bip32Key) GetECPubKey

func (key *Bip32Key) GetECPubKey() ECPubKeyCompressed

GetECPrivKey gets a copy of the underlying public key.

func (*Bip32Key) IsPrivate added in v0.0.5

func (key *Bip32Key) IsPrivate() bool

IsPrivate is true if the Bip32Key holds a Private key (otherwise it holds the corresponding Public key)

func (*Bip32Key) ParentFingerprint added in v0.0.5

func (key *Bip32Key) ParentFingerprint() uint32

ParentFingerprint is the fingerprint for the parent's public key. This is the fingerprint stored in the Bip32 Serialization Format (EncodeBip32WIF)

func (*Bip32Key) PrivateCKD added in v0.0.5

func (key *Bip32Key) PrivateCKD(path []uint32, useSLIP10 bool) (*Bip32Key, error)

func (*Bip32Key) Public added in v0.0.5

func (key *Bip32Key) Public() *Bip32Key

Public returns the Public Bip32Key corresponding to a Private Bip32Key. If key is already a Public Bip32Key, the same *Bip32Key is returned.

func (*Bip32Key) PublicCKD added in v0.0.5

func (key *Bip32Key) PublicCKD(path []uint32, useSLIP10 bool) (*Bip32Key, error)

func (*Bip32Key) ThisKeyFingerprint added in v0.0.5

func (key *Bip32Key) ThisKeyFingerprint() uint32

ThisKeyFingerprint is the fingerprint for this key's public key. NOTE: this is not the ParentFingerprint() sored in Bip23 WIF format! This is primarily included for tests.

type Block

type Block struct {
	Header BlockHeader
	AuxPoW *MerkleTx // if IsAuxPoW()
	Tx     []BlockTx
}

func DecodeBlock

func DecodeBlock(blockBytes []byte) Block

type BlockHeader

type BlockHeader struct {
	Version    uint32
	PrevBlock  []byte // 32 bytes
	MerkleRoot []byte // 32 bytes
	Timestamp  uint32
	Bits       uint32
	Nonce      uint32
}

func (*BlockHeader) IsAuxPoW

func (b *BlockHeader) IsAuxPoW() bool

type BlockTx

type BlockTx struct {
	Version  uint32
	VIn      []BlockTxIn
	VOut     []BlockTxOut
	LockTime uint32
	TxID     string // hex, computed from tx data
}

func DecodeTx

func DecodeTx(txBytes []byte) BlockTx

type BlockTxIn

type BlockTxIn struct {
	TxID     []byte // 32 bytes
	VOut     uint32
	Script   []byte // varied length
	Sequence uint32
}

type BlockTxOut

type BlockTxOut struct {
	Value  int64
	Script []byte // varied length
}

type ChainParams

type ChainParams struct {
	ChainName                string
	GenesisBlock             string
	P2PKH_Address_Prefix     byte
	P2SH_Address_Prefix      byte
	PKey_Prefix              byte
	Bip32_PrivKey_Prefix     uint32
	Bip32_PubKey_Prefix      uint32
	Bip32_WIF_PrivKey_Prefix string
	Bip32_WIF_PubKey_Prefix  string
}
var BitcoinMainChain ChainParams = ChainParams{
	ChainName:                "btc_main",
	GenesisBlock:             "000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f",
	P2PKH_Address_Prefix:     0x00,
	P2SH_Address_Prefix:      0x05,
	PKey_Prefix:              0x80,
	Bip32_PrivKey_Prefix:     0x0488ADE4,
	Bip32_PubKey_Prefix:      0x0488B21E,
	Bip32_WIF_PrivKey_Prefix: "xxxx",
	Bip32_WIF_PubKey_Prefix:  "xxxx",
}

Used in tests only.

var BitcoinTestChain ChainParams = ChainParams{
	ChainName:                "btc_test",
	GenesisBlock:             "000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f",
	P2PKH_Address_Prefix:     0x00,
	P2SH_Address_Prefix:      0x05,
	PKey_Prefix:              0x80,
	Bip32_PrivKey_Prefix:     0x0488ADE4,
	Bip32_PubKey_Prefix:      0x0488B21E,
	Bip32_WIF_PrivKey_Prefix: "xxxx",
	Bip32_WIF_PubKey_Prefix:  "xxxx",
}

Used in tests only.

var DogeMainNetChain ChainParams = ChainParams{
	ChainName:                "doge_main",
	GenesisBlock:             "1a91e3dace36e2be3bf030a65679fe821aa1d6ef92e7c9902eb318182c355691",
	P2PKH_Address_Prefix:     0x1e,
	P2SH_Address_Prefix:      0x16,
	PKey_Prefix:              0x9e,
	Bip32_PrivKey_Prefix:     0x02fac398,
	Bip32_PubKey_Prefix:      0x02facafd,
	Bip32_WIF_PrivKey_Prefix: "dgpv",
	Bip32_WIF_PubKey_Prefix:  "dgub",
}
var DogeRegTestChain ChainParams = ChainParams{
	ChainName:                "doge_regtest",
	GenesisBlock:             "3d2160a3b5dc4a9d62e7e66a295f70313ac808440ef7400d6c0772171ce973a5",
	P2PKH_Address_Prefix:     0x6f,
	P2SH_Address_Prefix:      0xc4,
	PKey_Prefix:              0xef,
	Bip32_PrivKey_Prefix:     0x04358394,
	Bip32_PubKey_Prefix:      0x043587cf,
	Bip32_WIF_PrivKey_Prefix: "tprv",
	Bip32_WIF_PubKey_Prefix:  "tpub",
}
var DogeTestNetChain ChainParams = ChainParams{
	ChainName:                "doge_test",
	GenesisBlock:             "bb0a78264637406b6360aad926284d544d7049f45189db5664f3c4d07350559e",
	P2PKH_Address_Prefix:     0x71,
	P2SH_Address_Prefix:      0xc4,
	PKey_Prefix:              0xf1,
	Bip32_PrivKey_Prefix:     0x04358394,
	Bip32_PubKey_Prefix:      0x043587cf,
	Bip32_WIF_PrivKey_Prefix: "tprv",
	Bip32_WIF_PubKey_Prefix:  "tpub",
}

func ChainFromBip32Version

func ChainFromBip32Version(version uint32, allowNonDoge bool) (bool, *ChainParams)

func ChainFromGenesisHash

func ChainFromGenesisHash(hash string) (*ChainParams, error)

func ChainFromTestNetFlag

func ChainFromTestNetFlag(isTestNet bool) *ChainParams

func ChainFromWIFPrefix

func ChainFromWIFPrefix(bytes []byte, allowNonDoge bool) *ChainParams

CAUTION: the result is a best-guess based on the 'version byte' in the decoded WIF data. Do not rely on the returned ChainParams alone for validation: it will fall back on DogeTestNetChain for unknown version bytes (so verify the version byte or bip32-prefix as well)

func ChainFromWIFString

func ChainFromWIFString(wif string) *ChainParams

CAUTION: the result is a best-guess based on the 'version byte' in the WIF string. Do not rely on the returned ChainParams alone for validation: it will fall back on DogeTestNetChain for unknown version bytes (so verify the version byte or bip32-prefix as well)

type ECPrivKey

type ECPrivKey = *[32]byte // 32 byte big-endian uint256; 0 < pk < n in secp256k1.

func GenerateECPrivKey

func GenerateECPrivKey() (ECPrivKey, error)

type ECPubKeyCompressed

type ECPubKeyCompressed = *[33]byte // 33 bytes with 0x02 or 0x03 prefix.

func ECPubKeyFromECPrivKey

func ECPubKeyFromECPrivKey(pk ECPrivKey) ECPubKeyCompressed

type ECPubKeySchnorr added in v0.0.10

type ECPubKeySchnorr = *[32]byte // 32 byte X-coordinate-only public key, encoded as a big-endian uint256

type ECPubKeyUncompressed

type ECPubKeyUncompressed = *[65]byte // 65 bytes with 0x04 prefix.

type Hash256

type Hash256 = []byte

func DoubleSha256

func DoubleSha256(bytes []byte) Hash256

func Sha256

func Sha256(bytes []byte) Hash256

type KeyType added in v0.0.5

type KeyType = int // keyECPriv,keyECPub,keyBip32Priv,keyBip32Pub,dogeMainNet,dogeTestNet

type MerkleBranch

type MerkleBranch struct {
	Hash     [][]byte // 32 bytes each
	SideMask uint32
}

type MerkleTx

type MerkleTx struct {
	CoinbaseTx       BlockTx
	ParentHash       []byte // 32 bytes
	CoinbaseBranch   MerkleBranch
	BlockchainBranch MerkleBranch
	ParentBlock      BlockHeader
}

type SchnorrSignature added in v0.0.10

type SchnorrSignature = *[64]byte // 64 byte signature, encoding (R.x, s) as big-endian uint256

func SignMessage added in v0.0.10

func SignMessage(privKey ECPrivKey, msg []byte) (SchnorrSignature, error)

SignMessage signs an arbitrary message with a private key. Returns an error if the private key is invalid.

type ScriptType

type ScriptType string

Dogecoin Script Types enum. Inferred from ScriptPubKey scripts by pattern-matching the code (script templates)

const (
	ScriptTypeP2PK     ScriptType = "p2pk"     // TX_PUBKEY (in Core)
	ScriptTypeP2PKH    ScriptType = "p2pkh"    // TX_PUBKEYHASH
	ScriptTypeP2PKHW   ScriptType = "p2wpkh"   // TX_WITNESS_V0_KEYHASH
	ScriptTypeP2SH     ScriptType = "p2sh"     // TX_SCRIPTHASH
	ScriptTypeP2SHW    ScriptType = "p2wsh"    // TX_WITNESS_V0_SCRIPTHASH
	ScriptTypeMultiSig ScriptType = "multisig" // TX_MULTISIG
	ScriptTypeNullData ScriptType = "nulldata" // TX_NULL_DATA
	ScriptTypeCustom   ScriptType = "custom"   // TX_NONSTANDARD
)

ScriptType constants - stored in gigawallet database!

type Stream

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

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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