Skip to content

Commit

Permalink
Add migrate_indy_wallet function (#28)
Browse files Browse the repository at this point in the history
Signed-off-by: conanoc <[email protected]>
  • Loading branch information
conanoc authored Jul 17, 2024
1 parent 01def70 commit 17962a0
Show file tree
Hide file tree
Showing 11 changed files with 105 additions and 13 deletions.
5 changes: 4 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ var package = Package(
.testTarget(
name: "AskarTests",
dependencies: ["Askar"],
path: "swift/Tests/AskarTests"),
path: "swift/Tests/AskarTests",
resources: [
.copy("resources/indy_wallet_sqlite.db")
]),
.binaryTarget(
name: "askar_uniffiFFI",
url: "https://github.com/hyperledger/aries-uniffi-wrappers/releases/download/0.2.0-binary/askar_uniffiFFI.xcframework.zip",
Expand Down
40 changes: 32 additions & 8 deletions askar/Cargo.lock

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

2 changes: 1 addition & 1 deletion askar/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ crate-type = ["staticlib", "cdylib"]
uniffi = { version = "0.25.2", features = ["build", "cli", "tokio"] }

[dependencies]
aries-askar = { git = "https://github.com/hyperledger/aries-askar", tag = "v0.3.1", features = ["logger", "sqlite"], default-features = false }
aries-askar = { git = "https://github.com/hyperledger/aries-askar", tag = "v0.3.2", features = ["logger", "sqlite", "migration"], default-features = false }
env_logger = { version = "0.9" }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
Expand Down
3 changes: 2 additions & 1 deletion askar/build-kotlin-library.sh
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ lipo -create $AARCH64_APPLE_DARWIN_PATH/lib$NAME.a \
$X86_64_APPLE_DARWIN_PATH/lib$NAME.a \
-output $OUT_PATH/macos-native/static/lib$NAME.a

cargo install cross --git https://github.com/cross-rs/cross
# https://github.com/cross-rs/cross/issues/1222
cargo install cross --git https://github.com/cross-rs/cross --rev 35a1e174

# Build for android targets
for target in "${android_targets[@]}"; do
Expand Down
2 changes: 1 addition & 1 deletion askar/src/uffi/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ impl LocalKeyFactory {
alg: AskarKeyAlg,
ephemeral: bool,
) -> Result<Arc<AskarLocalKey>, ErrorCode> {
let key = LocalKey::generate(alg.into(), ephemeral)?;
let key = LocalKey::generate_with_rng(alg.into(), ephemeral)?;
Ok(Arc::new(AskarLocalKey { key }))
}

Expand Down
1 change: 1 addition & 0 deletions askar/src/uffi/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ impl AskarSession {
&name,
&key.key,
metadata.as_deref(),
None,
tags.as_deref(),
expiry_ms,
)
Expand Down
16 changes: 15 additions & 1 deletion askar/src/uffi/store.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::uffi::{error::ErrorCode, scan::AskarScan, session::AskarSession};
use aries_askar::{
PassKey, Store, StoreKeyMethod,
storage::{entry::TagFilter, generate_raw_store_key},
storage::{entry::TagFilter, generate_raw_store_key, migration::IndySdkToAriesAskarMigration},
};
use std::{str::FromStr, sync::Arc};
use tokio::sync::RwLock;
Expand Down Expand Up @@ -74,6 +74,20 @@ impl AskarStoreManager {
let removed = Store::remove(spec_uri.as_str()).await?;
Ok(removed)
}

pub async fn migrate_indy_wallet(
&self,
spec_uri: String,
wallet_name: String,
wallet_key: String,
kdf_level: String,
) -> Result<(), ErrorCode> {
let migrator = IndySdkToAriesAskarMigration::connect(
spec_uri.as_str(), wallet_name.as_str(), wallet_key.as_str(), kdf_level.as_str(),
).await?;
migrator.migrate().await?;
Ok(())
}
}

pub struct AskarStore {
Expand Down
3 changes: 3 additions & 0 deletions kotlin/indy-vdr/src/commonTest/kotlin/TestVDR.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ class TestVDR {

println("\tPool Status: ${pool.getStatus()}")

pool.refresh()
println("\tPool Status after refresh: ${pool.getStatus()}")

val ledger = Ledger()
ffiObjects.add(ledger)

Expand Down
23 changes: 23 additions & 0 deletions swift/Sources/Askar/askar_uniffi.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1795,6 +1795,7 @@ public func FfiConverterTypeAskarStore_lower(_ value: AskarStore) -> UnsafeMutab

public protocol AskarStoreManagerProtocol {
func generateRawStoreKey(seed: String?) throws -> String
func migrateIndyWallet(specUri: String, walletName: String, walletKey: String, kdfLevel: String) async throws
func open(specUri: String, keyMethod: String?, passKey: String?, profile: String?) async throws -> AskarStore
func provision(specUri: String, keyMethod: String?, passKey: String?, profile: String?, recreate: Bool) async throws -> AskarStore
func remove(specUri: String) async throws -> Bool
Expand Down Expand Up @@ -1829,6 +1830,25 @@ public class AskarStoreManager: AskarStoreManagerProtocol {
)
}

public func migrateIndyWallet(specUri: String, walletName: String, walletKey: String, kdfLevel: String) async throws {
return try await uniffiRustCallAsync(
rustFutureFunc: {
uniffi_askar_uniffi_fn_method_askarstoremanager_migrate_indy_wallet(
self.pointer,
FfiConverterString.lower(specUri),
FfiConverterString.lower(walletName),
FfiConverterString.lower(walletKey),
FfiConverterString.lower(kdfLevel)
)
},
pollFunc: ffi_askar_uniffi_rust_future_poll_void,
completeFunc: ffi_askar_uniffi_rust_future_complete_void,
freeFunc: ffi_askar_uniffi_rust_future_free_void,
liftFunc: { $0 },
errorHandler: FfiConverterTypeErrorCode.lift
)
}

public func open(specUri: String, keyMethod: String?, passKey: String?, profile: String?) async throws -> AskarStore {
return try await uniffiRustCallAsync(
rustFutureFunc: {
Expand Down Expand Up @@ -3043,6 +3063,9 @@ private var initializationResult: InitializationResult {
if uniffi_askar_uniffi_checksum_method_askarstoremanager_generate_raw_store_key() != 8565 {
return InitializationResult.apiChecksumMismatch
}
if uniffi_askar_uniffi_checksum_method_askarstoremanager_migrate_indy_wallet() != 51575 {
return InitializationResult.apiChecksumMismatch
}
if uniffi_askar_uniffi_checksum_method_askarstoremanager_open() != 236 {
return InitializationResult.apiChecksumMismatch
}
Expand Down
23 changes: 23 additions & 0 deletions swift/Tests/AskarTests/StoreTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -155,4 +155,27 @@ final class StoreTests: XCTestCase {
key = try await session.fetchKey(name: keyName, forUpdate: false)
XCTAssertNil(key)
}

func testMigration() async throws {
guard let file = Bundle.module.url(forResource: "indy_wallet_sqlite", withExtension: "db") else {
XCTFail("Wallet file not found")
return
}

let copyPath = "/tmp/indy_wallet_sqlite_upgrade.db"
try FileManager.default.copyItem(atPath: file.path, toPath: copyPath)
let key = "GfwU1DC7gEZNs3w41tjBiZYj7BNToDoFEqKY6wZXqs1A"
try await storeManager.migrateIndyWallet(specUri: URI_SCHEMA + copyPath,
walletName: "walletwallet.0",
walletKey: key,
kdfLevel: "RAW")

// Try open the upgraded wallet
let upgraded = try await storeManager.open(specUri: URI_SCHEMA + copyPath,
keyMethod: "raw",
passKey: key,
profile: nil)
try await upgraded.close()
_ = try await storeManager.remove(specUri: URI_SCHEMA + copyPath)
}
}
Binary file not shown.

0 comments on commit 17962a0

Please sign in to comment.