Create new user and write indexedDB

This commit is contained in:
Sosthene00 2024-03-29 12:56:05 +01:00
parent 8cd7a8f361
commit 98e94799ef
5 changed files with 65 additions and 78 deletions

View File

@ -62,7 +62,7 @@ impl Into<JsValue> for ApiError {
#[derive(Tsify, Serialize, Deserialize)]
#[tsify(into_wasm_abi)]
#[allow(non_camel_case_types)]
pub struct GenerateCreateUserReturn {
pub struct create_user_return {
pub user: User,
pub output_list_vec: Vec<OutputList>,
}
@ -128,7 +128,8 @@ pub fn create_user(
password: String,
label: Option<String>,
birthday: u32,
) -> ApiResult<GenerateCreateUserReturn> {
process: String
) -> ApiResult<create_user_return> {
let mut output_list: Vec<OutputList> = Vec::new();
//recover
let sp_wallet_recover = generate_sp_wallet(label.clone(), birthday, true)?;
@ -172,9 +173,9 @@ pub fn create_user(
let user_keys = UserKeys::new(recover_keys, revoke_keys, main_keys);
let user = User::new(user_keys, password)?;
let user = User::new(user_keys, password, process)?;
let generate_user = GenerateCreateUserReturn {
let generate_user = create_user_return {
user,
output_list_vec: output_list,
};

View File

@ -97,18 +97,19 @@ impl UserKeys {
#[derive(Debug, Serialize, Deserialize, Clone, Tsify)]
#[tsify(into_wasm_abi, from_wasm_abi)]
pub struct User {
pre_id: String,
process: String,
recover_data: Vec<u8>,
revoke_data: Option<Vec<u8>>,
sharding: Sharding,
pre_id: String,
recovered_spend_key: Option<String>,
recovered_scan_key: Option<String>,
// recovered_spend_key: Option<String>,
}
impl User {
pub fn new(
user_keys: UserKeys,
user_password: String,
process: String
) -> Result<Self> {
let mut rng = thread_rng();
@ -116,6 +117,7 @@ impl User {
// We just take the 2 revoke keys
let revoke_scan_key = user_keys.get_keys(user_keys.revoke_keys.clone()).scan_key;
let revoke_spend_key = user_keys.get_keys(user_keys.revoke_keys.clone()).spend_key;
let mut revoke_data = Vec::with_capacity(64);
revoke_data.extend_from_slice(revoke_scan_key.as_ref());
revoke_data.extend_from_slice(revoke_spend_key.as_ref());
@ -205,12 +207,12 @@ impl User {
//Receive List Items (PCD)
Ok(User {
pre_id: pre_id.to_string(),
process,
recover_data,
revoke_data: Some(revoke_data),
sharding,
pre_id: pre_id.to_string(),
recovered_spend_key: None,
recovered_scan_key: None,
// recovered_spend_key: None,
})
}

View File

@ -4,11 +4,11 @@ class Database {
private dbName: string = '4nk';
private dbVersion: number = 1;
private storeDefinitions = {
SpClient: {
name: "sp_client",
options: {},
indices: []
},
// SpClient: {
// name: "sp_client",
// options: {},
// indices: []
// },
SpOutputs: {
name: "sp_outputs",
options: {'autoIncrement': true},
@ -22,8 +22,14 @@ class Database {
},
AnkUser: {
name: "user",
options: {},
indices: []
options: {'keyPath': 'pre_id'},
indices: [{
name: 'by_process',
keyPath: 'process',
options: {
'unique': false
}
}]
},
AnkSession: {
name: "session",

View File

@ -5,20 +5,7 @@ document.addEventListener('DOMContentLoaded', async () => {
try {
const services = await Services.getInstance();
if ((await services.isNewUser())) {
services.displayCreateId();
}
else {
services.displayRecover()
}
let sp_wallet_return = services.new_sp_client("default", 0, true);
const indexedDB = await IndexedDB.getInstance();
const db = indexedDB.getDb();
await indexedDB.writeObject(db, indexedDB.getStoreList().SpClient, sp_wallet_return.sp_client_json, "default");
await indexedDB.writeObject(db, indexedDB.getStoreList().SpOutputs, sp_wallet_return.sp_outputs_json, null);
await services.displayCreateId();
} catch (error) {
console.error(error);
}

View File

@ -1,11 +1,11 @@
import { generate_sp_wallet_return, User } from '../dist/pkg/sdk_client';
import { create_user_return, User } from '../dist/pkg/sdk_client';
import IndexedDB from './database'
import Processstore from './store/processstore';
class Services {
private static instance: Services;
private sdkClient: any;
private static CURRENT_PROCESS = "currentprocess";
private current_process: string | null = null;
// Private constructor to prevent direct instantiation from outside
private constructor() {}
@ -25,28 +25,24 @@ class Services {
this.sdkClient.setup();
}
public new_sp_client(label: string, current_tip: number, is_testnet: boolean): generate_sp_wallet_return {
return this.sdkClient.generate_sp_wallet(label, current_tip, is_testnet);
}
// public async getSpAddressDefaultClient(): Promise<string | null> {
// try {
// const indexedDB = await IndexedDB.getInstance();
// const db = indexedDB.getDb();
// const spClient = await indexedDB.getObject<string>(db, indexedDB.getStoreList().SpClient, "default");
public async getSpAddressDefaultClient(): Promise<string | null> {
try {
const indexedDB = await IndexedDB.getInstance();
const db = indexedDB.getDb();
const spClient = await indexedDB.getObject<string>(db, indexedDB.getStoreList().SpClient, "default");
// if (spClient) {
// return this.sdkClient.get_receiving_address(spClient);
// } else {
// console.error("SP client not found");
// return null;
// }
// } catch (error) {
// console.error("Failed to retrieve object or get sp address:", error);
// return null;
// }
if (spClient) {
return this.sdkClient.get_receiving_address(spClient);
} else {
console.error("SP client not found");
return null;
}
} catch (error) {
console.error("Failed to retrieve object or get sp address:", error);
return null;
}
}
// }
public async isNewUser(): Promise<boolean> {
let isNew = false;
@ -65,7 +61,7 @@ class Services {
public async displayCreateId(): Promise<void> {
Services.instance.injectHtml(Services.instance.get_html_create_id());
Services.instance.attachSubmitListener("form4nk", Services.instance.createId);
Services.instance.attachSubmitListener("form4nk", (event) => this.createId(event));
Services.instance.attachClickListener("displayrecover", Services.instance.displayRecover);
Services.instance.displayProcess(await Services.instance.getAllProcess());
}
@ -86,25 +82,22 @@ class Services {
}
const password = passwordElement.value;
const process = processElement.value;
console.log("JS password: " + password + " process: " + process);
this.current_process = processElement.value;
console.log("JS password: " + password + " process: " + this.current_process);
// To comment if test
if (!Services.instance.isPasswordValid(password)) return;
// if (!Services.instance.isPasswordValid(password)) return;
const image_to_recover = Services.instance.getImage("assets/4nk_image.png");
const image_to_revoke = Services.instance.getImage("assets/revoke.jpeg");
let label = null;
let birthday = 50000;
const user: User = this.sdkClient.create_user(password, image_to_recover, image_to_revoke, label, birthday);
const user: create_user_return = this.sdkClient.create_user(password, label, birthday, this.current_process);
try {
const indexedDb = await IndexedDB.getInstance();
const db = indexedDb.getDb();
await indexedDb.writeObject(db, indexedDb.getStoreList().AnkUser, user, process);
console.log("JS User added");
await indexedDb.writeObject(db, indexedDb.getStoreList().AnkUser, user.user, null);
// console.log("JS User added");
await indexedDb.writeObject(db, indexedDb.getStoreList().AnkSession, process, Services.CURRENT_PROCESS);
console.log("JS Sessionstore added currentprocess");
await indexedDb.writeObject(db, indexedDb.getStoreList().SpOutputs, user.output_list_vec, null);
} catch (error) {
console.error("Failed to write user object :", error);
}
@ -201,8 +194,6 @@ class Services {
}
public async displayUpdateAnId() {
let currentProcess = await this.getCurrentProcess();
let body = "";
let style = "";
let script = "";
@ -210,7 +201,7 @@ class Services {
const indexedDB = await IndexedDB.getInstance();
const db = indexedDB.getDb();
try {
let processObject = await indexedDB.getObject<Processstore>(db, indexedDB.getStoreList().AnkProcess, currentProcess);
let processObject = await indexedDB.getObject<Processstore>(db, indexedDB.getStoreList().AnkProcess, this.current_process!);
body = processObject.html;
style = processObject.style;
script = processObject.script;
@ -356,17 +347,17 @@ class Services {
container.innerHTML = html;
}
public async getCurrentProcess(): Promise<string> {
let currentProcess = "";
try {
const indexedDB = await IndexedDB.getInstance();
const db = indexedDB.getDb();
currentProcess = await indexedDB.getObject<string>(db, indexedDB.getStoreList().AnkSession, Services.CURRENT_PROCESS);
} catch (error) {
console.error("Failed to retrieve currentprocess object :", error);
}
return currentProcess;
}
// public async getCurrentProcess(): Promise<string> {
// let currentProcess = "";
// try {
// const indexedDB = await IndexedDB.getInstance();
// const db = indexedDB.getDb();
// currentProcess = await indexedDB.getObject<string>(db, indexedDB.getStoreList().AnkSession, Services.CURRENT_PROCESS);
// } catch (error) {
// console.error("Failed to retrieve currentprocess object :", error);
// }
// return currentProcess;
// }
public isPasswordValid(password: string) {
var alertElem = document.getElementById("passwordalert");