mirror of
https://github.com/IT4Change/gradido.git
synced 2025-12-13 07:45:54 +00:00
use protobuf models as static modules, first approach didn't work
This commit is contained in:
parent
a622fe02cd
commit
b4ed516a8e
@ -8,9 +8,11 @@
|
|||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"private": false,
|
"private": false,
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
"prebuild": "pbjs -t static-module -w commonjs -o src/proto/bundle.js src/proto/gradido/*.proto && pbts -o src/proto/bundle.d.ts src/proto/bundle.js",
|
||||||
"build": "tsc --build",
|
"build": "tsc --build",
|
||||||
"clean": "tsc --build --clean",
|
"clean": "tsc --build --clean",
|
||||||
"start": "node build/index.js",
|
"start": "node build/index.js",
|
||||||
|
"predev": "pbjs -t static-module -w commonjs -o src/proto/bundle.js src/proto/gradido/*.proto && pbts -o src/proto/bundle.d.ts src/proto/bundle.js",
|
||||||
"dev": "nodemon -w src --ext ts --exec ts-node src/index.ts",
|
"dev": "nodemon -w src --ext ts --exec ts-node src/index.ts",
|
||||||
"lint": "eslint . --ext .js,.ts",
|
"lint": "eslint . --ext .js,.ts",
|
||||||
"test": "jest --coverage"
|
"test": "jest --coverage"
|
||||||
@ -51,6 +53,7 @@
|
|||||||
"eslint-plugin-promise": "^5.1.0",
|
"eslint-plugin-promise": "^5.1.0",
|
||||||
"nodemon": "^2.0.7",
|
"nodemon": "^2.0.7",
|
||||||
"prettier": "^2.3.1",
|
"prettier": "^2.3.1",
|
||||||
|
"protobufjs": "^6.11.2",
|
||||||
"ts-node": "^10.0.0",
|
"ts-node": "^10.0.0",
|
||||||
"typescript": "^4.3.4"
|
"typescript": "^4.3.4"
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
/* eslint-disable @typescript-eslint/no-explicit-any */
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||||
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
|
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
|
||||||
|
|
||||||
import { Resolver, Query, Args, Authorized, Ctx, Mutation } from 'type-graphql'
|
import { Resolver, Query, Args, Authorized, Ctx, Mutation, Root } from 'type-graphql'
|
||||||
import { getCustomRepository, getConnection, getRepository } from 'typeorm'
|
import { getCustomRepository, getConnection, getRepository } from 'typeorm'
|
||||||
import { createTransport } from 'nodemailer'
|
import { createTransport } from 'nodemailer'
|
||||||
|
|
||||||
@ -33,7 +33,6 @@ import { calculateDecay, calculateDecayWithInterval } from '../../util/decay'
|
|||||||
import { TransactionTypeId } from '../enum/TransactionTypeId'
|
import { TransactionTypeId } from '../enum/TransactionTypeId'
|
||||||
import { TransactionType } from '../enum/TransactionType'
|
import { TransactionType } from '../enum/TransactionType'
|
||||||
import { hasUserAmount, isHexPublicKey } from '../../util/validate'
|
import { hasUserAmount, isHexPublicKey } from '../../util/validate'
|
||||||
import protobuf from '@apollo/protobufjs'
|
|
||||||
import {
|
import {
|
||||||
from_hex as fromHex,
|
from_hex as fromHex,
|
||||||
to_base64 as toBase64,
|
to_base64 as toBase64,
|
||||||
@ -46,6 +45,9 @@ import {
|
|||||||
crypto_generichash_BYTES as cryptoGenericHashBytes,
|
crypto_generichash_BYTES as cryptoGenericHashBytes,
|
||||||
} from 'libsodium-wrappers'
|
} from 'libsodium-wrappers'
|
||||||
|
|
||||||
|
import { proto } from '../../proto/bundle'
|
||||||
|
import context from '../../server/context'
|
||||||
|
|
||||||
// Helper function
|
// Helper function
|
||||||
async function calculateAndAddDecayTransactions(
|
async function calculateAndAddDecayTransactions(
|
||||||
userTransactions: DbUserTransaction[],
|
userTransactions: DbUserTransaction[],
|
||||||
@ -239,7 +241,7 @@ async function updateStateBalance(
|
|||||||
balance.amount = centAmount
|
balance.amount = centAmount
|
||||||
} else {
|
} else {
|
||||||
balance.amount =
|
balance.amount =
|
||||||
(await calculateDecay(balance.amount, balance.recordDate, received)) + centAmount
|
Number(await calculateDecay(balance.amount, balance.recordDate, received)) + centAmount
|
||||||
}
|
}
|
||||||
if (balance.amount <= 0) {
|
if (balance.amount <= 0) {
|
||||||
throw new Error('error new balance <= 0')
|
throw new Error('error new balance <= 0')
|
||||||
@ -254,16 +256,16 @@ async function updateStateBalance(
|
|||||||
// helper helper function
|
// helper helper function
|
||||||
async function addUserTransaction(user: dbUser, transaction: DbTransaction, centAmount: number) {
|
async function addUserTransaction(user: dbUser, transaction: DbTransaction, centAmount: number) {
|
||||||
let newBalance = centAmount
|
let newBalance = centAmount
|
||||||
|
|
||||||
const userTransactionRepository = getCustomRepository(UserTransactionRepository)
|
const userTransactionRepository = getCustomRepository(UserTransactionRepository)
|
||||||
const lastUserTransaction = await userTransactionRepository.findLastForUser(user.id)
|
const lastUserTransaction = await userTransactionRepository.findLastForUser(user.id)
|
||||||
if (lastUserTransaction) {
|
if (lastUserTransaction) {
|
||||||
newBalance += await calculateDecay(
|
newBalance += Number(await calculateDecay(
|
||||||
lastUserTransaction.balance,
|
Number(lastUserTransaction.balance),
|
||||||
lastUserTransaction.balanceDate,
|
lastUserTransaction.balanceDate,
|
||||||
transaction.received,
|
transaction.received,
|
||||||
)
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
if (newBalance <= 0) {
|
if (newBalance <= 0) {
|
||||||
throw new Error('error new balance <= 0')
|
throw new Error('error new balance <= 0')
|
||||||
}
|
}
|
||||||
@ -295,6 +297,7 @@ async function sendCoins(
|
|||||||
recipiantPublicKey: string,
|
recipiantPublicKey: string,
|
||||||
amount: number,
|
amount: number,
|
||||||
memo: string,
|
memo: string,
|
||||||
|
sessionId: number,
|
||||||
groupId = 0,
|
groupId = 0,
|
||||||
): Promise<boolean> {
|
): Promise<boolean> {
|
||||||
if (senderUser.pubkey.length !== 32) {
|
if (senderUser.pubkey.length !== 32) {
|
||||||
@ -309,37 +312,32 @@ async function sendCoins(
|
|||||||
if (!hasUserAmount(senderUser, amount)) {
|
if (!hasUserAmount(senderUser, amount)) {
|
||||||
throw new Error("user hasn't enough GDD")
|
throw new Error("user hasn't enough GDD")
|
||||||
}
|
}
|
||||||
const protoRoot = await protobuf.load('../../proto/gradido/GradidoTransfer.proto')
|
|
||||||
|
|
||||||
const GradidoTransfer = protoRoot.lookupType('proto.gradido.GradidoTransfer')
|
|
||||||
const TransferAmount = protoRoot.lookupType('proto.gradido.TransferAmount')
|
|
||||||
const centAmount = Math.trunc(amount * 10000)
|
const centAmount = Math.trunc(amount * 10000)
|
||||||
const transferAmount = TransferAmount.create({
|
const transferAmount = new proto.gradido.TransferAmount({
|
||||||
pubkey: senderUser.pubkey,
|
pubkey: senderUser.pubkey,
|
||||||
amount: centAmount,
|
amount: centAmount,
|
||||||
})
|
})
|
||||||
|
|
||||||
// no group id is given so we assume it is a local transfer
|
// no group id is given so we assume it is a local transfer
|
||||||
if (!groupId) {
|
if (!groupId) {
|
||||||
const LocalTransfer = protoRoot.lookupType('proto.gradido.LocalTransfer')
|
const localTransfer = new proto.gradido.LocalTransfer({
|
||||||
const localTransfer = LocalTransfer.create({
|
|
||||||
sender: transferAmount,
|
sender: transferAmount,
|
||||||
recipiant: fromHex(recipiantPublicKey),
|
recipiant: fromHex(recipiantPublicKey),
|
||||||
})
|
})
|
||||||
const createTransaction = GradidoTransfer.create({ local: localTransfer })
|
const transferTransaction = new proto.gradido.GradidoTransfer({ local: localTransfer })
|
||||||
const TransactionBody = protoRoot.lookupType('proto.gradido.TransactionBody')
|
const transactionBody = new proto.gradido.TransactionBody({
|
||||||
|
|
||||||
const transactionBody = TransactionBody.create({
|
|
||||||
memo: memo,
|
memo: memo,
|
||||||
created: new Date(),
|
created: { seconds: new Date().getTime() / 1000 },
|
||||||
data: createTransaction,
|
transfer: transferTransaction,
|
||||||
})
|
})
|
||||||
|
|
||||||
const bodyBytes = TransactionBody.encode(transactionBody).finish()
|
const bodyBytes = proto.gradido.TransactionBody.encode(transactionBody).finish()
|
||||||
const bodyBytesBase64 = toBase64(bodyBytes, base64Variants.ORIGINAL)
|
const bodyBytesBase64 = toBase64(bodyBytes, base64Variants.ORIGINAL)
|
||||||
// let Login-Server sign transaction
|
// let Login-Server sign transaction
|
||||||
|
|
||||||
const result = await apiPost(CONFIG.LOGIN_API_URL + 'signTransaction', {
|
const result = await apiPost(CONFIG.LOGIN_API_URL + 'signTransaction', {
|
||||||
|
session_id: sessionId,
|
||||||
bodyBytes: bodyBytesBase64,
|
bodyBytes: bodyBytesBase64,
|
||||||
})
|
})
|
||||||
if (!result.success) throw new Error(result.data)
|
if (!result.success) throw new Error(result.data)
|
||||||
@ -348,13 +346,12 @@ async function sendCoins(
|
|||||||
if (!cryptoSignVerifyDetached(sign, bodyBytesBase64, senderUser.pubkey)) {
|
if (!cryptoSignVerifyDetached(sign, bodyBytesBase64, senderUser.pubkey)) {
|
||||||
throw new Error('Could not verify signature')
|
throw new Error('Could not verify signature')
|
||||||
}
|
}
|
||||||
const SignatureMap = protoRoot.lookupType('proto.gradido.SignatureMap')
|
|
||||||
const SignaturePair = protoRoot.lookupType('proto.gradido.SignaturePair')
|
const sigPair = new proto.gradido.SignaturePair({
|
||||||
const sigPair = SignaturePair.create({
|
|
||||||
pubKey: senderUser.pubkey,
|
pubKey: senderUser.pubkey,
|
||||||
signature: { ed25519: sign },
|
ed25519: sign,
|
||||||
})
|
})
|
||||||
const sigMap = SignatureMap.create({ sigPair: [sigPair] })
|
const sigMap = new proto.gradido.SignatureMap({ sigPair: [sigPair] })
|
||||||
const userRepository = getCustomRepository(UserRepository)
|
const userRepository = getCustomRepository(UserRepository)
|
||||||
const recipiantUser = await userRepository.findByPubkeyHex(recipiantPublicKey)
|
const recipiantUser = await userRepository.findByPubkeyHex(recipiantPublicKey)
|
||||||
|
|
||||||
@ -424,7 +421,7 @@ async function sendCoins(
|
|||||||
// should match previous used format: yyyy-MM-dd HH:mm:ss
|
// should match previous used format: yyyy-MM-dd HH:mm:ss
|
||||||
const receivedString = transaction.received.toISOString().slice(0, 19).replace('T', ' ')
|
const receivedString = transaction.received.toISOString().slice(0, 19).replace('T', ' ')
|
||||||
cryptoGenerichashUpdate(state, receivedString)
|
cryptoGenerichashUpdate(state, receivedString)
|
||||||
cryptoGenerichashUpdate(state, SignatureMap.encode(sigMap).finish())
|
cryptoGenerichashUpdate(state, proto.gradido.SignatureMap.encode(sigMap).finish())
|
||||||
transaction.txHash = Buffer.from(cryptoGenerichashFinal(state, cryptoGenericHashBytes))
|
transaction.txHash = Buffer.from(cryptoGenerichashFinal(state, cryptoGenericHashBytes))
|
||||||
transactionRepository.save(transaction).catch(() => {
|
transactionRepository.save(transaction).catch(() => {
|
||||||
throw new Error('error saving transaction with tx hash')
|
throw new Error('error saving transaction with tx hash')
|
||||||
@ -589,7 +586,7 @@ export class TransactionResolver {
|
|||||||
const userRepository = getCustomRepository(UserRepository)
|
const userRepository = getCustomRepository(UserRepository)
|
||||||
const userEntity = await userRepository.findByPubkeyHex(context.pubKey)
|
const userEntity = await userRepository.findByPubkeyHex(context.pubKey)
|
||||||
|
|
||||||
const transaction = sendCoins(userEntity, recipiantPublicKey, amount, memo)
|
const transaction = sendCoins(userEntity, recipiantPublicKey, amount, memo, context.sessionId)
|
||||||
if (!transaction) {
|
if (!transaction) {
|
||||||
throw new Error('error sending coins')
|
throw new Error('error sending coins')
|
||||||
}
|
}
|
||||||
|
|||||||
@ -893,7 +893,7 @@
|
|||||||
resolved "https://registry.yarnpkg.com/@types/libsodium-wrappers/-/libsodium-wrappers-0.7.9.tgz#89c3ad2156d5143e64bce86cfeb0045a983aeccc"
|
resolved "https://registry.yarnpkg.com/@types/libsodium-wrappers/-/libsodium-wrappers-0.7.9.tgz#89c3ad2156d5143e64bce86cfeb0045a983aeccc"
|
||||||
integrity sha512-LisgKLlYQk19baQwjkBZZXdJL0KbeTpdEnrAfz5hQACbklCY0gVFnsKUyjfNWF1UQsCSjw93Sj5jSbiO8RPfdw==
|
integrity sha512-LisgKLlYQk19baQwjkBZZXdJL0KbeTpdEnrAfz5hQACbklCY0gVFnsKUyjfNWF1UQsCSjw93Sj5jSbiO8RPfdw==
|
||||||
|
|
||||||
"@types/long@^4.0.0":
|
"@types/long@^4.0.0", "@types/long@^4.0.1":
|
||||||
version "4.0.1"
|
version "4.0.1"
|
||||||
resolved "https://registry.npmjs.org/@types/long/-/long-4.0.1.tgz"
|
resolved "https://registry.npmjs.org/@types/long/-/long-4.0.1.tgz"
|
||||||
integrity sha512-5tXH6Bx/kNGd3MgffdmP4dy2Z+G4eaXw0SE81Tq3BNadtnMR5/ySMzX4SLEzHJzSmPNn4HIdpQsBvXMUykr58w==
|
integrity sha512-5tXH6Bx/kNGd3MgffdmP4dy2Z+G4eaXw0SE81Tq3BNadtnMR5/ySMzX4SLEzHJzSmPNn4HIdpQsBvXMUykr58w==
|
||||||
@ -913,6 +913,11 @@
|
|||||||
resolved "https://registry.npmjs.org/@types/node/-/node-15.12.4.tgz"
|
resolved "https://registry.npmjs.org/@types/node/-/node-15.12.4.tgz"
|
||||||
integrity sha512-zrNj1+yqYF4WskCMOHwN+w9iuD12+dGm0rQ35HLl9/Ouuq52cEtd0CH9qMgrdNmi5ejC1/V7vKEXYubB+65DkA==
|
integrity sha512-zrNj1+yqYF4WskCMOHwN+w9iuD12+dGm0rQ35HLl9/Ouuq52cEtd0CH9qMgrdNmi5ejC1/V7vKEXYubB+65DkA==
|
||||||
|
|
||||||
|
"@types/node@>=13.7.0":
|
||||||
|
version "16.10.3"
|
||||||
|
resolved "https://registry.yarnpkg.com/@types/node/-/node-16.10.3.tgz#7a8f2838603ea314d1d22bb3171d899e15c57bd5"
|
||||||
|
integrity sha512-ho3Ruq+fFnBrZhUYI46n/bV2GjwzSkwuT4dTf0GkuNFmnb8nq4ny2z9JEVemFi6bdEJanHLlYfy9c6FN9B9McQ==
|
||||||
|
|
||||||
"@types/node@^10.1.0":
|
"@types/node@^10.1.0":
|
||||||
version "10.17.60"
|
version "10.17.60"
|
||||||
resolved "https://registry.npmjs.org/@types/node/-/node-10.17.60.tgz"
|
resolved "https://registry.npmjs.org/@types/node/-/node-10.17.60.tgz"
|
||||||
@ -4589,6 +4594,25 @@ prompts@^2.0.1:
|
|||||||
kleur "^3.0.3"
|
kleur "^3.0.3"
|
||||||
sisteransi "^1.0.5"
|
sisteransi "^1.0.5"
|
||||||
|
|
||||||
|
protobufjs@^6.11.2:
|
||||||
|
version "6.11.2"
|
||||||
|
resolved "https://registry.yarnpkg.com/protobufjs/-/protobufjs-6.11.2.tgz#de39fabd4ed32beaa08e9bb1e30d08544c1edf8b"
|
||||||
|
integrity sha512-4BQJoPooKJl2G9j3XftkIXjoC9C0Av2NOrWmbLWT1vH32GcSUHjM0Arra6UfTsVyfMAuFzaLucXn1sadxJydAw==
|
||||||
|
dependencies:
|
||||||
|
"@protobufjs/aspromise" "^1.1.2"
|
||||||
|
"@protobufjs/base64" "^1.1.2"
|
||||||
|
"@protobufjs/codegen" "^2.0.4"
|
||||||
|
"@protobufjs/eventemitter" "^1.1.0"
|
||||||
|
"@protobufjs/fetch" "^1.1.0"
|
||||||
|
"@protobufjs/float" "^1.0.2"
|
||||||
|
"@protobufjs/inquire" "^1.1.0"
|
||||||
|
"@protobufjs/path" "^1.1.2"
|
||||||
|
"@protobufjs/pool" "^1.1.0"
|
||||||
|
"@protobufjs/utf8" "^1.1.0"
|
||||||
|
"@types/long" "^4.0.1"
|
||||||
|
"@types/node" ">=13.7.0"
|
||||||
|
long "^4.0.0"
|
||||||
|
|
||||||
proxy-addr@~2.0.5:
|
proxy-addr@~2.0.5:
|
||||||
version "2.0.7"
|
version "2.0.7"
|
||||||
resolved "https://registry.npmjs.org/proxy-addr/-/proxy-addr-2.0.7.tgz"
|
resolved "https://registry.npmjs.org/proxy-addr/-/proxy-addr-2.0.7.tgz"
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user