Skip to content

Commit

Permalink
Merge pull request #120 from blu3beri/js/add-migration
Browse files Browse the repository at this point in the history
feat(js): added migration to wrapper
  • Loading branch information
berendsliedrecht authored Mar 13, 2023
2 parents e3ad1a8 + 2bc5fa7 commit ef7a71b
Show file tree
Hide file tree
Showing 23 changed files with 381 additions and 342 deletions.
129 changes: 8 additions & 121 deletions include/cbindgen.toml
Original file line number Diff line number Diff line change
@@ -1,131 +1,18 @@
# This is a template cbindgen.toml file with all of the default values.
# Some values are commented out because their absence is the real default.
#
# See https://github.com/eqrion/cbindgen/blob/master/docs.md#cbindgentoml
# for detailed documentation of every option here.


language = "C"
cpp_compat = true


############## Options for Wrapping the Contents of the Header #################

# header = "/* Text to put at the beginning of the generated file. Probably a license. */"
# trailer = "/* Text to put at the end of the generated file */"
# include_guard = "my_bindings_h"
# pragma_once = true
# autogen_warning = "/* Warning, this file is autogenerated by cbindgen. Don't modify this manually. */"
pragma_once = true
autogen_warning = "/* Warning, this file is autogenerated by cbindgen. Don't modify this manually. */"
include_version = false
# namespace = "my_namespace"
namespaces = []
using_namespaces = []
sys_includes = []
includes = []
no_includes = false
after_includes = ""


############################ Code Style Options ################################

braces = "SameLine"
line_length = 100
tab_width = 2
documentation = true
documentation_style = "auto"
# documentation_length = "full"
line_endings = "LF" # also "CR", "CRLF", "Native"


############################# Codegen Options ##################################

style = "both"
sort_by = "Name" # default for `fn.sort_by` and `const.sort_by`
usize_is_size_t = true


[defines]
# "target_os = freebsd" = "DEFINE_FREEBSD"
# "feature = serde" = "DEFINE_SERDE"
"feature = rich_schema" = "DEFINE_RICH_SCHEMA"
"test" = "DEFINE_TEST"

[export]
include = []
exclude = []
# prefix = "CAPI_"
item_types = []
renaming_overrides_prefixing = false


[export.rename]


[export.body]


[export.mangle]


[fn]
rename_args = "None"
# must_use = "MUST_USE_FUNC"
# no_return = "NO_RETURN"
# prefix = "START_FUNC"
# postfix = "END_FUNC"
args = "auto"
sort_by = "Name"


[struct]
rename_fields = "None"
# must_use = "MUST_USE_STRUCT"
derive_constructor = false
derive_eq = false
derive_neq = false
derive_lt = false
derive_lte = false
derive_gt = false
derive_gte = false


[enum]
rename_variants = "None"
# must_use = "MUST_USE_ENUM"
add_sentinel = false
prefix_with_name = false
derive_helper_methods = false
derive_const_casts = false
derive_mut_casts = false
# cast_assert_name = "ASSERT"
derive_tagged_enum_destructor = false
derive_tagged_enum_copy_constructor = false
enum_class = true
private_default_tagged_enum_constructor = false


[const]
allow_static_const = true
allow_constexpr = false
sort_by = "Name"
after_includes = """

[macro_expansion]
bitflags = false


############## Options for How Your Rust library Should Be Parsed ##############
typedef size_t ScanHandle;
typedef size_t StoreHandle;
typedef size_t SessionHandle;
"""

[parse]
parse_deps = true
include = ["ffi-support"]
exclude = []
clean = false
extra_bindings = []


[parse.expand]
crates = []
all_features = false
default_features = true
features = []
include = ["ffi-support", "ffi"]
29 changes: 28 additions & 1 deletion include/libaries_askar.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
#pragma once

/* Warning, this file is autogenerated by cbindgen. Don't modify this manually. */

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

typedef size_t ScanHandle;
typedef size_t StoreHandle;
typedef size_t SessionHandle;

#define PAGE_SIZE 32

enum ErrorCode
#ifdef __cplusplus
Expand Down Expand Up @@ -385,6 +391,25 @@ ErrorCode askar_key_wrap_key(LocalKeyHandle handle,
struct ByteBuffer nonce,
struct EncryptedBuffer *out);

