Skip to content

Commit

Permalink
add sign unsigned and check for uncomputed change
Browse files Browse the repository at this point in the history
  • Loading branch information
rohenaz committed Sep 10, 2024
1 parent 118d076 commit 8823d79
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ All notable changes to this project will be documented in this file. The format
## Table of Contents

- [Unreleased](#unreleased)
- [1.1.7 - 2024-09-10](#117---2024-09-10)
- [1.1.6 - 2024-09-09](#116---2024-09-09)
- [1.1.5 - 2024-09-06](#115---2024-09-06)
- [1.1.4 - 2024-09-05](#114---2024-09-05)
Expand All @@ -14,6 +15,16 @@ All notable changes to this project will be documented in this file. The format
- [1.1.0 - 2024-08-19](#110---2024-08-19)
- [1.0.0 - 2024-06-06](#100---2024-06-06)


## [1.1.7] - 2024-09-10
- Introduce `tx.ShallowClone()`
- Introduce SignUnsigned to sign only inputs that have not already been signed
- Other minor performance improvements.

### Added
- New method `Transaction.ShallowClone()`
- New method `Transaction.SignUnsigned()`

## [1.1.6] - 2024-09-09
- Optimize handling of source transaction inputs. Avoid mocking up entire transaction when adding source inputs.
- Minor alignment in ECIES helper function
Expand Down
29 changes: 28 additions & 1 deletion transaction/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
crypto "github.com/bitcoin-sv/go-sdk/primitives/hash"
"github.com/bitcoin-sv/go-sdk/script"
"github.com/bitcoin-sv/go-sdk/util"
"github.com/pkg/errors"
)

type Transaction struct {
Expand Down Expand Up @@ -325,7 +326,6 @@ func (tx *Transaction) Clone() *Transaction {
if input.SourceTransaction != nil {
clone.Inputs[i].SourceTransaction = input.SourceTransaction.Clone()
}
// clone.Inputs[i].SourceTransaction = input.SourceTransaction
clone.Inputs[i].sourceOutput = input.sourceOutput
}

Expand Down Expand Up @@ -437,6 +437,7 @@ func (tx *Transaction) AddMerkleProof(bump *MerklePath) error {
}

func (tx *Transaction) Sign() error {
tx.checkFeeCompluted()
for vin, i := range tx.Inputs {
if i.UnlockingScriptTemplate != nil {
unlock, err := i.UnlockingScriptTemplate.Sign(tx, uint32(vin))
Expand All @@ -448,3 +449,29 @@ func (tx *Transaction) Sign() error {
}
return nil
}

func (tx *Transaction) SignUnsigned() error {
tx.checkFeeCompluted()
for vin, i := range tx.Inputs {
if i.UnlockingScript == nil {
if i.UnlockingScriptTemplate != nil {
unlock, err := i.UnlockingScriptTemplate.Sign(tx, uint32(vin))
if err != nil {
return err
}
i.UnlockingScript = unlock
}
}
}
return nil
}

func (tx *Transaction) checkFeeCompluted() error {
for _, out := range tx.Outputs {
if out.Satoshis == 0 {
if out.Change {
return errors.New("There are still change outputs with uncomputed amounts. Use the Fee() method to compute the change amounts and transaction fees prior to signing.")
}
}
}
}

Check failure on line 477 in transaction/transaction.go

View workflow job for this annotation

GitHub Actions / SonarCloud

missing return

Check failure on line 477 in transaction/transaction.go

View workflow job for this annotation

GitHub Actions / SonarCloud

missing return

Check failure on line 477 in transaction/transaction.go

View workflow job for this annotation

GitHub Actions / golangci-lint (/home/runner/work/go-sdk/go-sdk)

missing return) (typecheck)

Check failure on line 477 in transaction/transaction.go

View workflow job for this annotation

GitHub Actions / golangci-lint (/home/runner/work/go-sdk/go-sdk)

missing return) (typecheck)

Check failure on line 477 in transaction/transaction.go

View workflow job for this annotation

GitHub Actions / golangci-lint (/home/runner/work/go-sdk/go-sdk)

missing return) (typecheck)

Check failure on line 477 in transaction/transaction.go

View workflow job for this annotation

GitHub Actions / golangci-lint (/home/runner/work/go-sdk/go-sdk)

missing return) (typecheck)

Check failure on line 477 in transaction/transaction.go

View workflow job for this annotation

GitHub Actions / golangci-lint (/home/runner/work/go-sdk/go-sdk)

missing return) (typecheck)

0 comments on commit 8823d79

Please sign in to comment.