bmutil

package module
v0.0.0-...-bdbe780 Latest Latest
Warning

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

Go to latest
Published: May 18, 2015 License: ISC Imports: 10 Imported by: 0

README

bmutil

Package btcutil provides Bitmessage-specific convenience functions and types. A comprehensive suite of tests is provided to ensure proper functionality. Package btcutil is licensed under the liberal ISC license.

This package was developed for bmd, an alternative full-node implementation of Bitmessage which is under active development by Monetas. Although it was primarily written for bmd, this package has intentionally been designed so it can be used as a standalone package for any projects needing the functionality provided.

It is based off btcutil, which is developed by Conformal.

Documentation

[GoDoc] (http://godoc.org/github.com/monetas/bmutil)

Full go doc style documentation for the project can be viewed online without installing this package by using the GoDoc site here: http://godoc.org/github.com/monetas/bmutil

You can also view the documentation locally once the package is installed with the godoc tool by running godoc -http=":6060" and pointing your browser to http://localhost:6060/pkg/github.com/monetas/bmutil

Installation

$ go get github.com/monetas/bmutil

License

Package btcutil is licensed under the copyfree ISC License.

Documentation

Index

Constants

View Source
const MaxVarIntSize = 9

Maximum size of a variable length integer.

Variables

View Source
var (
	// ErrChecksumMismatch describes an error where decoding failed due
	// to a bad checksum.
	ErrChecksumMismatch = errors.New("checksum mismatch")

	// ErrUnknownAddressType describes an error where an address cannot be
	// decoded as a specific address type due to the string encoding
	// begining with an invalid identifier byte or unsupported version.
	ErrUnknownAddressType = errors.New("unknown address type/version")
)
View Source
var ErrMalformedPrivateKey = errors.New("malformed private key")

ErrMalformedPrivateKey describes an error where a WIF-encoded private key cannot be decoded due to being improperly formatted. This may occur if the byte length is incorrect or an unexpected magic number was encountered.

Functions

func CalcInventoryHash

func CalcInventoryHash(b []byte) []byte

CalcInventoryHash takes double sha512 of the bytes and returns the first half. It is meant to be a helper function for quickly calculating inventory hashes as required by the protocol.

func DecodeWIF

func DecodeWIF(wif string) (*btcec.PrivateKey, error)

DecodeWIF creates a btcec.PrivateKey by decoding the string encoding of the import format. It only supports uncompressed keys.

The WIF string must be a base58-encoded string of the following byte sequence:

  • 1 byte to identify the network, must be 0x80
  • 32 bytes of a binary-encoded, big-endian, zero-padded private key
  • 4 bytes of checksum, must equal the first four bytes of the double SHA256 of every byte before the checksum in this sequence

If the base58-decoded byte sequence does not match this, DecodeWIF will return a non-nil error. ErrMalformedPrivateKey is returned when the WIF is of an impossible length or the expected compressed pubkey magic number does not equal the expected value of 0x01. ErrChecksumMismatch is returned if the expected WIF checksum does not match the calculated checksum.

func DoubleSha512

func DoubleSha512(b []byte) []byte

DoubleSha512 returns the sha512^2 of the bytes

func EncodeWIF

func EncodeWIF(privKey *btcec.PrivateKey) string

EncodeWIF creates the Wallet Import Format string encoding of a WIF structure. See DecodeWIF for a detailed breakdown of the format and requirements of a valid WIF string.

func ReadVarBytes

func ReadVarBytes(r io.Reader, maxAllowed int,
	fieldName string) ([]byte, error)

ReadVarBytes reads a variable length byte array. A byte array is encoded as a varInt containing the length of the array followed by the bytes themselves. An error is returned if the length is greater than the passed maxAllowed parameter which helps protect against memory exhuastion attacks and forced panics thorugh malformed messages. The fieldName parameter is only used for the error message so it provides more context in the error.

func ReadVarInt

func ReadVarInt(r io.Reader) (uint64, error)

ReadVarInt reads a variable length integer from r and returns it as a uint64.

func ReadVarString

func ReadVarString(r io.Reader, maxAllowed int) (string, error)

ReadVarString reads a variable length string from r and returns it as a Go string. A varString is encoded as a varInt containing the length of the string, and the bytes that represent the string itself. An error is returned if the length is greater than the maximum block payload size, since it would not be possible to put a varString of that size into a block anyways and it also helps protect against memory exhaustion attacks and forced panics through malformed messages.

func Sha512

func Sha512(b []byte) []byte

Sha512 returns the sha512 of the bytes

func VarIntSerializeSize

func VarIntSerializeSize(val uint64) int

VarIntSerializeSize returns the number of bytes it would take to serialize val as a variable length integer.

func WriteVarBytes

func WriteVarBytes(w io.Writer, bytes []byte) error

WriteVarBytes serializes a variable length byte array to w as a varInt containing the number of bytes, followed by the bytes themselves.

func WriteVarInt

func WriteVarInt(w io.Writer, val uint64) error

WriteVarInt serializes val to w using a variable number of bytes depending on its value.

func WriteVarString

func WriteVarString(w io.Writer, str string) error

WriteVarString serializes str to w as a varInt containing the length of the string followed by the bytes that represent the string itself.

Types

type Address

type Address struct {
	Version uint64
	Stream  uint64
	Ripe    [20]byte
}

Address represents a Bitmessage address.

func DecodeAddress

func DecodeAddress(address string) (*Address, error)

DecodeAddress decodes the Bitmessage address. The assumption is that input address is properly formatted (according to specs).

func (*Address) Encode

func (addr *Address) Encode() (string, error)

Encode the address to a string that begins from BM- based on the hash. Output: [Varint(addressVersion) Varint(stream) ripe checksum] where the Varints are serialized. Then this byte array is base58 encoded to produce our needed address.

func (*Address) PrivateKey

func (addr *Address) PrivateKey() *btcec.PrivateKey

PrivateKey generates the decryption private key used to decrypt v4 pubkeys and v5 broadcasts originating from the address. Such objects are encrypted with the public key corresponding to this private key as the target key. It is the first half of the double SHA-512 hash of version, stream and ripe concatenated together.

func (*Address) PrivateKeySingleHash

func (addr *Address) PrivateKeySingleHash() *btcec.PrivateKey

PrivateKeySingleHash generates the decryption private key used to decrypt v4 broadcasts originating from the address. They are encrypted with the public key corresponding to this private key as the target key. It is the first half of the SHA-512 hash of version, stream and ripe concatenated together.

func (*Address) Tag

func (addr *Address) Tag() []byte

Tag calculates tag corresponding to the Bitmessage address. According to protocol specifications, it is the second half of the double SHA-512 hash of version, stream and ripe concatenated together.

Directories

Path Synopsis
Package identity is responsible for creation and management of user identities.
Package identity is responsible for creation and management of user identities.
Package pow implements proof-of-work and its checking mechanism.
Package pow implements proof-of-work and its checking mechanism.

Jump to

Keyboard shortcuts

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