Skip to content

Commit

Permalink
built and updated to support updateable conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
glitch003 committed Apr 8, 2022
1 parent 3823b9f commit 28d693c
Show file tree
Hide file tree
Showing 7 changed files with 201 additions and 50 deletions.
36 changes: 35 additions & 1 deletion dist/ceramic.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports._decodeFromB64 = exports._readCeramic = exports._writeCeramic = exports._authenticateCeramic = exports._createCeramic = void 0;
exports._decodeFromB64 = exports._readCeramic = exports._updateCeramic = exports._writeCeramic = exports._authenticateCeramic = exports._createCeramic = void 0;
var http_client_1 = __importDefault(require("@ceramicnetwork/http-client"));
var stream_caip10_link_1 = require("@ceramicnetwork/stream-caip10-link");
var stream_tile_1 = require("@ceramicnetwork/stream-tile");
Expand Down Expand Up @@ -161,6 +161,40 @@ function _writeCeramic(auth, toBeWritten) {
});
}
exports._writeCeramic = _writeCeramic;
function _updateCeramic(auth, streamId, newContent) {
return __awaiter(this, void 0, void 0, function () {
var ceramic, toStore, doc;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (!auth) return [3 /*break*/, 3];
ceramic = auth[1];
toStore = {
encryptedZip: (0, lit_1.encodeb64)(newContent[0]),
symKey: (0, lit_1.encodeb64)(newContent[1]),
accessControlConditions: newContent[2],
chain: newContent[3],
accessControlConditionType: newContent[4],
};
return [4 /*yield*/, stream_tile_1.TileDocument.load(ceramic, streamId.valueOf())];
case 1:
doc = _a.sent();
console.log("$$$kl - loaded previous ceramic data from StreamID: ", streamId.valueOf());
console.log("$$$kl - previous doc: ", doc);
console.log("$$$kl - new access control conditions: ", newContent[1]);
return [4 /*yield*/, doc.update(toStore)];
case 2:
_a.sent();
console.log("$$$kl - new doc: ", doc);
return [2 /*return*/, "updated access conditions stored in Ceramic"];
case 3:
console.error("Failed to authenticate in ceramic WRITE");
return [2 /*return*/, "error"];
}
});
});
}
exports._updateCeramic = _updateCeramic;
/**
* Read to Ceramic. This function takes in an auth and the streamID of the desired data and then sends it to a ceramic node in the proper format getting back a promised string of whatever was stored
*
Expand Down
61 changes: 59 additions & 2 deletions dist/integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ var Integration = /** @class */ (function () {
return [4 /*yield*/, (0, ceramic_1._authenticateCeramic)(this.ceramicPromise)];
case 2:
a = _a.sent();
return [4 /*yield*/, (0, lit_1._encryptWithLit)(a, toEncrypt, accessControlConditions, this.chain, accessControlConditionType)];
return [4 /*yield*/, (0, lit_1._encryptWithLit)(toEncrypt, accessControlConditions, this.chain, accessControlConditionType)];
case 3:
en = _a.sent();
return [4 /*yield*/, (0, ceramic_1._writeCeramic)(a, en)];
Expand Down Expand Up @@ -108,9 +108,11 @@ var Integration = /** @class */ (function () {
return [4 /*yield*/, (0, ceramic_1._authenticateCeramic)(this.ceramicPromise)];
case 1:
a = _a.sent();
console.log("authenticated RnD: ", a);
return [4 /*yield*/, (0, ceramic_1._readCeramic)(a, streamID)];
case 2:
en = _a.sent();
console.log("read from ceramic RnD: ", en);
return [4 /*yield*/, (0, ceramic_1._decodeFromB64)(en)];
case 3:
deco = _a.sent();
Expand All @@ -121,12 +123,67 @@ var Integration = /** @class */ (function () {
return [2 /*return*/, decrypt];
case 5:
error_2 = _a.sent();
return [2 /*return*/, "something went wrong decrypting: ".concat(error_2, " \n StreamID sent: ").concat(streamID)];
console.log("something went wrong decrypting: ".concat(error_2, " \n StreamID sent: ").concat(streamID));
return [2 /*return*/, "FALSE"];
case 6: return [2 /*return*/];
}
});
});
};
/**
* Retrieves a stream and decrypts message then returns to user
*
* @param {String} streamID the streamID of the encrypted data that you want to update the access control conditions for
* @param {Array<Object>} accessControlConditions the access control conditions that govern who is able to decrypt this data. Note that you cannot change the accessControlConditionType using this method, and you must use the same condition type that was used when you ran encryptAndWrite. See the docs here for examples of accessControlConditions: https://developer.litprotocol.com/docs/SDK/accessControlConditionExamples
* @returns {Promise<String>} A promise that resolves to the unencrypted string of what was stored
*/
Integration.prototype.updateAccess = function (streamID, newAccessControlConditions) {
return __awaiter(this, void 0, void 0, function () {
var a, en, deco, result, newContent, result2, error_3;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
_a.trys.push([0, 6, , 7]);
console.log("trying to update permissions for streamID: ", streamID);
return [4 /*yield*/, (0, ceramic_1._authenticateCeramic)(this.ceramicPromise)];
case 1:
a = _a.sent();
console.log("authenticated: ", a);
return [4 /*yield*/, (0, ceramic_1._readCeramic)(a, streamID)];
case 2:
en = _a.sent();
console.log("read from ceramic: ", en);
return [4 /*yield*/, (0, ceramic_1._decodeFromB64)(en)];
case 3:
deco = _a.sent();
console.log("data from ceramic: ", deco);
return [4 /*yield*/, (0, lit_1._saveEncryptionKey)(newAccessControlConditions, deco[1], //encryptedSymmetricKey
this.chain)];
case 4:
result = _a.sent();
console.log("update access result: ", result);
newContent = [
deco[0],
deco[1],
newAccessControlConditions,
deco[3],
deco[4],
];
//save the access conditions back to Ceramic
console.log("saving new ceramic access conditions: ", newContent, newAccessControlConditions);
return [4 /*yield*/, (0, ceramic_1._updateCeramic)(a, streamID, newContent)];
case 5:
result2 = _a.sent();
console.log("update ceramic access conditions: ", streamID, result);
return [2 /*return*/, result2];
case 6:
error_3 = _a.sent();
return [2 /*return*/, "something went wrong encrypting: ".concat(error_3)];
case 7: return [2 /*return*/];
}
});
});
};
return Integration;
}());
exports.Integration = Integration;
48 changes: 44 additions & 4 deletions dist/lit.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
}
};
Object.defineProperty(exports, "__esModule", { value: true });
exports._decryptWithLit = exports._encryptWithLit = exports.decodeb64 = exports.encodeb64 = void 0;
exports._saveEncryptionKey = exports._decryptWithLit = exports._encryptWithLit = exports.decodeb64 = exports.blobToBase64 = exports.encodeb64 = void 0;
// import LitJsSdk from 'lit-js-sdk'
var LitJsSdk = __importStar(require("lit-js-sdk"));
var to_string_1 = require("uint8arrays/to-string");
Expand Down Expand Up @@ -87,6 +87,7 @@ function blobToBase64(blob) {
reader.readAsDataURL(blob);
});
}
exports.blobToBase64 = blobToBase64;
/**
* This function decodes from base 64.
* it's useful for decrypting symkeys and files in ceramic
Expand All @@ -101,11 +102,13 @@ exports.decodeb64 = decodeb64;
* encrypts a message with Lit returns required details
* this obfuscates data such that it can be stored on ceramic without
* non-permissioned eyes seeing what the data is
* @param {blob} auth authentication from wallet
* @param {String} aStringThatYouWishToEncrypt the clear text you'd like encrypted
* @param {Array<Object>} accessControlConditions the access control conditions that govern who is able to decrypt this data. See the docs here for examples: https://developer.litprotocol.com/docs/SDK/accessControlConditionExamples
* @param {String} chain the chain you'd like to use for checking the access control conditions
* @param {String} accessControlConditionType the access control condition type you are using. Pass `accessControlConditions` for traditional access control conditions. This is the default if you don't pass anything. Pass `evmContractConditions` for custom smart contract access control conditions
* @returns {Promise<Array<any>>} returns, in this order: encryptedZipBase64, encryptedSymmetricKeyBase64, accessControlConditions, chain
*/
function _encryptWithLit(auth, aStringThatYouWishToEncrypt, accessControlConditions, chain, accessControlConditionType) {
function _encryptWithLit(aStringThatYouWishToEncrypt, accessControlConditions, chain, accessControlConditionType) {
if (accessControlConditionType === void 0) { accessControlConditionType = "accessControlConditions"; }
return __awaiter(this, void 0, void 0, function () {
var authSig, _a, encryptedZip, symmetricKey, encryptedSymmetricKey, encryptedZipBase64, encryptedSymmetricKeyBase64;
Expand All @@ -125,6 +128,7 @@ function _encryptWithLit(auth, aStringThatYouWishToEncrypt, accessControlConditi
symmetricKey: symmetricKey,
authSig: authSig,
chain: chain,
permanant: false,
})];
case 3:
encryptedSymmetricKey = _b.sent();
Expand All @@ -136,6 +140,7 @@ function _encryptWithLit(auth, aStringThatYouWishToEncrypt, accessControlConditi
symmetricKey: symmetricKey,
authSig: authSig,
chain: chain,
permanant: false,
})];
case 5:
encryptedSymmetricKey = _b.sent();
Expand All @@ -161,7 +166,9 @@ exports._encryptWithLit = _encryptWithLit;
* decrypt encrypted zip and symmetric key using the lit protocol
* @param {Uint8Array} encryptedZip encrypted data that will be converted into a string
* @param {Uint8Array} encryptedSymmKey symmetric key
* @param {Uint8Array} accessControlConditions conditions that determine access
* @param {Array<any>} accessControlConditions conditions that determine access
* @param {String} chain the chain you'd like to use for checking the access control conditions
* @param {String} accessControlConditionType the access control condition type you are using. Pass `accessControlConditions` for traditional access control conditions. This is the default if you don't pass anything. Pass `evmContractConditions` for custom smart contract access control conditions
* @returns {Promise<string>} promise with the decrypted string
*/
function _decryptWithLit(encryptedZip, encryptedSymmKey, accessControlConditions, chain, accessControlConditionType) {
Expand Down Expand Up @@ -214,3 +221,36 @@ function _decryptWithLit(encryptedZip, encryptedSymmKey, accessControlConditions
});
}
exports._decryptWithLit = _decryptWithLit;
// litCeramicIntegration.saveEncryptionKey({
// accessControlConditions: newAccessControlConditions,
// encryptedSymmetricKey,
// authSig,
// chain,
// permanant: false,
// });
function _saveEncryptionKey(newAccessControlConditions, encryptedSymmetricKey, chain) {
return __awaiter(this, void 0, void 0, function () {
var authSig, newEncryptedSymmetricKey;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, LitJsSdk.checkAndSignAuthMessage({
chain: chain,
})];
case 1:
authSig = _a.sent();
return [4 /*yield*/, window.litNodeClient.saveEncryptionKey({
accessControlConditions: newAccessControlConditions,
encryptedSymmetricKey: encryptedSymmetricKey,
authSig: authSig,
chain: chain,
permanant: false,
})];
case 2:
newEncryptedSymmetricKey = _a.sent();
console.log("updated the access control condition");
return [2 /*return*/, newEncryptedSymmetricKey];
}
});
});
}
exports._saveEncryptionKey = _saveEncryptionKey;
40 changes: 29 additions & 11 deletions documentation/integration.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
* [Parameters][2]
* [readAndDecrypt][3]
* [Parameters][4]
* [updateAccess][5]
* [Parameters][6]

