Skip to content

Commit 2d6cc85

Browse files
author
Silas Davis
authored
Merge pull request #44 from gregdhill/js-dynamic
cp hoard.proto into hoard-js
2 parents e971b66 + 4d78de7 commit 2d6cc85

File tree

7 files changed

+318
-327
lines changed

7 files changed

+318
-327
lines changed

Diff for: hoard-js/Makefile

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
.PHONY: protobuf
22
protobuf:
33
@grpc_tools_node_protoc --js_out=import_style=commonjs,binary:protobuf --grpc_out=protobuf \
4-
--plugin=protoc-gen-grpc=`which grpc_tools_node_protoc_plugin` --proto_path=../protobuf/ hoard.proto
4+
--plugin=protoc-gen-grpc=`which grpc_tools_node_protoc_plugin` --proto_path=../protobuf/ hoard.proto
5+
@cp ../protobuf/hoard.proto ./protobuf/hoard.proto

Diff for: hoard-js/examples.js

+2-53
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,6 @@
55
// the Hoard daemon.
66
let Hoard = require('./index.js');
77

8-
var fs = require('fs');
9-
const path = require('path')
10-
11-
const openpgp = require('openpgp');
12-
openpgp.initWorker({ path:'openpgp.worker.js' })
13-
148
// This is the default tcp socket that hoard runs on, just run `bin/hoard`
159
// after running `make build` in the main hoard repo.
1610
let hoard = new Hoard.Client('localhost:53431');
@@ -43,7 +37,7 @@ const example = async function (plaintextIn) {
4337
// Put the plaintext in storage
4438
ref = await hoard.put(plaintextIn);
4539
// (Base64 should be standard text representation for address, secretKey, and salt)
46-
console.log(base64ify(ref));
40+
console.log(hoard.base64ify(ref));
4741
// We can get the plaintext back by `get`ing the grant
4842
plaintext = await hoard.get(ref);
4943
console.log('Plaintext (Reference): ' + plaintext.Data.toString());
@@ -81,40 +75,11 @@ const example = async function (plaintextIn) {
8175
}
8276

8377
grant = await hoard.putseal(grantIn);
84-
console.log(base64ify(grant));
78+
console.log(hoard.base64ify(grant));
8579

8680
// We can get the plaintext back by `get`ing the grant
8781
plaintext = await hoard.unsealget(grant);
8882
console.log('Plaintext (Grant): ' + plaintext.Data.toString());
89-
90-
// OpenPGP
91-
// Note: This will only work if the hoard daemon is configured to do PGP signing
92-
var pubkey = fs.readFileSync(path.join(__dirname, '../grant', 'public.key.asc'));
93-
var privkey = fs.readFileSync(path.join(__dirname, '../grant', 'private.key.asc'));
94-
95-
grantIn = {
96-
Plaintext: plaintextIn,
97-
GrantSpec: {
98-
OpenPGP: {
99-
PublicKey: pubkey.toString()
100-
}
101-
}
102-
}
103-
104-
grant = await hoard.putseal(grantIn);
105-
console.log(grant.Data)
106-
console.log(base64ify(grant));
107-
108-
const options = {
109-
message: await openpgp.message.readArmored(grant.EncryptedReference),
110-
privateKeys: [(await openpgp.key.readArmored(privkey)).keys[0]]
111-
}
112-
113-
ref = JSON.parse((await openpgp.decrypt(options)).data)
114-
console.log(base64ify(ref));
115-
plaintext = await hoard.get(ref)
116-
console.log(plaintext)
117-
console.log('Plaintext (PGP Grant): ' + plaintext.Data.toString());
11883
}
11984
catch (err) {
12085
console.log(err)
@@ -123,19 +88,3 @@ const example = async function (plaintextIn) {
12388

12489
// Run the async example in this case ignoring the promise result
12590
example(plaintext);
126-
127-
128-
// Utility for printing message types
129-
const base64ify = function (obj) {
130-
let newObj = {};
131-
for (let key of Object.keys(obj)) {
132-
let value = obj[key];
133-
if (value instanceof Buffer) {
134-
newObj[key] = value.toString(`base64`)
135-
} else {
136-
newObj[key] = value
137-
}
138-
}
139-
return newObj;
140-
};
141-

Diff for: hoard-js/index.js

+14
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,20 @@ HoardClient.prototype.stat = function (address) {
153153
});
154154
};
155155

156+
// Utility for printing message types
157+
HoardClient.prototype.base64ify = function (obj) {
158+
let newObj = {};
159+
for (let key of Object.keys(obj)) {
160+
let value = obj[key];
161+
if (value instanceof Buffer) {
162+
newObj[key] = value.toString(`base64`)
163+
} else {
164+
newObj[key] = value
165+
}
166+
}
167+
return newObj;
168+
};
169+
156170
HoardClientStatic.prototype = Object.create(HoardClient.prototype);
157171
HoardClientDynamic.prototype = Object.create(HoardClient.prototype);
158172

Diff for: hoard-js/openpgp.js

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
let Hoard = require('./index.js');
2+
3+
var fs = require('fs');
4+
const path = require('path')
5+
6+
// npm install openpgp
7+
const openpgp = require('openpgp');
8+
openpgp.initWorker({ path:'openpgp.worker.js' })
9+
10+
let hoard = new Hoard.Client('localhost:53431');
11+
let plaintext = {
12+
Data: Buffer.from('some stuff', 'utf8'),
13+
Salt: Buffer.from('foo', 'ascii')
14+
};
15+
16+
// Below is an example of using Hoard's asymmetric grants
17+
// Note: This will only work if the hoard daemon is configured to do PGP signing
18+
const example = async function (plaintextIn) {
19+
try {
20+
var plaintext, ref, grant
21+
22+
var pubkey = fs.readFileSync(path.join(__dirname, '../grant', 'public.key.asc'));
23+
var privkey = fs.readFileSync(path.join(__dirname, '../grant', 'private.key.asc'));
24+
25+
grantIn = {
26+
Plaintext: plaintextIn,
27+
GrantSpec: {
28+
OpenPGP: {
29+
PublicKey: pubkey.toString()
30+
}
31+
}
32+
}
33+
34+
grant = await hoard.putseal(grantIn);
35+
console.log(grant.Data)
36+
console.log(hoard.base64ify(grant));
37+
38+
const options = {
39+
message: await openpgp.message.readArmored(grant.EncryptedReference),
40+
privateKeys: [(await openpgp.key.readArmored(privkey)).keys[0]]
41+
}
42+
43+
ref = JSON.parse((await openpgp.decrypt(options)).data)
44+
console.log(hoard.base64ify(ref));
45+
plaintext = await hoard.get(ref)
46+
console.log(plaintext)
47+
console.log('Plaintext (PGP Grant): ' + plaintext.Data.toString());
48+
}
49+
catch (err) {
50+
console.log(err)
51+
}
52+
};
53+
54+
example(plaintext);

0 commit comments

Comments
 (0)