-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.babel.js
94 lines (87 loc) · 2.39 KB
/
gulpfile.babel.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import gulp from 'gulp'
import { argv } from 'yargs'
import fs from 'fs'
import Client from 'o-api-client'
import forge from 'node-forge'
import fetch from 'node-fetch'
/*
*
* TODO: openssl rsa -pubin -inform PEM -text -noout < public.key
*/
gulp.task('key:new', () => {
let keypair = forge.pki.rsa.generateKeyPair({bits: 4096, e: 0x10001})
console.log(forge.pki.privateKeyToPem(keypair.privateKey))
console.log(forge.pki.publicKeyToPem(keypair.publicKey))
})
/**
* @param path - path to JSON-LD file we want to sign
* @param pubKey - URI of a public key
* @param privPem - PEM encoded private key
*/
gulp.task('sign', (done) => {
console.log('sign path: ', argv.path)
console.log('sign pubKey: ', argv.pubKey)
console.log('sign privPem: ', argv.privPem)
let doc = JSON.parse(fs.readFileSync(argv.path, 'utf8'))
let privPem = fs.readFileSync(argv.privPem, 'utf8')
let client = new Client(fetch, privPem, argv.pubKey)
client.sign(doc)
.then((signed) => {
console.log(JSON.stringify(signed))
return done()
}).catch((err) => {
console.log(err)
return done()
})
})
/**
* @param path - path to JSON-LD file we want to verify
*/
gulp.task('verify', (done) => {
console.log('sign path: ', argv.path)
let doc = JSON.parse(fs.readFileSync(argv.path, 'utf8'))
let client = new Client(fetch)
client.verify(doc)
.then((verified) => {
console.log(verified)
return done()
}).catch((err) => {
console.log(err)
return done()
})
})
/**
* @param uri - URI to fetch
*/
gulp.task('fetch', (done) => {
console.log('fetch uri: ', argv.uri)
fetch(argv.uri)
.then((res) => {
return res.text()
}).then((body) => {
console.log(body)
return done()
}).catch((err) => {
console.log(err)
return done()
})
})
/**
* @param path - path to JSON-LD file
* @param container - uri of container we post to
*/
gulp.task('publish', (done) => {
console.log('publish path: ', argv.path)
console.log('publish container: ', argv.container)
let doc = JSON.parse(fs.readFileSync(argv.path, 'utf8'))
fetch(argv.container, { method: 'POST', body: JSON.stringify(doc), headers: { 'Content-Type': 'application/ld+json' } })
.then((res) => {
return res.json()
}).then((json) => {
console.log(json)
return done()
}).catch((err) => {
console.log(err)
return done()
})
})