Skip to content

Commit d7c9629

Browse files
committed
feat: add copy profile support
Signed-off-by: Ariel Gentile <gentilester@gmail.com>
1 parent dfb133c commit d7c9629

File tree

10 files changed

+132
-16
lines changed

10 files changed

+132
-16
lines changed

packages/askar-nodejs/src/NodeJSAskar.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ import type {
6565
SetCustomLoggerOptions,
6666
SetMaxLogLevelOptions,
6767
StoreCloseOptions,
68+
StoreCopyProfileOptions,
6869
StoreCopyToOptions,
6970
StoreCreateProfileOptions,
7071
StoreGenerateRawKeyOptions,
@@ -1089,11 +1090,22 @@ export class NodeJSAskar implements Askar {
10891090
)
10901091
}
10911092

1092-
public async storeRenameProfile(options: StoreRenameProfileOptions): Promise<number> {
1093-
const { storeHandle, fromName, toName } = serializeArguments(options)
1093+
public async storeRenameProfile(options: StoreRenameProfileOptions): Promise<number> {
1094+
const { storeHandle, fromProfile, toProfile } = serializeArguments(options)
10941095

10951096
const response = await this.promisifyWithResponse<number>(
1096-
(cb, cbId) => this.nativeAskar.askar_store_rename_profile(storeHandle, fromName, toName, cb, cbId),
1097+
(cb, cbId) => this.nativeAskar.askar_store_rename_profile(storeHandle, fromProfile, toProfile, cb, cbId),
1098+
FFI_INT8
1099+
)
1100+
1101+
return handleInvalidNullResponse(response)
1102+
}
1103+
1104+
public async storeCopyProfile(options: StoreCopyProfileOptions): Promise<number> {
1105+
const { fromHandle, toHandle, fromProfile, toProfile } = serializeArguments(options)
1106+
1107+
const response = await this.promisifyWithResponse<number>(
1108+
(cb, cbId) => this.nativeAskar.askar_store_copy_profile(fromHandle, toHandle, fromProfile, toProfile, cb, cbId),
10971109
FFI_INT8
10981110
)
10991111

packages/askar-nodejs/src/library/bindings.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,14 @@ export const nativeBindings = {
231231
askar_store_remove: [FFI_ERROR_CODE, [FFI_STRING, FFI_CALLBACK_PTR, FFI_CALLBACK_ID]],
232232
askar_store_remove_profile: [FFI_ERROR_CODE, [FFI_STORE_HANDLE, FFI_STRING, FFI_CALLBACK_PTR, FFI_CALLBACK_ID]],
233233
askar_store_set_default_profile: [FFI_ERROR_CODE, [FFI_STORE_HANDLE, FFI_STRING, FFI_CALLBACK_PTR, FFI_CALLBACK_ID]],
234-
askar_store_rename_profile: [FFI_ERROR_CODE, [FFI_STORE_HANDLE, FFI_STRING, FFI_STRING, FFI_CALLBACK_PTR, FFI_CALLBACK_ID]],
234+
askar_store_rename_profile: [
235+
FFI_ERROR_CODE,
236+
[FFI_STORE_HANDLE, FFI_STRING, FFI_STRING, FFI_CALLBACK_PTR, FFI_CALLBACK_ID],
237+
],
238+
askar_store_copy_profile: [
239+
FFI_ERROR_CODE,
240+
[FFI_STORE_HANDLE, FFI_STORE_HANDLE, FFI_STRING, FFI_STRING, FFI_CALLBACK_PTR, FFI_CALLBACK_ID],
241+
],
235242

236243
askar_migrate_indy_sdk: [
237244
FFI_ERROR_CODE,
0 Bytes
Binary file not shown.

packages/askar-nodejs/tests/store.test.ts

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -308,15 +308,45 @@ describe('Store and Session', () => {
308308

309309
strictEqual(await session7.count(firstEntry), 0)
310310

311-
ok(await store.renameProfile({ fromName: profile, toName: 'newProfileName' }))
311+
await session7.insert(firstEntry)
312+
313+
strictEqual(await session7.count(firstEntry), 1)
314+
315+
await session7.close()
316+
317+
ok(await store.renameProfile({ fromProfile: profile, toProfile: 'newProfileName' }))
312318

313319
ok((await store.listProfiles()).includes('newProfileName'))
314320

315321
ok(!(await store.listProfiles()).includes(profile))
316322

317-
await session7.close()
318-
})
323+
const session8 = await store.session('newProfileName').open()
324+
325+
strictEqual(await session8.count(firstEntry), 1)
319326

327+
await session8.close()
328+
329+
const destinationStore = await Store.provision({
330+
uri: 'sqlite://:memory:',
331+
keyMethod: new StoreKeyMethod(KdfMethod.Raw),
332+
passKey: getRawKey(),
333+
recreate: true,
334+
})
335+
336+
ok(
337+
await store.copyProfile({
338+
toStore: destinationStore,
339+
fromProfile: 'newProfileName',
340+
toProfile: 'newerProfileName',
341+
})
342+
)
343+
344+
const session9 = await destinationStore.session('newerProfileName').open()
345+
346+
strictEqual(await session9.count(firstEntry), 1)
347+
348+
await session9.close()
349+
})
320350

321351
test('Copy', async () => {
322352
const key = getRawKey()
@@ -330,5 +360,4 @@ describe('Store and Session', () => {
330360
})
331361
)
332362
})
333-
334363
})

