import { hsm } from "@dinamonetworks/hsm-dinamo";

// Define os parâmetros de conexão com o HSM
const options = {
host: "127.0.0.1",
authUsernamePassword: {
username: "master",
password: "12345678",
},
};

// Define o usuário para criar e usar nos métodos
const USER_ID = "myUserTest";
const USER_PASSWORD = "12345678";

async function main() {
// Conecta ao HSM
const conn = await hsm.connect(options);

// Cria o usuário
try {
const permissions = [
hsm.enums.USER_PERMISSIONS.NS_AUTHORIZATION,
hsm.enums.USER_PERMISSIONS.SYS_OPERATOR,
];

const created = await conn.user.create(USER_ID, USER_PASSWORD, permissions);

if (created) {
console.log(`User "${USER_ID}" created successfully`);
} else {
console.log(`Failed to create user "${USER_ID}"`);
}
} catch (error) {
console.error("Error creating user:", error.message);
throw error;
}

await conn.disconnect();

const optNsAuth = {
host: "127.0.0.1",
authUsernamePassword: {
username: USER_ID,
password: USER_PASSWORD,
},
};

const nsAuthConn = await hsm.connect(optNsAuth);

// Array de shadows predefinidos
const shadows = [
"010202F5DD5EAC0092DA96C33761C13E48CAFD851A1BF91271E3CC",
"0202025752A336EF0F810FDD049DDFFBB4DE4FE8F051509ED91142",
];

// Define o estado NS Auth
try {
const aclMask = hsm.enums.ACL_MASK.NOP;
const state = hsm.enums.NSAUTH_STATE.ASSOCIATED;

const result = await nsAuthConn.management.setNsAuthState(
aclMask,
state,
shadows
);

console.log(`Set NS Auth State result: ${result}`);

if (result) {
console.log("NS Auth State set successfully.");
} else {
console.log("Failed to set NS Auth State.");
}
} catch (error) {
console.error("Error setting NS Auth State:", error.message);
throw error;
}

// Desconecta do HSM
await nsAuthConn.disconnect();
}

// Executa a função principal
main();