Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate wasm-themis to TypeScript #792

Merged
merged 22 commits into from
Apr 19, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/wrappers/themis/wasm/emscripten/exports.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
["_malloc","dynamicAlloc"]
232 changes: 232 additions & 0 deletions src/wrappers/themis/wasm/src/context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,232 @@
import { ThemisErrorCode } from "./themis_error";
import { KeyKind } from "./secure_keygen";

export interface YourOwnEmscriptenModule extends EmscriptenModule {
writeArrayToMemory: typeof writeArrayToMemory;
_memset: (buffer: number, offer: number, length: number) => void;
setValue: typeof setValue;
getValue: typeof getValue;

_secure_comparator_create(): number;

_secure_comparator_destroy(ptr: number): ThemisErrorCode;

_secure_comparator_append_secret(
comparatorPtr: number,
secret_ptr: number,
secret_length: number
): ThemisErrorCode;

_secure_comparator_proceed_compare(
comparatorPtr: number,
request_ptr: number,
request_length: number,
reply_ptr: number | null,
reply_length_ptr: number
): ThemisErrorCode;

_secure_comparator_begin_compare(
comparatorPtr: number,
message_ptr: number | null,
message_length_ptr: number
): ThemisErrorCode;

_themis_gen_sym_key(
key_ptr: number | null,
key_len_ptr: number
): ThemisErrorCode;

_themis_gen_ec_key_pair(
private_ptr: number | null,
private_len_ptr: number,
public_ptr: number | null,
public_len_ptr: number
): ThemisErrorCode;

_themis_get_asym_key_kind(buffer_ptr: number, buffer_len: number): KeyKind;

_themis_is_valid_asym_key(
buffer_ptr: number,
buffer_len: number
): ThemisErrorCode;

_themis_secure_cell_encrypt_context_imprint(
master_key_ptr: number,
master_key_length: number,
message_ptr: number,
message_length: number,
context_ptr: number,
context_length: number,
result_ptr: number | null,
result_length_ptr: number
): ThemisErrorCode;

_themis_secure_cell_decrypt_context_imprint(
master_key_ptr: number,
master_key_length: number,
message_ptr: number,
message_length: number,
context_ptr: number,
context_length: number,
result_ptr: number | null,
result_length_ptr: number
): ThemisErrorCode;

_themis_secure_cell_encrypt_seal(
master_key_ptr: number,
master_key_length: number,
context_ptr: number,
context_length: number,
message_ptr: number,
message_length: number,
result_ptr: number | null,
result_length_ptr: number
): ThemisErrorCode;

_themis_secure_cell_decrypt_seal(
master_key_ptr: number,
master_key_length: number,
context_ptr: number,
context_length: number,
message_ptr: number,
message_length: number,
result_ptr: number | null,
result_length_ptr: number
): ThemisErrorCode;

_themis_secure_cell_encrypt_seal_with_passphrase(
master_key_ptr: number,
master_key_length: number,
context_ptr: number,
context_length: number,
message_ptr: number,
message_length: number,
result_ptr: number | null,
result_length_ptr: number
): ThemisErrorCode;

_themis_secure_cell_decrypt_seal_with_passphrase(
master_key_ptr: number,
master_key_length: number,
context_ptr: number,
context_length: number,
message_ptr: number,
message_length: number,
result_ptr: number | null,
result_length_ptr: number
): ThemisErrorCode;

_themis_secure_cell_encrypt_token_protect(
master_key_ptr: number,
master_key_length: number,
context_ptr: number,
context_length: number,
message_ptr: number,
message_length: number,
token_ptr: number | null,
token_length_ptr: number,
result_ptr: number | null,
result_length_ptr: number
): ThemisErrorCode;

_themis_secure_cell_decrypt_token_protect(
master_key_ptr: number,
master_key_length: number,
context_ptr: number,
context_length: number,
message_ptr: number,
message_length: number,
token_ptr: number,
token_length_ptr: number,
result_ptr: number | null,
result_length_ptr: number
): ThemisErrorCode;

_themis_secure_message_encrypt(
private_key_ptr: number,
privateKey_length: number,
public_key_ptr: number,
publicKey_length: number,
message_ptr: number,
message_length: number,
result_ptr: number | null,
result_length_ptr: number
): ThemisErrorCode;

_themis_secure_message_decrypt(
private_key_ptr: number,
privateKey_length: number,
public_key_ptr: number,
publicKey_length: number,
message_ptr: number,
message_length: number,
result_ptr: number | null,
result_length_ptr: number
): ThemisErrorCode;

_themis_secure_message_sign(
private_key_ptr: number,
privateKey_length: number,
message_ptr: number,
message_length: number,
result_ptr: number | null,
result_length_ptr: number
): ThemisErrorCode;

_themis_secure_message_verify(
public_key_ptr: number,
public_key_length: number,
message_ptr: number,
message_length: number,
result_ptr: number | null,
result_length_ptr: number
): ThemisErrorCode;

_secure_session_create(
session_id_ptr: number,
sessionID_length: number,
private_key_ptr: number,
privateKey_length: number,
callbacks_ptr: number
): number;

_secure_session_destroy(sessionPtr: number): ThemisErrorCode;

_secure_session_is_established(sessionPtr: ThemisErrorCode): number;

_secure_comparator_get_result(comparatorPtr: number): ThemisErrorCode;

_secure_session_generate_connect_request(
sessionPtr: number,
request_ptr: number | null,
request_length_ptr: number
): ThemisErrorCode;

_secure_session_unwrap(
sessionPtr: number,
message_ptr: number,
message_length: number,
reply_ptr: number | null,
reply_length_ptr: number
): ThemisErrorCode;

_secure_session_wrap(
sessionPtr: number,
message_ptr: number,
message_length: number,
wrapped_ptr: number | null,
wrapped_length_ptr: number
): ThemisErrorCode;

allocate: (
slab: number[] | ArrayBufferView | number | ArrayBuffer,
allocator: number,
ptr?: number
) => number;
ALLOC_STACK: typeof ALLOC_STACK;
addFunction: typeof addFunction;
}

