aboutsummaryrefslogtreecommitdiffstats
path: root/swarm/api/client
diff options
context:
space:
mode:
authorJavier Peletier <jpeletier@users.noreply.github.com>2018-07-22 03:49:36 +0800
committerAnton Evangelatov <anton.evangelatov@gmail.com>2018-07-23 21:33:33 +0800
commit427316a7078e1876ad8db9d67550609c961e84f6 (patch)
tree382406ba5b2499076bb53e379aa4619df506d989 /swarm/api/client
parent0647c4de7b1b4b2090807a6db0b7a8eafdfa097b (diff)
downloadgo-tangerine-427316a7078e1876ad8db9d67550609c961e84f6.tar
go-tangerine-427316a7078e1876ad8db9d67550609c961e84f6.tar.gz
go-tangerine-427316a7078e1876ad8db9d67550609c961e84f6.tar.bz2
go-tangerine-427316a7078e1876ad8db9d67550609c961e84f6.tar.lz
go-tangerine-427316a7078e1876ad8db9d67550609c961e84f6.tar.xz
go-tangerine-427316a7078e1876ad8db9d67550609c961e84f6.tar.zst
go-tangerine-427316a7078e1876ad8db9d67550609c961e84f6.zip
swarm/storage/mru: Client-side MRU signatures (#784)
* swarm/storage/mru: Add embedded publickey and remove ENS dep This commit breaks swarm, swarm/api... but tests in swarm/storage/mru pass * swarm: Refactor swarm, swarm/api to mru changes, make tests pass * swarm/storage/mru: Remove self from recv, remove test ens vldtr * swarm/storage/mru: Remove redundant test, expose ResourceHash mthd * swarm/storage/mru: Make HeaderGetter mandatory + godoc fixes * swarm/storage: Remove validator prefix for metadata chunk * swarm/storage/mru: Use Address instead of PublicKey * swarm/storage/mru: Change index from name to metadata chunk addr * swarm/storage/mru: Refactor swarm/api/... to MRU index changes * swarm/storage/mru: Refactor cleanup * swarm/storage/mru: Rebase cleanup * swarm: Use constructor for GenericSigner MRU in swarm.go * swarm/storage: Change to BMTHash for MRU hashing * swarm/storage: Reduce loglevel on chunk validator logs * swarm/storage/mru: Delint * swarm: MRU Rebase cleanup * swarm/storage/mru: client-side mru signatures Rebase to PR #668 and fix all conflicts * swarm/storage/mru: refactor and documentation * swarm/resource/mru: error-checking tests for parseUpdate/newUpdateChunk * swarm/storage/mru: Added resourcemetadata tests * swarm/storage/mru: Added tests for UpdateRequest * swarm/storage/mru: more test coverage for UpdateRequest and comments * swarm/storage/mru: Avoid fake chunks in parseUpdate() * swarm/storage/mru: Documented resource.go extensively moved some functions where they make most sense * swarm/storage/mru: increase test coverage for UpdateRequest and variable name changes throughout to increase consistency * swarm/storage/mru: moved default timestamp to NewCreateRequest- * swarm/storage/mru: lookup refactor * swarm/storage/mru: added comments and renamed raw flag to rawmru * swarm/storage/mru: fix receiver typo * swarm/storage/mru: refactored update chunk new/create * swarm/storage/mru: refactored signature digest to avoid malleability * swarm/storage/mru: optimize update data serialization * swarm/storage/mru: refactor and cleanup * swarm/storage/mru: add timestamp struct and serialization * swarm/storage/mru: fix lint error and mark some old code for deletion * swarm/storage/mru: remove unnecessary variable * swarm/storage/mru: Added more comments throughout * swarm/storage/mru: Refactored metadata chunk layout + extensive error... * swarm/storage/mru: refactor cli parser Changed resource info output to JSON * swarm/storage/mru: refactor serialization for extensibility refactored error messages to NewErrorf * swarm/storage/mru: Moved Signature to resource_sign. Check Sign errors in server tests * swarm/storage/mru: Remove isSafeName() checks * swarm/storage/mru: scrubbed off all references to "block" for time * swarm/storage/mru: removed superfluous isSynced() call. * swarm/storage/mru: remove isMultihash() and ToSafeName functions * swarm/storage/mru: various fixes and comments * swarm/storage/mru: decoupled cli for independent create/update * Made resource name optional * Removed unused LookupPrevious * swarm/storage/mru: Decoupled resource create / update & refactor * swarm/storage/mru: Fixed some comments as per issues raised in PR #743 * swarm/storage/mru: Cosmetic changes as per #743 comments * swarm/storage/mru: refct request encoder/decoder > marshal/unmarshal * swarm/storage/mru: Cosmetic changes as per review in #748 * swarm/storage/mru: removed timestamp proof placeholder * swarm/storage/mru: cosmetic/doc/fixes changes as per comments in #704 * swarm/storage/mru: removed unnecessary check in Handler.update * swarm/storage/mru: Implemented Marshaler/Unmarshaler iface in Request * swarm/storage/mru: Fixed linter error * swarm/storage/mru: removed redundant address in signature digest * swarm/storage/mru: fixed bug: LookupLatestVersionInPeriod not working * swarm/storage/mru: Unfold Request creation API for create or update+create set common time source for mru package * swarm/api/http: fix HandleGetResource error variable shadowed when requesting a resource that does not exist * swarm/storage/mru: Add simple check to detect duplicate updates * swarm/storage/mru: moved Multihash() to the right place. * cmd/swarm: remove unneeded clientaccountmanager.go * swarm/storage/mru: Changed some comments as per reviews in #784 * swarm/storage/mru: Made SignedResourceUpdate.GetDigest() public * swarm/storage/mru: cosmetic changes as per comments in #784 * cmd/swarm: Inverted --multihash flag default * swarm/storage/mru: removed Verify from SignedResourceUpdate.fromChunk * swarm/storage/mru: Moved validation code out of serializer Cosmetic / comment changes * swarm/storage/mru: Added unit tests for UpdateLookup * swarm/storage/mru: Increased coverage of metadata serialization * swarm/storage/mru: Increased test coverage of updateHeader serializers * swarm/storage/mru: Add resourceUpdate serializer test
Diffstat (limited to 'swarm/api/client')
-rw-r--r--swarm/api/client/client.go87
-rw-r--r--swarm/api/client/client_test.go160
2 files changed, 247 insertions, 0 deletions
diff --git a/swarm/api/client/client.go b/swarm/api/client/client.go
index ef6222435..b3a5e929d 100644
--- a/swarm/api/client/client.go
+++ b/swarm/api/client/client.go
@@ -35,6 +35,7 @@ import (
"strings"
"github.com/ethereum/go-ethereum/swarm/api"
+ "github.com/ethereum/go-ethereum/swarm/storage/mru"
)
var (
@@ -562,3 +563,89 @@ func (c *Client) MultipartUpload(hash string, uploader Uploader) (string, error)
}
return string(data), nil
}
+
+// CreateResource creates a Mutable Resource with the given name and frequency, initializing it with the provided
+// data. Data is interpreted as multihash or not depending on the multihash parameter.
+// startTime=0 means "now"
+// Returns the resulting Mutable Resource manifest address that you can use to include in an ENS Resolver (setContent)
+// or reference future updates (Client.UpdateResource)
+func (c *Client) CreateResource(request *mru.Request) (string, error) {
+ responseStream, err := c.updateResource(request)
+ if err != nil {
+ return "", err
+ }
+ defer responseStream.Close()
+
+ body, err := ioutil.ReadAll(responseStream)
+ if err != nil {
+ return "", err
+ }
+
+ var manifestAddress string
+ if err = json.Unmarshal(body, &manifestAddress); err != nil {
+ return "", err
+ }
+ return manifestAddress, nil
+}
+
+// UpdateResource allows you to set a new version of your content
+func (c *Client) UpdateResource(request *mru.Request) error {
+ _, err := c.updateResource(request)
+ return err
+}
+
+func (c *Client) updateResource(request *mru.Request) (io.ReadCloser, error) {
+ body, err := request.MarshalJSON()
+ if err != nil {
+ return nil, err
+ }
+
+ req, err := http.NewRequest("POST", c.Gateway+"/bzz-resource:/", bytes.NewBuffer(body))
+ if err != nil {
+ return nil, err
+ }
+
+ res, err := http.DefaultClient.Do(req)
+ if err != nil {
+ return nil, err
+ }
+
+ return res.Body, nil
+
+}
+
+// GetResource returns a byte stream with the raw content of the resource
+// manifestAddressOrDomain is the address you obtained in CreateResource or an ENS domain whose Resolver
+// points to that address
+func (c *Client) GetResource(manifestAddressOrDomain string) (io.ReadCloser, error) {
+
+ res, err := http.Get(c.Gateway + "/bzz-resource:/" + manifestAddressOrDomain)
+ if err != nil {
+ return nil, err
+ }
+ return res.Body, nil
+
+}
+
+// GetResourceMetadata returns a structure that describes the Mutable Resource
+// manifestAddressOrDomain is the address you obtained in CreateResource or an ENS domain whose Resolver
+// points to that address
+func (c *Client) GetResourceMetadata(manifestAddressOrDomain string) (*mru.Request, error) {
+
+ responseStream, err := c.GetResource(manifestAddressOrDomain + "/meta")
+ if err != nil {
+ return nil, err
+ }
+ defer responseStream.Close()
+
+ body, err := ioutil.ReadAll(responseStream)
+ if err != nil {
+ return nil, err
+ }
+
+ var metadata mru.Request
+ if err := metadata.UnmarshalJSON(body); err != nil {
+ return nil, err
+ }
+ return &metadata, nil
+}
diff --git a/swarm/api/client/client_test.go b/swarm/api/client/client_test.go
index e68147ab2..dc608e3f1 100644
--- a/swarm/api/client/client_test.go
+++ b/swarm/api/client/client_test.go
@@ -25,8 +25,12 @@ import (
"sort"
"testing"
+ "github.com/ethereum/go-ethereum/common"
+ "github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/swarm/api"
swarmhttp "github.com/ethereum/go-ethereum/swarm/api/http"
+ "github.com/ethereum/go-ethereum/swarm/multihash"
+ "github.com/ethereum/go-ethereum/swarm/storage/mru"
"github.com/ethereum/go-ethereum/swarm/testutil"
)
@@ -354,3 +358,159 @@ func TestClientMultipartUpload(t *testing.T) {
checkDownloadFile(file)
}
}
+
+func newTestSigner() (*mru.GenericSigner, error) {
+ privKey, err := crypto.HexToECDSA("deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef")
+ if err != nil {
+ return nil, err
+ }
+ return mru.NewGenericSigner(privKey), nil
+}
+
+// test the transparent resolving of multihash resource types with bzz:// scheme
+//
+// first upload data, and store the multihash to the resulting manifest in a resource update
+// retrieving the update with the multihash should return the manifest pointing directly to the data
+// and raw retrieve of that hash should return the data
+func TestClientCreateResourceMultihash(t *testing.T) {
+
+ signer, _ := newTestSigner()
+
+ srv := testutil.NewTestSwarmServer(t, serverFunc)
+ client := NewClient(srv.URL)
+ defer srv.Close()
+
+ // add the data our multihash aliased manifest will point to
+ databytes := []byte("bar")
+
+ swarmHash, err := client.UploadRaw(bytes.NewReader(databytes), int64(len(databytes)), false)
+ if err != nil {
+ t.Fatalf("Error uploading raw test data: %s", err)
+ }
+
+ s := common.FromHex(swarmHash)
+ mh := multihash.ToMultihash(s)
+
+ // our mutable resource "name"
+ resourceName := "foo.eth"
+
+ createRequest, err := mru.NewCreateUpdateRequest(&mru.ResourceMetadata{
+ Name: resourceName,
+ Frequency: 13,
+ StartTime: srv.GetCurrentTime(),
+ Owner: signer.Address(),
+ })
+ if err != nil {
+ t.Fatal(err)
+ }
+ createRequest.SetData(mh, true)
+ if err := createRequest.Sign(signer); err != nil {
+ t.Fatalf("Error signing update: %s", err)
+ }
+
+ resourceManifestHash, err := client.CreateResource(createRequest)
+
+ if err != nil {
+ t.Fatalf("Error creating resource: %s", err)
+ }
+
+ correctManifestAddrHex := "6d3bc4664c97d8b821cb74bcae43f592494fb46d2d9cd31e69f3c7c802bbbd8e"
+ if resourceManifestHash != correctManifestAddrHex {
+ t.Fatalf("Response resource key mismatch, expected '%s', got '%s'", correctManifestAddrHex, resourceManifestHash)
+ }
+
+ reader, err := client.GetResource(correctManifestAddrHex)
+ if err != nil {
+ t.Fatalf("Error retrieving resource: %s", err)
+ }
+ defer reader.Close()
+ gotData, err := ioutil.ReadAll(reader)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if !bytes.Equal(mh, gotData) {
+ t.Fatalf("Expected: %v, got %v", mh, gotData)
+ }
+
+}
+
+// TestClientCreateUpdateResource will check that mutable resources can be created and updated via the HTTP client.
+func TestClientCreateUpdateResource(t *testing.T) {
+
+ signer, _ := newTestSigner()
+
+ srv := testutil.NewTestSwarmServer(t, serverFunc)
+ client := NewClient(srv.URL)
+ defer srv.Close()
+
+ // set raw data for the resource
+ databytes := []byte("En un lugar de La Mancha, de cuyo nombre no quiero acordarme...")
+
+ // our mutable resource name
+ resourceName := "El Quijote"
+
+ createRequest, err := mru.NewCreateUpdateRequest(&mru.ResourceMetadata{
+ Name: resourceName,
+ Frequency: 13,
+ StartTime: srv.GetCurrentTime(),
+ Owner: signer.Address(),
+ })
+ if err != nil {
+ t.Fatal(err)
+ }
+ createRequest.SetData(databytes, false)
+ if err := createRequest.Sign(signer); err != nil {
+ t.Fatalf("Error signing update: %s", err)
+ }
+
+ resourceManifestHash, err := client.CreateResource(createRequest)
+
+ correctManifestAddrHex := "cc7904c17b49f9679e2d8006fe25e87e3f5c2072c2b49cab50f15e544471b30a"
+ if resourceManifestHash != correctManifestAddrHex {
+ t.Fatalf("Response resource key mismatch, expected '%s', got '%s'", correctManifestAddrHex, resourceManifestHash)
+ }
+
+ reader, err := client.GetResource(correctManifestAddrHex)
+ if err != nil {
+ t.Fatalf("Error retrieving resource: %s", err)
+ }
+ defer reader.Close()
+ gotData, err := ioutil.ReadAll(reader)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if !bytes.Equal(databytes, gotData) {
+ t.Fatalf("Expected: %v, got %v", databytes, gotData)
+ }
+
+ // define different data
+ databytes = []byte("... no ha mucho tiempo que vivĂ­a un hidalgo de los de lanza en astillero ...")
+
+ updateRequest, err := client.GetResourceMetadata(correctManifestAddrHex)
+ if err != nil {
+ t.Fatalf("Error retrieving update request template: %s", err)
+ }
+
+ updateRequest.SetData(databytes, false)
+ if err := updateRequest.Sign(signer); err != nil {
+ t.Fatalf("Error signing update: %s", err)
+ }
+
+ if err = client.UpdateResource(updateRequest); err != nil {
+ t.Fatalf("Error updating resource: %s", err)
+ }
+
+ reader, err = client.GetResource(correctManifestAddrHex)
+ if err != nil {
+ t.Fatalf("Error retrieving resource: %s", err)
+ }
+ defer reader.Close()
+ gotData, err = ioutil.ReadAll(reader)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if !bytes.Equal(databytes, gotData) {
+ t.Fatalf("Expected: %v, got %v", databytes, gotData)
+ }
+
+}