src/auth/auth.web3.service.ts
Properties |
|
Methods |
|
| Async addDealer |
addDealer(id: string, address: string, email: string)
|
|
Defined in src/auth/auth.web3.service.ts:157
|
|
Returns :
unknown
|
| Async addInvestor | |||||
addInvestor(undefined: IAddInvestor)
|
|||||
|
Defined in src/auth/auth.web3.service.ts:101
|
|||||
|
Parameters :
Returns :
unknown
|
| Async onboardUser | |||||
onboardUser(undefined: IOnboardUser)
|
|||||
|
Defined in src/auth/auth.web3.service.ts:39
|
|||||
|
Parameters :
Returns :
unknown
|
| Private Readonly adminProfileService |
Type : AdminProfileService
|
Decorators :
@Inject(AdminProfileService)
|
|
Defined in src/auth/auth.web3.service.ts:37
|
| Private Readonly userService |
Type : UserService
|
Decorators :
@Inject(UserService)
|
|
Defined in src/auth/auth.web3.service.ts:34
|
import {
BadRequestException,
Inject,
Injectable,
ServiceUnavailableException,
} from "@nestjs/common";
import { ContractTransactionResponse, encodeBytes32String } from "ethers";
import {
ContractName,
EndUserRoles,
RegistryName,
TransactionNames,
UserJwtPayload,
} from "src/common/interfaces";
import type { IAddInvestor, IOnboardUser } from "./interfaces/auth.interface";
import { Roles } from "src/common/constants";
import { transactionSubmitter } from "src/common/provider";
import {
getContractByName,
getRoleAndContractAddressByNameOrAddress,
provider,
} from "src/common/provider/web3/web3Provider";
import { UserService } from "src/user/user.service";
import { orderGroupRegistryFields } from "src/common/utils";
import { AdminProfileService } from "../admin-profile/admin-profile.service";
import { RoleRegistry } from "src/shared/artifacts/RoleRegistry";
@Injectable()
export class AuthWeb3Service {
@Inject(UserService)
private readonly userService: UserService;
@Inject(AdminProfileService)
private readonly adminProfileService: AdminProfileService;
async onboardUser({
onBoardedByRole,
newUserRole,
newUser,
internalCustodialService,
onChainFields,
dealerAddressSubmitter,
dealerId,
userJwtPayload,
}: IOnboardUser) {
try {
if (
onBoardedByRole === "dealer" &&
!(dealerAddressSubmitter && dealerId)
) {
throw new BadRequestException(
`The dealer bad credentials address ${dealerAddressSubmitter} and id ${dealerId}`,
);
}
const txs: ContractTransactionResponse[] = [];
if (onBoardedByRole === "admin") {
if (newUserRole === EndUserRoles.dealer) {
const tx = await this.addDealer(
newUser.id,
newUser.wallets[0].address,
userJwtPayload.email,
);
txs.push(tx);
} else {
const tx = await this.addInvestor({
signerKey: (
await this.adminProfileService.getCustodial(userJwtPayload.email)
).privateKey,
id: newUser.id,
userAddress: newUser.wallets[0].address,
onChainFields,
dealerId,
onBoardedByRole,
});
txs.push(tx);
}
} else {
const custody = await internalCustodialService.findOne(
dealerAddressSubmitter,
);
const tx = await this.addInvestor({
signerKey: custody.privateKey,
id: newUser.id,
userAddress: newUser.wallets[0].address,
onChainFields,
dealerId,
onBoardedByRole,
});
txs.push(tx);
}
return txs;
} catch (e) {
console.error(e);
throw new ServiceUnavailableException(e);
}
}
async addInvestor({
signerKey,
id,
userAddress,
onChainFields,
dealerId,
onBoardedByRole,
}: IAddInvestor) {
const investorIdInBytes = encodeBytes32String(id);
const tx = await transactionSubmitter({
signerKey,
contractAddress: getContractByName["InvestorRegistry"].address,
contractName: ContractName.InvestorRegistry,
transactionName: TransactionNames.addInvestor,
args: [
Roles.Dealer,
investorIdInBytes,
userAddress,
encodeBytes32String(dealerId),
],
});
await provider.waitForTransaction(tx.hash);
let userJwtPayload: UserJwtPayload = {
email: "email",
idUser: "",
role: "admin",
};
if (onBoardedByRole === EndUserRoles.dealer) {
const dealer = await this.userService.findOne(dealerId);
userJwtPayload = {
email: dealer.email,
idUser: "",
role: EndUserRoles.dealer,
};
}
await this.userService.setRegistryData({
registryName: RegistryName.InvestorRegistry,
registries: orderGroupRegistryFields(
onChainFields.map((registry) => {
return {
id: encodeBytes32String(id),
dataFieldType: registry.type,
dataFieldName: registry.name,
dataFieldValue: registry.value,
};
}),
),
userJwtPayload,
onchainFields: [],
});
return tx;
}
async addDealer(id: string, address: string, email: string) {
const [roleAndContract, adminCustodial] = await Promise.all([
getRoleAndContractAddressByNameOrAddress({
contractName: ContractName.DealerRegistry,
transactionName: TransactionNames.addDealer,
userAddress: process.env.ADMIN_WALLET_ADDRESS,
}),
this.adminProfileService.getCustodial(email),
]);
const { role, contractAddress } = roleAndContract;
const tx = await transactionSubmitter({
signerKey: adminCustodial.privateKey,
contractAddress,
contractName: ContractName.DealerRegistry,
transactionName: TransactionNames.addDealer,
args: [role, encodeBytes32String(id), address],
});
await tx.wait();
await transactionSubmitter({
signerKey: process.env.RELAYER_KEY,
contractAddress: RoleRegistry.address,
transactionName: TransactionNames.grantRole,
contractName: ContractName.RoleRegistry,
args: [Roles.Dealer, address],
});
return tx;
}
}