packages/askar-react-native/cpp/askar.cpp

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -288,15 +288,31 @@ jsi::Value storeSetDefaultProfile(jsi::Runtime &rt, jsi::Object options) {
288288

289289
jsi::Value storeRenameProfile(jsi::Runtime &rt, jsi::Object options) {
290290
auto storeHandle = jsiToValue<int64_t>(rt, options, "storeHandle");
291-
auto fromName = jsiToValue<std::string>(rt, options, "fromName");
292-
auto toName = jsiToValue<std::string>(rt, options, "toName");
291+
auto fromProfile = jsiToValue<std::string>(rt, options, "fromProfile");
292+
auto toProfile = jsiToValue<std::string>(rt, options, "toProfile");
293293

294294
jsi::Function cb = options.getPropertyAsFunction(rt, "cb");
295295
State *state = new State(&cb);
296296
state->rt = &rt;
297297

298298
ErrorCode code = askar_store_rename_profile(
299-
storeHandle, fromName.c_str(), toName.c_str(), callbackWithResponse, CallbackId(state));
299+
storeHandle, fromProfile.c_str(), toProfile.c_str(), callbackWithResponse, CallbackId(state));
300+
301+
return createReturnValue(rt, code, nullptr);
302+
}
303+
304+
jsi::Value storeCopyProfile(jsi::Runtime &rt, jsi::Object options) {
305+
auto fromHandle = jsiToValue<int64_t>(rt, options, "fromHandle");
306+
auto toHandle = jsiToValue<int64_t>(rt, options, "toHandle");
307+
auto fromProfile = jsiToValue<std::string>(rt, options, "fromProfile");
308+
auto toProfile = jsiToValue<std::string>(rt, options, "toProfile");
309+
310+
jsi::Function cb = options.getPropertyAsFunction(rt, "cb");
311+
State *state = new State(&cb);
312+
state->rt = &rt;
313+
314+
ErrorCode code = askar_store_copy_profile(
315+
fromHandle, toHandle, fromProfile.c_str(), toProfile.c_str(), callbackWithResponse, CallbackId(state));
300316

301317
return createReturnValue(rt, code, nullptr);
302318
}

packages/askar-react-native/cpp/include/libaries_askar.h

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ ErrorCode askar_key_wrap_key(LocalKeyHandle handle,
410410
struct EncryptedBuffer *out);
411411

