stellarbase

package module
v0.0.0-...-8462f89 Latest Latest
Warning

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

Go to latest
Published: Oct 9, 2015 License: Apache-2.0, MIT Imports: 8 Imported by: 0

README

Go Stellar Base

Build Status

STATUS: This library is currently in alpha testing. It has support reading/writing xdr, and can sign and hash byte slices in accordance with the stellar protocol, but does not yet have the necessary helpers to make constructing valid transactions easy.

The stellar-base library is the lowest-level stellar helper library. It consists of classes to read, write, hash, and sign the xdr structures that are used in stellar-core.

Installation

go get github.com/stellar/go-stellar-base

Usage

Let's first decode a transaction (taken from stellar-core's txhistory table):

func ExampleDecodeTransaction() {
	data := "rqN6LeOagjxMaUP96Bzfs9e0corNZXzBWJkFoK7kvkwAAAAKAAAAAwAAAAEAAAAAAAA" +
		"AAAAAAAEAAAAAAAAAAW5oJtVdnYOVdZqtXpTHBtbcY0mCmfcBIKEgWnlvFIhaAAAAAA" +
		"AAAAAC+vCAAAAAAa6jei0gQGmrUfm+o2CMv/w32YzJgGYlmgG6CUW3FwyD6AZ/5TtPZ" +
		"qEt9kyC3GJeXfzoS667ZPhPUSNjSWgAeDPHFLcM"

	rawr := strings.NewReader(data)
	b64r := base64.NewDecoder(base64.StdEncoding, rawr)

	var tx xdr.TransactionEnvelope
	bytesRead, err := xdr.Unmarshal(b64r, &tx)

	fmt.Printf("read %d bytes\n", bytesRead)

	if err != nil {
		log.Fatal(err)
	}

	fmt.Printf("This tx has %d operations\n", len(tx.Tx.Operations))
	// Output: read 180 bytes
	// This tx has 1 operations
}

Now, the low-level creation of a TransactionEnvelope:

// ExampleLowLevelTransaction creates and signs a simple transaction, and then
// encodes it into a hex string capable of being submitted to stellar-core.
//
// It uses the low-level xdr facilities to create the transaction.
func ExampleLowLevelTransaction() {
	spub, spriv, err := GenerateKeyFromSeed("SDOTALIMPAM2IV65IOZA7KZL7XWZI5BODFXTRVLIHLQZQCKK57PH5F3H")

	if err != nil {
		log.Fatal(err)
	}

	dpub, _, err := GenerateKeyFromSeed("sfkPCKA6XgaeHZH3NE57i3QrjVcw61c1noWQCgnHa6KJP2BrbXD")

	if err != nil {
		log.Fatal(err)
	}

	op := xdr.PaymentOp{
		Destination: dpub.KeyData(),
		Currency:    xdr.NewCurrencyCurrencyTypeNative(),
		Amount:      50 * 10000000,
	}

	tx := xdr.Transaction{
		SourceAccount: spub.KeyData(),
		Fee:           10,
		SeqNum:        xdr.SequenceNumber(1),
		Memo:          xdr.NewMemoMemoNone(),
		Operations: []xdr.Operation{
			{Body: xdr.NewOperationBodyPayment(op)},
		},
	}

	var txBytes bytes.Buffer
	_, err = xdr.Marshal(&txBytes, tx)
	if err != nil {
		log.Fatal(err)
	}

	txHash := Hash(txBytes.Bytes())
	signature := spriv.Sign(txHash[:])

	ds := xdr.DecoratedSignature{
		Hint:      spriv.Hint(),
		Signature: xdr.Uint512(signature),
	}

	txe := xdr.TransactionEnvelope{
		Tx:         tx,
		Signatures: []xdr.DecoratedSignature{ds},
	}

	var txeBytes bytes.Buffer
	_, err = xdr.Marshal(&txeBytes, txe)
	if err != nil {
		log.Fatal(err)
	}

	txeHex := hex.EncodeToString(txeBytes.Bytes())

	fmt.Printf("tx hex: %s", txeHex)
	// Output: tx hex: 3658fe7598d20c7a6de3297f84bc52bd2a1ec8c0f1f6c5b41cc1c7571b4331f00000000a000000000000000100000000000000000000000100000000000000012d24692ed08bbf679ba199448870d2191e876fecd92fdd9f6d274da4e6de134100000000000000001dcd650000000001dd302d0c0cee527cf02f6a0aec6916966298712914c63e3c57de74a6e27c29ea234a555fcc36533417afe4e1147815a42529fbca3429bc7caf0a06dc6b383ca6e9d4d80f
}


