From 6699b4d6eb7a158ef027f4128d62c4ad63a696d9 Mon Sep 17 00:00:00 2001 From: einhorn_b Date: Wed, 29 Sep 2021 18:47:10 +0200 Subject: [PATCH] first steps for send coins --- .gitmodules | 3 +++ backend/package.json | 3 +++ backend/src/proto | 1 + backend/src/util/validate.ts | 21 ++++++++++++++++++++- 4 files changed, 27 insertions(+), 1 deletion(-) create mode 160000 backend/src/proto diff --git a/.gitmodules b/.gitmodules index 22790ccc7..5026a5b8a 100644 --- a/.gitmodules +++ b/.gitmodules @@ -34,3 +34,6 @@ [submodule "login_server/dependencies/protobuf"] path = login_server/dependencies/protobuf url = https://github.com/protocolbuffers/protobuf.git +[submodule "backend/src/proto"] + path = backend/src/proto + url = git@github.com:gradido/gradido_protocol.git diff --git a/backend/package.json b/backend/package.json index 4719595bb..5e3d712b3 100644 --- a/backend/package.json +++ b/backend/package.json @@ -16,6 +16,7 @@ "test": "jest --coverage" }, "dependencies": { + "@apollo/protobufjs": "^1.2.2", "@types/jest": "^27.0.2", "apollo-server-express": "^2.25.2", "axios": "^0.21.1", @@ -26,6 +27,7 @@ "graphql": "^15.5.1", "jest": "^27.2.4", "jsonwebtoken": "^8.5.1", + "libsodium-wrappers": "^0.7.9", "mysql2": "^2.3.0", "reflect-metadata": "^0.1.13", "ts-jest": "^27.0.5", @@ -35,6 +37,7 @@ "devDependencies": { "@types/express": "^4.17.12", "@types/jsonwebtoken": "^8.5.2", + "@types/libsodium-wrappers": "^0.7.9", "@typescript-eslint/eslint-plugin": "^4.28.0", "@typescript-eslint/parser": "^4.28.0", "eslint": "^7.29.0", diff --git a/backend/src/proto b/backend/src/proto new file mode 160000 index 000000000..cc9acbb21 --- /dev/null +++ b/backend/src/proto @@ -0,0 +1 @@ +Subproject commit cc9acbb212201a560d86fc87a47665497f11f27d diff --git a/backend/src/util/validate.ts b/backend/src/util/validate.ts index 6a38e5526..6f6c5af70 100644 --- a/backend/src/util/validate.ts +++ b/backend/src/util/validate.ts @@ -1,3 +1,8 @@ +import { User as dbUser } from '../typeorm/entity/User' +import { Balance as dbBalance } from '../typeorm/entity/Balance' +import { getRepository } from 'typeorm' +import { calculateDecay } from './decay' + function isStringBoolean(value: string): boolean { const lowerValue = value.toLowerCase() if (lowerValue === 'true' || lowerValue === 'false') { @@ -6,4 +11,18 @@ function isStringBoolean(value: string): boolean { return false } -export { isStringBoolean } +function isHexPublicKey(publicKey:string): boolean { + return /^[0-9A-Fa-f]{64}$/i.test(publicKey) +} + +async function hasUserAmount(user:dbUser, amount:number): Promise { + if(amount < 0) return false + const balanceRepository = getRepository(dbBalance) + const balance = await balanceRepository.findOne({ userId: user.id }) + if(!balance) return false + + const decay = await calculateDecay(balance.amount, balance.recordDate, new Date()) + return decay > amount +} + +export { isHexPublicKey, hasUserAmount, isStringBoolean } \ No newline at end of file