@veive-io/account-as
Version:
Veive smart account
266 lines (247 loc) • 11.7 kB
text/typescript
import { account } from "./proto/account";
import { authority, System, Protobuf, StringBytes } from "@koinos/sdk-as";
export class Account {
_contractId: Uint8Array;
/**
* Create an instance of a Account contract
* @example
* ```ts
* const contract = new Account(Base58.decode("1DQzuCcTKacbs9GGScFTU1Hc8BsyARTPqe"));
* ```
*/
constructor(contractId: Uint8Array) {
this._contractId = contractId;
}
/**
* Executes the given operation after performing pre-checks and post-checks.
*
* This method ensures that the caller is not itself and is not a registered execution module,
* performs pre-checks using all registered hook modules, executes the operation using all registered executor modules,
* and performs post-checks using all registered hook modules.
*
* @external
*/
execute(args: account.execute_args): void {
const argsBuffer = Protobuf.encode(args, account.execute_args.encode);
const callRes = System.call(this._contractId, 0xbe6bdfe8, argsBuffer);
if (callRes.code != 0) {
const errorMessage = `failed to call 'Account.execute': ${callRes.res.error && callRes.res.error!.message ? callRes.res.error!.message : "unknown error"}`;
System.exit(callRes.code, StringBytes.stringToBytes(errorMessage));
}
return;
}
/**
* Executes the given operation if the caller is a registered execution module.
*
* This method ensures that the caller is not itself and is a registered execution module,
* performs pre-checks using all registered hook modules, executes the operation using all registered executor modules,
* and performs post-checks using all registered hook modules.
*
* @external
*/
execute_executor(args: account.execute_executor_args): void {
const argsBuffer = Protobuf.encode(args, account.execute_executor_args.encode);
const callRes = System.call(this._contractId, 0x584d99e2, argsBuffer);
if (callRes.code != 0) {
const errorMessage = `failed to call 'Account.execute_executor': ${callRes.res.error && callRes.res.error!.message ? callRes.res.error!.message : "unknown error"}`;
System.exit(callRes.code, StringBytes.stringToBytes(errorMessage));
}
return;
}
/**
* Executes a user operation directly.
*
* This method ensures that the caller is allowed, and then directly calls the contract's entry point with the provided arguments.
*
* @external
*/
execute_user(args: account.execute_user_args): void {
const argsBuffer = Protobuf.encode(args, account.execute_user_args.encode);
const callRes = System.call(this._contractId, 0x0fd2d000, argsBuffer);
if (callRes.code != 0) {
const errorMessage = `failed to call 'Account.execute_user': ${callRes.res.error && callRes.res.error!.message ? callRes.res.error!.message : "unknown error"}`;
System.exit(callRes.code, StringBytes.stringToBytes(errorMessage));
}
return;
}
/**
* Installs a new module of the specified type.
* This method adds a new module to the appropriate storage map based on the module type.
*
* @external
*/
install_module(args: account.install_module_args): void {
const argsBuffer = Protobuf.encode(args, account.install_module_args.encode);
const callRes = System.call(this._contractId, 0xde5e2a66, argsBuffer);
if (callRes.code != 0) {
const errorMessage = `failed to call 'Account.install_module': ${callRes.res.error && callRes.res.error!.message ? callRes.res.error!.message : "unknown error"}`;
System.exit(callRes.code, StringBytes.stringToBytes(errorMessage));
}
return;
}
/**
* Uninstalls a module of the specified type.
*
* This method removes a module from the appropriate storage map based on the module type.
*
* @external
*/
uninstall_module(args: account.uninstall_module_args): void {
const argsBuffer = Protobuf.encode(args, account.uninstall_module_args.encode);
const callRes = System.call(this._contractId, 0x973e5dab, argsBuffer);
if (callRes.code != 0) {
const errorMessage = `failed to call 'Account.uninstall_module': ${callRes.res.error && callRes.res.error!.message ? callRes.res.error!.message : "unknown error"}`;
System.exit(callRes.code, StringBytes.stringToBytes(errorMessage));
}
return;
}
/**
* Checks if a module is installed.
*
* This method returns a boolean indicating whether the specified module is installed, based on the module type.
*
* @external
* @readonly
*/
is_module_installed(args: account.is_module_installed_args): account.is_module_installed_result {
const argsBuffer = Protobuf.encode(args, account.is_module_installed_args.encode);
const callRes = System.call(this._contractId, 0x1eec944d, argsBuffer);
if (callRes.code != 0) {
const errorMessage = `failed to call 'Account.is_module_installed': ${callRes.res.error && callRes.res.error!.message ? callRes.res.error!.message : "unknown error"}`;
System.exit(callRes.code, StringBytes.stringToBytes(errorMessage));
}
if (!callRes.res.object) return new account.is_module_installed_result();
return Protobuf.decode<account.is_module_installed_result>(callRes.res.object, account.is_module_installed_result.decode);
}
/**
* Checks if a module type is supported.
*
* This method returns a boolean indicating whether the specified module type is supported.
*
* @external
* @readonly
*/
is_module_type_supported(args: account.is_module_type_supported_args): account.is_module_type_supported_result {
const argsBuffer = Protobuf.encode(args, account.is_module_type_supported_args.encode);
const callRes = System.call(this._contractId, 0x5090887a, argsBuffer);
if (callRes.code != 0) {
const errorMessage = `failed to call 'Account.is_module_type_supported': ${callRes.res.error && callRes.res.error!.message ? callRes.res.error!.message : "unknown error"}`;
System.exit(callRes.code, StringBytes.stringToBytes(errorMessage));
}
if (!callRes.res.object) return new account.is_module_type_supported_result();
return Protobuf.decode<account.is_module_type_supported_result>(callRes.res.object, account.is_module_type_supported_result.decode);
}
/**
* Retrieves all installed modules.
*
* This method returns a list of all installed modules across all types.
*
* @external
* @readonly
*/
get_modules(): account.get_modules_result {
const argsBuffer = new Uint8Array(0);
const callRes = System.call(this._contractId, 0x5c766919, argsBuffer);
if (callRes.code != 0) {
const errorMessage = `failed to call 'Account.get_modules': ${callRes.res.error && callRes.res.error!.message ? callRes.res.error!.message : "unknown error"}`;
System.exit(callRes.code, StringBytes.stringToBytes(errorMessage));
}
if (!callRes.res.object) return new account.get_modules_result();
return Protobuf.decode<account.get_modules_result>(callRes.res.object, account.get_modules_result.decode);
}
/**
* Validates the signature by invoking the validation logic.
*
* This method checks the validity of a signature using all registered validator modules.
* The signature is valid if AT LEAST one of the validators is valid.
*
* @external
*/
is_valid_signature(args: account.is_valid_signature_args): account.is_valid_signature_result {
const argsBuffer = Protobuf.encode(args, account.is_valid_signature_args.encode);
const callRes = System.call(this._contractId, 0x4ab7a4bb, argsBuffer);
if (callRes.code != 0) {
const errorMessage = `failed to call 'Account.is_valid_signature': ${callRes.res.error && callRes.res.error!.message ? callRes.res.error!.message : "unknown error"}`;
System.exit(callRes.code, StringBytes.stringToBytes(errorMessage));
}
if (!callRes.res.object) return new account.is_valid_signature_result();
return Protobuf.decode<account.is_valid_signature_result>(callRes.res.object, account.is_valid_signature_result.decode);
}
/**
* Validates an operation by invoking the validation logic.
*
* This method checks the validity of an operation using all registered validator modules.
* The operation is valid if ALL validators are valid.
*
* @external
*/
is_valid_operation(args: account.is_valid_operation_args): account.is_valid_operation_result {
const argsBuffer = Protobuf.encode(args, account.is_valid_operation_args.encode);
const callRes = System.call(this._contractId, 0xff67ddda, argsBuffer);
if (callRes.code != 0) {
const errorMessage = `failed to call 'Account.is_valid_operation': ${callRes.res.error && callRes.res.error!.message ? callRes.res.error!.message : "unknown error"}`;
System.exit(callRes.code, StringBytes.stringToBytes(errorMessage));
}
if (!callRes.res.object) return new account.is_valid_operation_result();
return Protobuf.decode<account.is_valid_operation_result>(callRes.res.object, account.is_valid_operation_result.decode);
}
/**
* Initializes the account.
*
* Placeholder for the account initialization logic.
*
* @external
*/
init_account(args: account.init_account_args): void {
const argsBuffer = Protobuf.encode(args, account.init_account_args.encode);
const callRes = System.call(this._contractId, 0xb78fb739, argsBuffer);
if (callRes.code != 0) {
const errorMessage = `failed to call 'Account.init_account': ${callRes.res.error && callRes.res.error!.message ? callRes.res.error!.message : "unknown error"}`;
System.exit(callRes.code, StringBytes.stringToBytes(errorMessage));
}
return;
}
/**
* @external
*/
test(): void {
const argsBuffer = new Uint8Array(0);
const callRes = System.call(this._contractId, 0x9f86d081, argsBuffer);
if (callRes.code != 0) {
const errorMessage = `failed to call 'Account.test': ${callRes.res.error && callRes.res.error!.message ? callRes.res.error!.message : "unknown error"}`;
System.exit(callRes.code, StringBytes.stringToBytes(errorMessage));
}
return;
}
/**
* Authorizes an operation.
*
* This method checks if a given operation is authorized based on the type and the provided arguments.
*
* @external
*/
authorize(args: authority.authorize_arguments): authority.authorize_result {
const argsBuffer = Protobuf.encode(args, authority.authorize_arguments.encode);
const callRes = System.call(this._contractId, 0x4a2dbd90, argsBuffer);
if (callRes.code != 0) {
const errorMessage = `failed to call 'Account.authorize': ${callRes.res.error && callRes.res.error!.message ? callRes.res.error!.message : "unknown error"}`;
System.exit(callRes.code, StringBytes.stringToBytes(errorMessage));
}
if (!callRes.res.object) return new authority.authorize_result();
return Protobuf.decode<authority.authorize_result>(callRes.res.object, authority.authorize_result.decode);
}
/**
* @external
* @readonly
*/
manifest(): account.manifest {
const argsBuffer = new Uint8Array(0);
const callRes = System.call(this._contractId, 0x05b3abf2, argsBuffer);
if (callRes.code != 0) {
const errorMessage = `failed to call 'Account.manifest': ${callRes.res.error && callRes.res.error!.message ? callRes.res.error!.message : "unknown error"}`;
System.exit(callRes.code, StringBytes.stringToBytes(errorMessage));
}
if (!callRes.res.object) return new account.manifest();
return Protobuf.decode<account.manifest>(callRes.res.object, account.manifest.decode);
}
}