generated from mrz1836/go-template
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(SPV-1020): create draft transaction handling OP RETURN output;
- Loading branch information
1 parent
a134b1a
commit 5800184
Showing
19 changed files
with
876 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package transaction | ||
|
||
type Bucket string | ||
|
||
const ( | ||
BucketData Bucket = "data" | ||
) | ||
|
||
// Annotations represents a transaction metadata that will be used by server to properly handle given transaction. | ||
type Annotations struct { | ||
Outputs OutputsAnnotations | ||
} | ||
|
||
type OutputAnnotation struct { | ||
// What type of bucket should this output be stored in. | ||
Bucket Bucket | ||
} | ||
|
||
type OutputsAnnotations map[int]*OutputAnnotation | ||
|
||
func NewDataOutputAnnotation() *OutputAnnotation { | ||
return &OutputAnnotation{ | ||
Bucket: BucketData, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package draft | ||
|
||
import ( | ||
"context" | ||
|
||
sdk "github.com/bitcoin-sv/go-sdk/transaction" | ||
"github.com/bitcoin-sv/spv-wallet/engine/transaction" | ||
txerrors "github.com/bitcoin-sv/spv-wallet/engine/transaction/errors" | ||
) | ||
|
||
func Create(ctx context.Context, spec *TransactionSpec) (*Transaction, error) { | ||
tx := &sdk.Transaction{} | ||
if spec == nil { | ||
return nil, txerrors.ErrDraftSpecificationRequired | ||
} | ||
outputs, annotations, err := spec.outputs(ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
tx.Outputs = outputs | ||
|
||
beef, err := tx.BEEFHex() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &Transaction{ | ||
BEEF: beef, | ||
Annotations: &transaction.Annotations{ | ||
Outputs: annotations, | ||
}, | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package draft_test | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/bitcoin-sv/spv-wallet/engine/transaction/draft" | ||
"github.com/bitcoin-sv/spv-wallet/engine/transaction/draft/outputs" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestCreateTransactionDraftError(t *testing.T) { | ||
errorTests := map[string]struct { | ||
spec *draft.TransactionSpec | ||
expectedError string | ||
}{ | ||
"for nil as transaction spec": { | ||
spec: nil, | ||
expectedError: "draft requires a specification", | ||
}, | ||
"for no outputs in transaction spec": { | ||
spec: &draft.TransactionSpec{}, | ||
expectedError: "draft requires at least one output", | ||
}, | ||
"for empty output list in transaction spec": { | ||
spec: &draft.TransactionSpec{Outputs: outputs.NewSpecifications()}, | ||
}, | ||
} | ||
for name, test := range errorTests { | ||
t.Run("return error "+name, func(t *testing.T) { | ||
// when: | ||
_, err := draft.Create(context.Background(), test.spec) | ||
|
||
// then: | ||
require.Error(t, err) | ||
require.ErrorContains(t, err, test.expectedError) | ||
}) | ||
} | ||
} |
118 changes: 118 additions & 0 deletions
118
engine/transaction/draft/create_op_return_draft_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
package draft_test | ||
|
||
import ( | ||
"context" | ||
"encoding/hex" | ||
"testing" | ||
|
||
sdk "github.com/bitcoin-sv/go-sdk/transaction" | ||
"github.com/bitcoin-sv/spv-wallet/engine/transaction" | ||
"github.com/bitcoin-sv/spv-wallet/engine/transaction/draft" | ||
"github.com/bitcoin-sv/spv-wallet/engine/transaction/draft/outputs" | ||
"github.com/bitcoin-sv/spv-wallet/models/request/opreturn" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestCreateOpReturnDraft(t *testing.T) { | ||
successTests := map[string]struct { | ||
opReturn *outputs.OpReturn | ||
lockingScript string | ||
}{ | ||
"for single string": { | ||
opReturn: &outputs.OpReturn{ | ||
DataType: opreturn.DataTypeStrings, | ||
Data: []string{"Example data"}, | ||
}, | ||
lockingScript: "006a0c4578616d706c652064617461", | ||
}, | ||
"for multiple strings": { | ||
opReturn: &outputs.OpReturn{ | ||
DataType: opreturn.DataTypeStrings, | ||
Data: []string{"Example", " ", "data"}, | ||
}, | ||
lockingScript: "006a074578616d706c6501200464617461", | ||
}, | ||
"for single hex": { | ||
opReturn: &outputs.OpReturn{ | ||
DataType: opreturn.DataTypeHexes, | ||
Data: []string{toHex("Example data")}, | ||
}, | ||
lockingScript: "006a0c4578616d706c652064617461", | ||
}, | ||
"for multiple hexes": { | ||
opReturn: &outputs.OpReturn{ | ||
DataType: opreturn.DataTypeHexes, | ||
Data: []string{toHex("Example"), toHex(" "), toHex("data")}, | ||
}, | ||
lockingScript: "006a074578616d706c6501200464617461", | ||
}, | ||
} | ||
for name, test := range successTests { | ||
t.Run("return draft"+name, func(t *testing.T) { | ||
// given: | ||
spec := &draft.TransactionSpec{ | ||
Outputs: outputs.NewSpecifications(test.opReturn), | ||
} | ||
|
||
// when: | ||
draftTx, err := draft.Create(context.Background(), spec) | ||
|
||
// then: | ||
require.NoError(t, err) | ||
require.NotNil(t, draftTx) | ||
|
||
// and: | ||
annotations := draftTx.Annotations | ||
require.Len(t, annotations.Outputs, 1) | ||
require.Equal(t, transaction.BucketData, annotations.Outputs[0].Bucket) | ||
|
||
// debug: | ||
t.Logf("BEEF: %s", draftTx.BEEF) | ||
|
||
// when: | ||
tx, err := sdk.NewTransactionFromBEEFHex(draftTx.BEEF) | ||
|
||
// then: | ||
require.NoError(t, err) | ||
require.Len(t, tx.Outputs, 1) | ||
require.EqualValues(t, tx.Outputs[0].Satoshis, 0) | ||
require.Equal(t, tx.Outputs[0].LockingScript.IsData(), true) | ||
require.Equal(t, test.lockingScript, tx.Outputs[0].LockingScriptHex()) | ||
}) | ||
} | ||
|
||
errorTests := map[string]struct { | ||
spec *outputs.OpReturn | ||
expectedError string | ||
}{ | ||
"for no data in default type": { | ||
spec: &outputs.OpReturn{}, | ||
expectedError: "data is required for OP_RETURN output", | ||
}, | ||
"for no data string type": { | ||
spec: &outputs.OpReturn{ | ||
DataType: opreturn.DataTypeStrings, | ||
}, | ||
expectedError: "data is required for OP_RETURN output", | ||
}, | ||
} | ||
for name, test := range errorTests { | ||
t.Run("return error "+name, func(t *testing.T) { | ||
// given: | ||
spec := &draft.TransactionSpec{ | ||
Outputs: outputs.NewSpecifications(test.spec), | ||
} | ||
|
||
// when: | ||
_, err := draft.Create(context.Background(), spec) | ||
|
||
// then: | ||
require.Error(t, err) | ||
require.ErrorContains(t, err, test.expectedError) | ||
}) | ||
} | ||
} | ||
|
||
func toHex(data string) string { | ||
return hex.EncodeToString([]byte(data)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package outputs | ||
|
||
import ( | ||
"context" | ||
"encoding/hex" | ||
|
||
sdk "github.com/bitcoin-sv/go-sdk/transaction" | ||
"github.com/bitcoin-sv/spv-wallet/engine/spverrors" | ||
"github.com/bitcoin-sv/spv-wallet/engine/transaction" | ||
"github.com/bitcoin-sv/spv-wallet/models/request/opreturn" | ||
) | ||
|
||
const opReturnDataRequiredError = "data is required for OP_RETURN output" | ||
|
||
type OpReturn opreturn.Output | ||
|
||
func (o *OpReturn) evaluate(context.Context) (annotatedOutputs, error) { | ||
if len(o.Data) == 0 { | ||
return nil, spverrors.Newf(opReturnDataRequiredError) | ||
} | ||
|
||
data, err := o.getData() | ||
if err != nil { | ||
return nil, err | ||
} | ||
output, err := sdk.CreateOpReturnOutput(data) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
annotation := transaction.NewDataOutputAnnotation() | ||
return singleAnnotatedOutput(output, annotation), nil | ||
} | ||
|
||
func (o *OpReturn) getData() ([][]byte, error) { | ||
data := make([][]byte, 0) | ||
for _, dataToStore := range o.Data { | ||
bytes, err := toBytes(dataToStore, o.DataType) | ||
if err != nil { | ||
return nil, err | ||
} | ||
data = append(data, bytes) | ||
} | ||
return data, nil | ||
} | ||
|
||
func toBytes(data string, dataType opreturn.DataType) ([]byte, error) { | ||
switch dataType { | ||
case opreturn.DataTypeDefault, opreturn.DataTypeStrings: | ||
return []byte(data), nil | ||
case opreturn.DataTypeHexes: | ||
dataHex, err := hex.DecodeString(data) | ||
if err != nil { | ||
return nil, spverrors.Wrapf(err, "failed to decode hex") | ||
} | ||
return dataHex, nil | ||
default: | ||
return nil, spverrors.Newf("unsupported data type") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package outputs | ||
|
||
import ( | ||
"context" | ||
|
||
sdk "github.com/bitcoin-sv/go-sdk/transaction" | ||
"github.com/bitcoin-sv/spv-wallet/engine/transaction" | ||
txerrors "github.com/bitcoin-sv/spv-wallet/engine/transaction/errors" | ||
) | ||
|
||
type Specifications struct { | ||
Outputs []Spec | ||
} | ||
|
||
type Spec interface { | ||
evaluate(ctx context.Context) (annotatedOutputs, error) | ||
} | ||
|
||
func NewSpecifications(outputs ...Spec) *Specifications { | ||
return &Specifications{ | ||
Outputs: outputs, | ||
} | ||
} | ||
|
||
func (s *Specifications) Add(output Spec) { | ||
s.Outputs = append(s.Outputs, output) | ||
} | ||
|
||
func (s *Specifications) Evaluate(ctx context.Context) ([]*sdk.TransactionOutput, transaction.OutputsAnnotations, error) { | ||
if s.Outputs == nil { | ||
return nil, nil, txerrors.ErrDraftRequiresAtLeastOneOutput | ||
} | ||
outputs, err := s.evaluate(ctx) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
txOutputs, annotations := outputs.splitIntoTransactionOutputsAndAnnotations() | ||
return txOutputs, annotations, nil | ||
} | ||
|
||
func (s *Specifications) evaluate(ctx context.Context) (annotatedOutputs, error) { | ||
outputs := make(annotatedOutputs, 0) | ||
for _, spec := range s.Outputs { | ||
outs, err := spec.evaluate(ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
outputs = append(outputs, outs...) | ||
} | ||
return outputs, nil | ||
} | ||
|
||
type annotatedOutput struct { | ||
*transaction.OutputAnnotation | ||
*sdk.TransactionOutput | ||
} | ||
|
||
type annotatedOutputs []*annotatedOutput | ||
|
||
func singleAnnotatedOutput(txOut *sdk.TransactionOutput, out *transaction.OutputAnnotation) annotatedOutputs { | ||
return annotatedOutputs{ | ||
&annotatedOutput{ | ||
OutputAnnotation: out, | ||
TransactionOutput: txOut, | ||
}, | ||
} | ||
} | ||
|
||
func (a annotatedOutputs) splitIntoTransactionOutputsAndAnnotations() ([]*sdk.TransactionOutput, transaction.OutputsAnnotations) { | ||
outputs := make([]*sdk.TransactionOutput, len(a)) | ||
annotations := make(transaction.OutputsAnnotations) | ||
for i, out := range a { | ||
outputs[i] = out.TransactionOutput | ||
if out.OutputAnnotation != nil { | ||
annotations[i] = out.OutputAnnotation | ||
} | ||
} | ||
return outputs, annotations | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package draft | ||
|
||
import "github.com/bitcoin-sv/spv-wallet/engine/transaction" | ||
|
||
type Transaction struct { | ||
BEEF string | ||
Annotations *transaction.Annotations | ||
} |
Oops, something went wrong.