This repository has been archived by the owner on Apr 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
076ddd9
commit 81e51e4
Showing
6 changed files
with
267 additions
and
78 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package libp2pcarserver | ||
|
||
import ( | ||
"bytes" | ||
"encoding/base64" | ||
"fmt" | ||
|
||
"github.com/ipfs/go-cid" | ||
"github.com/ipld/go-ipld-prime" | ||
"github.com/ipld/go-ipld-prime/codec/dagcbor" | ||
basicnode "github.com/ipld/go-ipld-prime/node/basic" | ||
) | ||
|
||
// CARTransferRequest is the request sent by the client to transfer a CAR file | ||
// for the given root and selector | ||
type CARTransferRequest struct { | ||
Root string // base64 encoded byte array | ||
Selector string // base 64 encoded byte array | ||
} | ||
|
||
type dagTraversalRequest struct { | ||
root cid.Cid | ||
selector ipld.Node | ||
} | ||
|
||
func carRequestToDAGRequest(req *CARTransferRequest) (*dagTraversalRequest, error) { | ||
rootbz, err := base64.StdEncoding.DecodeString(req.Root) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to decode root: %s", err) | ||
} | ||
rootcid, err := cid.Cast(rootbz) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to cast root to cid: %s", err) | ||
} | ||
|
||
selbz, err := base64.StdEncoding.DecodeString(req.Selector) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to decode selector: %s", err) | ||
} | ||
sel, err := decodeSelector(selbz) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to decode selector to ipld node: %s", err) | ||
} | ||
|
||
return &dagTraversalRequest{ | ||
root: rootcid, | ||
selector: sel, | ||
}, nil | ||
} | ||
|
||
func decodeSelector(sel []byte) (ipld.Node, error) { | ||
nb := basicnode.Prototype.Any.NewBuilder() | ||
if err := dagcbor.Decode(nb, bytes.NewReader(sel)); err != nil { | ||
return nil, err | ||
} | ||
return nb.Build(), nil | ||
} |
Oops, something went wrong.