412412
/**
413-
* Migrate an sqlite wallet from an indy-sdk structure to an aries-askar structure.
413+
* Migrate an sqlite wallet from an indy-sdk structure to an Askar structure.
414414
* It is important to note that this does not do any post-processing. If the record values, tags,
415415
* names, etc. have changed, it must be processed manually afterwards. This script does the following:
416416
*
@@ -560,6 +560,13 @@ ErrorCode askar_store_copy(StoreHandle handle,
560560
void (*cb)(CallbackId cb_id, ErrorCode err, StoreHandle handle),
561561
CallbackId cb_id);
562562

563+
ErrorCode askar_store_copy_profile(StoreHandle from_handle,
564+
StoreHandle to_handle,
565+
FfiStr from_profile,
566+
FfiStr to_profile,
567+
void (*cb)(CallbackId cb_id, ErrorCode err),
568+
CallbackId cb_id);
569+
563570
ErrorCode askar_store_create_profile(StoreHandle handle,
564571
FfiStr profile,
565572
void (*cb)(CallbackId cb_id,
@@ -615,6 +622,12 @@ ErrorCode askar_store_remove_profile(StoreHandle handle,
615622
void (*cb)(CallbackId cb_id, ErrorCode err, int8_t removed),
616623
CallbackId cb_id);
617624

625+
ErrorCode askar_store_rename_profile(StoreHandle handle,
626+
FfiStr from_profile,
627+
FfiStr to_profile,
628+
void (*cb)(CallbackId cb_id, ErrorCode err, int8_t renamed),
629+
CallbackId cb_id);
630+
618631
ErrorCode askar_store_set_default_profile(StoreHandle handle,
619632
FfiStr profile,
620633
void (*cb)(CallbackId cb_id, ErrorCode err),

packages/askar-react-native/src/NativeBindings.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,10 @@ export interface NativeBindings {
171171

172172
storeRemoveProfile(options: unknown): ReturnObject<never>
173173

174+
storeRenameProfile(options: unknown): ReturnObject<never>
175+
176+
storeCopyProfile(options: unknown): ReturnObject<never>
177+
174178
storeSetDefaultProfile(options: unknown): ReturnObject<never>
175179

176180
migrateIndySdk(options: unknown): ReturnObject<never>

packages/askar-react-native/src/ReactNativeAskar.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ import type {
6161
SessionUpdateKeyOptions,
6262
SessionUpdateOptions,
6363
StoreCloseOptions,
64+
StoreCopyProfileOptions,
6465
StoreCopyToOptions,
6566
StoreCreateProfileOptions,
6667
StoreGenerateRawKeyOptions,
@@ -72,6 +73,7 @@ import type {
7273
StoreRekeyOptions,
7374
StoreRemoveOptions,
7475
StoreRemoveProfileOptions,
76+
StoreRenameProfileOptions,
7577
StoreSetDefaultProfileOptions,
7678
} from '@openwallet-foundation/askar-shared'
7779
import type { NativeBindings } from './NativeBindings'
@@ -726,6 +728,24 @@ export class ReactNativeAskar implements Askar {
726728
return handleInvalidNullResponse(response)
727729
}
728730

731+
public async storeRenameProfile(options: StoreRenameProfileOptions): Promise<number> {
732+
const serializedOptions = serializeArguments(options)
733+
const response = await this.promisifyWithResponse<number>((cb) =>
734+
this.handleError(this.askar.storeRenameProfile({ cb, ...serializedOptions }))
735+
)
736+
737+
return handleInvalidNullResponse(response)
738+
}
739+
740+
public async storeCopyProfile(options: StoreCopyProfileOptions): Promise<number> {
741+
const serializedOptions = serializeArguments(options)
742+
const response = await this.promisifyWithResponse<number>((cb) =>
743+
this.handleError(this.askar.storeCopyProfile({ cb, ...serializedOptions }))
744+
)
745+
746+
return handleInvalidNullResponse(response)
747+
}
748+
729749
public async storeSetDefaultProfile(options: StoreSetDefaultProfileOptions): Promise<void> {
730750
const serializedOptions = serializeArguments(options)
731751
const response = await this.promisifyWithResponse((cb) =>

packages/askar-shared/src/askar/Askar.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -337,8 +337,14 @@ export type StoreSetDefaultProfileOptions = {
337337
}
338338
export type StoreRenameProfileOptions = {
339339
storeHandle: StoreHandle
340-
fromName: string
341-
toName: string
340+
fromProfile: string
341+
toProfile: string
342+
}
343+
export type StoreCopyProfileOptions = {
344+
fromHandle: StoreHandle
345+
toHandle: StoreHandle
346+
fromProfile: string
347+
toProfile: string
342348
}
343349

344350
export type MigrateIndySdkOptions = {
@@ -432,6 +438,7 @@ export type Askar = {
432438
storeRekey(options: StoreRekeyOptions): Promise<void>
433439
storeRemove(options: StoreRemoveOptions): Promise<number>
434440
storeRenameProfile(options: StoreRenameProfileOptions): Promise<number>
441+
storeCopyProfile(options: StoreCopyProfileOptions): Promise<number>
435442
storeRemoveProfile(options: StoreRemoveProfileOptions): Promise<number>
436443
storeSetDefaultProfile(options: StoreSetDefaultProfileOptions): Promise<void>
437444

packages/askar-shared/src/store/Store.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,16 @@ export class Store {
4444
return askar.storeListProfiles({ storeHandle: this.handle })
4545
}
4646

47-
public async renameProfile({ fromName, toName }: { fromName: string, toName: string } ) {
48-
return await askar.storeRenameProfile({ fromName, toName, storeHandle: this.handle })
47+
public async renameProfile({ fromProfile, toProfile }: { fromProfile: string; toProfile: string }) {
48+
return await askar.storeRenameProfile({ fromProfile, toProfile, storeHandle: this.handle })
49+
}
50+
51+
public async copyProfile({
52+
toStore,
53+
fromProfile,
54+
toProfile,
55+
}: { toStore: Store; fromProfile: string; toProfile: string }) {
56+
return await askar.storeCopyProfile({ fromProfile, toProfile, fromHandle: this.handle, toHandle: toStore.handle })
4957
}
5058

5159
public async removeProfile(name: string) {

0 commit comments

Comments
 (0)