Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/ci-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,6 @@ jobs:
restore-keys: |
${{ runner.os }}-go-
- name: Unit Tests
env:
IPFS_URL: ${{secrets.IPFS_URL }}
run: go test -race -timeout=60s ./...
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.idea
.vscode
22 changes: 22 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/iden3/go-iden3-crypto v0.0.11
github.com/iden3/go-merkletree-sql v1.0.0-pre8
github.com/iden3/go-schema-registry-wrapper v0.0.7
github.com/ipfs/go-ipfs-api v0.3.0
github.com/pkg/errors v0.9.1
github.com/qri-io/jsonschema v0.2.1
github.com/stretchr/testify v1.7.0
Expand All @@ -18,17 +19,38 @@ require (
require (
github.com/StackExchange/wmi v0.0.0-20180116203802-5d049714c4a6 // indirect
github.com/btcsuite/btcd v0.22.0-beta // indirect
github.com/crackcomm/go-gitignore v0.0.0-20170627025303-887ab5e44cc3 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/deckarep/golang-set v0.0.0-20180603214616-504e848d77ea // indirect
github.com/go-ole/go-ole v1.2.1 // indirect
github.com/go-stack/stack v1.8.0 // indirect
github.com/gogo/protobuf v1.3.1 // indirect
github.com/gorilla/websocket v1.4.2 // indirect
github.com/ipfs/go-cid v0.0.7 // indirect
github.com/ipfs/go-ipfs-files v0.0.9 // indirect
github.com/libp2p/go-buffer-pool v0.0.2 // indirect
github.com/libp2p/go-flow-metrics v0.0.3 // indirect
github.com/libp2p/go-libp2p-core v0.6.1 // indirect
github.com/libp2p/go-openssl v0.0.7 // indirect
github.com/minio/blake2b-simd v0.0.0-20160723061019-3f5f724cb5b1 // indirect
github.com/minio/sha256-simd v0.1.1 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/mr-tron/base58 v1.2.0 // indirect
github.com/multiformats/go-base32 v0.0.3 // indirect
github.com/multiformats/go-base36 v0.1.0 // indirect
github.com/multiformats/go-multiaddr v0.3.0 // indirect
github.com/multiformats/go-multibase v0.0.3 // indirect
github.com/multiformats/go-multihash v0.0.14 // indirect
github.com/multiformats/go-varint v0.0.6 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/qri-io/jsonpointer v0.1.1 // indirect
github.com/shirou/gopsutil v3.21.4-0.20210419000835-c7a38de76ee5+incompatible // indirect
github.com/spacemonkeygo/spacelog v0.0.0-20180420211403-2296661a0572 // indirect
github.com/spaolacci/murmur3 v1.1.0 // indirect
github.com/tklauser/go-sysconf v0.3.5 // indirect
github.com/tklauser/numcpus v0.2.2 // indirect
github.com/whyrusleeping/tar-utils v0.0.0-20180509141711-8c6c8ba81d5c // indirect
go.opencensus.io v0.22.4 // indirect
golang.org/x/sys v0.0.0-20220114195835-da31bd327af9 // indirect
gopkg.in/natefinch/npipe.v2 v2.0.0-20160621034901-c1b8fa8bdcce // indirect
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
Expand Down
62 changes: 62 additions & 0 deletions go.sum

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion loaders/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ import (
"time"
)

// ErrorURLEmpty is empty url error
var ErrorURLEmpty = errors.New("URL is empty")

// HTTP is loader for http / https schemas
type HTTP struct {
URL string
Expand All @@ -20,7 +23,7 @@ type HTTP struct {
func (l HTTP) Load(ctx context.Context) (schema []byte, extension string, err error) {

if l.URL == "" {
return nil, "", errors.New("URL is empty")
return nil, "", ErrorURLEmpty
}
// parse schema url
u, err := url.Parse(l.URL)
Expand Down
45 changes: 45 additions & 0 deletions loaders/ipfs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package loaders

import (
"bytes"
"context"
shell "github.com/ipfs/go-ipfs-api"
"github.com/pkg/errors"
)

// ErrorCIDEEmpty is for error when CID is empty
var ErrorCIDEEmpty = errors.New("CID is empty")

// IPFS loader for fetching schema
type IPFS struct {
URL string
CID string
}

// Load method IPFS implementation
func (l IPFS) Load(ctx context.Context) (schema []byte, extension string, err error) {

if l.URL == "" {
return nil, "", ErrorURLEmpty
}

if l.CID == "" {
return nil, "", ErrorCIDEEmpty
}

sh := shell.NewShell(l.URL)

data, err := sh.Cat(l.CID)

if err != nil {
return nil, "", err
}

buf := new(bytes.Buffer)
_, err = buf.ReadFrom(data)
if err != nil {
return nil, "", err
}

return buf.Bytes(), "json-ld", nil
}
43 changes: 43 additions & 0 deletions loaders/ipfs_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package loaders

import (
"bytes"
"encoding/json"
"fmt"
shell "github.com/ipfs/go-ipfs-api"
"github.com/stretchr/testify/assert"
"os"
"testing"
)

var str = `{"id":"c0f6ac87-603e-44cd-8d83-0caeb458d50d","@context":["https://raw.githubusercontent.com/iden3/claim-schema-vocab/main/schemas/json-ld/iden3credential.json-ld","https://raw.githubusercontent.com/iden3/claim-schema-vocab/main/schemas/json-ld/auth.json-ld"],"@type":["Iden3Credential"],"expiration":"2361-03-21T21:14:48+02:00","updatable":false,"version":0,"rev_nonce":2034832188220019200,"credentialSubject":{"type":"AuthBJJCredential","x":"12747559771369266961976321746772881814229091957322087014312756428846389160887","y":"7732074634595480184356588475330446395691728690271550550016720788712795268212"},"credentialStatus":{"id":"http://localhost:8001/api/v1/identities/118VhAf6ng6J44FhNrGeYzSbJgGVmcpeXYFR2YTrZ6/claims/revocation/status/2034832188220019081","type":"SparseMerkleTreeProof"},"credentialSchema":{"@id":"https://raw.githubusercontent.com/iden3/claim-schema-vocab/main/schemas/json-ld/auth.json-ld","type":"JsonSchemaValidator2018"},"proof":[{"@type":"BJJSignature2021","issuer":"118VhAf6ng6J44FhNrGeYzSbJgGVmcpeXYFR2YTrZ6","h_index":"c89cf5b95157f091f2d8bf49bc1a57cd7988da83bbcd982a74c5e8c70e566403","h_value":"0262b2cd6b9ae44cd9a39045c9bb03ad4e1f056cb81d855f1fc4ef0cdf827912","created":1642518655,"issuer_mtp":{"@type":"Iden3SparseMerkleProof","issuer":"118VhAf6ng6J44FhNrGeYzSbJgGVmcpeXYFR2YTrZ6","h_index":"201a02eb979be695702ea37d930309d2965d803541be5f7b3900459b2fad8726","h_value":"0654da1d53ca201cb42b767a6f12265ff7a08720b88a82182e0f20702479d12d","state":{"claims_tree_root":"a5087cfa6f2c7c565d831327091533f09999133df1df51104d2ce6f8e4d90529","value":"dca344e95da517a301729d94b213298b9de96dfddaf7aad9423d918ea3208820"},"mtp":{"existence":true,"siblings":[]}},"verification_method":"2764e2d8241b18c217010ebf90bebb30240d32c33f3007f33e42d58680813123","proof_value":"c354eb1006534c59766ed8398d49a9a614312e430c5373ea493395db6369d49485e9a0d63f3bfe9fd157294ffbf706b6b7df7a8662a58fae0056a046af1caa04","proof_purpose":"Authentication"},{"@type":"Iden3SparseMerkleProof","issuer":"118VhAf6ng6J44FhNrGeYzSbJgGVmcpeXYFR2YTrZ6","h_index":"c89cf5b95157f091f2d8bf49bc1a57cd7988da83bbcd982a74c5e8c70e566403","h_value":"0262b2cd6b9ae44cd9a39045c9bb03ad4e1f056cb81d855f1fc4ef0cdf827912","state":{"tx_id":"0xf2e23524ab76cb4f371b921a214ff411d5d391962899a2afe20f356e3bdc0c71","block_timestamp":1642522496,"block_number":11837707,"claims_tree_root":"bebcaee8444e93b6e32855f54e9f617d5fd654570badce7d6bc649304169681d","revocation_tree_root":"0000000000000000000000000000000000000000000000000000000000000000","value":"2806aa9a045b2a5503b12f2979b2d19933e803fd3dd73d8ad40dc138bc9a582e"},"mtp":{"existence":true,"siblings":["0","0","0","18555164879275043542501047154170418730098376961920428892719505858997411121317"]}}]}`

func UploadToIpfs() (string, error) {

var m map[string]interface{}
sh := shell.NewShell(os.Getenv("IPFS_URL"))
err := json.Unmarshal([]byte(str), &m)
if err != nil {
return "", err
}
b, err := json.Marshal(m)
if err != nil {
return "", err
}

cid, err := sh.Add(bytes.NewReader(b))
if err != nil {
return "", err
}

return cid, nil

}

func TestUpload(t *testing.T) {

a, err := UploadToIpfs()
fmt.Println(a)
assert.Nil(t, err)

}
Loading