Skip to content

Commit

Permalink
Add helper for signing transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
jordanschalm committed May 21, 2017
1 parent dcaafba commit a828873
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
8 changes: 2 additions & 6 deletions blockchain/test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,8 @@ func newTxBody() TxBody {
func newTransaction() *Transaction {
sender := newWallet()
tbody := newTxBody()
digest := HashSum(tbody)
sig, _ := sender.Sign(digest, crand.Reader)
return &Transaction{
TxBody: tbody,
Sig: sig,
}
t, _ := tbody.Signed(sender, crand.Reader)
return t
}

func newBlockHeader() BlockHeader {
Expand Down
12 changes: 11 additions & 1 deletion blockchain/transaction.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package blockchain

import "encoding/binary"
import (
"encoding/binary"
"io"
)

const (
// TxHashPointerLen is the length in bytes of a hash pointer.
Expand Down Expand Up @@ -60,6 +63,13 @@ func (tb TxBody) Marshal() []byte {
return buf
}

// Signed returns a signed Transaction from a TxBody
func (tb TxBody) Signed(w Wallet, r io.Reader) (*Transaction, error) {
digest := HashSum(tb)
sig, err := w.Sign(digest, r)
return &Transaction{tb, sig}, err
}

// Transaction contains a TxBody and a signature verifying it
type Transaction struct {
TxBody
Expand Down

0 comments on commit a828873

Please sign in to comment.