Skip to content

Commit

Permalink
network: Adds HashTransaction helper
Browse files Browse the repository at this point in the history
  • Loading branch information
nullstyle committed Sep 1, 2016
1 parent 1f2fbd5 commit 5beff68
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 28 deletions.
17 changes: 17 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Changelog

All notable changes to this project will be documented in this
file. This project adheres to [Semantic Versioning](http:https://semver.org/).

As this project is pre 1.0, breaking changes may happen for minor version
bumps. A breaking change will get clearly notified in this log.

NOTE: this changelog represents the changes that are associated with the library code in this repo (rather than the tools or services in this repo).

## [Unreleased]

### Added

- network: Added the `HashTransaction` helper func to get the hash of a transaction targetted to a specific stellar network.

[Unreleased]: https://github.com/stellar/go/commits/master
35 changes: 8 additions & 27 deletions build/transaction.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package build

import (
"bytes"
"encoding/hex"
"errors"
"fmt"

"github.com/stellar/go/hash"
"github.com/stellar/go/network"
"github.com/stellar/go/xdr"
)

Expand All @@ -28,9 +26,9 @@ type TransactionMutator interface {

// TransactionBuilder represents a Transaction that is being constructed.
type TransactionBuilder struct {
TX *xdr.Transaction
NetworkID [32]byte
Err error
TX *xdr.Transaction
NetworkPassphrase string
Err error
}

// Mutate applies the provided TransactionMutators to this builder's transaction
Expand All @@ -50,24 +48,7 @@ func (b *TransactionBuilder) Mutate(muts ...TransactionMutator) {

// Hash returns the hash of this builder's transaction.
func (b *TransactionBuilder) Hash() ([32]byte, error) {
var txBytes bytes.Buffer

_, err := fmt.Fprintf(&txBytes, "%s", b.NetworkID)
if err != nil {
return [32]byte{}, err
}

_, err = xdr.Marshal(&txBytes, xdr.EnvelopeTypeEnvelopeTypeTx)
if err != nil {
return [32]byte{}, err
}

_, err = xdr.Marshal(&txBytes, b.TX)
if err != nil {
return [32]byte{}, err
}

return hash.Hash(txBytes.Bytes()), nil
return network.HashTransaction(b.TX, b.NetworkPassphrase)
}

// HashHex returns the hex-encoded hash of this builder's transaction
Expand Down Expand Up @@ -175,8 +156,8 @@ func (m Defaults) MutateTransaction(o *TransactionBuilder) error {
o.TX.Fee = xdr.Uint32(100 * len(o.TX.Operations))
}

if o.NetworkID == [32]byte{} {
o.NetworkID = DefaultNetwork.ID()
if o.NetworkPassphrase == "" {
o.NetworkPassphrase = DefaultNetwork.Passphrase
}
return nil
}
Expand Down Expand Up @@ -257,7 +238,7 @@ func (m MemoText) MutateTransaction(o *TransactionBuilder) (err error) {

// MutateTransaction for Network sets the Network ID to use when signing this transaction
func (m Network) MutateTransaction(o *TransactionBuilder) error {
o.NetworkID = m.ID()
o.NetworkPassphrase = m.Passphrase
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion build/transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ var _ = Describe("Transaction Mutators:", func() {
mut = Defaults{}
})
It("sets the fee", func() { Expect(subject.TX.Fee).To(BeEquivalentTo(100)) })
It("sets the network id", func() { Expect(subject.NetworkID).To(Equal(DefaultNetwork.ID())) })
It("sets the network passphrase", func() { Expect(subject.NetworkPassphrase).To(Equal(DefaultNetwork.Passphrase)) })

Context("on a transaction with 2 operations", func() {
BeforeEach(func() { subject.Mutate(Payment()) })
Expand Down
30 changes: 30 additions & 0 deletions network/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
package network

import (
"bytes"
"fmt"

"github.com/stellar/go/hash"
"github.com/stellar/go/support/errors"
"github.com/stellar/go/xdr"
)

const (
Expand All @@ -19,3 +24,28 @@ const (
func ID(passphrase string) [32]byte {
return hash.Hash([]byte(passphrase))
}

// HashTransaction derives the network specific hash for the provided
// transaction using the network identified by the supplied passphrase. The
// resulting hash is the value that can be signed by stellar secret key to
// authorize the transaction identified by the hash to stellar validators.
func HashTransaction(tx *xdr.Transaction, passphrase string) ([32]byte, error) {
var txBytes bytes.Buffer

_, err := fmt.Fprintf(&txBytes, "%s", ID(passphrase))
if err != nil {
return [32]byte{}, errors.Wrap(err, "fprint network id failed")
}

_, err = xdr.Marshal(&txBytes, xdr.EnvelopeTypeEnvelopeTypeTx)
if err != nil {
return [32]byte{}, errors.Wrap(err, "marshal type failed")
}

_, err = xdr.Marshal(&txBytes, tx)
if err != nil {
return [32]byte{}, errors.Wrap(err, "marshal tx failed")
}

return hash.Hash(txBytes.Bytes()), nil
}

0 comments on commit 5beff68

Please sign in to comment.