Skip to content
This repository has been archived by the owner on Apr 2, 2024. It is now read-only.

Added RecordRawTransaction action #125

Merged
merged 1 commit into from
Jun 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions action_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,21 @@ func (c *Client) RecordTransaction(ctx context.Context, xPubKey, txHex, draftID
return transaction, nil
}

// RecordRawTransaction will parse the transaction and save it into the Datastore directly, without any checks
//
// Only use this function when you know what you are doing!
//
// txHex is the raw transaction hex
// opts are model options and can include "metadata"
func (c *Client) RecordRawTransaction(ctx context.Context, txHex string,
opts ...ModelOps) (*Transaction, error) {

// Check for existing NewRelic transaction
ctx = c.GetOrStartTxn(ctx, "record_raw_transaction")

return c.recordTxHex(ctx, txHex, opts...)
}

// RecordMonitoredTransaction will parse the transaction and save it into the Datastore
//
// This function will try to record the transaction directly, without checking draft ids etc.
Expand All @@ -116,8 +131,12 @@ func recordMonitoredTransaction(ctx context.Context, client ClientInterface, txH
// Check for existing NewRelic transaction
ctx = client.GetOrStartTxn(ctx, "record_monitored_transaction")

return client.recordTxHex(ctx, txHex, opts...)
}

func (c *Client) recordTxHex(ctx context.Context, txHex string, opts ...ModelOps) (*Transaction, error) {
// Create the model & set the default options (gives options from client->model)
newOpts := client.DefaultModelOptions(append(opts, New())...)
newOpts := c.DefaultModelOptions(append(opts, New())...)
transaction := newTransaction(txHex, newOpts...)

// Ensure that we have a transaction id (created from the txHex)
Expand All @@ -128,7 +147,7 @@ func recordMonitoredTransaction(ctx context.Context, client ClientInterface, txH

// Create the lock and set the release for after the function completes
unlock, err := newWriteLock(
ctx, fmt.Sprintf(lockKeyRecordTx, id), client.Cachestore(),
ctx, fmt.Sprintf(lockKeyRecordTx, id), c.Cachestore(),
)
defer unlock()
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,9 @@ type TransactionService interface {
opts ...ModelOps) (*DraftTransaction, error)
RecordTransaction(ctx context.Context, xPubKey, txHex, draftID string,
opts ...ModelOps) (*Transaction, error)
RecordRawTransaction(ctx context.Context, txHex string, opts ...ModelOps) (*Transaction, error)
UpdateTransactionMetadata(ctx context.Context, xPubID, id string, metadata Metadata) (*Transaction, error)
recordTxHex(ctx context.Context, txHex string, opts ...ModelOps) (*Transaction, error)
}

// UTXOService is the utxo actions
Expand Down
5 changes: 5 additions & 0 deletions utils/destination_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@ func IsStas(lockingScript string) bool {
return StasRegexp.MatchString(lockingScript)
}

// IsRunJS Check whether the given string is a Run JS output
func IsRunJS(lockingScript string) bool {
return StasRegexp.MatchString(lockingScript)
}

// IsMultiSig Check whether the given string is a multi-sig locking script
func IsMultiSig(lockingScript string) bool {
script, err := bscript2.NewFromHexString(lockingScript)
Expand Down
16 changes: 16 additions & 0 deletions utils/destination_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,10 @@ func TestGetDestinationType(t *testing.T) {
t.Run("multisig - ScriptTypeMultiSig", func(t *testing.T) {
assert.Equal(t, bscript2.ScriptTypeMultiSig, GetDestinationType(multisigHex))
})

t.Run("stas - ScriptTypeTokenStas", func(t *testing.T) {
assert.Equal(t, ScriptTypeTokenStas, GetDestinationType(stas2Hex))
})
}

// TestGetAddressFromScript will test the method GetAddressFromScript()
Expand All @@ -176,3 +180,15 @@ func TestGetAddressFromScript(t *testing.T) {
assert.Equal(t, "", GetAddressFromScript("invalid-or-unknown-script"))
})
}

func BenchmarkIsP2PKH(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = IsP2PKH(p2pkhHex)
}
}

func BenchmarkGetDestinationTypeRegex(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = GetDestinationType(stas2Hex)
}
}