Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions config/config.template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,5 @@ export const config = {
name: "wwWallet demo",
},
},
alg: "EdDSA",
notifications: {
enabled: "NOTIFICATIONS_ENABLED",
serviceAccount: "firebaseConfig.json"
}
alg: "EdDSA"
}
1 change: 1 addition & 0 deletions development.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ RUN yarn cache clean && yarn install
FROM node:16-bullseye-slim as development

ENV NODE_PATH=/node_modules
ENV PATH="/node_modules/.bin:${PATH}"
COPY --from=dependencies /dependencies/node_modules /node_modules

WORKDIR /app
Expand Down
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@
"cors": "^2.8.5",
"express": "^4.18.1",
"express-ws": "^5.0.2",
"fcm-notification": "^2.0.0",
"firebase-admin": "^11.5.0",
"inversify": "^6.0.1",
"jose": "^4.9.3",
"jsonpath-plus": "^7.2.0",
Expand Down
8 changes: 8 additions & 0 deletions run-migrations.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env sh
set -e

CONTAINER_NAME="wallet-backend-server"

echo "Running migrations inside container $CONTAINER_NAME..."
docker exec -it $CONTAINER_NAME yarn typeorm migration:run
echo "Migrations completed."
5 changes: 4 additions & 1 deletion src/AppDataSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ const AppDataSource: DataSource = new DataSource({
password: config.db.password,
database: config.db.dbname,
entities: [__dirname + "/entities/*.entity.{js,ts}"],
synchronize: true
migrations: [
__dirname + "/migrations/*.{js,ts}"
],
synchronize: false,
});

(async function initDataSource() {
Expand Down
2 changes: 1 addition & 1 deletion src/entities/CredentialIssuer.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class CredentialIssuerEntity {


// Explicit default to workaround a bug in typeorm: https://github.com/typeorm/typeorm/issues/3076#issuecomment-703128687
@Column({ nullable: true, type: "tinyint" })
@Column({ nullable: true, type: "tinyint", default: () => "NULL" })
visible: boolean;
}

Expand Down
39 changes: 0 additions & 39 deletions src/entities/FcmToken.entity.ts

This file was deleted.

17 changes: 0 additions & 17 deletions src/entities/user.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import * as uuid from 'uuid';

import AppDataSource from "../AppDataSource";
import * as scrypt from "../scrypt";
import { FcmTokenEntity } from "./FcmToken.entity";
import { checkedUpdate, EtagUpdate, isResult } from "../util/util";
import { runTransaction } from "./common.entity";

Expand Down Expand Up @@ -119,10 +118,6 @@ class UserEntity {
{ cascade: true, onDelete: "CASCADE", orphanedRowAction: "delete", eager: true, nullable: false })
webauthnCredentials: WebauthnCredentialEntity[];


@OneToMany(() => FcmTokenEntity, (fcmToken) => fcmToken.user, { eager: true })
fcmTokenList: FcmTokenEntity[];

@Column({ nullable: false, default: 0 })
openidRefreshTokenMaxAgeInSeconds: number;
}
Expand Down Expand Up @@ -187,13 +182,11 @@ type CreateUser = {
username: string;
displayName: string,
passwordHash: string;
fcmToken: string;
privateData: Buffer;
} | {
uuid: UserId;
displayName: string,
keys: Buffer;
fcmToken: string;
privateData: Buffer;
webauthnCredentials: WebauthnCredentialEntity[];
}
Expand All @@ -215,10 +208,6 @@ enum UpdateUserErr {
PRIVATE_DATA_CONFLICT = "PRIVATE_DATA_CONFLICT",
}

enum UpdateFcmError {
DB_ERR = "Failed to update FCM token list"
}

enum DeleteUserErr {
FAILED_TO_DELETE = "FAILED_TO_DELETE"
}
Expand All @@ -237,11 +226,6 @@ async function createUser(createUser: CreateUser, isAdmin: boolean = false): Pro
did: uuid.id,
isAdmin,
}));
const fcmTokenEntity = new FcmTokenEntity();
fcmTokenEntity.value = createUser.fcmToken;
fcmTokenEntity.user = user;
AppDataSource.getRepository(FcmTokenEntity).save(fcmTokenEntity);

return Ok(user);
}
catch(e) {
Expand Down Expand Up @@ -497,7 +481,6 @@ export {
createUser,
getUser,
getUserByCredentials,
UpdateFcmError,
getUserByWebauthnCredential,
getAllUsers,
newWebauthnCredentialEntity,
Expand Down
64 changes: 0 additions & 64 deletions src/lib/firebase.ts

This file was deleted.

38 changes: 38 additions & 0 deletions src/migrations/1762258689037-RemoveFcmToken.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { MigrationInterface, QueryRunner, TableForeignKey } from "typeorm";

export class RemoveFcmToken1762258689037 implements MigrationInterface {
name = 'RemoveFcmToken1762258689037';

public async up(queryRunner: QueryRunner): Promise<void> {
const table = await queryRunner.getTable("fcm_token");
// Name-agnostic drop of user entity's one-to-many relationship
if (table) {
const fk = table.foreignKeys.find(k => k.columnNames.includes("userId"));
if (fk) await queryRunner.dropForeignKey("fcm_token", fk);
await queryRunner.query("DROP TABLE `fcm_token`");
}
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`
CREATE TABLE \`fcm_token\` (
\`id\` int NOT NULL AUTO_INCREMENT,
\`value\` varchar(255) NOT NULL,
\`userId\` int NULL,
PRIMARY KEY (\`id\`)
) ENGINE=InnoDB
`);

await queryRunner.createForeignKey(
"fcm_token",
new TableForeignKey({
columnNames: ["userId"],
referencedTableName: "user",
referencedColumnNames: ["id"],
onDelete: "NO ACTION",
onUpdate: "NO ACTION",
})
);
}

}
12 changes: 1 addition & 11 deletions src/routers/storage.router.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import express, { Request, Response, Router } from "express";
import { getAllVerifiableCredentials, getVerifiableCredentialByCredentialIdentifier, deleteVerifiableCredential, createVerifiableCredential, updateVerifiableCredential, VerifiableCredentialEntity } from "../entities/VerifiableCredential.entity";
import { createVerifiablePresentation, deletePresentationsByCredentialId, getAllVerifiablePresentations, getPresentationByIdentifier } from "../entities/VerifiablePresentation.entity";
import { sendPushNotification } from "../lib/firebase";
import { getUser } from "../entities/user.entity";


Expand Down Expand Up @@ -34,16 +33,7 @@ async function storeCredentials(req: Request, res: Response) {
issuanceDate: new Date(),
...storableCredential,
});
})).then(() => {
if (user.fcmTokenList) {
for (const fcmToken of user.fcmTokenList) {
sendPushNotification(fcmToken.value, "New Credential", "A new verifiable credential is in your wallet").catch(err => {
console.log("Failed to send notification")
console.log(err)
});
}
}
});
}))
res.send({});
}