Contributing

Please see CONTRIBUTING.md for details.

Documentation

Index

Constants

View Source
const One = 10000000

One is the value of one whole unit of currency. Stellar uses 7 fixed digits for fractional values, thus One is 10 million (10^7)

View Source
const RawSeedSize = 32

RawSeedSize is 32 bytes, the size of a raw (i.e. not encoded as a strkey) ed25519 seed

Variables

This section is empty.

Functions

func AddressToAccountId

func AddressToAccountId(address string) (result xdr.AccountId, err error)

AddressToAccountId converts the provided address into a xdr.AccountId

func GenerateKeyFromRawSeed

func GenerateKeyFromRawSeed(rawSeed RawSeed) (publicKey PublicKey, privateKey PrivateKey, err error)

func GenerateKeyFromSeed

func GenerateKeyFromSeed(seed string) (publicKey PublicKey, privateKey PrivateKey, err error)

func GenerateRandomKey

func GenerateRandomKey() (publicKey PublicKey, privateKey PrivateKey, err error)

func Hash

func Hash(message []byte) [32]byte

Hash returns a 32-byte hash for the provided message using the secure hash algorithm chosen for the Stellar network (SHA-256)

Types

type PrivateKey

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

PrivateKey is the type representing an ed25519 private key, as used by the stellar network

func (*PrivateKey) Address

func (privateKey *PrivateKey) Address() string

Address returns the StrKey encoded form of the PublicKey associated with this PrivateKey.

func (*PrivateKey) Hint

func (privateKey *PrivateKey) Hint() [4]byte

func (*PrivateKey) KeyData

func (privateKey *PrivateKey) KeyData() [ed25519.PrivateKeySize]byte

KeyData returns the raw key data

func (*PrivateKey) PublicKey

func (privateKey *PrivateKey) PublicKey() PublicKey

PublicKey returns the raw ed25519 public key for this PrivateKey

func (*PrivateKey) Seed

func (privateKey *PrivateKey) Seed() string

Seed returns the strkey encoded Seed for this PrivateKey

func (*PrivateKey) Sign

func (privateKey *PrivateKey) Sign(message []byte) Signature

type PublicKey

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

PublicKey is the type representing an ed25519 privpublicate key, as used by the stellar network

func (*PublicKey) Address

func (publicKey *PublicKey) Address() string

Address returns the StrKey encoded form of the PublicKey

func (*PublicKey) Hint

func (publicKey *PublicKey) Hint() (r [4]byte)

func (*PublicKey) KeyData

func (publicKey *PublicKey) KeyData() [ed25519.PublicKeySize]byte

KeyData returns the raw key data

func (*PublicKey) Verify

func (publicKey *PublicKey) Verify(message []byte, signature Signature) bool

type RawSeed

type RawSeed [RawSeedSize]byte

RawSeed is a ed25519 seed value

func NewRawSeed

func NewRawSeed(data []byte) (RawSeed, error)

NewRawSeed convertes the provided byte slice into a RawSeed, after confirming it is compatible.

type Signature

type Signature [ed25519.SignatureSize]byte

Signature is a raw ed25519 signature

type Signer

type Signer interface {
	Seed() string
	Hint() [4]byte
	Sign(message []byte) Signature
}

Signer implementors can create signatures

type Verifier

type Verifier interface {
	Address() string
	Verify(message []byte, signature Signature) bool
}

Verifier implementors can validate signatures

Directories

Path Synopsis
Package amount provides utilities for converting numbers to/from the format used internally to stellar-core.
Package amount provides utilities for converting numbers to/from the format used internally to stellar-core.
Package build provides a builder system for constructing various xdr structures used by the stellar network.
Package build provides a builder system for constructing various xdr structures used by the stellar network.
cmd
stellar-sign
stellar-sign is a small interactive utility to help you contribute a signature to a transaction envelope.
stellar-sign is a small interactive utility to help you contribute a signature to a transaction envelope.
Package crc16 is implementation according to CCITT standards.
Package crc16 is implementation according to CCITT standards.
Package strkey is an implementation of StrKey, the address scheme for the StellarNetwork.
Package strkey is an implementation of StrKey, the address scheme for the StellarNetwork.
Package xdr contains the generated code for parsing the xdr structures used for stellar.
Package xdr contains the generated code for parsing the xdr structures used for stellar.

Jump to

Keyboard shortcuts

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