Skip to content

Commit

Permalink
Add account management
Browse files Browse the repository at this point in the history
  • Loading branch information
danielailie committed Nov 14, 2024
1 parent 3092ab2 commit be40e3a
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 98 deletions.
64 changes: 0 additions & 64 deletions src/account.ts

This file was deleted.

62 changes: 62 additions & 0 deletions src/accountManagement/accountController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { IAccount } from "../accounts/interfaces";
import { Transaction } from "../transaction";
import { TransactionComputer } from "../transactionComputer";
import { TransactionsFactoryConfig } from "../transactionsFactories";
import { AccountTransactionsFactory } from "./accountTransactionsFactory";
import { SaveKeyValueInput, SetGuardianInput } from "./resources";

export class AccountController {
private factory: AccountTransactionsFactory;
private txComputer: TransactionComputer;

constructor(options: { chainID: string }) {
this.factory = new AccountTransactionsFactory({
config: new TransactionsFactoryConfig(options),
});
this.txComputer = new TransactionComputer();
}

async createTransactionForSavingKeyValue(
sender: IAccount,
nonce: bigint,
options: SaveKeyValueInput,
): Promise<Transaction> {
const transaction = this.factory.createTransactionForSavingKeyValue(sender.address, options);

transaction.nonce = nonce;
transaction.signature = await sender.sign(this.txComputer.computeBytesForSigning(transaction));

return transaction;
}

async createTransactionForSettingGuardian(
sender: IAccount,
nonce: bigint,
options: SetGuardianInput,
): Promise<Transaction> {
const transaction = this.factory.createTransactionForSettingGuardian(sender.address, options);

transaction.nonce = nonce;
transaction.signature = await sender.sign(this.txComputer.computeBytesForSigning(transaction));

return transaction;
}

async createTransactionForGuardingAccount(sender: IAccount, nonce: bigint): Promise<Transaction> {
const transaction = this.factory.createTransactionForGuardingAccount(sender.address);

transaction.nonce = nonce;
transaction.signature = await sender.sign(this.txComputer.computeBytesForSigning(transaction));

return transaction;
}

async createTransactionForUnguardingAccount(sender: IAccount, nonce: bigint): Promise<Transaction> {
const transaction = this.factory.createTransactionForUnguardingAccount(sender.address);

transaction.nonce = nonce;
transaction.signature = await sender.sign(this.txComputer.computeBytesForSigning(transaction));

return transaction;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { assert } from "chai";
import { Address } from "../address";
import { TransactionsFactoryConfig } from "./transactionsFactoryConfig";
import { TransactionsFactoryConfig } from "../transactionsFactories";
import { AccountTransactionsFactory } from "./accountTransactionsFactory";

describe("test account transactions factory", function () {
Expand All @@ -11,8 +11,7 @@ describe("test account transactions factory", function () {
const sender = Address.fromBech32("erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th");
const keyValuePairs = new Map([[Buffer.from("key0"), Buffer.from("value0")]]);

const transaction = factory.createTransactionForSavingKeyValue({
sender: sender,
const transaction = factory.createTransactionForSavingKeyValue(sender, {
keyValuePairs: keyValuePairs,
});

Expand All @@ -29,8 +28,7 @@ describe("test account transactions factory", function () {
const guardian = Address.fromBech32("erd1spyavw0956vq68xj8y4tenjpq2wd5a9p2c6j8gsz7ztyrnpxrruqzu66jx");
const serviceID = "MultiversXTCSService";

const transaction = factory.createTransactionForSettingGuardian({
sender: sender,
const transaction = factory.createTransactionForSettingGuardian(sender, {
guardianAddress: guardian,
serviceID: serviceID,
});
Expand All @@ -49,9 +47,7 @@ describe("test account transactions factory", function () {
it("should create 'Transaction' for guarding account", async function () {
const sender = Address.fromBech32("erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th");

const transaction = factory.createTransactionForGuardingAccount({
sender: sender,
});
const transaction = factory.createTransactionForGuardingAccount(sender);

assert.equal(transaction.sender, "erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th");
assert.equal(transaction.receiver, "erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th");
Expand All @@ -64,9 +60,7 @@ describe("test account transactions factory", function () {
it("should create 'Transaction' for unguarding account", async function () {
const sender = Address.fromBech32("erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th");

const transaction = factory.createTransactionForUnguardingAccount({
sender: sender,
});
const transaction = factory.createTransactionForUnguardingAccount(sender);

assert.equal(transaction.sender, "erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th");
assert.equal(transaction.receiver, "erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Address } from "../address";
import { IAddress } from "../interface";
import { Transaction } from "../transaction";
import { TransactionBuilder } from "./transactionBuilder";
import { TransactionBuilder } from "../transactionsFactories/transactionBuilder";
import { SaveKeyValueInput, SetGuardianInput } from "./resources";

interface IConfig {
export interface IConfig {
chainID: string;
minGasLimit: bigint;
gasLimitPerByte: bigint;
Expand All @@ -22,19 +23,16 @@ export class AccountTransactionsFactory {
this.config = options.config;
}

createTransactionForSavingKeyValue(options: {
sender: IAddress;
keyValuePairs: Map<Uint8Array, Uint8Array>;
}): Transaction {
createTransactionForSavingKeyValue(sender: IAddress, options: SaveKeyValueInput): Transaction {
const functionName = "SaveKeyValue";
const keyValueParts = this.computeDataPartsForSavingKeyValue(options.keyValuePairs);
const dataParts = [functionName, ...keyValueParts];
const extraGas = this.computeExtraGasForSavingKeyValue(options.keyValuePairs);

return new TransactionBuilder({
config: this.config,
sender: options.sender,
receiver: options.sender,
sender: sender,
receiver: sender,
dataParts: dataParts,
gasLimit: extraGas,
addDataMovementGas: true,
Expand Down Expand Up @@ -63,11 +61,7 @@ export class AccountTransactionsFactory {
return dataParts;
}

createTransactionForSettingGuardian(options: {
sender: IAddress;
guardianAddress: IAddress;
serviceID: string;
}): Transaction {
createTransactionForSettingGuardian(sender: IAddress, options: SetGuardianInput): Transaction {
const dataParts = [
"SetGuardian",
Address.fromBech32(options.guardianAddress.bech32()).toHex(),
Expand All @@ -76,34 +70,34 @@ export class AccountTransactionsFactory {

return new TransactionBuilder({
config: this.config,
sender: options.sender,
receiver: options.sender,
sender: sender,
receiver: sender,
dataParts: dataParts,
gasLimit: this.config.gasLimitSetGuardian,
addDataMovementGas: true,
}).build();
}

createTransactionForGuardingAccount(options: { sender: IAddress }): Transaction {
createTransactionForGuardingAccount(sender: IAddress): Transaction {
const dataParts = ["GuardAccount"];

return new TransactionBuilder({
config: this.config,
sender: options.sender,
receiver: options.sender,
sender: sender,
receiver: sender,
dataParts: dataParts,
gasLimit: this.config.gasLimitGuardAccount,
addDataMovementGas: true,
}).build();
}

createTransactionForUnguardingAccount(options: { sender: IAddress }): Transaction {
createTransactionForUnguardingAccount(sender: IAddress): Transaction {
const dataParts = ["UnGuardAccount"];

return new TransactionBuilder({
config: this.config,
sender: options.sender,
receiver: options.sender,
sender: sender,
receiver: sender,
dataParts: dataParts,
gasLimit: this.config.gasLimitUnguardAccount,
addDataMovementGas: true,
Expand Down
2 changes: 2 additions & 0 deletions src/accountManagement/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./accountController";
export * from "./accountTransactionsFactory";
4 changes: 4 additions & 0 deletions src/accountManagement/resources.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { IAddress } from "../interface";

export type SetGuardianInput = { guardianAddress: IAddress; serviceID: string };
export type SaveKeyValueInput = { keyValuePairs: Map<Uint8Array, Uint8Array> };
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
require("./globals");

export * from "./abi";
export * from "./account";
export * from "./accountManagement";
export * from "./accounts";
export * from "./adapters";
export * from "./address";
export * from "./asyncTimer";
Expand Down
2 changes: 1 addition & 1 deletion src/transactionsFactories/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export * from "../accountManagement/accountTransactionsFactory";
export * from "./delegationTransactionsFactory";
export * from "./relayedTransactionsFactory";
export * from "./smartContractTransactionsFactory";
export * from "./tokenManagementTransactionsFactory";
export * from "./transactionsFactoryConfig";
export * from "./transferTransactionsFactory";
export * from "./accountTransactionsFactory";

0 comments on commit be40e3a

Please sign in to comment.