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

Testing #1067

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open

Testing #1067

Show file tree
Hide file tree
Changes from all 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
13 changes: 13 additions & 0 deletions dev-cli/container/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,19 @@ RUN cd /opt/aolibc && make CC="emcc -s WASM=1 -s MEMORY64=1 -s SUPPORT_LONGJMP=1
ENV CC='emcc -s WASM=1'
ENV NM='/emsdk/upstream/bin/llvm-nm'

# VLAD: adding Rust compilation tools (rustup + cargo WASM32 targets)
# RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y
# RUN ~/.cargo/bin/rustup target add wasm32-unknown-unknown
# # VLAD: compiling a custom Rust library to WASM32 target
# RUN cd /opt/vlad_rusty && ~/.cargo/bin/cargo build --target=wasm32-unknown-unknown --release

# VIC: copy the didkit.a file to be built by ao-build-module
COPY ./src/core/didkit.h /opt/didkit.h
COPY ./src/core/didkit.a /opt/didkit.a

# note that the build lib will just include this library in the linking process as a dependancy
# the build module flags libraries with the wrapper, but we will assume the library is linked in main.c instead

###########################################
### Publish, Bundler, Process, Contract ###
###########################################
Expand Down
14 changes: 13 additions & 1 deletion dev-cli/container/src/ao-build-module
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,12 @@ def main():

# Load the config file
config = Config('/src/config.yml')
debug_print('Build config is {}'.format(' '.join(str(config))))


# Determine the language of the source files
language = determine_language()
debug_print('Determined the language to be {}'.format(language))

# Read the main.c file into c_program
c_program = ''
Expand Down Expand Up @@ -93,12 +96,17 @@ def main():
with open('/tmp/compile.c', mode='w') as build:
build.write(c_program)

# copy the generated file to be able to step through it
shutil.copy('/tmp/compile.c', os.path.join(os.getcwd(), './build_src/compile.c'))
# shutil.copy('/tmp/compile.c', os.path.join(os.getcwd(), './build_src/compile.c'))

# Compile the module to a WASM file using emcc
debug_print('Start to compile as WASM')