/**
* Migrate an sqlite wallet from an indy-sdk structure to an aries-askar structure.
* It is important to note that this does not do any post-processing. If the record values, tags,
* names, etc. have changed, it must be processed manually afterwards. This script does the following:
*
* 1. Create and rename the required tables
* 2. Fetch the indy key from the wallet
* 3. Create a new configuration
* 4. Initialize a profile
* 5. Update the items from the indy-sdk
* 6. Clean up (drop tables and add a version of "1")
*/
ErrorCode askar_migrate_indy_sdk(FfiStr spec_uri,
FfiStr wallet_name,
FfiStr wallet_key,
FfiStr kdf_level,
void (*cb)(CallbackId cb_id, ErrorCode err),
CallbackId cb_id);

ErrorCode askar_scan_free(ScanHandle handle);

ErrorCode askar_scan_next(ScanHandle handle,
Expand Down Expand Up @@ -540,6 +565,8 @@ ErrorCode askar_store_remove_profile(StoreHandle handle,
void (*cb)(CallbackId cb_id, ErrorCode err, int8_t removed),
CallbackId cb_id);

void askar_terminate(void);

char *askar_version(void);

#ifdef __cplusplus
Expand Down
1 change: 1 addition & 0 deletions src/backend/db_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use crate::{
},
};

/// cbindgen:ignore
pub const PAGE_SIZE: usize = 32;

pub type Expiry = chrono::DateTime<chrono::Utc>;
Expand Down
3 changes: 3 additions & 0 deletions wrappers/javascript/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ android/keystores/debug.keystore
aries-askar-react-native/ios/Frameworks
aries-askar-react-native/android/libs

# Test wallet
aries-askar-nodejs/tests/indy_wallet_sqlite_upgraded.db

# Example app
react-native-example

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ import type {
EncryptedBuffer,
AriesAskarErrorObject,
AeadParamsOptions,
MigrateIndySdkOptions,
} from '@hyperledger/aries-askar-shared'

