stellarbase

package module
v0.0.0-...-64489fe Latest Latest
Warning

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

Go to latest
Published: Apr 28, 2016 License: Apache-2.0, MIT Imports: 2 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)

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

Types

This section is empty.

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.
This file contains response structs from horizon
This file contains response structs from horizon
Package meta provides helpers for processing the metadata that is produced by stellar-core while processing transactions.
Package meta provides helpers for processing the metadata that is produced by stellar-core while processing transactions.
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