## encryptAndWrite

Expand All @@ -14,21 +16,33 @@ whatever the module user inputs (as long as it is a string for now)

### Parameters

* `toEncrypt` **[String][5]** what the module user wants to encrypt and store on ceramic
* `accessControlConditions` **[Array][6]<[Object][7]>** the access control conditions that govern who is able to decrypt this data. See the docs here for examples: [https://developer.litprotocol.com/docs/SDK/accessControlConditionExamples][8]
* `accessControlConditionType` **[String][5]** the access control condition type you are using. Pass `accessControlConditions` for traditional access control conditions. This is the default if you don't pass anything. Pass `evmContractConditions` for custom smart contract access control conditions (optional, default `"accessControlConditions"`)
* `toEncrypt` **[String][7]** what the module user wants to encrypt and store on ceramic
* `accessControlConditions` **[Array][8]<[Object][9]>** the access control conditions that govern who is able to decrypt this data. See the docs here for examples: [https://developer.litprotocol.com/docs/SDK/accessControlConditionExamples][10]
* `accessControlConditionType` **[String][7]** the access control condition type you are using. Pass `accessControlConditions` for traditional access control conditions. This is the default if you don't pass anything. Pass `evmContractConditions` for custom smart contract access control conditions (optional, default `"accessControlConditions"`)

Returns **[Promise][9]<[String][5]>** A promise that resolves to a streamID for the encrypted data that's been stored
Returns **[Promise][11]<[String][7]>** A promise that resolves to a streamID for the encrypted data that's been stored

## readAndDecrypt

Retrieves a stream and decrypts message then returns to user

### Parameters

* `streamID` **[String][5]** the streamID of the encrypted data the user wants to access
* `streamID` **[String][7]** the streamID of the encrypted data the user wants to access

Returns **[Promise][9]<[String][5]>** A promise that resolves to the unencrypted string of what was stored
Returns **[Promise][11]<[String][7]>** A promise that resolves to the unencrypted string of what was stored

## updateAccess

Retrieves a stream and decrypts message then returns to user

### Parameters

* `streamID` **[String][7]** the streamID of the encrypted data that you want to update the access control conditions for
* `newAccessControlConditions` **[Array][8]<[Object][9]>**
* `accessControlConditions` **[Array][8]<[Object][9]>** the access control conditions that govern who is able to decrypt this data. Note that you cannot change the accessControlConditionType using this method, and you must use the same condition type that was used when you ran encryptAndWrite. See the docs here for examples of accessControlConditions: [https://developer.litprotocol.com/docs/SDK/accessControlConditionExamples][10]

Returns **[Promise][11]<[String][7]>** A promise that resolves to the unencrypted string of what was stored

[1]: #encryptandwrite

Expand All @@ -38,12 +52,16 @@ Returns **[Promise][9]<[String][5]>** A promise that resolves to the unencrypted

[4]: #parameters-1

[5]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String
[5]: #updateaccess

[6]: #parameters-2

[7]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String

[6]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array
[8]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array

[7]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object
[9]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object

[8]: https://developer.litprotocol.com/docs/SDK/accessControlConditionExamples
[10]: https://developer.litprotocol.com/docs/SDK/accessControlConditionExamples

[9]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise
[11]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise
Loading

0 comments on commit 28d693c

Please sign in to comment.