File

src/wallet/wallet.service.ts

Index

Properties
Methods

Constructor

constructor(walletModel: Model<WalletDocument>)
Parameters :
Name Type Optional
walletModel Model<WalletDocument> No

Methods

Async create
create(createWalletDto: CreateWalletDto)
Parameters :
Name Type Optional
createWalletDto CreateWalletDto No
Async createWeb3Wallet
createWeb3Wallet(custodialProvider: CustodyProvider)
Parameters :
Name Type Optional
custodialProvider CustodyProvider No
Returns : Promise<string>
Async findAll
findAll()
Async findByAddress
findByAddress(address: string)
Parameters :
Name Type Optional
address string No
Async findByIds
findByIds(ids: Wallet[])
Parameters :
Name Type Optional
ids Wallet[] No
Async findOne
findOne(id: string)
Parameters :
Name Type Optional
id string No
Async generateInternalProviderCustody
generateInternalProviderCustody()
Returns : unknown
Async getInternalProviderCustody
getInternalProviderCustody(address: string)
Parameters :
Name Type Optional
address string No
Returns : unknown
Async update
update(id: string, updateWalletDto: UpdateWalletDto)
Parameters :
Name Type Optional
id string No
updateWalletDto UpdateWalletDto No
Async updateLabel
updateLabel(address: string, updateWalletDto: UpdateWalletDto)
Parameters :
Name Type Optional
address string No
updateWalletDto UpdateWalletDto No

Properties

Private Readonly internalCustodialService
Type : InternalCustodialService
Decorators :
@Inject(InternalCustodialService)
Private Readonly kms
Type : KMS
import { Model } from "mongoose";
import { BadRequestException, Inject, Injectable } from "@nestjs/common";
import { InjectModel } from "@nestjs/mongoose";
import { ethers } from "ethers";

import { CustodyProvider } from "src/common/interfaces";
import { CreateWalletDto } from "./dto/create-wallet.dto";
import { UpdateWalletDto } from "./dto/update-wallet.dto";
import { Wallet, WalletDocument } from "./schemas/wallet.schema";
import { InternalCustodialService } from "src/shared/custodial/internalCustodial.service";
import { KMS } from "aws-sdk";
import { EncryptionAlgorithm } from "src/common/constants";

@Injectable()
export class WalletService {
  @Inject(InternalCustodialService)
  private readonly internalCustodialService: InternalCustodialService;

  private readonly kms: KMS;
  constructor(
    @InjectModel(Wallet.name)
    private readonly walletModel: Model<WalletDocument>,
  ) {
    this.kms = new KMS({
      region: process.env.KMS_REGION,
      accessKeyId: process.env.AWS_KMS_ACCESS_KEY_ID,
      secretAccessKey: process.env.AWS_KMS_SECRET_ACCESS_KEY,
    });
  }

  async create(createWalletDto: CreateWalletDto): Promise<WalletDocument> {
    const address = await this.createWeb3Wallet("Internal");

    const wallet = new this.walletModel({
      ...createWalletDto,
      address,
      custodyProvider: "Internal",
    });
    return wallet.save();
  }

  async findAll(): Promise<WalletDocument[]> {
    return this.walletModel.find();
  }

  async findByIds(ids: Wallet[]): Promise<WalletDocument[]> {
    try {
      return this.walletModel.find({ _id: { $in: ids } });
    } catch (error) {
      throw new BadRequestException("Wrong subscribedFunds Id", {
        cause: error,
      });
    }
  }

  async findOne(id: string): Promise<WalletDocument> {
    return this.walletModel.findById(id);
  }

  async findByAddress(address: string): Promise<WalletDocument> {
    return this.walletModel.findOne({ address });
  }

  async update(
    id: string,
    updateWalletDto: UpdateWalletDto,
  ): Promise<WalletDocument> {
    return this.walletModel.findByIdAndUpdate(id, updateWalletDto);
  }
  async updateLabel(
    address: string,
    updateWalletDto: UpdateWalletDto,
  ): Promise<WalletDocument> {
    return this.walletModel.findOneAndUpdate({ address }, updateWalletDto);
  }
  async createWeb3Wallet(custodialProvider: CustodyProvider): Promise<string> {
    if (custodialProvider === "Internal") {
      return await this.generateInternalProviderCustody();
    }
    throw new Error("Unavailable wallet provider");
  }

  async generateInternalProviderCustody() {
    const wallet = ethers.Wallet.createRandom();

    const encryptedPrivateKey = await this.kms
      .encrypt({
        EncryptionAlgorithm,
        KeyId: process.env.KMS_KEY_ID,
        Plaintext: wallet.privateKey,
      })
      .promise();

    const privateKeyEncryptedString =
      encryptedPrivateKey.CiphertextBlob.toString("base64");

    await this.internalCustodialService.create({
      address: wallet.address,
      privateKey: privateKeyEncryptedString,
    });

    return wallet.address;
  }

  async getInternalProviderCustody(address: string) {
    return this.internalCustodialService.findOne(address);
  }
}

results matching ""

    No results matching ""