Skip to content

Commit 457d58e

Browse files
authored
Merge pull request #11 from iden3/feature/iden-210
feature/iden-210: IPFS SCHEMA LOADER
2 parents 9366230 + 01e3e4c commit 457d58e

File tree

9 files changed

+590
-9
lines changed

9 files changed

+590
-9
lines changed

.github/workflows/ci-test.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,6 @@ jobs:
2626
restore-keys: |
2727
${{ runner.os }}-go-
2828
- name: Unit Tests
29+
env:
30+
IPFS_URL: ${{secrets.IPFS_URL }}
2931
run: go test -race -timeout=60s ./...

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.idea
2+
.vscode

go.mod

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ require (
88
github.com/iden3/go-iden3-crypto v0.0.11
99
github.com/iden3/go-merkletree-sql v1.0.0-pre8
1010
github.com/iden3/go-schema-registry-wrapper v0.0.7
11+
github.com/ipfs/go-ipfs-api v0.3.0
1112
github.com/pkg/errors v0.9.1
1213
github.com/qri-io/jsonschema v0.2.1
1314
github.com/stretchr/testify v1.7.0
@@ -18,17 +19,38 @@ require (
1819
require (
1920
github.com/StackExchange/wmi v0.0.0-20180116203802-5d049714c4a6 // indirect
2021
github.com/btcsuite/btcd v0.22.0-beta // indirect
22+
github.com/crackcomm/go-gitignore v0.0.0-20170627025303-887ab5e44cc3 // indirect
2123
github.com/davecgh/go-spew v1.1.1 // indirect
2224
github.com/deckarep/golang-set v0.0.0-20180603214616-504e848d77ea // indirect
2325
github.com/go-ole/go-ole v1.2.1 // indirect
2426
github.com/go-stack/stack v1.8.0 // indirect
27+
github.com/gogo/protobuf v1.3.1 // indirect
2528
github.com/gorilla/websocket v1.4.2 // indirect
29+
github.com/ipfs/go-cid v0.0.7 // indirect
30+
github.com/ipfs/go-ipfs-files v0.0.9 // indirect
31+
github.com/libp2p/go-buffer-pool v0.0.2 // indirect
32+
github.com/libp2p/go-flow-metrics v0.0.3 // indirect
33+
github.com/libp2p/go-libp2p-core v0.6.1 // indirect
34+
github.com/libp2p/go-openssl v0.0.7 // indirect
35+
github.com/minio/blake2b-simd v0.0.0-20160723061019-3f5f724cb5b1 // indirect
36+
github.com/minio/sha256-simd v0.1.1 // indirect
37+
github.com/mitchellh/go-homedir v1.1.0 // indirect
2638
github.com/mr-tron/base58 v1.2.0 // indirect
39+
github.com/multiformats/go-base32 v0.0.3 // indirect
40+
github.com/multiformats/go-base36 v0.1.0 // indirect
41+
github.com/multiformats/go-multiaddr v0.3.0 // indirect
42+
github.com/multiformats/go-multibase v0.0.3 // indirect
43+
github.com/multiformats/go-multihash v0.0.14 // indirect
44+
github.com/multiformats/go-varint v0.0.6 // indirect
2745
github.com/pmezard/go-difflib v1.0.0 // indirect
2846
github.com/qri-io/jsonpointer v0.1.1 // indirect
2947
github.com/shirou/gopsutil v3.21.4-0.20210419000835-c7a38de76ee5+incompatible // indirect
48+
github.com/spacemonkeygo/spacelog v0.0.0-20180420211403-2296661a0572 // indirect
49+
github.com/spaolacci/murmur3 v1.1.0 // indirect
3050
github.com/tklauser/go-sysconf v0.3.5 // indirect
3151
github.com/tklauser/numcpus v0.2.2 // indirect
52+
github.com/whyrusleeping/tar-utils v0.0.0-20180509141711-8c6c8ba81d5c // indirect
53+
go.opencensus.io v0.22.4 // indirect
3254
golang.org/x/sys v0.0.0-20220114195835-da31bd327af9 // indirect
3355
gopkg.in/natefinch/npipe.v2 v2.0.0-20160621034901-c1b8fa8bdcce // indirect
3456
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect

go.sum

Lines changed: 62 additions & 0 deletions
Large diffs are not rendered by default.

loaders/http.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ import (
1111
"time"
1212
)
1313

14+
// ErrorURLEmpty is empty url error
15+
var ErrorURLEmpty = errors.New("URL is empty")
16+
1417
// HTTP is loader for http / https schemas
1518
type HTTP struct {
1619
URL string
@@ -20,7 +23,7 @@ type HTTP struct {
2023
func (l HTTP) Load(ctx context.Context) (schema []byte, extension string, err error) {
2124

2225
if l.URL == "" {
23-
return nil, "", errors.New("URL is empty")
26+
return nil, "", ErrorURLEmpty
2427
}
2528
// parse schema url
2629
u, err := url.Parse(l.URL)

loaders/ipfs.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package loaders
2+
3+
import (
4+
"bytes"
5+
"context"
6+
shell "github.com/ipfs/go-ipfs-api"
7+
"github.com/pkg/errors"
8+
)
9+
10+
// ErrorCIDEEmpty is for error when CID is empty
11+
var ErrorCIDEEmpty = errors.New("CID is empty")
12+
13+
// IPFS loader for fetching schema
14+
type IPFS struct {
15+
URL string
16+
CID string
17+
}
18+
19+
// Load method IPFS implementation
20+
func (l IPFS) Load(ctx context.Context) (schema []byte, extension string, err error) {
21+
22+
if l.URL == "" {
23+
return nil, "", ErrorURLEmpty
24+
}
25+
26+
if l.CID == "" {
27+
return nil, "", ErrorCIDEEmpty
28+
}
29+
30+
sh := shell.NewShell(l.URL)
31+
32+
data, err := sh.Cat(l.CID)
33+
34+
if err != nil {
35+
return nil, "", err
36+
}
37+
38+
buf := new(bytes.Buffer)
39+
_, err = buf.ReadFrom(data)
40+
if err != nil {
41+
return nil, "", err
42+
}
43+
44+
return buf.Bytes(), "json-ld", nil
45+
}

loaders/ipfs_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package loaders
2+
3+
import (
4+
"bytes"
5+
"encoding/json"
6+
"fmt"
7+
shell "github.com/ipfs/go-ipfs-api"
8+
"github.com/stretchr/testify/assert"
9+
"os"
10+
"testing"
11+
)
12+
13+
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"]}}]}`
14+
15+
func UploadToIpfs() (string, error) {
16+
17+
var m map[string]interface{}
18+
sh := shell.NewShell(os.Getenv("IPFS_URL"))
19+
err := json.Unmarshal([]byte(str), &m)
20+
if err != nil {
21+
return "", err
22+
}
23+
b, err := json.Marshal(m)
24+
if err != nil {
25+
return "", err
26+
}
27+
28+
cid, err := sh.Add(bytes.NewReader(b))
29+
if err != nil {
30+
return "", err
31+
}
32+
33+
return cid, nil
34+
35+
}
36+
37+
func TestUpload(t *testing.T) {
38+
39+
a, err := UploadToIpfs()
40+
fmt.Println(a)
41+
assert.Nil(t, err)
42+
43+
}

0 commit comments

Comments
 (0)