From 6b520724b6fa854016818bc0de9d19128fdb5f01 Mon Sep 17 00:00:00 2001 From: abdulhakim2902 Date: Mon, 13 Dec 2021 21:07:57 +0700 Subject: [PATCH] created migration for walletaddress --- src/migrations/0.0.0.migration.ts | 7 ++-- src/migrations/1.0.0.migration.ts | 58 ++++++++++++++++++++++--------- src/services/currency.service.ts | 12 +++---- 3 files changed, 48 insertions(+), 29 deletions(-) diff --git a/src/migrations/0.0.0.migration.ts b/src/migrations/0.0.0.migration.ts index fca26cda9..c7fb35e4f 100644 --- a/src/migrations/0.0.0.migration.ts +++ b/src/migrations/0.0.0.migration.ts @@ -97,7 +97,7 @@ export class MigrationScript000 implements MigrationScript { } async createPeople(people: People[]): Promise { - const {getKeyring, getHexPublicKey} = new PolkadotJs(); + const hasher = new BcryptHasher(); const filterPeople = ( await Promise.all( @@ -132,16 +132,13 @@ export class MigrationScript000 implements MigrationScript { await Promise.all( newPeople.map(async person => { - const hasher = new BcryptHasher(); const hashPeopleId = await hasher.hashPassword( person.id + config.ESCROW_SECRET_KEY, ); - const newKey = getKeyring().addFromUri('//' + hashPeopleId); - const walletAddress = getHexPublicKey(newKey); return this.peopleRepository.updateById(person.id, { createdAt: new Date().toString(), updatedAt: new Date().toString(), - walletAddress: walletAddress, + walletAddressPassword: hashPeopleId, }); }), ); diff --git a/src/migrations/1.0.0.migration.ts b/src/migrations/1.0.0.migration.ts index b0d9ba0c2..d76e0737c 100644 --- a/src/migrations/1.0.0.migration.ts +++ b/src/migrations/1.0.0.migration.ts @@ -11,7 +11,6 @@ import {inject, service} from '@loopback/core'; import {Twitter, Reddit, FriendService} from '../services'; import {BcryptHasher} from '../services/authentication/hash.password.service'; import {config} from '../config'; -import {PolkadotJs} from '../utils/polkadotJs-utils'; /* eslint-disable @typescript-eslint/no-explicit-any */ @migrationScript() @@ -38,6 +37,36 @@ export class MigrationScript100 implements MigrationScript { await this.doMigrateFriends(); // await this.doMigratePeople(); await this.doRemoveFriends(); + await this.doMigrateWalletAddress(); + } + + async doMigrateWalletAddress(): Promise { + const collection = ( + this.peopleRepository.dataSource.connector as any + ).collection(People.modelName); + + const people = await collection.aggregate().get(); + const hasher = new BcryptHasher(); + + await Promise.all( + people.map(async (e: AnyObject) => { + const hashPeopleId = await hasher.hashPassword( + e._id + config.ESCROW_SECRET_KEY, + ); + + return collection.update( + {_id: e._id}, + { + $unset: { + walletAddress: '', + }, + $set: { + walletAddressPassword: hashPeopleId, + }, + }, + ); + }), + ); } async doRemoveFriends(): Promise { @@ -113,23 +142,21 @@ export class MigrationScript100 implements MigrationScript { platform, }); - await this.postRepository.updateAll( + return this.postRepository.updateAll( {totalImporter: count}, {originPostId: originPostId, platform: platform}, ); - } else { - await collection.update( - {_id: post._id}, - { - $unset: { - totalImporter: '', - importers: '', - }, - }, - ); } - return null; + return collection.update( + {_id: post._id}, + { + $unset: { + totalImporter: '', + importers: '', + }, + }, + ); }), ); } @@ -250,8 +277,6 @@ export class MigrationScript100 implements MigrationScript { } async createOrFindPeople(people: People): Promise { - const {getKeyring, getHexPublicKey} = new PolkadotJs(); - let newPeople = await this.peopleRepository.findOne({ where: { originUserId: people.originUserId, @@ -266,10 +291,9 @@ export class MigrationScript100 implements MigrationScript { const hashPeopleId = await hasher.hashPassword( newPeople.id + config.ESCROW_SECRET_KEY, ); - const newKey = getKeyring().addFromUri('//' + hashPeopleId); await this.peopleRepository.updateById(newPeople.id, { - walletAddress: getHexPublicKey(newKey), + walletAddressPassword: hashPeopleId, }); } diff --git a/src/services/currency.service.ts b/src/services/currency.service.ts index a03f197ec..35423e1a0 100644 --- a/src/services/currency.service.ts +++ b/src/services/currency.service.ts @@ -18,13 +18,11 @@ import {PolkadotJs} from '../utils/polkadotJs-utils'; import acala from '../data-seed/currencies.json'; import {HttpErrors} from '@loopback/rest'; import {BcryptHasher} from './authentication/hash.password.service'; -import { - JWTService, - MetricService, - ActivityLogService, - TransactionService, - NotificationService, -} from './'; +import {NotificationService} from './notification.service'; +import {TransactionService} from './transaction.service'; +import {ActivityLogService} from './activity-log.service'; +import {MetricService} from './metric.service'; +import {JWTService} from './authentication'; import {TokenServiceBindings} from '../keys'; const BN = require('bn.js');