diff --git a/backend/package.json b/backend/package.json index c03acf1ed3..7d772ed617 100644 --- a/backend/package.json +++ b/backend/package.json @@ -19,6 +19,7 @@ "locales": "scripts/sort.sh" }, "dependencies": { + "@apollo/protobufjs": "^1.2.7", "apollo-server-express": "^2.25.2", "await-semaphore": "^0.1.3", "axios": "^0.21.1", diff --git a/backend/src/graphql/enum/AddressType.ts b/backend/src/graphql/enum/AddressType.ts new file mode 100644 index 0000000000..d75ad99883 --- /dev/null +++ b/backend/src/graphql/enum/AddressType.ts @@ -0,0 +1,8 @@ +export enum AddressType { + NONE = 0, // if no address was found + HUMAN = 1, + PROJECT = 2, // no creations allowed + SUBACCOUNT = 3, // no creations allowed + CRYPTO_ACCOUNT = 4, // user control his keys, no creations + COMMUNITY_ACCOUNT = 5, // community control keys, creations allowed +} diff --git a/backend/src/graphql/enum/CrossGroupType.ts b/backend/src/graphql/enum/CrossGroupType.ts new file mode 100644 index 0000000000..921ae4eed1 --- /dev/null +++ b/backend/src/graphql/enum/CrossGroupType.ts @@ -0,0 +1,7 @@ +export enum CrossGroupType { + LOCAL = 0, + INBOUND = 1, + OUTBOUND = 2, + // for cross group transaction which haven't a direction like group friend update + CROSS = 3, +} diff --git a/backend/src/proto/GradidoCreation.ts b/backend/src/proto/GradidoCreation.ts new file mode 100644 index 0000000000..90ca85806d --- /dev/null +++ b/backend/src/proto/GradidoCreation.ts @@ -0,0 +1,16 @@ +import { Field, Message } from '@apollo/protobufjs' + +import { TimestampSeconds } from './TimestampSeconds' +import { TransferAmount } from './TransferAmount' + +// need signature from group admin or +// percent of group users another than the receiver +// https://www.npmjs.com/package/@apollo/protobufjs +// eslint-disable-next-line no-use-before-define +export class GradidoCreation extends Message { + @Field.d(1, TransferAmount) + public recipient: TransferAmount + + @Field.d(3, 'TimestampSeconds') + public targetDate: TimestampSeconds +} diff --git a/backend/src/proto/GradidoDeferredTransfer.ts b/backend/src/proto/GradidoDeferredTransfer.ts new file mode 100644 index 0000000000..92e0c0e6ad --- /dev/null +++ b/backend/src/proto/GradidoDeferredTransfer.ts @@ -0,0 +1,31 @@ +import { Field, Message } from '@apollo/protobufjs' + +import { GradidoTransfer } from './GradidoTransfer' +import { Timestamp } from './Timestamp' + +// transaction type for chargeable transactions +// for transaction for people which haven't a account already +// consider using a seed number for key pair generation for recipiant +// using seed as redeem key for claiming transaction, technically make a default Transfer transaction from recipiant address +// seed must be long enough to prevent brute force, maybe base64 encoded +// to own account +// https://www.npmjs.com/package/@apollo/protobufjs +// eslint-disable-next-line no-use-before-define +export class GradidoDeferredTransfer extends Message { + // amount is amount with decay for time span between transaction was received and timeout + // useable amount can be calculated + // recipient address don't need to be registered in blockchain with register address + @Field.d(1, GradidoTransfer) + public transfer: GradidoTransfer + + // if timeout timestamp is reached if it wasn't used, it will be booked back minus decay + // technically on blockchain no additional transaction will be created because how should sign it? + // the decay for amount and the seconds until timeout is lost no matter what happened + // consider is as fee for this service + // rest decay could be transferred back as separate transaction + @Field.d(2, 'Timestamp') + public timeout: Timestamp + + // split for n recipient + // max gradido per recipient? or per transaction with cool down? +} diff --git a/backend/src/proto/GradidoTransaction.ts b/backend/src/proto/GradidoTransaction.ts new file mode 100644 index 0000000000..ca1a59e30b --- /dev/null +++ b/backend/src/proto/GradidoTransaction.ts @@ -0,0 +1,21 @@ +import { Field, Message } from '@apollo/protobufjs' + +import { SignatureMap } from './SignatureMap' + +// https://www.npmjs.com/package/@apollo/protobufjs +// eslint-disable-next-line no-use-before-define +export class GradidoTransaction extends Message { + @Field.d(1, SignatureMap) + public sigMap: SignatureMap + + // inspired by Hedera + // bodyBytes are the payload for signature + // bodyBytes are serialized TransactionBody + @Field.d(2, 'bytes') + public bodyBytes: Buffer + + // if it is a cross group transaction the parent message + // id from outbound transaction or other by cross + @Field.d(3, 'bytes') + public parentMessageId: Buffer +} diff --git a/backend/src/proto/GradidoTransfer.ts b/backend/src/proto/GradidoTransfer.ts new file mode 100644 index 0000000000..203b281ad7 --- /dev/null +++ b/backend/src/proto/GradidoTransfer.ts @@ -0,0 +1,13 @@ +import { Field, Message } from '@apollo/protobufjs' + +import { TransferAmount } from './TransferAmount' + +// https://www.npmjs.com/package/@apollo/protobufjs +// eslint-disable-next-line no-use-before-define +export class GradidoTransfer extends Message { + @Field.d(1, TransferAmount) + public sender: TransferAmount + + @Field.d(2, 'bytes') + public recipient: Buffer +} diff --git a/backend/src/proto/GroupFriendsUpdate.ts b/backend/src/proto/GroupFriendsUpdate.ts new file mode 100644 index 0000000000..64e74a6940 --- /dev/null +++ b/backend/src/proto/GroupFriendsUpdate.ts @@ -0,0 +1,15 @@ +import { Field, Message } from '@apollo/protobufjs' + +// connect group together +// only CrossGroupType CROSS (in TransactionBody) +// https://www.npmjs.com/package/@apollo/protobufjs +// eslint-disable-next-line no-use-before-define +export class GroupFriendsUpdate extends Message { + // if set to true, colors of this both groups are trait as the same + // on creation user get coins still in there color + // on transfer into another group which a connection exist, + // coins will be automatic swapped into user group color coin + // (if fusion between src coin and dst coin is enabled) + @Field.d(1, 'bool') + public colorFusion: boolean +} diff --git a/backend/src/proto/RegisterAddress.ts b/backend/src/proto/RegisterAddress.ts new file mode 100644 index 0000000000..85b8390df6 --- /dev/null +++ b/backend/src/proto/RegisterAddress.ts @@ -0,0 +1,19 @@ +import { Field, Message } from '@apollo/protobufjs' + +import { AddressType } from '@enum/AddressType' + +// https://www.npmjs.com/package/@apollo/protobufjs +// eslint-disable-next-line no-use-before-define +export class RegisterAddress extends Message { + @Field.d(1, 'bytes') + public userPubkey: Buffer + + @Field.d(2, 'AddressType') + public addressType: AddressType + + @Field.d(3, 'bytes') + public nameHash: Buffer + + @Field.d(4, 'bytes') + public subaccountPubkey: Buffer +} diff --git a/backend/src/proto/SignatureMap.ts b/backend/src/proto/SignatureMap.ts new file mode 100644 index 0000000000..e48b0232da --- /dev/null +++ b/backend/src/proto/SignatureMap.ts @@ -0,0 +1,10 @@ +import { Field, Message } from '@apollo/protobufjs' + +import { SignaturePair } from './SignaturePair' + +// https://www.npmjs.com/package/@apollo/protobufjs +// eslint-disable-next-line no-use-before-define +export class SignatureMap extends Message { + @Field.d(1, SignaturePair, 'repeated') + public sigPair: SignaturePair +} diff --git a/backend/src/proto/SignaturePair.ts b/backend/src/proto/SignaturePair.ts new file mode 100644 index 0000000000..07ed4cc558 --- /dev/null +++ b/backend/src/proto/SignaturePair.ts @@ -0,0 +1,11 @@ +import { Field, Message } from '@apollo/protobufjs' + +// https://www.npmjs.com/package/@apollo/protobufjs +// eslint-disable-next-line no-use-before-define +export class SignaturePair extends Message { + @Field.d(1, 'bytes') + public pubKey: Buffer + + @Field.d(2, 'bytes') + public signature: Buffer +} diff --git a/backend/src/proto/Timestamp.ts b/backend/src/proto/Timestamp.ts new file mode 100644 index 0000000000..2d33352721 --- /dev/null +++ b/backend/src/proto/Timestamp.ts @@ -0,0 +1,13 @@ +import { Field, Message } from '@apollo/protobufjs' + +// https://www.npmjs.com/package/@apollo/protobufjs +// eslint-disable-next-line no-use-before-define +export class Timestamp extends Message { + // Number of complete seconds since the start of the epoch + @Field.d(1, 'int64') + public seconds: number + + // Number of nanoseconds since the start of the last second + @Field.d(2, 'int32') + public nanoSeconds: number +} diff --git a/backend/src/proto/TimestampSeconds.ts b/backend/src/proto/TimestampSeconds.ts new file mode 100644 index 0000000000..9a8c5b9dec --- /dev/null +++ b/backend/src/proto/TimestampSeconds.ts @@ -0,0 +1,9 @@ +import { Field, Message } from '@apollo/protobufjs' + +// https://www.npmjs.com/package/@apollo/protobufjs +// eslint-disable-next-line no-use-before-define +export class TimestampSeconds extends Message { + // Number of complete seconds since the start of the epoch + @Field.d(1, 'int64') + public seconds: number +} diff --git a/backend/src/proto/TransactionBody.ts b/backend/src/proto/TransactionBody.ts new file mode 100644 index 0000000000..9a0468d474 --- /dev/null +++ b/backend/src/proto/TransactionBody.ts @@ -0,0 +1,33 @@ +import { Field, Message, OneOf } from '@apollo/protobufjs' + +import { CrossGroupType } from '@/graphql/enum/CrossGroupType' + +import { TimestampSeconds } from './TimestampSeconds' + +// https://www.npmjs.com/package/@apollo/protobufjs +// eslint-disable-next-line no-use-before-define +export class TransactionBody extends Message { + @Field.d(1, 'string') + public memo: string + + @Field.d(2, TimestampSeconds) + public createdAt: TimestampSeconds + + @Field.d(3, 'string') + public versionNumber: string + + @Field.d(4, CrossGroupType) + public type: CrossGroupType + + @Field.d(5, 'string') + public otherGroup: string + + @OneOf.d( + 'gradidoTransfer', + 'gradidoCreation', + 'groupFriendsUpdate', + 'registerAddress', + 'gradidoDeferredTransfer', + ) + public data: string +} diff --git a/backend/src/proto/TransferAmount.ts b/backend/src/proto/TransferAmount.ts new file mode 100644 index 0000000000..28e50500ff --- /dev/null +++ b/backend/src/proto/TransferAmount.ts @@ -0,0 +1,14 @@ +import { Field, Message } from '@apollo/protobufjs' + +// https://www.npmjs.com/package/@apollo/protobufjs +// eslint-disable-next-line no-use-before-define +export class TransferAmount extends Message { + @Field.d(1, 'bytes') + public pubkey: Buffer + + @Field.d(2, 'string') + public amount: string + + @Field.d(3, 'string') + public groupId: string +} diff --git a/backend/tsconfig.json b/backend/tsconfig.json index 146cd41e3d..1e89a73846 100644 --- a/backend/tsconfig.json +++ b/backend/tsconfig.json @@ -52,6 +52,7 @@ "@enum/*": ["src/graphql/enum/*"], "@model/*": ["src/graphql/model/*"], "@union/*": ["src/graphql/union/*"], + "@proto/*": ["src/proto/*"], "@repository/*": ["src/typeorm/repository/*"], "@test/*": ["test/*"], /* external */ diff --git a/backend/yarn.lock b/backend/yarn.lock index 84553d73ea..6357d48840 100644 --- a/backend/yarn.lock +++ b/backend/yarn.lock @@ -21,6 +21,24 @@ "@types/node" "^10.1.0" long "^4.0.0" +"@apollo/protobufjs@^1.2.7": + version "1.2.7" + resolved "https://registry.yarnpkg.com/@apollo/protobufjs/-/protobufjs-1.2.7.tgz#3a8675512817e4a046a897e5f4f16415f16a7d8a" + integrity sha512-Lahx5zntHPZia35myYDBRuF58tlwPskwHc5CWBZC/4bMKB6siTBWwtMrkqXcsNwQiFSzSx5hKdRPUmemrEp3Gg== + 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.0" + long "^4.0.0" + "@apollographql/apollo-tools@^0.5.0": version "0.5.1" resolved "https://registry.yarnpkg.com/@apollographql/apollo-tools/-/apollo-tools-0.5.1.tgz#f0baef739ff7e2fafcb8b98ad29f6ac817e53e32" @@ -3679,7 +3697,7 @@ graceful-fs@^4.1.6, graceful-fs@^4.2.0: integrity sha512-9ByhssR2fPVsNZj478qUUbKfmL0+t5BDVyjShtyZZLiK7ZDAArFFfopyOTj0M05wE2tJPisA4iTnnXl2YoPvOA== "gradido-database@file:../database": - version "1.22.0" + version "1.22.3" dependencies: "@types/uuid" "^8.3.4" cross-env "^7.0.3"