-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(x/data/v2/client)!: add generate-iri command (#2190)
- Loading branch information
Showing
9 changed files
with
152 additions
and
20 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package client | ||
|
||
import ( | ||
"crypto" | ||
"encoding/json" | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/piprate/json-gold/ld" | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/regen-network/regen-ledger/x/data/v2" | ||
) | ||
|
||
func GenerateIRI() *cobra.Command { | ||
return &cobra.Command{ | ||
Use: "generate-iri [filename]", | ||
Short: "Creates the content IRI for a file", | ||
Long: "Creates the content IRI for a file. If the extension is .jsonld, a graph IRI will be created, otherwise a raw IRI will be created.", | ||
Example: formatExample(`regen q data generate-iri myfile.ext`), | ||
Args: cobra.ExactArgs(1), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
_, ctx, err := mkQueryClient(cmd) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
filename := args[0] | ||
ext := filepath.Ext(filename) | ||
contents, err := os.ReadFile(filename) | ||
if err != nil { | ||
return fmt.Errorf("failed to read file: %w", err) | ||
} | ||
|
||
ch := &data.ContentHash{} | ||
switch ext { | ||
case ".jsonld": | ||
proc := ld.NewJsonLdProcessor() | ||
opts := ld.NewJsonLdOptions("") | ||
opts.Format = "application/n-quads" | ||
opts.Algorithm = ld.AlgorithmURDNA2015 | ||
|
||
var doc map[string]interface{} | ||
err = json.Unmarshal(contents, &doc) | ||
if err != nil { | ||
return fmt.Errorf("failed to unmarshal json: %w", err) | ||
} | ||
|
||
normalizedTriples, err := proc.Normalize(doc, opts) | ||
if err != nil { | ||
return fmt.Errorf("failed to normalize json: %w", err) | ||
} | ||
|
||
hash, err := blake2b256hash(fmt.Sprintf("%s", normalizedTriples)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
ch.Graph = &data.ContentHash_Graph{ | ||
Hash: hash, | ||
DigestAlgorithm: data.DigestAlgorithm_DIGEST_ALGORITHM_BLAKE2B_256, | ||
CanonicalizationAlgorithm: data.GraphCanonicalizationAlgorithm_GRAPH_CANONICALIZATION_ALGORITHM_URDNA2015, | ||
MerkleTree: data.GraphMerkleTree_GRAPH_MERKLE_TREE_NONE_UNSPECIFIED, | ||
} | ||
default: | ||
hash, err := blake2b256hash(string(contents)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
ext = ext[1:] // take the . off the extension | ||
mediaType := data.RawMediaTypeFromExt(ext) | ||
|
||
ch.Raw = &data.ContentHash_Raw{ | ||
Hash: hash, | ||
DigestAlgorithm: data.DigestAlgorithm_DIGEST_ALGORITHM_BLAKE2B_256, | ||
MediaType: mediaType, | ||
} | ||
} | ||
|
||
iri, err := ch.ToIRI() | ||
if err != nil { | ||
return fmt.Errorf("failed to convert content hash to IRI: %w", err) | ||
} | ||
|
||
return ctx.PrintString(iri) | ||
}, | ||
} | ||
} | ||
|
||
func blake2b256hash(contents string) ([]byte, error) { | ||
hasher := crypto.BLAKE2b_256.New() | ||
_, err := hasher.Write([]byte(contents)) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to hash normalized triples: %w", err) | ||
} | ||
return hasher.Sum(nil), 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
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