-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Publish IPLD node built with go-ipld-prime.
The RPC client still requires the legacy format, but now I'm ready when the client is. Also, now we can start to build linked data in SPACE™
- Loading branch information
Showing
8 changed files
with
246 additions
and
55 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 |
---|---|---|
@@ -1,3 +1,5 @@ | ||
# TODO | ||
|
||
Write better documentation and maybe some examples. It's about time | ||
|
||
Fix setting loglevel early with generation |
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 |
---|---|---|
@@ -1,35 +1,130 @@ | ||
package doc | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
|
||
blocks "github.com/ipfs/go-block-format" | ||
"github.com/ipfs/go-cid" | ||
ipldcbor "github.com/ipfs/go-ipld-cbor" | ||
format "github.com/ipfs/go-ipld-format" | ||
mc "github.com/multiformats/go-multicodec" | ||
mh "github.com/multiformats/go-multihash" | ||
ipldlegacy "github.com/ipfs/go-ipld-legacy" | ||
"github.com/ipld/go-ipld-prime" | ||
"github.com/ipld/go-ipld-prime/codec/dagcbor" | ||
"github.com/ipld/go-ipld-prime/node/basicnode" | ||
multihash "github.com/multiformats/go-multihash" | ||
) | ||
|
||
func (d *Document) Node() (format.Node, cid.Cid, error) { | ||
type documentNode struct { | ||
Node format.Node | ||
Cid cid.Cid | ||
} | ||
|
||
func (d *Document) Node() (documentNode, error) { | ||
|
||
// Convert your struct to an IPLD node | ||
node, err := d.ipldStructure() | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
var buf []byte | ||
buf, err = encodeIPLDNodeToDAGCBOR(node) | ||
if err != nil { | ||
return documentNode{}, fmt.Errorf("error encoding node to DAG-CBOR: %w", err) | ||
} | ||
// Create a CID for the block | ||
c, err := cid.V1Builder{Codec: cid.DagCBOR, MhType: multihash.SHA2_256}.Sum(buf) | ||
if err != nil { | ||
return documentNode{}, fmt.Errorf("error creating CID: %w", err) | ||
} | ||
|
||
// Create the block | ||
blk, err := blocks.NewBlockWithCid(buf, c) | ||
if err != nil { | ||
return documentNode{}, fmt.Errorf("error creating block: %w", err) | ||
} | ||
|
||
legacyNode := ipldlegacy.LegacyNode{Node: node, Block: blk} | ||
|
||
n := documentNode{Node: &legacyNode, Cid: c} | ||
|
||
return n, nil | ||
} | ||
|
||
func (d *Document) ipldStructure() (ipld.Node, error) { | ||
nb := basicnode.Prototype.Map.NewBuilder() | ||
ma, err := nb.BeginMap(7) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// Context | ||
contextNode, err := buildStringListNode(d.Context) | ||
if err != nil { | ||
return nil, err | ||
} | ||
ma.AssembleKey().AssignString("context") | ||
ma.AssembleValue().AssignNode(contextNode) | ||
|
||
// ID | ||
ma.AssembleKey().AssignString("id") | ||
ma.AssembleValue().AssignString(d.ID) | ||
|
||
// Controllers | ||
controllerNode, err := buildStringListNode(d.Controller) | ||
if err != nil { | ||
return nil, err | ||
} | ||
ma.AssembleKey().AssignString("controller") | ||
ma.AssembleValue().AssignNode(controllerNode) | ||
|
||
// Hash the CBOR data to create a CID | ||
data, err := d.MarshalToCBOR() | ||
// VerificationMethod | ||
ma.AssembleKey().AssignString("verificationMethod") | ||
verificationMethodsNode, err := buildVerificationMethodList(d.VerificationMethod) | ||
if err != nil { | ||
return nil, cid.Cid{}, err | ||
return nil, err | ||
} | ||
ma.AssembleValue().AssignNode(verificationMethodsNode) | ||
|
||
// AssertionMethod | ||
ma.AssembleKey().AssignString("assertionMethod") | ||
ma.AssembleValue().AssignString(d.AssertionMethod) | ||
|
||
// KeyAgreement | ||
ma.AssembleKey().AssignString("keyAgreement") | ||
ma.AssembleValue().AssignString(d.KeyAgreement) | ||
|
||
hash, err := mh.Sum(data, mh.SHA2_256, -1) | ||
// Proof | ||
proofNode, err := buildProofNode(d.Proof) | ||
if err != nil { | ||
return nil, cid.Cid{}, err | ||
return nil, err | ||
} | ||
ma.AssembleKey().AssignString("proof") | ||
ma.AssembleValue().AssignNode(proofNode) | ||
|
||
// Create a CID with the DagCBOR codec | ||
codecType := uint64(mc.DagCbor) | ||
c := cid.NewCidV1(codecType, hash) | ||
ma.Finish() | ||
|
||
return nb.Build(), nil | ||
} | ||
|
||
// Use go-ipld-cbor to decode the CBOR data into an IPLD node | ||
node, err := ipldcbor.Decode(data, mh.SHA2_256, -1) | ||
func buildStringListNode(controllers []string) (ipld.Node, error) { | ||
nb := basicnode.Prototype.List.NewBuilder() | ||
la, err := nb.BeginList(-1) | ||
if err != nil { | ||
return nil, cid.Cid{}, err | ||
return nil, err | ||
} | ||
for _, controller := range controllers { | ||
la.AssembleValue().AssignString(controller) | ||
} | ||
la.Finish() | ||
|
||
return nb.Build(), nil | ||
} | ||
|
||
return node, c, nil | ||
func encodeIPLDNodeToDAGCBOR(node ipld.Node) ([]byte, error) { | ||
var buf bytes.Buffer | ||
if err := dagcbor.Encode(node, &buf); err != nil { | ||
return nil, err | ||
} | ||
return buf.Bytes(), nil | ||
} |
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.