Expand Down
19 changes: 0 additions & 19 deletions src/routers/user.router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import { appContainer } from '../services/inversify.config';
import { RegistrationParams, WalletKeystoreManager } from '../services/interfaces';
import { TYPES } from '../services/types';
import { runTransaction } from '../entities/common.entity';
import { deleteAllFcmTokensForUser, FcmTokenEntity } from '../entities/FcmToken.entity';
import { deleteAllPresentationsWithHolderDID } from '../entities/VerifiablePresentation.entity';
import { deleteAllCredentialsWithHolderDID } from '../entities/VerifiableCredential.entity';
import { Err, Ok, Result } from 'ts-results';
Expand Down Expand Up @@ -314,23 +313,6 @@ noAuthUserController.post('/login-webauthn-finish', async (req: Request, res: Re
})


userController.post('/fcm_token/add', async (req: Request, res: Response) => {
updateUser(req.user.id, (userEntity, manager) => {
if (req.body.fcm_token &&
req.body.fcm_token != '' &&
userEntity.fcmTokenList.filter((fcmTokenEntity) => fcmTokenEntity.value == req.body.fcm_token).length == 0) {
const fcmTokenEntity = new FcmTokenEntity();
fcmTokenEntity.user = userEntity;
fcmTokenEntity.value = req.body.fcm_token;
manager.save(fcmTokenEntity).then((result) => {
userEntity.fcmTokenList.push(result);
});
}
return userEntity;
});
res.status(200).send({});
})

userController.get('/account-info', async (req: Request, res: Response) => {
const userRes = await getUser(req.user.id);
if (userRes.err) {
Expand Down Expand Up @@ -617,7 +599,6 @@ userController.delete('/', async (req: Request, res: Response) => {
// ts-results does not seem to provide an async-optimized version of Result.all(),
// and it turned out nontrivial to write one that preserves the Ok and Err types like Result.all() does.
return Result.all(
await deleteAllFcmTokensForUser(req.user.id, { entityManager }),
await deleteAllCredentialsWithHolderDID(req.user.did, { entityManager }),
await deleteAllPresentationsWithHolderDID(req.user.did, { entityManager }),
await deleteUser(req.user.id, { entityManager }),
Expand Down
4 changes: 1 addition & 3 deletions src/services/WalletKeystoreManagerService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@ export class WalletKeystoreManagerService implements WalletKeystoreManager {
@inject(TYPES.ClientKeystoreService) private clientWalletKeystoreService: WalletKeystore,
) { }

async initializeWallet(registrationParams: RegistrationParams): Promise<Result<{ fcmToken: string, keys: Buffer, displayName: string, privateData: Buffer, walletType: WalletType }, WalletKeystoreErr>> {
const fcmToken = registrationParams.fcm_token ? registrationParams.fcm_token : "";
async initializeWallet(registrationParams: RegistrationParams): Promise<Result<{ keys: Buffer, displayName: string, privateData: Buffer, walletType: WalletType }, WalletKeystoreErr>> {
return Ok({
fcmToken,
keys: Buffer.from(""),
displayName: registrationParams.displayName,
privateData: Buffer.from(registrationParams.privateData),
Expand Down
3 changes: 1 addition & 2 deletions src/services/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,14 @@ export type AdditionalKeystoreParameters = {
}

export type RegistrationParams = {
fcm_token?: string;
keys?: any;
privateData?: Buffer;
displayName: string;
}


export interface WalletKeystoreManager {
initializeWallet(registrationParams: RegistrationParams): Promise<Result<{ fcmToken: string, keys: Buffer, displayName: string, privateData: Buffer, walletType: WalletType }, WalletKeystoreErr>>;
initializeWallet(registrationParams: RegistrationParams): Promise<Result<{ keys: Buffer, displayName: string, privateData: Buffer, walletType: WalletType }, WalletKeystoreErr>>;

signJwtPresentation(userId: UserId, nonce: string, audience: string, verifiableCredentials: any[], additionalParameters?: AdditionalKeystoreParameters): Promise<Result<{ vpjwt: string }, WalletKeystoreErr>>;
generateOpenid4vciProof(userId: UserId, audience: string, nonce: string, additionalParameters?: AdditionalKeystoreParameters): Promise<Result<{ proof_jwt: string }, WalletKeystoreErr>>;
Expand Down
Loading