跳到内容
Tauri

要塞 (Stronghold)

使用 IOTA Stronghold 秘密管理引擎存储秘密和密钥。

此插件需要 Rust 版本至少为 **1.77.2**

平台 级别 备注
Windows
Linux
macOS
Android
iOS

安装 Stronghold 插件即可开始使用。

使用你的项目包管理器添加依赖项

npm run tauri add stronghold

该插件必须使用密码哈希函数进行初始化,该函数接受密码字符串并必须返回从中派生出的 32 字节哈希。

Stronghold 插件提供了一个使用 argon2 算法的默认哈希函数。

src-tauri/src/lib.rs
use tauri::Manager;
pub fn run() {
tauri::Builder::default()
.setup(|app| {
let salt_path = app
.path()
.app_local_data_dir()
.expect("could not resolve app local data path")
.join("salt.txt");
app.handle().plugin(tauri_plugin_stronghold::Builder::with_argon2(&salt_path).build())?;
Ok(())
})
.run(tauri::generate_context!())
.expect("error while running tauri application");
}

或者,您可以使用 tauri_plugin_stronghold::Builder::new 构造函数提供您自己的哈希算法。

src-tauri/src/lib.rs
pub fn run() {
tauri::Builder::default()
.plugin(
tauri_plugin_stronghold::Builder::new(|password| {
// Hash the password here with e.g. argon2, blake2b or any other secure algorithm
// Here is an example implementation using the `rust-argon2` crate for hashing the password
use argon2::{hash_raw, Config, Variant, Version};
let config = Config {
lanes: 4,
mem_cost: 10_000,
time_cost: 10,
variant: Variant::Argon2id,
version: Version::Version13,
..Default::default()
};
let salt = "your-salt".as_bytes();
let key = hash_raw(password.as_ref(), salt, &config).expect("failed to hash password");
key.to_vec()
})
.build(),
)
.run(tauri::generate_context!())
.expect("error while running tauri application");
}

Stronghold 插件在 JavaScript 中可用。

import { Client, Stronghold } from '@tauri-apps/plugin-stronghold';
// when using `"withGlobalTauri": true`, you may use
// const { Client, Stronghold } = window.__TAURI__.stronghold;
import { appDataDir } from '@tauri-apps/api/path';
// when using `"withGlobalTauri": true`, you may use
// const { appDataDir } = window.__TAURI__.path;
const initStronghold = async () => {
const vaultPath = `${await appDataDir()}/vault.hold`;
const vaultPassword = 'vault password';
const stronghold = await Stronghold.load(vaultPath, vaultPassword);
let client: Client;
const clientName = 'name your client';
try {
client = await stronghold.loadClient(clientName);
} catch {
client = await stronghold.createClient(clientName);
}
return {
stronghold,
client,
};
};
// Insert a record to the store
async function insertRecord(store: any, key: string, value: string) {
const data = Array.from(new TextEncoder().encode(value));
await store.insert(key, data);
}
// Read a record from store
async function getRecord(store: any, key: string): Promise<string> {
const data = await store.get(key);
return new TextDecoder().decode(new Uint8Array(data));
}
const { stronghold, client } = await initStronghold();
const store = client.getStore();
const key = 'my_key';
// Insert a record to the store
insertRecord(store, key, 'secret value');
// Read a record from store
const value = await getRecord(store, key);
console.log(value); // 'secret value'
// Save your updates
await stronghold.save();
// Remove a record from store
await store.remove(key);

默认情况下,所有潜在危险的插件命令和范围都被阻止,无法访问。您必须修改 capabilities 配置中的权限才能启用这些功能。

有关更多信息,请参阅功能概述,并参阅分步指南以使用插件权限。

src-tauri/capabilities/default.json
{
...,
"permissions": [
"stronghold:default",
]
}

默认权限

此权限集配置了 Stronghold 插件可用的操作类型。

已授予权限

所有非破坏性操作默认启用。

此默认权限集包括以下内容

  • 允许创建客户端
  • 允许获取存储记录
  • 允许初始化
  • 允许执行程序
  • 允许加载客户端
  • 允许保存秘密
  • 允许保存存储记录
  • 允许保存

权限表

标识符 描述

stronghold:允许创建客户端

启用 create_client 命令,不带任何预配置范围。

stronghold:拒绝创建客户端

拒绝 create_client 命令,不带任何预配置范围。

stronghold:允许销毁

启用 destroy 命令,不带任何预配置范围。

stronghold:拒绝销毁

拒绝 destroy 命令,不带任何预配置范围。

stronghold:允许执行程序

启用 execute_procedure 命令,不带任何预配置范围。

stronghold:拒绝执行程序

拒绝 execute_procedure 命令,不带任何预配置范围。

stronghold:允许获取存储记录

启用 get_store_record 命令,不带任何预配置范围。

stronghold:拒绝获取存储记录

拒绝 get_store_record 命令,不带任何预配置范围。

stronghold:允许初始化

启用 initialize 命令,不带任何预配置范围。

stronghold:拒绝初始化

拒绝 initialize 命令,不带任何预配置范围。

stronghold:允许加载客户端

启用 load_client 命令,不带任何预配置范围。

stronghold:拒绝加载客户端

拒绝 load_client 命令,不带任何预配置范围。

stronghold:允许删除秘密

启用 remove_secret 命令,不带任何预配置范围。

stronghold:拒绝删除秘密

拒绝 remove_secret 命令,不带任何预配置范围。

stronghold:允许删除存储记录

启用 remove_store_record 命令,不带任何预配置范围。

stronghold:拒绝删除存储记录

拒绝 remove_store_record 命令,不带任何预配置范围。

stronghold:允许保存

启用保存命令,不带任何预配置范围。

stronghold:拒绝保存

禁用保存命令,不带任何预配置范围。

stronghold:允许保存秘密

启用 save_secret 命令,不带任何预配置范围。

stronghold:拒绝保存秘密

拒绝 save_secret 命令,不带任何预配置范围。

stronghold:允许保存存储记录

启用 save_store_record 命令,不带任何预配置范围。

stronghold:拒绝保存存储记录

拒绝 save_store_record 命令,不带任何预配置范围。


© 2025 Tauri 贡献者。CC-BY / MIT