import {
Expand Down Expand Up @@ -954,4 +955,11 @@ export class NodeJSAriesAskar implements AriesAskar {

return handleInvalidNullResponse(response)
}

public async migrateIndySdk(options: MigrateIndySdkOptions): Promise<void> {
const { specUri, kdfLevel, walletKey, walletName } = serializeArguments(options)
await this.promisify((cb, cbId) =>
nativeAriesAskar.askar_migrate_indy_sdk(specUri, walletName, walletKey, kdfLevel, cb, cbId)
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -197,4 +197,9 @@ export const nativeBindings = {
askar_store_rekey: [FFI_ERROR_CODE, [FFI_STORE_HANDLE, FFI_STRING, FFI_STRING, FFI_CALLBACK_PTR, FFI_CALLBACK_ID]],
askar_store_remove: [FFI_ERROR_CODE, [FFI_STRING, FFI_CALLBACK_PTR, FFI_CALLBACK_ID]],
askar_store_remove_profile: [FFI_ERROR_CODE, [FFI_STORE_HANDLE, FFI_STRING, FFI_CALLBACK_PTR, FFI_CALLBACK_ID]],

askar_migrate_indy_sdk: [
FFI_ERROR_CODE,
[FFI_STRING, FFI_STRING, FFI_STRING, FFI_STRING, FFI_CALLBACK_PTR, FFI_CALLBACK_ID],
],
} as const
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { CryptoBox, Key, KeyAlgs } from '@hyperledger/aries-askar-shared'

import { setup } from './utils'

describe('CryptoBox', () => {
beforeAll(() => setup())
beforeAll(() => {
require('@hyperledger/aries-askar-nodejs')
})

test('seal', () => {
const x25519Key = Key.generate(KeyAlgs.X25519)
Expand Down
Binary file not shown.
6 changes: 4 additions & 2 deletions wrappers/javascript/aries-askar-nodejs/tests/joseEcdh.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { Ecdh1PU, EcdhEs, Jwk, Key, KeyAlgs } from '@hyperledger/aries-askar-shared'

import { base64url, setup } from './utils'
import { base64url } from './utils'

describe('jose ecdh', () => {
beforeAll(() => setup())
beforeAll(() => {
require('@hyperledger/aries-askar-nodejs')
})

test('ecdh es direct', () => {
const bobKey = Key.generate(KeyAlgs.EcSecp256r1)
Expand Down
7 changes: 4 additions & 3 deletions wrappers/javascript/aries-askar-nodejs/tests/keys.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Key, KeyAlgs, KeyMethod } from '@hyperledger/aries-askar-shared'

import { setup } from './utils'

describe('keys', () => {
beforeAll(() => setup())
beforeAll(() => {
require('@hyperledger/aries-askar-nodejs')
})

test('aes cbc hmac', () => {
const key = Key.generate(KeyAlgs.AesA128CbcHs256)
expect(key.algorithm).toStrictEqual(KeyAlgs.AesA128CbcHs256)
Expand Down
50 changes: 50 additions & 0 deletions wrappers/javascript/aries-askar-nodejs/tests/migration.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { ariesAskar, Migration } from '@hyperledger/aries-askar-shared'
import fs from 'fs'
import path from 'path'

const DB_TEMPLATE_PATH = path.join(__dirname, 'indy_wallet_sqlite.db')
const DB_UPGRADE_PATH = path.join(__dirname, 'indy_wallet_sqlite_upgraded.db')

describe('migration', () => {
beforeAll(() => {
require('@hyperledger/aries-askar-nodejs')
})

beforeEach(() => {
const tplPaths = [DB_TEMPLATE_PATH, `${DB_TEMPLATE_PATH}-shm`, `${DB_TEMPLATE_PATH}-wal`]

const updPaths = [DB_UPGRADE_PATH, `${DB_UPGRADE_PATH}-shm`, `${DB_UPGRADE_PATH}-wal`]

for (let i = 0; i <= 3; i++) {
const tplPath = tplPaths[i]
const updPath = updPaths[i]

if (fs.existsSync(tplPath)) {
fs.copyFileSync(tplPath, updPath)
} else {
fs.existsSync(updPath) && fs.rmSync(updPath)
}
}
})

test('migrate', async () => {
await expect(
Migration.migrate({
specUri: DB_UPGRADE_PATH,
kdfLevel: 'RAW',
walletName: 'walletwallet.0',
walletKey: 'GfwU1DC7gEZNs3w41tjBiZYj7BNToDoFEqKY6wZXqs1A',
})
).resolves.toBeUndefined()

// Double migrate should not work
await expect(
Migration.migrate({
specUri: DB_UPGRADE_PATH,
kdfLevel: 'RAW',
walletName: 'walletwallet.0',
walletKey: 'GfwU1DC7gEZNs3w41tjBiZYj7BNToDoFEqKY6wZXqs1A',
})
).rejects.toThrowError('Database is already migrated')
})
})
7 changes: 5 additions & 2 deletions wrappers/javascript/aries-askar-nodejs/tests/store.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { Store, StoreKeyMethod, Key, KeyAlgs, AriesAskarError } from '@hyperledger/aries-askar-shared'
import { promises } from 'fs'

import { firstEntry, getRawKey, secondEntry, setup, setupWallet, testStoreUri } from './utils'
import { firstEntry, getRawKey, secondEntry, setupWallet, testStoreUri } from './utils'

describe('Store and Session', () => {
let store: Store

beforeAll(() => {
require('@hyperledger/aries-askar-nodejs')
})

beforeEach(async () => {
setup()
store = await setupWallet()
})

Expand Down
23 changes: 1 addition & 22 deletions wrappers/javascript/aries-askar-nodejs/tests/utils/initialize.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,9 @@
import '@hyperledger/aries-askar-nodejs'
import { ariesAskar, LogLevel, Store, StoreKeyMethod } from '@hyperledger/aries-askar-shared'
import { Store, StoreKeyMethod } from '@hyperledger/aries-askar-shared'

export const getRawKey = () => Store.generateRawKey(Buffer.from('00000000000000000000000000000My1'))
export const testStoreUri = process.env.URI || 'sqlite://:memory:'

let fnCounter = 0
const fnOnce = (fn: () => void) => {
if (!fnCounter) {
fn()
fnCounter++
}
}

export const setup = () => {
fnOnce(
() =>
process.env.LOG &&
ariesAskar.setCustomLogger({
logger: () => {
/* TODO */
},
logLevel: LogLevel.Trace,
})
)
}

export const setupWallet = async () => {
const key = getRawKey()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ FunctionMap AriesAskarTurboModuleHostObject::functionMapping(jsi::Runtime &rt) {
fMap.insert(std::make_tuple("keyEntryListLoadLocal",
&ariesAskar::keyEntryListLoadLocal));

fMap.insert(std::make_tuple("migrateIndySdk", &ariesAskar::migrateIndySdk));

return fMap;
}

Expand Down
Loading

0 comments on commit ef7a71b

Please sign in to comment.