Skip to content

Commit

Permalink
Return did resolution result from anchor op. (#422)
Browse files Browse the repository at this point in the history
  • Loading branch information
andresuribe87 authored Jun 28, 2023
1 parent 9617360 commit 83e6a4e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
10 changes: 6 additions & 4 deletions did/ion/operations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,22 +133,24 @@ func TestResolver(t *testing.T) {
t.Run("bad anchor", func(tt *testing.T) {
gock.New("https://test-ion-resolution.com").
Post("/operations").
Reply(400)
Reply(400).
BodyString("{}")
defer gock.Off()

resolver, err := NewIONResolver(http.DefaultClient, "https://test-ion-resolution.com")
assert.NoError(tt, err)
assert.NotEmpty(tt, resolver)

err = resolver.Anchor(context.Background(), nil)
_, err = resolver.Anchor(context.Background(), nil)
assert.Error(tt, err)
assert.Contains(tt, err.Error(), "anchor operation failed")
})

t.Run("good anchor", func(tt *testing.T) {
gock.New("https://test-ion-resolution.com").
Post("/operations").
Reply(200)
Reply(200).
BodyString("{}")
defer gock.Off()

resolver, err := NewIONResolver(http.DefaultClient, "https://test-ion-resolution.com")
Expand All @@ -170,7 +172,7 @@ func TestResolver(t *testing.T) {
assert.NotEmpty(tt, ionDID)
assert.NotEmpty(tt, createOp)

err = resolver.Anchor(context.Background(), createOp)
_, err = resolver.Anchor(context.Background(), createOp)
assert.NoError(tt, err)
})
}
Expand Down
21 changes: 13 additions & 8 deletions did/ion/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,34 +96,39 @@ func (i Resolver) Resolve(ctx context.Context, id string, _ ...resolution.Option

// Anchor submits an anchor operation to the ION node by appending the operations path to the base URL
// and making a POST request
func (i Resolver) Anchor(ctx context.Context, op AnchorOperation) error {
func (i Resolver) Anchor(ctx context.Context, op AnchorOperation) (*resolution.Result, error) {
if i.baseURL.String() == "" {
return errors.New("resolution URL cannot be empty")
return nil, errors.New("resolution URL cannot be empty")
}
jsonOpBytes, err := json.Marshal(op)
if err != nil {
return errors.Wrapf(err, "marshalling anchor operation %+v", op)
return nil, errors.Wrapf(err, "marshalling anchor operation %+v", op)
}

req, err := http.NewRequestWithContext(ctx, http.MethodPost, strings.Join([]string{i.baseURL.String(), "operations"}, "/"), bytes.NewReader(jsonOpBytes))
if err != nil {
return errors.Wrap(err, "creating request")
return nil, errors.Wrap(err, "creating request")
}
resp, err := i.client.Do(req)
if err != nil {
return errors.Wrapf(err, "posting anchor operation %+v", op)
return nil, errors.Wrapf(err, "posting anchor operation %+v", op)
}

defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return errors.Wrapf(err, "could not resolve with response %+v", resp)
return nil, errors.Wrapf(err, "could not resolve with response %+v", resp)
}
if !is2xxStatusCode(resp.StatusCode) {
return fmt.Errorf("anchor operation failed: %s", string(body))
return nil, fmt.Errorf("anchor operation failed: %s", string(body))
}
logrus.Infof("successfully anchored operation: %s", string(body))
return nil

var resolutionResult resolution.Result
if err := json.Unmarshal(body, &resolutionResult); err != nil {
return nil, errors.Wrap(err, "unmarshalling anchor response")
}
return &resolutionResult, nil
}

func (Resolver) Methods() []did.Method {
Expand Down

0 comments on commit 83e6a4e

Please sign in to comment.