Skip to content

Commit

Permalink
remove libsodium dependency for oydid-js
Browse files Browse the repository at this point in the history
  • Loading branch information
fabianekc committed Feb 18, 2024
1 parent b096fd1 commit bb4a194
Show file tree
Hide file tree
Showing 19 changed files with 380 additions and 148 deletions.
24 changes: 2 additions & 22 deletions oydid-js/dist/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,14 @@ Object.defineProperty(exports, "__esModule", { value: true });
exports.multi_hash = exports.multi_encode = exports.DEFAULT_ENCODING = exports.DEFAULT_DIGEST = void 0;
exports.DEFAULT_DIGEST = "sha2-256";
exports.DEFAULT_ENCODING = "base58btc";
const libsodium_wrappers_1 = require("libsodium-wrappers");
const multi_encode = (message, options) => __awaiter(void 0, void 0, void 0, function* () {
const o = Object.assign({ encode: exports.DEFAULT_ENCODING, digest: exports.DEFAULT_DIGEST, simulate: false }, options);
const method = o.encode;
return "asdf";
return "string";
});
exports.multi_encode = multi_encode;
const multi_hash = (message, options) => __awaiter(void 0, void 0, void 0, function* () {
const opt = Object.assign({ encode: exports.DEFAULT_ENCODING, digest: exports.DEFAULT_DIGEST, simulate: false }, options);
yield libsodium_wrappers_1.ready; // Wait for libsodium to be ready
// Convert the string to Uint8Array
const data = new TextEncoder().encode(message);
const method = opt.digest;
var digest = "";
switch (method) {
case "sha2-256":
case "blake2b-64":
// Make sure the desired hash length is valid
if (libsodium_wrappers_1.crypto_generichash_BYTES_MAX < 64) {
throw new Error('Hash length is too large for BLAKE2b with this version of libsodium.');
}
const digest = (0, libsodium_wrappers_1.crypto_generichash)(64, data);
break;
default:
throw new Error("unsupported digest: '" + method.toString() + "'");
break;
}
const encoded = yield (0, exports.multi_encode)(digest, opt);
return encoded;
return "string";
});
exports.multi_hash = multi_hash;
59 changes: 47 additions & 12 deletions oydid-js/dist/oydid.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,13 @@ export interface RegistrarResponse {
*/
keys: didKey[];
}
/**
* create a new DID
* @param content payload in the new DID Document
* @param options optional parameters
* @returns DID and private keys
*/
export declare const create: (content?: any, options?: Partial<ReadOptions>) => Promise<Did>;
/**
* resolve DID to DID Document
* @param did DID string (in format did:oyd:123)
Expand All @@ -102,14 +109,50 @@ export interface RegistrarResponse {
*/
export declare const read: (did: string, options?: Partial<ReadOptions>) => Promise<DidDocument>;
/**
* create a new DID
* @param content payload in the new DID Document
* update DID Document for existing DID
* @param did DID string (in format did:oyd:123)
* @param content payload of the updated DID Document
* @param options optional parameters
* @returns DID and private keys
*/
export declare const create: (content?: any, options?: Partial<ReadOptions>) => Promise<Did>;
export declare const update: (did: string, content: any, options?: Partial<ReadOptions>) => Promise<Did>;
/**
* deactivate DID
* @param did DID string (in format did:oyd:123)
* @param options optional parameters
* @returns DID
*/
export declare const deactivate: (did: string, options?: Partial<ReadOptions>) => Promise<Did>;
/**
* encrypt a message using libsodium
* @param payload to encrypt
* @param option parameters with public key for encryption
* @returns cipher and nonce of encrypted message
*/
export declare const encrypt: (payload: string, options: Partial<ReadOptions>) => Promise<any>;
/**
* decrypt a libsodium encrypted message
* @param message cipher and nonce of encrypted message
* @param key private key to decrypt message
* @param options optional parameters
* @returns decrypted message
*/
export declare const decrypt: (message: CipherMessage, key: string, options?: Partial<ReadOptions>) => Promise<string>;
/**
* sign a message
* @param payload to sign
* @param option parameters with private key for signing
* @returns signature of payload
*/
export declare const sign: (payload: string, options: Partial<ReadOptions>) => Promise<string>;
/**
* verify signature for a message
* @param hexKey hexadecimal encoded object
* @param options optional parameters to specify preferred target encoding
* @returns base58btc Multiformat encoded object
*/
export declare const verify: (message: string, signature: string, options?: Partial<ReadOptions>) => Promise<boolean>;
/**
*
* @param did DID string (in format did:oyd:123)
* @param key private key necessary for signing during authorization process
* @param regapi_url RegAPI URL (only protocol and host, e.g. http://host.com)
Expand All @@ -123,11 +166,3 @@ export declare const didAuth: (did: string, key: string, regapi_url: string) =>
* @returns base58btc Multiformat encoded object
*/
export declare const hexToMulti: (hexKey: string, options?: Partial<ReadOptions>) => Promise<string>;
/**
* decrypt a libsodium encrypted message
* @param message cipher and nonce of encrypted message
* @param key private key to decrypt message
* @param options optional parameters
* @returns decrypted message
*/
export declare const decrypt: (message: CipherMessage, key: string, options?: Partial<ReadOptions>) => Promise<string>;
147 changes: 113 additions & 34 deletions oydid-js/dist/oydid.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,28 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.decrypt = exports.hexToMulti = exports.didAuth = exports.create = exports.read = exports.DEFAULT_ENCODING = exports.DEFAULT_DIGEST = void 0;
exports.hexToMulti = exports.didAuth = exports.verify = exports.sign = exports.decrypt = exports.encrypt = exports.deactivate = exports.update = exports.read = exports.create = exports.DEFAULT_ENCODING = exports.DEFAULT_DIGEST = void 0;
const did_jwt_1 = __importDefault(require("did-jwt"));
const axios_1 = __importDefault(require("axios"));
const libsodium_wrappers_sumo_1 = require("libsodium-wrappers-sumo");
const base58_1 = require("multiformats/bases/base58");
// import bs58 from 'bs58';
exports.DEFAULT_DIGEST = "sha2-256";
exports.DEFAULT_ENCODING = "base58btc";
/**
* create a new DID
* @param content payload in the new DID Document
* @param options optional parameters
* @returns DID and private keys
*/
const create = (content, options) => __awaiter(void 0, void 0, void 0, function* () {
const url = "https://oydid-registrar.data-container.net/1.0/createIdentifier";
const result = yield axios_1.default.post(url, {});
return {
id: result.data.did,
docKey: result.data.keys[0].privateKeyHex,
revKey: result.data.keys[1].privateKeyHex
};
});
exports.create = create;
/**
* resolve DID to DID Document
* @param did DID string (in format did:oyd:123)
Expand All @@ -28,36 +43,118 @@ exports.DEFAULT_ENCODING = "base58btc";
const read = (did, options) => __awaiter(void 0, void 0, void 0, function* () {
const o = Object.assign({ encode: exports.DEFAULT_ENCODING, digest: exports.DEFAULT_DIGEST, simulate: false }, options);
if (!did) {
throw new Error("missing DID1");
throw new Error("missing DID");
}
return { doc: { "hello": "world" }, key: "asdf:qwer", log: "asdf" };
});
exports.read = read;
/**
* create a new DID
* @param content payload in the new DID Document
* update DID Document for existing DID
* @param did DID string (in format did:oyd:123)
* @param content payload of the updated DID Document
* @param options optional parameters
* @returns DID and private keys
*/
const create = (content, options) => __awaiter(void 0, void 0, void 0, function* () {
const url = "https://oydid-registrar.data-container.net/1.0/createIdentifier";
const result = yield axios_1.default.post(url, {});
const update = (did, content, options) => __awaiter(void 0, void 0, void 0, function* () {
const o = Object.assign({ encode: exports.DEFAULT_ENCODING, digest: exports.DEFAULT_DIGEST, simulate: false }, options);
if (!did) {
throw new Error("missing DID");
}
return {
id: result.data.did,
docKey: result.data.keys[0].privateKeyHex,
revKey: result.data.keys[1].privateKeyHex
id: did,
docKey: "",
revKey: ""
};
});
exports.create = create;
exports.update = update;
/**
* deactivate DID
* @param did DID string (in format did:oyd:123)
* @param options optional parameters
* @returns DID
*/
const deactivate = (did, options) => __awaiter(void 0, void 0, void 0, function* () {
const o = Object.assign({ encode: exports.DEFAULT_ENCODING, digest: exports.DEFAULT_DIGEST, simulate: false }, options);
if (!did) {
throw new Error("missing DID");
}
return {
id: did,
docKey: "",
revKey: ""
};
});
exports.deactivate = deactivate;
/**
* encrypt a message using libsodium
* @param payload to encrypt
* @param option parameters with public key for encryption
* @returns cipher and nonce of encrypted message
*/
const encrypt = (payload, options) => __awaiter(void 0, void 0, void 0, function* () {
if (!payload) {
throw new Error("missing payload");
}
const url = "https://oydid.ownyourdata.eu/helper/encrypt";
const body = { message: payload, key: "" };
const result = yield axios_1.default.post(url, body);
return {
cipher: result.data.cipher,
nonce: result.data.nonce
};
});
exports.encrypt = encrypt;
/**
* decrypt a libsodium encrypted message
* @param message cipher and nonce of encrypted message
* @param key private key to decrypt message
* @param options optional parameters
* @returns decrypted message
*/
const decrypt = (message, key, options) => __awaiter(void 0, void 0, void 0, function* () {
const url = "https://oydid.ownyourdata.eu/helper/decrypt";
const body = { message: message, key: key };
const result = yield axios_1.default.post(url, body);
return JSON.stringify(result.data, null, 0);
});
exports.decrypt = decrypt;
/**
* sign a message
* @param payload to sign
* @param option parameters with private key for signing
* @returns signature of payload
*/
const sign = (payload, options) => __awaiter(void 0, void 0, void 0, function* () {
if (!payload) {
throw new Error("missing payload");
}
return "string";
});
exports.sign = sign;
/**
* verify signature for a message
* @param hexKey hexadecimal encoded object
* @param options optional parameters to specify preferred target encoding
* @returns base58btc Multiformat encoded object
*/
const verify = (message, signature, options) => __awaiter(void 0, void 0, void 0, function* () {
if (!message) {
throw new Error("missing message");
}
if (!signature) {
throw new Error("missing signature");
}
return true;
});
exports.verify = verify;
/**
*
* @param did DID string (in format did:oyd:123)
* @param key private key necessary for signing during authorization process
* @param regapi_url RegAPI URL (only protocol and host, e.g. http://host.com)
* @returns OAuth 2.0 Bearer Token
*/
const didAuth = (did, key, regapi_url) => __awaiter(void 0, void 0, void 0, function* () {
const url = regapi_url + "/did_auth";
const url = regapi_url + (regapi_url.endsWith('/') ? "" : "/") + "did_auth";
const body = { did: did, key: key };
const result = yield axios_1.default.post(url, body);
return result.data.access_token;
Expand All @@ -70,26 +167,8 @@ exports.didAuth = didAuth;
* @returns base58btc Multiformat encoded object
*/
const hexToMulti = (hexKey, options) => __awaiter(void 0, void 0, void 0, function* () {
yield libsodium_wrappers_sumo_1.ready;
const keyBytes = (0, libsodium_wrappers_sumo_1.from_hex)(hexKey);
const keyBytes = did_jwt_1.default.hexToBytes(hexKey);
const multiformatKey = base58_1.base58btc.encode(keyBytes);
return multiformatKey;
});
exports.hexToMulti = hexToMulti;
/**
* decrypt a libsodium encrypted message
* @param message cipher and nonce of encrypted message
* @param key private key to decrypt message
* @param options optional parameters
* @returns decrypted message
*/
const decrypt = (message, key, options) => __awaiter(void 0, void 0, void 0, function* () {
yield libsodium_wrappers_sumo_1.ready;
const privateKeyBytes = base58_1.base58btc.decode(key);
const privateKey = privateKeyBytes.slice(privateKeyBytes.length - 32);
const authHash = (0, libsodium_wrappers_sumo_1.crypto_hash_sha256)((0, libsodium_wrappers_sumo_1.from_string)('auth'));
const authKey = (0, libsodium_wrappers_sumo_1.crypto_scalarmult_base)(authHash);
const decryptedMessageBytes = (0, libsodium_wrappers_sumo_1.crypto_box_open_easy)((0, libsodium_wrappers_sumo_1.from_hex)(message.value), (0, libsodium_wrappers_sumo_1.from_hex)(message.nonce), authKey, privateKey);
return (0, libsodium_wrappers_sumo_1.to_string)(decryptedMessageBytes);
});
exports.decrypt = decrypt;
Loading

0 comments on commit bb4a194

Please sign in to comment.