Skip to content

Commit

Permalink
created migration for walletaddress
Browse files Browse the repository at this point in the history
  • Loading branch information
abdulhakim2902 authored and irmannmal committed Dec 13, 2021
1 parent 0247cbc commit 6b52072
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 29 deletions.
7 changes: 2 additions & 5 deletions src/migrations/0.0.0.migration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export class MigrationScript000 implements MigrationScript {
}

async createPeople(people: People[]): Promise<void> {
const {getKeyring, getHexPublicKey} = new PolkadotJs();
const hasher = new BcryptHasher();

const filterPeople = (
await Promise.all(
Expand Down Expand Up @@ -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,
});
}),
);
Expand Down
58 changes: 41 additions & 17 deletions src/migrations/1.0.0.migration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -38,6 +37,36 @@ export class MigrationScript100 implements MigrationScript {
await this.doMigrateFriends();
// await this.doMigratePeople();
await this.doRemoveFriends();
await this.doMigrateWalletAddress();
}

async doMigrateWalletAddress(): Promise<void> {
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<void> {
Expand Down Expand Up @@ -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: '',
},
},
);
}),
);
}
Expand Down Expand Up @@ -250,8 +277,6 @@ export class MigrationScript100 implements MigrationScript {
}

async createOrFindPeople(people: People): Promise<People> {
const {getKeyring, getHexPublicKey} = new PolkadotJs();

let newPeople = await this.peopleRepository.findOne({
where: {
originUserId: people.originUserId,
Expand All @@ -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,
});
}

Expand Down
12 changes: 5 additions & 7 deletions src/services/currency.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down

0 comments on commit 6b52072

Please sign in to comment.