# Setup the compile command
cmd = ['emcc', '-O3',
'-g2',
'-g4',
'--source-map-base', './build_src',
# '-s', 'MAIN_MODULE', # This is required to load dynamic libraries at runtime
'-s', 'ASYNCIFY=1',
'-s', 'STACK_SIZE=' + str(config.stack_size),
Expand Down Expand Up @@ -126,6 +134,10 @@ def main():
cmd.extend(['-I', quote('/lua-{}-32/src'.format(os.environ.get('LUA_VERSION')))])
cmd.extend([quote('/lua-{}-32/src/liblua.a'.format(os.environ.get('LUA_VERSION')))])

# cmd.extend(['-I', quote('/opt')])
# cmd.extend(['-L/opt/vlad_rusty/target/wasm32-unknown-unknown/release', '-l:libvlad_rusty.a'])


# Link Rust library
if(language == 'rust'):
cmd.extend(['-L/opt/aorustlib', '-l:libaorust.a'])
Expand Down
8 changes: 6 additions & 2 deletions dev-cli/container/src/ao_module_lib/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def mb_to_bytes(megabytes):
'maximum_memory': mb_to_bytes(256)
},
'md': {
'target': 32,
'stack_size': mb_to_bytes(64),
'initial_memory': mb_to_bytes(68),
'maximum_memory': mb_to_bytes(512)
Expand Down Expand Up @@ -46,7 +47,7 @@ class Config():
maximum_memory = 0
extra_compile_args = []
keep_js = False
target = 64
target = 32

def __init__(self, config_file):

Expand Down Expand Up @@ -96,4 +97,7 @@ def get_extra_args(self):
for val in self.extra_compile_args:
args.extend(['{}'.format(val)])
debug_print('Extra compile args: {}'.format(args))
return args
return args

def __str__(self):
return f'preset={self.preset}, stack_size={self.stack_size}, initial_memory={self.initial_memory}, maximum_memory={self.maximum_memory}, extra_compile_args={self.extra_compile_args}, keep_js={self.keep_js}, target={self.target}'
4 changes: 2 additions & 2 deletions dev-cli/container/src/ao_module_lib/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ def shell_exec(*cmd_args):
return proc.stdout.decode('utf-8').strip('\n'), proc.returncode

def debug_print(message):
if IS_DEBUG:
print(message)
# if IS_DEBUG:
print(message)


def __get_output(output, entry_file):
Expand Down
4 changes: 4 additions & 0 deletions dev-cli/container/src/ao_module_lib/libraries.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ def load_libraries(config: Config, definition: Definition, c_program: str, link_
bundle_files += glob.glob('/src/libs/**/*.a', recursive=True)
bundle_files += glob.glob('/src/libs/**/*.o', recursive=True)
bundle_files += glob.glob('/src/libs/**/*.dylib', recursive=True)

bundle_files += glob.glob('/opt/didkit.a', recursive=True)

# Load aolib from /opt/aolibc/
if(config.target == 64):
Expand All @@ -61,6 +63,8 @@ def load_libraries(config: Config, definition: Definition, c_program: str, link_
'/share/lua/**/*.lua', recursive=True)

for bundle in bundle_files:
debug_print('*** Considering bundle: {}'.format(bundle))

if is_lua_source_file(bundle):
debug_print('Lua file found: {}'.format(bundle))
basename = re.sub(LUAROCKS_LOCAL_MODULE_PREFIX_RE, '', bundle)
Expand Down
Binary file added dev-cli/container/src/core/didkit.a
Binary file not shown.
136 changes: 136 additions & 0 deletions dev-cli/container/src/core/didkit.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
#pragma once

// didkit.h - autogenerated by cbindgen

#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>

/**
* Issue a Verifiable Presentation for [DIDAuth](https://w3c-ccg.github.io/vp-request-spec/#did-authentication-request). Input parameters are the holder URI as a C string, and JSON C strings for the linked data proof options and the JWK for signing. On success,
* a newly-issued verifiable presentation is returned as a newly-allocated C string. The
* returned string should be freed using [`didkit_free_string`]. On failure, `NULL` is returned, and the
* error message can be retrieved using [`didkit_error_message`].
*/
const char *didkit_did_auth(const char *holder,
const char *proof_options_json,
const char *key_json);

/**
* Resolve a DID to a DID Document. Arguments are a C string containing the DID to resolve, and a
* C string containing a JSON object for resolution input metadata. The return value on success is
* a newly-allocated C string containing either the resolved DID document or a DID resolution
* result JSON object. On error, `NULL` is returned, and the error can be retrieved using
* [`didkit_error_message`].
*/
const char *didkit_did_resolve(const char *did, const char *input_metadata_json);

/**
* Resolve a DID to a DID Document. Arguments are a C string containing the DID URL to dereference, and a
* C string containing a JSON object for dereferencing input metadata. The return value on success is
* a newly-allocated C string containing either a resolved resource or a DID resolution
* result JSON object. On error, `NULL` is returned, and the error can be retrieved using
* [`didkit_error_message`].
*/
const char *didkit_did_url_dereference(const char *did_url,
const char *input_metadata_json);

/**
* Retrieve a numeric code for the most recent error encountered by a DIDKit C function. If there
* has not been an error, 0 is returned.
*/
int didkit_error_code(void);

/**
* Retrieve a human-readable description of the most recent error encountered by a DIDKit C
* function. The returned string is valid until the next call to a DIDKit function in the current
* thread, and should not be mutated or freed. If there has not been any error, `NULL` is returned.
*/
const char *didkit_error_message(void);

/**
* Free a C string that has been dynamically allocated by DIDKit. This should be used for strings
* returned from most DIDKit C functions, per their respective documentation.
*/
void didkit_free_string(const char *string);

/**
* Get the version of the DIDKit library. Returns a static C string which should not be mutated or
* freed.
*/
const char *didkit_get_version(void);

/**
* Convert a key in JWK format to a did:key DID. Input should be a JWK containing public key
* parameters. Private key parameters in the JWK are ignored. On success, returns a
* newly-allocated C string containing a DID corresponding to the JWK. The returned string must be
* freed
* with [`didkit_free_string`]. On failure, returns `NULL`; the error message can be retrieved
* with [`didkit_error_message`].
*/
const char *didkit_key_to_did(const char *method_pattern, const char *jwk);

/**
* Convert a key to a `did:key` DID URI for use in the `verificationMethod` property of a linked data
* proof. Input should be a C string containing the key as a JWK. The JWK should contain public
* key material; private key parameters are ignored. On success, this function returns a newly-allocated C string containing the `verificationMethod` URI. On failure, `NULL` is returned; the
* error message can be retrieved using [`didkit_error_message`].
*/
const char *didkit_key_to_verification_method(const char *method_pattern,
const char *jwk);

/**
* Generate a new Ed25519 keypair in JWK format. On success, returns a pointer to a
* newly-allocated string containing the JWK. The string must be freed with [`didkit_free_string`]. On
* failure, returns `NULL`; the error message can be retrieved with [`didkit_error_message`].
*/
const char *didkit_vc_generate_ed25519_key(void);

/**
* Issue a Verifiable Credential. Input parameters are JSON C strings for the unsigned credential
* to be issued, the linked data proof options, and the JWK for signing. On success, the
* newly-issued verifiable credential is returned as a newly-allocated C string. The returned
* string should be freed using [`didkit_free_string`]. On failure, `NULL` is returned, and the error
* message can be retrieved using [`didkit_error_message`].
*/
const char *didkit_vc_issue_credential(const char *credential_json,
const char *proof_options_json,
const char *key_json);

/**
* Issue a Verifiable Presentation. Input parameters are JSON C strings for the unsigned
* presentation to be issued, the linked data proof options, and the JWK for signing. On success,
* the newly-issued verifiable presentation is returned as a newly-allocated C string. The
* returned string should be freed using [`didkit_free_string`]. On failure, `NULL` is returned, and the
* error message can be retrieved using [`didkit_error_message`].
*/
const char *didkit_vc_issue_presentation(const char *presentation_json,
const char *proof_options_json,
const char *key_json);

/**
* Verify a Verifiable Credential. Arguments are a C string containing the Verifiable Credential
* to verify, and a C string containing a JSON object for the linked data proof options for
* verification. The return value is a newly-allocated C string containing a JSON object for the
* verification result, or `NULL` in case of certain errors. On successful verification, the
* verification result JSON object contains a "errors" property whose value is an empty array. If
* verification fails, either `NULL` is returned and the error can be retrieved using
* [`didkit_error_message`], or a verification result JSON object is returned with an "errors" array
* containing information about the verification error(s) encountered. A string returned from this
* function should be freed using [`didkit_free_string`].
*/
const char *didkit_vc_verify_credential(const char *credential, const char *proof_options_json);

/**
* Verify a Verifiable Presentation. Arguments are a C string containing the Verifiable
* Presentation to verify, and a C string containing a JSON object for the linked data proof
* options for verification. The return value is a newly-allocated C string containing a JSON
* object for the verification result, or `NULL` in case of certain errors. On successful
* verification, the verification result JSON object contains a "errors" property whose value is
* an empty array. If verification fails, either `NULL` is returned and the error can be retrieved
* using [`didkit_error_message`], or a verification result JSON object is returned with an "errors"
* array containing information about the verification error(s) encountered. A string returned
* from this function should be freed using [`didkit_free_string`].
*/
const char *didkit_vc_verify_presentation(const char *presentation, const char *proof_options_json);
74 changes: 74 additions & 0 deletions dev-cli/container/src/core/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@
#include "lauxlib.h"
#endif

//#include "vlad_rusty_lib.h"
// #include "didkit.h"

int get_version(void);
const char *get_version2(void);

#ifdef __cplusplus
extern "C"
{
Expand All @@ -20,6 +26,65 @@ extern "C"
int boot_lua(lua_State *L);
lua_State *wasm_lua_state = NULL;

int lua_didkit_version(lua_State* L) {
// // Call the Rust function
// const char* result = get_version();

// // Push the result back onto the Lua stack
// lua_pushstring(L, result);

// Call the Rust function
int result = get_version();

// Push the result back onto the Lua stack
lua_pushinteger(L, result);

// Return the number of results
return 1;
}

int lua_didkit_version2(lua_State* L) {
// Call the Rust function
const char* result = get_version2();

// Push the result back onto the Lua stack
lua_pushstring(L, result);

// Return the number of results
return 1;
}

// // Define a wrapper function to call add_two_integers from Lua
// int lua_add_two_integers(lua_State* L) {
// // Get the two integer arguments from the Lua stack
// int a = luaL_checkinteger(L, 1);
// int b = luaL_checkinteger(L, 2);

// // Call the Rust function
// int result = add_two_integers(a, b);

// // Push the result back onto the Lua stack
// lua_pushinteger(L, result);

// // Return the number of results
// return 1;
// }
// // Define a wrapper function to call subtract_two_integers from Lua
// int lua_subtract_two_integers(lua_State* L) {
// // Get the two integer arguments from the Lua stack
// int a = luaL_checkinteger(L, 1);
// int b = luaL_checkinteger(L, 2);

// // Call the Rust function
// int result = subtract_two_integers(a, b);

// // Push the result back onto the Lua stack
// lua_pushinteger(L, result);

// // Return the number of results
// return 1;
// }

// Pre-compiled lua loader program
static const unsigned char program[] = {__LUA_BASE__};
// Pre-compiled entry script which user wrote
Expand Down Expand Up @@ -132,6 +197,7 @@ extern "C"
lua_close(wasm_lua_state);
return 1;
}

// printf("Boot Lua Webassembly!\n");
return 0;
}
Expand All @@ -153,6 +219,14 @@ extern "C"
// This place will be injected by emcc-lua
__INJECT_LUA_FILES__

// VLAD: Register our custom C functions with Lua
// lua_register(wasm_lua_state, "add_two_integers", lua_add_two_integers);
// lua_register(wasm_lua_state, "subtract_two_integers", lua_subtract_two_integers);

// VIC: Register our custom C functions with Lua
lua_register(wasm_lua_state, "didkit_version", lua_didkit_version);
lua_register(wasm_lua_state, "didkit_version2", lua_didkit_version2);

if (docall(L, 1, LUA_MULTRET))
{
const char *errmsg = lua_tostring(L, 1);
Expand Down
7 changes: 7 additions & 0 deletions dev-cli/container/src/core/vlad_rusty/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions dev-cli/container/src/core/vlad_rusty/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "vlad_rusty"
version = "0.1.0"
edition = "2021"

[lib]
crate-type = ["staticlib", "cdylib"]

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
9 changes: 9 additions & 0 deletions dev-cli/container/src/core/vlad_rusty/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#[no_mangle] // Ensures the function name isn't mangled by Rust's name mangling
pub extern "C" fn add_two_integers(a: i32, b: i32) -> i32 {
a + b
}

#[no_mangle]
pub extern "C" fn subtract_two_integers(a: i32, b: i32) -> i32 {
a - b
}
7 changes: 7 additions & 0 deletions dev-cli/container/src/core/vlad_rusty_lib.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#ifndef VLAD_RUSTY_LIB_H
#define VLAD_RUSTY_LIB_H

int add_two_integers(int a, int b);
int subtract_two_integers(int a, int b);

#endif
Loading