Skip to content

Commit

Permalink
Clean up API interface to be simpler for publicPeerFile
Browse files Browse the repository at this point in the history
  • Loading branch information
creationix committed Apr 12, 2013
1 parent 7e8bd1d commit d8bfac7
Showing 1 changed file with 85 additions and 44 deletions.
129 changes: 85 additions & 44 deletions lib/stack/genPeer.js
Expand Up @@ -108,72 +108,113 @@ function merge(a, b) {
}

/**
* @param sections is an object containing A, B, and/or C objects
*
* "A" contains "cipher", "created", "expires", and "saltBundle"
* "B" if it exists, contains "findSecret"
* "C" if it exists, contains "identities"
* @param args is an object hash of the named arguments.
*
* @param keyPair is an rsa key pair with publicKey and privateKey properties.
* @param domain is the domain to use in the peer contact URI
* "privateKey" - RSA private key used to sign bundles
* "publicKey" - RSA public key stored in section A's signature
* "domain" - domain to use in contactURI
* "lifetime" - the number of seconds till this new file expires
* "saltBundle" - the actual saltBundle
* "findSecret" - Optional parameter that creates a section B
* "identityBundle" - Optional list of identity bundles
*/
function publicPeerFile(sections, keyPair, domain) {
var sectionBundle = [];
if (!sections.A) {
throw new Error("Section A is required");
}
var A = merge({ $id: 'A' }, sections.A);
sectionBundle.push(bundle('section', A, keyPair.privateKey, {
x509Data: binaryToBase64(asn1.toDer(pki.publicKeyToAsn1(keyPair.publicKey)).getBytes())
}));
function publicPeerFile(args) {
if (!args.privateKey) throw new Error("privateKey is required");
if (!args.publicKey) throw new Error("publicKey is required");
if (!args.domain) throw new Error("domain is required");
if (!args.lifetime) throw new Error("lifetime is required");
if (!args.saltBundle) throw new Error("saltBundle is required");

var now = Math.floor(Date.now() / 1000);
var A = {
$id: 'A',
cipher: 'sha256/aes256',
created: now,
expires: now + args.lifetime,
saltBundle: args.saltBundle
};
var sectionBundle = [bundle('section', A, args.privateKey, {
x509Data: binaryToBase64(asn1.toDer(pki.publicKeyToAsn1(args.publicKey)).getBytes())
})];

var md = sha1.create();
md.start();
md.update('contact:' + sortedStringify(A));
var contactUri = 'peer://' + domain + '/' + md.digest().toHex();
var contact = getContactUri(A, args.domain);

if (sections.B) {
sectionBundle.push(bundle('section', merge({
if (args.findSecret) {
sectionBundle.push(bundle('section', {
$id: 'B',
contact: contactUri
}, sections.B), keyPair.privateKey, {
uri: contactUri
contact: contact,
findSecret: args.findSecret
}, args.privateKey, {
uri: contact
}));
}

if (sections.C) {
sectionBundle.push(bundle('section', merge({
if (args.identityBundle) {
sectionBundle.push(bundle('section', {
$id: 'C',
contact: contactUri
}, sections.C), keyPair.privateKey, {
uri: contactUri
contact: contact,
identities: {
identityBundle: args.identityBundle
}
}, args.privateKey, {
uri: contact
}));
}

return {
// NOTE that you only need the "peer" branch when creating a peer file as JSON.
contact: contact,
peer: {
$version: "1",
sectionBundle: sectionBundle
}
};
}

function privatePeerFile() {
function getContactUri(A, domain) {
var md = sha1.create();
md.start();
md.update(sortedStringify(A));
return 'peer://' + domain + '/' + md.digest().toHex();
}

/**
* @param sections is an object containing A and B objects
*
* "A" contains "cipher", and "salt"
* "B" contains "privateKey", "publicPeerFile", and "data"
*
* @param keyPair is an rsa key pair with publicKey and privateKey properties.
* @fileSecret is the private-peer-file-secret key used to encrypt the sensitive parts of the document
*
*/
/*
function privatePeerFile(contact, sections, keyPair, fileSecret) {
var sectionBundle = [];
if (!sections.A) {
throw new Error('Section A is required');
}
var A = {
$id: 'A',
contact: contact,
sections.A.cipher
}, sections.A);
A.secretProof = hmac('proof:' + sections.A.contact
if (!sections.B) {
throw new Error('Section B is required');
}
}
*/

var now = Math.floor(Date.now() / 1000);
var pub = publicPeerFile({
A: {
cipher: 'sha256/aes256',
created: now,
expires: now + 10845400,
saltBundle: {
real: 'salt',
data: 'goes',
here: true
}
},
B: { findSecret: 'YjAwOWE2YmU4OWNlOTdkY2QxNzY1NDA5MGYy' },
C: { identities: { identityBundle: [ "real", "identities", "go", "here" ] } }
}, pair, "example.com");
lifetime: 10845400, // Number of seconds till the file expires
saltBundle: { real: 'salt', data: 'goes', here: true },
findSecret: 'YjAwOWE2YmU4OWNlOTdkY2QxNzY1NDA5MGYy',
identityBundle: [ "real", "identities", "go", "here" ],
privateKey: pair.privateKey,
publicKey: pair.publicKey,
domain: "example.com"
});

console.log(require('util').inspect(pub, {depth: null, colors: true}));

0 comments on commit d8bfac7

Please sign in to comment.