generated from mrz1836/go-template
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(SPV-1125) finalize the story for op_return recording (#877)
- Loading branch information
1 parent
0f4e6b8
commit 3893827
Showing
22 changed files
with
422 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,7 +34,7 @@ dist | |
coverage.txt | ||
|
||
# Development data dir | ||
data/ | ||
/data/ | ||
|
||
# Configuration files | ||
.env.config | ||
|
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,41 @@ | ||
package data | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/bitcoin-sv/spv-wallet/actions/v2/data/internal/mapping" | ||
"github.com/bitcoin-sv/spv-wallet/engine/spverrors" | ||
"github.com/bitcoin-sv/spv-wallet/models/bsv" | ||
"github.com/bitcoin-sv/spv-wallet/server/reqctx" | ||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
func get(c *gin.Context, userContext *reqctx.UserContext) { | ||
logger := reqctx.Logger(c) | ||
|
||
userID, err := userContext.ShouldGetUserID() | ||
if err != nil { | ||
spverrors.AbortWithErrorResponse(c, err, logger) | ||
return | ||
} | ||
|
||
dataID := c.Param("id") | ||
_, err = bsv.OutpointFromString(dataID) | ||
if err != nil { | ||
spverrors.ErrorResponse(c, spverrors.ErrInvalidDataID.Wrap(err), logger) | ||
return | ||
} | ||
|
||
data, err := reqctx.Engine(c).DataService().FindForUser(c.Request.Context(), dataID, userID) | ||
if err != nil { | ||
spverrors.ErrorResponse(c, err, logger) | ||
return | ||
} | ||
|
||
if data == nil { | ||
spverrors.ErrorResponse(c, spverrors.ErrDataNotFound, logger) | ||
return | ||
} | ||
|
||
c.JSON(http.StatusOK, mapping.DataResponse(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,107 @@ | ||
package data_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/bitcoin-sv/spv-wallet/actions/testabilities" | ||
"github.com/bitcoin-sv/spv-wallet/actions/testabilities/apierror" | ||
testengine "github.com/bitcoin-sv/spv-wallet/engine/testabilities" | ||
"github.com/bitcoin-sv/spv-wallet/engine/tester/fixtures" | ||
"github.com/bitcoin-sv/spv-wallet/models/bsv" | ||
) | ||
|
||
// NOTE: More complex and real-world test case can be found in outlines_record_test.go | ||
func TestGetData(t *testing.T) { | ||
// given: | ||
given, then := testabilities.New(t) | ||
cleanup := given.StartedSPVWalletWithConfiguration( | ||
testengine.WithV2(), | ||
) | ||
defer cleanup() | ||
|
||
// and: | ||
dataToStore := "hello world" | ||
|
||
// and: | ||
_, dataID := given.Faucet(fixtures.Sender).StoreData(dataToStore) | ||
|
||
// and: | ||
client := given.HttpClient().ForGivenUser(fixtures.Sender) | ||
|
||
// when: | ||
res, _ := client.R(). | ||
Get("/api/v2/data/" + dataID) | ||
|
||
// then: | ||
then.Response(res). | ||
IsOK().WithJSONMatching(`{ | ||
"id": "{{ .outpoint }}", | ||
"blob": "{{ .blob }}" | ||
}`, map[string]any{ | ||
"outpoint": dataID, | ||
"blob": dataToStore, | ||
}) | ||
} | ||
|
||
func TestErrorCases(t *testing.T) { | ||
// given: | ||
givenForAllTests := testabilities.Given(t) | ||
cleanup := givenForAllTests.StartedSPVWalletWithConfiguration( | ||
testengine.WithV2(), | ||
) | ||
defer cleanup() | ||
|
||
// and | ||
mockTx := fixtures.GivenTX(t).WithInput(1).WithP2PKHOutput(1) | ||
mockOutpoint := bsv.Outpoint{ | ||
TxID: mockTx.ID(), | ||
Vout: 0, | ||
} | ||
|
||
t.Run("try to get data as admin", func(t *testing.T) { | ||
// given: | ||
given, then := testabilities.NewOf(givenForAllTests, t) | ||
|
||
// and: | ||
client := given.HttpClient().ForAdmin() | ||
|
||
// when: | ||
res, _ := client.R().Get("/api/v2/data/" + mockOutpoint.String()) | ||
|
||
// then: | ||
then.Response(res).IsUnauthorizedForAdmin() | ||
}) | ||
|
||
t.Run("try to get data as anonymous", func(t *testing.T) { | ||
// given: | ||
given, then := testabilities.NewOf(givenForAllTests, t) | ||
|
||
// and: | ||
client := given.HttpClient().ForAnonymous() | ||
|
||
// when: | ||
res, _ := client.R().Get("/api/v2/data/" + mockOutpoint.String()) | ||
|
||
// then: | ||
then.Response(res).IsUnauthorized() | ||
}) | ||
|
||
t.Run("try to get data with wrong id", func(t *testing.T) { | ||
// given: | ||
given, then := testabilities.NewOf(givenForAllTests, t) | ||
|
||
// and: | ||
client := given.HttpClient().ForUser() | ||
|
||
// and: | ||
wrongID := "wrong_id" //doesn't match the outpoint format "<txID>-<vout>" | ||
|
||
// when: | ||
res, _ := client.R().Get("/api/v2/data/" + wrongID) | ||
|
||
// then: | ||
then.Response(res).HasStatus(400).WithJSONf( | ||
apierror.ExpectedJSON("error-invalid-data-id", "invalid data id"), | ||
) | ||
}) | ||
} |
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,14 @@ | ||
package mapping | ||
|
||
import ( | ||
"github.com/bitcoin-sv/spv-wallet/engine/v2/data/datamodels" | ||
"github.com/bitcoin-sv/spv-wallet/models/response" | ||
) | ||
|
||
// DataResponse maps a domain data model to a response model | ||
func DataResponse(data *datamodels.Data) response.Data { | ||
return response.Data{ | ||
ID: data.ID(), | ||
Blob: string(data.Blob), | ||
} | ||
} |
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,12 @@ | ||
package data | ||
|
||
import ( | ||
"github.com/bitcoin-sv/spv-wallet/server/handlers" | ||
routes "github.com/bitcoin-sv/spv-wallet/server/handlers" | ||
) | ||
|
||
// RegisterRoutes creates the specific package routes | ||
func RegisterRoutes(handlersManager *routes.Manager) { | ||
group := handlersManager.Group(routes.GroupAPIV2, "/data") | ||
group.GET("/:id", handlers.AsUser(get)) | ||
} |
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
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
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
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
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
Oops, something went wrong.