const context: { libthemis?: YourOwnEmscriptenModule } = {};

export default context;
45 changes: 0 additions & 45 deletions src/wrappers/themis/wasm/src/index.js

This file was deleted.

41 changes: 41 additions & 0 deletions src/wrappers/themis/wasm/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright (c) 2019 Cossack Labs Limited
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import context, { YourOwnEmscriptenModule } from "./context";
import libthemisFn from "./libthemis.js";

export { SecureCellSeal } from "./secure_cell_seal";
export { SecureCellTokenProtect } from "./secure_cell_token_protect";
export { SecureCellContextImprint } from "./secure_cell_context_imprint";
export { ThemisError, ThemisErrorCode } from "./themis_error";
export {
SecureMessageSign,
SecureMessage,
SecureMessageVerify,
} from "./secure_message";
export { SecureSession } from "./secure_session";
export { KeyPair, PrivateKey, PublicKey, SymmetricKey } from "./secure_keygen";
export { SecureComparator } from "./secure_comparator";

export const initialize = async (wasmPath: string) => {
const libthemis = await libthemisFn({
onRuntimeInitialized: function () {},
locateFile: function () {
return wasmPath;
},
});
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regarding the formatting change from "no semicolons and 4-space indents with single quotes" to "use semicolons with 2-space indents and double quotes". Most of the codebase uses 4-space indents, unless there is a strong preference for a particular style in the language (e.g., tabs in Go, 2-space indents in Ruby). Is there such thing in TypeScript ecosystem as a whole?

I don't really mind the style changing, given that all these lines are still going to change anyway with added typing and strict checks. But it would be nice to make it less of a cowboy and more organized. It would be cool to throw in code formatter to ensure that all newly added code is consistent with the new style. (Not in this PR. But maybe some time later.)

(Admittedly, existing JavaScript code is a mess right now. JsThemis tests, for example, use 2, 4, 8-space indents and a bunch of tabs in different places.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, I already throught about throwing in prettier.

Pretter works across IDEs and is the defacto standard for formating. I add this to the todo list.


context.libthemis = libthemis as YourOwnEmscriptenModule;
return libthemis;
};
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@
* Themis Secure Cell.
*/

module.exports.SecureCellSeal = require('./secure_cell_seal.js')
module.exports.SecureCellTokenProtect = require('./secure_cell_token_protect.js')
module.exports.SecureCellContextImprint = require('./secure_cell_context_imprint.js')
export { SecureCellSeal } from "./secure_cell_seal";
export { SecureCellTokenProtect } from "./secure_cell_token_protect";
export { SecureCellContextImprint } from "./secure_cell_context_imprint";
Loading